-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmacro.go
106 lines (91 loc) · 2.87 KB
/
macro.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
99
100
101
102
103
104
105
106
// Copyright 2014 SteelSeries ApS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements user defined macros.
package golisp
import (
"errors"
"fmt"
)
type Macro struct {
Name string
Params *Data
VarArgs bool
RequiredArgCount int
Body *Data
Env *SymbolTableFrame
}
func MakeMacro(name string, params *Data, body *Data, parentEnv *SymbolTableFrame) *Macro {
requiredArgs, varArgs := computeRequiredArgumentCount(params)
return &Macro{Name: name, Params: params, VarArgs: varArgs, RequiredArgCount: requiredArgs, Body: body, Env: parentEnv}
}
func (self *Macro) String() string {
return fmt.Sprintf("<macro: %s>", self.Name)
}
func (self *Macro) makeLocalBindings(args *Data, argEnv *SymbolTableFrame, localEnv *SymbolTableFrame, eval bool) (err error) {
if self.VarArgs {
if Length(args) < self.RequiredArgCount {
return errors.New(fmt.Sprintf("%s expected at least %d parameters, received %d.", self.Name, self.RequiredArgCount, Length(args)))
}
} else {
if Length(args) != self.RequiredArgCount {
return errors.New(fmt.Sprintf("%s expected %d parameters, received %d.", self.Name, self.RequiredArgCount, Length(args)))
}
}
var argValue *Data
var accumulatingParam *Data = nil
accumulatedArgs := make([]*Data, 0)
for p, a := self.Params, args; NotNilP(a); a = Cdr(a) {
if eval {
argValue, err = Eval(Car(a), argEnv)
if err != nil {
return
}
} else {
argValue = Car(a)
}
if accumulatingParam != nil {
accumulatedArgs = append(accumulatedArgs, argValue)
} else {
_, err = localEnv.BindLocallyTo(Car(p), argValue)
if err != nil {
return
}
}
if accumulatingParam == nil {
p = Cdr(p)
}
if SymbolP(p) {
accumulatingParam = p
}
}
if accumulatingParam != nil {
_, err = localEnv.BindLocallyTo(accumulatingParam, ArrayToList(accumulatedArgs))
if err != nil {
return
}
}
return nil
}
func (self *Macro) Expand(args *Data, argEnv *SymbolTableFrame) (result *Data, err error) {
localEnv := NewSymbolTableFrameBelow(self.Env, self.Name)
err = self.makeLocalBindings(args, argEnv, localEnv, false)
if err != nil {
return
}
return Eval(self.Body, localEnv)
}
func (self *Macro) internalApply(args *Data, argEnv *SymbolTableFrame, eval bool) (result *Data, err error) {
expandedMacro, err := self.Expand(args, argEnv)
if err != nil {
return
}
return Eval(expandedMacro, argEnv)
}
func (self *Macro) Apply(args *Data, argEnv *SymbolTableFrame) (result *Data, err error) {
return self.internalApply(args, argEnv, false)
}
func (self *Macro) ApplyWithoutEval(args *Data, argEnv *SymbolTableFrame) (result *Data, err error) {
return self.internalApply(args, argEnv, false)
}