use crate::lexer::tokens::Literal;
use crate::lexer::tokens::Operator;
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, Default)]
pub struct Span {
pub line: usize,
pub column: usize,
}
#[derive(Debug, Clone)]
pub struct Program {
pub statements: Vec<Statement>,
pub statement_spans: Vec<Option<Span>>,
}
#[derive(Debug, Clone)]
pub enum Statement {
Expression(Expression),
Let(LetStatement),
Return(ReturnStatement),
Block(BlockStatement),
Function(FunctionStatement),
Service(ServiceStatement),
Spawn(SpawnStatement),
Agent(AgentStatement),
Message(MessageStatement),
Event(EventStatement),
If(IfStatement),
While(WhileStatement),
Try(TryStatement),
ForIn(ForInStatement),
Break(BreakStatement),
Continue(ContinueStatement),
Loop(LoopStatement),
Match(MatchStatement),
Import(ImportStatement),
}
#[derive(Debug, Clone)]
pub struct ImportStatement {
pub path: String,
pub alias: Option<String>,
}
#[derive(Debug, Clone)]
pub struct WhileStatement {
pub condition: Expression,
pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub struct ForInStatement {
pub variable: String,
pub iterable: Expression,
pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub struct BreakStatement {
pub value: Option<Expression>, }
#[derive(Debug, Clone)]
pub struct ContinueStatement;
#[derive(Debug, Clone)]
pub struct LoopStatement {
pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub struct MatchStatement {
pub expression: Expression,
pub cases: Vec<MatchCase>,
pub default_case: Option<BlockStatement>,
}
#[derive(Debug, Clone)]
pub struct MatchCase {
pub pattern: MatchPattern,
pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub enum MatchPattern {
Literal(Literal), Identifier(String), Wildcard, Range(Box<Expression>, Box<Expression>), }
#[derive(Debug, Clone)]
pub struct LetStatement {
pub name: String,
pub value: Expression,
pub line: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct ReturnStatement {
pub value: Option<Expression>,
}
#[derive(Debug, Clone)]
pub struct BlockStatement {
pub statements: Vec<Statement>,
}
#[derive(Debug, Clone)]
pub struct FunctionStatement {
pub name: String,
pub parameters: Vec<Parameter>,
pub return_type: Option<String>,
pub body: BlockStatement,
pub attributes: Vec<Attribute>,
pub is_async: bool,
pub exported: bool,
}
impl FunctionStatement {
pub fn new(
name: String,
parameters: Vec<Parameter>,
return_type: Option<String>,
body: BlockStatement,
) -> Self {
Self {
name,
parameters,
return_type,
body,
attributes: Vec::new(),
is_async: false,
exported: false,
}
}
}
#[derive(Debug, Clone)]
pub struct Parameter {
pub name: String,
pub param_type: Option<String>,
}
#[derive(Debug, Clone)]
pub struct FunctionCall {
pub name: String,
pub arguments: Vec<Expression>,
}
#[derive(Debug, Clone)]
pub struct SpawnStatement {
pub agent_name: String,
pub agent_type: Option<String>, pub config: Option<HashMap<String, Expression>>, pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub struct AgentStatement {
pub name: String,
pub agent_type: AgentType,
pub config: HashMap<String, Expression>,
pub capabilities: Vec<String>,
pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub enum AgentType {
AI,
System,
Worker,
IDE,
Custom(String),
}
#[derive(Debug, Clone)]
pub struct MessageStatement {
pub recipient: String,
pub data: HashMap<String, Expression>,
}
#[derive(Debug, Clone)]
pub struct EventStatement {
pub event_name: String,
pub data: HashMap<String, Expression>,
}
#[derive(Debug, Clone)]
pub struct IfStatement {
pub condition: Expression,
pub consequence: BlockStatement,
pub alternative: Option<BlockStatement>,
}
#[derive(Debug, Clone)]
pub struct TryStatement {
pub try_block: BlockStatement,
pub catch_blocks: Vec<CatchBlock>,
pub finally_block: Option<BlockStatement>,
}
#[derive(Debug, Clone)]
pub struct CatchBlock {
pub error_type: Option<String>,
pub error_variable: Option<String>,
pub body: BlockStatement,
}
#[derive(Debug, Clone)]
pub struct ServiceField {
pub name: String,
pub field_type: String,
pub initial_value: Option<Expression>,
pub visibility: FieldVisibility,
}
#[derive(Debug, Clone)]
pub enum FieldVisibility {
Public,
Private,
Internal,
}
#[derive(Debug, Clone)]
pub struct EventDeclaration {
pub name: String,
pub parameters: Vec<Parameter>,
}
#[derive(Debug, Clone)]
pub struct CompilationTargetInfo {
pub target: crate::lexer::tokens::CompilationTarget,
pub constraints: crate::lexer::tokens::TargetConstraint,
pub validation_errors: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ServiceStatement {
pub name: String,
pub attributes: Vec<Attribute>,
pub fields: Vec<ServiceField>,
pub methods: Vec<FunctionStatement>,
pub events: Vec<EventDeclaration>,
pub compilation_target: Option<CompilationTargetInfo>,
pub exported: bool,
}
#[derive(Debug, Clone)]
pub struct Attribute {
pub name: String,
pub parameters: Vec<Expression>,
pub target: AttributeTarget,
}
#[derive(Debug, Clone)]
pub enum AttributeTarget {
Function,
Block,
Variable,
Module,
}
#[derive(Debug, Clone)]
pub enum Expression {
Literal(Literal),
Identifier(String),
BinaryOp(Box<Expression>, Operator, Box<Expression>),
UnaryOp(Operator, Box<Expression>),
Assignment(String, Box<Expression>),
FunctionCall(FunctionCall),
FieldAccess(Box<Expression>, String), FieldAssignment(Box<Expression>, String, Box<Expression>), Await(Box<Expression>),
Spawn(Box<Expression>), Throw(Box<Expression>),
ObjectLiteral(HashMap<String, Expression>), ArrayLiteral(Vec<Expression>), IndexAccess(Box<Expression>, Box<Expression>),
ArrowFunction {
param: String,
body: BlockStatement,
},
Range(Box<Expression>, Box<Expression>),
MethodCall {
receiver: Box<Expression>,
method_name: String,
arguments: Vec<Expression>,
},
}
impl Program {
pub fn new() -> Self {
Self {
statements: Vec::new(),
statement_spans: Vec::new(),
}
}
pub fn add_statement(&mut self, statement: Statement) {
self.add_statement_with_span(statement, None);
}
pub fn add_statement_with_span(&mut self, statement: Statement, span: Option<Span>) {
self.statements.push(statement);
self.statement_spans.push(span);
}
}
impl Default for Program {
fn default() -> Self {
Self::new()
}
}
impl BlockStatement {
pub fn new() -> Self {
Self {
statements: Vec::new(),
}
}
pub fn add_statement(&mut self, statement: Statement) {
self.statements.push(statement);
}
}
impl Default for BlockStatement {
fn default() -> Self {
Self::new()
}
}