-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (37 loc) · 892 Bytes
/
Copy pathmain.go
File metadata and controls
47 lines (37 loc) · 892 Bytes
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
package main
import (
"errors"
"os"
"nondv.io/glisp/interpreter"
. "nondv.io/glisp/types"
)
func main() {
bindings := interpreter.BuildBaseBindings()
bindings = bindings.Assoc(BuildSymbol("sqr"), BuildNativeFn(nativeSqr))
// No arguments provided
if len(os.Args) == 1 {
interpreter.Repl(bindings)
return
}
filename := os.Args[1]
contents, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
lastResult, err := interpreter.ReadEvalAll(bindings, string(contents))
if err != nil {
panic(err)
}
interpreter.Print(lastResult)
}
// example of extending the language
func nativeSqr(bindings *Bindings, args *Value) (*Value, error) {
if args.ListLength() != 1 {
return nil, errors.New("Only one argument expected")
}
if !args.Car().IsInteger() {
return nil, errors.New("Integer expected")
}
n := args.Car().ToInt()
return BuildInteger(n * n), nil
}