From: Mike D. Lowis Date: Mon, 26 Mar 2012 17:59:09 +0000 (-0400) Subject: Made the syntax more permissive of commas in lists, vectors, maps, and macros. This... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=aeed31b156956bb0f77b17867211c96b886ccade;p=archive%2Fdlang.git Made the syntax more permissive of commas in lists, vectors, maps, and macros. This also allows us to define a list of length 1 by placing a comma immediately before the closing paren --- diff --git a/example.dl b/example.dl index 4480afa..8c248ea 100644 --- a/example.dl +++ b/example.dl @@ -20,7 +20,7 @@ foo = $some_symbol foo = { $foo : 1 + 1, "stuff" : 2 + 2, - $bar : 3 + 3 + $bar : 3 + 3, } print( foo[$bar] ) @@ -36,6 +36,7 @@ foo = [1,2,3,4,5][2] # List foo = () +foo = (1,) foo = (1,2,3) foo = foo[1] foo = (1,2,3,4,5)[2] @@ -53,7 +54,7 @@ foo = ({|a,b| a + b })(1,2) @ if [ (E B B) : exec_if($1, $2, $3), - (E B) : exec_if($1, $2) + (E B) : exec_if($1, $2), ] if (1 < 2) diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index 2c49bc9..1c4ab10 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -421,29 +421,30 @@ AST* DLParser::Literal(void) // MapLiteral = '{' (Literal ':' LogicalExpr)* '}' AST* DLParser::MapLiteral(void) { - AST* ret = NULL; + AST* ret = _new AST(MAP); AST* child = NULL; try { match(LBRACE); do { - if( lookaheadType(1) == COMMA ) consume(); - child = Literal(); match(SEP); child = _new AST(SEP, 2, child, LogicalExpr()); - - ret = ((ret == NULL) ? _new AST(MAP) : ret); ret->addChild(child); + + if( lookaheadType(1) == COMMA ) consume(); } - while( lookaheadType(1) == COMMA ); + while( lookaheadType(1) != RBRACE ); match(RBRACE); } catch(Exception e) { - if(ret != NULL) delete ret; + // Cleanup our mess so we dont leak memory + delete ret; if(child != NULL) delete child; + + // Re throw the exception so higher-ups can handle it throw e; } return ret; @@ -516,10 +517,16 @@ std::list DLParser::MacroPatternList(void) std::list patterns; patterns.push_back( MacroPattern() ); - while(lookaheadType(1) == COMMA) + while (lookaheadType(1) != RBRACK) { - match(COMMA); - patterns.push_back( MacroPattern() ); + if ( lookaheadType(1) == COMMA ) + { + consume(); + if (lookaheadType(1) != RBRACK) + { + patterns.push_back( MacroPattern() ); + } + } } return patterns; @@ -560,10 +567,16 @@ AST* DLParser::ExpList(TokenType_T node_type, TokenType_T terminator) if(lookaheadType(1) != terminator) { node->addChild( Expression() ); - while(lookaheadType(1) == COMMA) + while(lookaheadType(1) != terminator) { - match(COMMA); - node->addChild( Expression() ); + if ( lookaheadType(1) == COMMA ) + { + consume(); + if (lookaheadType(1) != terminator) + { + node->addChild( Expression() ); + } + } } } return node;