Source code for gam.ast.stmt

from typing import List, Optional, Union, Literal

from pydantic import Field

from gam.ast.base_ast import Statement, ASTNode
from gam.ast.expr import Expression, Identifier, MemberExpression
from gam.ast.types import TypeAnnotation



[docs] class ExpressionStatement(Statement): """Represents a statement that is an expression. Example: ```python x + y; ``` """ name: Literal['ExpressionStatement'] = Field(default='ExpressionStatement') expression: Expression = Field( description="Expression to be evaluated" )
[docs] class BlockStatement(Statement): """Represents a block of statements enclosed in curly braces. A block statement groups multiple statements together and can create a new scope. Example: ```python { x = 1; y = 2; x + y; } ``` """ name: Literal['BlockStatement'] = Field(default='BlockStatement') body: List[Statement] = Field( default_factory=list, description="List of statements contained within the block" )
[docs] class AssignmentStatement(Statement): """Represents an assignment operation. Example: ```python x = 5; obj.prop = value; ``` """ name: Literal['AssignmentStatement'] = Field(default='AssignmentStatement') left: Union[Identifier, MemberExpression] = Field( description="Target of the assignment (variable or property)" ) right: Expression = Field( description="Value being assigned" )
[docs] class FunctionParameter(ASTNode): """Represents a parameter in a function declaration. Example: ```python function add(x: number, y: number) { ... } # x and y are FunctionParameters ``` """ name: Literal['FunctionParameter'] = Field(default='FunctionParameter') id: Identifier = Field( description="Name of the parameter" ) type_annotation: Optional[TypeAnnotation] = Field( default=None, description="Type annotation for the parameter" )
[docs] class FunctionDeclaration(Statement): """Represents a function declaration Support normally function declaration, also agrs with type hint, default values, and return type hint. Example: ```python def create_user(name: str, age: int) -> User: return User(name=name, age=age) # will be converted to: FunctionDeclaration( id=Identifier(name="create_user"), params=[ FunctionParameter( id=Identifier(name="name"), type_annotation=StringType() ), FunctionParameter( id=Identifier(name="age"), type_annotation=IntType() ) ], body=BlockStatement( body=[ ReturnStatement( argument=StructLiteral( struct_type=Identifier(name="User"), fields=[ Property( key=Identifier(name="name"), value=Identifier(name="name") ), Property( key=Identifier(name="age"), value=Identifier(name="age") ), Property( key=Identifier(name="is_admin"), value=Identifier(name="is_admin") ) ] ) ) ] ), return_type=StructType(struct_type=Identifier(name="User")) ) ``` """ name: Literal['FunctionDeclaration'] = Field(default='FunctionDeclaration') id: Identifier = Field( description="Name of the function" ) description: Optional[str] = Field( default=None, description="Description of the function" ) params: List[FunctionParameter] = Field( default_factory=list, description="List of function parameters" ) body: BlockStatement = Field( description="Function body containing the statements" ) return_type: Optional[TypeAnnotation] = Field( default=None, description="Return type annotation of the function" )
[docs] class ReturnStatement(Statement): """Represents a return statement that exits a function and returns a value. Example: ```python return name # will be converted to: ReturnStatement( argument=Identifier(name="name") ) ``` """ name: Literal['ReturnStatement'] = Field(default='ReturnStatement') argument: Expression = Field( description="Expression whose value is returned" )
# -> while
[docs] class WhileStatement(Statement): """Represents a while loop that executes its body while a condition is true. **hello** ```python while (i < 10): i += 1 """ name: Literal['WhileStatement'] = Field(default='WhileStatement') test: Expression = Field( description="Condition that determines whether to continue the loop" ) body: BlockStatement = Field( description="Statements to execute in each iteration" )
[docs] class IfStatement(Statement): """Represents an if-else conditional statement with optional elif branches. The IfStatement node represents a complete conditional control structure including: - A main if condition and its body - Optional elif branches (stored in elif_branches) - An optional else branch (stored in alternate) Example: .. code-block:: python while (i < 10): i += 1 if (i < 10): i += 1 elif (i == 10): i += 1 # will be converted to: .. code-block:: python IfStatement( test=BinaryExpression( left=Identifier(name="i"), operator=LessThanOps(), right=IntegerLiteral(value=10) ), consequent=BlockStatement( body=[ AssignmentStatement( left=Identifier(name="i"), right=BinaryExpression( left=Identifier(name="i"), operator=AddOps(), right=IntegerLiteral(value=1) ) ) ] ), elif_branches=[ ElifStatement( test=BinaryExpression( left=Identifier(name="i"), operator=EqualOps(), right=IntegerLiteral(value=10) ), consequent=BlockStatement( body=[ AssignmentStatement( left=Identifier(name="i"), right=BinaryExpression( left=Identifier(name="i"), operator=AddOps(), right=IntegerLiteral(value=1) ) ) ] ) ) ] ) """ name: Literal['IfStatement'] = Field(default='IfStatement') test: Expression = Field( description="Condition to evaluate" ) consequent: BlockStatement = Field( description="Statements to execute if condition is true" ) elif_branches: List['ElifStatement'] = Field( default_factory=list, description="List of else-if branches" ) alternate: Optional[BlockStatement] = Field( default=None, description="Else branch (executed if all conditions are false)" )
[docs] class ElifStatement(Statement): """Represents an else-if branch in an if statement. Example: ```python if (x > 0): ... elif (x < 0): ... # will be converted to: IfStatement( test=BinaryExpression( left=Identifier(name="x"), operator=GreaterThanOps(), right=IntegerLiteral(value=0) ), consequent=BlockStatement( body=[ ... ] ), elif_branches=[ ElifStatement( test=BinaryExpression( left=Identifier(name="x"), operator=LessThanOps(), right=IntegerLiteral(value=0) ), consequent=BlockStatement( body=[ ... ] ) ) ], alternate=BlockStatement( body=[ ... ] ) ) ``` """ name: Literal['ElifStatement'] = Field(default='ElifStatement') test: Expression = Field( description="Condition to evaluate for this else-if branch" ) consequent: BlockStatement = Field( description="Statements to execute if this else-if condition is true" )
[docs] class BreakStatement(Statement): """Represents a break statement that exits a loop or switch. Example: ```python while (i < 10): if (i == 5): break # will be converted to: WhileStatement( test=BinaryExpression( left=Identifier(name="i"), operator=LessThanOps(), right=IntegerLiteral(value=10) ), body=BlockStatement( body=[ IfStatement( test=BinaryExpression( left=Identifier(name="i"), operator=EqualOps(), right=IntegerLiteral(value=5) ), consequent=BlockStatement( body=[ BreakStatement() ] ) ) ] ) ``` """ name: Literal['BreakStatement'] = Field(default='BreakStatement') label: Optional[Identifier] = Field( default=None, description="Optional label to break from a specific loop" )
[docs] class TryStatement(Statement): """Represents a try-catch-finally block for exception handling. Example: ```python try: riskyOperation() except ValueError as e: handleError(e) # will be converted to: TryStatement( block=BlockStatement( body=[ ExpressionStatement( expression=CallExpression( callee=Identifier(name="riskyOperation"), arguments=[] ) ) ], ), handlers=[ CatchClause( param=Identifier(name="error"), exception_type=Identifier(name="Error"), body=BlockStatement( body=[ ExpressionStatement( expression=CallExpression( callee=Identifier(name="handleError"), arguments=[Identifier(name="e")] ) ) ] ) ) ] ) ``` """ name: Literal['TryStatement'] = Field(default='TryStatement') block: BlockStatement = Field( description="Statements to try executing" ) handlers: List['CatchClause'] = Field( default_factory=list, description="List of catch blocks to handle exceptions" ) finalizer: Optional[BlockStatement] = Field( default=None, description="Finally block that always executes" )
[docs] class CatchClause(ASTNode): """Represents a catch block in a try-catch statement. Example: ```python try: riskyOperation() except ValueError as e: handleError(e) # will be converted to: CatchClause( param=Identifier(name="error"), exception_type=Identifier(name="Error"), body=BlockStatement( body=[ ExpressionStatement( expression=CallExpression( callee=Identifier(name="handleError"), arguments=[Identifier(name="e")] ) ) ] ) ) ``` """ name: Literal['CatchClause'] = Field(default='CatchClause') param: Optional[Identifier] = Field( default=None, description="Name of the variable to hold the caught exception" ) exception_type: Optional[Identifier] = Field( default=None, description="Type of exception to catch" ) body: BlockStatement = Field( description="Statements to execute when exception is caught" )
[docs] class ThrowStatement(Statement): """Represents a throw statement that raises an exception. Example: ```python raise Exception("Something went wrong") # will be converted to: ThrowStatement( argument=StringLiteral(value="Something went wrong") ) ``` """ name: Literal['ThrowStatement'] = Field(default='ThrowStatement') argument: Expression = Field( description="Expression to throw as an exception" )
# Struct-related nodes
[docs] class StructDeclaration(Statement): """Represents a struct/class declaration. Example: ```python class Point: x: int y: int # will be converted to: StructDeclaration( id=Identifier(name="Point"), fields=[ StructField( field_id=Identifier(name="x"), type_annotation=NumberType(), default_value=None ), StructField( field_id=Identifier(name="y"), type_annotation=NumberType(), default_value=None ) ] ``` """ name: Literal['StructDeclaration'] = Field(default='StructDeclaration') id: Identifier = Field( description="Name of the struct" ) fields: List['StructField'] = Field( default_factory=list, description="List of fields in the struct" )
[docs] class StructField(ASTNode): """Represents a field in a struct/class declaration. Example: ```python class Point: x: int y: int # will be converted to: StructDeclaration( id=Identifier(name="Point"), fields=[ StructField( field_id=Identifier(name="x"), type_annotation=NumberType(), default_value=None ), StructField( field_id=Identifier(name="y"), type_annotation=NumberType(), default_value=None ) ] ``` """ name: Literal['StructField'] = Field(default='StructField') field_id: Identifier = Field( description="Name of the field" ) type_annotation: Optional[TypeAnnotation] = Field( default=None, description="Type annotation for the field" ) default_value: Optional[Expression] = Field( default=None, description="Default value for the field" )