Skip to content

Commit d0ea27b

Browse files
committed
compile: implement Dict, Set, attribute lookup
1 parent 532fd9c commit d0ea27b

File tree

3 files changed

+147
-3
lines changed

3 files changed

+147
-3
lines changed

compile/compile.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,22 @@ func (c *compiler) Expr(expr ast.Expr) {
397397
case *ast.Dict:
398398
// Keys []Expr
399399
// Values []Expr
400-
panic("FIXME compile: Dict not implemented")
400+
n := len(node.Keys)
401+
if n != len(node.Values) {
402+
panic("compile: Dict keys and values differing sizes")
403+
}
404+
c.OpArg(vm.BUILD_MAP, uint32(n))
405+
for i := range node.Keys {
406+
c.Expr(node.Values[i])
407+
c.Expr(node.Keys[i])
408+
c.Op(vm.STORE_MAP)
409+
}
401410
case *ast.Set:
402411
// Elts []Expr
403-
panic("FIXME compile: Set not implemented")
412+
for _, elt := range node.Elts {
413+
c.Expr(elt)
414+
}
415+
c.OpArg(vm.BUILD_SET, uint32(len(node.Elts)))
404416
case *ast.ListComp:
405417
// Elt Expr
406418
// Generators []Comprehension
@@ -507,7 +519,9 @@ func (c *compiler) Expr(expr ast.Expr) {
507519
// Value Expr
508520
// Attr Identifier
509521
// Ctx ExprContext
510-
panic("FIXME compile: Attribute not implemented")
522+
// FIXME do something with Ctx
523+
c.Expr(node.Value)
524+
c.OpArg(vm.LOAD_ATTR, c.Name(node.Attr))
511525
case *ast.Subscript:
512526
// Value Expr
513527
// Slice Slicer

compile/compile_data_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,4 +998,123 @@ var compileTestData = []struct {
998998
Firstlineno: 1,
999999
Lnotab: "",
10001000
}, " 1 0 LOAD_CONST 0 (None)\n 3 RETURN_VALUE\n"},
1001+
{"a.b", "eval", py.Code{
1002+
Argcount: 0,
1003+
Kwonlyargcount: 0,
1004+
Nlocals: 0,
1005+
Stacksize: 1,
1006+
Flags: 64,
1007+
Code: "\x65\x00\x00\x6a\x01\x00\x53",
1008+
Consts: []py.Object{},
1009+
Names: []string{"a", "b"},
1010+
Varnames: []string{},
1011+
Freevars: []string{},
1012+
Cellvars: []string{},
1013+
Filename: "<string>",
1014+
Name: "<module>",
1015+
Firstlineno: 1,
1016+
Lnotab: "",
1017+
}, " 1 0 LOAD_NAME 0 (a)\n 3 LOAD_ATTR 1 (b)\n 6 RETURN_VALUE\n"},
1018+
{"a.b.c", "eval", py.Code{
1019+
Argcount: 0,
1020+
Kwonlyargcount: 0,
1021+
Nlocals: 0,
1022+
Stacksize: 1,
1023+
Flags: 64,
1024+
Code: "\x65\x00\x00\x6a\x01\x00\x6a\x02\x00\x53",
1025+
Consts: []py.Object{},
1026+
Names: []string{"a", "b", "c"},
1027+
Varnames: []string{},
1028+
Freevars: []string{},
1029+
Cellvars: []string{},
1030+
Filename: "<string>",
1031+
Name: "<module>",
1032+
Firstlineno: 1,
1033+
Lnotab: "",
1034+
}, " 1 0 LOAD_NAME 0 (a)\n 3 LOAD_ATTR 1 (b)\n 6 LOAD_ATTR 2 (c)\n 9 RETURN_VALUE\n"},
1035+
{"a.b.c.d", "eval", py.Code{
1036+
Argcount: 0,
1037+
Kwonlyargcount: 0,
1038+
Nlocals: 0,
1039+
Stacksize: 1,
1040+
Flags: 64,
1041+
Code: "\x65\x00\x00\x6a\x01\x00\x6a\x02\x00\x6a\x03\x00\x53",
1042+
Consts: []py.Object{},
1043+
Names: []string{"a", "b", "c", "d"},
1044+
Varnames: []string{},
1045+
Freevars: []string{},
1046+
Cellvars: []string{},
1047+
Filename: "<string>",
1048+
Name: "<module>",
1049+
Firstlineno: 1,
1050+
Lnotab: "",
1051+
}, " 1 0 LOAD_NAME 0 (a)\n 3 LOAD_ATTR 1 (b)\n 6 LOAD_ATTR 2 (c)\n 9 LOAD_ATTR 3 (d)\n 12 RETURN_VALUE\n"},
1052+
{"{}", "eval", py.Code{
1053+
Argcount: 0,
1054+
Kwonlyargcount: 0,
1055+
Nlocals: 0,
1056+
Stacksize: 1,
1057+
Flags: 64,
1058+
Code: "\x69\x00\x00\x53",
1059+
Consts: []py.Object{},
1060+
Names: []string{},
1061+
Varnames: []string{},
1062+
Freevars: []string{},
1063+
Cellvars: []string{},
1064+
Filename: "<string>",
1065+
Name: "<module>",
1066+
Firstlineno: 1,
1067+
Lnotab: "",
1068+
}, " 1 0 BUILD_MAP 0\n 3 RETURN_VALUE\n"},
1069+
{"{1:2,a:b}", "eval", py.Code{
1070+
Argcount: 0,
1071+
Kwonlyargcount: 0,
1072+
Nlocals: 0,
1073+
Stacksize: 3,
1074+
Flags: 64,
1075+
Code: "\x69\x02\x00\x64\x00\x00\x64\x01\x00\x36\x65\x00\x00\x65\x01\x00\x36\x53",
1076+
Consts: []py.Object{py.Int(2), py.Int(1)},
1077+
Names: []string{"b", "a"},
1078+
Varnames: []string{},
1079+
Freevars: []string{},
1080+
Cellvars: []string{},
1081+
Filename: "<string>",
1082+
Name: "<module>",
1083+
Firstlineno: 1,
1084+
Lnotab: "",
1085+
}, " 1 0 BUILD_MAP 2\n 3 LOAD_CONST 0 (2)\n 6 LOAD_CONST 1 (1)\n 9 STORE_MAP\n 10 LOAD_NAME 0 (b)\n 13 LOAD_NAME 1 (a)\n 16 STORE_MAP\n 17 RETURN_VALUE\n"},
1086+
{"{1}", "eval", py.Code{
1087+
Argcount: 0,
1088+
Kwonlyargcount: 0,
1089+
Nlocals: 0,
1090+
Stacksize: 1,
1091+
Flags: 64,
1092+
Code: "\x64\x00\x00\x68\x01\x00\x53",
1093+
Consts: []py.Object{py.Int(1)},
1094+
Names: []string{},
1095+
Varnames: []string{},
1096+
Freevars: []string{},
1097+
Cellvars: []string{},
1098+
Filename: "<string>",
1099+
Name: "<module>",
1100+
Firstlineno: 1,
1101+
Lnotab: "",
1102+
}, " 1 0 LOAD_CONST 0 (1)\n 3 BUILD_SET 1\n 6 RETURN_VALUE\n"},
1103+
{"{1,2,a,b}", "eval", py.Code{
1104+
Argcount: 0,
1105+
Kwonlyargcount: 0,
1106+
Nlocals: 0,
1107+
Stacksize: 4,
1108+
Flags: 64,
1109+
Code: "\x64\x00\x00\x64\x01\x00\x65\x00\x00\x65\x01\x00\x68\x04\x00\x53",
1110+
Consts: []py.Object{py.Int(1), py.Int(2)},
1111+
Names: []string{"a", "b"},
1112+
Varnames: []string{},
1113+
Freevars: []string{},
1114+
Cellvars: []string{},
1115+
Filename: "<string>",
1116+
Name: "<module>",
1117+
Firstlineno: 1,
1118+
Lnotab: "",
1119+
}, " 1 0 LOAD_CONST 0 (1)\n 3 LOAD_CONST 1 (2)\n 6 LOAD_NAME 0 (a)\n 9 LOAD_NAME 1 (b)\n 12 BUILD_SET 4\n 15 RETURN_VALUE\n"},
10011120
}

compile/make_compile_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@
8080
('''True''', "eval"),
8181
('''False''', "eval"),
8282
('''None''', "eval"),
83+
# attribute
84+
('''a.b''', "eval"),
85+
('''a.b.c''', "eval"),
86+
('''a.b.c.d''', "eval"),
87+
# dict
88+
('''{}''', "eval"),
89+
('''{1:2,a:b}''', "eval"),
90+
# set
91+
# ('''set()''', "eval"),
92+
('''{1}''', "eval"),
93+
('''{1,2,a,b}''', "eval"),
8394
]
8495

8596
def string(s):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy