Skip to content

Commit

Permalink
hmmm
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Oct 27, 2014
1 parent 1aa6524 commit acf3937
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 60 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sayaka/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bin/
lexer.h
lex.yy.c
lexer.cpp
parser.h
parser.cpp
parser.log
2 changes: 1 addition & 1 deletion sayaka/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion sayaka/src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
#include "parser.h"
#include <cstdio>

// 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 ")"
Expand Down
16 changes: 2 additions & 14 deletions sayaka/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace boost {
throw e;
}
}

#endif // BOOST_NO_EXCEPTIONS

int yyparse(NExpression** expression, yyscan_t scanner);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
58 changes: 23 additions & 35 deletions sayaka/src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
#include "node.h"
#include "parser.h"
#include "lexer.h"
#include <cstdio>
#include <map>
#include <deque>
#include <iostream>

// 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<std::map<std::string, NType*>*> identifier_stack;

static void identifier_stack_push() {
identifier_stack.push_back(new std::map<std::string, NType*>);
}
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) {
Expand Down Expand Up @@ -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 }

Expand All @@ -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> TOKEN_ADD TOKEN_MULTIPLY TOKEN_DIVIDE TOKEN_SUBTRACT TOKEN_POW
%token <str> TOKEN_NUMBER TOKEN_IDENTIFIER TOKEN_TYPE_NAME

%type <expr> program expr number_expr number_expr2 number_expr3 number_expr4 number assignment_expr variable_declaration_expr cast_expr
%type <expr> program expr number binary_operator_expr assignment_expr variable_declaration_expr cast_expr
%type <block> stmts
%type <identifier> identifier new_identifier
%type <type> type_name
Expand All @@ -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
Expand Down Expand Up @@ -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); }
;

0 comments on commit acf3937

Please sign in to comment.