prog8/il65/plyparser.py

784 lines
18 KiB
Python
Raw Normal View History

2018-01-07 01:36:27 +00:00
"""
Programming Language for 6502/6510 microprocessors
This is the parser of the IL65 code, that generates a parse tree.
Written by Irmen de Jong (irmen@razorvine.net)
License: GNU GPL 3.0, see LICENSE
"""
2018-01-07 13:36:12 +00:00
import attr
2018-01-03 20:43:19 +00:00
from ply.yacc import yacc
2018-01-07 13:36:12 +00:00
from typing import Union
from .symbols import SourceRef
2018-01-03 20:43:19 +00:00
from .lexer import tokens, lexer, find_tok_column # get the lexer tokens. required.
2018-01-07 13:36:12 +00:00
2018-01-03 20:43:19 +00:00
start = "start"
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False, slots=True, frozen=False)
class AstNode:
sourceref = attr.ib(type=SourceRef)
@property
def lineref(self) -> str:
return "src l. " + str(self.sourceref.line)
def print_tree(self) -> None:
def tostr(node: AstNode, level: int) -> None:
if not isinstance(node, AstNode):
return
indent = " " * level
name = getattr(node, "name", "")
print(indent, node.__class__.__name__, repr(name))
try:
variables = vars(node).items()
except TypeError:
return
for name, value in variables:
if isinstance(value, AstNode):
tostr(value, level + 1)
if isinstance(value, (list, tuple, set)):
if len(value) > 0:
elt = list(value)[0]
if isinstance(elt, AstNode) or name == "nodes":
print(indent, " >", name, "=")
for elt in value:
tostr(elt, level + 2)
tostr(self, 0)
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Module(AstNode):
2018-01-07 13:36:12 +00:00
nodes = attr.ib(type=list)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Directive(AstNode):
2018-01-07 13:36:12 +00:00
name = attr.ib(type=str)
args = attr.ib(type=list, default=attr.Factory(list))
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
class Scope(AstNode):
nodes = attr.ib(type=list)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
class Block(AstNode):
scope = attr.ib(type=Scope)
name = attr.ib(type=str, default=None)
address = attr.ib(type=int, default=None)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Label(AstNode):
2018-01-07 13:36:12 +00:00
name = attr.ib(type=str)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Register(AstNode):
2018-01-07 13:36:12 +00:00
name = attr.ib(type=str)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class PreserveRegs(AstNode):
2018-01-07 13:36:12 +00:00
registers = attr.ib(type=str)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Assignment(AstNode):
2018-01-07 13:36:12 +00:00
left = attr.ib() # type: Union[str, TargetRegisters, Dereference]
right = attr.ib()
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-07 01:36:27 +00:00
class AugAssignment(Assignment):
2018-01-07 13:36:12 +00:00
operator = attr.ib(type=str)
2018-01-07 01:36:27 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class SubCall(AstNode):
2018-01-07 13:36:12 +00:00
target = attr.ib()
preserve_regs = attr.ib()
arguments = attr.ib()
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-05 21:52:23 +00:00
class Return(AstNode):
2018-01-07 13:36:12 +00:00
value_A = attr.ib(default=None)
value_X = attr.ib(default=None)
value_Y = attr.ib(default=None)
2018-01-07 01:36:27 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-07 01:36:27 +00:00
class TargetRegisters(AstNode):
2018-01-07 13:36:12 +00:00
registers = attr.ib(type=list)
2018-01-05 21:52:23 +00:00
2018-01-07 13:36:12 +00:00
def add(self, register: str) -> None:
2018-01-07 03:39:21 +00:00
self.registers.append(register)
2018-01-05 21:52:23 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class InlineAssembly(AstNode):
2018-01-07 13:36:12 +00:00
assembly = attr.ib(type=str)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class VarDef(AstNode):
2018-01-07 13:36:12 +00:00
name = attr.ib(type=str)
vartype = attr.ib()
datatype = attr.ib()
value = attr.ib(default=None)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False, slots=True)
2018-01-03 20:43:19 +00:00
class Datatype(AstNode):
2018-01-07 13:36:12 +00:00
name = attr.ib(type=str)
dimension = attr.ib(type=list, default=None)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Subroutine(AstNode):
2018-01-07 13:36:12 +00:00
name = attr.ib(type=str)
param_spec = attr.ib()
result_spec = attr.ib()
scope = attr.ib(type=Scope, default=None)
address = attr.ib(type=int, default=None)
def __attrs_post_init__(self):
if self.scope is not None and self.address is not None:
raise ValueError("subroutine must have either a scope or an address, not both")
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class Goto(AstNode):
2018-01-07 13:36:12 +00:00
target = attr.ib()
if_stmt = attr.ib(default=None)
condition = attr.ib(default=None)
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-05 21:52:23 +00:00
class Dereference(AstNode):
2018-01-07 13:36:12 +00:00
location = attr.ib()
datatype = attr.ib()
2018-01-05 21:52:23 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False, slots=True)
2018-01-05 21:52:23 +00:00
class CallTarget(AstNode):
2018-01-07 13:36:12 +00:00
target = attr.ib()
address_of = attr.ib(type=bool)
2018-01-05 21:52:23 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False, slots=True)
2018-01-05 21:52:23 +00:00
class CallArgument(AstNode):
2018-01-07 13:36:12 +00:00
value = attr.ib()
name = attr.ib(type=str, default=None)
2018-01-05 21:52:23 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False)
2018-01-03 20:43:19 +00:00
class UnaryOp(AstNode):
2018-01-07 13:36:12 +00:00
operator = attr.ib(type=str)
operand = attr.ib()
2018-01-03 20:43:19 +00:00
2018-01-07 13:36:12 +00:00
@attr.s(cmp=False, slots=True)
2018-01-07 01:36:27 +00:00
class Expression(AstNode):
2018-01-07 13:36:12 +00:00
left = attr.ib()
operator = attr.ib(type=str)
right = attr.ib()
2018-01-03 20:43:19 +00:00
def p_start(p):
2018-01-07 01:36:27 +00:00
"""
start : empty
| module_elements
"""
2018-01-03 20:43:19 +00:00
if p[1]:
2018-01-07 13:36:12 +00:00
p[0] = Module(nodes=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_module(p):
2018-01-07 01:36:27 +00:00
"""
module_elements : module_elt
| module_elements module_elt
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]
def p_module_elt(p):
2018-01-07 01:36:27 +00:00
"""
module_elt : ENDL
| directive
| block
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_directive(p):
2018-01-07 01:36:27 +00:00
"""
directive : DIRECTIVE ENDL
| DIRECTIVE directive_args ENDL
2018-01-03 20:43:19 +00:00
"""
if len(p) == 2:
2018-01-07 13:36:12 +00:00
p[0] = Directive(name=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
else:
2018-01-07 13:36:12 +00:00
p[0] = Directive(name=p[1], args=p[2], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_directive_args(p):
2018-01-07 01:36:27 +00:00
"""
directive_args : directive_arg
| directive_args ',' directive_arg
2018-01-03 20:43:19 +00:00
"""
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[3]]
def p_directive_arg(p):
2018-01-07 01:36:27 +00:00
"""
directive_arg : NAME
| INTEGER
| STRING
2018-01-03 20:43:19 +00:00
"""
p[0] = p[1]
2018-01-05 21:52:23 +00:00
def p_block_name_addr(p):
2018-01-07 01:36:27 +00:00
"""
block : BITINVERT NAME INTEGER endl_opt scope
"""
2018-01-07 13:36:12 +00:00
p[0] = Block(name=p[2], address=p[3], scope=p[5], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_block_name(p):
2018-01-07 01:36:27 +00:00
"""
block : BITINVERT NAME endl_opt scope
"""
2018-01-07 13:36:12 +00:00
p[0] = Block(name=p[2], scope=p[4], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
2018-01-03 20:43:19 +00:00
def p_block(p):
2018-01-07 01:36:27 +00:00
"""
block : BITINVERT endl_opt scope
"""
2018-01-07 13:36:12 +00:00
p[0] = Block(scope=p[3], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_endl_opt(p):
2018-01-07 01:36:27 +00:00
"""
endl_opt : empty
| ENDL
"""
pass
2018-01-03 20:43:19 +00:00
def p_scope(p):
2018-01-07 01:36:27 +00:00
"""
scope : '{' scope_elements_opt '}'
"""
2018-01-07 13:36:12 +00:00
p[0] = Scope(nodes=p[2], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_scope_elements_opt(p):
2018-01-07 01:36:27 +00:00
"""
scope_elements_opt : empty
| scope_elements
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_scope_elements(p):
2018-01-07 01:36:27 +00:00
"""
scope_elements : scope_element
| scope_elements scope_element
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]
def p_scope_element(p):
2018-01-07 01:36:27 +00:00
"""
scope_element : ENDL
| label
| directive
| vardef
| subroutine
| inlineasm
| statement
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_label(p):
2018-01-07 01:36:27 +00:00
"""
label : LABEL
"""
2018-01-07 13:36:12 +00:00
p[0] = Label(name=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_inlineasm(p):
2018-01-07 01:36:27 +00:00
"""
inlineasm : INLINEASM ENDL
"""
2018-01-07 13:36:12 +00:00
p[0] = InlineAssembly(assembly=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_vardef(p):
2018-01-07 01:36:27 +00:00
"""
vardef : VARTYPE type_opt NAME ENDL
"""
2018-01-07 13:36:12 +00:00
p[0] = VarDef(name=p[3], vartype=p[1], datatype=p[2], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_vardef_value(p):
2018-01-07 01:36:27 +00:00
"""
vardef : VARTYPE type_opt NAME IS expression
"""
2018-01-07 13:36:12 +00:00
p[0] = VarDef(name=p[3], vartype=p[1], datatype=p[2], value=p[5], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_type_opt(p):
2018-01-07 01:36:27 +00:00
"""
type_opt : DATATYPE '(' dimensions ')'
| DATATYPE
| empty
"""
if len(p) == 5:
2018-01-07 13:36:12 +00:00
p[0] = Datatype(name=p[1], dimension=p[3], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
elif len(p) == 2:
2018-01-07 13:36:12 +00:00
p[0] = Datatype(name=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_dimensions(p):
2018-01-07 01:36:27 +00:00
"""
dimensions : INTEGER
| dimensions ',' INTEGER
"""
2018-01-05 01:41:38 +00:00
if len(p) == 2:
2018-01-03 20:43:19 +00:00
p[0] = [p[1]]
else:
p[0] = p[1] + [p[3]]
def p_literal_value(p):
"""literal_value : INTEGER
| FLOATINGPOINT
| STRING
| CHARACTER
| BOOLEAN"""
p[0] = p[1]
def p_subroutine(p):
2018-01-07 01:36:27 +00:00
"""
2018-01-07 13:36:12 +00:00
subroutine : SUB NAME '(' sub_param_spec ')' RARROW '(' sub_result_spec ')' subroutine_body ENDL
2018-01-07 01:36:27 +00:00
"""
2018-01-07 13:36:12 +00:00
body = p[10]
if isinstance(body, Scope):
p[0] = Subroutine(name=p[2], param_spec=p[4], result_spec=p[8], scope=body, sourceref=_token_sref(p, 1))
elif isinstance(body, int):
p[0] = Subroutine(name=p[2], param_spec=p[4], result_spec=p[8], address=body, sourceref=_token_sref(p, 1))
else:
raise TypeError("subroutine_body", p.slice)
2018-01-03 20:43:19 +00:00
def p_sub_param_spec(p):
2018-01-07 01:36:27 +00:00
"""
sub_param_spec : empty
| sub_param_list
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_sub_param_list(p):
2018-01-07 01:36:27 +00:00
"""
sub_param_list : sub_param
| sub_param_list ',' sub_param
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
p[0] = [p[1]]
else:
2018-01-07 01:36:27 +00:00
p[0] = p[1] + [p[3]]
2018-01-03 20:43:19 +00:00
def p_sub_param(p):
2018-01-07 01:36:27 +00:00
"""
sub_param : LABEL REGISTER
| REGISTER
"""
2018-01-05 21:52:23 +00:00
if len(p) == 3:
p[0] = (p[1], p[2])
elif len(p) == 2:
p[0] = (None, p[1])
2018-01-03 20:43:19 +00:00
def p_sub_result_spec(p):
2018-01-07 01:36:27 +00:00
"""
sub_result_spec : empty
| '?'
| sub_result_list
"""
2018-01-03 20:43:19 +00:00
if p[1] == '?':
p[0] = ['A', 'X', 'Y'] # '?' means: all registers clobbered
2018-01-07 01:36:27 +00:00
else:
p[0] = p[1]
2018-01-03 20:43:19 +00:00
def p_sub_result_list(p):
2018-01-07 01:36:27 +00:00
"""
sub_result_list : sub_result_reg
| sub_result_list ',' sub_result_reg
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[3]]
def p_sub_result_reg(p):
2018-01-07 01:36:27 +00:00
"""
sub_result_reg : REGISTER
| CLOBBEREDREGISTER
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_subroutine_body(p):
2018-01-07 01:36:27 +00:00
"""
subroutine_body : scope
| IS INTEGER
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[2]
def p_statement(p):
2018-01-07 01:36:27 +00:00
"""
statement : assignment ENDL
| aug_assignment ENDL
| subroutine_call ENDL
| goto ENDL
| conditional_goto ENDL
| incrdecr ENDL
| return ENDL
2018-01-03 20:43:19 +00:00
"""
p[0] = p[1]
def p_incrdecr(p):
2018-01-07 01:36:27 +00:00
"""
incrdecr : assignment_target INCR
| assignment_target DECR
"""
2018-01-07 13:36:12 +00:00
p[0] = UnaryOp(operator=p[2], operand=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_call_subroutine(p):
2018-01-07 01:36:27 +00:00
"""
subroutine_call : calltarget preserveregs_opt '(' call_arguments_opt ')'
"""
2018-01-07 13:36:12 +00:00
p[0] = SubCall(target=p[1], preserve_regs=p[2], arguments=p[4], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_preserveregs_opt(p):
2018-01-07 01:36:27 +00:00
"""
preserveregs_opt : empty
| preserveregs
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_preserveregs(p):
2018-01-07 01:36:27 +00:00
"""
preserveregs : PRESERVEREGS
"""
2018-01-07 13:36:12 +00:00
p[0] = PreserveRegs(registers=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_call_arguments_opt(p):
2018-01-07 01:36:27 +00:00
"""
call_arguments_opt : empty
| call_arguments
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_call_arguments(p):
2018-01-07 01:36:27 +00:00
"""
call_arguments : call_argument
| call_arguments ',' call_argument
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[3]]
def p_call_argument(p):
2018-01-07 01:36:27 +00:00
"""
call_argument : expression
| register IS expression
| NAME IS expression
"""
2018-01-05 21:52:23 +00:00
if len(p) == 2:
2018-01-07 13:36:12 +00:00
p[0] = CallArgument(value=p[1], sourceref=_token_sref(p, 1))
2018-01-07 01:36:27 +00:00
elif len(p) == 4:
2018-01-07 13:36:12 +00:00
p[0] = CallArgument(name=p[1], value=p[3], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_return(p):
2018-01-07 01:36:27 +00:00
"""
return : RETURN
| RETURN expression
| RETURN expression ',' expression
| RETURN expression ',' expression ',' expression
"""
2018-01-05 21:52:23 +00:00
if len(p) == 2:
2018-01-07 13:36:12 +00:00
p[0] = Return(sourceref=_token_sref(p, 1))
2018-01-07 01:36:27 +00:00
elif len(p) == 3:
2018-01-07 13:36:12 +00:00
p[0] = Return(value_A=p[2], sourceref=_token_sref(p, 1))
2018-01-07 01:36:27 +00:00
elif len(p) == 5:
2018-01-07 13:36:12 +00:00
p[0] = Return(value_A=p[2], value_X=p[4], sourceref=_token_sref(p, 1))
2018-01-07 01:36:27 +00:00
elif len(p) == 7:
2018-01-07 13:36:12 +00:00
p[0] = Return(value_A=p[2], value_X=p[4], value_Y=p[6], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_register(p):
2018-01-07 01:36:27 +00:00
"""
register : REGISTER
"""
2018-01-07 13:36:12 +00:00
p[0] = Register(name=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
def p_goto(p):
2018-01-07 01:36:27 +00:00
"""
goto : GOTO calltarget
"""
2018-01-07 13:36:12 +00:00
p[0] = Goto(target=p[2], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
2018-01-05 21:52:23 +00:00
def p_conditional_goto_plain(p):
2018-01-07 01:36:27 +00:00
"""
conditional_goto : IF GOTO calltarget
"""
2018-01-07 13:36:12 +00:00
p[0] = Goto(target=p[3], if_stmt=p[1], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
2018-01-05 21:52:23 +00:00
def p_conditional_goto_expr(p):
2018-01-07 01:36:27 +00:00
"""
conditional_goto : IF expression GOTO calltarget
"""
2018-01-07 13:36:12 +00:00
p[0] = Goto(target=p[4], if_stmt=p[1], condition=p[2], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_calltarget(p):
2018-01-07 01:36:27 +00:00
"""
calltarget : symbolname
| INTEGER
| BITAND symbolname
| dereference
"""
2018-01-05 21:52:23 +00:00
if len(p) == 2:
2018-01-07 13:36:12 +00:00
p[0] = CallTarget(target=p[1], address_of=False, sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
elif len(p) == 3:
2018-01-07 13:36:12 +00:00
p[0] = CallTarget(target=p[2], address_of=True, sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_dereference(p):
2018-01-07 01:36:27 +00:00
"""
dereference : '[' dereference_operand ']'
"""
2018-01-07 13:36:12 +00:00
p[0] = Dereference(location=p[2][0], datatype=p[2][1], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_dereference_operand(p):
2018-01-07 01:36:27 +00:00
"""
dereference_operand : symbolname type_opt
| REGISTER type_opt
| INTEGER type_opt
"""
2018-01-05 21:52:23 +00:00
p[0] = (p[1], p[2])
2018-01-03 20:43:19 +00:00
def p_symbolname(p):
2018-01-07 01:36:27 +00:00
"""
symbolname : NAME
| DOTTEDNAME
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
def p_assignment(p):
2018-01-07 01:36:27 +00:00
"""
assignment : assignment_target IS expression
| assignment_target IS assignment
"""
2018-01-07 13:36:12 +00:00
p[0] = Assignment(left=p[1], right=p[3], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
2018-01-07 01:36:27 +00:00
def p_aug_assignment(p):
"""
aug_assignment : assignment_target AUGASSIGN expression
"""
2018-01-07 13:36:12 +00:00
p[0] = AugAssignment(left=p[1], operator=p[2], right=p[3], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
2018-01-05 21:52:23 +00:00
precedence = (
('left', '+', '-'),
('left', '*', '/'),
('right', 'UNARY_MINUS', 'BITINVERT', "UNARY_ADDRESSOF"),
('left', "LT", "GT", "LE", "GE", "EQUALS", "NOTEQUALS"),
('nonassoc', "COMMENT"),
)
def p_expression(p):
2018-01-07 01:36:27 +00:00
"""
expression : expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| expression LT expression
| expression GT expression
| expression LE expression
| expression GE expression
| expression EQUALS expression
| expression NOTEQUALS expression
"""
2018-01-07 13:36:12 +00:00
p[0] = Expression(left=p[1], operator=p[2], right=p[3], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_expression_uminus(p):
2018-01-07 01:36:27 +00:00
"""
expression : '-' expression %prec UNARY_MINUS
"""
2018-01-07 13:36:12 +00:00
p[0] = UnaryOp(operator=p[1], operand=p[2], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_expression_addressof(p):
2018-01-07 01:36:27 +00:00
"""
expression : BITAND symbolname %prec UNARY_ADDRESSOF
"""
2018-01-07 13:36:12 +00:00
p[0] = UnaryOp(operator=p[1], operand=p[2], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_unary_expression_bitinvert(p):
2018-01-07 01:36:27 +00:00
"""
expression : BITINVERT expression
"""
2018-01-07 13:36:12 +00:00
p[0] = UnaryOp(operator=p[1], operand=p[2], sourceref=_token_sref(p, 1))
2018-01-05 21:52:23 +00:00
def p_expression_group(p):
2018-01-07 01:36:27 +00:00
"""
expression : '(' expression ')'
"""
2018-01-05 21:52:23 +00:00
p[0] = p[2]
2018-01-07 13:36:12 +00:00
def p_expression_expr_value(p):
2018-01-07 01:36:27 +00:00
"""expression : expression_value"""
2018-01-05 21:52:23 +00:00
p[0] = p[1]
def p_expression_value(p):
2018-01-07 01:36:27 +00:00
"""
expression_value : literal_value
| symbolname
| register
| subroutine_call
| dereference
"""
p[0] = p[1]
def p_assignment_target(p):
"""
assignment_target : target_registers
| symbolname
| dereference
"""
2018-01-03 20:43:19 +00:00
p[0] = p[1]
2018-01-07 01:36:27 +00:00
def p_target_registers(p):
"""
target_registers : register
| target_registers ',' register
"""
2018-01-03 20:43:19 +00:00
if len(p) == 2:
2018-01-07 13:36:12 +00:00
p[0] = TargetRegisters(registers=[p[1]], sourceref=_token_sref(p, 1))
2018-01-03 20:43:19 +00:00
else:
2018-01-07 13:36:12 +00:00
p[1].add(p[3])
2018-01-07 01:36:27 +00:00
p[0] = p[1]
2018-01-03 20:43:19 +00:00
def p_empty(p):
"""empty :"""
pass
def p_error(p):
if p:
2018-01-05 21:52:23 +00:00
sref = SourceRef(p.lexer.source_filename, p.lineno, find_tok_column(p))
p.lexer.error_function("{}: before '{:.20s}' ({})", sref, str(p.value), repr(p))
2018-01-03 20:43:19 +00:00
else:
lexer.error_function("{}: at end of input", "@todo-filename3")
def _token_sref(p, token_idx):
""" Returns the coordinates for the YaccProduction object 'p' indexed
with 'token_idx'. The coordinate includes the 'lineno' and
'column'. Both follow the lex semantic, starting from 1.
"""
last_cr = p.lexer.lexdata.rfind('\n', 0, p.lexpos(token_idx))
if last_cr < 0:
last_cr = -1
column = (p.lexpos(token_idx) - last_cr)
2018-01-05 21:52:23 +00:00
return SourceRef(p.lexer.source_filename, p.lineno(token_idx), column)
2018-01-03 20:43:19 +00:00
2018-01-05 21:52:23 +00:00
class TokenFilter:
def __init__(self, lexer):
self.lexer = lexer
self.prev_was_EOL = False
assert "ENDL" in tokens
def token(self):
# make sure we only ever emit ONE "ENDL" token in sequence
if self.prev_was_EOL:
# skip all EOLS that might follow
while True:
tok = self.lexer.token()
if not tok or tok.type != "ENDL":
break
self.prev_was_EOL = False
else:
tok = self.lexer.token()
self.prev_was_EOL = tok and tok.type == "ENDL"
return tok
2018-01-03 20:43:19 +00:00
2018-01-05 21:52:23 +00:00
parser = yacc(write_tables=True)
2018-01-03 20:43:19 +00:00
if __name__ == "__main__":
import sys
2018-01-05 01:41:38 +00:00
file = sys.stdin # open(sys.argv[1], "rU")
2018-01-05 21:52:23 +00:00
lexer.source_filename = "derp"
tokenfilter = TokenFilter(lexer)
result = parser.parse(input=file.read(),
tokenfunc=tokenfilter.token) or Module(None, SourceRef(lexer.source_filename, 1, 1))
2018-01-07 03:39:21 +00:00
print("RESULT:")
result.print_tree()