mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-28 18:57:48 +01:00
rest of stuff
This commit is contained in:
35
clitests/comb.psil
Normal file
35
clitests/comb.psil
Normal 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
9
clitests/comb.psil.ex
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
1
|
||||||
|
4
|
||||||
|
4
|
||||||
|
2
|
||||||
|
1
|
||||||
|
3
|
||||||
|
3
|
||||||
20
clitests/decorate.psil
Normal file
20
clitests/decorate.psil
Normal 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)))
|
||||||
5
clitests/decorate.psil.ex
Normal file
5
clitests/decorate.psil.ex
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
4
|
||||||
|
36
|
||||||
|
3
|
||||||
|
1
|
||||||
|
0
|
||||||
@@ -5,7 +5,7 @@ FAILED=()
|
|||||||
|
|
||||||
for FILE in *.psil; do
|
for FILE in *.psil; do
|
||||||
echo "TESTING $FILE"
|
echo "TESTING $FILE"
|
||||||
$PSIL -f $FILE --repl- > $FILE.res
|
$PSIL -f $FILE --repl- --default_log_level:0 > $FILE.res
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
FAILED+=("test-"$FILE)
|
FAILED+=("test-"$FILE)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -44,19 +44,21 @@ namespace Command {
|
|||||||
EQ = 25,
|
EQ = 25,
|
||||||
LT = 26,
|
LT = 26,
|
||||||
GT = 27,
|
GT = 27,
|
||||||
NILC = 28
|
NILC = 28,
|
||||||
|
MULT = 29,
|
||||||
|
DIV = 30
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline std::unordered_map<std::string_view, CellValType> str_to_cmd{
|
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},
|
{"NIL", 1}, {"LDC", 2}, {"LD", 3}, {"SEL", 4}, {"JOIN", 5}, {"LDF", 6}, {"AP", 7}, {"RET", 8},
|
||||||
{"RET", 8}, {"DUM", 9}, {"RAP", 10}, {"STOP", 11}, {"ATOM", 12}, {"ADD", 13}, {"SUB", 14},
|
{"DUM", 9}, {"RAP", 10}, {"STOP", 11}, {"ATOM", 12}, {"ADD", 13}, {"SUB", 14}, {"READCHAR", 15}, {"PUTCHAR", 16},
|
||||||
{"READCHAR", 15}, {"PUTCHAR", 16}, {"PUTNUM", 17}, {"EVAL", 18}, {"PRINT", 19}, {"READ", 20}, {"CONS", 21},
|
{"PUTNUM", 17}, {"EVAL", 18}, {"PRINT", 19}, {"READ", 20}, {"CONS", 21}, {"LDG", 22}, {"CAR", 23}, {"CDR", 24},
|
||||||
{"LDG", 22}, {"CAR", 23}, {"CDR", 24}, {"EQ", 25}, {"LT", 26}, {"GT", 27}, {"NILC", 28}};
|
{"EQ", 25}, {"LT", 26}, {"GT", 27}, {"NILC", 28}, {"MULT", 29}, {"DIV", 30}};
|
||||||
static inline std::unordered_map<CellValType, std::string> cmd_to_str{
|
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"},
|
{1, "NIL"}, {2, "LDC"}, {3, "LD"}, {4, "SEL"}, {5, "JOIN"}, {6, "LDF"}, {7, "AP"}, {8, "RET"},
|
||||||
{8, "RET"}, {9, "DUM"}, {10, "RAP"}, {11, "STOP"}, {12, "ATOM"}, {13, "ADD"}, {14, "SUB"},
|
{9, "DUM"}, {10, "RAP"}, {11, "STOP"}, {12, "ATOM"}, {13, "ADD"}, {14, "SUB"}, {15, "READCHAR"}, {16, "PUTCHAR"},
|
||||||
{15, "READCHAR"}, {16, "PUTCHAR"}, {17, "PUTNUM"}, {18, "EVAL"}, {19, "PRINT"}, {20, "READ"}, {21, "CONS"},
|
{17, "PUTNUM"}, {18, "EVAL"}, {19, "PRINT"}, {20, "READ"}, {21, "CONS"}, {22, "LDG"}, {23, "CAR"}, {24, "CDR"},
|
||||||
{22, "LDG"}, {23, "CAR"}, {24, "CDR"}, {25, "EQ"}, {26, "LT"}, {27, "GT"}, {28, "NILC"}};
|
{25, "EQ"}, {26, "LT"}, {27, "GT"}, {28, "NILC"}, {29, "MULT"}, {30, "DIV"}};
|
||||||
|
|
||||||
static inline Handle make_cmd(CellValType cmd) {
|
static inline Handle make_cmd(CellValType cmd) {
|
||||||
if (Options::get_bool("command_strs")) {
|
if (Options::get_bool("command_strs")) {
|
||||||
|
|||||||
@@ -17,9 +17,9 @@
|
|||||||
using namespace Command;
|
using namespace Command;
|
||||||
|
|
||||||
|
|
||||||
static std::unordered_map<std::string_view, CommandE> builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR},
|
static std::unordered_map<std::string_view, CommandE> builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR}, {"cdr", CDR},
|
||||||
{"cdr", CDR}, {"=", EQ}, {">", GT}, {"<", LT},
|
{"=", EQ}, {">", GT}, {"<", LT}, {"nil", NIL}, {"nil?", NILC},
|
||||||
{"nil", NIL}, {"nil?", NILC}, {"atom", ATOM}};
|
{"atom", ATOM}, {"*", MULT}, {"/", DIV}};
|
||||||
|
|
||||||
Handle Compiler::compile(const Handle &src, Handle fake_env, const Handle &suffix) {
|
Handle Compiler::compile(const Handle &src, Handle fake_env, const Handle &suffix) {
|
||||||
Handle out;
|
Handle out;
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ std::string_view Parser::Tokenizer::peek() const { return _tokens.front(); }
|
|||||||
void Parser::Tokenizer::load(std::string_view input) {
|
void Parser::Tokenizer::load(std::string_view input) {
|
||||||
std::string_view::size_type curpos = input.find_first_not_of(std::string{' ', '\n', '\r'});
|
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 = "().";
|
static const std::string special = "().";
|
||||||
|
|
||||||
while (curpos != std::string_view::npos) {
|
while (curpos != std::string_view::npos) {
|
||||||
|
|||||||
@@ -171,10 +171,16 @@ void VM::step() {
|
|||||||
_stop = true;
|
_stop = true;
|
||||||
} else if (poppedCmd == ADD) {
|
} else if (poppedCmd == ADD) {
|
||||||
_s.push(_s.pop().val() + _s.pop().val());
|
_s.push(_s.pop().val() + _s.pop().val());
|
||||||
|
} else if (poppedCmd == MULT) {
|
||||||
|
_s.push(_s.pop().val() * _s.pop().val());
|
||||||
} else if (poppedCmd == SUB) {
|
} else if (poppedCmd == SUB) {
|
||||||
CellValType a1 = _s.pop().val();
|
CellValType a1 = _s.pop().val();
|
||||||
CellValType a2 = _s.pop().val();
|
CellValType a2 = _s.pop().val();
|
||||||
_s.push(a1 - a2);
|
_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) {
|
} else if (poppedCmd == EQ) {
|
||||||
_s.push(_s.pop() == _s.pop() ? 1 : 0);
|
_s.push(_s.pop() == _s.pop() ? 1 : 0);
|
||||||
} else if (poppedCmd == LT) {
|
} else if (poppedCmd == LT) {
|
||||||
|
|||||||
Reference in New Issue
Block a user