Skip to content

Commit

Permalink
hmmm
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Nov 19, 2014
1 parent 52f5fca commit 93d5a06
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ hmmm
- http://flex.sourceforge.net/manual/Reentrant.html
- http://flex.sourceforge.net/manual/Scanner-Options.html
- http://flex.sourceforge.net/manual/Index-of-Scanner-Options.html
- http://flex.sourceforge.net/manual/Matching.html
- https://www.gnu.org/software/bison/manual/html_node/index.html
- https://www.gnu.org/software/bison/manual/html_node/Grammar-File.html
- https://www.gnu.org/software/bison/manual/html_node/Declarations.html (see "%define Summary", "%define api.pure")
Expand Down
2 changes: 1 addition & 1 deletion sayaka/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = g++
CFLAGS = -c -g -std=c++11 -fexceptions
CFLAGS = -c -g -Wall -Wextra -std=c++11 -fexceptions
LDFLAGS = -g -rdynamic
LLVM_COMPILER_FLAGS = $(shell llvm-config --cxxflags)
LLVM_LINKER_FLAGS = $(shell llvm-config --ldflags --libs core jit native)
Expand Down
41 changes: 41 additions & 0 deletions sayaka/src/ast_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,44 @@
ASTNode::~ASTNode() {
return;
}

void ASTNode::accept(IASTNodeVisitor* visitor) {
visitor->visit(this);
}

