From 8c542226aa6bf610e660e43b474099d64d747f0e Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Wed, 3 Jan 2024 23:18:32 +0100 Subject: [PATCH] rest of stuff --- clitests/comb.psil | 35 +++++++++++++++++++++++++++++++++++ clitests/comb.psil.ex | 9 +++++++++ clitests/decorate.psil | 20 ++++++++++++++++++++ clitests/decorate.psil.ex | 5 +++++ clitests/testall.sh | 2 +- src/vm/include/Command.h | 20 +++++++++++--------- src/vm/src/Compiler.cpp | 6 +++--- src/vm/src/Parser.cpp | 2 +- src/vm/src/VM.cpp | 6 ++++++ 9 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 clitests/comb.psil create mode 100644 clitests/comb.psil.ex create mode 100644 clitests/decorate.psil create mode 100644 clitests/decorate.psil.ex diff --git a/clitests/comb.psil b/clitests/comb.psil new file mode 100644 index 0000000..2cd3c5f --- /dev/null +++ b/clitests/comb.psil @@ -0,0 +1,35 @@ +(define (equal? a b) + (if (nil? a) (if (nil? b) 1 0) + (if (atom a) + (if (atom b) (= a b) 0) + (if (atom b) 0 (if (equal? (car a) (car b)) (equal? (cdr a) (cdr b)) 0)) + ) + ) +) + +(define (empty? l) + (equal? l (nil)) +) + +(define (comb-impl list cur n) + (if (empty? list) + (if (= cur n) 1 0) + (+ + (comb-impl (cdr list) cur n) + (comb-impl (cdr list) (+ cur (car list)) n) + ) + ) +) + +(define (comb list n) + (comb-impl list 0 n)) + +(comb (quote(1)) 1) +(comb (quote(1 1)) 1) +(comb (quote(1 1)) 2) +(comb (quote(1 1 1 2)) 3) +(comb (quote(1 2 1 2)) 3) +(comb (quote(1 3 4 2)) 3) +(comb (quote(1 2 3 4 5)) 15) +(comb (quote(1 2 3 4 5)) 10) +(comb (quote(1 2 3 4 5)) 5) diff --git a/clitests/comb.psil.ex b/clitests/comb.psil.ex new file mode 100644 index 0000000..7857a52 --- /dev/null +++ b/clitests/comb.psil.ex @@ -0,0 +1,9 @@ +1 +2 +1 +4 +4 +2 +1 +3 +3 \ No newline at end of file diff --git a/clitests/decorate.psil b/clitests/decorate.psil new file mode 100644 index 0000000..4e72ea4 --- /dev/null +++ b/clitests/decorate.psil @@ -0,0 +1,20 @@ +(define (decorate f g) + (lambda (x) (g (f x))) +) + +(define (foldl op acc l) + (if (nil? l) + acc + (op (car l) (foldl op acc (cdr l))) + ) +) + +(define (plus a b) + (+ a b) +) + +((decorate (lambda (x) (+ x 1)) (lambda (x) (+ x 2))) 1) +((decorate (lambda (lst) (foldl plus 0 lst)) (lambda (x) (* x x))) (quote(1 2 3))) +((decorate (lambda (lst) (foldl plus 0 lst)) (lambda (x) (/ x 2))) (quote(1 2 3))) +((decorate (lambda (lst) (foldl plus 0 lst)) (lambda (x) (= x 0))) (nil)) +((decorate (lambda (lst) (foldl plus 0 lst)) (lambda (x) (= x 0))) (quote(1))) \ No newline at end of file diff --git a/clitests/decorate.psil.ex b/clitests/decorate.psil.ex new file mode 100644 index 0000000..2e86104 --- /dev/null +++ b/clitests/decorate.psil.ex @@ -0,0 +1,5 @@ +4 +36 +3 +1 +0 \ No newline at end of file diff --git a/clitests/testall.sh b/clitests/testall.sh index c38909c..9fb604e 100755 --- a/clitests/testall.sh +++ b/clitests/testall.sh @@ -5,7 +5,7 @@ FAILED=() for FILE in *.psil; do echo "TESTING $FILE" - $PSIL -f $FILE --repl- > $FILE.res + $PSIL -f $FILE --repl- --default_log_level:0 > $FILE.res if [ $? -ne 0 ]; then FAILED+=("test-"$FILE) continue diff --git a/src/vm/include/Command.h b/src/vm/include/Command.h index 7c84f62..c9cdf4c 100644 --- a/src/vm/include/Command.h +++ b/src/vm/include/Command.h @@ -44,19 +44,21 @@ namespace Command { EQ = 25, LT = 26, GT = 27, - NILC = 28 + NILC = 28, + MULT = 29, + DIV = 30 }; static inline std::unordered_map str_to_cmd{ - {"NIL", 1}, {"LDC", 2}, {"LD", 3}, {"SEL", 4}, {"JOIN", 5}, {"LDF", 6}, {"AP", 7}, - {"RET", 8}, {"DUM", 9}, {"RAP", 10}, {"STOP", 11}, {"ATOM", 12}, {"ADD", 13}, {"SUB", 14}, - {"READCHAR", 15}, {"PUTCHAR", 16}, {"PUTNUM", 17}, {"EVAL", 18}, {"PRINT", 19}, {"READ", 20}, {"CONS", 21}, - {"LDG", 22}, {"CAR", 23}, {"CDR", 24}, {"EQ", 25}, {"LT", 26}, {"GT", 27}, {"NILC", 28}}; + {"NIL", 1}, {"LDC", 2}, {"LD", 3}, {"SEL", 4}, {"JOIN", 5}, {"LDF", 6}, {"AP", 7}, {"RET", 8}, + {"DUM", 9}, {"RAP", 10}, {"STOP", 11}, {"ATOM", 12}, {"ADD", 13}, {"SUB", 14}, {"READCHAR", 15}, {"PUTCHAR", 16}, + {"PUTNUM", 17}, {"EVAL", 18}, {"PRINT", 19}, {"READ", 20}, {"CONS", 21}, {"LDG", 22}, {"CAR", 23}, {"CDR", 24}, + {"EQ", 25}, {"LT", 26}, {"GT", 27}, {"NILC", 28}, {"MULT", 29}, {"DIV", 30}}; static inline std::unordered_map cmd_to_str{ - {1, "NIL"}, {2, "LDC"}, {3, "LD"}, {4, "SEL"}, {5, "JOIN"}, {6, "LDF"}, {7, "AP"}, - {8, "RET"}, {9, "DUM"}, {10, "RAP"}, {11, "STOP"}, {12, "ATOM"}, {13, "ADD"}, {14, "SUB"}, - {15, "READCHAR"}, {16, "PUTCHAR"}, {17, "PUTNUM"}, {18, "EVAL"}, {19, "PRINT"}, {20, "READ"}, {21, "CONS"}, - {22, "LDG"}, {23, "CAR"}, {24, "CDR"}, {25, "EQ"}, {26, "LT"}, {27, "GT"}, {28, "NILC"}}; + {1, "NIL"}, {2, "LDC"}, {3, "LD"}, {4, "SEL"}, {5, "JOIN"}, {6, "LDF"}, {7, "AP"}, {8, "RET"}, + {9, "DUM"}, {10, "RAP"}, {11, "STOP"}, {12, "ATOM"}, {13, "ADD"}, {14, "SUB"}, {15, "READCHAR"}, {16, "PUTCHAR"}, + {17, "PUTNUM"}, {18, "EVAL"}, {19, "PRINT"}, {20, "READ"}, {21, "CONS"}, {22, "LDG"}, {23, "CAR"}, {24, "CDR"}, + {25, "EQ"}, {26, "LT"}, {27, "GT"}, {28, "NILC"}, {29, "MULT"}, {30, "DIV"}}; static inline Handle make_cmd(CellValType cmd) { if (Options::get_bool("command_strs")) { diff --git a/src/vm/src/Compiler.cpp b/src/vm/src/Compiler.cpp index 9bf2db1..fafc83f 100644 --- a/src/vm/src/Compiler.cpp +++ b/src/vm/src/Compiler.cpp @@ -17,9 +17,9 @@ using namespace Command; -static std::unordered_map builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR}, - {"cdr", CDR}, {"=", EQ}, {">", GT}, {"<", LT}, - {"nil", NIL}, {"nil?", NILC}, {"atom", ATOM}}; +static std::unordered_map builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR}, {"cdr", CDR}, + {"=", EQ}, {">", GT}, {"<", LT}, {"nil", NIL}, {"nil?", NILC}, + {"atom", ATOM}, {"*", MULT}, {"/", DIV}}; Handle Compiler::compile(const Handle &src, Handle fake_env, const Handle &suffix) { Handle out; diff --git a/src/vm/src/Parser.cpp b/src/vm/src/Parser.cpp index 7ffb39e..66cf93e 100644 --- a/src/vm/src/Parser.cpp +++ b/src/vm/src/Parser.cpp @@ -63,7 +63,7 @@ std::string_view Parser::Tokenizer::peek() const { return _tokens.front(); } void Parser::Tokenizer::load(std::string_view input) { std::string_view::size_type curpos = input.find_first_not_of(std::string{' ', '\n', '\r'}); - static const std::string alnum = "-+>