Files
rscript/src/ast.rs
T
2026-11-15 12:34:33 +01:00

133 lines
2.1 KiB
Rust

use crate::span::Span;
#[derive(Debug, PartialEq)]
pub enum Stmt {
Let(Let),
Assign(Assign),
Expr(Expr),
Block(Block),
Fn(FnDef),
}
impl From<Let> for Stmt {
fn from(let_stmt: Let) -> Self {
Stmt::Let(let_stmt)
}
}
impl From<Assign> for Stmt {
fn from(assign_stmt: Assign) -> Self {
Stmt::Assign(assign_stmt)
}
}
impl From<Expr> for Stmt {
fn from(expr_stmt: Expr) -> Self {
Stmt::Expr(expr_stmt)
}
}
impl From<Block> for Stmt {
fn from(block: Block) -> Self {
Stmt::Block(block)
}
}
impl From<FnDef> for Stmt {
fn from(fn_def: FnDef) -> Self {
Stmt::Fn(fn_def)
}
}
#[derive(Debug, PartialEq)]
pub struct Let {
pub name: Ident,
pub ty: Option<Type>,
pub value: Expr,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct Assign {
pub name: Ident,
pub value: Expr,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct FnDef {
pub name: Ident,
pub params: Vec<FnParam>,
pub body: Vec<Stmt>,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct Block {
pub stmts: Vec<Stmt>,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct FnParam {
pub name: Ident,
pub ty: Type,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub enum Expr {
Literal(Literal),
Variable(Ident),
FnCall(FnCall),
}
#[derive(Debug, PartialEq)]
pub enum Literal {
Number(LitNumber),
String(LitString),
}
impl From<LitNumber> for Literal {
fn from(lit: LitNumber) -> Self {
Literal::Number(lit)
}
}
impl From<LitString> for Literal {
fn from(lit: LitString) -> Self {
Literal::String(lit)
}
}
#[derive(Debug, PartialEq)]
pub struct LitNumber {
pub value: f64,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct LitString {
pub value: String,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct FnCall {
pub name: Ident,
pub args: Vec<Expr>,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct Ident {
pub name: String,
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct Type {
pub name: String,
pub span: Span,
}