This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
builtins.go
98 lines (83 loc) · 2.31 KB
/
builtins.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package parens
import (
"fmt"
)
var (
_ Analyzer = (*BuiltinAnalyzer)(nil)
_ Expander = (*builtinExpander)(nil)
)
// BuiltinAnalyzer parses builtin value forms and returns Expr that can
// be evaluated against parens Env. Custom special form parsers can be
// set using WithAnalyzer().
type BuiltinAnalyzer struct {
SpecialForms map[string]ParseSpecial
}
// ParseSpecial validates a special form invocation, parse the form and
// returns an expression that can be evaluated for result.
type ParseSpecial func(env *Env, args Seq) (Expr, error)
// Analyze performs syntactic analysis of given form and returns an Expr
// that can be evaluated for result against an Env.
func (ba BuiltinAnalyzer) Analyze(env *Env, form Any) (Expr, error) {
if IsNil(form) {
return &ConstExpr{Const: Nil{}}, nil
}
switch f := form.(type) {
case Symbol:
v := env.Resolve(string(f))
if v == nil {
return nil, Error{
Cause: ErrNotFound,
Message: string(f),
}
}
return &ConstExpr{Const: v}, nil
case Seq:
cnt, err := f.Count()
if err != nil {
return nil, err
} else if cnt == 0 {
break
}
return ba.analyzeSeq(env, f)
}
return &ConstExpr{Const: form}, nil
}
func (ba BuiltinAnalyzer) analyzeSeq(env *Env, seq Seq) (Expr, error) {
// Analyze the call target. This is the first item in the sequence.
first, err := seq.First()
if err != nil {
return nil, err
}
// The call target may be a special form. In this case, we need to get the
// corresponding parser function, which will take care of parsing/analyzing
// the tail.
if sym, ok := first.(Symbol); ok {
if parse, found := ba.SpecialForms[string(sym)]; found {
next, err := seq.Next()
if err != nil {
return nil, err
}
return parse(env, next)
}
}
// Call target is not a special form and must be a Invokable. Analyze
// the arguments and create an InvokeExpr.
ie := InvokeExpr{Name: fmt.Sprintf("%s", first)}
err = ForEach(seq, func(item Any) (done bool, err error) {
if ie.Target == nil {
ie.Target, err = ba.Analyze(env, first)
return
}
var arg Expr
if arg, err = ba.Analyze(env, item); err == nil {
ie.Args = append(ie.Args, arg)
}
return
})
return &ie, err
}
type builtinExpander struct{}
func (be builtinExpander) Expand(_ *Env, _ Any) (Any, error) {
// TODO: implement macro expansion.
return nil, nil
}