From 9323f05ee5ee4cced0134d6c0593b54621c3de47 Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Thu, 9 Nov 2023 22:25:54 +0300 Subject: [PATCH] Added tests for order of operations --- MiniScript-cpp/src/main.cpp | 5 + TestSuite.txt | 213 +++++++++++++++++++++++++++++++++++- 2 files changed, 217 insertions(+), 1 deletion(-) diff --git a/MiniScript-cpp/src/main.cpp b/MiniScript-cpp/src/main.cpp index 0829065..e8109af 100644 --- a/MiniScript-cpp/src/main.cpp +++ b/MiniScript-cpp/src/main.cpp @@ -199,6 +199,11 @@ static void DoOneIntegrationTest(List sourceLines, long sourceLineNum, void RunIntegrationTests(String path) { std::ifstream infile(path.c_str()); + if (!infile.is_open()) { + Print(String("\nFailed to open ") + path + "\n"); + return; + } + List sourceLines; List expectedOutput; long testLineNum = 0; diff --git a/TestSuite.txt b/TestSuite.txt index 481149f..58645cc 100644 --- a/TestSuite.txt +++ b/TestSuite.txt @@ -1734,4 +1734,215 @@ titlecase = function(s) end function print titlecase("AND NOW for something completely different") ---------------------------------------------------------------------- -And Now For Something Completely Different \ No newline at end of file +And Now For Something Completely Different +====================================================================== +==== Order of operations: assignment +p = function(first, second) + print first + return @second +end function +m = {} +m[p("LEFT", "x")] = p("RIGHT", 3) +m[p("LEFT", "x")] += p("RIGHT", 3) +m[p("LEFT", "x")] -= p("RIGHT", 3) +m[p("LEFT", "x")] *= p("RIGHT", 3) +m[p("LEFT", "x")] /= p("RIGHT", 3) +m[p("LEFT", "x")] ^= p("RIGHT", 3) +m[p("LEFT", "x")] %= p("RIGHT", 3) +---------------------------------------------------------------------- +LEFT +RIGHT +LEFT +RIGHT +LEFT +RIGHT +LEFT +RIGHT +LEFT +RIGHT +LEFT +RIGHT +LEFT +RIGHT +====================================================================== +==== Order of operations: boolean +p = function(first, second) + print first + return @second +end function +print p("LEFT", false) or p("RIGHT", true) +print p("LEFT", true) and p("RIGHT", false) +---------------------------------------------------------------------- +LEFT +RIGHT +1 +LEFT +RIGHT +0 +====================================================================== +==== Order of operations: isa +p = function(first, second) + print first + return @second +end function +print p("LEFT", {}) isa p("RIGHT", {}) +---------------------------------------------------------------------- +LEFT +RIGHT +0 +====================================================================== +==== Order of operations: comparisons +p = function(first, second) + print first + return @second +end function +print p("LEFT", 1) == p("RIGHT", 1) +print p("LEFT", 1) != p("RIGHT", 1) +print p("LEFT", 1) > p("RIGHT", 1) +print p("LEFT", 1) < p("RIGHT", 1) +print p("LEFT", 1) >= p("RIGHT", 1) +print p("LEFT", 1) <= p("RIGHT", 1) +print p("A", 1) == + p("B", 1) != + p("C", 1) > + p("D", 1) < + p("E", 1) >= + p("F", 1) <= + p("G", 1) +---------------------------------------------------------------------- +LEFT +RIGHT +1 +LEFT +RIGHT +0 +LEFT +RIGHT +0 +LEFT +RIGHT +0 +LEFT +RIGHT +1 +LEFT +RIGHT +1 +A +B +C +D +E +F +G +0 +====================================================================== +==== Order of operations: add and subtract +p = function(first, second) + print first + return @second +end function +print p("LEFT", 2) + p("RIGHT", 3) +print p("LEFT", 2) - p("RIGHT", 3) +print p("A", 2) + + p("B", 3) - + p("C", 4) + + p("D", 5) +---------------------------------------------------------------------- +LEFT +RIGHT +5 +LEFT +RIGHT +-1 +A +B +C +D +6 +====================================================================== +==== Order of operations: multiply, divide, modulo +p = function(first, second) + print first + return @second +end function +print p("LEFT", 3) * p("RIGHT", 2) +print p("LEFT", 3) / p("RIGHT", 2) +print p("LEFT", 3) % p("RIGHT", 2) +print p("A", 2) * + p("B", 3) / + p("C", 4) % + p("D", 5) * + p("E", 6) +---------------------------------------------------------------------- +LEFT +RIGHT +6 +LEFT +RIGHT +1.5 +LEFT +RIGHT +1 +A +B +C +D +E +9 +====================================================================== +==== Order of operations: power +p = function(first, second) + print first + return @second +end function +print p("LEFT", 2) ^ p("RIGHT", 3) +---------------------------------------------------------------------- +LEFT +RIGHT +8 +====================================================================== +==== Order of operations: indexing and slicing +p = function(first, second) + print first + return @second +end function +print p("OBJECT", {"x": "hello"})[ + p("KEY", "x") ] +print p("OBJECT", range(4))[ + p("START", 1) : + p("STOP", -1) ] +---------------------------------------------------------------------- +OBJECT +KEY +hello +OBJECT +START +STOP +[3, 2, 1] +====================================================================== +==== Order of operations: call operation and statement +p = function(first, second) + print first + return @second +end function +sum3 = function(a, b, c) + return a + b + c +end function +print p("CALLABLE", {"f": @sum3}).f( + p("ARG1", "a"), + p("ARG2", "b"), + p("ARG3", "c") ) +p("CALLABLE", @sum3) p("ARG1", "a"), + p("ARG2", "b"), + p("ARG3", "c") +---------------------------------------------------------------------- +CALLABLE +ARG1 +ARG2 +ARG3 +abc +CALLABLE +ARG1 +ARG2 +ARG3 \ No newline at end of file