57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
import sys
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from src.consts import VERSION
|
|
from src.interpreter.error import PebbleRuntimeError
|
|
from src.core.position import Position
|
|
from src.token.token import Token, TokenType
|
|
|
|
|
|
class Pebble:
|
|
had_error: bool = False
|
|
had_runtime_error: bool = False
|
|
|
|
@staticmethod
|
|
def version() -> str:
|
|
major, minor, patch = VERSION
|
|
return f"{major}.{minor}.{patch}"
|
|
|
|
@staticmethod
|
|
def run_file(path: str | Path):
|
|
source: str = ""
|
|
with open(path, "r") as f:
|
|
source = f.read()
|
|
Pebble.run(source, str(path))
|
|
|
|
@staticmethod
|
|
def run(source: str, path: Optional[str] = None):
|
|
from src.runner import Runner
|
|
runner: Runner = Runner(source, path)
|
|
runner.run()
|
|
if Pebble.had_error:
|
|
exit(65)
|
|
if Pebble.had_runtime_error:
|
|
exit(70)
|
|
|
|
@staticmethod
|
|
def report(position: Position, where: str, msg: str):
|
|
print(f"({position}) Error{where}: {msg}", file=sys.stderr)
|
|
Pebble.had_error = True
|
|
|
|
@classmethod
|
|
def error(cls, position: Position, msg: str):
|
|
cls.report(position, "", msg)
|
|
|
|
@classmethod
|
|
def token_error(cls, token: Token, msg: str):
|
|
if token.type == TokenType.EOF:
|
|
cls.report(token.position, " at end", msg)
|
|
else:
|
|
cls.report(token.position, f" at '{token.lexeme}'", msg)
|
|
|
|
@classmethod
|
|
def runtime_error(cls, error: PebbleRuntimeError):
|
|
print(f"{error}\n({error.token.position})", file=sys.stderr)
|
|
Pebble.had_runtime_error = True
|