From b23442a2f4f30eba51818d881542ee5f80ed1cbe Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 19 Jan 2010 06:23:40 +0000 Subject: [PATCH] printing ReturnStatementNode nodes git-svn-id: svn://anubis/jtlc/trunk@25 f5bc74b8-7b62-4e90-9214-7121d538519f --- main/Compiler.cc | 2 ++ nodes/DoubleNode.cc | 13 +++++++++++++ nodes/IntegerNode.cc | 13 +++++++++++++ nodes/Node.h | 9 +++++++++ nodes/ReturnStatementNode.cc | 13 +++++++++++++ nodes/StringNode.cc | 11 +++++++++++ parser/parser.yy | 18 +++++++++++++++++- tests/Initial.jtl | 2 +- 8 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 nodes/DoubleNode.cc create mode 100644 nodes/IntegerNode.cc create mode 100644 nodes/ReturnStatementNode.cc create mode 100644 nodes/StringNode.cc diff --git a/main/Compiler.cc b/main/Compiler.cc index 610757e..239ca7f 100644 --- a/main/Compiler.cc +++ b/main/Compiler.cc @@ -6,6 +6,8 @@ using namespace std; Compiler::Compiler(FILE * output_file) { m_outfile = output_file; + write("#include \n"); + write("\n"); } void Compiler::write(const string & str) diff --git a/nodes/DoubleNode.cc b/nodes/DoubleNode.cc new file mode 100644 index 0000000..216e639 --- /dev/null +++ b/nodes/DoubleNode.cc @@ -0,0 +1,13 @@ + +#include +#include "Node.h" +#include "parser/parser.h" +#include "main/Compiler.h" +using namespace std; + +void DoubleNode::process(refptr compiler) +{ + char buff[50]; + sprintf(buff, "%lf", m_double); + compiler->write(buff); +} diff --git a/nodes/IntegerNode.cc b/nodes/IntegerNode.cc new file mode 100644 index 0000000..8d81829 --- /dev/null +++ b/nodes/IntegerNode.cc @@ -0,0 +1,13 @@ + +#include +#include "Node.h" +#include "parser/parser.h" +#include "main/Compiler.h" +using namespace std; + +void IntegerNode::process(refptr compiler) +{ + char buff[30]; + sprintf(buff, "%llu", (long long unsigned int) m_integer); + compiler->write(buff); +} diff --git a/nodes/Node.h b/nodes/Node.h index e7c2145..f12685e 100644 --- a/nodes/Node.h +++ b/nodes/Node.h @@ -49,6 +49,11 @@ class DoubleNode : public Node { public: DoubleNode(double val) { m_double = val; } + void process(refptr compiler); +}; + +class ExpressionNode : public Node +{ }; class FunctionNode : public Node @@ -67,6 +72,7 @@ class IntegerNode : public Node { public: IntegerNode(uint64_t integer) { m_integer = integer; } + void process(refptr compiler); }; class ItemsNode : public Node @@ -90,12 +96,15 @@ class ProgramNode : public Node class ReturnStatementNode : public Node { + public: + void process(refptr compiler); }; class StringNode : public Node { public: StringNode(const std::string & str) { m_string = str; } + void process(refptr compiler); }; class StructTypeNode : public Node diff --git a/nodes/ReturnStatementNode.cc b/nodes/ReturnStatementNode.cc new file mode 100644 index 0000000..183c632 --- /dev/null +++ b/nodes/ReturnStatementNode.cc @@ -0,0 +1,13 @@ + +#include +#include "Node.h" +#include "parser/parser.h" +#include "main/Compiler.h" +using namespace std; + +void ReturnStatementNode::process(refptr compiler) +{ + compiler->write("return ("); + m_children[0]->process(compiler); + compiler->write(");\n"); +} diff --git a/nodes/StringNode.cc b/nodes/StringNode.cc new file mode 100644 index 0000000..4cf65b5 --- /dev/null +++ b/nodes/StringNode.cc @@ -0,0 +1,11 @@ + +#include +#include "Node.h" +#include "parser/parser.h" +#include "main/Compiler.h" +using namespace std; + +void StringNode::process(refptr compiler) +{ + compiler->write(m_string); +} diff --git a/parser/parser.yy b/parser/parser.yy index c8a57bd..234695f 100644 --- a/parser/parser.yy +++ b/parser/parser.yy @@ -273,7 +273,12 @@ return_statement: RETURN expression SEMICOLON { ; expression: assign_expr { - $$ = $1; + $$ = new ExpressionNode(); + $$->addChild($1); + } + | literal { + $$ = new ExpressionNode(); + $$->addChild($1); } ; @@ -284,6 +289,17 @@ assign_expr: lvalue ASSIGN expression { } ; +literal: INT_LITERAL { + $$ = $1; + } + | REAL_LITERAL { + $$ = $1; + } + | STRING_LITERAL { + $$ = $1; + } + ; + lvalue: IDENTIFIER { $$ = new LValueNode(); $$->addChild($1); diff --git a/tests/Initial.jtl b/tests/Initial.jtl index d5676b7..a2d7a21 100644 --- a/tests/Initial.jtl +++ b/tests/Initial.jtl @@ -1,5 +1,5 @@ main() : int { - c("return 42;"); + return 42; }