/
type.go
153 lines (136 loc) · 2.98 KB
/
type.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
146
147
148
149
150
151
152
153
package xin
import (
"strconv"
)
func stringForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 1 {
return nil, IncorrectNumberOfArgsError{
required: 1,
given: len(args),
}
}
return StringValue(args[0].String()), nil
}
func intForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 1 {
return nil, IncorrectNumberOfArgsError{
required: 1,
given: len(args),
}
}
first := args[0]
switch val := first.(type) {
case IntValue:
return val, nil
case FracValue:
return IntValue(float64(val)), nil
case StringValue:
intVal, err := strconv.ParseInt(string(val), 10, 64)
if err != nil {
return zeroValue, nil
}
return IntValue(intVal), nil
default:
return zeroValue, nil
}
}
func fracForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 1 {
return nil, IncorrectNumberOfArgsError{
required: 1,
given: len(args),
}
}
first := args[0]
switch val := first.(type) {
case IntValue:
return FracValue(val), nil
case FracValue:
return val, nil
case StringValue:
floatVal, err := strconv.ParseFloat(string(val), 64)
if err != nil {
return FracValue(0.0), nil
}
return FracValue(floatVal), nil
default:
return FracValue(0), nil
}
}
func equalForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 2 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 2,
given: len(args),
}
}
first, second := args[0], args[1]
if firstInt, fok := first.(IntValue); fok {
if _, sok := second.(FracValue); sok {
first = FracValue(float64(firstInt))
}
} else if _, fok := first.(FracValue); fok {
if secondInt, sok := second.(IntValue); sok {
second = FracValue(float64(secondInt))
}
}
if first.Equal(second) {
return trueValue, nil
} else {
return falseValue, nil
}
}
func typeForm(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
if len(args) < 1 {
return nil, IncorrectNumberOfArgsError{
node: node,
required: 1,
given: len(args),
}
}
first := args[0]
switch first.(type) {
case IntValue:
return NativeFormValue{
name: "int",
evaler: intForm,
}, nil
case FracValue:
return NativeFormValue{
name: "frac",
evaler: fracForm,
}, nil
case StringValue:
return NativeFormValue{
name: "str",
evaler: streamForm,
}, nil
case VecValue:
return NativeFormValue{
name: "vec",
evaler: vecForm,
}, nil
case MapValue:
return NativeFormValue{
name: "map",
evaler: mapForm,
}, nil
case StreamValue:
return NativeFormValue{
name: "stream",
evaler: streamForm,
}, nil
case FormValue, NativeFormValue:
return NativeFormValue{
name: "form",
evaler: func(fr *Frame, args []Value, node *astNode) (Value, InterpreterError) {
panic("form constructor as type should never be called")
},
}, nil
}
return nil, MismatchedArgumentsError{
node: node,
args: args,
}
}