alien.parser: make int[3]* parse, make int[3][4 not parse

db4
Slava Pestov 2010-08-15 02:37:17 -07:00
parent 74af9abf20
commit 25ea734a8e
3 changed files with 12 additions and 4 deletions

View File

@ -33,6 +33,8 @@ TYPEDEF: int MyInt
[ 32 ] [ { int 8 } heap-size ] unit-test
[ ] [ pointer: { int 8 } heap-size pointer: void heap-size assert= ] unit-test
TYPEDEF: char MyChar
[ t ] [ pointer: void c-type pointer: MyChar c-type = ] unit-test

View File

@ -24,12 +24,15 @@ CONSTANT: eleven 11
[ pointer: int** ] [ "int***" parse-c-type ] unit-test
[ pointer: int*** ] [ "int****" parse-c-type ] unit-test
[ { pointer: int 3 } ] [ "int*[3]" parse-c-type ] unit-test
[ "void[3]" parse-c-type ] must-fail
[ { pointer: void 3 } ] [ "void*[3]" parse-c-type ] unit-test
[ pointer: { int 3 } ] [ "int[3]*" parse-c-type ] unit-test
[ c-string ] [ "c-string" parse-c-type ] unit-test
[ char2 ] [ "char2" parse-c-type ] unit-test
[ pointer: char2 ] [ "char2*" parse-c-type ] unit-test
[ "void[3]" parse-c-type ] must-fail
[ "int[3" parse-c-type ] must-fail
[ "int[3][4" parse-c-type ] must-fail
[ "not-word" parse-c-type ] [ error>> no-word-error? ] must-fail-with
] with-file-vocabs

View File

@ -14,15 +14,18 @@ SYMBOL: current-library
DEFER: (parse-c-type)
ERROR: bad-array-type ;
: parse-array-type ( name -- c-type )
"[" split unclip
[ [ "]" ?tail drop parse-word ] map ] [ (parse-c-type) ] bi*
prefix ;
[ [ "]" ?tail [ bad-array-type ] unless parse-word ] map ]
[ (parse-c-type) ]
bi* prefix ;
: (parse-c-type) ( string -- type )
{
{ [ CHAR: ] over member? ] [ parse-array-type ] }
{ [ "*" ?tail ] [ (parse-c-type) <pointer> ] }
{ [ CHAR: ] over member? ] [ parse-array-type ] }
{ [ dup search ] [ parse-c-type-name ] }
[ dup search [ ] [ no-word ] ?if ]
} cond ;