tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit d1a09ddb51f3be70521a6b9124b19e64c4792215
parent 1b74e5b352146c46ad66d9d3843c8eb08426e014
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat,  9 Jan 2021 00:48:46 -0800

Rename lambda and macro to Func and Macro

Support single argument macro like functions.

Diffstat:
test.c | 22+++++++++++-----------
tib/core.tsp | 22+++++++++++-----------
tib/doc.tsp | 13+++++++------
tisp.c | 20+++++++++++---------
4 files changed, 40 insertions(+), 37 deletions(-)

diff --git a/test.c b/test.c @@ -178,8 +178,8 @@ char *tests[][2] = { { "(= '(1 2 3) '(1))", "Nil" }, { "(= '((1 2) 3 4) '((1 2) 3 4))", "True" }, { "(= '((1 b) 3 4) '((1 2) 3 4))", "Nil" }, - { "(= (lambda (it) it) @it)", "True" }, - { "(= @it (lambda (x) x))", "Nil" }, + { "(= (Func (it) it) @it)", "True" }, + { "(= @it (Func (x) x))", "Nil" }, { "def", NULL }, { "(def foo 4)", "(Void)" }, @@ -212,12 +212,12 @@ char *tests[][2] = { { "(defined? when)", "True" }, { "(defined? apply)", "True" }, - { "lambda", NULL }, - { "((lambda (x) x) 3)", "3" }, - { "((lambda (x) x) (+ 1 2))", "3" }, - { "((lambda (x) (+ x 1)) 8)", "9" }, - { "((lambda (a b) (+ a b)) 2 2)", "4" }, - { "((lambda () 5))", "5" }, + { "Func", NULL }, + { "((Func (x) x) 3)", "3" }, + { "((Func (x) x) (+ 1 2))", "3" }, + { "((Func (x) (+ x 1)) 8)", "9" }, + { "((Func (a b) (+ a b)) 2 2)", "4" }, + { "((Func () 5))", "5" }, { "control", NULL }, { "(if True 1 2)", "1" }, @@ -303,7 +303,7 @@ char *tests[][2] = { { "(apply list '(a b c d e))", "(a b c d e)" }, { "(map car '((1 a) (2 b) (3 c)))", "(1 2 3)" }, { "(map cdr '((1 a) (2 b) (3 c)))", "((a) (b) (c))" }, - { "(map (lambda (x) (car (cdr x))) '((1 a) (2 b) (3 c)))", "(a b c)" }, + { "(map (Func (x) (car (cdr x))) '((1 a) (2 b) (3 c)))", "(a b c)" }, { "(map cadr '((1/2 .5) (\"conky\" .25) ('bubbles .125)))", "(0.5 0.25 0.125)" }, { "(map inc (list 2 4 8 (^ 2 4)))", "(3 5 9 17)" }, { "(filter positive? '(1 2 -4 5 -9 10))", "(1 2 5 10)" }, @@ -341,8 +341,8 @@ char *tests[][2] = { { "member", NULL }, { "(memp even? (list 1 3 19 4 7 8 2))", "(4 7 8 2)" }, { "(memp negative? (list 1/3 pi 3.2e-9 0 4 -7 2))", "(-7 2)" }, - { "(memp (lambda (x) (> x 8)) '(1/3 1/2 5/3 8 9))", "(9)" }, - { "(memp (lambda (x) (= x \"fry\")) " + { "(memp (Func (x) (> x 8)) '(1/3 1/2 5/3 8 9))", "(9)" }, + { "(memp (Func (x) (= x \"fry\")) " "'(\"fry\" \"nibbler\" \"prof\"))", "(fry nibbler prof)" }, { "(member 'foo '(foo bar baz))", "(foo bar baz)" }, { "(member 'bar '(foo bar baz))", "(bar baz)" }, diff --git a/tib/core.tsp b/tib/core.tsp @@ -3,10 +3,10 @@ (def quit "type (quit) or press CTRL-D to exit REPL") (def defmacro - (macro (args . body) + (Macro (args . body) (cond ((pair? args) - (list 'def (car args) (list 'macro (cdr args) . body))) + (list 'def (car args) (list 'Macro (cdr args) . body))) (else (error 'defmacro "expected macro name and argument List, recieved " (typeof args)))))) @@ -107,10 +107,10 @@ (list 'cond (list (list not con) (cons 'do body)))) (defmacro (let vars . body) - (list (list* 'lambda () + (list (list* 'Func () (append (map - (lambda (x) + (Func (x) (list* 'def (car x) (cdr x))) vars) body)))) @@ -118,13 +118,13 @@ (defmacro (recur proc vars . body) (list 'let (list* - (list proc (list* 'lambda (map car vars) body)) + (list proc (list* 'Func (map car vars) body)) vars) (list* proc (map car vars)))) (defmacro (switch val . body) (list* 'cond (map - (lambda (line) + (Func (line) `((= ,val ,(car line)) ,(cadr line))) body))) @@ -260,7 +260,7 @@ ; TODO many args (def (apply proc args) - (eval (map (lambda (x) ; prevent args from being evaluated twice + (eval (map (Func (x) ; prevent args from being evaluated twice (list 'quote x)) (cons proc args)))) @@ -279,10 +279,10 @@ (def (compose . procs) (cond - ((nil? procs) (lambda x x)) + ((nil? procs) (Func x x)) ((nil? (cdr procs)) (car procs)) (else - (lambda x + (Func x ((car procs) (apply (apply compose (cdr procs)) x)))))) (def (reverse l) @@ -317,9 +317,9 @@ (else (memp proc (cdr lst))))) (def (member mem lst) - (memp (lambda (x) (= mem x)) lst)) + (memp (Func (x) (= mem x)) lst)) -; def English list element accessors +; define English list element accessors (def rest cdr) ; TODO first and rest are generics for list, vec, str types (def first car) (recur f ((n 1) diff --git a/tib/doc.tsp b/tib/doc.tsp @@ -32,14 +32,15 @@ (get "(get val prop)" "Get the property of the given value depending on its type") - (lambda - "(lambda args . body)" - "(lambda body)" + (Func + "(Func args . body)" + "(Func body)" "Create anonymous function" " If only body is given, create function with one argument labeled 'it'") - (macro - "(macro args . body)" - "Create anonymous macro") + (Macro + "(Macro args . body)" + "Create anonymous macro, arguments are not evaluated before given to body" + " If only body is given, create macro with one argument labeled 'it'") (def "(def var . val)" "Create new variable with value" diff --git a/tisp.c b/tisp.c @@ -560,7 +560,7 @@ tisp_read(Tsp st) "`", "quasiquote", ",@", "unquote-splice", /* always check before , */ ",", "unquote", - "@", "lambda", + "@", "Func", }; skip_ws(st, 1); if (strlen(st->file+st->filec) == 0) /* empty list */ @@ -923,12 +923,12 @@ prim_get(Tsp st, Hash env, Val args) prop->v.s, type_str(v->t)); } -/* creates new tisp lambda function */ +/* creates new tisp function */ static Val -form_lambda(Tsp st, Hash env, Val args) +form_Func(Tsp st, Hash env, Val args) { Val params, body; - tsp_arg_min(args, "lambda", 1); + tsp_arg_min(args, "Func", 1); if (nilp(cdr(args))) { /* if only 1 argument is given, auto fill func parameters */ params = mk_pair(mk_sym(st, "it"), st->nil); body = args; @@ -941,10 +941,12 @@ form_lambda(Tsp st, Hash env, Val args) /* creates new tisp defined macro */ static Val -form_macro(Tsp st, Hash env, Val args) +form_Macro(Tsp st, Hash env, Val args) { - tsp_arg_min(args, "macro", 2); - return mk_func(TSP_MACRO, NULL, car(args), cdr(args), env); + tsp_arg_min(args, "Macro", 1); + Val ret = form_Func(st, env, args); + ret->t = TSP_MACRO; + return ret; } /* creates new variable of given name and value @@ -1125,8 +1127,8 @@ tisp_env_init(size_t cap) tsp_env_form(cond); tsp_env_prim(typeof); tsp_env_prim(get); - tsp_env_form(lambda); - tsp_env_form(macro); + tsp_env_form(Func); + tsp_env_form(Macro); tsp_env_form(def); tsp_env_name_form(set!, set); tsp_env_prim(load);