Snippets
Created by
Patrick Logan
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | package nconc
import (
"fmt"
)
type Conditional struct {
Test, Yes, No Expression
}
type ConditionalK struct {
Yes, No Expression
NextK Continuation
}
type Sequence []Expression
type SequenceK struct {
Es []Expression
NextK Continuation
}
type Quote struct {
D Datum
}
type Definition struct {
Id Datum
E Expression
}
type DefinitionK struct {
Id Datum
NextK Continuation
}
type Assignment struct {
Id Datum
E Expression
}
type AssignmentK struct {
Id Datum
NextK Continuation
}
type Combination []Expression
type CombinationK struct {
Es []Expression // Execute each expression of a combination via successive CombinationK continuations
Ds []Datum // Collect the data, giving the final expression an Applicationk continuation
NextK Continuation
}
type Application []Datum // Apply the first datum, a procedure, to the remaining data, the actual arguments
type ApplicationK struct {
Ds []Datum // Collect the data from evaluating each element of a combination then apply via Application
NextK Continuation
}
type GlobalReference string
type LexicalReference struct {
Frame, Offset int
}
func (k ApplicationK) Invoke(result Datum, rte *RuntimeFrame, interp *Interpreter) (b Bouncer, d Datum, err error) {
b = Bouncer{E: Application(append(k.Ds, result)), F: rte, K: k.NextK}
return
}
func (a Application) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
if len(a) == 0 {
err = ImplementationError("An application must have a callable procedure")
return
}
c, ok := a[0].(Callable)
if !ok {
err = ProcedureExpectedError{a[0]}
return
}
var actuals []Datum
if len(a) > 1 {
actuals = a[1:]
}
var values Datum
b, values, err = c.Call(rte, rtk, interp, actuals...)
if err != nil {
return
}
if values != nil {
b, d, err = rtk.Invoke(values, rte, interp)
}
return
}
func (k CombinationK) Invoke(result Datum, rte *RuntimeFrame, interp *Interpreter) (b Bouncer, d Datum, err error) {
switch len(k.Es) {
case 0:
err = ImplementationError("A combination continuation must have at least one expression to execute")
return
case 1:
k1 := ApplicationK{Ds: append(k.Ds, result), NextK: k.NextK}
b = Bouncer{E: k.Es[0], F: rte, K: k1}
return
default:
k1 := CombinationK{Es: k.Es[1:], Ds: append(k.Ds, result), NextK: k.NextK}
b = Bouncer{E: k.Es[0], F: rte, K: k1}
return
}
}
func (c Combination) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
switch len(c) {
case 0:
err = EmptyListError{}
return
case 1:
k := ApplicationK{NextK: rtk}
b = Bouncer{E: c[0], F: rte, K: k}
return
default:
k := CombinationK{Es: c[1:], NextK: rtk}
b = Bouncer{E: c[0], F: rte, K: k}
return
}
}
// Currently allows redefinitions, no need to use SET!
func (k DefinitionK) Invoke(result Datum, rte *RuntimeFrame, interp *Interpreter) (b Bouncer, d Datum, err error) {
var s *Symbol
s, ok := k.Id.(*Symbol)
if !ok {
err = ImplementationError(fmt.Sprintf("Identifier expected, %T", k.Id))
return
}
interp.TopLevel[s] = result
k.NextK.Invoke(Void{}, rte, interp)
return
}
func (def Definition) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
k := DefinitionK{Id: def.Id, NextK: rtk}
b = Bouncer{E: def.E, F: rte, K: k}
return
}
func (k AssignmentK) Invoke(result Datum, rte *RuntimeFrame, interp *Interpreter) (b Bouncer, d Datum, err error) {
var s *Symbol
s, ok := k.Id.(*Symbol)
if !ok {
err = ImplementationError(fmt.Sprintf("Identifier expected, %T", k.Id))
return
}
interp.TopLevel[s] = result
b, d, err = k.NextK.Invoke(Void{}, rte, interp)
return
}
func (def Assignment) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
k := AssignmentK{Id: def.Id, NextK: rtk}
b = Bouncer{E: def.E, F: rte, K: k}
return
}
func (ref GlobalReference) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
s := interp.Intern(string(ref))
datum, ok := interp.TopLevel[s]
if !ok {
err = UnboundVariableError(string(ref))
}
b, d, err = rtk.Invoke(datum, rte, interp)
return
}
func (ref LexicalReference) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
var datum Datum
datum, err = rte.lookup(ref.Frame, ref.Offset)
if err != nil {
return
}
b, d, err = rtk.Invoke(datum, rte, interp)
return
}
func (q Quote) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
b, d, err = rtk.Invoke(q.D, rte, interp)
return
}
func (k SequenceK) Invoke(result Datum, rte *RuntimeFrame, interp *Interpreter) (b Bouncer, d Datum, err error) {
switch len(k.Es) {
case 0:
err = ImplementationError("A SequenceK must have at least one expression")
case 1:
b = Bouncer{E: k.Es[0], F: rte, K: k.NextK}
default:
k1 := SequenceK{Es: k.Es[1:], NextK: k.NextK}
b = Bouncer{E: k.Es[0], F: rte, K: k1}
}
return
}
func (s Sequence) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
switch len(s) {
case 0:
b, d, err = rtk.Invoke(Void{}, rte, interp) // (begin) => void
case 1:
b = Bouncer{E: s[0], F: rte, K: rtk}
default:
k := SequenceK{Es: s[1:], NextK: rtk}
b = Bouncer{E: s[0], F: rte, K: k}
}
return
}
func (k ConditionalK) Invoke(result Datum, rte *RuntimeFrame, interp *Interpreter) (b Bouncer, d Datum, err error) {
var e Expression
if result.IsFalse() {
e = k.No
} else {
e = k.Yes
}
b = Bouncer{E: e, F: rte, K: k.NextK}
return
}
func (c Conditional) Execute(rte *RuntimeFrame, rtk Continuation, interp *Interpreter) (b Bouncer, d Datum, err error) {
k := ConditionalK{Yes: c.Yes, No: c.No, NextK: rtk}
b = Bouncer{E: c.Test, F: rte, K: k}
return
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.