Skip to content

Commit 532fd9c

Browse files
committed
compile: implement named constant (True, False, None)
1 parent 6b7f42a commit 532fd9c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

compile/compile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ func (c *compiler) Expr(expr ast.Expr) {
500500
c.OpArg(vm.LOAD_CONST, c.Const(node.S))
501501
case *ast.NameConstant:
502502
// Value Singleton
503-
panic("FIXME compile: NameConstant not implemented")
503+
c.OpArg(vm.LOAD_CONST, c.Const(node.Value))
504504
case *ast.Ellipsis:
505505
panic("FIXME compile: Ellipsis not implemented")
506506
case *ast.Attribute:

compile/compile_data_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,55 @@ var compileTestData = []struct {
947947
Firstlineno: 1,
948948
Lnotab: "",
949949
}, " 1 0 LOAD_NAME 0 (a)\n 3 LOAD_NAME 1 (b)\n 6 LOAD_NAME 2 (c)\n 9 LOAD_NAME 3 (d)\n 12 BUILD_LIST 4\n 15 RETURN_VALUE\n"},
950+
{"True", "eval", py.Code{
951+
Argcount: 0,
952+
Kwonlyargcount: 0,
953+
Nlocals: 0,
954+
Stacksize: 1,
955+
Flags: 64,
956+
Code: "\x64\x00\x00\x53",
957+
Consts: []py.Object{py.Int(1)},
958+
Names: []string{},
959+
Varnames: []string{},
960+
Freevars: []string{},
961+
Cellvars: []string{},
962+
Filename: "<string>",
963+
Name: "<module>",
964+
Firstlineno: 1,
965+
Lnotab: "",
966+
}, " 1 0 LOAD_CONST 0 (True)\n 3 RETURN_VALUE\n"},
967+
{"False", "eval", py.Code{
968+
Argcount: 0,
969+
Kwonlyargcount: 0,
970+
Nlocals: 0,
971+
Stacksize: 1,
972+
Flags: 64,
973+
Code: "\x64\x00\x00\x53",
974+
Consts: []py.Object{py.Int(0)},
975+
Names: []string{},
976+
Varnames: []string{},
977+
Freevars: []string{},
978+
Cellvars: []string{},
979+
Filename: "<string>",
980+
Name: "<module>",
981+
Firstlineno: 1,
982+
Lnotab: "",
983+
}, " 1 0 LOAD_CONST 0 (False)\n 3 RETURN_VALUE\n"},
984+
{"None", "eval", py.Code{
985+
Argcount: 0,
986+
Kwonlyargcount: 0,
987+
Nlocals: 0,
988+
Stacksize: 1,
989+
Flags: 64,
990+
Code: "\x64\x00\x00\x53",
991+
Consts: []py.Object{py.None},
992+
Names: []string{},
993+
Varnames: []string{},
994+
Freevars: []string{},
995+
Cellvars: []string{},
996+
Filename: "<string>",
997+
Name: "<module>",
998+
Firstlineno: 1,
999+
Lnotab: "",
1000+
}, " 1 0 LOAD_CONST 0 (None)\n 3 RETURN_VALUE\n"},
9501001
}

compile/make_compile_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
('''[a]''', "eval"),
7777
('''[a,b]''', "eval"),
7878
('''[a,b,c,d]''', "eval"),
79+
# named constant
80+
('''True''', "eval"),
81+
('''False''', "eval"),
82+
('''None''', "eval"),
7983
]
8084

8185
def string(s):

0 commit comments

Comments
 (0)