Open waynee95 opened 1 year ago
For now I fixed it with a dirty fix:
def eval_imm(self, e) -> int:
if e.data == 'int_a':
### fix ###
from lark import Tree
if type(e.children[0]) == Tree:
v = int(e.children[0].children[0])
else:
### fix ###
v = int(e.children[0])
if is_int64(v):
return v
else:
raise Exception('eval_imm: invalid immediate:', v)
#if e.data == 'int_a':
# return int(e.children[0])
#elif e.data == 'neg_a':
if e.data == 'neg_a':
return -self.eval_imm(e.children[0])
else:
raise Exception('eval_imm: unknown immediate:', e)
Another thing that was missing is the actual "eval" part in the function parse_and_eval_program
:
def parse_and_eval_program(self, s):
p = x86_parser.parse(s)
return self.eval_program(p)
Using
x86_parser.parse
on the following assembly program (equivalent toprint(40 + 2)
)yields something of the form (I truncated the output)
which is not in the correct shape for
eval_x86.eval_program
to accept it.However, evaluating assembly generated by the compiler works.
Using a finished compiler from chapter 2 to compile
print(40 + 2)
to x86 ast and then usingconvert_x86.convert_program
yields something of the form (I truncated the output again)which is of the correct form for
eval_x86.eval_program
to accept and produce a result.I feel like I am missing something since
eval_x86.eval_program
is written to operate on the result that lark produces no?