printing ReturnStatementNode nodes

git-svn-id: svn://anubis/jtlc/trunk@25 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh 2010-01-19 06:23:40 +00:00
parent 922c1bc4b4
commit b23442a2f4
8 changed files with 79 additions and 2 deletions

View File

@ -6,6 +6,8 @@ using namespace std;
Compiler::Compiler(FILE * output_file)
{
m_outfile = output_file;
write("#include <stdint.h>\n");
write("\n");
}
void Compiler::write(const string & str)

13
nodes/DoubleNode.cc Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
#include "Node.h"
#include "parser/parser.h"
#include "main/Compiler.h"
using namespace std;
void DoubleNode::process(refptr<Compiler> compiler)
{
char buff[50];
sprintf(buff, "%lf", m_double);
compiler->write(buff);
}

13
nodes/IntegerNode.cc Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
#include "Node.h"
#include "parser/parser.h"
#include "main/Compiler.h"
using namespace std;
void IntegerNode::process(refptr<Compiler> compiler)
{
char buff[30];
sprintf(buff, "%llu", (long long unsigned int) m_integer);
compiler->write(buff);
}

View File

@ -49,6 +49,11 @@ class DoubleNode : public Node
{
public:
DoubleNode(double val) { m_double = val; }
void process(refptr<Compiler> 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> compiler);
};
class ItemsNode : public Node
@ -90,12 +96,15 @@ class ProgramNode : public Node
class ReturnStatementNode : public Node
{
public:
void process(refptr<Compiler> compiler);
};
class StringNode : public Node
{
public:
StringNode(const std::string & str) { m_string = str; }
void process(refptr<Compiler> compiler);
};
class StructTypeNode : public Node

View File

@ -0,0 +1,13 @@
#include <iostream>
#include "Node.h"
#include "parser/parser.h"
#include "main/Compiler.h"
using namespace std;
void ReturnStatementNode::process(refptr<Compiler> compiler)
{
compiler->write("return (");
m_children[0]->process(compiler);
compiler->write(");\n");
}

11
nodes/StringNode.cc Normal file
View File

@ -0,0 +1,11 @@
#include <iostream>
#include "Node.h"
#include "parser/parser.h"
#include "main/Compiler.h"
using namespace std;
void StringNode::process(refptr<Compiler> compiler)
{
compiler->write(m_string);
}

View File

@ -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);

View File

@ -1,5 +1,5 @@
main() : int
{
c("return 42;");
return 42;
}