diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-03 16:24:07 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-03 16:24:07 +0000 |
commit | 522cc9d286c5d35ca25ebaa85374f5f9214a7f6e (patch) | |
tree | e4c944cce2fcd736834e35d8e5d06f984b57a976 /c_compiler/src | |
parent | 446c2394ec8970198d645bbbb462c67b9e3f1b1e (diff) | |
download | Compiler-522cc9d286c5d35ca25ebaa85374f5f9214a7f6e.tar.gz Compiler-522cc9d286c5d35ca25ebaa85374f5f9214a7f6e.zip |
Still working on ast
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/c_parser.y | 30 | ||||
-rw-r--r-- | c_compiler/src/compiler_main.cpp (renamed from c_compiler/src/parser_main.cpp) | 4 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 11 | ||||
-rw-r--r-- | c_compiler/src/function.cpp | 18 | ||||
-rw-r--r-- | c_compiler/src/primitives.cpp | 35 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 53 |
6 files changed, 134 insertions, 17 deletions
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 2f9f1b8..374ee46 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -37,17 +37,17 @@ void yyerror(const char *); StatementList ArgumentExpressionList %type <base_node> ExtDeclaration FuncDef Declaration DeclarationSpec DeclarationSpec_T - InitDeclarator Statement CompoundStatement CompoundStatement_2 + Statement CompoundStatement CompoundStatement_2 PrimaryExpression SelectionStatement ExpressionStatement JumpStatement IterationStatement Expression AssignmentExpression ConditionalExpression LogicalOrExpression LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression AndExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression - PostfixExpression PostfixExpression2 PrimaryExpression + PostfixExpression PostfixExpression2 -%type <base_prim> Parameter ParamDeclarator Declarator DirectDeclarator +%type <base_prim> Parameter ParamDeclarator Declarator DirectDeclarator InitDeclarator Constant -%type <number> T_INT_CONST Constant +%type <number> T_INT_CONST %type <string> T_IDENTIFIER ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT @@ -65,7 +65,7 @@ ROOT: ExtDef: ExtDeclaration { $$ = new ExternalDefinition($1); } - | ExtDef ExtDeclaration { $$->push($2); } +| ExtDef ExtDeclaration { $$->push($2); } ; ExtDeclaration: @@ -76,7 +76,7 @@ ExtDeclaration: // FUNCTION DEFINITION FuncDef: - DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); } + DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); } ; ParameterList: @@ -140,8 +140,8 @@ DirectDeclarator: ; IdentifierList: - T_IDENTIFIER { $$ = new Declarator(*$1); } - | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declarator(*$3); } + T_IDENTIFIER { $$ = new BaseList(); } + | IdentifierList T_CMA T_IDENTIFIER { $$ = new BaseList(); } ; // Statement @@ -181,7 +181,7 @@ ExpressionStatement: ; JumpStatement: - T_RETURN ExpressionStatement { $$ = $2; } + T_RETURN ExpressionStatement { $$ = new JumpStatement($2); } ; IterationStatement: @@ -299,22 +299,22 @@ PostfixExpression: PostfixExpression2: T_RRB { $$ = new Expression(); } - | ArgumentExpressionList T_RRB { $$ = $1; } + | ArgumentExpressionList T_RRB { $$ = new BaseNode; } ; ArgumentExpressionList: - AssignmentExpression { $$ = $1; } - | ArgumentExpressionList T_CMA AssignmentExpression { $$ = $3; } + AssignmentExpression { $$ = new BaseList; } + | ArgumentExpressionList T_CMA AssignmentExpression { $$ = new BaseList; } ; PrimaryExpression: T_IDENTIFIER { $$ = new Expression(); } - | Constant { $$ = new Expression(); } + | Constant { $$ = new Expression($1); } | T_LRB Expression T_RRB { $$ = $2; } ; Constant: - T_INT_CONST { $$ = $1; } + T_INT_CONST { $$ = new Immediate($1); } ; %% @@ -322,7 +322,7 @@ Constant: const BaseList* g_root; // Definition of variable (to match declaration earlier) const BaseList* parseAST() { - g_root = NULL; + g_root = 0; yyparse(); return g_root; } diff --git a/c_compiler/src/parser_main.cpp b/c_compiler/src/compiler_main.cpp index 8922417..d551b8b 100644 --- a/c_compiler/src/parser_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -3,9 +3,9 @@ #include <iostream> int main(int argc, char *argv[]) { - const Base *ast = parseAST(); + const BaseList* ast = parseAST(); - ast->print(); + ast->printasm(); return 0; } diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp new file mode 100644 index 0000000..cf2b58f --- /dev/null +++ b/c_compiler/src/expression.cpp @@ -0,0 +1,11 @@ +#include "ast.hpp" + + +// Expression definition + +Expression::Expression(const Base* _expr) + : BaseNode(_expr) {} + +void Expression::printasm() const { + leftNode->printasm(); +} diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp new file mode 100644 index 0000000..420c782 --- /dev/null +++ b/c_compiler/src/function.cpp @@ -0,0 +1,18 @@ +#include "ast.hpp" + +Function::Function(const std::string& _id, const BaseList* _param_list, + const BaseNode* _comp_statement) + : BaseNode(_param_list, _comp_statement), id(_id) {} + +void Function::printxml() const { + std::cout << "<Function id=\"" << id << "\">" << std::endl; + leftNode->printxml(); + rightNode->printxml(); + std::cout << "</Function>" << std::endl; +} + +void Function::printasm() const { + std::cout << id << ":\n\taddiu\t$sp,$sp,-24\n\tsw\t$fp,20($sp)\n\tmove\t$fp,$sp" << std::endl; + rightNode->printasm(); + std::cout << "\tmove\t$sp,$fp\n\tlw\t$fp,20($sp)\n\taddiu\t$sp,$sp,24\n\tjr\t$31\n\tnop" << std::endl; +} diff --git a/c_compiler/src/primitives.cpp b/c_compiler/src/primitives.cpp new file mode 100644 index 0000000..e3ec0eb --- /dev/null +++ b/c_compiler/src/primitives.cpp @@ -0,0 +1,35 @@ +#include "ast.hpp" + + +// Parameter list definition (used in functions) + +ParamList::ParamList() : BaseList() {} + +ParamList::ParamList(const Base* _param) : BaseList(_param) {} + + +// Declarator defintion, used when declaring a variable + +Declarator::Declarator(const std::string& _id) : BasePrimitive(_id) {} + +void Declarator::printxml() const { + std::cout << "<Variable id=\"" << id << "\" />" << std::endl; +} + + +// Parameter class defition + +Parameter::Parameter(const std::string& _id) : BasePrimitive(_id) {} + +void Parameter::printxml() const { + std::cout << "<Parameter id=\"" << id << "\" />" << std::endl; +} + + +// Immediate class definition, used when loading an immediate value + +Immediate::Immediate(const int32_t& _imm) : BasePrimitive(), imm(_imm) {} + +void Immediate::printasm() const { + std::cout << "\tli\t$2," << imm << "\n\tsw\t$2,8($fp)" << std::endl; +} diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp new file mode 100644 index 0000000..e828eaa --- /dev/null +++ b/c_compiler/src/statement.cpp @@ -0,0 +1,53 @@ +#include "ast.hpp" + + +// General base Statement definition + +Statement::Statement(const Base* _left, const Base* _right) + : BaseNode(_left, _right) {} + + +// Statement list definition + +StatementList::StatementList(const Base* _statement) + : BaseList(_statement) {} + + +// Compound Statement definition + +CompoundStatement::CompoundStatement(const Base* _dec, const Base* _statement) + : Statement(_dec, _statement) {} + +void CompoundStatement::printxml() const { + std::cout << "<Scope>" << std::endl; + leftNode->printxml(); + rightNode->printxml(); + std::cout << "</Scope>" << std::endl; +} + + +// Selection Statement definition + +SelectionStatement::SelectionStatement(const Base* _if, const Base* _else) + : Statement(_if, _else) {} + + +// Expression Statement definition + +ExpressionStatement::ExpressionStatement(const Base* _expr) + : Statement(_expr) {} + + +// Jump Statement definition + +JumpStatement::JumpStatement(const Base* _el) : Statement(_el) {} + +void JumpStatement::printasm() const { + leftNode->printasm(); + std::cout << "\tlw\t$2,8($fp)" << std::endl; +} + + +// Iteration Statement definition + +IterationStatement::IterationStatement(const Base* _el) : Statement(_el) {} |