%{ #include #include #include #include "ast.hpp" #define YYDEBUG 1 using namespace std; void yyerror(string s) { cerr << s << endl; exit(EXIT_FAILURE); } extern int yylex(); int br_fje = 0; %} %union { double d; string *s; ExprAST *e; vector *v; vector *v1; PrototypeAST *p; } %token def_token extern_token if_token then_token else_token for_token in_token %token num_token %token id_token %type E Step %type NizE NizE1 %type Args %type

Potpis %nonassoc else_token in_token %left '<' '>' %left '+' '-' %left '*' '/' %% Program: Program ';' Naredba { } | Naredba { } ; Naredba: def_token Potpis E { FunctionAST *f = new FunctionAST($2, $3); f->codegen(); delete f; } | extern_token Potpis { $2->codegen(); delete $2; } | E { FunctionAST *f = new FunctionAST(new PrototypeAST("anonimna_fja_" + to_string(br_fje++), vector()), $1); f->codegen(); delete f; } ; Potpis: id_token '(' Args ')' { $$ = new PrototypeAST(*$1, *$3); delete $1; delete $3; } ; Args: Args id_token { $$ = $1; $$->push_back(*$2); delete $2; } | { $$ = new vector(); } ; E: E '+' E { $$ = new AddExprAST($1, $3); } | E '-' E { $$ = new SubExprAST($1, $3); } | E '*' E { $$ = new MulExprAST($1, $3); } | E '/' E { $$ = new DivExprAST($1, $3); } | E '<' E { $$ = new LtExprAST($1, $3); } | E '>' E { $$ = new GtExprAST($1, $3); } | '(' E ')' { $$ = $2; } | num_token { $$ = new NumberExprAST($1); } | id_token { $$ = new VariableExprAST(*$1); delete $1; } | id_token '(' NizE ')' { $$ = new CallExprAST(*$1, *$3); delete $1; delete $3; } | if_token E then_token E else_token E { $$ = new IfExprAST($2, $4, $6); } | for_token id_token '=' E ',' E Step in_token E { $$ = new ForExprAST(*$2, $4, $6, $7, $9); delete $2; } ; Step: ',' E { $$ = $2; } | { $$ = new NumberExprAST(1.0); } ; NizE: NizE1 { $$ = $1; } | { $$ = new vector(); } ; NizE1: NizE1 ',' E { $$ = $1; $$->push_back($3); } | E { $$ = new vector(); $$->push_back($1); } ; %% extern LLVMContext TheContext; extern Module* TheModule; extern legacy::FunctionPassManager *TheFPM; int main() { //yydebug = 1; TheModule = new Module("Moj modul", TheContext); TheFPM = new legacy::FunctionPassManager(TheModule); TheFPM->add(createInstructionCombiningPass()); TheFPM->add(createReassociatePass()); TheFPM->add(createNewGVNPass()); TheFPM->add(createCFGSimplificationPass()); TheFPM->doInitialization(); yyparse(); TheModule->print(outs(), nullptr); delete TheModule; delete TheFPM; return 0; }