This repository was archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathbuiltin_types.go
More file actions
602 lines (562 loc) · 20.8 KB
/
builtin_types.go
File metadata and controls
602 lines (562 loc) · 20.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grumpy
import (
"fmt"
"math/big"
"reflect"
"unicode"
)
var (
// Builtins contains all of the Python built-in identifiers.
Builtins = NewDict()
builtinStr = NewStr("__builtin__")
// ExceptionTypes contains all builtin exception types.
ExceptionTypes []*Type
// NoneType is the object representing the Python 'NoneType' type.
NoneType = newSimpleType("NoneType", ObjectType)
// None is the singleton NoneType object representing the Python 'None'
// object.
None = &Object{typ: NoneType}
// NotImplementedType is the object representing the Python
// 'NotImplementedType' object.
NotImplementedType = newSimpleType("NotImplementedType", ObjectType)
// NotImplemented is the singleton NotImplementedType object
// representing the Python 'NotImplemented' object.
NotImplemented = newObject(NotImplementedType)
unboundLocalType = newSimpleType("UnboundLocalType", ObjectType)
// UnboundLocal is a singleton held by local variables in generated
// code before they are bound.
UnboundLocal = newObject(unboundLocalType)
)
func noneRepr(*Frame, *Object) (*Object, *BaseException) {
return NewStr("None").ToObject(), nil
}
func initNoneType(map[string]*Object) {
NoneType.flags &= ^(typeFlagInstantiable | typeFlagBasetype)
NoneType.slots.Repr = &unaryOpSlot{noneRepr}
}
func initNotImplementedType(map[string]*Object) {
NotImplementedType.flags &= ^(typeFlagInstantiable | typeFlagBasetype)
}
func initUnboundLocalType(map[string]*Object) {
unboundLocalType.flags &= ^(typeFlagInstantiable | typeFlagBasetype)
}
type typeState int
const (
typeStateNotReady typeState = iota
typeStateInitializing
typeStateReady
)
type builtinTypeInit func(map[string]*Object)
type builtinTypeInfo struct {
state typeState
init builtinTypeInit
global bool
}
var builtinTypes = map[*Type]*builtinTypeInfo{
ArithmeticErrorType: {global: true},
AssertionErrorType: {global: true},
AttributeErrorType: {global: true},
BaseExceptionType: {init: initBaseExceptionType, global: true},
BaseStringType: {init: initBaseStringType, global: true},
BoolType: {init: initBoolType, global: true},
BytesWarningType: {global: true},
CodeType: {},
DeprecationWarningType: {global: true},
dictItemIteratorType: {init: initDictItemIteratorType},
dictKeyIteratorType: {init: initDictKeyIteratorType},
DictType: {init: initDictType, global: true},
enumerateType: {init: initEnumerateType, global: true},
EnvironmentErrorType: {global: true},
ExceptionType: {global: true},
FileType: {init: initFileType, global: true},
FloatType: {init: initFloatType, global: true},
FrameType: {init: initFrameType},
FrozenSetType: {init: initFrozenSetType, global: true},
FunctionType: {init: initFunctionType},
FutureWarningType: {global: true},
GeneratorType: {init: initGeneratorType},
ImportErrorType: {global: true},
ImportWarningType: {global: true},
IndexErrorType: {global: true},
IntType: {init: initIntType, global: true},
IOErrorType: {global: true},
KeyErrorType: {global: true},
listIteratorType: {init: initListIteratorType},
ListType: {init: initListType, global: true},
LongType: {init: initLongType, global: true},
LookupErrorType: {global: true},
MemoryErrorType: {global: true},
MethodType: {init: initMethodType},
ModuleType: {init: initModuleType},
NameErrorType: {global: true},
nativeFuncType: {init: initNativeFuncType},
nativeMetaclassType: {init: initNativeMetaclassType},
nativeSliceType: {init: initNativeSliceType},
nativeType: {init: initNativeType},
NoneType: {init: initNoneType, global: true},
NotImplementedErrorType: {global: true},
NotImplementedType: {init: initNotImplementedType, global: true},
ObjectType: {init: initObjectType, global: true},
OSErrorType: {global: true},
OverflowErrorType: {global: true},
PendingDeprecationWarningType: {global: true},
PropertyType: {init: initPropertyType, global: true},
rangeIteratorType: {init: initRangeIteratorType, global: true},
ReferenceErrorType: {global: true},
RuntimeErrorType: {global: true},
RuntimeWarningType: {global: true},
seqIteratorType: {init: initSeqIteratorType},
SetType: {init: initSetType, global: true},
sliceIteratorType: {init: initSliceIteratorType},
SliceType: {init: initSliceType, global: true},
StandardErrorType: {global: true},
StaticMethodType: {init: initStaticMethodType, global: true},
StopIterationType: {global: true},
StrType: {init: initStrType, global: true},
superType: {init: initSuperType, global: true},
SyntaxErrorType: {global: true},
SyntaxWarningType: {global: true},
SystemErrorType: {global: true},
SystemExitType: {global: true, init: initSystemExitType},
TracebackType: {init: initTracebackType},
TupleType: {init: initTupleType, global: true},
TypeErrorType: {global: true},
TypeType: {init: initTypeType, global: true},
UnboundLocalErrorType: {global: true},
unboundLocalType: {init: initUnboundLocalType},
UnicodeDecodeErrorType: {global: true},
UnicodeEncodeErrorType: {global: true},
UnicodeErrorType: {global: true},
UnicodeType: {init: initUnicodeType, global: true},
UnicodeWarningType: {global: true},
UserWarningType: {global: true},
ValueErrorType: {global: true},
WarningType: {global: true},
WeakRefType: {init: initWeakRefType},
xrangeType: {init: initXRangeType, global: true},
ZeroDivisionErrorType: {global: true},
}
func initBuiltinType(typ *Type, info *builtinTypeInfo) {
if info.state == typeStateReady {
return
}
if info.state == typeStateInitializing {
logFatal(fmt.Sprintf("cycle in type initialization for: %s", typ.name))
}
info.state = typeStateInitializing
for _, base := range typ.bases {
baseInfo, ok := builtinTypes[base]
if !ok {
logFatal(fmt.Sprintf("base type not registered for: %s", typ.name))
}
initBuiltinType(base, baseInfo)
}
prepareBuiltinType(typ, info.init)
info.state = typeStateReady
if typ.isSubclass(BaseExceptionType) {
ExceptionTypes = append(ExceptionTypes, typ)
}
}
func builtinAbs(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "abs", args, ObjectType); raised != nil {
return nil, raised
}
return Abs(f, args[0])
}
func builtinBin(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "bin", args, ObjectType); raised != nil {
return nil, raised
}
index, raised := Index(f, args[0])
if raised != nil {
return nil, raised
}
if index == nil {
format := "%s object cannot be interpreted as an index"
return nil, f.RaiseType(TypeErrorType, fmt.Sprintf(format, args[0].typ.Name()))
}
return NewStr(numberToBase("0b", 2, index)).ToObject(), nil
}
func builtinChr(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "chr", args, IntType); raised != nil {
return nil, raised
}
i := toIntUnsafe(args[0]).Value()
if i < 0 || i > 255 {
return nil, f.RaiseType(ValueErrorType, "chr() arg not in range(256)")
}
return NewStr(string([]byte{byte(i)})).ToObject(), nil
}
func builtinDir(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
// TODO: Support __dir__.
if raised := checkFunctionArgs(f, "dir", args, ObjectType); raised != nil {
return nil, raised
}
d := NewDict()
o := args[0]
if o.dict != nil {
raised := seqForEach(f, o.dict.ToObject(), func(k *Object) *BaseException {
return d.SetItem(f, k, None)
})
if raised != nil {
return nil, raised
}
}
for _, t := range o.typ.mro {
raised := seqForEach(f, t.dict.ToObject(), func(k *Object) *BaseException {
return d.SetItem(f, k, None)
})
if raised != nil {
return nil, raised
}
}
l := d.Keys(f)
if raised := l.Sort(f); raised != nil {
return nil, raised
}
return l.ToObject(), nil
}
func builtinFrame(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "__frame__", args); raised != nil {
return nil, raised
}
return f.ToObject(), nil
}
func builtinGetAttr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
expectedTypes := []*Type{ObjectType, StrType, ObjectType}
argc := len(args)
if argc == 2 {
expectedTypes = expectedTypes[:2]
}
if raised := checkFunctionArgs(f, "getattr", args, expectedTypes...); raised != nil {
return nil, raised
}
var def *Object
if argc == 3 {
def = args[2]
}
return GetAttr(f, args[0], toStrUnsafe(args[1]), def)
}
func builtinGlobals(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "globals", args); raised != nil {
return nil, raised
}
return f.globals.ToObject(), nil
}
func builtinHasAttr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "hasattr", args, ObjectType, StrType); raised != nil {
return nil, raised
}
if _, raised := GetAttr(f, args[0], toStrUnsafe(args[1]), nil); raised != nil {
if raised.isInstance(AttributeErrorType) {
f.RestoreExc(nil, nil)
return False.ToObject(), nil
}
return nil, raised
}
return True.ToObject(), nil
}
func builtinHash(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "hash", args, ObjectType); raised != nil {
return nil, raised
}
h, raised := Hash(f, args[0])
if raised != nil {
return nil, raised
}
return h.ToObject(), nil
}
func builtinHex(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
// In Python3 we would call __index__ similarly to builtinBin().
if raised := checkFunctionArgs(f, "hex", args, ObjectType); raised != nil {
return nil, raised
}
if method, raised := args[0].typ.mroLookup(f, NewStr("__hex__")); raised != nil {
return nil, raised
} else if method != nil {
return method.Call(f, args, nil)
}
if !args[0].isInstance(IntType) && !args[0].isInstance(LongType) {
return nil, f.RaiseType(TypeErrorType, "hex() argument can't be converted to hex")
}
s := numberToBase("0x", 16, args[0])
if args[0].isInstance(LongType) {
s += "L"
}
return NewStr(s).ToObject(), nil
}
func builtinID(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "id", args, ObjectType); raised != nil {
return nil, raised
}
return NewInt(int(reflect.ValueOf(args[0]).Pointer())).ToObject(), nil
}
func builtinIsInstance(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "isinstance", args, ObjectType, ObjectType); raised != nil {
return nil, raised
}
ret, raised := IsInstance(f, args[0], args[1])
if raised != nil {
return nil, raised
}
return GetBool(ret).ToObject(), nil
}
func builtinIsSubclass(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "issubclass", args, ObjectType, ObjectType); raised != nil {
return nil, raised
}
ret, raised := IsSubclass(f, args[0], args[1])
if raised != nil {
return nil, raised
}
return GetBool(ret).ToObject(), nil
}
func builtinIter(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "iter", args, ObjectType); raised != nil {
return nil, raised
}
return Iter(f, args[0])
}
func builtinLen(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "len", args, ObjectType); raised != nil {
return nil, raised
}
ret, raised := Len(f, args[0])
return ret.ToObject(), raised
}
func builtinMax(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
return builtinMinMax(f, true, args, kwargs)
}
func builtinMin(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
return builtinMinMax(f, false, args, kwargs)
}
func builtinNext(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "next", args, ObjectType); raised != nil {
return nil, raised
}
ret, raised := Next(f, args[0])
if raised != nil {
return nil, raised
}
if ret != nil {
return ret, nil
}
return nil, f.Raise(StopIterationType.ToObject(), nil, nil)
}
func builtinOct(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
// In Python3 we would call __index__ similarly to builtinBin().
if raised := checkFunctionArgs(f, "oct", args, ObjectType); raised != nil {
return nil, raised
}
if method, raised := args[0].typ.mroLookup(f, NewStr("__oct__")); raised != nil {
return nil, raised
} else if method != nil {
return method.Call(f, args, nil)
}
if !args[0].isInstance(IntType) && !args[0].isInstance(LongType) {
return nil, f.RaiseType(TypeErrorType, "oct() argument can't be converted to oct")
}
s := numberToBase("0", 8, args[0])
if args[0].isInstance(LongType) {
s += "L"
}
// For oct(0), return "0", not "00".
if s == "00" {
s = "0"
}
return NewStr(s).ToObject(), nil
}
func builtinOpen(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
return FileType.Call(f, args, kwargs)
}
func builtinOrd(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
const lenMsg = "ord() expected a character, but string of length %d found"
if raised := checkFunctionArgs(f, "ord", args, BaseStringType); raised != nil {
return nil, raised
}
o := args[0]
var result int
if o.isInstance(StrType) {
s := toStrUnsafe(o).Value()
if numChars := len(s); numChars != 1 {
return nil, f.RaiseType(ValueErrorType, fmt.Sprintf(lenMsg, numChars))
}
result = int(([]byte(s))[0])
} else {
s := toUnicodeUnsafe(o).Value()
if numChars := len(s); numChars != 1 {
return nil, f.RaiseType(ValueErrorType, fmt.Sprintf(lenMsg, numChars))
}
result = int(s[0])
}
return NewInt(result).ToObject(), nil
}
func builtinRange(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
r, raised := xrangeType.Call(f, args, nil)
if raised != nil {
return nil, raised
}
return ListType.Call(f, []*Object{r}, nil)
}
func builtinRepr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "repr", args, ObjectType); raised != nil {
return nil, raised
}
s, raised := Repr(f, args[0])
if raised != nil {
return nil, raised
}
return s.ToObject(), nil
}
func builtinUniChr(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "unichr", args, IntType); raised != nil {
return nil, raised
}
i := toIntUnsafe(args[0]).Value()
if i < 0 || i > unicode.MaxRune {
return nil, f.RaiseType(ValueErrorType, fmt.Sprintf("unichr() arg not in range(0x%x)", unicode.MaxRune))
}
return NewUnicodeFromRunes([]rune{rune(i)}).ToObject(), nil
}
func init() {
builtinMap := map[string]*Object{
"__frame__": newBuiltinFunction("__frame__", builtinFrame).ToObject(),
"abs": newBuiltinFunction("abs", builtinAbs).ToObject(),
"bin": newBuiltinFunction("bin", builtinBin).ToObject(),
"chr": newBuiltinFunction("chr", builtinChr).ToObject(),
"dir": newBuiltinFunction("dir", builtinDir).ToObject(),
"False": False.ToObject(),
"getattr": newBuiltinFunction("getattr", builtinGetAttr).ToObject(),
"globals": newBuiltinFunction("globals", builtinGlobals).ToObject(),
"hasattr": newBuiltinFunction("hasattr", builtinHasAttr).ToObject(),
"hash": newBuiltinFunction("hash", builtinHash).ToObject(),
"hex": newBuiltinFunction("hex", builtinHex).ToObject(),
"id": newBuiltinFunction("id", builtinID).ToObject(),
"isinstance": newBuiltinFunction("isinstance", builtinIsInstance).ToObject(),
"issubclass": newBuiltinFunction("issubclass", builtinIsSubclass).ToObject(),
"iter": newBuiltinFunction("iter", builtinIter).ToObject(),
"len": newBuiltinFunction("len", builtinLen).ToObject(),
"max": newBuiltinFunction("max", builtinMax).ToObject(),
"min": newBuiltinFunction("min", builtinMin).ToObject(),
"next": newBuiltinFunction("next", builtinNext).ToObject(),
"None": None,
"NotImplemented": NotImplemented,
"oct": newBuiltinFunction("oct", builtinOct).ToObject(),
"open": newBuiltinFunction("open", builtinOpen).ToObject(),
"ord": newBuiltinFunction("ord", builtinOrd).ToObject(),
"range": newBuiltinFunction("range", builtinRange).ToObject(),
"repr": newBuiltinFunction("repr", builtinRepr).ToObject(),
"True": True.ToObject(),
"unichr": newBuiltinFunction("unichr", builtinUniChr).ToObject(),
}
// Do type initialization in two phases so that we don't have to think
// about hard-to-understand cycles.
for typ, info := range builtinTypes {
initBuiltinType(typ, info)
if info.global {
builtinMap[typ.name] = typ.ToObject()
}
}
for name := range builtinMap {
InternStr(name)
}
Builtins = newStringDict(builtinMap)
}
// builtinMinMax implements the builtin min/max() functions. When doMax is
// true, the max is found, otherwise the min is found. There are two forms of
// the builtins. The first takes a single iterable argument and the result is
// the min/max of the elements of that sequence. The second form takes two or
// more args and returns the min/max of those. For more details see:
// https://docs.python.org/2/library/functions.html#min
func builtinMinMax(f *Frame, doMax bool, args Args, kwargs KWArgs) (*Object, *BaseException) {
name := "min"
if doMax {
name = "max"
}
if raised := checkFunctionVarArgs(f, name, args, ObjectType); raised != nil {
return nil, raised
}
keyFunc := kwargs.get("key", nil)
// selected is the min/max element found so far.
var selected, selectedKey *Object
partialFunc := func(o *Object) (raised *BaseException) {
oKey := o
if keyFunc != nil {
oKey, raised = keyFunc.Call(f, Args{o}, nil)
if raised != nil {
return raised
}
}
// sel dictates whether o is the new min/max. It defaults to
// true when selected == nil (we don't yet have a selection).
sel := true
if selected != nil {
result, raised := LT(f, selectedKey, oKey)
if raised != nil {
return raised
}
lt, raised := IsTrue(f, result)
if raised != nil {
return raised
}
// Select o when looking for max and selection < o, or
// when looking for min and o < selection.
sel = doMax && lt || !doMax && !lt
}
if sel {
selected = o
selectedKey = oKey
}
return nil
}
if len(args) == 1 {
// Take min/max of the single iterable arg passed.
if raised := seqForEach(f, args[0], partialFunc); raised != nil {
return nil, raised
}
if selected == nil {
return nil, f.RaiseType(ValueErrorType, fmt.Sprintf("%s() arg is an empty sequence", name))
}
} else {
// Take min/max of the passed args.
for _, arg := range args {
if raised := partialFunc(arg); raised != nil {
return nil, raised
}
}
}
return selected, nil
}
// numberToBase implements the builtins "bin", "hex", and "oct".
// base must be between 2 and 36, and o must be an instance of
// IntType or LongType.
func numberToBase(prefix string, base int, o *Object) string {
z := big.Int{}
switch {
case o.isInstance(LongType):
z = toLongUnsafe(o).value
case o.isInstance(IntType):
z.SetInt64(int64(toIntUnsafe(o).Value()))
default:
panic("numberToBase requires an Int or Long argument")
}
s := z.Text(base)
if s[0] == '-' {
// Move the negative sign before the prefix.
return "-" + prefix + s[1:]
}
return prefix + s
}