rest of stuff

This commit is contained in:
2024-01-03 23:18:32 +01:00
parent 2926f07a0e
commit 8c542226aa
9 changed files with 91 additions and 14 deletions

35
clitests/comb.psil Normal file
View File

@@ -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)

9
clitests/comb.psil.ex Normal file
View File

@@ -0,0 +1,9 @@
1
2
1
4
4
2
1
3
3

20
clitests/decorate.psil Normal file
View File

@@ -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)))

View File

@@ -0,0 +1,5 @@
4
36
3
1
0

View File

@@ -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

View File

@@ -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<std::string_view, CellValType> 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<CellValType, std::string> 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")) {

View File

@@ -17,9 +17,9 @@
using namespace Command;
static std::unordered_map<std::string_view, CommandE> builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR},
{"cdr", CDR}, {"=", EQ}, {">", GT}, {"<", LT},
{"nil", NIL}, {"nil?", NILC}, {"atom", ATOM}};
static std::unordered_map<std::string_view, CommandE> 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;

View File

@@ -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 = "-+><?=!0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static const std::string alnum = "-+><?=!*/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static const std::string special = "().";
while (curpos != std::string_view::npos) {

View File

@@ -171,10 +171,16 @@ void VM::step() {
_stop = true;
} else if (poppedCmd == ADD) {
_s.push(_s.pop().val() + _s.pop().val());
} else if (poppedCmd == MULT) {
_s.push(_s.pop().val() * _s.pop().val());
} else if (poppedCmd == SUB) {
CellValType a1 = _s.pop().val();
CellValType a2 = _s.pop().val();
_s.push(a1 - a2);
} else if (poppedCmd == DIV) {
CellValType a1 = _s.pop().val();
CellValType a2 = _s.pop().val();
_s.push(a1 / a2);
} else if (poppedCmd == EQ) {
_s.push(_s.pop() == _s.pop() ? 1 : 0);
} else if (poppedCmd == LT) {