-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatement.cpp
78 lines (66 loc) · 2.21 KB
/
Statement.cpp
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
#include "Statement.h"
#include "Expression.h"
#include <string>
#include "Context.h"
#include "TypedValue.h"
#include "FunctionPointer.h"
Statement::Statement(int line, int column)
: line(line), column(column)
{ }
int Statement::getLine() const
{
return line;
}
int Statement::getColumn() const
{
return column;
}
std::ostream & operator<<(std::ostream & os, const Statement& other)
{
other.print(os);
return os;
}
VariableDeclaration::VariableDeclaration(std::string identifier, const Expression * expression, int line, int column)
: Statement(line, column), identifier(identifier), expression(expression)
{ }
VariableDeclaration::~VariableDeclaration()
{
if (expression != nullptr)
delete expression;
}
void VariableDeclaration::print(std::ostream & os, std::string spacing) const
{
os << spacing << "VariableDeclaration @line " << line << " @column " << column << std::endl;
os << spacing << " Identifier: " << identifier << std::endl;
os << spacing << " Expression:" << std::endl;
expression->print(os, spacing + " ");
}
void VariableDeclaration::run(GlobalContext & context) const
{
context.defineVariable(identifier, expression->evaluate(context));
}
FunctionDeclaration::FunctionDeclaration(std::string identifier, std::vector<std::string> parameters, const Expression* expression, int line, int column)
: Statement(line, column), identifier(identifier), parameters(parameters), expression(expression)
{ }
FunctionDeclaration::~FunctionDeclaration()
{
if (expression != nullptr)
delete expression;
}
void FunctionDeclaration::print(std::ostream & os, std::string spacing) const
{
os << spacing << "FunctionDeclaration @line " << line << " @column " << column << std::endl;
os << spacing << " Identifier: " << identifier << std::endl;
os << spacing << " Parameters:" << std::endl;
for (std::string param : parameters)
{
os << spacing << " " << param << std::endl;
}
os << spacing << " Expression: " << identifier << std::endl;
expression->print(os, spacing + " ");
}
void FunctionDeclaration::run(GlobalContext & context) const
{
context.defineVariable(identifier, std::make_shared<FunctionValue>(std::make_shared<ExpressionFunctionPointer>(parameters, expression)));
expression = nullptr;
}