summaryrefslogtreecommitdiff
path: root/old/toy/node.cpp
blob: ed24f4cb799ae5851255374d162992056d0f90fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "node.hpp"
#include "grammar.hpp"

std::ostream& operator<<( std::ostream& o, const Node& n )
{
	n.printTo( o );
	return o;
}
	
void Node::printTo( std::ostream& o ) const
{
	o << "<abstract node>";
}		

void NBlock::printTo(std::ostream& o ) const
{
	o << "NBlock[" << std::endl;
	StatementList::const_iterator eit = m_statements.end( );
	for( StatementList::const_iterator it = m_statements.begin( );
		it != eit; it++ ) {
		o << "\t";
		(*it)->printTo( o );
		o << std::endl;
	}
	o << "]" << std::endl;
}

void NInteger::printTo( std::ostream& o ) const
{
	o << "NInteger[" << m_value << "]";
}

void NStatement::printTo(std::ostream& o ) const
{
	o << "NStatement";
}		

void NExpressionStatement::printTo( std::ostream& o ) const
{
	o << "NStatement[" << m_expression << "]";
}		

void NIdentifier::printTo( std::ostream& o ) const
{
	o << "NIdentifier['" << m_name << "']";
}

void NAssignment::printTo( std::ostream& o ) const
{
	o << "NAssignment[" << m_lhs << " = " << m_rhs << "]";
}

void NVariableDeclaration::printTo( std::ostream& o ) const
{
	o << "NVariableDeclaration[";
	m_type.printTo( o );
	o << " ";
	m_name.printTo( o );
	if( m_assigned_expr != 0 ) {
		o << "=";
		m_assigned_expr->printTo( o );
	}
	o << "]";
}

void NFunctionDeclaration::printTo( std::ostream& o ) const
{
	o << "NFunctionDeclaration[returns: ";
	m_type.printTo( o );
	o << ", name: ";
	m_name.printTo( o );
	if( m_arguments.size( ) > 0 ) {
		o << ", params: ";
		VariableList::const_iterator eit = m_arguments.end( );
		for( VariableList::const_iterator it = m_arguments.begin( );
			it != eit; ) {
			(*it)->printTo( o );
			it++;
			if( it != eit ) {
				o << ",";
			}
		}
	}	
	o << ":" << std::endl;
	m_block.printTo( o );
	o << "]";
}

void NFunctionCall::printTo( std::ostream& o ) const
{
	o << "NFunctionCall, ";
	o << "name: " << m_name << " (";
	ExpressionList::const_iterator eit = m_arguments.end( );
	for( ExpressionList::const_iterator it = m_arguments.begin( );
		it != eit; ) {
		(*it)->printTo( o );
		it++;
		if( it != eit ) {
			o << ",";
		}
	}
	o << ")";
}

void NBinaryOperator::printTo( std::ostream& o ) const
{
	o << "NExpression[";
	m_lhs.printTo( o );
	o << " ";
	switch( m_op ) {
		case TPLUS:	o << "+"; break;
		case TMUL:	o << "*"; break;
		default:	o << "<unknown operator>"; break;
	}
	o << " ";
	m_rhs.printTo( o );
	o << "]";
}