Add integers
diff --git a/Evaluator.cpp b/Evaluator.cpp
index fd46805..696e555 100644
--- a/Evaluator.cpp
+++ b/Evaluator.cpp
@@ -56,6 +56,10 @@
{
return RuntimeResult(QList<Token>{Token(node.name())});
}
+ else if (node.isInteger())
+ {
+ return RuntimeResult(QList<Token>{Token::fromInteger(node.integer())});
+ }
else if (node.isVar())
{
if (!ctx.exists(node.name()) || ctx.exists(node.name()) != node.symbol())
diff --git a/Parser.cpp b/Parser.cpp
index c3d8330..145972f 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -78,7 +78,7 @@
while (peek().isDigit())
buffer += get();
- *node = T(buffer, 10);
+ *node = T::fromInteger(buffer.toInt());
return true;
}
diff --git a/Token.cpp b/Token.cpp
index 20e2a16..53ba43a 100644
--- a/Token.cpp
+++ b/Token.cpp
@@ -1,6 +1,8 @@
#include "Token.h"
#include "AstNode.h"
+#include <QDebug>
+
template class TokenBase<Token>;
// This is kind of ugly and breaks separation of concerns; but if I don't do
@@ -45,6 +47,17 @@
}
template <typename T>
+T TokenBase<T>::fromInteger(int integer)
+{
+ T tok;
+
+ tok._type = INTEGER;
+ tok._intVal = integer;
+
+ return tok;
+}
+
+template <typename T>
TokenBase<T>::TokenBase(char varType, const QString name)
{
_type = VAR;
@@ -89,7 +102,15 @@
template <typename T>
bool TokenBase<T>::operator==(const T &other) const
{
- return _type == other._type && _stringVal == other._stringVal && _charVal == other._charVal && _listVal == other._listVal;
+ // Why is this needed? Beats me.
+ if (isInteger() && other.isInteger())
+ return _intVal == other._intVal;
+
+ return _type == other._type &&
+ _stringVal == other._stringVal &&
+ _charVal == other._charVal &&
+ _listVal == other._listVal &&
+ _intVal == other._intVal;
}
template <typename T>
diff --git a/Token.h b/Token.h
index 6e0450c..41d8031 100644
--- a/Token.h
+++ b/Token.h
@@ -16,6 +16,8 @@
explicit TokenBase(QList<T> parenthesized);
TokenBase(char varType, const QString name);
+ static T fromInteger(int integer);
+
bool operator==(const T &other) const;
bool operator!=(const T &other) const;
diff --git a/main.cpp b/main.cpp
index 9a276f0..12f6dea 100644
--- a/main.cpp
+++ b/main.cpp
@@ -13,6 +13,7 @@
int g_numFailed = 0;
+
void testEval(QString function, QString expression, QString expected)
{
Evaluator eval;
@@ -75,6 +76,11 @@
qDebug() << "\033[36mEvaluate\033[0m" << function << expression << "->" << result;
}
+void testEval(QString function, QString expression, QList<Token> expected)
+{
+ testEval(function, expression, pprint(expected));
+}
+
void testMatch(const QString &test, bool shouldBe, const MatchResult &result)
{
if (result.success != shouldBe)
@@ -210,7 +216,7 @@
testParseAst("Hello; Goodbye");
testParseAst("Key = Value");
testParseAst("123", {AstNode("123", 10)});
- testParseAst("12 00", {AstNode("12", 10), AstNode("0", 10)});
+ testParseAst("12 00", {AstNode::fromInteger(12), AstNode::fromInteger(0)});
}
void testAllFunctionDefs()
@@ -222,6 +228,7 @@
void testAllEvals()
{
testEval("First {s.A e.Rest = s.A;}", "<First hello>", "h");
+ testEval("Number { = 123; }", "<Number>", QList<Token>{Token::fromInteger(123)});
}
int main(int argc, char *argv[])
@@ -235,7 +242,7 @@
cli.addHelpOption();
cli.addVersionOption();
- QCommandLineOption testOption(QStringList{"t", "test"}, "Run test suite");
+ QCommandLineOption testOption(QStringList{"t", "test"}, "Run test suite.");
cli.addOption(testOption);
cli.process(a);