Skip to content

Commit 17b9080

Browse files
committed
Example improved
1 parent 0f01aa3 commit 17b9080

2 files changed

Lines changed: 41 additions & 38 deletions

File tree

README.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -86,44 +86,45 @@ You execute lisp from Go code and get results from it back to Go. Example from [
8686

8787
```go
8888
func ExampleEVAL() {
89-
newEnv := env.NewEnv()
90-
91-
// Load required lisp libraries
92-
for _, library := range []struct {
93-
name string
94-
load func(newEnv types.EnvType) error
95-
}{
96-
{"core mal", nscore.Load},
97-
{"core mal with input", nscore.LoadInput},
98-
{"command line args", nscore.LoadCmdLineArgs},
99-
{"core mal extended", nscoreextended.Load},
100-
{"assert", nsassert.Load},
101-
} {
102-
if err := library.load(newEnv); err != nil {
103-
log.Fatalf("Library Load Error: %v", err)
104-
}
105-
}
106-
107-
// parse (READ) lisp code
108-
ast, err := lisp.READ(`(+ 2 2)`, nil)
109-
if err != nil {
110-
log.Fatalf("READ error: %v", err)
111-
}
112-
113-
// eval AST
114-
result, err := lisp.EVAL(ast, newEnv, nil)
115-
if err != nil {
116-
log.Fatalf("EVAL error: %v", err)
117-
}
118-
119-
// use result
120-
if result.(int) != 4 {
121-
log.Fatalf("Result check error: %v", err)
122-
}
123-
124-
// optionally print resulting AST
125-
fmt.Println(lisp.PRINT(result))
126-
// Output: 4
89+
newEnv := env.NewEnv()
90+
91+
// Load required lisp libraries
92+
for _, library := range []struct {
93+
name string
94+
load func(newEnv types.EnvType) error
95+
}{
96+
{"core mal", nscore.Load},
97+
{"core mal with input", nscore.LoadInput},
98+
{"command line args", nscore.LoadCmdLineArgs},
99+
{"concurrent", nsconcurrent.Load},
100+
{"core mal extended", nscoreextended.Load},
101+
{"system", nssystem.Load},
102+
} {
103+
if err := library.load(newEnv); err != nil {
104+
log.Fatalf("Library Load Error %q: %v", library.name, err)
105+
}
106+
}
107+
108+
// parse (READ) lisp code
109+
ast, err := lisp.READ(`(+ 2 2)`, nil, newEnv)
110+
if err != nil {
111+
log.Fatalf("READ error: %v", err)
112+
}
113+
114+
// eval AST
115+
result, err := lisp.EVAL(context.TODO(), ast, newEnv)
116+
if err != nil {
117+
log.Fatalf("EVAL error: %v", err)
118+
}
119+
120+
// use result
121+
if result.(int) != 4 {
122+
log.Fatalf("Result check error: %v", err)
123+
}
124+
125+
// optionally print resulting AST
126+
fmt.Println(lisp.PRINT(result))
127+
// Output: 4
127128
}
128129
```
129130

example/example_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/jig/lisp/lib/concurrent/nsconcurrent"
1111
"github.com/jig/lisp/lib/core/nscore"
1212
"github.com/jig/lisp/lib/coreextented/nscoreextended"
13+
"github.com/jig/lisp/lib/system/nssystem"
1314
"github.com/jig/lisp/types"
1415
)
1516

@@ -26,6 +27,7 @@ func ExampleEVAL() {
2627
{"command line args", nscore.LoadCmdLineArgs},
2728
{"concurrent", nsconcurrent.Load},
2829
{"core mal extended", nscoreextended.Load},
30+
{"system", nssystem.Load},
2931
} {
3032
if err := library.load(newEnv); err != nil {
3133
log.Fatalf("Library Load Error %q: %v", library.name, err)

0 commit comments

Comments
 (0)