diff --git a/src/arch/z80/backend/common.py b/src/arch/z80/backend/common.py index 933b12b08..07a1dd01b 100644 --- a/src/arch/z80/backend/common.py +++ b/src/arch/z80/backend/common.py @@ -34,6 +34,7 @@ class DataType(StrEnum): i32 = "i32" f16 = "f16" f = "f" + str = "str" # Handy constants, to not having to type long names :-) @@ -46,7 +47,7 @@ class DataType(StrEnum): I32_t: Final[DataType] = DataType.i32 F16_t: Final[DataType] = DataType.f16 F_t: Final[DataType] = DataType.f - +STR_t: Final[DataType] = DataType.str # Internal data types definition, with its size in bytes, or -1 if it is variable (string) # Compound types are only arrays, and have the t @@ -297,32 +298,6 @@ def get_bytes_size(elements: list[str]) -> int: return len(get_bytes(elements)) -def to_bool(stype: DataType) -> list[str]: - """Returns the instruction sequence for converting the number given number (in the stack) - to boolean (just 0 (False) or non-zero (True)).""" - - if stype in (U8_t, I8_t): - return [] - - if stype in (U16_t, I16_t): - return [ - "ld a, h" "or l", - ] - - if stype in (U32_t, I32_t, F16_t): - return ["ld a, h" "or l" "or d", "or e,"] - - if stype == F_t: - return [ - "or b", - "or c", - "or d", - "or e", - ] - - raise NotImplementedError(f"type conversion from {stype} to bool is undefined") - - def normalize_boolean() -> list[str]: if OPTIONS.opt_strategy == OptimizationStrategy.Size: return [runtime_call(RuntimeLabel.NORMALIZE_BOOLEAN)] diff --git a/src/arch/z80/backend/generic.py b/src/arch/z80/backend/generic.py index 882e49739..691923d1d 100644 --- a/src/arch/z80/backend/generic.py +++ b/src/arch/z80/backend/generic.py @@ -24,7 +24,6 @@ get_bytes_size, new_ASMID, runtime_call, - to_bool, to_byte, to_fixed, to_float, @@ -365,8 +364,6 @@ def _cast(ins: Quad): output.extend(to_fixed(tA)) elif tB == "f": output.extend(to_float(tA)) - elif tB == "bool": - output.extend(to_bool(tA)) else: raise exception.GenericError("Internal error: invalid typecast from %s to %s" % (tA, tB)) diff --git a/src/arch/z80/visitor/translator_inst_visitor.py b/src/arch/z80/visitor/translator_inst_visitor.py index c853744af..04c100e11 100644 --- a/src/arch/z80/visitor/translator_inst_visitor.py +++ b/src/arch/z80/visitor/translator_inst_visitor.py @@ -2,7 +2,7 @@ from src.api.debug import __DEBUG__ from src.arch.interface.quad import Quad from src.arch.z80.backend import Backend -from src.arch.z80.backend.common import BOOL_t, F16_t, F_t, I8_t, I16_t, I32_t, U8_t, U16_t, U32_t +from src.arch.z80.backend.common import BOOL_t, F16_t, F_t, I8_t, I16_t, I32_t, STR_t, U8_t, U16_t, U32_t from src.ast import NodeVisitor from src.symbols import sym as symbols @@ -30,7 +30,7 @@ def TSUFFIX(type_: TYPE | symbols.TYPEREF | symbols.BASICTYPE) -> str: TYPE.ulong: U32_t, TYPE.fixed: F16_t, TYPE.float: F_t, - TYPE.string: "str", + TYPE.string: STR_t, TYPE.boolean: BOOL_t, } diff --git a/src/zxbpp/base_pplex.py b/src/zxbpp/base_pplex.py index 274f474a0..678ed3376 100644 --- a/src/zxbpp/base_pplex.py +++ b/src/zxbpp/base_pplex.py @@ -9,7 +9,7 @@ import os import sys -from collections.abc import Iterable +from collections.abc import Callable, Iterable from dataclasses import dataclass from enum import Enum, unique @@ -88,6 +88,10 @@ def __init__( for macro_name, macro_func in self.builtin_macros.items(): self.defines_table[macro_name] = BuiltinMacro(macro_name=macro_name, func=macro_func) + def set_macro(self, macro_name: str, func: Callable[[str], str]) -> None: + assert self.defines_table is not None + self.defines_table[macro_name] = func + def put_current_line(self, prefix: str = "", suffix: str = "") -> str: """Returns line and file for include / end of include sequences.""" assert self.lex is not None diff --git a/src/zxbpp/prepro/builtinmacro.py b/src/zxbpp/prepro/builtinmacro.py index 077de2c0d..b6a44ccbd 100644 --- a/src/zxbpp/prepro/builtinmacro.py +++ b/src/zxbpp/prepro/builtinmacro.py @@ -1,3 +1,5 @@ +from collections.abc import Callable + from src.zxbpp import prepro from .id_ import ID @@ -7,12 +9,12 @@ class BuiltinMacro(ID): """A call to a builtin macro like __FILE__ or __LINE__ Every time the macro() is called, the macro returns - it value. + its value. """ - def __init__(self, macro_name: str, func): + def __init__(self, macro_name: str, func: Callable[[MacroCall | None], str]): super().__init__(fname="", lineno=0, id_=macro_name) self.func = func - def __call__(self, symbolTable: "prepro.DefinesTable" = None, macro: MacroCall = None) -> str: + def __call__(self, symbolTable: prepro.DefinesTable | None = None, macro: MacroCall | None = None) -> str: return self.func(macro) diff --git a/src/zxbpp/zxbpp.py b/src/zxbpp/zxbpp.py index a23ffdc42..ff03b11b8 100755 --- a/src/zxbpp/zxbpp.py +++ b/src/zxbpp/zxbpp.py @@ -112,8 +112,7 @@ def reset_id_table(): ID_TABLE.define(name, value=val, lineno=0) for macro_name, macro_func in LEXER.builtin_macros.items(): - # FIXME - LEXER.defines_table[macro_name] = BuiltinMacro(macro_name=macro_name, func=macro_func) # type: ignore[index] + LEXER.set_macro(macro_name, BuiltinMacro(macro_name=macro_name, func=macro_func)) def init():