Skip to content

Commit c8a27c6

Browse files
committed
Implement blocks and make while loop work. Fix args > 255 also
1 parent 36faf9f commit c8a27c6

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

notes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Testing
1616
python3 -m dis hello.py
1717
python3 -mcompileall hello.py
1818
mv __pycache__/hello.cpython-33.pyc hello.pyc
19-
python -c 'import hello; import dis; dis.dis(hello.fn)'
19+
python3 -c 'import hello; import dis; dis.dis(hello.fn)'
2020

2121
go build ./... && go build
2222
./gpython hello.pyc

py/frame.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88

99
// Store information about try blocks
1010
type TryBlock struct {
11-
Type int // what kind of block this is
12-
Handler int // where to jump to find handler
13-
Level int // value stack level to pop to
11+
Type byte // what kind of block this is (the opcode)
12+
Handler int32 // where to jump to find handler
13+
Level int // value stack level to pop to
1414
}
1515

1616
// A python Frame object
@@ -51,6 +51,7 @@ type Frame struct {
5151
Iblock int // index in f_blockstack
5252
Executing byte // whether the frame is still executing
5353
Blockstack []TryBlock // for try and loop blocks
54+
Block *TryBlock // pointer to current block or nil
5455
Localsplus []Object // locals+stack, dynamically sized
5556
}
5657

@@ -90,3 +91,23 @@ func (f *Frame) Lookup(name string) (obj Object) {
9091
// FIXME this should be a NameError
9192
panic(fmt.Sprintf("NameError: name '%s' is not defined", name))
9293
}
94+
95+
// Make a new Block (try/for/while)
96+
func (f *Frame) PushBlock(Type byte, Handler int32, Level int) {
97+
f.Blockstack = append(f.Blockstack, TryBlock{
98+
Type: Type,
99+
Handler: Handler,
100+
Level: Level,
101+
})
102+
f.Block = &f.Blockstack[len(f.Blockstack)-1]
103+
}
104+
105+
// Pop the current block off
106+
func (f *Frame) PopBlock() {
107+
f.Blockstack = f.Blockstack[:len(f.Blockstack)-1]
108+
if len(f.Blockstack) > 0 {
109+
f.Block = &f.Blockstack[len(f.Blockstack)-1]
110+
} else {
111+
f.Block = nil
112+
}
113+
}

vm/eval.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func do_IMPORT_STAR(vm *Vm, arg int32) {
379379
// Removes one block from the block stack. Per frame, there is a stack
380380
// of blocks, denoting nested loops, try statements, and such.
381381
func do_POP_BLOCK(vm *Vm, arg int32) {
382-
vm.NotImplemented("POP_BLOCK", arg)
382+
vm.frame.PopBlock()
383383
}
384384

385385
// Removes one block from the block stack. The popped block must be an
@@ -620,19 +620,19 @@ func do_LOAD_GLOBAL(vm *Vm, namei int32) {
620620
// Pushes a block for a loop onto the block stack. The block spans
621621
// from the current instruction with a size of delta bytes.
622622
func do_SETUP_LOOP(vm *Vm, delta int32) {
623-
vm.NotImplemented("SETUP_LOOP", delta)
623+
vm.frame.PushBlock(SETUP_LOOP, vm.frame.Lasti+delta, len(vm.stack))
624624
}
625625

626626
// Pushes a try block from a try-except clause onto the block
627627
// stack. delta points to the first except block.
628628
func do_SETUP_EXCEPT(vm *Vm, delta int32) {
629-
vm.NotImplemented("SETUP_EXCEPT", delta)
629+
vm.frame.PushBlock(SETUP_EXCEPT, vm.frame.Lasti+delta, len(vm.stack))
630630
}
631631

632632
// Pushes a try block from a try-except clause onto the block
633633
// stack. delta points to the finally block.
634634
func do_SETUP_FINALLY(vm *Vm, delta int32) {
635-
vm.NotImplemented("SETUP_FINALLY", delta)
635+
vm.frame.PushBlock(SETUP_FINALLY, vm.frame.Lasti+delta, len(vm.stack))
636636
}
637637

638638
// Store a key and value pair in a dictionary. Pops the key and value
@@ -931,7 +931,7 @@ func Run(globals, locals py.StringDict, code *py.Code) (err error) {
931931
if HAS_ARG(opcode) {
932932
arg = int32(opcodes[frame.Lasti])
933933
frame.Lasti++
934-
arg += int32(opcodes[frame.Lasti] << 8)
934+
arg += int32(opcodes[frame.Lasti]) << 8
935935
frame.Lasti++
936936
if vm.extended {
937937
arg += vm.ext << 16

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