-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
145 lines (130 loc) · 3.18 KB
/
function.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
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
// Copyright 2012 Yuichi Araki. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package yall
import (
"fmt"
)
var builtinFunctions = map[string]func(*Cell) Expr{
"car": func(args *Cell) Expr {
if cell, ok := args.Car().(*Cell); ok && cell != Empty {
return cell.Car()
}
panic(NewRuntimeError("pair required, but got " + args.Car().String()))
},
"cdr": func(args *Cell) Expr {
if cell, ok := args.Car().(*Cell); ok && cell != Empty {
return cell.Cdr()
}
panic(NewRuntimeError("pair required, but got " + args.Car().String()))
},
"cons": func(arg *Cell) Expr {
if cadr, ok := arg.Cadr().(*Cell); ok {
return NewCell(arg.Car(), cadr)
}
panic(NewRuntimeError("Cons requires a cell for the second argument"))
},
"+": func(args *Cell) Expr {
result := 0
for Empty != args {
result += args.Car().(*Integer).Value()
args = args.Cdr()
}
return NewInteger(result)
},
"-": func(args *Cell) Expr {
if Empty == args {
panic(NewRuntimeError("Too few arguments to minus, at least 1 required"))
}
i, iok := args.Car().(*Integer)
if !iok {
panic(NewRuntimeError("Minus requires integers"))
}
result := i.Value()
if Empty == args.Cdr() {
return NewInteger(result * -1)
}
for cell := args.Cdr(); cell != Empty; cell = cell.Cdr() {
i, iok := cell.Car().(*Integer)
if !iok {
panic(NewRuntimeError("Minus requires integers"))
}
result -= i.Value()
}
return NewInteger(result)
},
"*": func(args *Cell) Expr {
result := 1
for Empty != args {
result *= args.Car().(*Integer).Value()
args = args.Cdr()
}
return NewInteger(result)
},
"/": func(args *Cell) Expr {
if Empty == args {
panic(NewRuntimeError("Too few arguments to '/', at least 1 required"))
}
i, iok := args.Car().(*Integer)
if !iok {
panic(NewRuntimeError("'/' requires integers"))
}
result := i.Value()
if Empty == args.Cdr() {
return NewInteger(1 / result)
}
for cell := args.Cdr(); cell != Empty; cell = cell.Cdr() {
i, iok := cell.Car().(*Integer)
if !iok {
panic(NewRuntimeError("'/' requires integers"))
}
result /= i.Value()
}
return NewInteger(result)
},
"type-of": func(args *Cell) Expr {
if args.Cdr() != Empty {
panic(NewRuntimeError("Too many arguments to type-of"))
}
return typeOf(args.Car())
},
"println": func(args *Cell) Expr {
args.Each(func(expr Expr) {
if str, ok := expr.(*String); ok {
fmt.Println(str.value)
} else {
fmt.Println(expr.String())
}
})
return True
},
"empty?": func(args *Cell) Expr {
if Empty == args.car {
return True
}
return False
},
"list": func(args *Cell) Expr {
return args
},
"=": func(args *Cell) Expr {
if Empty == args {
panic(NewRuntimeError("Too few arguments to '=', at least 1 required"))
}
i, iok := args.Car().(*Integer)
if !iok {
panic(NewRuntimeError("'=' requires integers"))
}
value := i.Value()
for cell := args.Cdr(); cell != Empty; cell = cell.Cdr() {
i, iok := cell.Car().(*Integer)
if !iok {
panic(NewRuntimeError("'=' requires integers"))
}
if (value != i.Value()) {
return False
}
}
return True
},
}