diff --git a/README.md b/README.md index 482773d..bad2269 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,8 @@ hmmm - flex - bison -##### Stuff -- variables -- floats, 8, 16, 64 byte ints -- library functions -- scope -- user functions -- logical operators -- conditionals +# Flex and Bison stuff +- hmmmm ### Resources - http://stackoverflow.com/questions/3104389/can-i-bind-an-existing-method-to-a-llvm-function-and-use-it-from-jit-compiled-c diff --git a/sayaka/.gitignore b/sayaka/.gitignore index 7da368e..fcd5b11 100644 --- a/sayaka/.gitignore +++ b/sayaka/.gitignore @@ -1,6 +1,6 @@ bin/ lexer.h -lex.yy.c lexer.cpp parser.h parser.cpp +parser.log diff --git a/sayaka/Makefile b/sayaka/Makefile index 872dcf6..e183a97 100644 --- a/sayaka/Makefile +++ b/sayaka/Makefile @@ -10,7 +10,7 @@ lexer.cpp: $(SRC_DIR)/lexer.l cd $(SRC_DIR) && flex --outfile=lexer.cpp --header-file=lexer.h lexer.l parser.cpp: $(SRC_DIR)/parser.y - cd $(SRC_DIR) && bison --defines=parser.h --output=parser.cpp parser.y + cd $(SRC_DIR) && bison --defines=parser.h --output=parser.cpp --verbose --report-file=parser.log parser.y build: $(SOURCES) $(CC) $(SOURCES) -o $(BIN_DIR)/$(EXECUTABLE) `llvm-config --libs core jit native --cxxflags --ldflags` $(CFLAGS) diff --git a/sayaka/src/lexer.l b/sayaka/src/lexer.l index 58bc315..c8ade9d 100644 --- a/sayaka/src/lexer.l +++ b/sayaka/src/lexer.l @@ -4,12 +4,18 @@ #include "parser.h" #include +// yytext, yyleng globals (http://flex.sourceforge.net/manual/Matching.html) #define SAVE_TOKEN yylval->str = new std::string(yytext, yyleng) %} -%option reentrant noyywrap never-interactive nounistd +/* (http://flex.sourceforge.net/manual/Code_002dLevel-And-API-Options.html) */ +%option reentrant +%option noyywrap +%option never-interactive +%option nounistd %option bison-bridge +%option bison-locations LPAREN "(" RPAREN ")" diff --git a/sayaka/src/main.cpp b/sayaka/src/main.cpp index d1faa69..d6f6d09 100644 --- a/sayaka/src/main.cpp +++ b/sayaka/src/main.cpp @@ -16,7 +16,6 @@ namespace boost { throw e; } } - #endif // BOOST_NO_EXCEPTIONS int yyparse(NExpression** expression, yyscan_t scanner); @@ -27,6 +26,7 @@ NExpression* getAST(const char* str) { NExpression* expr; yyscan_t scanner; YY_BUFFER_STATE state; + // (http://flex.sourceforge.net/manual/Reentrant-Overview.html) if (yylex_init(&scanner)) { return NULL; } @@ -55,25 +55,13 @@ void run_code(const char* code) { llvm::ExecutionEngine* execution_engine = llvm::EngineBuilder(module).setErrorStr(&error_str).setEngineKind(llvm::EngineKind::JIT).create(); CodeGen code_gen(module); - std::cout << "execution engine " << execution_engine << std::endl; - if (execution_engine == NULL) { - std::cout << "Unable to create execution engine." << std::endl; - return; - } - NFunction main_fn(root_expr, NType::int_ty()); - // llvm::Value* root_val = root_expr->gen_code(&code_gen); - - // std::cout << "Root val code:" << std::endl; - // root_val->dump(); - llvm::Function* main_fn_val = (llvm::Function*) main_fn.gen_code(&code_gen); std::cout << "Main fn code:" << std::endl; main_fn_val->dump(); void* fn_ptr = execution_engine->getPointerToFunction(main_fn_val); - int32_t (*fn_ptr_native)() = (int32_t (*)()) fn_ptr; - int32_t ret = fn_ptr_native(); + int32_t ret = ((int32_t (*)()) fn_ptr)(); std::cout << "Main fn at " << fn_ptr << "; executed: " << ret << std::endl; } diff --git a/sayaka/src/parser.y b/sayaka/src/parser.y index cd540ae..16e45f4 100644 --- a/sayaka/src/parser.y +++ b/sayaka/src/parser.y @@ -3,19 +3,14 @@ #include "node.h" #include "parser.h" #include "lexer.h" -#include #include #include #include -// TODO: memory management - -void yyerror(NExpression**, yyscan_t scanner, const char *s) { - fprintf(stderr, "YYERROR: %s!", s); +void yyerror(YYLTYPE* llocp, NExpression**, yyscan_t scanner, const char *s) { + std::cerr << "YYERROR: " << s << std::endl; } -NExpression* programBlock; - static std::deque*> identifier_stack; static void identifier_stack_push() { @@ -23,7 +18,7 @@ static void identifier_stack_push() { } static void identifier_stack_pop() { delete identifier_stack.back(); - identifier_stack.pop_back(); // TODO: does this call destructors? + identifier_stack.pop_back(); } static NType* identifier_stack_get(std::string name) { @@ -53,7 +48,9 @@ typedef void* yyscan_t; } %define api.pure -%lex-param { yyscan_t scanner } +//%define parse.error verbose +%locations +%lex-param { yyscan_t scanner } %parse-param { NExpression** expression } %parse-param { yyscan_t scanner } @@ -65,13 +62,16 @@ typedef void* yyscan_t; NType* type; } -%left TOKEN_ADD TOKEN_SUBTRACT TOKEN_MULTIPLY TOKEN_DIVIDE TOKEN_POW +%left TOKEN_ADD TOKEN_SUBTRACT +%left TOKEN_MULTIPLY TOKEN_DIVIDE +%left TOKEN_POW +%left TOKEN_EQUALS TOKEN_RPAREN %token TOKEN_LPAREN TOKEN_RPAREN TOKEN_SEMICOLON TOKEN_EQUALS TOKEN_LBRACE TOKEN_RBRACE %token TOKEN_ADD TOKEN_MULTIPLY TOKEN_DIVIDE TOKEN_SUBTRACT TOKEN_POW %token TOKEN_NUMBER TOKEN_IDENTIFIER TOKEN_TYPE_NAME -%type program expr number_expr number_expr2 number_expr3 number_expr4 number assignment_expr variable_declaration_expr cast_expr +%type program expr number binary_operator_expr assignment_expr variable_declaration_expr cast_expr %type stmts %type identifier new_identifier %type type_name @@ -97,10 +97,13 @@ stmts | stmts expr TOKEN_SEMICOLON { $$->push($2); } expr - : cast_expr { $$ = $1; } - | number_expr { $$ = $1; } - | assignment_expr { $$ = $1; } + : TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; } + | number { $$ = $1; } + | identifier { $$ = $1; } | variable_declaration_expr { $$ = $1; } + | assignment_expr { $$ = $1; } + | cast_expr { $$ = $1; } + | binary_operator_expr { $$ = $1; } ; identifier @@ -131,25 +134,10 @@ cast_expr : TOKEN_LPAREN type_name TOKEN_RPAREN expr { $$ = new NCast($4, $2); } ; -number_expr - : number_expr2 { $$ = $1; } - | number_expr TOKEN_ADD number_expr2 { $$ = new NBinaryOperator(eADD, $1, $3); } - | number_expr TOKEN_SUBTRACT number_expr2 { $$ = new NBinaryOperator(eSUBTRACT, $1, $3); } - ; - -number_expr2 - : number_expr3 { $$ = $1; } - | number_expr2 TOKEN_DIVIDE number_expr3 { $$ = new NBinaryOperator(eDIVIDE, $1, $3); } - | number_expr2 TOKEN_MULTIPLY number_expr3 { $$ = new NBinaryOperator(eMULTIPLY, $1, $3); } - ; - -number_expr3 - : number_expr4 { $$ = $1; } - | number_expr3 TOKEN_POW number_expr4 { $$ = new NBinaryOperator(ePOW, $1, $3); } - ; - -number_expr4 - : number { $$ = $1; } - | identifier { $$ = $1; } - | TOKEN_LPAREN number_expr TOKEN_RPAREN { $$ = $2; } +binary_operator_expr + : expr TOKEN_ADD expr { $$ = new NBinaryOperator(eADD, $1, $3); } + | expr TOKEN_SUBTRACT expr { $$ = new NBinaryOperator(eSUBTRACT, $1, $3); } + | expr TOKEN_DIVIDE expr { $$ = new NBinaryOperator(eDIVIDE, $1, $3); } + | expr TOKEN_MULTIPLY expr { $$ = new NBinaryOperator(eMULTIPLY, $1, $3); } + | expr TOKEN_POW expr { $$ = new NBinaryOperator(ePOW, $1, $3); } ;