2023-04-10 03:20:05 +01:00
|
|
|
from typing import Dict, List, Optional, cast
|
2023-04-09 02:48:26 +01:00
|
|
|
from position import Node
|
2023-04-10 03:20:05 +01:00
|
|
|
from utils import Err, Ok, Result
|
2023-04-09 02:48:26 +01:00
|
|
|
import parsed
|
|
|
|
|
|
|
|
class Value:
|
|
|
|
pass
|
|
|
|
|
2023-04-10 03:20:05 +01:00
|
|
|
class Unit(Value):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
|
|
|
|
2023-04-09 02:48:26 +01:00
|
|
|
class Int(Value):
|
|
|
|
def __init__(self, value: int) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class Char(Value):
|
|
|
|
def __init__(self, value: str) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class String(Value):
|
|
|
|
def __init__(self, value: str) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class Bool(Value):
|
|
|
|
def __init__(self, value: bool) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class EvalResult:
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ValueResult(EvalResult):
|
|
|
|
def __init__(self, value: Value) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class ReturnResult(EvalResult):
|
|
|
|
def __init__(self, value: Optional[Value]) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class BreakResult(EvalResult):
|
|
|
|
def __init__(self, value: Optional[Value]) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
class ContinueResult(EvalResult):
|
|
|
|
def __init__(self, value: Optional[Value]) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.value = value
|
|
|
|
|
2023-04-10 03:20:05 +01:00
|
|
|
class PanicResult(EvalResult):
|
|
|
|
def __init__(self, message: str) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.message = message
|
|
|
|
|
|
|
|
class SymbolTable:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.symbols: Dict[str, Value] = {}
|
|
|
|
|
|
|
|
def define(self, symbol_id: str, value: Value) -> Result[None, str]:
|
|
|
|
if symbol_id in self.symbols:
|
|
|
|
return Err("symbol already defined")
|
|
|
|
return Ok(None)
|
|
|
|
|
2023-04-09 02:48:26 +01:00
|
|
|
class Evaluator:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def evaluate(self, ast: List[Node[parsed.Expr]]) -> None:
|
|
|
|
for node in ast:
|
|
|
|
self.eval_expr(node)
|
|
|
|
|
|
|
|
def eval_expr(self, node: Node[parsed.Expr]) -> EvalResult:
|
2023-04-10 03:20:05 +01:00
|
|
|
if isinstance(node.value, parsed.ExprError):
|
|
|
|
return PanicResult(f"error: {cast(parsed.ExprError, node.value).message}, at {node.span}")
|
|
|
|
elif isinstance(node.value, parsed.Id):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Int):
|
|
|
|
return ValueResult(Int(cast(parsed.Int, node.value).value))
|
|
|
|
elif isinstance(node.value, parsed.Char):
|
|
|
|
return ValueResult(Char(cast(parsed.Char, node.value).value))
|
|
|
|
elif isinstance(node.value, parsed.String):
|
|
|
|
return ValueResult(String(cast(parsed.String, node.value).value))
|
|
|
|
elif isinstance(node.value, parsed.Bool):
|
|
|
|
return ValueResult(Bool(cast(parsed.Bool, node.value).value))
|
|
|
|
elif isinstance(node.value, parsed.Unit):
|
|
|
|
return ValueResult(Unit())
|
|
|
|
elif isinstance(node.value, parsed.Tuple):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Block):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Lambda):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.If):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Match):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Loop):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.While):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.For):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.StructMember):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.TupleMember):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Index):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Call):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Unary):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Binary):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Assign):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Let):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Function):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Return):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Break):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
2023-04-10 03:20:05 +01:00
|
|
|
elif isinstance(node.value, parsed.Continue):
|
2023-04-09 02:48:26 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
else:
|
2023-04-10 03:20:05 +01:00
|
|
|
raise NotImplementedError(str(node))
|
2023-04-09 02:48:26 +01:00
|
|
|
|