From 0d0159f70b0f3b23d6a4187d43167ded6b104e0f Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Mon, 2 Nov 2020 09:03:05 +0200 Subject: Support for strings in the AST interpreter --- JSImpl/src/ASTInterpreter.cpp | 114 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 16 deletions(-) (limited to 'JSImpl/src/ASTInterpreter.cpp') 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 + ASTInterpreter::ValuePtr operator()(V) const + { + throw std::runtime_error("unsupported operation"); + } + + template + 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 + 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 + 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 + 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 + 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 + void operator()(V&) const + { + throw std::runtime_error("unsupported operation"); + } }; struct ValueDecrement @@ -123,6 +194,12 @@ namespace { v -= 1; } + + template + 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) -- cgit v1.3.1-23-gd82d