-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_addpreamble_test.go
59 lines (52 loc) · 1013 Bytes
/
example_addpreamble_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package lisp_test
import (
"context"
"fmt"
"log"
"github.com/jig/lisp"
"github.com/jig/lisp/env"
"github.com/jig/lisp/lib/core/nscore"
"github.com/jig/lisp/types"
)
func ExampleAddPreamble() {
// incrementInLisp is sample function implemented in Lisp
result, err := incrementInLisp(2)
if err != nil {
log.Fatalf("eval error: %s", err)
}
fmt.Printf("result: %d\n", result)
// Output:
// result: 3
}
const incrementInLispSourceCode = "(+ 1 $ARG)"
func incrementInLisp(arg int) (int, error) {
ns := env.NewEnv()
nscore.Load(ns) // to load '+' function
preamble := map[string]types.MalType{
"$ARG": arg,
}
sourceCode, err := lisp.AddPreamble(
incrementInLispSourceCode,
preamble,
)
if err != nil {
return 0, err
}
ast, err := lisp.READWithPreamble(
sourceCode,
types.NewCursorFile("ExampleREAD"),
ns,
)
if err != nil {
return 0, err
}
result, err := lisp.EVAL(
context.Background(),
ast,
ns,
)
if err != nil {
return 0, err
}
return result.(int), nil
}