aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/src/expression.cpp
blob: 29bfe0d0a090c9354609f66df6cfe3a16f963629 (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
#include "expression.hpp"
#include "bindings.hpp"

#include <iostream>

// Expression definition

Expression::~Expression()
{}

void Expression::print() const
{}

void Expression::printxml() const
{}

int32_t Expression::getPostfixStackPosition(VariableStackBindings bindings) const
{
    return -1;
}


// OperationExpression definition

OperationExpression::OperationExpression(Expression* _lhs, Expression* _rhs)
    : lhs(_lhs), rhs(_rhs)
{}

OperationExpression::~OperationExpression()
{
    delete lhs;
    delete rhs;
}


// Assignment Expression definition

AssignmentExpression::AssignmentExpression(Expression* _lhs, Expression* _rhs)
    : OperationExpression(_lhs, _rhs)
{}

VariableStackBindings AssignmentExpression::printasm(VariableStackBindings bindings) const
{
    int32_t store_stack_position = lhs->getPostfixStackPosition(bindings);
    rhs->printasm(bindings);

    std::cout << "\tsw\t$2," << store_stack_position << "($fp)" << std::endl;
								   
    return bindings;
}


// Identifier definition

Identifier::Identifier(const std::string& id)
    : m_id(id)
{}

VariableStackBindings Identifier::printasm(VariableStackBindings bindings) const
{
    if(bindings.bindingExists(m_id)) {
	int32_t stack_position = bindings.getStackPosition(m_id);

	std::cout << "\tlw\t$2," << stack_position << "($fp)" << std::endl;
    } else
	std::cerr << "Can't find identifier '" << m_id << "' in current scope binding" << std::endl;
	
    return bindings;
}

int32_t Identifier::getPostfixStackPosition(VariableStackBindings bindings) const
{
    if(bindings.bindingExists(m_id)) {
	return bindings.getStackPosition(m_id);
    }

    return -1;
}


// Constant definition

Constant::Constant(const int32_t& constant)
    : m_constant(constant)
{}

VariableStackBindings Constant::printasm(VariableStackBindings bindings) const
{
    std::cout << "\tli\t$2," << m_constant << std::endl;

    return bindings;
}