From 8532be1d4f5894a5b5676e8140547453877a7b5f Mon Sep 17 00:00:00 2001 From: Sebeck10 Date: Mon, 31 Oct 2022 17:22:34 +0900 Subject: [PATCH] append part almost done & beginning of map function --- .gitignore | 1 + ccmeta.leg | 81 +++++++++++++++++++++++++------------------ tests-parsimony/001.c | 6 +++- tests-parsimony/002.c | 7 ++-- tests-parsimony/003.c | 28 ++------------- tests-parsimony/004.c | 6 ++-- tests-parsimony/005.c | 11 +++--- tests-parsimony/006.c | 15 ++++---- tests-parsimony/007.c | 14 ++++++++ tests-parsimony/tmp.c | 28 ++++++--------- 10 files changed, 103 insertions(+), 94 deletions(-) create mode 100644 tests-parsimony/007.c diff --git a/.gitignore b/.gitignore index 5b45798..52ec767 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ tests-c/*.otags tests-c/*.tags tests-c/*.expected */*.out +*tmp* ccmeta ccmeta.c diff --git a/ccmeta.leg b/ccmeta.leg index 946673b..a4c70f0 100644 --- a/ccmeta.leg +++ b/ccmeta.leg @@ -30,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(Quote) /* _DO(Quasiquote) _DO(Unquote) */ \ + _DO(Quote) _DO(Append) /* _DO(Quasiquote) _DO(Unquote) */ \ DO_C_PROTOS() #define META_PROTO_MAX t_Try @@ -1295,24 +1295,24 @@ typedef enum { language printLang = C; -int getProtoNumber(oop obj) { - return get(map_get(map_get(obj, __proto___symbol), __name___symbol), Symbol, prototype); -} +oop outputProgram= 0; -int isReturnable(oop obj, oop obj2) { - printf("%i", getProtoNumber(obj)); - if (getType(obj) == 6 && (getProtoNumber(obj) < 8 || getProtoNumber(obj) > 13) ) { - return 0; +oop mapFunction(oop func, oop args) { + println(args); + oop mapValue = makeMap(); + oop argsObj = eval(globals, args); + int size = map_size(argsObj); + for(int i = 0; i < size; i++) { + oop oopInt = makeInteger(i); + oop obj = map_get(map_get(args, value_symbol), oopInt); + obj = newCall(func, obj); + map_append(mapValue, obj); } - if (getType(obj2) != 6) - return 0; - int objNB = getProtoNumber(obj2); - if (objNB <= 65) - return 0; - return 1; + map_set(args, value_symbol, mapValue); + println(args); + return args; } -oop outputProgram= 0; %} @@ -1459,9 +1459,8 @@ sCharSequence = ( escapeSequence | !EOL [^\"\\] )* #" # 6.5.1 primaryExpression = stringLiteral | constant | id - | META_AT ( META_LCB x:mstmts { $$ = x } - | x:mvalue { $$ = eval(globals, x) } - | x:mexp { $$ = eval(globals, x) } + | META_AT ( META_LCB x:mstmts + | x:mvalue #{ $$ = 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) } @@ -2171,23 +2170,26 @@ _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 ( m:mvalue { map_append(outputProgram, eval(globals, m)); } - | m:mexp { map_append(outputProgram, m) } +metaCatch = META_AT ( m:mvalue { map_append(outputProgram, 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:MO_OPERATION META_RPAREN { $$ = i } - -mexp = { listBegin() } META_LPAREN ( i:meta_block | i:meta_exp ) META_RPAREN { $$ = eval(globals, listEnd()) } +mvalue = { listBegin() } ( i:META_IDENT { listAppend(eval(globals, newGetVariable(i))) } + | META_LPAREN ( i:MO_OPERATION + i:meta_block + i:meta_exp + ) META_RPAREN { listAppend(eval(globals, i)) } + ) { $$ = listEnd() } -mstmts = { listBegin() } ( s:meta_stmt { eval(globals, s) } )* META_RCB { $$ = listEnd() } - -# oop ret = eval(globals, s); if (isReturnable(s, ret)) listAppend(ret) +mstmts = { listBegin() } ( s:meta_stmt { eval(globals, s) } )* + ( (META_NIL META_SEMICOLON )? META_RCB { listEnd(); $$ = null } + | META_DAT META_SEMICOLON META_RCB { $$ = listEnd() } + ) meta_stmt = s:meta_block { $$ = s } + | META_DAT META_DOT META_APPEND META_LPAREN s:meta_exp META_RPAREN META_SEMICOLON { $$ = newUnary(Append_proto, s) } | META_SEMICOLON { $$ = null } | l:META_IDENT p:meta_paramList e:meta_block { $$ = newFunc(l, p, e, null) } | META_IF META_LPAREN c:meta_exp META_RPAREN t:meta_stmt META_ELSE f:meta_stmt { $$ = newIf(c, t, f ) } @@ -2197,7 +2199,6 @@ 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() } @@ -2207,13 +2208,14 @@ meta_stmt = s:meta_block | e:meta_exp META_SEMICOLON { $$ = e } meta_block = META_LCB m:meta_makeMap - ( s:meta_stmt { eval(globals, s); map_append(m, s) } + ( s:meta_stmt { map_append(m, s) } ) * - ( s:meta_exp { eval(globals, s); map_append(m, s) } + ( s:meta_exp { map_append(m, s) } ) ? META_RCB { $$ = newBlock(m) } meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp { $$ = newDeclaration(l, e) } + | META_FUNCMAP m:meta_funcMap {$$ = m} # | 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)) } @@ -2225,7 +2227,6 @@ meta_exp = META_VAR l:meta_ident META_ASSIGN e:meta_exp | c:meta_cond { $$ = c } - meta_ident = l:META_IDENT { $$ = l } # | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) } @@ -2324,11 +2325,13 @@ 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) } - | a:meta_argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a) } + | a:meta_argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a); } | META_PLUSPLUS { i = newPostIncrement(i) } | META_MINUSMINUS { i = newPostDecrement(i) } ) * { $$ = i } +meta_funcMap = META_LPAREN i:meta_value META_COMMA e:meta_exp META_RPAREN { mapFunction(i, e) } + meta_paramList = META_LPAREN m:meta_makeMap ( i:META_IDENT { map_append(m, i) } ( META_COMMA i:META_IDENT { map_append(m, i) } @@ -2384,7 +2387,7 @@ meta_makeMap = { $$ = makeMap() } meta_key = META_IDENT | meta_integer meta_keyword = META_SWITCH | META_CASE | META_DEFAULT | META_DO | META_FOR | META_IN | META_WHILE | META_IF | META_ELSE | META_NULL | META_RETURN | META_BREAK | META_CONTINUE - | META_THROW | META_TRY | META_CATCH | META_FINALLY + | META_THROW | META_TRY | META_CATCH | META_FINALLY | META_NIL # | META_SYNTAX META_IDENT = !meta_keyword < [a-zA-Z_][a-zA-Z0-9_]* > -- { $$ = intern(yytext) } @@ -2417,11 +2420,16 @@ 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_DECLARATION = 'declaration' ![(a-zA-Z0-9_] -- +MO_DECLARATION = 'declaration' ![(a-zA-Z0-9_] -- ############################ +META_NIL = 'nil' ![a-zA-Z0-9_] -- +META_FUNCMAP = 'map' ![a-zA-Z0-9_] -- +META_DAT = '@@' ![a-zA-Z0-9_] -- +META_APPEND = 'append' ![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_] -- @@ -2886,6 +2894,11 @@ oop eval(oop scope, oop ast) oop obj = map_get(ast, rhs_symbol); return obj; } + case t_Append: { + oop obj = eval(scope, map_get(ast, rhs_symbol)); + listAppend(obj); + return null; + } #if 0 case t_Quasiquote: { oop obj = map_get(ast, rhs_symbol); diff --git a/tests-parsimony/001.c b/tests-parsimony/001.c index 27ea2f2..09373be 100644 --- a/tests-parsimony/001.c +++ b/tests-parsimony/001.c @@ -1,4 +1,5 @@ -@{ newObject(type, fields) { fields.__proto__ = type; fields } +@{ + newObject(type, fields) { fields.__proto__ = type; fields } newCint(text) { newObject(C_int, { text: text }) } newCstring(text) { newObject(C_string, { text: "\""+text+"\"" }) } newCinitializer(list) { { __proto__: C_initializer, leftCurly: lcurly, initList: list, rightCurly: rcurly } } @@ -6,8 +7,11 @@ comma = newToken(","); lcurly = newToken("{"); rcurly = newToken("}"); + nil; } +@(`declaration int a = 0;); + enum foo { FOO, BAR, BAZ }; char *x[] = @(newCinitializer([newCstring("X"), comma, newCstring("Y")])); int i = @(newCint("42")); diff --git a/tests-parsimony/002.c b/tests-parsimony/002.c index eee98a9..8fae184 100644 --- a/tests-parsimony/002.c +++ b/tests-parsimony/002.c @@ -2,11 +2,12 @@ saved = `initializer (1,2,"string",21<<1); cons = `constant 2; two = `integer 2; + @@.append(`declaration char *test = "foo";); + @@; } - +@(print("6 outputs : \n")) @{a = `constant 10;} - -char t = @(`constant 't'); +char t = @(`constant '@a'); int i = @cons; int q = @(`constant 10); int k = @(`initializer (1,2,"stringy thingy",21<<1)); diff --git a/tests-parsimony/003.c b/tests-parsimony/003.c index 19b89c3..cf1b674 100644 --- a/tests-parsimony/003.c +++ b/tests-parsimony/003.c @@ -6,37 +6,15 @@ 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; + @@.append(forLoop); } else { - whileLoop; + @@.append(whileLoop); } + @@; }; - @{ - useForLoop = 0; - if (useForLoop) { - @forLoop; - } else { - @whileLoop; - } - }; - return 0; } \ No newline at end of file diff --git a/tests-parsimony/004.c b/tests-parsimony/004.c index bca8718..126917a 100644 --- a/tests-parsimony/004.c +++ b/tests-parsimony/004.c @@ -7,7 +7,8 @@ int x = @{ - @(`initializer 2 * @num); + @@.append(`initializer 2 * @num); + @@; }; int main() { @@ -17,7 +18,8 @@ int main() { int a = @{ num = `integer 21; test = 2; - @(`initializer @num); + @@.append(`initializer @num); + @@; }; return 0; diff --git a/tests-parsimony/005.c b/tests-parsimony/005.c index 67e402c..65fe4d0 100644 --- a/tests-parsimony/005.c +++ b/tests-parsimony/005.c @@ -1,16 +1,17 @@ @{ - @(`declaration int a = 5;); - @(`declaration int b = 2;); + @@.append(`declaration int a = 5;); + @@.append(`declaration int b = 2;); } int main() { @{ a = (`declaration int a = 2;); - @(`declaration int c = 5;); + @@.append(`declaration int c = 5;); b = 5; - @(`declaration int d = 2;); - @(`statement for(int i =0; i < 10; i++) { + @@.append(`declaration int d = 2;); + @@.append(`statement for(int i =0; i < 10; i++) { a = 2; }); + @@; }; } \ No newline at end of file diff --git a/tests-parsimony/006.c b/tests-parsimony/006.c index 3139032..5cd9438 100644 --- a/tests-parsimony/006.c +++ b/tests-parsimony/006.c @@ -1,27 +1,28 @@ @{ - (`declaration int a = 5;); + @@.append(`declaration int a = 5;); a = (`declaration int a = 5;); - @(`declaration int a = 5;); + @@.append(`declaration int a = 5;); + @@; }; @a; -@(t = `integer 2) +@{t = `integer 2;} @(`declaration int abc = @t;); int main() { @a; int b = 0; @{ - @(`declaration int c = 0;); + @@.append(`declaration int c = 0;); x = (`declaration int d = 5;); - @x; + @@.append(x); if (1) { if (1) { x = (`declaration int d = 5;); - @x; + @@.append(x); } } - + @@; }; int e = 0; return 0; diff --git a/tests-parsimony/007.c b/tests-parsimony/007.c new file mode 100644 index 0000000..c040c8e --- /dev/null +++ b/tests-parsimony/007.c @@ -0,0 +1,14 @@ +@{ + @@.append(`declaration int a = 0;); + a = (`declaration int b = 0;); + @@.append(a); + if (1) { + @@.append(`declaration int c = 0;); + } else { + @@.append(`declaration int c = 1;); + } + @@; +}; + +@a; +@(`declaration int d = 12;); \ No newline at end of file diff --git a/tests-parsimony/tmp.c b/tests-parsimony/tmp.c index 791070d..b57cf60 100644 --- a/tests-parsimony/tmp.c +++ b/tests-parsimony/tmp.c @@ -1,18 +1,12 @@ - - -int main() { - @({ - @(`declaration int a = 5;); - t = 2; - print(t); - @(`declaration int a = 5;); - x = (`declaration int d = 5;); - @x; - if (1) { - if (1) { - x = (`declaration int d = 5;); - @x; - } +@{ + test(foo) { + foo * 2; } - }); -} \ No newline at end of file + // print("after test decl\n"); + // a = [1, 2, 3]; + // print(a[3]); + // print("\n"); +} + +@{print(map(test, [1, 2, 3]));} +// @{print(test(1));}