diff options
| Commit | 0d0159f70b0f3b23d6a4187d43167ded6b104e0f (patch) | |
|---|---|---|
| Author | 2020-11-02 09:03:05 +0200 | |
| Committer | 2020-11-09 22:08:19 +0200 | |
| Files | 18b71ac80128b6a00d501cd64260977cac3efd2e /JSImpl/src | |
| IPL-0d0159f70b0f3b23d6a4187d43167ded6b104e0f.tar IPL-0d0159f70b0f3b23d6a4187d43167ded6b104e0f.tar.gz IPL-0d0159f70b0f3b23d6a4187d43167ded6b104e0f.zip | ||
| Parent | acb4362f28e783632a5c79db98f14b60aa12f175 (diff) | |
Support for strings in the AST interpreter
3 files changed, 105 insertions, 18 deletions
| -rw-r--r-- | JSImpl/src/ASTInterpreter.cpp | 114 | ||||
| -rw-r--r-- | JSImpl/src/ASTInterpreter.h | 3 | ||||
| -rw-r--r-- | JSImpl/src/main.cpp | 6 |
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) 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<double> Value; + typedef std::variant<double, IPLString> Value; typedef IPLSharedPtr<Value> ValuePtr; typedef IPLVector<ValuePtr> 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; } }; |
