sh-dis/test_parser.py
Zack Buhman ea3c389944 add non-FPU/UBC/MMU/cache SH4 instructions
Previously, ast transformations were performed informally as ad-hoc
modifications to the generated C source code. In this commit, the
same transformations are performed by rewriting the ast prior to code
generation time.

The most significant new transformer is transform_assignment_list.
This transforms assignments such as:

  a, b, c = f(b, c, d)

To:

  a = f(&b, &c, d)

The former syntax is used frequently in the manual's description of
FPU-related instructions.
2024-04-22 21:34:43 +08:00

43 lines
830 B
Python

from pprint import pprint
from lexer import Lexer
from parser import Parser
from ast_to_c_source import generate
sources = [
"op1 ← FloatValue64(DR2n);",
"UTLB[MMUCR.URC].ASID ← PTEH.ASID;",
"IF (n_field = m_field)"
"{"
"m_address ← m_address + 4;"
"n_address ← n_address + 4;"
"}",
"""
IF (op1 ≥ 0)
op2 ← op2 << shift_amount;
ELSE IF (shift_amount ≠ 0)
op2 ← op2 >> (32 - shift_amount);
"""
]
def all_tokens(lexer):
while True:
try:
token = lexer.next_token()
except IndexError:
break
yield token
def dump(source):
lexer = Lexer(source)
tokens = list(all_tokens(lexer))
parser = Parser(tokens)
root = parser.statement()
pprint(root)
source = "c ← foobar(b, c, d);"
dump(source);