forked from 0xfaded/eval
/
util.go
192 lines (177 loc) · 5.31 KB
/
util.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
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
package eval
import (
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"go/ast"
"go/token"
)
// Equivalent of reflect.New, but unwraps internal Types into their original reflect.Type
func hackedNew(t reflect.Type) reflect.Value {
switch tt := t.(type) {
case Rune:
return reflect.New(tt.Type)
default:
return reflect.New(t)
}
}
func assignableValue(x reflect.Value, to reflect.Type, xTyped bool) (reflect.Value, error) {
var err error
if xTyped {
if x.Type().AssignableTo(to) {
return x, nil
}
} else {
if x, err = promoteUntypedNumeral(x, to); err == nil {
return x, nil
}
}
return x, errors.New(fmt.Sprintf("Cannot convert %v to type %v", x, to))
}
func setTypedValue(dst, src reflect.Value, srcTyped bool) error {
if assignable, err := assignableValue(src, dst.Type(), srcTyped); err != nil {
return errors.New(fmt.Sprintf("Cannot assign %v = %v", dst, src))
} else {
dst.Set(assignable)
return nil
}
}
func makeSliceWithValues(elts []reflect.Value, sliceType reflect.Type) (reflect.Value, error) {
slice := reflect.MakeSlice(sliceType, len(elts), len(elts))
for i := 0; i < slice.Len(); i += 1 {
if err := setTypedValue(slice.Index(i), elts[i], true); err != nil {
return reflect.Value{}, nil
}
}
return slice, nil
}
// Only considers untyped kinds produced by our runtime. Assumes input type is unnamed
func isUntypedNumeral(x reflect.Value) bool {
switch x.Kind() {
case reflect.Int64, reflect.Float64, reflect.Complex128:
return true
default:
return false
}
}
func promoteUntypedNumeral(untyped reflect.Value, to reflect.Type) (reflect.Value, error) {
// The only valid promotion that cannot be directly converted is int|float -> complex
if untyped.Type().ConvertibleTo(to) {
return untyped.Convert(to), nil
} else if to.Kind() == reflect.Complex64 || to.Kind() == reflect.Complex128 {
floatType := reflect.TypeOf(float64(0))
if untyped.Type().ConvertibleTo(floatType) {
return reflect.ValueOf(complex(untyped.Convert(floatType).Float(), 0)), nil
}
}
return reflect.Value{}, errors.New(fmt.Sprintf("cannot convert %v to %v", untyped, to))
}
// Only considers untyped kinds produced by our runtime. Assumes input type is unnamed
func promoteUntypedNumerals(x, y reflect.Value) (reflect.Value, reflect.Value) {
switch x.Kind() {
case reflect.Int64:
switch y.Kind() {
case reflect.Int64:
return x, y
case reflect.Float64:
return x.Convert(y.Type()), y
case reflect.Complex128:
return reflect.ValueOf(complex(float64(x.Int()), 0)), y
}
case reflect.Float64:
switch y.Kind() {
case reflect.Int64:
return x, y.Convert(x.Type())
case reflect.Float64:
return x, y
case reflect.Complex128:
return reflect.ValueOf(complex(x.Float(), 0)), y
}
case reflect.Complex128:
switch y.Kind() {
case reflect.Int64:
return x, reflect.ValueOf(complex(float64(y.Int()), 0))
case reflect.Float64:
return x, reflect.ValueOf(complex(y.Float(), 0))
case reflect.Complex128:
return x, y
}
}
panic(fmt.Sprintf("runtime: bad untyped numeras %v and %v", x, y))
}
// TODO remove this when type checker is complete
func expectSingleValue(ctx *Ctx, values []reflect.Value, srcExpr ast.Expr) (reflect.Value, error) {
if len(values) == 0 {
return reflect.Value{}, ErrMissingValue{at(ctx, srcExpr)}
} else if len(values) != 1 {
return reflect.Value{}, ErrMultiInSingleContext{at(ctx, srcExpr)}
} else {
return values[0], nil
}
}
func expectSingleType(ctx *Ctx, types []reflect.Type, srcExpr ast.Expr) (reflect.Type, error) {
if len(types) == 0 {
return nil, ErrMissingValue{at(ctx, srcExpr)}
} else if len(types) != 1 {
return nil, ErrMultiInSingleContext{at(ctx, srcExpr)}
} else {
return types[0], nil
}
}
func isBooleanOp(op token.Token) bool {
switch op {
case token.EQL, token.NEQ, token.LEQ, token.GEQ, token.LSS, token.GTR, token.LAND, token.LOR:
return true
default:
return false
}
}
// FIXME: should also match and handle just a line and no column
var parseError = regexp.MustCompile(`^([0-9]+):([0-9]+): `)
// FormatErrorPos formats source to show the position that a (parse)
// error occurs. When this works, it returns a slice of one or two
// strings: the source line with the error and if it can find a column
// position under that, a line indicating the position where the error
// occurred.
//
// For example, if we have:
// source := `split(os.Args ", )")`
// errmsg := "1:15: expected ')'"
// then PrintErrPos(source, errmsg) returns:
// {
// `split(os.Args ", )")`,
// `-------------^`
// }
//
// If something is wrong parsing the error message or matching it with
// the source, an empty slice is returned.
func FormatErrorPos(source, errmsg string) (cursored [] string) {
matches := parseError.FindStringSubmatch(errmsg)
if len(matches) == 3 {
var err error
var line, column int
if line, err = strconv.Atoi(matches[1]); err != nil {
return cursored
}
if column, err = strconv.Atoi(matches[2]); err != nil {
return cursored
}
sourceLines := strings.Split(source, "\n")
if line > len(sourceLines) {
return cursored
}
errLine := sourceLines[line-1]
cursored = append(cursored, errLine)
if column-1 > len(errLine) || column < 1 {
return cursored
} else if column == 1 {
cursored = append(cursored, "^")
} else {
cursored = append(cursored, strings.Repeat("-", column-1) + "^")
}
}
return cursored
}