From 5dc4fe726f897e5371b489c0e423f1cfb43a68b7 Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Thu, 29 Jan 2009 19:53:16 +0100 Subject: [PATCH] Suport for goto and labels --- src/ast/generator-data.cc | 10 ++++++++++ src/ast/llvm-generator-visitor.cc | 15 ++++++++++++++- src/ast/llvm-generator-visitor.hh | 3 +++ src/ast/simplify-visitor.cc | 12 ++++++++++++ src/ast/simplify-visitor.hh | 3 +++ src/lang/mugiwara/bison-parser/lexer.l | 2 ++ src/lang/mugiwara/bison-parser/parser.y | 20 ++++++++++++++------ 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/ast/generator-data.cc b/src/ast/generator-data.cc index 27a1df3..755fb09 100644 --- a/src/ast/generator-data.cc +++ b/src/ast/generator-data.cc @@ -213,6 +213,16 @@ const ag::Node ag::nodes[] = { } }, + {"Label", "Node", "A label", { + {"std::string", "name"} + } + }, + + {"Goto", "Node", "goto instruction", { + {"std::string", "label"} + } + }, + /* Functions */ {"Function", "Node", "Represent a function declaration", { {"std::string", "name"}, diff --git a/src/ast/llvm-generator-visitor.cc b/src/ast/llvm-generator-visitor.cc index 4d75ca7..ece785f 100644 --- a/src/ast/llvm-generator-visitor.cc +++ b/src/ast/llvm-generator-visitor.cc @@ -87,7 +87,7 @@ namespace ast // void // LLVMGeneratorVisitor::visit(const EqExp & node) // { - + // } void @@ -107,4 +107,17 @@ namespace ast { out_ << node.number; } + + void + LLVMGeneratorVisitor::visit(const Label & node) + { + out_ << " br label %" << node.name << std::endl + << node.name << ":" << std::endl; + } + + void + LLVMGeneratorVisitor::visit(const Goto & node) + { + out_ << " br label %" << node.label << std::endl; + } } diff --git a/src/ast/llvm-generator-visitor.hh b/src/ast/llvm-generator-visitor.hh index a5732d5..e65cdb5 100644 --- a/src/ast/llvm-generator-visitor.hh +++ b/src/ast/llvm-generator-visitor.hh @@ -53,6 +53,9 @@ namespace ast virtual void visit(const GtExp & node); virtual void visit(const GtEqExp & node); + virtual void visit(const Label & node); + virtual void visit(const Goto & node); + protected: std::ostream & out_; }; diff --git a/src/ast/simplify-visitor.cc b/src/ast/simplify-visitor.cc index d6adcc2..0bffb3f 100644 --- a/src/ast/simplify-visitor.cc +++ b/src/ast/simplify-visitor.cc @@ -152,4 +152,16 @@ namespace ast replacement_ = tre; delete &node; } + + void + SimplifyVisitor::visit(Label & node) + { + simplifications_.push_back(&node); + } + + void + SimplifyVisitor::visit(Goto & node) + { + simplifications_.push_back(&node); + } } diff --git a/src/ast/simplify-visitor.hh b/src/ast/simplify-visitor.hh index bb07172..ab097b3 100644 --- a/src/ast/simplify-visitor.hh +++ b/src/ast/simplify-visitor.hh @@ -61,6 +61,9 @@ namespace ast virtual void visit(Block & node); //virtual void visit(Function & node); + virtual void visit(Label & node); + virtual void visit(Goto & node); + protected: std::string currentId() const; std::string nextId(); diff --git a/src/lang/mugiwara/bison-parser/lexer.l b/src/lang/mugiwara/bison-parser/lexer.l index 0b74fbb..caa00dd 100644 --- a/src/lang/mugiwara/bison-parser/lexer.l +++ b/src/lang/mugiwara/bison-parser/lexer.l @@ -23,6 +23,7 @@ >= return GTEQ; ; return SEMICOL; \. return DOT; +: return DDOT; \( return LPAR; \) return RPAR; @@ -49,6 +50,7 @@ && return ANDAND; const return CONST; +goto return GOTO; (([0-9]+)?\.)?[0-9]+ yylval->number = strtod(yytext, 0); return NUMBER; [a-zA-Z]+ yylval->string = strdup(yytext); return ID; diff --git a/src/lang/mugiwara/bison-parser/parser.y b/src/lang/mugiwara/bison-parser/parser.y index 8ecd317..09dc61d 100644 --- a/src/lang/mugiwara/bison-parser/parser.y +++ b/src/lang/mugiwara/bison-parser/parser.y @@ -66,13 +66,13 @@ int yywrap(yyscan_t /*yyscanner*/) %token NUMBER %token ID -%token CONST -%token EQ SEMICOL DOT +%token EQ SEMICOL DOT DDOT %token EQEQ LT LTEQ GT GTEQ NEQ %token ADD SUB DIV MUL MOD %token OR AND XOR SHL ASHR LSHR %token NOT BANG OROR ANDAND %token LPAR RPAR LBR RBR +%token GOTO CONST %type decls %type decl @@ -122,7 +122,7 @@ var_decls: var_decls var_decl SEMICOL { assert($2); $$ = $1 ? : new std::vector (); $$->push_back($2); - } | /* epsilon */ { $$ = 0; }; + } | /* epsilon */ { $$ = new std::vector (); }; var_decl: type ID { $$ = new ast::VarDecl(); @@ -130,15 +130,23 @@ var_decl: type ID { $$->name = $2; }; -instrs: instrs instr SEMICOL { +instrs: instrs instr { assert($2); $$ = $1 ? : new std::vector (); $$->push_back($2); - } | /* epsilon */ { $$ = 0; }; + } | /* epsilon */ { $$ = new std::vector (); }; -instr: exp { +instr: exp SEMICOL { assert($1); $$ = $1; +} | ID DDOT { + ast::Label * label = new ast::Label(); + label->name = $1; + $$ = label; +} | GOTO ID SEMICOL { + ast::Goto * gt = new ast::Goto(); + gt->label = $2; + $$ = gt; }; type: is_const ID { -- 2.11.4.GIT