Skip to content

Commit

Permalink
Allow easier aliasing.
Browse files Browse the repository at this point in the history
Rather than using "(set! new-name old-name)", which works, we should
use an "(alias new-name old-name)" function which expresses our intent
more cleanly.

This closes #38.
  • Loading branch information
skx committed Oct 16, 2022
1 parent 9c4f68d commit bd5524c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,24 @@ func (ev *Eval) eval(exp primitive.Primitive, e *env.Environment, expandMacro bo
// first token/symbol
switch listExp[0] {

// (alias ..)
case primitive.Symbol("alias"):
if len(listExp) != 3 {
return primitive.Error("Expected two arguments")
}

// Name we'll use
name := listExp[1]

// Existing function.
val := listExp[2]

old, ok := e.Get(val.ToString())
if ok {
e.Set(name.ToString(), old)
}
return primitive.Nil{}

// (do ..)
case primitive.Symbol("do"):
var ret primitive.Primitive
Expand Down
8 changes: 7 additions & 1 deletion eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ a
{`(split "steve" "")`, `(s t e v e)`},
{`(join (list "s" "t" "e" "v" "e"))`, `steve`},

// alias
{`(alias explode split) (explode "steve" "")`, `(s t e v e)`},
{`(alias ** #) (** 3 3)`, `27`},

// comparison
{"(< 1 3)", "#t"},
{"(< 10 3)", "#f"},
Expand All @@ -203,7 +207,7 @@ a
{"(= 10 10)", "#t"},
{"(= -1 -1)", "#t"},

// we have a LOT of built ins, but not 100
// we have a LOT of built ins, but not 200
{"(> (length (env)) 10)", "#t"},
{"(> (length (env)) 50)", "#t"},
{"(< (length (env)) 200)", "#t"},
Expand Down Expand Up @@ -266,6 +270,8 @@ a
{"{ :age 333 ", "nil"},
{"}}}}}}", "nil"},

{"(alias foo)", "ERROR{Expected two arguments}"},

// try / catch
{"(try 3)", "ERROR{arity-error: not enough arguments for (try ..)}"},
{"(try 3 3)", "ERROR{expected a list for argument, got 3}"},
Expand Down

0 comments on commit bd5524c

Please sign in to comment.