1
0
mirror of https://github.com/rzzzwilson/pymlac.git synced 2025-06-10 09:32:41 +00:00

Allow value syntax in test comments

This commit is contained in:
Ross Wilson 2016-02-13 17:29:53 +07:00
parent fd03f7576e
commit 6df2cdcd11
7 changed files with 71 additions and 9 deletions

View File

@ -35,6 +35,34 @@ BlockloaderSize = 8 * 16 # 8 lines of 16 bytes
HighBitMask = 0100000
def eval_expr(dot, expr):
"""Evaluate an expression that may contain '.'.
dot the current value of 'dot'
expr the expression string to avaluate
Returns the value of the expression.
Raises ArithmeticError if something is wrong.
"""
# replace any "." value with "dot" defined in the symbol table
expr = string.replace(expr, '.', 'DOT')
# evaluate the expression
globs = {'DOT': dot} # passed environment contains 'DOT'
try:
result = eval(expr, globs)
except (TypeError, NameError) as e:
Undefined = e.message
if 'is not defined' in e.message:
Undefined = e.message[len("name '"):-len("' is not defined")]
msg = "Test expression has '%s' undefined" % Undefined
raise ArithmeticError(msg)
msg = "Test expression has an error"
raise ArithmeticError(msg)
return result
def oct_dec_int(s):
"""Convert string 's' to binary integer."""
@ -187,7 +215,7 @@ def test_file(filename):
else:
# have label, $n or octal/decimal?
# set 'dot' either way
(label, value) = test.split()
(label, value) = test.split(' ', 1)
if label[0] == '$':
org_num = int(label[1:])
try:
@ -200,7 +228,9 @@ def test_file(filename):
else:
address = oct_dec_int(label)
dot = address
value = oct_dec_int(value)
# evaluate the value field
value = eval_expr(dot, value)
if address is None:
warn("File '%s' has label '%s' with bad ORG number"
@ -224,9 +254,6 @@ def test_file(filename):
dot += 1
# if errors:
# print("File '%s' had errors" % filename)
def warn(msg):
"""Print error message and continue."""

View File

@ -1,4 +1,4 @@
; test file
; test file, old syntax tests
org 0100
law 1
hlt

View File

@ -1,7 +1,7 @@
; test file
; test file, new syntax
org 0100
law 1
hlt
end
;|$1 004001
;|$1 004000 + 1
;| 000000

View File

@ -0,0 +1,9 @@
; deliberate errors at lines 7 and 9
org 0100
law 1
hlt
end
;|$1 004000 + 2 ; is actually 004001
;| 000000
;| 000001 ; address outside block

View File

@ -0,0 +1,12 @@
; test file with two ORG blocks
org 0100
law 1
hlt
org 0200
law 2
hlt
end
;|$1 004000 + 1
;| 000000
;|$2 004000 + 2
;| 000000

View File

@ -4,5 +4,5 @@
hlt
end
;|$2 004001 ; bad ORG number
;|$3 004001 ; bad ORG number
;| 000000

View File

@ -0,0 +1,14 @@
; test file with two adjacent ORG blocks
org 0100
law 1
hlt
org 0102
law 2
hlt
end
;|$1 004000 + 1
;| 000000
;| 004000 + 2
;| 000000