Skip to content

Commit 082fdbb

Browse files
committed
Use pathlib in preprocessor.
1 parent cf80577 commit 082fdbb

26 files changed

+135
-123
lines changed

examples/wasm/wasmboy.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,37 @@
55
$ copy the file dist/core/index.untouched.wasm
66
"""
77

8-
import sys
9-
import logging
10-
import os
118
import argparse
9+
import logging
10+
import sys
11+
from pathlib import Path
1212

1313
import numpy as np
1414
import pygame
1515
from pygame.locals import QUIT
1616

17-
from ppci.wasm import Module, instantiate
1817
from ppci.utils import reporting
18+
from ppci.wasm import Module, instantiate
1919

20+
this_dir = Path(__file__).resolve().parent
21+
default_gb_path = this_dir / "cpu_instrs.gb"
22+
wasmboy_path = this_dir / "wasmboy.wasm"
2023
parser = argparse.ArgumentParser()
21-
parser.add_argument("--rom", default="cpu_instrs.gb")
24+
parser.add_argument("--rom", default=default_gb_path, type=Path)
2225
args = parser.parse_args()
2326
logging.basicConfig(level=logging.INFO)
27+
logger = logging.getLogger()
2428

25-
with open("wasmboy.wasm", "rb") as f:
29+
logger.info(f"Loading {wasmboy_path}")
30+
with wasmboy_path.open("rb") as f:
2631
wasm_module = Module(f)
2732

2833

2934
def log(a: int, b: int, c: int, d: int, e: int, f: int, g: int) -> None:
3035
print("Log:", a, b, c, d, e, f, g)
3136

3237

33-
this_dir = os.path.dirname(os.path.abspath(__file__))
34-
html_report = os.path.join(this_dir, "wasmboy_report.html")
38+
html_report = this_dir / "wasmboy_report.html"
3539
with reporting.html_reporter(html_report) as reporter:
3640
wasm_boy = instantiate(
3741
wasm_module,
@@ -44,11 +48,10 @@ def log(a: int, b: int, c: int, d: int, e: int, f: int, g: int) -> None:
4448
# https://github.com/torch2424/wasmBoy/wiki/%5BWIP%5D-Core-API
4549

4650
rom_filename = args.rom
47-
logging.info("Loading %s", rom_filename)
51+
logger.info("Loading %s", rom_filename)
4852
# Load in a game to CARTRIDGE_ROM_LOCATION
4953
rom_location = wasm_boy.exports.CARTRIDGE_ROM_LOCATION.read()
50-
with open(rom_filename, "rb") as f:
51-
rom_data = f.read()
54+
rom_data = rom_filename.read_bytes()
5255
rom_size = len(rom_data)
5356
wasm_boy.exports.memory[rom_location : rom_location + rom_size] = rom_data
5457

ppci/lang/c/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
"""C front end."""
22

3-
from .context import CContext
3+
from .api import c_to_ir, preprocess
44
from .builder import CBuilder, create_ast, parse_text, parse_type
5+
from .context import CContext
56
from .lexer import CLexer
7+
from .options import COptions
68
from .parser import CParser
7-
from .semantics import CSemantics
8-
from .synthesize import CSynthesizer
99
from .preprocessor import CPreProcessor
10-
from .utils import CAstPrinter, print_ast
1110
from .printer import CPrinter, render_ast
12-
from .options import COptions
11+
from .semantics import CSemantics
12+
from .synthesize import CSynthesizer
1313
from .token import CTokenPrinter
14-
from .api import preprocess, c_to_ir
15-
14+
from .utils import CAstPrinter, print_ast
1615

1716
__all__ = [
1817
"create_ast",

ppci/lang/c/api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import io
2+
3+
from ...utils.reporting import DummyReportGenerator
24
from .builder import CBuilder
5+
from .options import COptions
36
from .preprocessor import CPreProcessor
47
from .token import CTokenPrinter
5-
from .options import COptions
6-
from ...utils.reporting import DummyReportGenerator
78

89

910
def preprocess(f, output_file, coptions=None):

ppci/lang/c/builder.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import logging
21
import io
3-
from .options import COptions
2+
import logging
3+
4+
from .codegenerator import CCodeGenerator
45
from .context import CContext
6+
from .options import COptions
57
from .parser import CParser
6-
from .semantics import CSemantics
78
from .preprocessor import CPreProcessor, prepare_for_parsing
8-
from .codegenerator import CCodeGenerator
9+
from .semantics import CSemantics
910
from .utils import print_ast
1011

1112

ppci/lang/c/castxml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import xml.etree.ElementTree as ET
2+
23
from .nodes import nodes
34

45

ppci/lang/c/codegenerator.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
"""
99

1010
import logging
11+
1112
from ... import ir, irutils
12-
from ...common import CompilerError
1313
from ...binutils import debuginfo
14-
from .nodes import types, declarations, statements, expressions
15-
from .utils import required_padding
14+
from ...common import CompilerError
15+
from ...utils.bitfun import bits_to_bytes, value_to_bits
16+
from .eval import ConstantExpressionEvaluator
17+
from .nodes import declarations, expressions, statements, types
1618
from .nodes.types import BasicType
1719
from .scope import RootScope
18-
from ...utils.bitfun import value_to_bits, bits_to_bytes
19-
from .eval import ConstantExpressionEvaluator
20+
from .utils import required_padding
2021

2122

2223
class CCodeGenerator:

ppci/lang/c/context.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import logging
44
import struct
5-
from ...common import CompilerError
6-
from ...arch.arch_info import Endianness
5+
76
from ... import ir
7+
from ...arch.arch_info import Endianness
8+
from ...common import CompilerError
9+
from .eval import ConstantExpressionEvaluator
10+
from .nodes import expressions, types
811
from .nodes.types import BasicType
9-
from .nodes import types, expressions
1012
from .utils import required_padding
11-
from .eval import ConstantExpressionEvaluator
1213

1314

1415
class CContext:

ppci/lang/c/eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .nodes import types, expressions, declarations
1+
from .nodes import declarations, expressions, types
22

33

44
class ConstantExpressionEvaluator:

ppci/lang/c/init.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import abc
8+
89
from .nodes import expressions, types
910

1011

ppci/lang/c/lexer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""C Language lexer"""
22

3-
import logging
43
import io
4+
import logging
55

6-
from .token import CToken
76
from ..tools.handlexer import HandLexerBase
7+
from .token import CToken
88

99

1010
class SourceFile:

0 commit comments

Comments
 (0)