Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean up code

  • Loading branch information...
commit 2dc19ccd501c57962fd1b366363855f5cbc734ae 1 parent 89a295f
@lsegal authored
View
2  Makefile
@@ -1,7 +1,7 @@
all: parser
clean:
- rm parser.cpp parser tokens.cpp
+ rm parser.cpp parser.hpp parser tokens.cpp
parser.cpp: parser.y
bison -d -o $@ $^
View
47 codegen.cpp
@@ -4,42 +4,57 @@
using namespace std;
-static const Type *typeOf(const NIdentifier& type)
-{
- if (type.name.compare("int") == 0) {
- return Type::Int64Ty;
- }
- else if (type.name.compare("double") == 0) {
- return Type::FP128Ty;
- }
- return Type::VoidTy;
-}
-
+/* Compile the AST into a module */
void CodeGenContext::generateCode(NBlock& root)
{
- std::cout << "generating code...\n";
+ std::cout << "Generating code...\n";
+
+ /* Create the top level interpreter function to call as entry */
vector<const Type*> argTypes;
FunctionType *ftype = FunctionType::get(Type::VoidTy, argTypes, false);
mainFunction = Function::Create(ftype, GlobalValue::InternalLinkage, "main", module);
BasicBlock *bblock = BasicBlock::Create("entry", mainFunction, 0);
+
+ /* Push a new variable/block context */
pushBlock(bblock);
- root.codeGen(*this);
+ root.codeGen(*this); /* emit bytecode for the toplevel block */
ReturnInst::Create(bblock);
popBlock();
- std::cout << "code is generated.\n";
+
+ /* Print the bytecode in a human-readable format
+ to see if our program compiled properly
+ */
+ std::cout << "Code is generated.\n";
PassManager pm;
pm.add(createPrintModulePass(&outs()));
pm.run(*module);
}
+/* Executes the AST by running the main function */
GenericValue CodeGenContext::runCode() {
- std::cout << "running code...\n";
+ std::cout << "Running code...\n";
ExistingModuleProvider *mp = new ExistingModuleProvider(module);
ExecutionEngine *ee = ExecutionEngine::create(mp, false);
vector<GenericValue> noargs;
- return ee->runFunction(mainFunction, noargs);
+ GenericValue v = ee->runFunction(mainFunction, noargs);
+ std::cout << "Code was run.\n";
+ return v;
}
+/* Returns an LLVM type based on the identifier */
+static const Type *typeOf(const NIdentifier& type)
+{
+ if (type.name.compare("int") == 0) {
+ return Type::Int64Ty;
+ }
+ else if (type.name.compare("double") == 0) {
+ return Type::FP128Ty;
+ }
+ return Type::VoidTy;
+}
+
+/* -- Code Generation -- */
+
Value* NInteger::codeGen(CodeGenContext& context)
{
std::cout << "Creating integer: " << value << endl;
View
27 codegen.h
@@ -18,23 +18,22 @@ class NBlock;
class CodeGenBlock {
public:
- BasicBlock *block;
- std::map<std::string, Value*> locals;
+ BasicBlock *block;
+ std::map<std::string, Value*> locals;
};
class CodeGenContext {
- std::stack<CodeGenBlock *> blocks;
-
- Function *mainFunction;
+ std::stack<CodeGenBlock *> blocks;
+ Function *mainFunction;
public:
- Module *module;
- CodeGenContext() { module = new Module("main"); }
-
- void generateCode(NBlock& root);
- GenericValue runCode();
- std::map<std::string, Value*>& locals() { return blocks.top()->locals; }
- BasicBlock *currentBlock() { return blocks.top()->block; }
- void pushBlock(BasicBlock *block) { blocks.push(new CodeGenBlock()); blocks.top()->block = block; }
- void popBlock() { CodeGenBlock *top = blocks.top(); blocks.pop(); delete top; }
+ Module *module;
+ CodeGenContext() { module = new Module("main"); }
+
+ void generateCode(NBlock& root);
+ GenericValue runCode();
+ std::map<std::string, Value*>& locals() { return blocks.top()->locals; }
+ BasicBlock *currentBlock() { return blocks.top()->block; }
+ void pushBlock(BasicBlock *block) { blocks.push(new CodeGenBlock()); blocks.top()->block = block; }
+ void popBlock() { CodeGenBlock *top = blocks.top(); blocks.pop(); delete top; }
};
View
5 example.txt
@@ -0,0 +1,5 @@
+int do_math(int a) {
+ int x = a * 5 + 3
+}
+
+do_math(10)
View
3  main.cpp
@@ -10,10 +10,11 @@ extern NBlock* programBlock;
int main(int argc, char **argv)
{
yyparse();
+ std::cout << programBlock << endl;
CodeGenContext context;
- std::cout << programBlock << endl;
context.generateCode(*programBlock);
context.runCode();
+
return 0;
}
View
4 node.h
@@ -48,7 +48,7 @@ class NMethodCall : public NExpression {
public:
const NIdentifier& id;
ExpressionList arguments;
- NMethodCall(const NIdentifier& id, std::vector<NExpression*>& arguments) :
+ NMethodCall(const NIdentifier& id, ExpressionList& arguments) :
id(id), arguments(arguments) { }
NMethodCall(const NIdentifier& id) : id(id) { }
virtual llvm::Value* codeGen(CodeGenContext& context);
@@ -107,7 +107,7 @@ class NFunctionDeclaration : public NStatement {
VariableList arguments;
NBlock& block;
NFunctionDeclaration(const NIdentifier& type, const NIdentifier& id,
- const std::vector<NVariableDeclaration*>& arguments, NBlock& block) :
+ const VariableList& arguments, NBlock& block) :
type(type), id(id), arguments(arguments), block(block) { }
virtual llvm::Value* codeGen(CodeGenContext& context);
};
View
14 parser.y
@@ -89,7 +89,7 @@ expr : ident TEQUAL expr { $$ = new NAssignment(*$<ident>1, *$3); }
| ident TLPAREN call_args TRPAREN { $$ = new NMethodCall(*$1, *$3); delete $3; }
| ident { $<ident>$ = $1; }
| numeric
- | expr comparison expr { printf("XXX:%d\n", $2); $$ = new NBinaryOperator(*$1, $2, *$3); }
+ | expr comparison expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| TLPAREN expr TRPAREN { $$ = $2; }
;
@@ -98,16 +98,8 @@ call_args : /*blank*/ { $$ = new ExpressionList(); }
| call_args TCOMMA expr { $1->push_back($3); }
;
-comparison : TCEQ { $$ = TCEQ; }
- | TCNE { $$ = TCNE; }
- | TCLT { $$ = TCLT; }
- | TCLE { $$ = TCLE; }
- | TCGT { $$ = TCGT; }
- | TCGE { $$ = TCGE; }
- | TPLUS { $$ = TPLUS; }
- | TMINUS { $$ = TMINUS; }
- | TMUL { $$ = TMUL; }
- | TDIV { $$ = TDIV; }
+comparison : TCEQ | TCNE | TCLT | TCLE | TCGT | TCGE
+ | TPLUS | TMINUS | TMUL | TDIV
;
%%
View
35 tokens.l
@@ -3,6 +3,7 @@
#include "node.h"
#include "parser.hpp"
#define SAVE_TOKEN yylval.string = new std::string(yytext, yyleng)
+#define TOKEN(t) (yylval.token = t)
extern "C" int yywrap() { }
%}
@@ -12,23 +13,23 @@ extern "C" int yywrap() { }
[a-zA-Z_][a-zA-Z0-9_]* SAVE_TOKEN; return TIDENTIFIER;
[0-9]+\.[0-9]* SAVE_TOKEN; return TDOUBLE;
[0-9]+ SAVE_TOKEN; return TINTEGER;
-"=" return TEQUAL;
-"==" return TCEQ;
-"!=" return TCNE;
-"<" return TCLT;
-"<=" return TCLE;
-">" return TCGT;
-">=" return TCGE;
-"(" return TLPAREN;
-")" return TRPAREN;
-"{" return TLBRACE;
-"}" return TRBRACE;
-"." return TDOT;
-"," return TCOMMA;
-"+" return TPLUS;
-"-" return TMINUS;
-"*" return TMUL;
-"/" return TDIV;
+"=" return TOKEN(TEQUAL);
+"==" return TOKEN(TCEQ);
+"!=" return TOKEN(TCNE);
+"<" return TOKEN(TCLT);
+"<=" return TOKEN(TCLE);
+">" return TOKEN(TCGT);
+">=" return TOKEN(TCGE);
+"(" return TOKEN(TLPAREN);
+")" return TOKEN(TRPAREN);
+"{" return TOKEN(TLBRACE);
+"}" return TOKEN(TRBRACE);
+"." return TOKEN(TDOT);
+"," return TOKEN(TCOMMA);
+"+" return TOKEN(TPLUS);
+"-" return TOKEN(TMINUS);
+"*" return TOKEN(TMUL);
+"/" return TOKEN(TDIV);
. printf("Unknown token!\n"); yyterminate();
%%
Please sign in to comment.
Something went wrong with that request. Please try again.