From 2fdc3233dd051f6c76d03dcfd4616985dc3244de Mon Sep 17 00:00:00 2001 From: Theo Souchon Date: Mon, 27 Feb 2023 15:51:36 +0900 Subject: [PATCH] Add comments to some functions and parsefrom fix (hack one) --- src/ccmeta.leg | 69 +++- src/parsimonyLibrary/boot.mc | 312 ++++++++++-------- src/parsimonyLibrary/dynamicObjectExtDecl.mc | 4 + src/parsimonyLibrary/dynamicObjectParsFrom.mc | 8 +- src/tests/tests-parsimony/realObjectED.out | 4 + .../tests-parsimony/toFix/realObjectPF.c | 2 - 6 files changed, 237 insertions(+), 162 deletions(-) diff --git a/src/ccmeta.leg b/src/ccmeta.leg index a3c28f7..87e33c0 100644 --- a/src/ccmeta.leg +++ b/src/ccmeta.leg @@ -1343,6 +1343,9 @@ language printLang = C; oop outputProgram= 0; +/// @brief Check if everyExternalDeclaration is define in parsimony and if so call it with the s parameter +/// @param s +/// @return Return the parameter s oop everyExternalDeclaration(oop s) { oop eed = map_get(globals, intern("everyExternalDeclaration")); @@ -1352,11 +1355,14 @@ oop everyExternalDeclaration(oop s) return s; } +/// @brief Check if the object correspond to what is expected +/// @param id +/// @param s +/// @return Return the parameter s oop ensure(int id, oop s) { if (is(Map, s)) { oop protoSymbol = map_get(s, __proto___symbol); - // int protoNumber = get(map_get(map_get(s, __proto___symbol), __name___symbol), Symbol, prototype); switch(id) { case t_C_id: { if (map_get(protoSymbol, __name___symbol) != map_get(C_id_proto, __name___symbol)) { // map_get for tree copy because __name__ : C_id != __name__ : C_id @@ -1374,6 +1380,9 @@ oop ensure(int id, oop s) return s; } +/// @brief Make a copy of the tree in input +/// @param obj +/// @return The copy of the object oop clone(oop obj) { switch(getType(obj)) { @@ -1397,6 +1406,9 @@ oop clone(oop obj) return obj; } +/// @brief Make a deep copy of the tree in input +/// @param obj +/// @return The deep copy of the object oop treeCopy(oop obj) { switch(getType(obj)) { @@ -1423,6 +1435,10 @@ oop treeCopy(oop obj) return obj; } +/// @brief Evaluate the remaining Parsimony code not evaluated +/// @param scope +/// @param obj +/// @return The same object with everything evaluated inside oop treeCopyUnquoting(oop scope, oop obj) { switch(getType(obj)) { @@ -1498,9 +1514,6 @@ hexQuad = hexadecimalDigit hexadecimalDigit hexadecimalDigit hex # constant rule -# constant = META_AT @{puts("-----------------------------------------")} -# < [a-zA-Z][a-zA-Z0-9]* > -# x:@{ printf("<%s>\n", yytext); exit(0) } { $$ = x } constant = characterConstant | floatingConstant | integerConstant @@ -1598,10 +1611,11 @@ sCharSequence = ( escapeSequence | !EOL [^\"\\] )* #" # primaryExpression rule -primaryExpression = stringLiteral | constant | id - | META_AT ( META_LCB mstmts - | mvalue - ) +primaryExpression = META_AT ( META_LCB mstmts + #| < [a-zA-Z][a-zA-Z0-9]* > @{ printf("<%s>\n", yytext); exit(0) } + | mvalue + ) + | stringLiteral | constant | id | 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) } @@ -2312,6 +2326,7 @@ mvalue = ( i:META_IDENT { i= newGetVariable(i) } ( a:meta_argumentList { ) | META_LPAREN ( i:meta_exp ) META_RPAREN ) { $$= eval(globals, i) } + #) { i = eval(globals, i); puts("mvalue-yylval");println(i);$$= i } mstmts = ( s:eval_stmt )* META_RCB { $$= s } # return the last stmt @@ -3684,6 +3699,7 @@ void readEvalPrint(oop scope, char *fileName) int jbt= sigsetjmp(jbs->jb, 0); if (0 == jbt) { while (yyparse(&ctx)) { + // println(yylval); if (!yylval) { // EOF assert(top == inputStack); break; @@ -3774,19 +3790,37 @@ oop prim_parseFrom(oop scope, oop params) if (!is(String, name)) return null; char *s = get(name, String, value); assert(inputStack); - struct _yycontext *yy = inputStack->yy; - if (!strcmp(s, "declaration" )) return yylval = yyparsefrom(yy, yy_declaration ) ? __ : null ; - else if (!strcmp(s, "expression" )) return yylval = yyparsefrom(yy, yy_expression ) ? __ : null ; - else if (!strcmp(s, "statement" )) return yylval = yyparsefrom(yy, yy_statement ) ? __ : null ; - else if (!strcmp(s, "id" )) return yylval = yyparsefrom(yy, yy_id ) ? __ : null ; - else if (!strcmp(s, "func" )) return yylval = yyparsefrom(yy, yy_functionDefinition) ? __ : null ; - else if (!strcmp(s, "error" )) return yylval = yyparsefrom(yy, yy_error) ? __ : null ; + struct _yycontext *zz = inputStack->yy; + struct _yycontext ctx, *yy = &ctx; + memcpy(&ctx, zz, sizeof(struct _yycontext)); + yy->__thunkslen= YY_STACK_SIZE; + yy->__thunks= (yythunk *)YY_MALLOC(yy, sizeof(yythunk) * yy->__thunkslen); + yy->__valslen= YY_STACK_SIZE; + yy->__vals= (YYSTYPE *)YY_MALLOC(yy, sizeof(YYSTYPE) * yy->__valslen); + yy->__thunkpos= 0; + int res; + if (!strcmp(s, "declaration" )) res = yyparsefrom(yy, yy_declaration ); + else if (!strcmp(s, "expression" )) res = yyparsefrom(yy, yy_expression ); + else if (!strcmp(s, "statement" )) res = yyparsefrom(yy, yy_statement ); + else if (!strcmp(s, "id" )) res = yyparsefrom(yy, yy_id ); + else if (!strcmp(s, "func" )) res = yyparsefrom(yy, yy_functionDefinition); + else if (!strcmp(s, "error" )) res = yyparsefrom(yy, yy_error ); else { fprintf(stderr, "parseFrom: %s not implemented", s); exit(1); } - printf("%s expected\n", s); - return null; + oop value; + if (res) { + value = __; + zz->__buf= yy->__buf; + zz->__buflen= yy->__buflen; + zz->__pos= yy->__pos; + zz->__limit= yy->__limit; + } else { + printf("%s expected\n", s); + value = null; + } + return value; } #include @@ -3810,7 +3844,6 @@ void outputText(char *text) void outputNode(oop node) { - if (!node) return; switch (getType(node)) { case Undefined: diff --git a/src/parsimonyLibrary/boot.mc b/src/parsimonyLibrary/boot.mc index 2af2c20..6dd1ad6 100644 --- a/src/parsimonyLibrary/boot.mc +++ b/src/parsimonyLibrary/boot.mc @@ -1,161 +1,193 @@ @{ - println() { - apply(print, __arguments__); - print("\n"); - __arguments__; - } - - __requires__ = {}; - - require(__fileName__) { - if (__requires__[__fileName__] != null) { - return __requires__[__fileName__] - } - import(__fileName__); - __requires__[__fileName__]= scope(); - return scope(); - } - - millis() { - microseconds() / 1000; - } - - //------------ creation C structure ------------// - - newId(x, comm) { - if (comm == null) { - { identifier: x, __proto__: C_id }; - } else { - { identifier: x, comment: { text : comm, __proto__: Comment }, __proto__: C_id }; + // create the function println + println(){ + apply(print, __arguments__); + print("\n"); + __arguments__; } - } - newText(x) { { value: x, __proto__: C_string }; } - - newComment(com) { { text : com, __proto__: Comment }; } - - //--- Token ---// + __requires__ = {}; + + // Import the file + require(__fileName__) + { + if (__requires__[__fileName__] != null) + { + return __requires__[__fileName__] + } + import(__fileName__); + __requires__[__fileName__] = scope(); + return scope(); + } + + // Get the current time in milliseconds + millis() + { + microseconds() / 1000; + } - newToken(txt, com) { - if (com == null) return { text: txt, comment: newComment(""), __proto__: Token }; - { text: txt, comment: newComment(com), __proto__: Token }; - } + // Get the last element of s + last(s) + { + s[length(s) - 1]; + } - newSemicolon(com) { newToken(";", com) } + // adding the element e at the end of the list l + append(s, e) + { + s[length(s)] = e; + } - newComma() { newToken(",", " "); } + // adding the element e at the end of the list l + push(s, e) + { + append(s, e); + } - newStar(com) { newToken("*", com); } + // remove the last element of the list s or remove the element e if not null + pop(s, e) + { + if (e == null) + { + e = length(s) - 1 + } + ret = s[e]; + t = {}; + for (i in s) + { + if (i == e) + continue; + append(t, s[i]); + } + s = treeCopy(t); + ret; + } - newBinary(com) { newToken("=", com); } - - //--- Declaration ---// + // add or don't change an element in a dictionnary + intern(s, e) + { + lo = 0; + hi = length(s) - 1; + while (lo <= hi) + { + mid = (lo + hi) / 2; + if (e > s[mid]) + lo = mid + 1; + else if (e < s[mid]) + hi = mid - 1; + else + return e; + } + for (i = length(s); i > lo; i = i - 1) + { + s[i] = s[i - 1]; + } + return s[lo] = e; + } - newStruct() { - { - declarators: null, - specifier: null, - semicolon: newSemicolon("\n"), - __proto__: C_declaration - } // TO FIX - } + // merge the two list s1 and s2 + fusion(s1, s2) + { + for (i in s2) + { + append(s1, s2[i]); + } + return s1; + } - newEnum(x) { { attributeL: null, expression: null, name: newId(x), __proto__: C_enum }; } + // If g then we add a filter on the application of the function f to each element of seq + map(f, seq, g) + { + out = {}; + if (g) + { + for (i in seq) + { + e = seq[i]; + if (g(e)) + { + e = f(e); + } + append(out, e); + } + } + else + { + for (i in seq) + { + append(out, f(seq[i])); + } + } + out; + } - newFunction() {} + notToken(x){x.__proto__ != Token} + + // Select element e of s if the the result of the application of f on e is true + select(f, s) + { + out = {}; + for (i in s) + { + e = s[i]; + if (f(e)) + { + append(out, e); + } + } + out; + } - //-------------------- end ---------------------// + // Select element e of s if the the result of the application of f on e is false + reject(f, s) + { + out = {}; + for (i in s) + { + e = s[i]; + if (!f(e)) + { + append(out, e); + } + } + out; + } - // Get the last element of s - last(s) { - s[length(s)-1]; - } + //------------ C structure ------------// + + newId(x, comm) + { + if (comm == null) + { + {identifier : x, __proto__ : C_id}; + } + else + { + {identifier : x, comment : {text : comm, __proto__ : Comment}, __proto__ : C_id}; + } + } - // adding the element e at the end of the list l - append(s, e) { - s[length(s)] = e; - } + newText(x) { {value : x, __proto__ : C_string}; } - push(s, e) { - append(s, e); - } + newComment(com) { {text : com, __proto__ : Comment}; } - pop(s, e) { - if (e == null) { - e = length(s) - 1 - } - ret = s[e]; - t = {}; - for (i in s) { - if (i == e) continue; - append(t, s[i]); - } - s = treeCopy(t); - ret; - } - - // add or don't change an element in a dictionnary - intern(s, e) { - lo = 0; - hi = length(s) - 1; - while (lo <= hi) { - mid = (lo+hi)/2; - if (e > s[mid]) lo = mid + 1; - else if (e < s[mid]) hi = mid - 1; - else return e; + newToken(txt, com) + { + if (com == null) return {text : txt, comment : newComment(""), __proto__ : Token}; + {text : txt, comment : newComment(com), __proto__ : Token}; } - for (i = length(s); i > lo; i = i - 1) { - s[i] = s[i-1]; - } - return s[lo] = e; - } - fusion(s1, s2) { - for (i in s2) { - append(s1, s2[i]); - } - return s1; - } - - // If g then we add a filter on the application of the function f to each element of seq - map(f, seq, g) { - out = {}; - if (g) { - for (i in seq) { - e = seq[i]; - if (g(e)) { e = f(e); } - append(out, e); - } - } else { - for (i in seq) { - append(out, f(seq[i])); - } - } - out; - } + newSemicolon(com){newToken(";", com)} - notToken(x) { x.__proto__ != Token } + newComma() { newToken(",", " "); } - // Select element e of s if the the result of the application of f on e is true - select(f, s) { - out = {}; - for (i in s) { - e = s[i]; - if (f(e)) { append(out, e); } - } - out; - } - - // Select element e of s if the the result of the application of f on e is false - reject(f, s) { - out = {}; - for (i in s) { - e = s[i]; - if (!f(e)) { append(out, e); } - } - out; - } - - - nil; + newStar(com) { newToken("*", com); } + + newBinary(com) { newToken("=", com); } + + newEnum(x) { {attributeL : null, expression : null, name : newId(x), __proto__ : C_enum}; } + + //-------------------- end ---------------------// + + nil; } diff --git a/src/parsimonyLibrary/dynamicObjectExtDecl.mc b/src/parsimonyLibrary/dynamicObjectExtDecl.mc index 3b1f8ba..98f8b5c 100644 --- a/src/parsimonyLibrary/dynamicObjectExtDecl.mc +++ b/src/parsimonyLibrary/dynamicObjectExtDecl.mc @@ -1,3 +1,7 @@ +// This example serves to illustrate the possibility +// of adding the object aspect to C +// using the everyExternalDeclaration function. + #include #include #include diff --git a/src/parsimonyLibrary/dynamicObjectParsFrom.mc b/src/parsimonyLibrary/dynamicObjectParsFrom.mc index d18dcae..23b96ef 100644 --- a/src/parsimonyLibrary/dynamicObjectParsFrom.mc +++ b/src/parsimonyLibrary/dynamicObjectParsFrom.mc @@ -1,3 +1,7 @@ +// This example serves to illustrate the possibility +// of adding the object aspect to C +// using the parseFrom primitive. + #include #include #include @@ -166,7 +170,7 @@ typedef struct __oop *oop; tmp[0] = makeln(`declaration struct @@(newId(id, " ")) *self = (struct @@(newId(id, " ")) *) __self;, 1); decl.compoundS.expression = treeCopy(fusion(tmp, decl.compoundS.expression)); program.objects.function = "none"; - nil; + decl; } constructor() { @@ -185,7 +189,7 @@ typedef struct __oop *oop; append(decl.compoundS.expression, makeln(`statement return (struct __oop *) self;)); decl.compoundS.expression = fusion(t, decl.compoundS.expression); program.objects.function = "none"; - nil; + decl; } // TODO diff --git a/src/tests/tests-parsimony/realObjectED.out b/src/tests/tests-parsimony/realObjectED.out index 6b22552..5af0df4 100644 --- a/src/tests/tests-parsimony/realObjectED.out +++ b/src/tests/tests-parsimony/realObjectED.out @@ -1,3 +1,7 @@ +// This example serves to illustrate the possibility +// of adding the object aspect to C +// using the everyExternalDeclaration function. + #include #include #include diff --git a/src/tests/tests-parsimony/toFix/realObjectPF.c b/src/tests/tests-parsimony/toFix/realObjectPF.c index a1ccd69..4e7b573 100644 --- a/src/tests/tests-parsimony/toFix/realObjectPF.c +++ b/src/tests/tests-parsimony/toFix/realObjectPF.c @@ -26,7 +26,5 @@ int main() { oop p = newPoint(3, 4); - @send2 magnitude(); - return 0; } \ No newline at end of file