173 lines
3.1 KiB
Typst
173 lines
3.1 KiB
Typst
#import "@preview/acrostiche:0.7.0": acr
|
|
#import "utils.typ": format-ast-nodes
|
|
|
|
#let ast-nodes = (
|
|
("Other classes", none, ```python
|
|
class ParamSpec:
|
|
pos: list[Function.Parameter]
|
|
mixed: list[Function.Parameter]
|
|
kw: list[Function.Parameter]
|
|
@property
|
|
def all(self) -> list[Function.Parameter]:
|
|
return self.pos + self.mixed + self.kw
|
|
|
|
|
|
class ImportAlias:
|
|
location: Location
|
|
name: str
|
|
alias: Optional[str] = None
|
|
@property
|
|
def imported_name(self) -> str:
|
|
return (
|
|
self.alias
|
|
if self.alias is not None else
|
|
self.name
|
|
)
|
|
```),
|
|
|
|
("Type annotations", `MidasType`, ```python
|
|
class BaseType:
|
|
base: str
|
|
args: tuple[MidasType,...]
|
|
|
|
class FrameColumn:
|
|
name: Optional[str]
|
|
type: Optional[MidasType]
|
|
|
|
class FrameType:
|
|
columns: list[FrameColumn]
|
|
```),
|
|
|
|
("Statements", `Stmt`, ```python
|
|
class ExpressionStmt:
|
|
expr: Expr
|
|
|
|
class Function:
|
|
name: str
|
|
params: ParamSpec
|
|
returns: Optional[MidasType]
|
|
body: list[Stmt]
|
|
|
|
class Parameter:
|
|
location: Optional[Location] = None
|
|
name: str
|
|
type: Optional[MidasType]
|
|
default: Optional[Expr]
|
|
|
|
class TypeAssign:
|
|
name: str
|
|
type: MidasType
|
|
|
|
class AssignStmt:
|
|
targets: list[Expr]
|
|
value: Expr
|
|
|
|
class ReturnStmt:
|
|
value: Optional[Expr]
|
|
|
|
class IfStmt:
|
|
test: Expr
|
|
body: list[Stmt]
|
|
orelse: list[Stmt]
|
|
|
|
class Pass: ...
|
|
|
|
class ForStmt:
|
|
target: Expr
|
|
iterator: Expr
|
|
body: list[Stmt]
|
|
|
|
class ImportStmt:
|
|
imports: list[ImportAlias]
|
|
|
|
class FromImportStmt:
|
|
module: Optional[str]
|
|
imports: list[ImportAlias]
|
|
level: int
|
|
|
|
class RawStmt:
|
|
stmt: ast.stmt
|
|
```),
|
|
|
|
("Expressions", `Expr`, ```python
|
|
class BinaryExpr:
|
|
left: Expr
|
|
operator: ast.operator
|
|
right: Expr
|
|
|
|
class CompareExpr:
|
|
left: Expr
|
|
operator: ast.cmpop
|
|
right: Expr
|
|
|
|
class UnaryExpr:
|
|
operator: ast.unaryop
|
|
right: Expr
|
|
|
|
class CallExpr:
|
|
callee: Expr
|
|
arguments: list[Expr]
|
|
keywords: dict[str, Expr]
|
|
|
|
class GetExpr:
|
|
object: Expr
|
|
name: str
|
|
|
|
class LiteralExpr:
|
|
value: Any
|
|
|
|
class VariableExpr:
|
|
name: str
|
|
|
|
class LogicalExpr:
|
|
left: Expr
|
|
operator: ast.boolop
|
|
right: Expr
|
|
|
|
class CastExpr:
|
|
type: MidasType
|
|
expr: Expr
|
|
unsafe: bool
|
|
|
|
class TernaryExpr:
|
|
test: Expr
|
|
if_true: Expr
|
|
if_false: Expr
|
|
|
|
class ListExpr:
|
|
items: list[Expr]
|
|
|
|
class DictExpr:
|
|
keys: list[Optional[Expr]]
|
|
values: list[Expr]
|
|
|
|
class SubscriptExpr:
|
|
object: Expr
|
|
index: Expr
|
|
|
|
class SliceExpr:
|
|
lower: Optional[Expr]
|
|
upper: Optional[Expr]
|
|
step: Optional[Expr]
|
|
|
|
class TupleExpr:
|
|
items: tuple[Expr, ...]
|
|
|
|
class RawExpr:
|
|
expr: ast.expr
|
|
```),
|
|
)
|
|
|
|
#let python-ast-nodes = table(
|
|
inset: .5em,
|
|
stroke: gray,
|
|
row-gutter: 2em,
|
|
..ast-nodes.map(nodes => format-ast-nodes(..nodes))
|
|
)
|
|
|
|
#show figure: set block(breakable: true)
|
|
#figure(
|
|
python-ast-nodes,
|
|
caption: [Python #acr("AST") Nodes],
|
|
) <tab:python-ast-nodes>
|