Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / ASTInterpreter.cpp
diff options
context:
space:
mode:
Commit0d0159f70b0f3b23d6a4187d43167ded6b104e0f (patch)
Author
 Dimitar Trendafilov <trendafilov.dn@gmail.com>
2020-11-02 09:03:05 +0200
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-11-09 22:08:19 +0200
Files18b71ac80128b6a00d501cd64260977cac3efd2e /JSImpl/src/ASTInterpreter.cpp
IPL-0d0159f70b0f3b23d6a4187d43167ded6b104e0f.tar
IPL-0d0159f70b0f3b23d6a4187d43167ded6b104e0f.tar.gz
IPL-0d0159f70b0f3b23d6a4187d43167ded6b104e0f.zip
Parentacb4362f28e783632a5c79db98f14b60aa12f175 (diff)
Support for strings in the AST interpreter
1 files changed, 98 insertions, 16 deletions
-rw-r--r--JSImpl/src/ASTInterpreter.cpp114
diff --git a/JSImpl/src/ASTInterpreter.cpp b/JSImpl/src/ASTInterpreter.cpp
index fd631ca..9503494 100644
--- a/JSImpl/src/ASTInterpreter.cpp
+++ b/JSImpl/src/ASTInterpreter.cpp
@@ -26,95 +26,166 @@ namespace
{
return d != 0.0;
}
+ bool operator()(const IPLString&) const
+ {
+ return true;
+ }
};
- struct ValueAdd
+ struct UnsupportedOpReport
{
+ template <typename V>
+ ASTInterpreter::ValuePtr operator()(V) const
+ {
+ throw std::runtime_error("unsupported operation");
+ }
+
+ template <typename L, typename R>
+ ASTInterpreter::ValuePtr operator()(L, R) const
+ {
+ throw std::runtime_error("unsupported operation");
+ }
+ };
+
+ struct ValueAdd : UnsupportedOpReport
+ {
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double l, double r) const
{
return MakeValue(l + r);
}
+
+ ASTInterpreter::ValuePtr operator()(const IPLString& l, const IPLString& r) const
+ {
+ return MakeValue(l + r);
+ }
};
- struct ValueMinus
+ struct ValueMinus : UnsupportedOpReport
{
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double l, double r) const
{
return MakeValue(l - r);
}
};
- struct ValueProduct
+
+ struct ValueProduct : UnsupportedOpReport
{
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double l, double r) const
{
return MakeValue(l * r);
}
};
- struct ValueDivide
+
+ struct ValueDivide : UnsupportedOpReport
{
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double l, double r) const
{
return MakeValue(l / r);
}
};
- struct ValueLess
+
+ struct ValueLess : UnsupportedOpReport
{
- ASTInterpreter::ValuePtr operator()(double l, double r) const
+ using UnsupportedOpReport::operator();
+
+ template <typename V>
+ ASTInterpreter::ValuePtr operator()(const V& l, const V& r) const
{
return MakeValue(l < r);
}
};
- struct ValueLessEqual
+ struct ValueLessEqual : UnsupportedOpReport
{
- ASTInterpreter::ValuePtr operator()(double l, double r) const
+ using UnsupportedOpReport::operator();
+
+ template <typename V>
+ ASTInterpreter::ValuePtr operator()(V l, V r) const
{
return MakeValue(l <= r);
}
};
- struct ValueGreater
+
+ struct ValueGreater : UnsupportedOpReport
{
- ASTInterpreter::ValuePtr operator()(double l, double r) const
+ using UnsupportedOpReport::operator();
+
+ template <typename V>
+ ASTInterpreter::ValuePtr operator()(V l, V r) const
{
return MakeValue(l > r);
}
};
- struct ValueGreaterEqual
+
+ struct ValueGreaterEqual : UnsupportedOpReport
{
- ASTInterpreter::ValuePtr operator()(double l, double r) const
+ using UnsupportedOpReport::operator();
+
+ template <typename V>
+ ASTInterpreter::ValuePtr operator()(V l, V r) const
{
return MakeValue(l >= r);
}
};
- struct ValueEqualEqual
+ struct ValueEqualEqual : UnsupportedOpReport
{
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double left, double right) const
{
return MakeValue(fabs(left - right) <= 0.0001);
}
+
+ ASTInterpreter::ValuePtr operator()(const IPLString& left, const IPLString& right) const
+ {
+ return MakeValue(left == right);
+ }
};
- struct ValueBangEqual
+ struct ValueBangEqual : UnsupportedOpReport
{
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double left, double right) const
{
return MakeValue(fabs(left - right) > 0.0001);
}
+ ASTInterpreter::ValuePtr operator()(const IPLString& left, const IPLString& right) const
+ {
+ return MakeValue(left != right);
+ }
};
- struct ValueUnaryMinus
+ struct ValueUnaryMinus : UnsupportedOpReport
{
+ using UnsupportedOpReport::operator();
+
ASTInterpreter::ValuePtr operator()(double v) const
{
return MakeValue(-v);
}
};
+
struct ValueIncrement
{
void operator()(double& v) const
{
v += 1;
}
+
+ template <typename V>
+ void operator()(V&) const
+ {
+ throw std::runtime_error("unsupported operation");
+ }
};
struct ValueDecrement
@@ -123,6 +194,12 @@ namespace
{
v -= 1;
}
+
+ template <typename V>
+ void operator()(V&) const
+ {
+ throw std::runtime_error("unsupported operation");
+ }
};
struct ValuePrint
@@ -137,6 +214,11 @@ namespace
Printer->PrintVariable(Name, value);
}
+ void operator()(const IPLString& value) const
+ {
+ Printer->PrintVariable(Name, value);
+ }
+
const char* Name;
ASTInterpreter::Printer* Printer;
};
@@ -281,7 +363,7 @@ void ASTInterpreter::Visit(LiteralUndefined* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralString* e) {
- NOT_IMPLEMENTED;
+ m_Evaluation.push_back(MakeValue(e->GetValue()));
}
void ASTInterpreter::Visit(LiteralObject* e)