diff --git a/ccmeta.leg b/ccmeta.leg index 0d85bf1..343ab24 100644 --- a/ccmeta.leg +++ b/ccmeta.leg @@ -1299,6 +1299,21 @@ int getProtoNumber(oop obj) { return get(map_get(map_get(obj, __proto___symbol), __name___symbol), Symbol, prototype); } +int isReturnable(oop obj, oop obj2) { + printf("%i", getProtoNumber(obj)); + if (getType(obj) == 6 && (getProtoNumber(obj) < 8 || getProtoNumber(obj) > 13) ) { + return 0; + } + if (getType(obj2) != 6) + return 0; + int objNB = getProtoNumber(obj2); + if (objNB <= 65) + return 0; + return 1; +} + +oop outputProgram= 0; + %} @@ -1444,8 +1459,7 @@ sCharSequence = ( escapeSequence | !EOL [^\"\\] )* #" # 6.5.1 primaryExpression = stringLiteral | constant | id - | META_AT ( META_LCB x:mstmts { $$ = eval(globals, x) } - | META_OPERATORS META_LPAREN x:mvalue { $$ = eval(globals, x) } + | META_AT ( META_LCB x:mstmts { $$ = x } | x:mvalue { $$ = eval(globals, x) } ) | l:LPAREN x:expression r:RPAREN { $$= new_C_subexpr(l, x, r) } @@ -1602,8 +1616,7 @@ declaration = @{ C_declarationBegin() } d:initDeclaratorListOpt t:SEMI { $$= new_C_declaration(s, d, t) } @{ C_declarationEnd() } - | - &{ C_declarationAbort() } + | &{ C_declarationAbort() } ) declarationSpecifiers = @{ int specified= 0 } { listBegin() } @@ -1935,7 +1948,7 @@ functionDefinition = @{ C_declarationBegin() } l:declarationListOpt c:compoundStatement { $$= new_C_functionDef(s, d, l, c) } @{ C_declarationEnd() } - | &{ C_declarationAbort() } + | &{ C_declarationAbort() } functionDeclarationSpecifiers = @{ int specified= 0 } { listBegin() } ( s:functionStorageClassSpecifier { listAppend(s) } @@ -2094,22 +2107,22 @@ BXOR = '^' !'=' { $$= newToken("^" ) } - BXOR_ASSIGN = "^=" { $$= newToken("^=" ) } - ALIGNOF = '__alignof__' !IDREST { $$= newToken("__alignof__" ) } - - | '__alignof' !IDREST { $$= newToken("__alignof" ) } - + | '__alignof' !IDREST { $$= newToken("__alignof" ) } - -ASM = 'asm' !IDREST { $$= newToken("asm" ) } - +ASM = 'asm' !IDREST { $$= newToken("asm" ) } - | '__asm' !IDREST { $$= newToken("__asm" ) } - | '__asm__' !IDREST { $$= newToken("__asm__" ) } - -ATTRIBUTE = '__attribute__' !IDREST { $$= newToken("__attribute__") } - +ATTRIBUTE = '__attribute__' !IDREST { $$= newToken("__attribute__") } - AUTO = 'auto' !IDREST { $$= newToken("auto" ) } - BOOL = '_Bool' !IDREST { $$= newToken("_Bool" ) } - BREAK = 'break' !IDREST { $$= newToken("break" ) } - CASE = 'case' !IDREST { $$= newToken("case" ) } - CHAR = 'char' !IDREST { $$= newToken("char" ) } - COMPLEX = '_Complex' !IDREST { $$= newToken("_Complex" ) } - - | '__complex__' !IDREST &{gnu} { $$= newToken("__complex__" ) } - + | '__complex__' !IDREST &{gnu} { $$= newToken("__complex__" ) } - CONST = 'const' !IDREST { $$= newToken("const" ) } - - | '__const' !IDREST { $$= newToken("__const" ) } - + | '__const' !IDREST { $$= newToken("__const" ) } - CONTINUE = 'continue' !IDREST { $$= newToken("continue" ) } - DEFAULT = 'default' !IDREST { $$= newToken("default" ) } - DO = 'do' !IDREST { $$= newToken("do" ) } - @@ -2122,7 +2135,7 @@ FOR = 'for' !IDREST { $$= newToken("for" GOTO = 'goto' !IDREST { $$= newToken("goto" ) } - IF = 'if' !IDREST { $$= newToken("if" ) } - INLINE = 'inline' !IDREST { $$= newToken("inline" ) } - - | '__inline__' !IDREST &{gnu} { $$= newToken("__inline__" ) } - + | '__inline__' !IDREST &{gnu} { $$= newToken("__inline__" ) } - INT = 'int' !IDREST { $$= newToken("int" ) } - LONG = 'long' !IDREST { $$= newToken("long" ) } - REGISTER = 'register' !IDREST { $$= newToken("register" ) } - @@ -2136,7 +2149,7 @@ STRUCT = 'struct' !IDREST { $$= newToken("struct" SWITCH = 'switch' !IDREST { $$= newToken("switch" ) } - TYPEDEF = 'typedef' !IDREST { $$= newToken("typedef" ) } - TYPEOF = 'typeof' !IDREST { $$= newToken("typeof" ) } - - | '__typeof__' !IDREST { $$= newToken("__typeof__" ) } - + | '__typeof__' !IDREST { $$= newToken("__typeof__" ) } - UNION = 'union' !IDREST { $$= newToken("union" ) } - UNSIGNED = 'unsigned' !IDREST { $$= newToken("unsigned" ) } - VOID = 'void' !IDREST { $$= newToken("void" ) } - @@ -2157,18 +2170,19 @@ _FLOAT128 = '_Float128' !IDREST &{gnu} { $$= newToken("_Float1 # the semicolon has to be explicit with no space eaten afterwards to prevent the # input buffer from moving past it before redirecting input from the imported file -metaCatch = META_AT ( META_OPERATORS META_LPAREN m:mvalue { eval(globals, m) } - | META_LPAREN m:mvalue { eval(globals, m) } - | META_LCB m:mstmts - | META_IMPORT s:META_STRING ";" { inputStackPush(get(s, String, value)) } +metaCatch = META_AT ( m:mvalue { map_append(outputProgram, eval(globals, m)); } + | META_LCB m:mstmts { map_append(outputProgram, m); } + | META_IMPORT s:META_STRING ";" { inputStackPush(get(s, String, value)) } ) -mvalue = i:META_IDENT { $$ = newGetVariable(i) } - | META_LPAREN ( i:meta_block | i:meta_exp ) META_RPAREN { $$ = i } +mvalue = i:META_IDENT { $$ = newGetVariable(i) } + | META_LPAREN i:MO_OPERATION META_RPAREN { $$ = i } + | META_LPAREN ( i:meta_block | i:meta_exp ) META_RPAREN { eval(globals, i); $$ = null } + +mstmts = { listBegin() } ( s:meta_stmt { eval(globals, s) } )* META_RCB { $$ = listEnd() } -mstmts = ( s:meta_stmt { eval(globals, s) })* META_RCB - | &{ 0 } +# oop ret = eval(globals, s); if (isReturnable(s, ret)) listAppend(ret) meta_stmt = s:meta_block { $$ = s } | META_SEMICOLON { $$ = null } @@ -2180,6 +2194,7 @@ meta_stmt = s:meta_block | META_FOR META_LPAREN i:meta_ident META_IN e:meta_exp META_RPAREN s:meta_stmt { $$ = newForIn(i, e, s) } | META_FOR META_LPAREN i:meta_stmt c:meta_stmt u:meta_exp META_RPAREN s:meta_stmt { $$ = newFor(i, c, u, s) } | s:meta_switch { $$ = s } + | META_AT m:mvalue { listAppend(eval(globals, m))} #right place ??? | META_RETURN e:meta_exp { $$ = newReturn(e) } | META_RETURN { $$ = newReturn(null) } | META_BREAK { $$ = newBreak() } @@ -2195,21 +2210,21 @@ meta_block = META_LCB m:meta_makeMap ) ? META_RCB { $$ = newBlock(m) } -meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp { $$ = newDeclaration(l, e) } +meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp { $$ = newDeclaration(l, e) } # | META_SYNTAX l:META_IDENT p:meta_paramList q:META_IDENT e:meta_block { $$ = (map_append(p, q), newFunc(l, p, e, makeInteger(2))) } # | META_SYNTAX p:meta_paramList q:META_IDENT e:meta_block { $$ = (map_append(p, q), newFunc(null, p, e, makeInteger(2))) } # | META_SYNTAX l:META_IDENT p:meta_paramList e:meta_block { $$ = newFunc(l, p, e, makeInteger(1)) } # | META_SYNTAX p:meta_paramList e:meta_block { $$ = newFunc(null, p, e, makeInteger(1)) } - | l:META_IDENT o:meta_assignOp e:meta_exp { $$ = newAssign(Assign_proto, l, o, e) } - | l:meta_postfix META_DOT i:META_IDENT o:meta_assignOp e:meta_exp { $$ = newSetMap(SetMember_proto, l, i, o, e) } - | l:meta_postfix META_LBRAC i:meta_exp META_RBRAC o:meta_assignOp e:meta_exp { $$ = newSetMap(SetIndex_proto, l, i, o, e) } - | l:meta_syntax2 a:meta_argumentList s:meta_block { $$ = (map_append(a, s), apply(globals, globals, l, a, a)) } - | c:meta_cond { $$ = c } + | l:META_IDENT o:meta_assignOp e:meta_exp { $$ = newAssign(Assign_proto, l, o, e) } + | l:meta_postfix META_DOT i:META_IDENT o:meta_assignOp e:meta_exp { $$ = newSetMap(SetMember_proto, l, i, o, e) } + | l:meta_postfix META_LBRAC i:meta_exp META_RBRAC o:meta_assignOp e:meta_exp { $$ = newSetMap(SetIndex_proto, l, i, o, e) } + | l:meta_syntax2 a:meta_argumentList s:meta_block { $$ = (map_append(a, s), apply(globals, globals, l, a, a)) } + | c:meta_cond { $$ = c } -meta_ident = l:META_IDENT { $$ = l } -# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) } +meta_ident = l:META_IDENT { $$ = l } +# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) } meta_syntax2 = < [a-zA-Z_][a-zA-Z0-9_]* > &{ null != getSyntaxId(2, intern(yytext)) } -- { $$ = getSyntaxId(2, intern(yytext)) } @@ -2218,54 +2233,54 @@ meta_try = META_TRY t:meta_stmt i:me ( META_CATCH META_LPAREN i:META_IDENT META_RPAREN c:meta_stmt ) ? ( META_FINALLY f:meta_stmt ) ? { $$ = newTry(t, i, c, f) } -meta_null = { $$ = null } +meta_null = { $$ = null } -meta_assignOp = META_ASSIGN { $$= null } - | META_ASSIGNADD { $$= Add_symbol } - | META_ASSIGNSUB { $$= Sub_symbol } - | META_ASSIGNMUL { $$= Mul_symbol } - | META_ASSIGNDIV { $$= Div_symbol } - | META_ASSIGNMOD { $$= Mod_symbol } - | META_ASSIGNBITOR { $$= Bitor_symbol } - | META_ASSIGNBITXOR { $$= Bitxor_symbol } - | META_ASSIGNBITAND { $$= Bitand_symbol } - | META_ASSIGNSHLEFT { $$= Shleft_symbol } - | META_ASSIGNSHRIGHT { $$= Shright_symbol } +meta_assignOp = META_ASSIGN { $$= null } + | META_ASSIGNADD { $$= Add_symbol } + | META_ASSIGNSUB { $$= Sub_symbol } + | META_ASSIGNMUL { $$= Mul_symbol } + | META_ASSIGNDIV { $$= Div_symbol } + | META_ASSIGNMOD { $$= Mod_symbol } + | META_ASSIGNBITOR { $$= Bitor_symbol } + | META_ASSIGNBITXOR { $$= Bitxor_symbol } + | META_ASSIGNBITAND { $$= Bitand_symbol } + | META_ASSIGNSHLEFT { $$= Shleft_symbol } + | META_ASSIGNSHRIGHT { $$= Shright_symbol } meta_switch = META_SWITCH META_LPAREN e:meta_exp META_RPAREN META_LCB statements:meta_makeMap labels:meta_makeMap - ( META_CASE l:meta_exp META_COLON { map_set(labels, eval(globals, l), makeInteger(map_size(statements))) } - | META_DEFAULT META_COLON { map_set(labels, __default___symbol, makeInteger(map_size(statements))) } - | s:meta_stmt { map_append(statements, s) } + ( META_CASE l:meta_exp META_COLON { map_set(labels, eval(globals, l), makeInteger(map_size(statements))) } + | META_DEFAULT META_COLON { map_set(labels, __default___symbol, makeInteger(map_size(statements))) } + | s:meta_stmt { map_append(statements, s) } )* - META_RCB { $$= newSwitch(e, labels, statements) } + META_RCB { $$= newSwitch(e, labels, statements) } -meta_cond = c:meta_logor META_QUERY t:meta_exp META_COLON f:meta_cond { $$ = newIf(c, t, f) } +meta_cond = c:meta_logor META_QUERY t:meta_exp META_COLON f:meta_cond { $$ = newIf(c, t, f) } | meta_logor meta_logor = l:meta_logand - ( META_LOGOR r:meta_logand { l = newBinary(Logor_proto, l, r) } + ( META_LOGOR r:meta_logand { l = newBinary(Logor_proto, l, r) } )* { $$ = l } meta_logand = l:meta_bitor - ( META_LOGAND r:meta_bitor { l = newBinary(Logand_proto, l, r) } + ( META_LOGAND r:meta_bitor { l = newBinary(Logand_proto, l, r) } )* { $$ = l } meta_bitor = l:meta_bitxor - ( META_BITOR r:meta_bitxor { l = newBinary(Bitor_proto, l, r) } + ( META_BITOR r:meta_bitxor { l = newBinary(Bitor_proto, l, r) } )* { $$ = l } meta_bitxor = l:meta_bitand - ( META_BITXOR r:meta_bitand { l = newBinary(Bitxor_proto, l, r) } + ( META_BITXOR r:meta_bitand { l = newBinary(Bitxor_proto, l, r) } )* { $$ = l } meta_bitand = l:meta_eq - ( META_BITAND r:meta_eq { l = newBinary(Bitand_proto, l, r) } + ( META_BITAND r:meta_eq { l = newBinary(Bitand_proto, l, r) } )* { $$ = l } meta_eq = l:meta_ineq - ( META_EQUAL r:meta_ineq { l = newBinary(Equal_proto, l, r) } - | META_NOTEQ r:meta_ineq { l = newBinary(Noteq_proto, l, r) } + ( META_EQUAL r:meta_ineq { l = newBinary(Equal_proto, l, r) } + | META_NOTEQ r:meta_ineq { l = newBinary(Noteq_proto, l, r) } )* { $$ = l } meta_ineq = l:meta_shift @@ -2276,19 +2291,19 @@ meta_ineq = l:meta_shift )* { $$ = l } meta_shift = l:meta_sum - ( META_SHLEFT r:meta_sum { l = newBinary(Shleft_proto, l, r) } - | META_SHRIGHT r:meta_sum { l = newBinary(Shright_proto, l, r) } + ( META_SHLEFT r:meta_sum { l = newBinary(Shleft_proto, l, r) } + | META_SHRIGHT r:meta_sum { l = newBinary(Shright_proto, l, r) } )* { $$ = l } meta_sum = l:meta_prod - ( META_PLUS r:meta_prod { l = newBinary(Add_proto, l, r) } - | META_MINUS r:meta_prod { l = newBinary(Sub_proto, l, r) } + ( META_PLUS r:meta_prod { l = newBinary(Add_proto, l, r) } + | META_MINUS r:meta_prod { l = newBinary(Sub_proto, l, r) } )* { $$ = l } meta_prod = l:meta_prefix - ( META_MULTI r:meta_prefix { l = newBinary(Mul_proto, l, r) } - | META_DIVIDE r:meta_prefix { l = newBinary(Div_proto, l, r) } - | META_MODULO r:meta_prefix { l = newBinary(Mod_proto, l, r) } + ( META_MULTI r:meta_prefix { l = newBinary(Mul_proto, l, r) } + | META_DIVIDE r:meta_prefix { l = newBinary(Div_proto, l, r) } + | META_MODULO r:meta_prefix { l = newBinary(Mod_proto, l, r) } )* { $$ = l } meta_prefix = META_PLUS n:meta_prefix { $$= n } @@ -2297,15 +2312,12 @@ meta_prefix = META_PLUS n:meta_prefix { $$= n } | META_PLING n:meta_prefix { $$= newUnary(Not_proto, n) } | META_PLUSPLUS n:meta_prefix { $$= newPreIncrement(n) } | META_MINUSMINUS n:meta_prefix { $$= newPreDecrement(n) } - | META_BACKTICK MO_INITIALIZER i:initializer { $$ = newUnary(Quote_proto ,i) } - | META_BACKTICK MO_CONSTANT c:constant { $$ = newUnary(Quote_proto ,c) } - | META_BACKTICK MO_STATEMENT c:statement { $$ = newUnary(Quote_proto ,c) } - | META_BACKTICK MO_INTEGER i:integerConstant { $$ = newUnary(Quote_proto ,i) } + | m:MO_OPERATION { $$= m } # | META_BACKTICK n:meta_prefix { $$ = newUnary(Quasiquote_proto, n) } # | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) } | n:meta_postfix { $$= n } -meta_postfix = i:meta_value +meta_postfix = i:meta_value ( META_DOT s:META_IDENT a:meta_argumentList { i = newInvoke(i, s, a) } | META_DOT s:META_IDENT !meta_assignOp { i = newGetMap(GetMember_proto, i, s) } | META_LBRAC p:meta_exp META_RBRAC !meta_assignOp { i = newGetMap(GetIndex_proto, i, p) } @@ -2389,12 +2401,20 @@ META_FLOAT = < [-+]* [0-9]+ '.' [0-9]* ('e'[-+]*[0-9]+)? > { $$ = mak ###### META operators ###### -META_OPERATORS = MO_INITIALIZER | MO_CONSTANT | MO_STATEMENT | MO_INTEGER +MO_OPERATION = META_BACKTICK ( MO_INITIALIZER i:initializer { $$ = newUnary(Quote_proto ,i) } + | MO_CONSTANT c:constant { $$ = newUnary(Quote_proto ,c) } + | MO_STATEMENT c:statement { $$ = newUnary(Quote_proto ,c) } + | MO_INTEGER i:integerConstant { $$ = newUnary(Quote_proto ,i) } + | MO_DECLARATION i:declaration { $$ = newUnary(Quote_proto ,i) } + ) + +META_OPERATORS = MO_INITIALIZER | MO_CONSTANT | MO_STATEMENT | MO_INTEGER | MO_DECLARATION MO_INITIALIZER = 'initializer' ![(a-zA-Z0-9_] -- MO_CONSTANT = 'constant' ![(a-zA-Z0-9_] -- MO_STATEMENT = 'statement' ![(a-zA-Z0-9_] -- -MO_INTEGER = 'integer' ![(a-zA-Z0-9_] -- +MO_INTEGER = 'integer' ![(a-zA-Z0-9_] -- +MO_DECLARATION = 'declaration' ![(a-zA-Z0-9_] -- ############################ @@ -3500,7 +3520,6 @@ oop evalArgs(oop scope, oop args) } oop AST= NULL; -oop outputProgram= 0; void outputNode(oop node); void printTree(oop element, language id); diff --git a/tests-parsimony/003.c b/tests-parsimony/003.c index ffb34a7..19b89c3 100644 --- a/tests-parsimony/003.c +++ b/tests-parsimony/003.c @@ -1,12 +1,12 @@ @{ nTimes = `constant 10; - forLoop = `statement for (int i = 0; i < 10; ++i) printf("%d\n", i); ; + forLoop = (`statement for (int i = 0; i < 10; ++i) printf("%d\n", i);); whileLoop = `statement while (i < 10) { printf("%d\n", i); ++il; }; useForLoop = 1; x = null; } -@{forLoop = `statement for (int i = 0; i < 10; ++i) printf("%d\n", i);;} +@{forLoop = `statement for (int i = 0; i < 10; ++i) printf("%d\n", i);;} @{ if (useForLoop) { @@ -18,7 +18,7 @@ int main() { - @(x); + @x; @(`statement for (int i = 0; i < @(nTimes); ++i) printf("%d\n", i);); @@ -32,9 +32,9 @@ int main() @{ useForLoop = 0; if (useForLoop) { - forLoop; + @forLoop; } else { - whileLoop; + @whileLoop; } }; diff --git a/tests-parsimony/004.c b/tests-parsimony/004.c index ec291b3..bca8718 100644 --- a/tests-parsimony/004.c +++ b/tests-parsimony/004.c @@ -6,17 +6,19 @@ } -int x = @{`initializer 2 * @(num);}; +int x = @{ + @(`initializer 2 * @num); + }; int main() { int i = @(`initializer 2 * @num); int y = @test; int a = @{ - num = `integer 21; - test = 2; - `initializer @(num); - }; + num = `integer 21; + test = 2; + @(`initializer @num); + }; return 0; } \ No newline at end of file diff --git a/tests-parsimony/005.c b/tests-parsimony/005.c new file mode 100644 index 0000000..67e402c --- /dev/null +++ b/tests-parsimony/005.c @@ -0,0 +1,16 @@ +@{ + @(`declaration int a = 5;); + @(`declaration int b = 2;); +} + +int main() { + @{ + a = (`declaration int a = 2;); + @(`declaration int c = 5;); + b = 5; + @(`declaration int d = 2;); + @(`statement for(int i =0; i < 10; i++) { + a = 2; + }); + }; +} \ No newline at end of file diff --git a/tests-parsimony/err_001.c b/tests-parsimony/err_001.c deleted file mode 100644 index bf0fb1a..0000000 --- a/tests-parsimony/err_001.c +++ /dev/null @@ -1,5 +0,0 @@ -@{ - num = `integer 21; - test = 2; - `initializer @(num); -} \ No newline at end of file diff --git a/tests-parsimony/err_002.c b/tests-parsimony/err_002.c deleted file mode 100644 index bb4b4bd..0000000 --- a/tests-parsimony/err_002.c +++ /dev/null @@ -1,8 +0,0 @@ -@{ - num = `integer 21; - t = `initializer 2 * @(num); -} - -int x = @{t;}; - - diff --git a/tests-parsimony/tmp.c b/tests-parsimony/tmp.c new file mode 100644 index 0000000..3139032 --- /dev/null +++ b/tests-parsimony/tmp.c @@ -0,0 +1,28 @@ +@{ + (`declaration int a = 5;); + a = (`declaration int a = 5;); + @(`declaration int a = 5;); +}; + +@a; +@(t = `integer 2) +@(`declaration int abc = @t;); + +int main() { + @a; + int b = 0; + @{ + @(`declaration int c = 0;); + x = (`declaration int d = 5;); + @x; + if (1) { + if (1) { + x = (`declaration int d = 5;); + @x; + } + } + + }; + int e = 0; + return 0; +} \ No newline at end of file