Repository navigation
Expand file tree
/
Copy pathconstant.go
More file actions
263 lines (223 loc) 路 5.8 KB
/
Copy pathconstant.go
File metadata and controls
263 lines (223 loc) 路 5.8 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package analysis
import (
"fmt"
"maps"
"slices"
"strconv"
"strings"
"unicode/utf8"
"github.com/ProCode-Software/klar/internal/lexer"
)
type ConstExpr struct {
Type
Value ConstValue
}
func (c *ConstExpr) Underlying() Type { return c.Type }
func (c *ConstExpr) String() string { return c.Type.String() }
func (c *ConstExpr) ConstString() string {
if v, ok := c.Value.(ObjectConst); ok {
// Print the type with the fields
// TODO: The fields should probably be in the same order
// as the struct/interface's
return c.Type.String() + v.String()
}
return c.String()
}
// Only simple ConstValues are supported ([FloatConst], [IntConst], [StringConst]).
// ConstValueToConstExpr panics if cv is any other type.
func ConstValueToConstExpr(cv ConstValue) *ConstExpr {
switch cv := cv.(type) {
case IntConst:
return &ConstExpr{IntType, cv}
case StringConst:
return &ConstExpr{StringType, cv}
case FloatConst:
return &ConstExpr{FloatType, cv}
default:
panic(fmt.Sprintf("ConstValueToConstExpr: invalid ConstValueType: %T", cv))
}
}
type ConstValue interface {
String() string
ConstValue() any
}
// Helper initializers
// ===========
func NewIntConstExpr(val int64) *ConstExpr {
return &ConstExpr{IntType, IntConst(val)}
}
func NewBoolConstExpr(val bool) *ConstExpr {
return &ConstExpr{BoolType, BoolConst(val)}
}
func NewFloatConstExpr(val float64) *ConstExpr {
return &ConstExpr{FloatType, FloatConst(val)}
}
func NewStringConstExpr(val string) *ConstExpr {
return &ConstExpr{StringType, StringConst{Value: val}}
}
// ConstValues
// =======
type UnknownConst struct{}
func (u UnknownConst) ConstValue() any { return nil }
func (u UnknownConst) String() string { return "invalid" }
type IntConst int64
func (i IntConst) ConstValue() any { return int64(i) }
func (i IntConst) String() string { return strconv.FormatInt(int64(i), 10) }
// [IntConst.Int64] should be preferred to avoid overflows.
func (i IntConst) Int() int { return int(i) }
type StringConst struct {
Value string
len int // Lazily evaluated
}
func (s StringConst) ConstValue() any { return s.Value }
// TODO: Should single quotes be used?
func (s StringConst) String() string { return strconv.Quote(s.Value) }
func (s StringConst) Len() int {
if s.len <= 0 {
// TODO: Count grapheme clusters
s.len = utf8.RuneCountInString(s.Value)
}
return s.len
}
// TODO: Should float constants be represented as [math/big.Float]/[math/big.Rat] instead
// of float64 like the Go type checker does?
type FloatConst float64
func (f FloatConst) ConstValue() any { return float64(f) }
func (f FloatConst) String() string {
return strconv.FormatFloat(float64(f), 'g', -1, 64)
}
type BoolConst bool
func (b BoolConst) ConstValue() any { return bool(b) }
func (b BoolConst) String() string { return strconv.FormatBool(bool(b)) }
// An ObjectConst can represent a struct or interface. The type is based
// on [ConstExpr.Type].
type ObjectConst struct {
Fields map[string]ConstValue
}
func (o ObjectConst) ConstValue() any { return o.Fields }
func (o ObjectConst) String() string {
var b strings.Builder
b.WriteByte('(')
for i, key := range slices.Sorted(maps.Keys(o.Fields)) {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(key)
b.WriteString(": ")
b.WriteString(o.Fields[key].String())
}
b.WriteByte(')')
return b.String()
}
type TupleConst struct {
Items []ConstValue
}
func (t TupleConst) ConstValue() any { return t.Items }
func (t TupleConst) String() string {
var b strings.Builder
b.WriteByte('(')
for i, item := range t.Items {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(item.String())
}
b.WriteByte(')')
return b.String()
}
type ConstHash int64
type ListConst struct {
Items []ConstValue
}
func (l ListConst) ConstValue() any { return l.Items }
func (l ListConst) String() string {
var b strings.Builder
b.WriteByte('[')
for i, item := range l.Items {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(item.String())
}
b.WriteByte(']')
return b.String()
}
/* type MapConst struct {
Keys []ConstValue
Entries map[ConstHash]ConstValue
}
func (m MapConst) ConstValue() any { return m.Entries }
func (m MapConst) String() string {
var b strings.Builder
b.WriteString("#{")
for i, key := range m.Keys {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(key.String())
b.WriteString(": ")
b.WriteString(m.Entries[ConstHash(i)].String())
}
b.WriteByte('}')
return b.String()
}
*/
type RegexConst struct {
Pattern, Flags string
}
func (r RegexConst) ConstValue() any { return r }
func (r RegexConst) String() string {
return "#/" + r.Pattern + "/" + r.Flags
}
// Operations
// ========
func ConstBinaryOp(lhs, rhs *ConstExpr, op lexer.TokenType, typ Type) *ConstExpr {
res := &ConstExpr{Type: typ}
switch op {
// Arithmetic
case lexer.Plus:
case lexer.Minus:
case lexer.Asterisk:
case lexer.Slash:
case lexer.Percent:
case lexer.Caret:
// Logical
case lexer.AndAnd:
case lexer.OrOr:
// Relational
case lexer.EqualEqual:
case lexer.NotEqual:
case lexer.GreaterThan:
case lexer.LessThan:
case lexer.GreaterEqualTo:
case lexer.LessEqualTo:
default:
panic("invalid binary operator: " + op.String())
}
if res.Value == nil {
panic(fmt.Sprintf("invalid operation: %s %s %s", lhs, op, rhs))
}
return res
}
func ConstUnaryOp(c *ConstExpr, op lexer.TokenType) *ConstExpr {
res := &ConstExpr{Type: c.Type}
switch op {
case lexer.Not:
if c.Type.Kind() != BoolType {
panic("invalid operation: can't use '!' operator on type " + c.Type.String())
}
res.Value = !c.Value.(BoolConst)
case lexer.Minus:
switch c.Type.Kind() {
case FloatType:
res.Value = -c.Value.(FloatConst)
case IntType:
res.Value = -c.Value.(IntConst)
default:
panic("invalid operation: can't use '-' operator on type " + c.Type.String())
}
default:
panic("invalid unary operator: " + op.String())
}
return res
}