Add integer token type
diff --git a/Matcher.cpp b/Matcher.cpp
index 1b65158..54cd9d9 100644
--- a/Matcher.cpp
+++ b/Matcher.cpp
@@ -18,7 +18,7 @@
Token dataHead = data.first();
pattern.removeFirst();
- if (ph.isSym() || ph.isIdent())
+ if (ph.isSym() || ph.isIdent() || ph.isInteger())
{
if (ph == data.first())
{
@@ -90,7 +90,7 @@
typeIsOk = true;
case 's':
- if (dataHead.isSym() || dataHead.isIdent())
+ if (dataHead.isSym() || dataHead.isIdent() || dataHead.isInteger())
typeIsOk = true;
if (!typeIsOk)
diff --git a/Parser.cpp b/Parser.cpp
index fd07c79..c3d8330 100644
--- a/Parser.cpp
+++ b/Parser.cpp
@@ -78,7 +78,7 @@
while (peek().isDigit())
buffer += get();
- *node = T(buffer.toInt());
+ *node = T(buffer, 10);
return true;
}
diff --git a/Token.cpp b/Token.cpp
index ac104a4..20e2a16 100644
--- a/Token.cpp
+++ b/Token.cpp
@@ -38,6 +38,13 @@
}
template <typename T>
+TokenBase<T>::TokenBase(QString integer, int base)
+{
+ _type = INTEGER;
+ _intVal = integer.toInt(nullptr, base);
+}
+
+template <typename T>
TokenBase<T>::TokenBase(char varType, const QString name)
{
_type = VAR;
@@ -69,6 +76,12 @@
}
template <typename T>
+bool TokenBase<T>::isInteger() const
+{
+ return _type == INTEGER;
+}
+
+template <typename T>
TokenBase<T>::TokenBase() : TokenBase("Null")
{
}
@@ -93,6 +106,12 @@
}
template <typename T>
+int TokenBase<T>::integer() const
+{
+ return _intVal;
+}
+
+template <typename T>
char TokenBase<T>::varType() const
{
return _charVal.toLatin1();
@@ -119,6 +138,8 @@
return _charVal;
if (isVar())
return QString(_charVal) + "." + _stringVal;
+ if (isInteger())
+ return QString::number(_intVal, 10);
if (isParen())
{
QStringList parts;
@@ -142,7 +163,7 @@
template <typename T>
QString TokenBase<T>::typeToString(int type)
{
- static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR"};
+ static const QString typeNames[] = {"SYMBOL", "IDENT", "PAREN", "VAR", "INTEGER"};
return typeNames[type];
}
diff --git a/Token.h b/Token.h
index 75c4455..6e0450c 100644
--- a/Token.h
+++ b/Token.h
@@ -10,6 +10,7 @@
TokenBase();
TokenBase(const T &other);
+ TokenBase(QString integer, int base);
explicit TokenBase(QChar symbol);
explicit TokenBase(QString identifier);
explicit TokenBase(QList<T> parenthesized);
@@ -22,12 +23,14 @@
bool isIdent() const;
bool isParen() const;
bool isVar() const;
+ bool isInteger() const;
QList<T> parenContent();
char varType() const;
const QString &name() const;
QChar symbol() const;
+ int integer() const;
operator QString() const;
@@ -37,6 +40,7 @@
IDENT,
PAREN,
VAR,
+ INTEGER,
TOKEN_TYPE_LAST,
};
@@ -46,6 +50,7 @@
protected:
int _type = 0;
+ int _intVal = 0;
QString _stringVal = "";
QList<T> _listVal;
QChar _charVal = 0;
diff --git a/main.cpp b/main.cpp
index 765e380..9a276f0 100644
--- a/main.cpp
+++ b/main.cpp
@@ -9,6 +9,7 @@
#include "Evaluator.h"
#include "VarContext.h"
#include "Repl.h"
+#include "PPrint.h"
int g_numFailed = 0;
@@ -100,13 +101,20 @@
match(dataParser.parseMany<Token>(), patternParser.parseMany<Token>(), VarContext()));
}
-void testParseAst(QString string)
+void testParseAst(QString string, QList<AstNode> equals = {})
{
Parser parser{string};
QList<AstNode> result = parser.parseMany<AstNode>();
- qDebug() << "\033[36mParse\033[0m" << string << result;
+ if (!equals.empty() && result != equals)
+ {
+ g_numFailed++;
+ qDebug() << "\n\033[31mTEST FAILS:\033[0m";
+ qDebug() << "Expected" << pprint(equals);
+ }
+
+ qDebug() << "\033[36mParse\033[0m" << string << pprint(result);
}
void testParseFunc(QString string)
@@ -182,6 +190,9 @@
testMatch("(a)", "(a)");
testMatch("hello", "s.A e.Rest");
+ testMatch("123", "123");
+ testMatch("(123)", "t.a");
+ testMatch("(123)", "(s.a)");
}
void testAllParses()
@@ -198,6 +209,8 @@
testParseAst("(s.a) e.Middle s.a");
testParseAst("Hello; Goodbye");
testParseAst("Key = Value");
+ testParseAst("123", {AstNode("123", 10)});
+ testParseAst("12 00", {AstNode("12", 10), AstNode("0", 10)});
}
void testAllFunctionDefs()