Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16d6e1b603
|
||
|
|
c3229b557c
|
||
|
|
0a8e0fb6c2
|
||
|
|
61514d036c
|
@@ -92,6 +92,10 @@ class ForStmt:
|
||||
body: list[Stmt]
|
||||
|
||||
|
||||
class RawStmt:
|
||||
stmt: ast.stmt
|
||||
|
||||
|
||||
###<
|
||||
|
||||
|
||||
@@ -164,4 +168,8 @@ class SliceExpr:
|
||||
step: Optional[Expr]
|
||||
|
||||
|
||||
class RawExpr:
|
||||
expr: ast.expr
|
||||
|
||||
|
||||
###<
|
||||
|
||||
@@ -613,6 +613,11 @@ class PythonAstPrinter(
|
||||
self._mark_last()
|
||||
body_stmt.accept(self)
|
||||
|
||||
def visit_raw_stmt(self, stmt: p.RawStmt) -> None:
|
||||
self._write_line("RawStmt")
|
||||
with self._child_level(single=True):
|
||||
self._write_line(f"stmt: {ast.unparse(stmt.stmt)}")
|
||||
|
||||
def visit_binary_expr(self, expr: p.BinaryExpr) -> None:
|
||||
self._write_line("BinaryExpr")
|
||||
with self._child_level():
|
||||
@@ -756,3 +761,8 @@ class PythonAstPrinter(
|
||||
self._write_optional_child("lower", expr.lower)
|
||||
self._write_optional_child("upper", expr.upper)
|
||||
self._write_optional_child("step", expr.step, last=True)
|
||||
|
||||
def visit_raw_expr(self, expr: p.RawExpr) -> None:
|
||||
self._write_line("RawExpr")
|
||||
with self._child_level(single=True):
|
||||
self._write_line(f"expr: {ast.unparse(expr.expr)}")
|
||||
|
||||
@@ -113,6 +113,9 @@ class Stmt(ABC):
|
||||
@abstractmethod
|
||||
def visit_for_stmt(self, stmt: ForStmt) -> T: ...
|
||||
|
||||
@abstractmethod
|
||||
def visit_raw_stmt(self, stmt: RawStmt) -> T: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ExpressionStmt(Stmt):
|
||||
@@ -202,6 +205,14 @@ class ForStmt(Stmt):
|
||||
return visitor.visit_for_stmt(self)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RawStmt(Stmt):
|
||||
stmt: ast.stmt
|
||||
|
||||
def accept(self, visitor: Stmt.Visitor[T]) -> T:
|
||||
return visitor.visit_raw_stmt(self)
|
||||
|
||||
|
||||
###############
|
||||
# Expressions #
|
||||
###############
|
||||
@@ -254,6 +265,9 @@ class Expr(ABC):
|
||||
@abstractmethod
|
||||
def visit_slice_expr(self, expr: SliceExpr) -> T: ...
|
||||
|
||||
@abstractmethod
|
||||
def visit_raw_expr(self, expr: RawExpr) -> T: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BinaryExpr(Expr):
|
||||
@@ -373,3 +387,11 @@ class SliceExpr(Expr):
|
||||
|
||||
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
||||
return visitor.visit_slice_expr(self)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RawExpr(Expr):
|
||||
expr: ast.expr
|
||||
|
||||
def accept(self, visitor: Expr.Visitor[T]) -> T:
|
||||
return visitor.visit_raw_expr(self)
|
||||
|
||||
@@ -252,7 +252,7 @@ class PythonTyper(
|
||||
if returns_hint is not None:
|
||||
assert stmt.returns is not None
|
||||
returns = returns_hint
|
||||
if returns != inferred_return:
|
||||
if not self.is_subtype(inferred_return, returns):
|
||||
self.reporter.error(
|
||||
stmt.returns.location,
|
||||
f"Return type mismatch, annotated {returns} but returns {inferred_return}",
|
||||
@@ -370,6 +370,9 @@ class PythonTyper(
|
||||
if body_returned:
|
||||
raise ReturnException()
|
||||
|
||||
def visit_raw_stmt(self, stmt: p.RawStmt) -> None:
|
||||
pass
|
||||
|
||||
def visit_binary_expr(self, expr: p.BinaryExpr) -> Type:
|
||||
method: Optional[str] = OPERATOR_METHODS.get(expr.operator.__class__)
|
||||
if method is None:
|
||||
@@ -566,6 +569,9 @@ class PythonTyper(
|
||||
def visit_slice_expr(self, expr: p.SliceExpr) -> Type:
|
||||
return self.types.get_type("slice")
|
||||
|
||||
def visit_raw_expr(self, expr: p.RawExpr) -> Type:
|
||||
return UnknownType()
|
||||
|
||||
def visit_base_type(self, node: p.BaseType) -> Type:
|
||||
base: Type
|
||||
try:
|
||||
|
||||
@@ -163,6 +163,9 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]):
|
||||
self.resolve(*stmt.body)
|
||||
self.end_scope()
|
||||
|
||||
def visit_raw_stmt(self, stmt: p.RawStmt) -> None:
|
||||
pass
|
||||
|
||||
def visit_binary_expr(self, expr: p.BinaryExpr) -> None:
|
||||
self.resolve(expr.left)
|
||||
self.resolve(expr.right)
|
||||
@@ -221,3 +224,6 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]):
|
||||
self.resolve(expr.upper)
|
||||
if expr.step is not None:
|
||||
self.resolve(expr.step)
|
||||
|
||||
def visit_raw_expr(self, expr: p.RawExpr) -> None:
|
||||
pass
|
||||
|
||||
@@ -1,11 +1,33 @@
|
||||
import ast
|
||||
import shutil
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
from midas.ast.location import Location
|
||||
import midas.ast.python as p
|
||||
from midas.checker.types import (
|
||||
AliasType,
|
||||
AppliedType,
|
||||
BaseType,
|
||||
ComplexType,
|
||||
ExtensionType,
|
||||
Function,
|
||||
GenericType,
|
||||
OverloadedFunction,
|
||||
TopType,
|
||||
Type,
|
||||
TypeVar,
|
||||
UnitType,
|
||||
)
|
||||
from midas.utils import TypedAST
|
||||
|
||||
|
||||
@dataclass
|
||||
class Scope:
|
||||
pre_assertions: list[ast.stmt] = field(default_factory=list)
|
||||
aliases: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
class Generator(p.Stmt.Visitor[ast.stmt], p.Expr.Visitor[ast.expr]):
|
||||
def __init__(self, workdir: Path) -> None:
|
||||
self.workdir: Path = workdir.resolve()
|
||||
@@ -13,19 +35,28 @@ class Generator(p.Stmt.Visitor[ast.stmt], p.Expr.Visitor[ast.expr]):
|
||||
if self.build_dir.exists():
|
||||
shutil.rmtree(self.build_dir)
|
||||
self.build_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.rel_src_path: Path = Path()
|
||||
|
||||
self._typed_ast: TypedAST = TypedAST(
|
||||
stmts=[],
|
||||
judgements=[],
|
||||
)
|
||||
self._alias_count: int = 0
|
||||
self._scopes: list[Scope] = []
|
||||
|
||||
def generate(self, typed_ast: TypedAST, src_path: Path) -> Path:
|
||||
self.rel_src_path = src_path.relative_to(self.workdir)
|
||||
self._typed_ast = typed_ast
|
||||
body: list[ast.stmt] = self._visit_body(typed_ast.stmts)
|
||||
module = ast.Module(body=body, type_ignores=[])
|
||||
module = ast.fix_missing_locations(module)
|
||||
compiled: str = ast.unparse(module)
|
||||
rel_src_path: Path = src_path.relative_to(self.workdir)
|
||||
out_path: Path = (self.build_dir / rel_src_path).resolve()
|
||||
out_path: Path = (self.build_dir / self.rel_src_path).resolve()
|
||||
try:
|
||||
_ = out_path.relative_to(self.build_dir)
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
f"Directory traversal, {rel_src_path} points outside of parent directory"
|
||||
f"Directory traversal, {self.rel_src_path} points outside of parent directory"
|
||||
)
|
||||
out_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
out_path.write_text(compiled)
|
||||
@@ -80,8 +111,13 @@ class Generator(p.Stmt.Visitor[ast.stmt], p.Expr.Visitor[ast.expr]):
|
||||
)
|
||||
|
||||
def visit_cast_expr(self, expr: p.CastExpr) -> ast.expr:
|
||||
# TODO: insert assertion
|
||||
return expr.expr.accept(self)
|
||||
expr2: ast.expr = expr.expr.accept(self)
|
||||
alias: ast.expr = self._make_alias(expr2)
|
||||
|
||||
type: Type = self._get_expr_type(expr)
|
||||
self._make_cast_asserts(expr.location, alias, type)
|
||||
|
||||
return alias
|
||||
|
||||
def visit_ternary_expr(self, expr: p.TernaryExpr) -> ast.expr:
|
||||
return ast.IfExp(
|
||||
@@ -108,6 +144,9 @@ class Generator(p.Stmt.Visitor[ast.stmt], p.Expr.Visitor[ast.expr]):
|
||||
step=expr.step.accept(self) if expr.step is not None else None,
|
||||
)
|
||||
|
||||
def visit_raw_expr(self, expr: p.RawExpr) -> ast.expr:
|
||||
return expr.expr
|
||||
|
||||
def visit_expression_stmt(self, stmt: p.ExpressionStmt) -> ast.stmt:
|
||||
return ast.Expr(
|
||||
value=stmt.expr.accept(self),
|
||||
@@ -169,5 +208,120 @@ class Generator(p.Stmt.Visitor[ast.stmt], p.Expr.Visitor[ast.expr]):
|
||||
orelse=[],
|
||||
)
|
||||
|
||||
def visit_raw_stmt(self, stmt: p.RawStmt) -> ast.stmt:
|
||||
return stmt.stmt
|
||||
|
||||
def _visit_body(self, stmts: list[p.Stmt]) -> list[ast.stmt]:
|
||||
return [stmt.accept(self) for stmt in stmts]
|
||||
generated: list[ast.stmt] = []
|
||||
for stmt in stmts:
|
||||
scope = Scope()
|
||||
self._scopes.append(scope)
|
||||
|
||||
stmt2 = stmt.accept(self)
|
||||
generated.extend(scope.pre_assertions)
|
||||
generated.append(stmt2)
|
||||
if len(scope.aliases) != 0:
|
||||
generated.append(
|
||||
ast.Delete(targets=[ast.Name(id=alias) for alias in scope.aliases])
|
||||
)
|
||||
self._scopes.pop()
|
||||
|
||||
# Remove redundant pass statements
|
||||
if len(generated) > 1:
|
||||
generated = [stmt for stmt in generated if not isinstance(stmt, ast.Pass)]
|
||||
return generated
|
||||
|
||||
def _make_alias(self, expr: ast.expr) -> ast.expr:
|
||||
name: str = f"__midas_alias_{self._alias_count}__"
|
||||
alias = ast.Name(id=name)
|
||||
self._alias_count += 1
|
||||
self._scopes[-1].aliases.append(name)
|
||||
self._scopes[-1].pre_assertions.append(
|
||||
ast.Assign(
|
||||
targets=[alias],
|
||||
value=expr,
|
||||
)
|
||||
)
|
||||
return alias
|
||||
|
||||
def _add_assert(self, expr: ast.expr, message: str | ast.expr):
|
||||
if isinstance(message, str):
|
||||
message = ast.Constant(value=message)
|
||||
self._scopes[-1].pre_assertions.append(
|
||||
ast.Assert(
|
||||
test=expr,
|
||||
msg=message,
|
||||
)
|
||||
)
|
||||
|
||||
def _get_expr_type(self, query: p.Expr) -> Type:
|
||||
for expr, type in self._typed_ast.judgements:
|
||||
if expr == query:
|
||||
return type
|
||||
raise RuntimeError(f"Cannot get type judgement for {query}")
|
||||
|
||||
def _make_cast_asserts(self, src_location: Location, expr: ast.expr, type: Type):
|
||||
match type:
|
||||
case BaseType(name=name):
|
||||
self._add_assert(
|
||||
ast.Call(
|
||||
func=ast.Name(id="isinstance"),
|
||||
args=[expr, ast.Name(id=name)],
|
||||
keywords=[],
|
||||
),
|
||||
self._make_cast_assert_message(src_location, expr, type),
|
||||
)
|
||||
|
||||
case AliasType(type=base):
|
||||
self._make_cast_asserts(src_location, expr, base)
|
||||
|
||||
case UnitType():
|
||||
self._add_assert(
|
||||
ast.Compare(
|
||||
left=expr,
|
||||
ops=[ast.Is()],
|
||||
comparators=[
|
||||
ast.Constant(value=None),
|
||||
],
|
||||
),
|
||||
self._make_cast_assert_message(src_location, expr, type),
|
||||
)
|
||||
|
||||
case AppliedType():
|
||||
self._make_cast_asserts(src_location, expr, type.body)
|
||||
|
||||
case (
|
||||
TopType()
|
||||
| Function()
|
||||
| OverloadedFunction()
|
||||
| ComplexType()
|
||||
| ExtensionType()
|
||||
| GenericType()
|
||||
):
|
||||
raise NotImplementedError(f"Can't make assertion for type {type}")
|
||||
|
||||
case TypeVar():
|
||||
raise RuntimeError("Unexpected TypeVar")
|
||||
|
||||
def _make_cast_assert_message(
|
||||
self, location: Location, expr: ast.expr, type: Type
|
||||
) -> ast.expr:
|
||||
loc_str: str = f"{self.rel_src_path}:L{location.lineno}:{location.col_offset+1}"
|
||||
# f"file.py:L1:1: CastError: Cannot cast {type(expr).__name__} to Type"
|
||||
return ast.JoinedStr(
|
||||
values=[
|
||||
ast.Constant(f"{loc_str}: CastError: Cannot cast "),
|
||||
ast.FormattedValue(
|
||||
value=ast.Attribute(
|
||||
value=ast.Call(
|
||||
func=ast.Name(id="type"),
|
||||
args=[expr],
|
||||
keywords=[],
|
||||
),
|
||||
attr="__name__",
|
||||
),
|
||||
conversion=-1,
|
||||
),
|
||||
ast.Constant(f" to {type}"),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -22,6 +22,8 @@ from midas.ast.python import (
|
||||
LiteralExpr,
|
||||
LogicalExpr,
|
||||
MidasType,
|
||||
RawExpr,
|
||||
RawStmt,
|
||||
ReturnStmt,
|
||||
SliceExpr,
|
||||
Stmt,
|
||||
@@ -99,7 +101,7 @@ class PythonParser:
|
||||
|
||||
case _:
|
||||
print(f"Unsupported statement: {ast.unparse(node)}")
|
||||
return None
|
||||
return RawStmt(location=location, stmt=node)
|
||||
|
||||
def parse_annotation_assign(self, node: ast.AnnAssign) -> list[Stmt]:
|
||||
statements: list[Stmt] = []
|
||||
@@ -461,7 +463,8 @@ class PythonParser:
|
||||
)
|
||||
|
||||
case _:
|
||||
raise UnsupportedSyntaxError(node)
|
||||
print(f"Unsupported expression: {ast.unparse(node)}")
|
||||
return RawExpr(location=location, expr=node)
|
||||
|
||||
def parse_bool_op(self, node: ast.BoolOp) -> LogicalExpr:
|
||||
op: ast.boolop = node.op
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"stmts": [
|
||||
{
|
||||
"_type": "RawStmt",
|
||||
"stmt": "from __future__ import annotations"
|
||||
},
|
||||
{
|
||||
"_type": "TypeAssign",
|
||||
"name": "df",
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"stmts": [
|
||||
{
|
||||
"_type": "RawStmt",
|
||||
"stmt": "from __future__ import annotations"
|
||||
},
|
||||
{
|
||||
"_type": "TypeAssign",
|
||||
"name": "df",
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"stmts": [
|
||||
{
|
||||
"_type": "RawStmt",
|
||||
"stmt": "from __future__ import annotations"
|
||||
},
|
||||
{
|
||||
"_type": "Function",
|
||||
"name": "func",
|
||||
|
||||
@@ -22,6 +22,8 @@ from midas.ast.python import (
|
||||
LogicalExpr,
|
||||
MidasType,
|
||||
Pass,
|
||||
RawExpr,
|
||||
RawStmt,
|
||||
ReturnStmt,
|
||||
SliceExpr,
|
||||
Stmt,
|
||||
@@ -191,6 +193,12 @@ class PythonAstJsonSerializer(
|
||||
"body": self._serialize_list(stmt.body),
|
||||
}
|
||||
|
||||
def visit_raw_stmt(self, stmt: RawStmt) -> dict:
|
||||
return {
|
||||
"_type": "RawStmt",
|
||||
"stmt": ast.unparse(stmt.stmt),
|
||||
}
|
||||
|
||||
def visit_binary_expr(self, expr: BinaryExpr) -> dict:
|
||||
return {
|
||||
"_type": "BinaryExpr",
|
||||
@@ -284,3 +292,9 @@ class PythonAstJsonSerializer(
|
||||
"upper": self._serialize_optional(expr.upper),
|
||||
"step": self._serialize_optional(expr.step),
|
||||
}
|
||||
|
||||
def visit_raw_expr(self, expr: RawExpr) -> dict:
|
||||
return {
|
||||
"_type": "RawExpr",
|
||||
"expr": ast.unparse(expr.expr),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user