diff options
author | kintel <kintel@b57f626f-c46c-0410-a088-ec61d464b74c> | 2010-02-12 13:20:15 (GMT) |
---|---|---|
committer | kintel <kintel@b57f626f-c46c-0410-a088-ec61d464b74c> | 2010-02-12 13:20:15 (GMT) |
commit | 572631746157766359a878c7b52d0c26ae731496 (patch) | |
tree | 4bdcfc0176a0cf4e455a78c6e35cf2ff706ce7c9 /src/parser.y | |
parent | 501fc8d3a3503c88b54d37ede5388ac81814a330 (diff) |
Support for if-else statements
git-svn-id: http://svn.clifford.at/openscad/trunk@436 b57f626f-c46c-0410-a088-ec61d464b74c
Diffstat (limited to 'src/parser.y')
-rw-r--r-- | src/parser.y | 63 |
1 files changed, 55 insertions, 8 deletions
diff --git a/src/parser.y b/src/parser.y index 7bee0c1..a0e4ac9 100644 --- a/src/parser.y +++ b/src/parser.y @@ -62,12 +62,15 @@ public: class Value *value; class Expression *expr; class ModuleInstantiation *inst; + class IfElseModuleInstantiation *ifelse; class ArgContainer *arg; class ArgsContainer *args; } %token TOK_MODULE %token TOK_FUNCTION +%token TOK_IF +%token TOK_ELSE %token <text> TOK_ID %token <text> TOK_STRING @@ -96,6 +99,9 @@ public: %type <expr> vector_expr %type <inst> module_instantiation +%type <ifelse> if_statement +%type <ifelse> ifelse_statement +%type <inst> children_instantiation %type <inst> module_instantiation_list %type <inst> single_module_instantiation @@ -161,29 +167,70 @@ statement: delete $4; } ';' ; -module_instantiation: - single_module_instantiation ';' { +/* Will return a dummy parent node with zero or more children */ +children_instantiation: + module_instantiation { + $$ = new ModuleInstantiation(); + if ($1) { + $$->children.append($1); + } else { + delete $1; + } + } | + '{' module_instantiation_list '}' { + $$ = $2; + } ; + +if_statement: + TOK_IF '(' expr ')' children_instantiation { + $$ = new IfElseModuleInstantiation(); + $$->modname = "if"; + $$->argnames.append(QString()); + $$->argexpr.append($3); + + if ($$) { + $$->children = $5->children; + } else { + for (int i = 0; i < $5->children.count(); i++) + delete $5->children[i]; + } + $5->children.clear(); + delete $5; + } ; + +ifelse_statement: + if_statement { $$ = $1; } | - single_module_instantiation '{' module_instantiation_list '}' { + if_statement TOK_ELSE children_instantiation { $$ = $1; if ($$) { - $$->children = $3->children; + $$->else_children = $3->children; } else { for (int i = 0; i < $3->children.count(); i++) delete $3->children[i]; } $3->children.clear(); delete $3; + } ; + +module_instantiation: + single_module_instantiation ';' { + $$ = $1; } | - single_module_instantiation module_instantiation { + single_module_instantiation children_instantiation { $$ = $1; if ($$) { - if ($2) - $$->children.append($2); + $$->children = $2->children; } else { - delete $2; + for (int i = 0; i < $2->children.count(); i++) + delete $2->children[i]; } + $2->children.clear(); + delete $2; + } | + ifelse_statement { + $$ = $1; } ; module_instantiation_list: |