mirror of
https://github.com/irmen/prog8.git
synced 2024-11-26 11:49:22 +00:00
plyparsing more or less done
This commit is contained in:
parent
16b95cf3e9
commit
18526469ed
@ -6,7 +6,7 @@ Written by Irmen de Jong (irmen@razorvine.net)
|
||||
License: GNU GPL 3.0, see LICENSE
|
||||
"""
|
||||
|
||||
from typing import List, Any
|
||||
from typing import List, Set
|
||||
from ply.yacc import yacc
|
||||
from .symbols import SourceRef, AstNode
|
||||
from .lexer import tokens, lexer, find_tok_column # get the lexer tokens. required.
|
||||
@ -89,10 +89,13 @@ class Return(AstNode):
|
||||
|
||||
|
||||
class TargetRegisters(AstNode):
|
||||
def __init__(self, registers, sourceref: SourceRef) -> None:
|
||||
def __init__(self, registers: List[str], sourceref: SourceRef) -> None:
|
||||
super().__init__(sourceref)
|
||||
self.registers = registers
|
||||
|
||||
def add_register(self, register) -> None:
|
||||
self.registers.append(register)
|
||||
|
||||
|
||||
class InlineAssembly(AstNode):
|
||||
def __init__(self, assembly: str, sourceref: SourceRef) -> None:
|
||||
@ -754,5 +757,5 @@ if __name__ == "__main__":
|
||||
tokenfilter = TokenFilter(lexer)
|
||||
result = parser.parse(input=file.read(),
|
||||
tokenfunc=tokenfilter.token) or Module(None, SourceRef(lexer.source_filename, 1, 1))
|
||||
# print("RESULT:")
|
||||
# print(str(result))
|
||||
print("RESULT:")
|
||||
result.print_tree()
|
||||
|
@ -700,37 +700,28 @@ class AstNode:
|
||||
def lineref(self) -> str:
|
||||
return "src l. " + str(self.sourceref.line)
|
||||
|
||||
def __str__(self) -> str:
|
||||
def tostr(node: Any, level: int) -> str:
|
||||
indent = " "
|
||||
clsname = node.__class__.__name__
|
||||
attrs = []
|
||||
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:
|
||||
nvars = vars(node)
|
||||
variables = vars(node).items()
|
||||
except TypeError:
|
||||
if type(node) is str:
|
||||
sv = repr(node)
|
||||
if len(sv) > 20:
|
||||
sv = sv[:20] + "...'"
|
||||
return sv
|
||||
return str(node)
|
||||
for name, value in nvars.items():
|
||||
if name == "sourceref":
|
||||
continue
|
||||
elif type(value) in (str, int, float, bool, type(None)):
|
||||
attrs.append((name, tostr(value, level+1)))
|
||||
elif type(value) is list:
|
||||
strvalue = "["
|
||||
strvalue += (",\n" + indent*(level+1)).join(tostr(v, level+1) for v in value) + "\n" + (1+level)*indent + "]"
|
||||
attrs.append((name, strvalue))
|
||||
elif type(value) is not list and type(value) not in (str, int, float, bool, type(None)):
|
||||
attrs.append((name, tostr(value, level+2)))
|
||||
else:
|
||||
raise TypeError("WEIRD TYPE", type(value))
|
||||
attrstr = ("\n" + indent*(1+level)).join("{} = {}".format(name, sv) for name, sv in attrs)
|
||||
result = "\n" + indent * level + "<{0:s} l={1:d} c={2:d}".format(clsname, node.sourceref.line, node.sourceref.column)
|
||||
return result + "{} |end {} l={:d}|>".format(attrstr, clsname, node.sourceref.line)
|
||||
return tostr(self, 0)
|
||||
variables = {}
|
||||
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)
|
||||
|
||||
|
||||
class Block(AstNode):
|
||||
|
Loading…
Reference in New Issue
Block a user