-
Notifications
You must be signed in to change notification settings - Fork 0
/
VariableCreation.cpp
40 lines (34 loc) · 1.06 KB
/
VariableCreation.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
#include "VariableCreation.h"
#include "Expression.h"
VariableCreation::VariableCreation(const QString& variableName, Expression* expression, Scope* context)
: ASTNode(context), m_variableName(variableName), m_expression(expression)
{
}
VariableCreation::~VariableCreation()
{
delete m_expression;
}
QVariant VariableCreation::evaluate()
{
QVariant expressionValue = m_expression->evaluate();
if(expressionValue.canConvert<ClassInstance>())
{
if(context()->hasScope(m_variableName) || context()->hasVariable(m_variableName))
{
showErrorMessage(QString("Variable %1 is already defined").arg(m_variableName));
return QVariant();
}
ClassInstance instance = expressionValue.value<ClassInstance>();
context()->addScope(m_variableName, instance.classInstance);
}
else
{
if(context()->hasScope(m_variableName) || context()->hasVariable(m_variableName))
{
showErrorMessage(QString("Variable %1 is already defined").arg(m_variableName));
return QVariant();
}
context()->setVariable(m_variableName, m_expression->evaluate());
}
return QVariant();
}