aboutsummaryrefslogtreecommitdiffstats
path: root/atk16-asm/asm_pass4.py
blob: 2fff870c165e78eb2eae1a40a40bebca15b26a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from dataclasses import dataclass
from asm_ops import *
from asm_eval import *
from asm_pass3 import *

@dataclass
class Result4Line:
  line_num: int
  src_file: str
  address: int
  word: int
  text: str
  original_text: str

@dataclass
class Result4:
  lines: list[Result4Line]
  operations: OpExpansionDict
  symbols: dict[str, int]

def pass_4(result3: Result3) -> Result4:
  result_lines: list[Result4Line] = []

  for line in result3.lines:
    keyword, *args = line.parts
    meta = Meta(
      address=line.address,
    )

    text = " ".join([keyword, *args])
    original_text = " ".join(line.original_parts)

    if keyword in operations:
      fn = operations[keyword]
      word = fn(meta, result3.symbols, *args)
      result_lines.append(Result4Line(
        line_num=line.line_num,
        src_file=line.src_file,
        address=line.address,
        word=word,
        text=text,
        original_text=original_text,
      ))
    else:
      try:
        result_lines.append(Result4Line(
          line_num=line.line_num,
          src_file=line.src_file,
          address=line.address,
          word=eval_expr(result3.symbols, keyword),
          text=text,
          original_text=original_text,
        ))
      except:
        raise Exception(f"Invalid assembly at {line.src_file}:{line.line_num + 1}\n\n{line}")

  return Result4(
    operations=result3.operations,
    symbols=result3.symbols,
    lines=result_lines,
  )