-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableAssignment.cpp
44 lines (36 loc) · 1.12 KB
/
VariableAssignment.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
#include "VariableAssignment.h"
#include "Expression.h"
VariableAssignment::VariableAssignment(const QString& variableName, Expression* expression, Scope* context)
: ASTNode(context), m_variableName(variableName), m_expression(expression)
{
}
VariableAssignment::~VariableAssignment()
{
delete m_expression;
}
QVariant VariableAssignment::evaluate()
{
Scope* scopeToUse = 0;
QString variableName = context()->findIdentifierAndScope(m_variableName,scopeToUse);
QVariant expressionValue = m_expression->evaluate();
if(expressionValue.canConvert<ClassInstance>())
{
if(!scopeToUse->hasScope(variableName))
{
showErrorMessage(QString("Scope %1 does not exists for assignment").arg(variableName));
return QVariant();
}
ClassInstance instance = expressionValue.value<ClassInstance>();
scopeToUse->addScope(variableName, instance.classInstance);
}
else
{
if(!scopeToUse->hasVariable(variableName))
{
showErrorMessage(QString("Variable %1 is not definied in assignment context").arg(variableName));
return QVariant();
}
scopeToUse->setVariable(variableName, m_expression->evaluate());
}
return QVariant();
}