-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathcompile.go
1933 lines (1789 loc) · 48.2 KB
/
compile.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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// compile python code
package compile
// FIXME name mangling
// FIXME kill ast.Identifier and turn into string?
import (
"bytes"
"fmt"
"log"
"strings"
"github.com/go-python/gpython/ast"
"github.com/go-python/gpython/parser"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/symtable"
"github.com/go-python/gpython/vm"
)
type loopType byte
// type of loop
const (
loopLoop loopType = iota
exceptLoop
finallyTryLoop
finallyEndLoop
)
// Loop - used to track loops, try/except and try/finally
type loop struct {
Start *Label
End *Label
Type loopType
}
// Loopstack
type loopstack []loop
// Push a loop
func (ls *loopstack) Push(l loop) {
*ls = append(*ls, l)
}
// Pop a loop
func (ls *loopstack) Pop() {
*ls = (*ls)[:len(*ls)-1]
}
// Return current loop or nil for none
func (ls loopstack) Top() *loop {
if len(ls) == 0 {
return nil
}
return &ls[len(ls)-1]
}
type compilerScopeType uint8
const (
compilerScopeModule compilerScopeType = iota
compilerScopeClass
compilerScopeFunction
compilerScopeLambda
compilerScopeComprehension
)
// State for the compiler
type compiler struct {
Code *py.Code // code being built up
Filename string
Lineno int // current line number
OpCodes Instructions
loops loopstack
SymTable *symtable.SymTable
scopeType compilerScopeType
qualname string
private string
parent *compiler
depth int
interactive bool
}
// Set in py to avoid circular import
func init() {
py.Compile = Compile
}
// Compile(src, srcDesc, compileMode, flags, dont_inherit) -> code object
//
// Compile the source string (a Python module, statement or expression)
// into a code object that can be executed.
//
// srcDesc is used for run-time error messages and is typically a file system pathname,
//
// See py.CompileMode for compile mode options.
//
// The flags argument, if present, controls which future statements influence
// the compilation of the code.
//
// The dont_inherit argument, if non-zero, stops the compilation inheriting
// the effects of any future statements in effect in the code calling
// compile; if absent or zero these statements do influence the compilation,
// in addition to any features explicitly specified.
func Compile(src, srcDesc string, mode py.CompileMode, futureFlags int, dont_inherit bool) (*py.Code, error) {
// Parse Ast
Ast, err := parser.Parse(bytes.NewBufferString(src), srcDesc, mode)
if err != nil {
return nil, err
}
// Make symbol table
SymTable, err := symtable.NewSymTable(Ast, srcDesc)
if err != nil {
return nil, err
}
c := newCompiler(nil, compilerScopeModule)
c.Filename = srcDesc
err = c.compileAst(Ast, srcDesc, futureFlags, dont_inherit, SymTable)
if err != nil {
return nil, err
}
return c.Code, nil
}
// Make a new compiler object with empty code object
func newCompiler(parent *compiler, scopeType compilerScopeType) *compiler {
code := &py.Code{
Firstlineno: 1, // FIXME
Name: "<module>", // FIXME
}
c := &compiler{
Code: code,
parent: parent,
scopeType: scopeType,
depth: 1,
interactive: false,
}
if parent != nil {
c.depth = parent.depth + 1
c.Filename = parent.Filename
}
return c
}
// Panics abount a syntax error on this ast node
func (c *compiler) panicSyntaxErrorf(Ast ast.Ast, format string, a ...interface{}) {
err := py.ExceptionNewf(py.SyntaxError, format, a...)
err = py.MakeSyntaxError(err, c.Filename, Ast.GetLineno(), Ast.GetColOffset(), "")
panic(err)
}
// Sets Lineno from an ast node
func (c *compiler) SetLineno(node ast.Ast) {
c.Lineno = node.GetLineno()
}
// Create a new compiler object at Ast, using private for name mangling
func (c *compiler) newCompilerScope(compilerScope compilerScopeType, Ast ast.Ast, private string) (newC *compiler) {
newSymTable := c.SymTable.FindChild(Ast)
if newSymTable == nil {
panic(fmt.Sprintf("No symtable found for scope type %v", compilerScope))
}
newC = newCompiler(c, compilerScope)
/* use the class name for name mangling */
newC.private = private
if newSymTable.NeedsClassClosure {
// Cook up a implicit __class__ cell.
if compilerScope != compilerScopeClass {
panic("class closure not in class")
}
newSymTable.Symbols["__class__"] = symtable.Symbol{Scope: symtable.ScopeCell}
}
err := newC.compileAst(Ast, c.Code.Filename, 0, false, newSymTable)
if err != nil {
panic(err)
}
return newC
}
// Compile an Ast with the current compiler
func (c *compiler) compileAst(Ast ast.Ast, filename string, futureFlags int, dont_inherit bool, SymTable *symtable.SymTable) (err error) {
defer func() {
if r := recover(); r != nil {
err = py.MakeException(r)
}
}()
c.SymTable = SymTable
code := c.Code
code.Filename = filename
code.Varnames = append(code.Varnames, SymTable.Varnames...)
code.Cellvars = append(code.Cellvars, SymTable.Find(symtable.ScopeCell, 0)...)
code.Freevars = append(code.Freevars, SymTable.Find(symtable.ScopeFree, symtable.DefFreeClass)...)
code.Flags = c.codeFlags(SymTable) | int32(futureFlags&py.CO_COMPILER_FLAGS_MASK)
valueOnStack := false
c.SetLineno(Ast)
switch node := Ast.(type) {
case *ast.Module:
c.Stmts(c.docString(node.Body, false))
case *ast.Interactive:
c.interactive = true
c.Stmts(node.Body)
case *ast.Expression:
c.Expr(node.Body)
valueOnStack = true
case *ast.Suite:
panic("suite should not be possible")
case *ast.Lambda:
code.Argcount = int32(len(node.Args.Args))
code.Kwonlyargcount = int32(len(node.Args.Kwonlyargs))
// Make None the first constant as lambda can't have a docstring
c.Const(py.None)
code.Name = "<lambda>"
c.setQualname()
c.Expr(node.Body)
valueOnStack = true
case *ast.FunctionDef:
code.Argcount = int32(len(node.Args.Args))
code.Kwonlyargcount = int32(len(node.Args.Kwonlyargs))
code.Name = string(node.Name)
c.setQualname()
c.Stmts(c.docString(node.Body, true))
case *ast.ClassDef:
code.Name = string(node.Name)
/* load (global) __name__ ... */
c.NameOp("__name__", ast.Load)
/* ... and store it as __module__ */
c.NameOp("__module__", ast.Store)
c.setQualname()
if c.qualname == "" {
panic("Need qualname")
}
c.LoadConst(py.String(c.qualname))
c.NameOp("__qualname__", ast.Store)
/* compile the body proper */
c.Stmts(c.docString(node.Body, false))
if SymTable.NeedsClassClosure {
/* return the (empty) __class__ cell */
i := c.FindId("__class__", code.Cellvars)
if i != 0 {
panic("__class__ must be first constant")
}
/* Return the cell where to store __class__ */
c.OpArg(vm.LOAD_CLOSURE, uint32(i))
} else {
if len(code.Cellvars) != 0 {
panic("Can't have cellvars without closure")
}
/* This happens when nobody references the cell. Return None. */
c.LoadConst(py.None)
}
c.Op(vm.RETURN_VALUE)
case *ast.ListComp:
// Elt Expr
// Generators []Comprehension
valueOnStack = true
code.Name = "<listcomp>"
c.OpArg(vm.BUILD_LIST, 0)
c.comprehensionGenerator(node.Generators, 0, node.Elt, nil, Ast)
case *ast.SetComp:
// Elt Expr
// Generators []Comprehension
valueOnStack = true
code.Name = "<setcomp>"
c.OpArg(vm.BUILD_SET, 0)
c.comprehensionGenerator(node.Generators, 0, node.Elt, nil, Ast)
case *ast.DictComp:
// Key Expr
// Value Expr
// Generators []Comprehension
valueOnStack = true
code.Name = "<dictcomp>"
c.OpArg(vm.BUILD_MAP, 0)
c.comprehensionGenerator(node.Generators, 0, node.Key, node.Value, Ast)
case *ast.GeneratorExp:
// Elt Expr
// Generators []Comprehension
code.Name = "<genexpr>"
c.comprehensionGenerator(node.Generators, 0, node.Elt, nil, Ast)
default:
panic(fmt.Sprintf("Unknown ModuleBase: %v", Ast))
}
if !c.OpCodes.EndsWithReturn() {
// add a return
if !valueOnStack {
// return None if there is nothing on the stack
c.LoadConst(py.None)
}
c.Op(vm.RETURN_VALUE)
}
code.Code = c.OpCodes.Assemble()
code.Stacksize = int32(c.OpCodes.StackDepth())
code.Nlocals = int32(len(code.Varnames))
code.Lnotab = string(c.OpCodes.Lnotab())
code.InitCell2arg()
return nil
}
// Check for docstring as first Expr in body and remove it and set the
// first constant if found if fn is set, or set __doc__ if it isn't
func (c *compiler) docString(body []ast.Stmt, fn bool) []ast.Stmt {
var docstring *ast.Str
if len(body) > 0 {
if expr, ok := body[0].(*ast.ExprStmt); ok {
if docstring, ok = expr.Value.(*ast.Str); ok {
body = body[1:]
}
}
}
if fn {
if docstring != nil {
c.Const(docstring.S)
} else {
// If no docstring put None in
c.Const(py.None)
}
} else {
if docstring != nil {
c.LoadConst(docstring.S)
c.NameOp("__doc__", ast.Store)
}
}
return body
}
// Compiles a python constant
//
// Returns the index into the Consts tuple
func (c *compiler) Const(obj py.Object) uint32 {
// FIXME back this with a dict to stop O(N**2) behaviour on lots of consts
for i, c := range c.Code.Consts {
if obj.Type() == c.Type() {
eq, err := py.Eq(obj, c)
if err != nil {
log.Printf("compiler: Const: error %v", err) // FIXME
} else if eq == py.True {
return uint32(i)
}
}
}
c.Code.Consts = append(c.Code.Consts, obj)
return uint32(len(c.Code.Consts) - 1)
}
// Loads a constant
func (c *compiler) LoadConst(obj py.Object) {
c.OpArg(vm.LOAD_CONST, c.Const(obj))
}
// Finds the Id in the slice provided, returning -1 if not found
func (c *compiler) FindId(Id string, Names []string) int {
// FIXME back this with a dict to stop O(N**2) behaviour on lots of vars
for i, s := range Names {
if Id == s {
return i
}
}
return -1
}
// Returns the index into the slice provided, updating the slice if necessary
func (c *compiler) Index(Id string, Names *[]string) uint32 {
i := c.FindId(Id, *Names)
if i >= 0 {
return uint32(i)
}
*Names = append(*Names, Id)
return uint32(len(*Names) - 1)
}
// Compiles a python name
//
// Returns the index into the Name tuple
func (c *compiler) Name(Id ast.Identifier) uint32 {
return c.Index(string(Id), &c.Code.Names)
}
// Adds this opcode with mangled name as an argument
func (c *compiler) OpName(opcode vm.OpCode, name ast.Identifier) {
// FIXME mangled := _Py_Mangle(c->u->u_private, o);
mangled := name
c.OpArg(opcode, c.Name(mangled))
}
// Compiles an instruction with an argument
func (c *compiler) OpArg(Op vm.OpCode, Arg uint32) {
if !Op.HAS_ARG() {
panic("OpArg called with an instruction which doesn't take an Arg")
}
instr := &OpArg{Op: Op, Arg: Arg}
instr.SetLineno(c.Lineno)
c.OpCodes.Add(instr)
}
// Compiles an instruction without an argument
func (c *compiler) Op(op vm.OpCode) {
if op.HAS_ARG() {
panic("Op called with an instruction which takes an Arg")
}
instr := &Op{Op: op}
instr.SetLineno(c.Lineno)
c.OpCodes.Add(instr)
}
// Inserts an existing label
func (c *compiler) Label(Dest *Label) {
c.OpCodes.Add(Dest)
}
// Inserts and creates a label
func (c *compiler) NewLabel() *Label {
Dest := new(Label)
c.OpCodes.Add(Dest)
return Dest
}
// Compiles a jump instruction
func (c *compiler) Jump(Op vm.OpCode, Dest *Label) {
var instr Instruction
switch Op {
case vm.JUMP_IF_FALSE_OR_POP, vm.JUMP_IF_TRUE_OR_POP, vm.JUMP_ABSOLUTE, vm.POP_JUMP_IF_FALSE, vm.POP_JUMP_IF_TRUE, vm.CONTINUE_LOOP: // Absolute
instr = &JumpAbs{OpArg: OpArg{Op: Op}, Dest: Dest}
case vm.JUMP_FORWARD, vm.SETUP_WITH, vm.FOR_ITER, vm.SETUP_LOOP, vm.SETUP_EXCEPT, vm.SETUP_FINALLY:
instr = &JumpRel{OpArg: OpArg{Op: Op}, Dest: Dest}
default:
panic("Jump called with non jump instruction")
}
instr.SetLineno(c.Lineno)
c.OpCodes.Add(instr)
}
/*
The test for LOCAL must come before the test for FREE in order to
handle classes where name is both local and free. The local var is
a method and the free var is a free var referenced within a method.
*/
func (c *compiler) getRefType(name string) symtable.Scope {
if c.scopeType == compilerScopeClass && name == "__class__" {
return symtable.ScopeCell
}
scope := c.SymTable.GetScope(name)
if scope == symtable.ScopeInvalid {
panic(fmt.Sprintf("compile: getRefType: unknown scope for %s in %s\nsymbols: %v\nlocals: %s\nglobals: %s", name, c.Code.Name, c.SymTable.Symbols, c.Code.Varnames, c.Code.Names))
}
return scope
}
// makeClosure constructs the function or closure for a func/class/lambda etc
func (c *compiler) makeClosure(code *py.Code, args uint32, child *compiler, qualname string) {
free := uint32(len(code.Freevars))
if free == 0 {
c.LoadConst(code)
c.LoadConst(py.String(qualname))
c.OpArg(vm.MAKE_FUNCTION, args)
return
}
for i := range code.Freevars {
/* Bypass com_addop_varname because it will generate
LOAD_DEREF but LOAD_CLOSURE is needed.
*/
name := code.Freevars[i]
/* Special case: If a class contains a method with a
free variable that has the same name as a method,
the name will be considered free *and* local in the
class. It should be handled by the closure, as
well as by the normal name loookup logic.
*/
reftype := c.getRefType(name)
arg := 0
if reftype == symtable.ScopeCell {
arg = c.FindId(name, c.Code.Cellvars)
} else { /* (reftype == FREE) */
// using CellAndFreeVars in closures requires skipping Cellvars
arg = len(c.Code.Cellvars) + c.FindId(name, c.Code.Freevars)
}
if arg < 0 {
panic(fmt.Sprintf("compile: makeClosure: lookup %q in %q %v %v\nfreevars of %q: %v\n", name, c.SymTable.Name, reftype, arg, code.Name, code.Freevars))
}
c.OpArg(vm.LOAD_CLOSURE, uint32(arg))
}
c.OpArg(vm.BUILD_TUPLE, free)
c.LoadConst(code)
c.LoadConst(py.String(qualname))
c.OpArg(vm.MAKE_CLOSURE, args)
}
// Compute the flags for the current Code
func (c *compiler) codeFlags(st *symtable.SymTable) (flags int32) {
if st.Type == symtable.FunctionBlock {
flags |= py.CO_NEWLOCALS
if st.Unoptimized == 0 {
flags |= py.CO_OPTIMIZED
}
if st.Nested {
flags |= py.CO_NESTED
}
if st.Generator {
flags |= py.CO_GENERATOR
}
if st.Varargs {
flags |= py.CO_VARARGS
}
if st.Varkeywords {
flags |= py.CO_VARKEYWORDS
}
}
/* (Only) inherit compilerflags in PyCF_MASK */
flags |= c.Code.Flags & py.CO_COMPILER_FLAGS_MASK
if len(c.Code.Freevars) == 0 && len(c.Code.Cellvars) == 0 {
flags |= py.CO_NOFREE
}
return flags
}
// Sets the qualname
func (c *compiler) setQualname() {
var base string
if c.depth > 1 {
force_global := false
parent := c.parent
if parent == nil {
panic("compile: setQualname: expecting a parent")
}
if c.scopeType == compilerScopeFunction || c.scopeType == compilerScopeClass {
// FIXME mangled = _Py_Mangle(parent.u_private, u.u_name)
mangled := c.Code.Name
scope := parent.SymTable.GetScope(mangled)
if scope == symtable.ScopeGlobalImplicit {
panic("compile: setQualname: not expecting scopeGlobalImplicit")
}
if scope == symtable.ScopeGlobalExplicit {
force_global = true
}
}
if !force_global {
if parent.scopeType == compilerScopeFunction || parent.scopeType == compilerScopeLambda {
base = parent.qualname + ".<locals>"
} else {
base = parent.qualname
}
}
}
if base != "" {
c.qualname = base + "." + c.Code.Name
} else {
c.qualname = c.Code.Name
}
}
// Compile a function
func (c *compiler) compileFunc(compilerScope compilerScopeType, Ast ast.Ast, Args *ast.Arguments, DecoratorList []ast.Expr, Returns ast.Expr) {
newC := c.newCompilerScope(compilerScope, Ast, "")
newC.Code.Argcount = int32(len(Args.Args))
newC.Code.Kwonlyargcount = int32(len(Args.Kwonlyargs))
// Defaults
c.Exprs(Args.Defaults)
// KwDefaults
if len(Args.KwDefaults) > len(Args.Kwonlyargs) {
panic("compile: more KwDefaults than Kwonlyargs")
}
for i := range Args.KwDefaults {
c.LoadConst(py.String(Args.Kwonlyargs[i].Arg))
c.Expr(Args.KwDefaults[i])
}
// Annotations
annotations := py.Tuple{}
addAnnotation := func(args ...*ast.Arg) {
for _, arg := range args {
if arg != nil && arg.Annotation != nil {
c.Expr(arg.Annotation)
annotations = append(annotations, py.String(arg.Arg))
}
}
}
addAnnotation(Args.Args...)
addAnnotation(Args.Vararg)
addAnnotation(Args.Kwonlyargs...)
addAnnotation(Args.Kwarg)
if Returns != nil {
c.Expr(Returns)
annotations = append(annotations, py.String("return"))
}
num_annotations := uint32(len(annotations))
if num_annotations > 0 {
num_annotations++ // include the tuple
c.LoadConst(annotations)
}
// Load decorators onto stack
c.Exprs(DecoratorList)
// Make function or closure, leaving it on the stack
posdefaults := uint32(len(Args.Defaults))
kwdefaults := uint32(len(Args.KwDefaults))
args := uint32(posdefaults + (kwdefaults << 8) + (num_annotations << 16))
c.makeClosure(newC.Code, args, newC, newC.qualname)
// Call decorators
for range DecoratorList {
c.OpArg(vm.CALL_FUNCTION, 1) // 1 positional, 0 keyword pair
}
}
// Compile class definition
func (c *compiler) class(Ast ast.Ast, class *ast.ClassDef) {
// Load decorators onto stack
c.Exprs(class.DecoratorList)
/* ultimately generate code for:
<name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
where:
<func> is a function/closure created from the class body;
it has a single argument (__locals__) where the dict
(or MutableSequence) representing the locals is passed
<name> is the class name
<bases> is the positional arguments and *varargs argument
<keywords> is the keyword arguments and **kwds argument
This borrows from compiler_call.
*/
/* 1. compile the class body into a code object */
newC := c.newCompilerScope(compilerScopeClass, Ast, string(class.Name))
// newSymTable := c.SymTable.FindChild(Ast)
// if newSymTable == nil {
// panic("No symtable found for class")
// }
// newC := newCompiler(c, compilerScopeClass)
/* use the class name for name mangling */
newC.private = string(class.Name)
// code, err := newC.compileAst(Ast, c.Code.Filename, 0, false, newSymTable)
// if err != nil {
// panic(err)
// }
/* 2. load the 'build_class' function */
c.Op(vm.LOAD_BUILD_CLASS)
/* 3. load a function (or closure) made from the code object */
c.makeClosure(newC.Code, 0, newC, string(class.Name))
/* 4. load class name */
c.LoadConst(py.String(class.Name))
/* 5. generate the rest of the code for the call */
c.callHelper(2, class.Bases, class.Keywords, class.Starargs, class.Kwargs)
/* 6. apply decorators */
for range class.DecoratorList {
c.OpArg(vm.CALL_FUNCTION, 1) // 1 positional, 0 keyword pair
}
/* 7. store into <name> */
c.NameOp(string(class.Name), ast.Store)
}
/*
Implements the with statement from PEP 343.
The semantics outlined in that PEP are as follows:
with EXPR as VAR:
BLOCK
It is implemented roughly as:
context = EXPR
exit = context.__exit__ # not calling it
value = context.__enter__()
try:
VAR = value # if VAR present in the syntax
BLOCK
finally:
if an exception was raised:
exc = copy of (exception, instance, traceback)
else:
exc = (None, None, None)
exit(*exc)
*/
func (c *compiler) with(node *ast.With, pos int) {
item := node.Items[pos]
finally := new(Label)
/* Evaluate EXPR */
c.Expr(item.ContextExpr)
c.Jump(vm.SETUP_WITH, finally)
/* SETUP_WITH pushes a finally block. */
c.loops.Push(loop{Type: finallyTryLoop})
if item.OptionalVars != nil {
c.Expr(item.OptionalVars)
} else {
/* Discard result from context.__enter__() */
c.Op(vm.POP_TOP)
}
pos++
if pos == len(node.Items) {
/* BLOCK code */
c.Stmts(node.Body)
} else {
c.with(node, pos)
}
/* End of try block; start the finally block */
c.Op(vm.POP_BLOCK)
c.loops.Pop()
c.LoadConst(py.None)
/* Finally block starts; context.__exit__ is on the stack under
the exception or return information. Just issue our magic
opcode. */
c.Label(finally)
c.Op(vm.WITH_CLEANUP)
/* Finally block ends. */
c.Op(vm.END_FINALLY)
}
/*
Code generated for "try: <body> finally: <finalbody>" is as follows:
SETUP_FINALLY L
<code for body>
POP_BLOCK
LOAD_CONST <None>
L: <code for finalbody>
END_FINALLY
The special instructions use the block stack. Each block
stack entry contains the instruction that created it (here
SETUP_FINALLY), the level of the value stack at the time the
block stack entry was created, and a label (here L).
SETUP_FINALLY:
Pushes the current value stack level and the label
onto the block stack.
POP_BLOCK:
Pops en entry from the block stack, and pops the value
stack until its level is the same as indicated on the
block stack. (The label is ignored.)
END_FINALLY:
Pops a variable number of entries from the *value* stack
and re-raises the exception they specify. The number of
entries popped depends on the (pseudo) exception type.
The block stack is unwound when an exception is raised:
when a SETUP_FINALLY entry is found, the exception is pushed
onto the value stack (and the exception condition is cleared),
and the interpreter jumps to the label gotten from the block
stack.
*/
func (c *compiler) tryFinally(node *ast.Try) {
end := new(Label)
c.Jump(vm.SETUP_FINALLY, end)
if len(node.Handlers) > 0 {
c.tryExcept(node)
} else {
c.loops.Push(loop{Type: finallyTryLoop})
c.Stmts(node.Body)
c.loops.Pop()
}
c.Op(vm.POP_BLOCK)
c.LoadConst(py.None)
c.Label(end)
c.loops.Push(loop{Type: finallyEndLoop})
c.Stmts(node.Finalbody)
c.loops.Pop()
c.Op(vm.END_FINALLY)
}
/*
Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
(The contents of the value stack is shown in [], with the top
at the right; 'tb' is trace-back info, 'val' the exception's
associated value, and 'exc' the exception.)
Value stack Label Instruction Argument
[] SETUP_EXCEPT L1
[] <code for S>
[] POP_BLOCK
[] JUMP_FORWARD L0
[tb, val, exc] L1: DUP )
[tb, val, exc, exc] <evaluate E1> )
[tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
[tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
[tb, val, exc] POP
[tb, val] <assign to V1> (or POP if no V1)
[tb] POP
[] <code for S1>
JUMP_FORWARD L0
[tb, val, exc] L2: DUP
.............................etc.......................
[tb, val, exc] Ln+1: END_FINALLY # re-raise exception
[] L0: <next statement>
Of course, parts are not generated if Vi or Ei is not present.
*/
func (c *compiler) tryExcept(node *ast.Try) {
c.loops.Push(loop{Type: exceptLoop})
except := new(Label)
orelse := new(Label)
end := new(Label)
c.Jump(vm.SETUP_EXCEPT, except)
c.Stmts(node.Body)
c.Op(vm.POP_BLOCK)
c.Jump(vm.JUMP_FORWARD, orelse)
n := len(node.Handlers)
c.Label(except)
for i, handler := range node.Handlers {
if handler.ExprType == nil && i < n-1 {
c.panicSyntaxErrorf(handler, "default 'except:' must be last")
}
// FIXME c.u.u_lineno_set = 0
// c.u.u_lineno = handler.lineno
// c.u.u_col_offset = handler.col_offset
except := new(Label)
if handler.ExprType != nil {
c.Op(vm.DUP_TOP)
c.Expr(handler.ExprType)
c.OpArg(vm.COMPARE_OP, vm.PyCmp_EXC_MATCH)
c.Jump(vm.POP_JUMP_IF_FALSE, except)
}
c.Op(vm.POP_TOP)
if handler.Name != "" {
cleanup_end := new(Label)
c.NameOp(string(handler.Name), ast.Store)
c.Op(vm.POP_TOP)
/*
try:
# body
except type as name:
try:
# body
finally:
name = None
del name
*/
/* second try: */
c.Jump(vm.SETUP_FINALLY, cleanup_end)
/* second # body */
c.Stmts(handler.Body)
c.Op(vm.POP_BLOCK)
c.Op(vm.POP_EXCEPT)
/* finally: */
c.LoadConst(py.None)
c.Label(cleanup_end)
/* name = None */
c.LoadConst(py.None)
c.NameOp(string(handler.Name), ast.Store)
/* del name */
c.NameOp(string(handler.Name), ast.Del)
c.Op(vm.END_FINALLY)
} else {
c.Op(vm.POP_TOP)
c.Op(vm.POP_TOP)
c.Stmts(handler.Body)
c.Op(vm.POP_EXCEPT)
}
c.Jump(vm.JUMP_FORWARD, end)
c.Label(except)
}
c.Op(vm.END_FINALLY)
c.Label(orelse)
c.Stmts(node.Orelse)
c.Label(end)
c.loops.Pop()
}
// Compile a try statement
func (c *compiler) try(node *ast.Try) {
if len(node.Finalbody) > 0 {
c.tryFinally(node)
} else {
c.tryExcept(node)
}
}
/*
The IMPORT_NAME opcode was already generated. This function
merely needs to bind the result to a name.
If there is a dot in name, we need to split it and emit a
LOAD_ATTR for each name.
*/
func (c *compiler) importAs(name ast.Identifier, asname ast.Identifier) {
attrs := strings.Split(string(name), ".")
if len(attrs) > 1 {
for _, attr := range attrs[1:] {
c.OpArg(vm.LOAD_ATTR, c.Name(ast.Identifier(attr)))
}
}
c.NameOp(string(asname), ast.Store)
}
/*
The Import node stores a module name like a.b.c as a single
string. This is convenient for all cases except
import a.b.c as d
where we need to parse that string to extract the individual
module names.
XXX Perhaps change the representation to make this case simpler?
*/
func (c *compiler) import_(node *ast.Import) {
//n = asdl_seq_LEN(s.v.Import.names);
for _, alias := range node.Names {
c.LoadConst(py.Int(0))
c.LoadConst(py.None)
c.OpName(vm.IMPORT_NAME, alias.Name)
if alias.AsName != "" {
c.importAs(alias.Name, alias.AsName)
} else {
tmp := alias.Name
dot := strings.IndexByte(string(alias.Name), '.')
if dot >= 0 {
tmp = alias.Name[:dot]
}
c.NameOp(string(tmp), ast.Store)
}
}
}
func (c *compiler) importFrom(node *ast.ImportFrom) {
names := make(py.Tuple, len(node.Names))
/* build up the names */
for i, alias := range node.Names {
names[i] = py.String(alias.Name)
}
// FIXME if s.lineno > c.c_future.ff_lineno && s.v.ImportFrom.module && !PyUnicode_CompareWithASCIIString(s.v.ImportFrom.module, "__future__") {
// return compiler_error(c, "from __future__ imports must occur at the beginning of the file")
// }
c.LoadConst(py.Int(node.Level))
c.LoadConst(names)
c.OpName(vm.IMPORT_NAME, node.Module)
for i, alias := range node.Names {
if i == 0 && alias.Name[0] == '*' {
if len(alias.Name) != 1 {
panic("can only import *")
}
c.Op(vm.IMPORT_STAR)
return
}
c.OpName(vm.IMPORT_FROM, alias.Name)
store_name := alias.Name
if alias.AsName != "" {
store_name = alias.AsName
}
c.NameOp(string(store_name), ast.Store)
}
/* remove imported module */
c.Op(vm.POP_TOP)
}
// Compile statements
func (c *compiler) Stmts(stmts []ast.Stmt) {
for _, stmt := range stmts {