Skip to content

Commit

Permalink
Implement (exit) and (forever)
Browse files Browse the repository at this point in the history
This closes #133 - though there are no tests for either.
  • Loading branch information
skx committed Apr 13, 2023
1 parent e5015c3 commit e53a96e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func (ev *Eval) eval(exp primitive.Primitive, e *env.Environment, expandMacro bo

// For each default argument set it
for k, v := range proc.Defaults {
e.Set(k.ToString(),v)
e.Set(k.ToString(), v)
}

// For each of the arguments that have been supplied
Expand Down
11 changes: 5 additions & 6 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func TestEvaluate(t *testing.T) {
{"(fn* ( (3 3) ) a )", "ERROR{expected a symbol for an argument, got 3}"},
{"(fn* ( (a 3 c) ) a )", "ERROR{only two list items allowed for a default-value, got 3}"},


// literals
{":foo", ":foo"},

Expand Down Expand Up @@ -451,7 +450,7 @@ a
}

// This function tests (read)
func TestRead(t *testing.T ) {
func TestRead(t *testing.T) {

tst := `(read)`

Expand All @@ -467,7 +466,7 @@ func TestRead(t *testing.T ) {

// Ensure we read from a fake input
c := config.DefaultIO()
c.STDIN = strings.NewReader( "hello, world\n")
c.STDIN = strings.NewReader("hello, world\n")

// Environment will have a config
ev.SetIOConfig(c)
Expand All @@ -479,14 +478,14 @@ func TestRead(t *testing.T ) {
out := l.Evaluate(ev)

if out.ToString() != "hello, world" {
t.Fatalf("read '%s' from (read)", out )
t.Fatalf("read '%s' from (read)", out)
}

// Run it again - this time reading will fail
out = l.Evaluate(ev)

if !strings.Contains(out.ToString(), "failed to read from STDIN" ) {
t.Fatalf("(read) didn't give expected error, got '%s'", out )
if !strings.Contains(out.ToString(), "failed to read from STDIN") {
t.Fatalf("(read) didn't give expected error, got '%s'", out)
}

}
Expand Down
42 changes: 36 additions & 6 deletions eval/specials.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package eval
import (
"bufio"
"fmt"
"strings"
"sort"

"github.com/skx/yal/env"
"github.com/skx/yal/primitive"
"os"
"sort"
"strings"
)

// evalSpecialForm is invoked to execute one of our special forms.
Expand Down Expand Up @@ -148,6 +148,36 @@ func (ev *Eval) evalSpecialForm(name string, args []primitive.Primitive, e *env.
return primitive.Error(fmt.Sprintf("unexpected type for eval %V.", args[0])), true
}

case "exit":
ret := 0

if len(args) == 1 {

n, ok := args[0].(primitive.Number)
if ok {
ret = int(n)
}
}

os.Exit(ret)

// not reached
return nil, true

case "forever":

// We run the body forever.
for true {

// Process all the expressions
for _, x := range args {
_ = ev.eval(x, e, expandMacro)
}
}

// not reached
return nil, true

case "if":
if len(args) < 2 {
return primitive.ArityError(), true
Expand Down Expand Up @@ -237,10 +267,10 @@ func (ev *Eval) evalSpecialForm(name string, args []primitive.Primitive, e *env.
proc.Args = arguments
proc.Body = body
proc.Env = e
proc.Help= help
proc.Help = help
proc.Macro = false

return proc,true
return proc, true

case "let*":
// We need to have at least one argument.
Expand Down Expand Up @@ -399,7 +429,7 @@ func (ev *Eval) evalSpecialForm(name string, args []primitive.Primitive, e *env.
for _, entry := range ev.stdlib {
ret = append(ret, primitive.String(entry))
}
return ret,true
return ret, true

case "struct":
if len(args) <= 1 {
Expand Down

0 comments on commit e53a96e

Please sign in to comment.