Compare commits
3
Commits
893e1ba190
...
f2c61d24e2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2c61d24e2
|
||
|
|
112ed0e816
|
||
|
|
7eb1e13b70
|
@@ -173,6 +173,8 @@ class PythonHighlighter(
|
||||
|
||||
def visit_set_expr(self, expr: p.SetExpr) -> None: ...
|
||||
|
||||
def visit_cast_expr(self, expr: p.CastExpr) -> None: ...
|
||||
|
||||
|
||||
class MidasHighlighter(Highlighter, m.Stmt.Visitor[None], m.Expr.Visitor[None]):
|
||||
EXTRA_CSS_PATH: Optional[Path] = Path(__file__).parent / "hl_midas.css"
|
||||
|
||||
@@ -64,6 +64,9 @@ class PythonParser:
|
||||
case ast.Assign():
|
||||
return self.parse_assign(node)
|
||||
|
||||
case ast.AugAssign():
|
||||
return self.parse_aug_assign(node)
|
||||
|
||||
case ast.FunctionDef():
|
||||
return self.parse_function(node)
|
||||
|
||||
@@ -129,6 +132,21 @@ class PythonParser:
|
||||
value=value,
|
||||
)
|
||||
|
||||
def parse_aug_assign(self, node: ast.AugAssign) -> AssignStmt:
|
||||
location: Location = Location.from_ast(node)
|
||||
target: Expr = self.parse_expr(node.target)
|
||||
value: Expr = self.parse_expr(node.value)
|
||||
return AssignStmt(
|
||||
location=location,
|
||||
targets=[target],
|
||||
value=BinaryExpr(
|
||||
location=location,
|
||||
left=target,
|
||||
operator=node.op,
|
||||
right=value,
|
||||
),
|
||||
)
|
||||
|
||||
def parse_function(self, node: ast.FunctionDef) -> Function:
|
||||
loc: Location = Location.from_ast(node)
|
||||
match node:
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from midas.checker.types import BaseType, Type
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from midas.resolver.midas import MidasResolver
|
||||
|
||||
|
||||
def basic_op(ctx: MidasResolver, type: Type, op: str):
|
||||
ctx.define_operation(
|
||||
left=type,
|
||||
operator=op,
|
||||
right=type,
|
||||
result=type,
|
||||
)
|
||||
|
||||
|
||||
def define_builtins(ctx: MidasResolver):
|
||||
"""Define builtin types and operations"""
|
||||
bool = ctx.define_type("bool", BaseType(name="bool"))
|
||||
int = ctx.define_type("int", BaseType(name="int"))
|
||||
float = ctx.define_type("float", BaseType(name="float"))
|
||||
str = ctx.define_type("str", BaseType(name="str"))
|
||||
|
||||
basic_op(ctx, int, "__add__")
|
||||
basic_op(ctx, int, "__sub__")
|
||||
basic_op(ctx, int, "__mul__")
|
||||
basic_op(ctx, int, "__pow__")
|
||||
basic_op(ctx, int, "__mod__")
|
||||
basic_op(ctx, int, "__and__")
|
||||
basic_op(ctx, int, "__or__")
|
||||
basic_op(ctx, int, "__xor__")
|
||||
basic_op(ctx, float, "__add__")
|
||||
basic_op(ctx, float, "__sub__")
|
||||
basic_op(ctx, float, "__mul__")
|
||||
basic_op(ctx, float, "__truediv__")
|
||||
basic_op(ctx, str, "__add__")
|
||||
+2
-14
@@ -2,6 +2,7 @@ from typing import Optional
|
||||
|
||||
import midas.ast.midas as m
|
||||
from midas.checker.types import BaseType, SimpleType, Type
|
||||
from midas.resolver.builtin import define_builtins
|
||||
|
||||
|
||||
class MidasResolver(m.Stmt.Visitor[None], m.Expr.Visitor[Type]):
|
||||
@@ -11,7 +12,7 @@ class MidasResolver(m.Stmt.Visitor[None], m.Expr.Visitor[Type]):
|
||||
self._types: dict[str, Type] = {}
|
||||
self._operations: dict[tuple[Type, str, Type], Type] = {}
|
||||
|
||||
self._define_builtin()
|
||||
define_builtins(self)
|
||||
|
||||
def get_type(self, name: str) -> Type:
|
||||
"""Get a type from its name
|
||||
@@ -47,19 +48,6 @@ class MidasResolver(m.Stmt.Visitor[None], m.Expr.Visitor[Type]):
|
||||
result: Optional[Type] = self._operations.get(operation)
|
||||
return result
|
||||
|
||||
def _define_builtin(self):
|
||||
"""Define builtin types and operations"""
|
||||
self.define_type("bool", BaseType(name="bool"))
|
||||
self.define_type("int", BaseType(name="int"))
|
||||
self.define_type("float", BaseType(name="float"))
|
||||
self.define_type("str", BaseType(name="str"))
|
||||
self.define_operation(
|
||||
left=self.get_type("int"),
|
||||
operator="__add__",
|
||||
right=self.get_type("int"),
|
||||
result=self.get_type("int"),
|
||||
)
|
||||
|
||||
def define_type(self, name: str, type: Type) -> Type:
|
||||
"""Define a type in the registry
|
||||
|
||||
|
||||
Reference in New Issue
Block a user