Source code for gam.ast.expr
from typing import List, Optional, Union, Literal
from datetime import datetime
from pydantic import Field, create_model
from gam.ast.base_ast import Expression, ASTNode
from gam.ast.ops import BinaryOperator, UnaryOperator, Operator
[docs]
class BaseLiteral(Expression):
"""Base class for all literal values in the AST."""
name: Literal['Literal'] = Field(default='Literal')
[docs]
class IntegerLiteral(BaseLiteral):
"""Represents an integer literal value in the AST."""
name: Literal['IntegerLiteral'] = Field(default='IntegerLiteral')
value: int = Field(description="The integer value of the literal")
[docs]
class FloatLiteral(BaseLiteral):
"""Represents a floating-point literal value in the AST."""
name: Literal['FloatLiteral'] = Field(default='FloatLiteral')
value: float = Field(
description="The floating-point value of the literal"
)
[docs]
class StringLiteral(BaseLiteral):
"""Represents a string literal value in the AST."""
name: Literal['StringLiteral'] = Field(default='StringLiteral')
value: str = Field(
description="The string value of the literal"
)
[docs]
class BooleanLiteral(BaseLiteral):
"""Represents a boolean literal value in the AST."""
name: Literal['BooleanLiteral'] = Field(default='BooleanLiteral')
value: bool = Field(
description="The boolean value of the literal"
)
[docs]
class DateTimeLiteral(BaseLiteral):
"""Represents a datetime literal value in the AST."""
name: Literal['DateTimeLiteral'] = Field(default='DateTimeLiteral')
value: Optional[datetime] = Field(
description="The datetime value of the literal. If not provided, the current datetime will be used.",
default=None
)
[docs]
class NullLiteral(BaseLiteral):
"""Represents a null literal value in the AST."""
name: Literal['NullLiteral'] = Field(default='NullLiteral')
[docs]
class StructLiteral(BaseLiteral):
"""Represents a struct instance creation in the AST."""
name: Literal['StructLiteral'] = Field(default='StructLiteral')
struct_type: "Identifier" = Field(
description="The type identifier of the struct"
)
fields: List["Property"] = Field(
default_factory=list,
description="List of field initializations"
)
[docs]
class ArrayLiteral(BaseLiteral):
"""Represents an array literal value in the AST."""
name: Literal['ArrayLiteral'] = Field(default='ArrayLiteral')
elements: List[Expression] = Field(
default_factory=list,
description="The elements of the array"
)
[docs]
class Identifier(Expression):
"""Represents an identifier (variable name, function name, etc.) in the AST.
Only use UQN for identifier.
"""
name: Literal['Identifier'] = Field(default='Identifier')
uqn: str = Field(
description="The unique name of the identifier"
)
[docs]
class BinaryExpression(Expression):
"""Represents a binary operation expression in the AST."""
name: Literal['BinaryExpression'] = Field(default='BinaryExpression')
left: Expression = Field(
description="Left-hand side of the binary operation"
)
operator: BinaryOperator = Field(description="The binary operator")
right: Expression = Field(
description="Right-hand side of the binary operation"
)
[docs]
class UnaryExpression(Expression):
"""Represents a unary operation expression in the AST."""
name: Literal['UnaryExpression'] = Field(default='UnaryExpression')
operator: UnaryOperator = Field(
description="The unary operator"
)
operand: Expression = Field(
description="The expression being operated on"
)
[docs]
class CallExpression(Expression):
"""Represents a function or method call expression in the AST."""
name: Literal['CallExpression'] = Field(default='CallExpression')
callee: Expression = Field(
description="The expression being called"
)
arguments: List[Expression] = Field(
default_factory=list,
description="Positional arguments"
)
kwargs: List["Property"] = Field(
default_factory=list,
description="Keyword arguments"
)
[docs]
class MemberExpression(Expression):
"""Represents a member access expression (object.property) in the AST."""
name: Literal['MemberExpression'] = Field(default='MemberExpression')
object: Expression = Field( # should be a struct instance ?
description="The object being accessed"
)
property: Union[Identifier, Expression] = Field(
description="The property being accessed"
)
computed: bool = Field(
default=False,
description="Whether the property is computed (e.g., obj[expr] vs obj.prop)"
)
[docs]
class ArrayExpression(Expression):
"""Represents an array literal expression in the AST."""
name: Literal['ArrayExpression'] = Field(default='ArrayExpression')
elements: List[Expression] = Field(
default_factory=list,
description="Array elements"
)
[docs]
class Property(Expression):
"""Represents a property in an object literal or struct initialization."""
name: Literal['Property'] = Field(default='Property')
key: Union[Identifier, BaseLiteral] = Field(
description="The property key"
)
value: Expression = Field(
description="The property value"
)
[docs]
class ObjectExpression(Expression):
"""Represents an object literal expression in the AST."""
name: Literal['ObjectExpression'] = Field(default='ObjectExpression')
properties: List[Property] = Field(
default_factory=list,
description="Object properties"
)