Skip to content

Commit

Permalink
fix all mypy issues we had
Browse files Browse the repository at this point in the history
  • Loading branch information
ibx34 committed Dec 15, 2024
1 parent e90688f commit f138379
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
31 changes: 26 additions & 5 deletions ast_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@
from typing import Generic, Callable, Self, Type, TypeVar, Union

from common import TT, PrimitiveTypes, bcolors, operators
from ast_exprs import AstirExpr, ShuntingYardAlgorithmResults, Identifier, Literal, PrimitiveType, Reference, AstirTuple, SymbolTable, Parameter, Symbol, Parenthesized, Lambda, Assignment, Application, check_is_allowed
from ast_exprs import (
AstirExpr,
ShuntingYardAlgorithmResults,
Identifier,
Literal,
PrimitiveType,
Reference,
AstirTuple,
SymbolTable,
Parameter,
Symbol,
Parenthesized,
Lambda,
Assignment,
Application,
check_is_allowed,
)


class Token:
Expand All @@ -22,6 +38,7 @@ def __init__(
def __repr__(self) -> str:
return f"{self.ty} ({self.val})"


def get_op(possible_op: Token | None) -> tuple[str, dict[str, int]] | None:
if (
possible_op is None
Expand All @@ -32,6 +49,7 @@ def get_op(possible_op: Token | None) -> tuple[str, dict[str, int]] | None:
op = operators[possible_op.ty.value]
return (possible_op.ty.value, op)


def is_valid_ident(c: str) -> bool:
return c.isalnum() or c == "_"

Expand Down Expand Up @@ -196,7 +214,7 @@ def identifier_check(c: str | None, rest: str) -> bool:
class Parser(Cursor):
def __init__(self, input: list[Token]) -> None:
super().__init__(input)
self.results: list['AstirExpr'] = []
self.results: list["AstirExpr"] = []
global_symbols = SymbolTable(0)
# TODO: we are waiting for typedef!
global_symbols.insert("int", PrimitiveType(PrimitiveTypes.INT))
Expand Down Expand Up @@ -313,7 +331,7 @@ def parse(self) -> AstirExpr | None:
# paren = self.parse()
# # raise Exception(f"Enum value(?): {c.val} -> {paren}")
# result = DataVariantWithInnerValue(Identifier(c.val), paren)
else:
else:
self.advance()
for_assignment = False
if (
Expand Down Expand Up @@ -352,7 +370,9 @@ def parse(self) -> AstirExpr | None:
elif len(the_between) == 1:
# Init Parenthesized with an expression (the_between[0])
# to do exactly what it says... for example (\ :: int ...)
result = Parenthesized(PrimitiveType(PrimitiveTypes.UNIT), the_between[0])
result = Parenthesized(
PrimitiveType(PrimitiveTypes.UNIT), the_between[0]
)
elif len(the_between) > 1 and has_comma:
# Handle tuples
result = AstirTuple(the_between)
Expand Down Expand Up @@ -455,7 +475,8 @@ def parse(self) -> AstirExpr | None:
possible_arg = self.parse()
if possible_arg is None:
raise Exception("Failed to parse")
if possible_arg.ty.ty != type_symbol.val.ty:

if possible_arg.ty != type_symbol.val.ty:
raise Exception(
f"{bcolors.FAIL}{bcolors.BOLD}Type mismatch{bcolors.ENDC}"
)
Expand Down
6 changes: 6 additions & 0 deletions ast_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class AstirExpr(ABC):
def __init__(self, ty: Union['PrimitiveTypes', 'AstirExpr']):
super().__init__()
self.ty = ty


def check_is_allowed(AstirExpr: AstirExpr | None) -> bool:
allowed = AstirExpr is not None or (
Expand Down Expand Up @@ -83,6 +84,9 @@ def __init__(

self.parameters=parameters

def __repr__(self):
return f"LambdaDef(Parameters={self.parameters})"


class Lambda(AstirExpr):
def __init__(self, parameters: SymbolTable, body: AstirExpr):
Expand All @@ -91,6 +95,8 @@ def __init__(self, parameters: SymbolTable, body: AstirExpr):
self.definition = lambda_def
self.body = body

def __repr__(self):
return f"Lambda(Def={self.definition}, Body={self.body})"

class Parenthesized(AstirExpr):
def __init__(self, ty: AstirExpr | PrimitiveTypes, inner: AstirExpr | None = None) -> None:
Expand Down

0 comments on commit f138379

Please sign in to comment.