Compare commits
2 Commits
4118f95753
...
e2efbf693e
| Author | SHA1 | Date | |
|---|---|---|---|
|
e2efbf693e
|
|||
|
68f83ab6cf
|
@@ -15,6 +15,7 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
BUILTIN_SUBTYPES: dict[str, set[str]] = {
|
||||
"object": {"float", "list", "dict"},
|
||||
"float": {"int"},
|
||||
"int": {"bool"},
|
||||
}
|
||||
|
||||
@@ -224,9 +224,9 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
||||
def visit_binary_expr(self, expr: m.BinaryExpr) -> Type:
|
||||
method: Optional[str] = MIDAS_BINARY_METHODS.get(expr.operator.type)
|
||||
if method is None:
|
||||
self.logger.warning(f"Unsupported operator {expr.operator}")
|
||||
self.logger.warning(f"Unsupported operator {expr.operator.lexeme}")
|
||||
self.reporter.warning(
|
||||
expr.location, f"Unsupported operator {expr.operator}"
|
||||
expr.location, f"Unsupported operator {expr.operator.lexeme}"
|
||||
)
|
||||
return UnknownType()
|
||||
|
||||
@@ -257,9 +257,9 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
||||
def visit_unary_expr(self, expr: m.UnaryExpr) -> Type:
|
||||
method: Optional[str] = MIDAS_UNARY_METHODS.get(expr.operator.type)
|
||||
if method is None:
|
||||
self.logger.warning(f"Unsupported operator {expr.operator}")
|
||||
self.logger.warning(f"Unsupported operator {expr.operator.lexeme}")
|
||||
self.reporter.warning(
|
||||
expr.location, f"Unsupported operator {expr.operator}"
|
||||
expr.location, f"Unsupported operator {expr.operator.lexeme}"
|
||||
)
|
||||
return UnknownType()
|
||||
|
||||
@@ -303,7 +303,7 @@ class MidasTyper(m.Stmt.Visitor[None], m.Expr.Visitor[Type], m.Type.Visitor[Type
|
||||
member: Optional[Type] = self.types.lookup_member(object, expr.name.lexeme)
|
||||
if member is None:
|
||||
self.reporter.error(
|
||||
expr.location, f"Unknown member '{expr.name}' of {object}"
|
||||
expr.location, f"Unknown member '{expr.name.lexeme}' of {object}"
|
||||
)
|
||||
return UnknownType()
|
||||
return member
|
||||
|
||||
@@ -21,7 +21,7 @@ PY_OPERATOR_METHODS: dict[Type[ast.operator], str] = {
|
||||
|
||||
PY_COMPARATOR_METHODS: dict[Type[ast.cmpop], str] = {
|
||||
ast.Eq: "__eq__",
|
||||
# ast.NotEq: "__noteq__",
|
||||
ast.NotEq: "__eq__",
|
||||
ast.Lt: "__lt__",
|
||||
ast.LtE: "__le__",
|
||||
ast.Gt: "__gt__",
|
||||
@@ -52,7 +52,7 @@ MIDAS_BINARY_METHODS: dict[TokenType, str] = {
|
||||
# ast.BitAnd: "__and__",
|
||||
# ast.FloorDiv: "__floordiv__",
|
||||
TokenType.EQUAL_EQUAL: "__eq__",
|
||||
# ast.NotEq: "__noteq__",
|
||||
TokenType.BANG_EQUAL: "__eq__",
|
||||
TokenType.LESS: "__lt__",
|
||||
TokenType.LESS_EQUAL: "__le__",
|
||||
TokenType.GREATER: "__gt__",
|
||||
|
||||
@@ -69,6 +69,8 @@ class MidasLexer(Lexer):
|
||||
):
|
||||
self.advance()
|
||||
self.add_token(TokenType.WHITESPACE)
|
||||
case '"' | "'":
|
||||
self.scan_string(char)
|
||||
case _:
|
||||
if char.isdigit():
|
||||
self.scan_number()
|
||||
@@ -78,6 +80,17 @@ class MidasLexer(Lexer):
|
||||
self.error("Unexpected character")
|
||||
return None
|
||||
|
||||
def scan_string(self, opening: str):
|
||||
while self.peek() != opening and not self.is_at_end():
|
||||
self.advance()
|
||||
|
||||
if self.is_at_end():
|
||||
self.error("Unterminated string")
|
||||
|
||||
self.advance()
|
||||
value: str = self.source[self.start + 1 : self.idx - 1]
|
||||
self.add_token(TokenType.STRING, value)
|
||||
|
||||
def scan_number(self):
|
||||
"""Scan the rest of number and add it as a token
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ class TokenType(Enum):
|
||||
TRUE = auto()
|
||||
FALSE = auto()
|
||||
NONE = auto()
|
||||
STRING = auto()
|
||||
|
||||
# Keywords
|
||||
TYPE = auto()
|
||||
|
||||
@@ -418,6 +418,9 @@ class MidasParser(Parser):
|
||||
if self.match(TokenType.NUMBER):
|
||||
return LiteralExpr(location=token.get_location(), value=token.value)
|
||||
|
||||
if self.match(TokenType.STRING):
|
||||
return LiteralExpr(location=token.get_location(), value=token.value)
|
||||
|
||||
if self.match_identifier():
|
||||
return VariableExpr(location=token.get_location(), name=token)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user