From 1f09f074a1a4a41af1d34d92f943ee8f3dae14ba Mon Sep 17 00:00:00 2001 From: theo <2022xmm3@kuas.ac.jp> Date: Mon, 17 Oct 2022 16:33:46 +0900 Subject: [PATCH] Added constant, initializer and statement processing. --- ccmeta.leg | 100 ++++++++++++++++++++++++------------------ tests-parsimony/001.c | 1 + tests-parsimony/002.c | 12 ++++- tests-parsimony/003.c | 26 ++++++++++- tests-parsimony/004.c | 9 ---- tests-parsimony/tmp.c | 37 ---------------- 6 files changed, 94 insertions(+), 91 deletions(-) delete mode 100644 tests-parsimony/004.c delete mode 100644 tests-parsimony/tmp.c diff --git a/ccmeta.leg b/ccmeta.leg index 2ed9dac..7661708 100644 --- a/ccmeta.leg +++ b/ccmeta.leg @@ -5,6 +5,7 @@ # # Last edited: 2022-09-30 16:23:05 by piumarta on zora-10.local + %{ /* compile: leg -o ccmeta.c ccmeta.leg * cc -o ccmeta ccmeta.c -lgc -lm @@ -29,7 +30,7 @@ _DO(PostDecVariable) _DO(PostDecMember) _DO(PostDecIndex) \ _DO(GetVariable) _DO(GetMember) _DO(SetMember) _DO(GetIndex) _DO(SetIndex) \ _DO(Return) _DO(Break) _DO(Continue) _DO(Throw) _DO(Try) \ - /* _DO(Quasiquote) _DO(Unquote) */ \ + _DO(Quote) /* _DO(Quasiquote) _DO(Unquote) */ \ DO_C_PROTOS() #define META_PROTO_MAX t_Try @@ -54,6 +55,7 @@ DO_PROTOS() #undef _DO } proto_t; + #define SYMBOL_PAYLOAD proto_t prototype; #define DELTA 3 @@ -1347,6 +1349,7 @@ hexQuad = hexadecimalDigit hexadecimalDigit hexadecimalDigit hex constant = characterConstant | floatingConstant | integerConstant + #| META_AT m:meta_exp { $$ = m } @@ -1443,8 +1446,11 @@ sCharSequence = ( escapeSequence | !EOL [^\"\\] )* # 6.5.1 primaryExpression = stringLiteral | constant | id - | META_AT META_LPAREN x:mexp { $$ = eval(globals, x) } - | l:LPAREN x:expression r:RPAREN { $$= new_C_subexpr(l, x, r) } + | META_AT ( META_LCB x:mstmt + | META_OPERATORS META_LPAREN x:mexp + | META_LPAREN x:mexp + ) { $$ = eval(globals, x) } + | l:LPAREN x:expression r:RPAREN { $$= new_C_subexpr(l, x, r) } | l:LPAREN x:compoundStatement r:RPAREN &{gnu} { $$= new_C_subexpr(l, x, r) } # 6.5.2 @@ -1825,6 +1831,7 @@ typedefName = &{ isTypedefName(yytext) } { $$= new_C_id(yytext) } - initializer = l:LCURLY i:initializerList ( c:COMMA | {c=newNullObject()} ) r:RCURLY { $$= new_C_initializer(l, i, c, r) } | assignmentExpression + | META_AT m:meta_exp { $$ = m } initializerList = { listBegin() } ( d:designation { listAppend(d) } @@ -1869,18 +1876,18 @@ labeledStatement = i:id c:COLON # 6.8.2 -compoundStatement = @{ C_scopeBegin() } - l:LCURLY { listBegin() } - ( x:localLabelDeclaration &{gnu} { listAppend(x) } +compoundStatement = @{ C_scopeBegin() } + l:LCURLY { listBegin() } + ( x:localLabelDeclaration &{gnu} { listAppend(x) } )* - ( x:declaration { listAppend(x) } - | x:statement { listAppend(x) } - | x:functionDefinition &{gnu} { listAppend(x) } + ( x:declaration { listAppend(x) } + | x:statement { listAppend(x) } + | x:functionDefinition &{gnu} { listAppend(x) } | !'}' &{ errmsg= "statement expected" } error - )* { x= listEnd() } - r:RCURLY { $$= new_C_compound(l, x, r) } + )* { x= listEnd() } + r:RCURLY { $$= new_C_compound(l, x, r) } @{ C_scopeEnd() } - | &{ C_scopeAbort() } + | &{ C_scopeAbort() } # 6.8.3 @@ -2151,9 +2158,10 @@ _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_LPAREN m:mexp { $$ = m } - | META_IMPORT s:META_STRING ";" { $$ = null; inputStackPush(get(s, String, value)) } - | META_LCB { $$ = null; lang= META } +metaCatch = META_AT ( META_OPERATORS META_LPAREN m:mexp { $$ = m } + | META_LPAREN m:mexp { $$ = m } + | META_IMPORT s:META_STRING ";" { $$ = null; inputStackPush(get(s, String, value)) } + | META_LCB { $$ = null; lang= META } ) | &{ lang == META } - ( META_RCB { $$ = null; lang = C } | s:meta_stmt { $$ = s } @@ -2162,6 +2170,9 @@ metaCatch = META_AT ( META_LPAREN m:mexp { $$ = m } mexp = @{ lang = META } s:meta_exp META_RPAREN @{ lang = C } { $$= s } | @{ lang = C } &{ 0 } +mstmt = @{ lang = META } s:meta_stmt META_RCB @{ lang = C } { $$= s } + | @{ lang = C } &{ 0 } + meta_stmt = s:meta_block { $$ = s } | META_SEMICOLON { $$ = null } | l:META_IDENT p:meta_paramList e:meta_block { $$ = newFunc(l, p, e, null) } @@ -2283,15 +2294,18 @@ meta_prod = l:meta_prefix | META_MODULO r:meta_prefix { l = newBinary(Mod_proto, l, r) } )* { $$ = l } -meta_prefix = META_PLUS n:meta_prefix { $$= n } - | META_NEGATE n:meta_prefix { $$= newUnary(Neg_proto, n) } - | META_TILDE n:meta_prefix { $$= newUnary(Com_proto, 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 n:meta_prefix { $$ = newUnary(Quasiquote_proto, n) } -# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) } - | n:meta_postfix { $$= n } +meta_prefix = META_PLUS n:meta_prefix { $$= n } + | META_NEGATE n:meta_prefix { $$= newUnary(Neg_proto, n) } + | META_TILDE n:meta_prefix { $$= newUnary(Com_proto, 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 META_INITIALIZER i:initializer { $$ = newUnary(Quote_proto ,i) } + | META_BACKTICK META_CONSTANT c:constant { $$ = newUnary(Quote_proto ,c) } + | META_BACKTICK META_STATEMENT c:statement { $$ = newUnary(Quote_proto ,c) } +# | 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_DOT s:META_IDENT a:meta_argumentList { i = newInvoke(i, s, a) } @@ -2318,8 +2332,7 @@ meta_argumentList = META_LPAREN m:meta_makeMap -meta_value = META_BACKTICK - m:meta2c_expr { $$ = m } - | n:META_FLOAT { $$ = newFloat(n) } +meta_value = n:META_FLOAT { $$ = newFloat(n) } | n:meta_integer { $$ = newInteger(n) } | s:meta_string { $$ = newString(s) } | s:meta_symbol { $$ = s } @@ -2329,21 +2342,6 @@ meta_value = META_BACKTICK - m:meta2c_expr { $$ | p:meta_paramList e:meta_block { $$ = newFunc(null, p, e, null) } | META_LPAREN ( i:meta_block | i:meta_exp ) META_RPAREN { $$ = i } -meta2c_statement = m:meta2c_expr { $$ = m } - | 'statement' { $$ = null } - -meta2c_expr = m:meta2c_constant { $$ = m } - | 'expression' { $$ = null } - -meta2c_constant = 'constant' - m:meta2c_value { $$ = m } - -meta2c_value = m:meta_makeMap - ( < [0-9]+ > { map_set(m, intern("__proto__"), intern("C_int")) } { map_set(m, intern("text"), makeString(unescape(yytext))) } { $$ = newMap(m) } - | s:meta2c_string { map_set(m, intern("__proto__"), intern("C_string")) } { map_set(m, intern("text"), s ) } { $$ = newMap(m) } - ) - -meta2c_string = META_DQUOTE < (!META_DQUOTE meta_char)* > META_DQUOTE {$$ = makeString(unescape(yytext)) } - meta_string = s:META_STRING - { $$ = s } META_STRING = META_DQUOTE < (!META_DQUOTE meta_char)* > META_DQUOTE { $$ = makeString(unescape(yytext)) } @@ -2391,6 +2389,17 @@ META_FLOAT = < [-+]* [0-9]+ '.' [0-9]* ('e'[-+]*[0-9]+)? > - { $$ = ma | < [-+]* [0-9]* '.' [0-9]+ ('e'[-+]*[0-9]+)? > - { $$ = makeFloat(strtold(yytext, 0)) } | < [-+]* [0-9]+ ('e'[-+]*[0-9]+) > - { $$ = makeFloat(strtold(yytext, 0)) } +###### META operators ###### + +META_OPERATORS = META_INITIALIZER | META_CONSTANT | META_STATEMENT + +META_INITIALIZER = 'initializer' ![(a-zA-Z0-9_] - +META_CONSTANT = 'constant' ![(a-zA-Z0-9_] - +META_STATEMENT = 'statement' ![(a-zA-Z0-9_] - + + +############################ + #META_FUN = 'fun' ![a-zA-Z0-9_] - #META_SYNTAX = 'syntax' ![a-zA-Z0-9_] - META_VAR = 'var' ![a-zA-Z0-9_] - @@ -2851,6 +2860,10 @@ oop eval(oop scope, oop ast) } return map; } + case t_Quote: { + oop obj = map_get(ast, rhs_symbol); + return obj; + } #if 0 case t_Quasiquote: { oop obj = map_get(ast, rhs_symbol); @@ -4554,6 +4567,9 @@ void outputTree(oop node, int depth) CASE(GetVariable) OUT(key) break; + CASE(Quote) + OUT(rhs); + break; /** TODO * CASE(Quasiquote) * PRINT(Quasiquote); @@ -4681,4 +4697,4 @@ int main(int argc, char **argv) // Local Variables: // indent-tabs-mode: nil -// End: +// End: \ No newline at end of file diff --git a/tests-parsimony/001.c b/tests-parsimony/001.c index ed38619..27ea2f2 100644 --- a/tests-parsimony/001.c +++ b/tests-parsimony/001.c @@ -11,4 +11,5 @@ enum foo { FOO, BAR, BAZ }; char *x[] = @(newCinitializer([newCstring("X"), comma, newCstring("Y")])); int i = @(newCint("42")); +int i = @({ __proto__: Token, text: "14" }); char *foos[] = { "FOO", "BAR", "BAZ" }; diff --git a/tests-parsimony/002.c b/tests-parsimony/002.c index b48741d..b8c2dd8 100644 --- a/tests-parsimony/002.c +++ b/tests-parsimony/002.c @@ -1,7 +1,15 @@ +@{ + saved = `initializer (1,2,"stringy thingy",21<<1); + cons = `constant 2; +} + @(a = `constant 10) +char t = @(`constant 't'); +int i = @(cons); int q = @(`constant 10); +int k = @(`initializer (1,2,"stringy thingy",21<<1)); -int w = @(a); -char *text=@(`constant "text"); +int i = @(saved); +int w = @(a); diff --git a/tests-parsimony/003.c b/tests-parsimony/003.c index 70c77e3..122abd4 100644 --- a/tests-parsimony/003.c +++ b/tests-parsimony/003.c @@ -1 +1,25 @@ -int i = @({ __proto__: Token, text: "14" }); +@{ + nTimes = `constant 10; + 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);;} + + +@{if (useForLoop) { x = forLoop;} else { x = whileLoop; } } + + +int main() +{ + @(x); + + @(`statement for (int i = 0; i < @(nTimes); ++i) printf("%d\n", i);); + + @{if (useForLoop) {forLoop;} else {whileLoop;}}; // Impossible to modify useForLoop in a function + + return 0; +} \ No newline at end of file diff --git a/tests-parsimony/004.c b/tests-parsimony/004.c deleted file mode 100644 index d24dca4..0000000 --- a/tests-parsimony/004.c +++ /dev/null @@ -1,9 +0,0 @@ -@{if (1) print("yo");} - -int main(){ - if (1) { - return 0; - } else { - return 1; - } -} diff --git a/tests-parsimony/tmp.c b/tests-parsimony/tmp.c deleted file mode 100644 index b035305..0000000 --- a/tests-parsimony/tmp.c +++ /dev/null @@ -1,37 +0,0 @@ -@{ - - // File used only for rule creation help - - newObject(type, fields) { fields.__proto__ = type; fields } - newCint(text) { newObject(C_int, { text: text }) } - newCstring(text) { newObject(C_string, { text: "\""+text+"\"" }) } - newCinitializer(list) { newObject(C_initializer, { leftCurly: lcurly, initList: list, rightCurly: rcurly }) } - //newCif(list) { newObject(C_if, {ifTok: iftok, lParen: lpar, condition: , oop rParen, oop consequent }) } - - newCexpression() {} - - //newCcondition() { newObject(C_conditional, {logicalOrExpression, question, expression, colon, conditionalExpression}) } - - //newCbinary() { newObject() } - - //newCcast() { newObject(C_cast, { lParen: lpar, typeName, rParen: rpar, expression }) } - - //newCdeclaration() { newObject(C_declaration, {specifiers, declarators, semicolon: semicolon }) } - - newCstatement() {} - - - - newToken(text) { { __proto__: Token, text: text } } - comma = newToken(","); - semicolon = newToken(";"); - lcurly = newToken("{"); - rcurly = newToken("}"); - lpar = newToken("("); - rpar = newToken(")"); - iftok = newToken("if"); - } - - -char *x[] = @(newCinitializer([newCstring("X"), comma, newCstring("Y")])); -int i = @(newCint("42"));