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 ++++++++++++++++++++++++++++++++++++------ JSImpl/src/ASTInterpreter.h | 3 +- JSImpl/src/main.cpp | 6 ++- 3 files changed, 105 insertions(+), 18 deletions(-) (limited to 'JSImpl/src') 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) diff --git a/JSImpl/src/ASTInterpreter.h b/JSImpl/src/ASTInterpreter.h index ba797c1..cce6108 100644 --- a/JSImpl/src/ASTInterpreter.h +++ b/JSImpl/src/ASTInterpreter.h @@ -11,7 +11,7 @@ public: ~ASTInterpreter(); - typedef std::variant Value; + typedef std::variant Value; typedef IPLSharedPtr ValuePtr; typedef IPLVector ValueStack; @@ -47,6 +47,7 @@ public: { public: virtual void PrintVariable(const char* name, double value) = 0; + virtual void PrintVariable(const char* name, const IPLString& value) = 0; }; void Print(Printer& p); diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp index 6ed1122..06421a7 100644 --- a/JSImpl/src/main.cpp +++ b/JSImpl/src/main.cpp @@ -38,7 +38,11 @@ class InterpreterPrinter : public ASTInterpreter::Printer public: virtual void PrintVariable(const char* name, double value) override { - std::cout << name << ": " << value << std::endl; + std::cout << name << ": number : " << value << std::endl; + } + virtual void PrintVariable(const char* name, const IPLString& value) override + { + std::cout << name << ": string : " << value << std::endl; } }; -- cgit v1.3.1-23-gd82d