From f412bfa6c3c0364f03e19fed454322aa0a42e14f Mon Sep 17 00:00:00 2001 From: Nathan R Date: Fri, 9 Jul 2021 16:54:38 +0200 Subject: [PATCH] String + new C constructors, TODO : isInteger --- ccmeta-test.ref | 6 ++ ccmeta-test.txt | 6 ++ ccmeta.leg | 211 +++++++++++++++++++++++++++++------------------- 3 files changed, 139 insertions(+), 84 deletions(-) diff --git a/ccmeta-test.ref b/ccmeta-test.ref index 8aeb91a..a22a147 100644 --- a/ccmeta-test.ref +++ b/ccmeta-test.ref @@ -18,6 +18,12 @@ 'bonjour tout le monde' 'je m appelle Nathan et je suis né en 1998' 'enchanté !' + +/* Test id */ +identifier +newId +id_3 + /* multi * line * comment diff --git a/ccmeta-test.txt b/ccmeta-test.txt index ec66916..6459219 100644 --- a/ccmeta-test.txt +++ b/ccmeta-test.txt @@ -18,6 +18,12 @@ 'bonjour tout le monde'; 'je m appelle Nathan et je suis né en 1998'; 'enchanté !'; +; +/* Test id */ +identifier; +newId; +id_3; +; /* multi * line * comment diff --git a/ccmeta.leg b/ccmeta.leg index 15c2c7f..2f17324 100644 --- a/ccmeta.leg +++ b/ccmeta.leg @@ -31,7 +31,8 @@ _DO(Return) _DO(Break) _DO(Continue) _DO(Throw) _DO(Try) \ /* _DO(Quasiquote) _DO(Unquote) */ \ _DO(Comment) _DO(Token) \ - _DO(C_if) _DO(C_int) _DO(C_float) _DO(C_char) + _DO(C_if) _DO(C_int) _DO(C_float) _DO(C_char) _DO(C_id) _DO(C_while) _DO(C_do) _DO(C_for) \ + _DO(C_binary) typedef enum { t_UNDEFINED=0, @@ -82,7 +83,9 @@ oop globals= 0; _DO(try) _DO(catch) _DO(finally) _DO(exception) \ _DO(__line__) _DO(__file__) \ _DO(comment) \ - _DO(text) _DO(if) _DO(lparen) _DO(rparen) _DO(else) + _DO(text) _DO(if) _DO(lparen) _DO(rparen) _DO(else) _DO(identifier) _DO(semicolon) _DO(while) \ + _DO(do) _DO(for) _DO(initExpr) _DO(condExpr) _DO(incrExpr) _DO(firstSemi) _DO(secondSemi) \ + _DO(binary) #define _DO(NAME) oop NAME##_symbol; @@ -256,48 +259,40 @@ oop new_C_if(oop ifTok, oop lParen, oop condition, oop rParen, oop consequent, o return obj; } -oop newIf(oop cond, oop cons, oop alt) -{ - oop obj = newObject(If_proto); - map_set(obj, condition_symbol, cond); - map_set(obj, consequent_symbol, cons); - map_set(obj, alternate_symbol, alt); - return obj; -} - -oop newWhile(oop cond, oop body) -{ - oop obj = newObject(While_proto); - map_set(obj, condition_symbol, cond); - map_set(obj, body_symbol, body); - return obj; -} - -oop newDo(oop body, oop cond) -{ - oop obj= newObject(Do_proto); - map_set(obj, body_symbol, body); - map_set(obj, condition_symbol, cond); - return obj; -} - -oop newFor(oop init, oop cond, oop step, oop body) -{ - oop obj= newObject(For_proto); - map_set(obj, initialise_symbol, init); - map_set(obj, condition_symbol, cond); - map_set(obj, update_symbol, step); - map_set(obj, body_symbol, body); - return obj; +oop new_C_while (oop whileTok, oop lParen, oop expression, oop rParen, oop statement) { + oop object = newObject(C_while_proto); + map_set(object, while_symbol, whileTok); + map_set(object, lparen_symbol, lParen); + map_set(object, expression_symbol, expression); + map_set(object, rparen_symbol, rParen); + map_set(object, statements_symbol, statement); + return object; } -oop newForIn(oop name, oop expression, oop body) -{ - oop obj= newObject(ForIn_proto); - map_set(obj, name_symbol, name); - map_set(obj, expression_symbol, expression); - map_set(obj, body_symbol, body); - return obj; +oop new_C_do (oop doTok, oop statement, oop whileTok, oop lParen, oop expression, oop rParen, oop semicolon) { + oop object = newObject(C_do_proto); + map_set(object, do_symbol, doTok); + map_set(object, statements_symbol, statement); + map_set(object, while_symbol, whileTok); + map_set(object, lparen_symbol, lParen); + map_set(object, expression_symbol, expression); + map_set(object, rparen_symbol, rParen); + map_set(object, semicolon_symbol, semicolon); + return object; +} + +oop new_C_for(oop forTok, oop lParen, oop initExpr, oop semicolon1, oop condExpr, oop semicolon2, oop incrExpr, oop rParen, oop statement) { + oop object = newObject(C_for_proto); + map_set(object, for_symbol, forTok); + map_set(object, lparen_symbol, lParen); + map_set(object, initExpr_symbol, initExpr); + map_set(object, firstSemi_symbol, semicolon1); + map_set(object, condExpr_symbol, condExpr); + map_set(object, secondSemi_symbol, semicolon2); + map_set(object, incrExpr_symbol, incrExpr); + map_set(object, rparen_symbol, rParen); + map_set(object, statements_symbol, statement); + return object; } oop newSwitch(oop expression, oop labels, oop statements) @@ -486,12 +481,12 @@ oop newUnary(oop proto, oop rhs) return obj; } -oop newBinary(oop proto, oop lhs, oop rhs) -{ - oop obj = newObject(proto); - map_set(obj, lhs_symbol, lhs); - map_set(obj, rhs_symbol, rhs); - return obj; +oop new_C_binary(oop lhs, oop binary, oop rhs) { + oop object = newObject(C_binary_proto); + map_set(object, lhs_symbol, lhs); + map_set(object, binary_symbol, binary); + map_set(object, rhs_symbol, rhs); + return object; } oop newAssign(oop proto, oop lhs, oop operator, oop rhs) @@ -610,6 +605,13 @@ oop newTry(oop try, oop exception, oop catch, oop finally) return obj; } +oop new_C_id(char* id) { + oop object = newObject(C_id_proto); + map_set(object, identifier_symbol, intern(id)); + return object; +} + + #define YY_INPUT(buf, result, max_size) \ { \ int yyc= feof(inputStack->file) ? EOF : getc(inputStack->file); \ @@ -674,18 +676,18 @@ start = externalDeclaration error = EOL* < (!EOL .)* EOL* (!EOL .)* > &{ error(yytext), 1 } -#|### A.1.3 Identifiers -#| -#|# 6.4.2.1 -#| -#|idOpt = id | {$$=0} -#| -#|id = { $$= newId(yytext) } - -#|ID = &{ !intern(yytext)->isKeyword } -#| -#|name = { $$= newId(yytext) } - -#|NAME = IDFIRST IDREST* -#| +### A.1.3 Identifiers + +# 6.4.2.1 + +idOpt = id #| TODO : End parsing | {$$=0} + +id = { $$= new_C_id(yytext) } - +ID = #| TODO : &{ !intern(yytext)->isKeyword } + +name = { $$= new_C_id(yytext) } - +NAME = IDFIRST IDREST* + IDFIRST = [a-zA-Z_] | universalCharacterName | '$' &{gnu} IDREST = IDFIRST | [0-9] @@ -941,8 +943,8 @@ hexadecimalEscapeSequence = '\\x' hexadecimalDigit+ #|# 6.5.17 #| #|expression = l:assignmentExpression -#| ( o:COMMA r:assignmentExpression { l= newBinary(l, o, r) } -#| )* { $$= l } +#| ( o:COMMA r:assignmentExpression { l= newBinary(l, o, r) } +#| )* { $$= l } #| #|expressionOpt = expression | { $$= 0 } #| @@ -952,32 +954,33 @@ hexadecimalEscapeSequence = '\\x' hexadecimalDigit+ #| #|# 6.7 #| -#|declaration = @{ declarationBegin() } -#| ( s:declarationSpecifiers -#| d:initDeclaratorListOpt -#| t:SEMI { $$= newDeclaration(s, d, t) } -#| @{ declarationEnd() } -#| | &{ declarationAbort() } -#| ) -#| -#|declarationSpecifiers = @{ int specified= 0 } { listBegin() } -#| ( s:storageClassSpecifier { listAppend(s) } -#| | s:typeSpecifier @{ specified++ } { listAppend(s) } -#| | s:typedefName &{ !specified++ } { listAppend(s) } -#| | s:typeQualifier { listAppend(s) } -#| | s:functionSpecifier { listAppend(s) } -#| )+ { $$= listEnd() } -#| | &{gnu} { $$= 0 } +#|declaration = @{ declarationBegin() } +#| ( s:declarationSpecifiers +#| d:initDeclaratorListOpt +#| t:SEMI { $$= newDeclaration(s, d, t) } +#| @{ declarationEnd() } +#| | +#| &{ declarationAbort() } +#| ) +#| +#|declarationSpecifiers = @{ int specified= 0 } { listBegin() } +#| ( s:storageClassSpecifier { listAppend(s) } +#| | s:typeSpecifier @{ specified++ } { listAppend(s) } +#| | s:typedefName &{ !specified++ } { listAppend(s) } +#| | s:typeQualifier { listAppend(s) } +#| | s:functionSpecifier { listAppend(s) } +#| )+ { $$= listEnd() } +#| | &{gnu} { $$= 0 } #| #|initDeclaratorListOpt = initDeclaratorList | { $$= 0 } #| #|initDeclaratorList = d:initDeclarator { listWith(d) } -#| ( c:COMMA d:initDeclarator { listAppend2(c, d) } -#| )* { $$= listEnd() } +#| ( c:COMMA d:initDeclarator { listAppend2(c, d) } +#| )* { $$= listEnd() } #| #|initDeclarator = d:declarator -#| ( a:ASSIGN i:initializer { d= newBinary(d, a, i) } -#| )? { $$= d } +#| ( a:ASSIGN i:initializer { d= newBinary(d, a, i) } +#| )? { $$= d } #| #|# 6.7.1 #| @@ -1264,9 +1267,10 @@ hexadecimalEscapeSequence = '\\x' hexadecimalDigit+ #| #|translationUnit = externalDeclaration+ #| -externalDeclaration = { yylval= newComment(yytext); } +externalDeclaration = { yylval = newComment(yytext); } | ( SEMI &{gnu} - | c:constant { yylval = c;} #| TODO + | c:constant { yylval = c; } #| TODO + | i:idOpt { yylval = i; } #| TODO #| | declaration #| | functionDefinition #| | meta @@ -2606,6 +2610,9 @@ void outputNode(oop node) case Float: outputFloat(getFloat(node)); return; + case Symbol: + outputText(get(node, Symbol, name)); + return; default: fprintf(stderr, "\noutputNode: unknown node type %i\n", node->type); abort(); @@ -2630,6 +2637,10 @@ void outputNode(oop node) break; case t_C_char: outputNode(map_get(node, value_symbol)); + break; + case t_C_id: + outputNode(map_get(node, identifier_symbol)); + break; case t_C_if: outputNode(map_get(node, if_symbol)); outputNode(map_get(node, lparen_symbol)); @@ -2639,6 +2650,38 @@ void outputNode(oop node) outputNode(map_get(node, else_symbol)); // null if no else clause outputNode(map_get(node, alternate_symbol)); // null if no else clause break; + case t_C_while: + outputNode(map_get(node, while_symbol)); + outputNode(map_get(node, lparen_symbol)); + outputNode(map_get(node, expression_symbol)); + outputNode(map_get(node, rparen_symbol)); + outputNode(map_get(node, statements_symbol)); + break; + case t_C_do: + outputNode(map_get(node, do_symbol)); + outputNode(map_get(node, statements_symbol)); + outputNode(map_get(node, while_symbol)); + outputNode(map_get(node, lparen_symbol)); + outputNode(map_get(node, expression_symbol)); + outputNode(map_get(node, rparen_symbol)); + outputNode(map_get(node, semicolon_symbol)); + break; + case t_C_for: + outputNode(map_get(node, for_symbol)); + outputNode(map_get(node, lparen_symbol)); + outputNode(map_get(node, initExpr_symbol)); + outputNode(map_get(node, firstSemi_symbol)); + outputNode(map_get(node, condExpr_symbol)); + outputNode(map_get(node, secondSemi_symbol)); + outputNode(map_get(node, incrExpr_symbol)); + outputNode(map_get(node, rparen_symbol)); + outputNode(map_get(node, statements_symbol)); + break; + case t_C_binary: + outputNode(map_get(node, lhs_symbol)); + outputNode(map_get(node, binary_symbol)); + outputNode(map_get(node, rhs_symbol)); + break; } #if 0 while (orow < node->row) { printf("\n"); ++orow; ocol= 0; }