void IASTNodeVisitor::visit(ASTNode* node) {
if (ASTNodeIdentifier* x = dynamic_cast<ASTNodeIdentifier*>(node)) {
this->visit(x);
}
if (ASTNodePrimitive* x = dynamic_cast<ASTNodePrimitive*>(node)) {
this->visit(x);
}
if (ASTNodeDeclaration* x = dynamic_cast<ASTNodeDeclaration*>(node)) {
this->visit(x);
}
if (ASTNodeBlock* x = dynamic_cast<ASTNodeBlock*>(node)) {
this->visit(x);
}
if (ASTNodeCast* x = dynamic_cast<ASTNodeCast*>(node)) {
this->visit(x);
}
if (ASTNodeAssignment* x = dynamic_cast<ASTNodeAssignment*>(node)) {
this->visit(x);
}
if (ASTNodeBinaryOperator* x = dynamic_cast<ASTNodeBinaryOperator*>(node)) {
this->visit(x);
}
if (ASTNodeFunctionPrototype* x = dynamic_cast<ASTNodeFunctionPrototype*>(node)) {
this->visit(x);
}
if (ASTNodeFunction* x = dynamic_cast<ASTNodeFunction*>(node)) {
this->visit(x);
}
if (ASTNodeFunctionCall* x = dynamic_cast<ASTNodeFunctionCall*>(node)) {
this->visit(x);
}
if (ASTNodeIfElse* x = dynamic_cast<ASTNodeIfElse*>(node)) {
this->visit(x);
}
throw std::runtime_error("Unknown node type!");
}
20 changes: 20 additions & 0 deletions sayaka/src/ast_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ enum tagEBinaryOperationType {

typedef enum tagEBinaryOperationType EBinaryOperationType;

class IASTNodeVisitor;

class ASTNode {
public:
ASTType* type;

void accept(IASTNodeVisitor*);

virtual ~ASTNode() = 0;
virtual llvm::Value* gen_code(CodeGenContext*) = 0;
virtual ASTNode* pass_types(CodeGenContext*, ASTType*) = 0;
Expand Down Expand Up @@ -166,4 +170,20 @@ class ASTNodeIfElse : public ASTNode {
virtual ASTNodeIfElse* pass_types(CodeGenContext*, ASTType*) override;
};

class IASTNodeVisitor {
public:
void visit(ASTNode*);
virtual void visit(ASTNodeIdentifier*) = 0;
virtual void visit(ASTNodePrimitive*) = 0;
virtual void visit(ASTNodeDeclaration*) = 0;
virtual void visit(ASTNodeBlock*) = 0;
virtual void visit(ASTNodeCast*) = 0;
virtual void visit(ASTNodeAssignment*) = 0;
virtual void visit(ASTNodeBinaryOperator*) = 0;
virtual void visit(ASTNodeFunctionPrototype*) = 0;
virtual void visit(ASTNodeFunction*) = 0;
virtual void visit(ASTNodeFunctionCall*) = 0;
virtual void visit(ASTNodeIfElse*) = 0;
};

#endif /* __AST_NODE_H_ */
2 changes: 1 addition & 1 deletion sayaka/src/ast_node_functionprototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ASTNodeFunctionPrototype::~ASTNodeFunctionPrototype() {
delete this->args;
}

ASTNodeFunctionPrototype* ASTNodeFunctionPrototype::pass_types(CodeGenContext* code_gen_context, ASTType* type) {
ASTNodeFunctionPrototype* ASTNodeFunctionPrototype::pass_types(CodeGenContext* code_gen_context, ASTType* ignore) {
this->type = code_gen_context->ast_types_resolver.int_ty(); // TODO
for (std::vector<ASTNodeDeclaration*>::iterator it = this->args->begin(); it != this->args->end(); it++) {
*it = (*it)->pass_types(code_gen_context, NULL);
Expand Down
2 changes: 1 addition & 1 deletion sayaka/src/ast_node_identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ASTNodeIdentifier::~ASTNodeIdentifier() {
return;
}

ASTNodeIdentifier* ASTNodeIdentifier::pass_types(CodeGenContext* code_gen_context, ASTType* type) {
ASTNodeIdentifier* ASTNodeIdentifier::pass_types(CodeGenContext* code_gen_context, ASTType* ignore) {
CodeGenVariable* var = code_gen_context->scope.get(this->name);
if (var == NULL) {
throw std::runtime_error("Undeclared identifier");
Expand Down
83 changes: 83 additions & 0 deletions sayaka/src/code_gen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "code_gen.h"
#include <iostream>

CodeGen::CodeGen(llvm::LLVMContext& context, llvm::Module* module) : context(context), builder(context), ast_types_resolver(context) {
this->module = module;
}

CodeGen::~CodeGen() {
return;
}

void CodeGen::push_block(llvm::BasicBlock* block) {
std::cout << "Pushing block" << std::endl;
this->blocks.push(block);
this->builder.SetInsertPoint(this->current_block());
}

void CodeGen::pop_block() {
std::cout << "Popping block" << std::endl;
this->blocks.pop();
this->builder.SetInsertPoint(this->current_block());
}

void CodeGen::push_scope() {
std::cout << "Pushing scope" << std::endl;
this->scope.push();
}

void CodeGen::pop_scope() {
std::cout << "Popping scope" << std::endl;
this->scope.pop();
}

llvm::BasicBlock* CodeGen::current_block() {
if (this->blocks.size() == 0) {
return NULL;
}
return this->blocks.top();
}

void CodeGen::visit(ASTNodeIdentifier* node) {
return;
}

void CodeGen::visit(ASTNodePrimitive* node) {
return;
}

void CodeGen::visit(ASTNodeDeclaration* node) {
return;
}

void CodeGen::visit(ASTNodeBlock* node) {
return;
}

void CodeGen::visit(ASTNodeCast* node) {
return;
}

void CodeGen::visit(ASTNodeAssignment* node) {
return;
}

void CodeGen::visit(ASTNodeBinaryOperator* node) {
return;
}

void CodeGen::visit(ASTNodeFunctionPrototype* node) {
return;
}

void CodeGen::visit(ASTNodeFunction* node) {
return;
}

void CodeGen::visit(ASTNodeFunctionCall* node) {
return;
}

void CodeGen::visit(ASTNodeIfElse* node) {
return;
}
54 changes: 54 additions & 0 deletions sayaka/src/code_gen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef __CODE_GEN_H_
#define __CODE_GEN_H_

#include <stack>
#include <list>
#include <llvm/Analysis/Passes.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/PassManager.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Transforms/Scalar.h>
#include "identifier_scope.h"
#include "ast_types_resolver.h"
#include "ast_node.h"

class CodeGen : public IASTNodeVisitor {
public:
llvm::LLVMContext& context;
llvm::Module* module;
llvm::IRBuilder<> builder;
std::stack<llvm::BasicBlock*> blocks;
IdentifierScope scope;
ASTTypesResolver ast_types_resolver;

CodeGen(llvm::LLVMContext&, llvm::Module*);
~CodeGen();

void push_block(llvm::BasicBlock*);
void pop_block();
void push_scope();
void pop_scope();
llvm::BasicBlock* current_block();

virtual void visit(ASTNodeIdentifier*) override;
virtual void visit(ASTNodePrimitive*) override;
virtual void visit(ASTNodeDeclaration*) override;
virtual void visit(ASTNodeBlock*) override;
virtual void visit(ASTNodeCast*) override;
virtual void visit(ASTNodeAssignment*) override;
virtual void visit(ASTNodeBinaryOperator*) override;
virtual void visit(ASTNodeFunctionPrototype*) override;
virtual void visit(ASTNodeFunction*) override;
virtual void visit(ASTNodeFunctionCall*) override;
virtual void visit(ASTNodeIfElse*) override;
};

#endif /* __CODE_GEN_H_ */

5 changes: 4 additions & 1 deletion sayaka/src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <iostream>
#include "parser.h"

// yytext, yyleng globals (http://flex.sourceforge.net/manual/Matching.html)
#define SAVE_TOKEN yylval->str = new std::string(yytext, yyleng)

%}
Expand Down Expand Up @@ -37,6 +36,7 @@ LT "<"
GT ">"
LEQ "<="
GEQ ">="
COLON ":"

IDENTIFIER [a-z_][a-zA-Z0-9_]*
TYPE_NAME [A-Z][a-zA-Z0-9_]*
Expand Down Expand Up @@ -68,8 +68,11 @@ WS [ \r\n\t]+
{GT} { return TOKEN_GT; }
{LEQ} { return TOKEN_LEQ; }
{GEQ} { return TOKEN_GEQ; }
{COLON} { return TOKEN_COLON; }
if { return TOKEN_IF; }
else { return TOKEN_ELSE; }
var { return TOKEN_VAR; }
val { return TOKEN_VAL; }

{NUMBER} { SAVE_TOKEN; return TOKEN_NUMBER; }
{IDENTIFIER} { SAVE_TOKEN; return TOKEN_IDENTIFIER; }
Expand Down
1 change: 1 addition & 0 deletions sayaka/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ int main(int argc, char* argv[]) {
//delete node;
}
}
compiler.code_gen_context.module->dump();
compiler.shutdown();
compiler.llvm_shutdown();
std::cout << "Done." << std::endl;
Expand Down
11 changes: 6 additions & 5 deletions sayaka/src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ typedef void* yyscan_t;
%left TOKEN_POW
%left TOKEN_RPAREN

%token TOKEN_LPAREN TOKEN_RPAREN TOKEN_SEMICOLON TOKEN_ASSIGN TOKEN_LBRACE TOKEN_RBRACE TOKEN_LBRACKET TOKEN_RBRACKET
%token TOKEN_LPAREN TOKEN_RPAREN TOKEN_ASSIGN TOKEN_LBRACE TOKEN_RBRACE TOKEN_LBRACKET TOKEN_RBRACKET
%token TOKEN_ADD TOKEN_MULTIPLY TOKEN_DIVIDE TOKEN_SUBTRACT TOKEN_POW
%token TOKEN_COMMA TOKEN_IF TOKEN_ELSE
%token TOKEN_COMMA TOKEN_IF TOKEN_ELSE TOKEN_VAR TOKEN_VAL
%token TOKEN_SEMICOLON TOKEN_COLON
%token <str> TOKEN_NUMBER TOKEN_IDENTIFIER TOKEN_TYPE_NAME

%type <node> program expr number binary_operator_expr assignment_expr cast_expr function_call_expr function_expr if_else_expr
Expand Down Expand Up @@ -125,10 +126,10 @@ assignment_expr
;

declaration_expr
: TOKEN_TYPE_NAME TOKEN_IDENTIFIER {
$$ = new ASTNodeDeclaration(*$1, *$2);
delete $1;
: TOKEN_VAR TOKEN_IDENTIFIER TOKEN_COLON TOKEN_TYPE_NAME {
$$ = new ASTNodeDeclaration(*$4, *$2);
delete $2;
delete $4;
}
;

Expand Down
8 changes: 4 additions & 4 deletions sayaka/test_script.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Int test_fn(Int x);
Int test_fn(var x: Int);

Int fib(Int x) {
Int fib(var x: Int) {
if (x <= 0) {
0;
} else {
Expand All @@ -12,7 +12,7 @@ Int fib(Int x) {
};
};

Int loop(Int x) {
Int loop(var x: Int) {
if (x == 0) {
0;
} else {
Expand All @@ -21,5 +21,5 @@ Int loop(Int x) {
};
};

Int n = 10;
var n: Int = 10;
fib(n) + loop(n);

0 comments on commit 93d5a06

Please sign in to comment.