Skip to content

Commit 94a3045

Browse files
committed
"do" not needed for side-effects in functions
1 parent 535cb7e commit 94a3045

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

dolessfn_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package lisp_test
2+
3+
import (
4+
"context"
5+
_ "embed"
6+
"log"
7+
"testing"
8+
9+
"github.com/jig/lisp"
10+
"github.com/jig/lisp/env"
11+
"github.com/jig/lisp/lib/core/nscore"
12+
"github.com/jig/lisp/types"
13+
)
14+
15+
//go:embed dolessfn_test.lisp
16+
var dolessfn_test string
17+
18+
func TestDoLessFunction(t *testing.T) {
19+
ns := env.NewEnv()
20+
for _, library := range []struct {
21+
name string
22+
load func(ns types.EnvType) error
23+
}{
24+
{"core mal", nscore.Load},
25+
// {"core mal with input", nscore.LoadInput},
26+
// {"command line args", nscore.LoadCmdLineArgs},
27+
// {"concurrent", nsconcurrent.Load},
28+
// {"core mal extended", nscoreextended.Load},
29+
// {"assert", nsassert.Load},
30+
} {
31+
if err := library.load(ns); err != nil {
32+
log.Fatalf("Library Load Error: %v\n", err)
33+
}
34+
}
35+
36+
ast, err := lisp.READ(dolessfn_test, nil, nil)
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
res, err := lisp.EVAL(context.Background(), ast, ns)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
if !res.(bool) {
45+
t.Fatalf(`res.(int) != 4 (res.(int) == %d)`, res)
46+
}
47+
}

dolessfn_test.lisp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(do
2+
(def sum-with-sideeffects (fn [x y]
3+
;; this area is executed only for side effects
4+
(- 1 1)
5+
;; This is returns the value of the function
6+
(+ x y)))
7+
(assert (= 7 (sum-with-sideeffects 3 4)))
8+
true)

mal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func EVAL(ctx context.Context, ast MalType, env EnvType) (res MalType, e error)
567567
case "fn":
568568
fn := MalFunc{
569569
Eval: EVAL,
570-
Exp: a2,
570+
Exp: List{Val: append([]MalType{Symbol{Val: "do"}}, ast.(List).Val[2:]...)},
571571
Env: env,
572572
Params: a1,
573573
IsMacro: false,

0 commit comments

Comments
 (0)