Skip to content
Permalink
Browse files

Merge pull request #4 from daitangio/bugfix/on_goto_missed

First on goto implementation with test
  • Loading branch information...
irmen committed Mar 20, 2019
2 parents 4e876e5 + 6f93f25 commit 9b102c52f9e5d4439f7205a99c9cadf4173cacff
Showing with 94 additions and 2 deletions.
  1. +4 −0 drive8/ongoto1.bas
  2. +5 −0 drive8/ongoto2.bas
  3. +50 −2 pyc64/basic.py
  4. 0 tests/__init__.py
  5. +35 −0 tests/on_goto.py
@@ -0,0 +1,4 @@
0 rem Tester
20 on 2 goto 30,40
30 print "failure"
40 print "success"
@@ -0,0 +1,5 @@
0 rem test with variable
10 a=2
20 on a goto 30,40
30 print "failure"
40 print "success"
@@ -160,7 +160,7 @@ def execute_line(self, line, recursive=False):
# schedule next line to be executed
self.next_run_line_idx += 1
except GotoLineException as gx:
self.next_run_line_idx = gx.line_idx
self.implementGoto(gx)
except FlowcontrolException:
if in_program:
if not recursive:
@@ -183,6 +183,9 @@ def execute_line(self, line, recursive=False):
self.write_prompt()
self.stop_running_program()

def implementGoto(self,gx: GotoLineException):
self.next_run_line_idx = gx.line_idx

def process_programline_entry(self, line):
match = re.match("(\d+)(\s*.*)", line)
if match:
@@ -242,6 +245,8 @@ def _execute_cmd(self, cmd, all_cmds_on_line=None):
self.execute_sys(cmd)
elif cmd.startswith(("goto", "gO")):
self.execute_goto(cmd)
elif cmd.startswith(("on")):
self.execute_on_goto_gosub(cmd)
elif cmd.startswith(("for", "fO")):
self.execute_for(cmd, all_cmds_on_line)
elif cmd.startswith(("next", "nE")):
@@ -292,7 +297,9 @@ def _execute_cmd(self, cmd, all_cmds_on_line=None):
def execute_help(self, cmd):
self.screen.writestr("\nknown statements:\n")
known = ["?", "print", "cls", "color", "cursor", "data", "dos", "end", "for", "get", "gopy",
"goto", "if", "list", "load", "new", "next", "peek", "peekw", "poke", "pokew",
"goto",
"on...goto",
"if", "list", "load", "new", "next", "peek", "peekw", "poke", "pokew",
"read", "rem", "restore", "run", "save", "scroll", "sleep", "stop", "sys", "help",
"monitor", "sync"]
for kw in sorted(known):
@@ -405,6 +412,47 @@ def execute_goto(self, cmd):
if line not in self.program:
raise BasicError("undef'd statement")
raise GotoLineException(self.program_lines.index(line))
"""
on <index-1-based> goto|gosub <line1>,<line2>
if index evaluate to 1 the execution proceed on line1
"""
def execute_on_goto_gosub(self,cmd):
gosub=False
if cmd.startswith("on"):
cmd=cmd[2:]
if cmd.find("goto")==-1 and cmd.find("gosub")==-1:
raise BasicError("syntax")
else:
# Find out index list
l2=(cmd[(cmd.find("go")+2):]).strip()
# DEBUG print(l2)
if l2.startswith("to"):
# goto branch
gosub=False
targetLineList=l2[2:]
elif l2.startswith("sub"):
gosub=True
targetLineList=l2[3:]
else:
raise BasicError("syntax")
lineTargetTuple=targetLineList.strip().split(",")
goInx=cmd.find("go")
expr=cmd[0:goInx]
# eval the on <expr> goto part
onGoIndex=int(eval(expr,self.symbols))-1
line=int(lineTargetTuple[onGoIndex])
if gosub==False:
if not self.running_program:
self.execute_run("run " + str(line))
else:
if line not in self.program:
raise BasicError("undef'd statement")
raise GotoLineException(self.program_lines.index(line))
else:
raise BasicError("gosub unsupported yet")
# raise BasicError("syntax")


def execute_sleep(self, cmd, all_cmds_on_line):
if cmd.startswith("sL"):
No changes.
@@ -0,0 +1,35 @@
import unittest
from pyc64.basic import BasicInterpreter, BasicError, GotoLineException
from pyc64.memory import ScreenAndMemory

class BasicEmulatorTest(unittest.TestCase):
def test_ok(self):
self.assertEqual(True,True)
def test_on_goto0(self):
try:
b=BasicInterpreter(ScreenAndMemory())
b.execute_on_goto_gosub("on 1 goto 1,2")
self.fail("unde'd statement error expected")
except BasicError as e:
pass

def test_on_goto1(self):
try:
# LOADED:{0: 'rem Tester', 20: 'on 2 goto 30,40', 30: 'print "failure"', 40: 'print "success"'}
b=BasicInterpreter(ScreenAndMemory())
b.execute_load("load \"ongoto1\" ")
b.execute_run("run 20")
#b.execute_line("run 20")
self.fail("No error?")
except GotoLineException as gx:
b.implementGoto(gx)
self.assertTrue(b.running_program)
b.program_step()
print("CURRENT LINE:"+str(b.next_run_line_idx))
self.assertEqual(3,b.next_run_line_idx)
#b.execute_run("run")
# print("***"+str(b.))
# TODO Implement a silly test program to test the on expr syntax

if __name__ == '__main__':
unittest.main()

0 comments on commit 9b102c5

Please sign in to comment.
You can’t perform that action at this time.