Python module polishing
This commit is contained in:
parent
2ac3c1ac26
commit
0b37a4210f
|
|
@ -0,0 +1,140 @@
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from enum import Enum, auto
|
||||||
|
from typing import Callable, Dict, List, Optional
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
...
|
||||||
|
|
||||||
|
SLS_NAME: str
|
||||||
|
SLS_VERSION: str
|
||||||
|
SLS_COMMIT: str
|
||||||
|
INTERPRETER_NAME: str
|
||||||
|
INTERPRETER_VER: int
|
||||||
|
MODULE_TIMESTAMP: str
|
||||||
|
|
||||||
|
|
||||||
|
class LexerInfo:
|
||||||
|
filename: str
|
||||||
|
source_code: str
|
||||||
|
pos: int
|
||||||
|
column: int
|
||||||
|
line: int
|
||||||
|
|
||||||
|
def __init__(self, filename: str = "", source_code: str = ""): ...
|
||||||
|
|
||||||
|
|
||||||
|
class TokenType(Enum):
|
||||||
|
EOF = auto()
|
||||||
|
IDENTIFIER = auto()
|
||||||
|
INTEGER = auto()
|
||||||
|
FLOAT = auto()
|
||||||
|
DOUBLE = auto()
|
||||||
|
CHARACTER = auto()
|
||||||
|
STRING = auto()
|
||||||
|
BOOLEAN = auto()
|
||||||
|
ARRAY = auto()
|
||||||
|
TOKEN_STRING = auto()
|
||||||
|
TYPE_TUPLE = auto()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Identifier:
|
||||||
|
name: str
|
||||||
|
is_literal: bool
|
||||||
|
|
||||||
|
|
||||||
|
class IntegerBuiltInType(Enum):
|
||||||
|
I64 = auto()
|
||||||
|
I32 = auto()
|
||||||
|
I16 = auto()
|
||||||
|
I8 = auto()
|
||||||
|
U64 = auto()
|
||||||
|
U32 = auto()
|
||||||
|
U16 = auto()
|
||||||
|
U8 = auto()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class IntegerLiteral:
|
||||||
|
value: int
|
||||||
|
type: IntegerBuiltInType
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TokenString:
|
||||||
|
tokens: List["Token"] = field(default_factory=list)
|
||||||
|
|
||||||
|
def deep_copy(self) -> TokenString: ...
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Token:
|
||||||
|
type: TokenType
|
||||||
|
identifier: Optional[Identifier] = None
|
||||||
|
integer_literal: Optional[IntegerLiteral] = None
|
||||||
|
float_literal: Optional[float] = None
|
||||||
|
double_literal: Optional[float] = None
|
||||||
|
character_literal: Optional[int] = None
|
||||||
|
string_literal: Optional[str] = None
|
||||||
|
boolean_literal: Optional[bool] = None
|
||||||
|
token_string: Optional[TokenString] = None
|
||||||
|
|
||||||
|
|
||||||
|
def lexical_analysis(lexer_info: LexerInfo) -> List[Token]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class StackType(Enum):
|
||||||
|
IDENTIFIER = auto()
|
||||||
|
I64 = auto()
|
||||||
|
I32 = auto()
|
||||||
|
I16 = auto()
|
||||||
|
I8 = auto()
|
||||||
|
U64 = auto()
|
||||||
|
U32 = auto()
|
||||||
|
U16 = auto()
|
||||||
|
U8 = auto()
|
||||||
|
FLOAT = auto()
|
||||||
|
DOUBLE = auto()
|
||||||
|
CHARACTER = auto()
|
||||||
|
BOOLEAN = auto()
|
||||||
|
TOKEN_STRING = auto()
|
||||||
|
CALLABLE = auto()
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class StackEntry:
|
||||||
|
type: StackType
|
||||||
|
value: object
|
||||||
|
|
||||||
|
|
||||||
|
class FunctionType(Enum):
|
||||||
|
TOKEN_STRING = auto()
|
||||||
|
BUILTIN = auto()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FunctionItem:
|
||||||
|
type: FunctionType
|
||||||
|
token_string: Optional[TokenString] = None
|
||||||
|
builtin: Optional[Callable[["InterpreterState"], bool]] = None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_token_string(cls, ts: TokenString) -> "FunctionItem": ...
|
||||||
|
@classmethod
|
||||||
|
def from_builtin(cls, fn: Callable[["InterpreterState"], bool]) -> "FunctionItem": ...
|
||||||
|
|
||||||
|
|
||||||
|
class InterpreterState:
|
||||||
|
stack: List[StackEntry]
|
||||||
|
functions: Dict[str, FunctionItem]
|
||||||
|
|
||||||
|
def __init__(self): ...
|
||||||
|
def push(self, entry: StackEntry) -> None: ...
|
||||||
|
def pop(self) -> Optional[StackEntry]: ...
|
||||||
|
def top(self) -> Optional[StackEntry]: ...
|
||||||
|
def add_function(self, name: str, item: FunctionItem) -> None: ...
|
||||||
|
def get_function(self, name: str) -> Optional[FunctionItem]: ...
|
||||||
|
def push_token(self, token: Token) -> bool: ...
|
||||||
|
def execute_func(self, key: str) -> bool: ...
|
||||||
|
def execute_token_string(self, token_string: TokenString) -> bool: ...
|
||||||
|
def execute(self, token: Token) -> bool: ...
|
||||||
|
|
@ -1,6 +1,52 @@
|
||||||
"""sls — Python SLS reimplementation
|
from .__main__ import main
|
||||||
|
|
||||||
Expose package version and small helpers here.
|
from .meta import (
|
||||||
"""
|
SLS_NAME,
|
||||||
|
SLS_VERSION,
|
||||||
|
SLS_COMMIT,
|
||||||
|
INTERPRETER_NAME,
|
||||||
|
INTERPRETER_VER,
|
||||||
|
MODULE_TIMESTAMP,
|
||||||
|
)
|
||||||
|
|
||||||
__all__ = ["main"]
|
from .lexer import (
|
||||||
|
LexerInfo,
|
||||||
|
TokenType,
|
||||||
|
Identifier,
|
||||||
|
IntegerBuiltInType,
|
||||||
|
IntegerLiteral,
|
||||||
|
TokenString,
|
||||||
|
Token,
|
||||||
|
lexical_analysis,
|
||||||
|
)
|
||||||
|
|
||||||
|
from .interpreter import (
|
||||||
|
StackType,
|
||||||
|
StackEntry,
|
||||||
|
FunctionType,
|
||||||
|
FunctionItem,
|
||||||
|
InterpreterState,
|
||||||
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"main",
|
||||||
|
"SLS_NAME",
|
||||||
|
"SLS_VERSION",
|
||||||
|
"SLS_COMMIT",
|
||||||
|
"INTERPRETER_NAME",
|
||||||
|
"INTERPRETER_VER",
|
||||||
|
"MODULE_TIMESTAMP",
|
||||||
|
"LexerInfo",
|
||||||
|
"TokenType",
|
||||||
|
"Identifier",
|
||||||
|
"IntegerBuiltInType",
|
||||||
|
"IntegerLiteral",
|
||||||
|
"TokenString",
|
||||||
|
"Token",
|
||||||
|
"lexical_analysis",
|
||||||
|
"StackType",
|
||||||
|
"StackEntry",
|
||||||
|
"FunctionType",
|
||||||
|
"FunctionItem",
|
||||||
|
"InterpreterState",
|
||||||
|
]
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,15 @@ except ImportError:
|
||||||
|
|
||||||
|
|
||||||
SLS_NAME = "SLS_PYTHON"
|
SLS_NAME = "SLS_PYTHON"
|
||||||
|
SLS_VERSION = version
|
||||||
|
SLS_COMMIT = commit
|
||||||
|
|
||||||
# Runtime interpreter info (Python equivalent of compiler)
|
# Runtime interpreter info (Python equivalent of compiler)
|
||||||
_impl = sys.implementation
|
_impl = sys.implementation
|
||||||
INTERPRETER_NAME = _impl.name.capitalize()
|
INTERPRETER_NAME = _impl.name.capitalize()
|
||||||
INTERPRETER_VER = _impl.version.major
|
INTERPRETER_VER = _impl.version.major
|
||||||
|
MODULE_TIMESTAMP = timestamp
|
||||||
|
|
||||||
def print_version() -> None:
|
def print_version() -> None:
|
||||||
print(f"YREA SLS ({SLS_NAME}) {version} ({commit})")
|
print(f"YREA SLS ({SLS_NAME}) {SLS_VERSION} ({SLS_COMMIT})")
|
||||||
print(f"Running on {INTERPRETER_NAME} {INTERPRETER_VER} at {timestamp}")
|
print(f"Running on {INTERPRETER_NAME} {INTERPRETER_VER} at {MODULE_TIMESTAMP}")
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,9 @@ def print_top_of_stack(interpreter: InterpreterState) -> None:
|
||||||
print(f"#0: {item.value}:{t}")
|
print(f"#0: {item.value}:{t}")
|
||||||
elif t in {StackType.U64, StackType.U32, StackType.U16, StackType.U8}:
|
elif t in {StackType.U64, StackType.U32, StackType.U16, StackType.U8}:
|
||||||
print(f"#0: {item.value}:{t}")
|
print(f"#0: {item.value}:{t}")
|
||||||
elif t == StackType.F32:
|
elif t == StackType.FLOAT:
|
||||||
print(f"#0: {item.value}:f32")
|
print(f"#0: {item.value}:f32")
|
||||||
elif t == StackType.F64:
|
elif t == StackType.DOUBLE:
|
||||||
print(f"#0: {item.value}")
|
print(f"#0: {item.value}")
|
||||||
elif t == StackType.CHARACTER:
|
elif t == StackType.CHARACTER:
|
||||||
print(f"#0: {item.value}")
|
print(f"#0: {item.value}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue