Skip to content

Commit 30367ff

Browse files
committed
vm: rename variables to be more like the python original code
1 parent 87adad3 commit 30367ff

File tree

3 files changed

+69
-73
lines changed

3 files changed

+69
-73
lines changed

‎vm/eval.go‎

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Evaluate opcodes
22
package vm
33

4-
// FIXME vm.result -> retval
5-
// vm.exit -> vm.why
6-
// why codes
7-
84
// FIXME make opcode its own type so can stringer
95

106
// FIXME use LocalVars instead of storing everything in the Locals dict
@@ -119,13 +115,13 @@ func (vm *Vm) AddTraceback(exc *py.ExceptionInfo){
119115
// The exception must be a valid exception instance (eg as returned by
120116
// py.MakeException)
121117
//
122-
// It sets vm.curexc.* and sets vm.exit to exitException
118+
// It sets vm.curexc.* and sets vm.why to whyException
123119
func (vm*Vm) SetException(exception py.Object){
124120
vm.curexc.Value=exception
125121
vm.curexc.Type=exception.Type()
126122
vm.curexc.Traceback=nil
127123
vm.AddTraceback(&vm.curexc)
128-
vm.exit=exitException
124+
vm.why=whyException
129125
}
130126

131127
// Check for an exception (panic)
@@ -136,7 +132,7 @@ func (vm *Vm) CheckExceptionRecover(r interface{}){
136132
ifexc, ok:=r.(py.ExceptionInfo); ok{
137133
vm.curexc=exc
138134
vm.AddTraceback(&vm.curexc)
139-
vm.exit=exitException
135+
vm.why=whyException
140136
debugf("*** Propagating exception: %s\n", exc.Error())
141137
} else{
142138
// Coerce whatever was raised into a *Exception
@@ -489,15 +485,15 @@ func do_PRINT_EXPR(vm *Vm, arg int32){
489485
// Terminates a loop due to a break statement.
490486
funcdo_BREAK_LOOP(vm*Vm, argint32){
491487
defervm.CheckException()
492-
vm.exit=exitBreak
488+
vm.why=whyBreak
493489
}
494490

495491
// Continues a loop due to a continue statement. target is the address
496492
// to jump to (which should be a FOR_ITER instruction).
497493
funcdo_CONTINUE_LOOP(vm*Vm, targetint32){
498494
defervm.CheckException()
499-
vm.result=py.Int(target)
500-
vm.exit=exitContinue
495+
vm.retval=py.Int(target)
496+
vm.why=whyContinue
501497
}
502498

503499
// Iterate v argcnt times and store the results on the stack (via decreasing
@@ -599,13 +595,13 @@ func do_MAP_ADD(vm *Vm, i int32){
599595
// Returns with TOS to the caller of the function.
600596
funcdo_RETURN_VALUE(vm*Vm, argint32){
601597
defervm.CheckException()
602-
vm.result=vm.POP()
598+
vm.retval=vm.POP()
603599
iflen(vm.frame.Stack) !=0{
604600
debugf("vmstack = %#v\n", vm.frame.Stack)
605601
panic("vm stack should be empty at this point")
606602
}
607603
vm.frame.Yielded=false
608-
vm.exit=exitReturn
604+
vm.why=whyReturn
609605
}
610606

611607
// Pops TOS and delegates to it as a subiterator from a generator.
@@ -627,21 +623,21 @@ func do_YIELD_FROM(vm *Vm, arg int32){
627623
}
628624
// x remains on stack, retval is value to be yielded
629625
// FIXME vm.frame.Stacktop = stack_pointer
630-
//why = exitYield
626+
//why = whyYield
631627
// and repeat...
632628
vm.frame.Lasti--
633629

634-
vm.result=retval
630+
vm.retval=retval
635631
vm.frame.Yielded=true
636-
vm.exit=exitYield
632+
vm.why=whyYield
637633
}
638634

639635
// Pops TOS and yields it from a generator.
640636
funcdo_YIELD_VALUE(vm*Vm, argint32){
641637
defervm.CheckException()
642-
vm.result=vm.POP()
638+
vm.retval=vm.POP()
643639
vm.frame.Yielded=true
644-
vm.exit=exitYield
640+
vm.why=whyYield
645641
}
646642

647643
// Loads all symbols not starting with '_' directly from the module
@@ -702,16 +698,16 @@ func do_END_FINALLY(vm *Vm, arg int32){
702698
// None exception
703699
debugf(" END_FINALLY: None\n")
704700
} elseifvInt, ok:=v.(py.Int); ok{
705-
vm.exit=vmExit(vInt)
706-
debugf(" END_FINALLY: Int %v\n", vm.exit)
707-
switchvm.exit{
708-
caseexitYield:
709-
panic("Unexpected exitYield in END_FINALLY")
710-
caseexitException:
711-
panic("Unexpected exitException in END_FINALLY")
712-
caseexitReturn, exitContinue:
713-
vm.result=vm.POP()
714-
caseexitSilenced:
701+
vm.why=vmStatus(vInt)
702+
debugf(" END_FINALLY: Int %v\n", vm.why)
703+
switchvm.why{
704+
casewhyYield:
705+
panic("Unexpected whyYield in END_FINALLY")
706+
casewhyException:
707+
panic("Unexpected whyException in END_FINALLY")
708+
casewhyReturn, whyContinue:
709+
vm.retval=vm.POP()
710+
casewhySilenced:
715711
// An exception was silenced by 'with', we must
716712
// manually unwind the EXCEPT_HANDLER block which was
717713
// created when the exception was caught, otherwise
@@ -723,7 +719,7 @@ func do_END_FINALLY(vm *Vm, arg int32){
723719
panic("Expecting EXCEPT_HANDLER in END_FINALLY")
724720
}
725721
vm.UnwindExceptHandler(frame, b)
726-
vm.exit=exitNot
722+
vm.why=whyNot
727723
}
728724
} elseifpy.ExceptionClassCheck(v){
729725
w:=vm.POP()
@@ -733,11 +729,11 @@ func do_END_FINALLY(vm *Vm, arg int32){
733729
vm.curexc.Type, _=v.(*py.Type)
734730
vm.curexc.Value=w
735731
vm.curexc.Traceback, _=u.(*py.Traceback)
736-
vm.exit=exitException
732+
vm.why=whyException
737733
} else{
738734
vm.SetException(py.ExceptionNewf(py.SystemError, "'finally' pops bad exception %#v", v))
739735
}
740-
debugf("END_FINALLY: vm.exit = %v\n", vm.exit)
736+
debugf("END_FINALLY: vm.why = %v\n", vm.why)
741737
}
742738

743739
// Loads the __build_class__ helper function to the stack which
@@ -1250,7 +1246,7 @@ func (vm *Vm) raise(exc, cause py.Object){
12501246
// Resignal the exception
12511247
vm.curexc=vm.exc
12521248
// Signal the existing exception again
1253-
vm.exit=exitException
1249+
vm.why=whyException
12541250

12551251
}
12561252
} else{
@@ -1518,7 +1514,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error){
15181514

15191515
varopcodebyte
15201516
varargint32
1521-
forvm.exit==exitNot{
1517+
forvm.why==whyNot{
15221518
frame:=vm.frame
15231519
debugf("* %4d:", frame.Lasti)
15241520
opcodes:=frame.Code.Code
@@ -1546,21 +1542,21 @@ func RunFrame(frame *py.Frame) (res py.Object, err error){
15461542
// }
15471543
// }
15481544
}
1549-
ifvm.exit==exitYield{
1545+
ifvm.why==whyYield{
15501546
goto fast_yield
15511547
}
15521548

15531549
// Something exceptional has happened - unwind the block stack
15541550
// and find out what
1555-
forvm.exit!=exitNot&&vm.frame.Block!=nil{
1551+
forvm.why!=whyNot&&vm.frame.Block!=nil{
15561552
// Peek at the current block.
15571553
frame:=vm.frame
15581554
b:=frame.Block
15591555
debugf("*** Unwinding %#v vm %#v\n", b, vm)
15601556

1561-
ifb.Type==SETUP_LOOP&&vm.exit==exitContinue{
1562-
vm.exit=exitNot
1563-
dest:=vm.result.(py.Int)
1557+
ifb.Type==SETUP_LOOP&&vm.why==whyContinue{
1558+
vm.why=whyNot
1559+
dest:=vm.retval.(py.Int)
15641560
frame.Lasti=int32(dest)
15651561
break
15661562
}
@@ -1574,13 +1570,13 @@ func RunFrame(frame *py.Frame) (res py.Object, err error){
15741570
continue
15751571
}
15761572
vm.UnwindBlock(frame, b)
1577-
ifb.Type==SETUP_LOOP&&vm.exit==exitBreak{
1573+
ifb.Type==SETUP_LOOP&&vm.why==whyBreak{
15781574
debugf("*** Loop\n")
1579-
vm.exit=exitNot
1575+
vm.why=whyNot
15801576
frame.Lasti=b.Handler
15811577
break
15821578
}
1583-
ifvm.exit==exitException&& (b.Type==SETUP_EXCEPT||b.Type==SETUP_FINALLY){
1579+
ifvm.why==whyException&& (b.Type==SETUP_EXCEPT||b.Type==SETUP_FINALLY){
15841580
debugf("*** Exception\n")
15851581
handler:=b.Handler
15861582
// This invalidates b
@@ -1615,29 +1611,29 @@ func RunFrame(frame *py.Frame) (res py.Object, err error){
16151611
} else{
16161612
vm.PUSH(exc)
16171613
}
1618-
vm.exit=exitNot
1614+
vm.why=whyNot
16191615
frame.Lasti=handler
16201616
break
16211617
}
16221618
ifb.Type==SETUP_FINALLY{
1623-
ifvm.exit==exitReturn||vm.exit==exitContinue{
1624-
vm.PUSH(vm.result)
1619+
ifvm.why==whyReturn||vm.why==whyContinue{
1620+
vm.PUSH(vm.retval)
16251621
}
1626-
vm.PUSH(py.Int(vm.exit))
1627-
vm.exit=exitNot
1622+
vm.PUSH(py.Int(vm.why))
1623+
vm.why=whyNot
16281624
frame.Lasti=b.Handler
16291625
break
16301626
}
16311627
}
16321628
}
1633-
debugf("EXIT with %v\n", vm.exit)
1634-
ifvm.exit!=exitReturn{
1635-
vm.result=nil
1629+
debugf("EXIT with %v\n", vm.why)
1630+
ifvm.why!=whyReturn{
1631+
vm.retval=nil
16361632
}
1637-
ifvm.result==nil&&!vm.curexc.IsSet(){
1633+
ifvm.retval==nil&&!vm.curexc.IsSet(){
16381634
panic("vm: no result or exception")
16391635
}
1640-
ifvm.result!=nil&&vm.curexc.IsSet(){
1636+
ifvm.retval!=nil&&vm.curexc.IsSet(){
16411637
panic("vm: result and exception")
16421638
}
16431639

@@ -1664,9 +1660,9 @@ fast_yield:
16641660
// }
16651661

16661662
ifvm.curexc.IsSet(){
1667-
returnvm.result, vm.curexc
1663+
returnvm.retval, vm.curexc
16681664
}
1669-
returnvm.result, nil
1665+
returnvm.retval, nil
16701666
}
16711667

16721668
// Run the virtual machine on a Code object

‎vm/stringer.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// generated by stringer -type=vmExit -output stringer.go; DO NOT EDIT
1+
// generated by stringer -type=vmStatus -output stringer.go; DO NOT EDIT
22

33
package vm
44

55
import"fmt"
66

7-
const_vmExit_name="exitNotexitExceptionexitReturnexitBreakexitContinueexitYieldexitSilenced"
7+
const_vmStatus_name="whyNotwhyExceptionwhyReturnwhyBreakwhyContinuewhyYieldwhySilenced"
88

9-
var_vmExit_index= [...]uint8{0, 7, 20, 30, 39, 51, 60, 72}
9+
var_vmStatus_index= [...]uint8{0, 6, 18, 27, 35, 46, 54, 65}
1010

11-
func (ivmExit) String() string{
12-
ifi+1>=vmExit(len(_vmExit_index)){
13-
returnfmt.Sprintf("vmExit(%d)", i)
11+
func (ivmStatus) String() string{
12+
ifi+1>=vmStatus(len(_vmStatus_index)){
13+
returnfmt.Sprintf("vmStatus(%d)", i)
1414
}
15-
return_vmExit_name[_vmExit_index[i]:_vmExit_index[i+1]]
15+
return_vmStatus_name[_vmStatus_index[i]:_vmStatus_index[i+1]]
1616
}

‎vm/vm.go‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import (
55
"github.com/ncw/gpython/py"
66
)
77

8-
//go:generate stringer -type=vmExit -output stringer.go
8+
//go:generate stringer -type=vmStatus -output stringer.go
99

10-
// VM exit type
11-
typevmExitbyte
10+
// VM status code
11+
typevmStatusbyte
1212

13-
// VM exit values
13+
// VM Status code for main loop (reason for stack unwind)
1414
const (
15-
exitNotvmExit=iota// No error
16-
exitException// Exception occurred
17-
exitReturn// 'return' statement
18-
exitBreak// 'break' statement
19-
exitContinue// 'continue' statement
20-
exitYield// 'yield' operator
21-
exitSilenced// Exception silenced by 'with'
15+
whyNotvmStatus=iota// No error
16+
whyException// Exception occurred
17+
whyReturn// 'return' statement
18+
whyBreak// 'break' statement
19+
whyContinue// 'continue' statement
20+
whyYield// 'yield' operator
21+
whySilenced// Exception silenced by 'with'
2222
)
2323

2424
// Virtual machine state
@@ -30,9 +30,9 @@ type Vm struct{
3030
// 16 bit extension for argument for next opcode
3131
extint32
3232
// Return value
33-
result py.Object
34-
// Exit value
35-
exitvmExit
33+
retval py.Object
34+
// VM Status code for main loop
35+
whyvmStatus
3636
// Current Pending exception type, value and traceback
3737
curexc py.ExceptionInfo
3838
// Previous exception type, value and traceback

0 commit comments

Comments
(0)