aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_asm/asm_pass1.py
blob: d192b5e61134c606ba07fc76f7a7ce2e132f98bc (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
import importlib
import sys
import os.path
from dataclasses import dataclass
from .asm_ops import *
from .asm_eval import *
from .asm_pass0 import *
from .tokenizer import tokenize

@dataclass
class Result1Line:
  line_num: int
  src_file: str
  parts: list[str]

@dataclass
class Result1:
  lines: list[Result1Line]
  operations: OpExpansionDict

def pass_1(result0: Result0) -> Result1:
  result_lines: list[Result1Line] = []
  operations: OpExpansionDict = expansions.copy()

  for line in result0.lines:
    keyword, *args = tokenize(line.line)
    match keyword:
      case "@use":
        module_name, ops = args[0].split(":")
        ops_split = ops.split(",")
        sys.path.append(os.path.dirname(line.src_file))
        module = importlib.import_module(module_name)
        mod_expansions: OpExpansionDict = module.expansions
        for op in mod_expansions:
          expansion = mod_expansions[op]
          if ops == "*" or op in ops_split:
            operations[op] = expansion
      case "%%data_segment":
        # drop the data_segment sentinel
        continue
      case _:
        result_lines.append(Result1Line(
          src_file=line.src_file,
          line_num=line.line_num,
          parts=[keyword, *args]
        ))

  return Result1(
    operations=operations,
    lines=result_lines
  )