99 lines
2.1 KiB
Rust
99 lines
2.1 KiB
Rust
#![allow(clippy::enum_variant_names)]
|
|
|
|
use crate::builtin::Builtin;
|
|
use std::rc::Rc;
|
|
|
|
#[derive(Debug)]
|
|
pub struct ReducedAST(pub Vec<Stmt>);
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Stmt {
|
|
PreBinding {
|
|
name: Rc<String>,
|
|
func: Func,
|
|
},
|
|
Binding {
|
|
name: Rc<String>,
|
|
constant: bool,
|
|
expr: Expr,
|
|
},
|
|
Expr(Expr),
|
|
Noop,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Expr {
|
|
Lit(Lit),
|
|
Sym(Rc<String>), //a Sym is anything that can be looked up by name at runtime - i.e. a function or variable address
|
|
Tuple(Vec<Expr>),
|
|
Func(Func),
|
|
Constructor {
|
|
type_name: Rc<String>,
|
|
name: Rc<String>,
|
|
tag: usize,
|
|
arity: usize, // n.b. arity here is always the value from the symbol table - if it doesn't match what it's being called with, that's an eval error, eval will handle it
|
|
},
|
|
Call {
|
|
f: Box<Expr>,
|
|
args: Vec<Expr>,
|
|
},
|
|
Assign {
|
|
val: Box<Expr>, //TODO this probably can't be a val
|
|
expr: Box<Expr>,
|
|
},
|
|
Conditional {
|
|
cond: Box<Expr>,
|
|
then_clause: Vec<Stmt>,
|
|
else_clause: Vec<Stmt>,
|
|
},
|
|
ConditionalTargetSigilValue,
|
|
CaseMatch {
|
|
cond: Box<Expr>,
|
|
alternatives: Vec<Alternative>,
|
|
},
|
|
UnimplementedSigilValue,
|
|
ReductionError(String),
|
|
}
|
|
|
|
impl Expr {
|
|
// The unit value is an empty tuple
|
|
pub fn unit() -> Expr {
|
|
Expr::Tuple(vec![])
|
|
}
|
|
}
|
|
|
|
pub type BoundVars = Vec<Option<Rc<String>>>; //remember that order matters here
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Alternative {
|
|
pub matchable: Subpattern,
|
|
pub item: Vec<Stmt>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Subpattern {
|
|
pub tag: Option<usize>,
|
|
pub subpatterns: Vec<Option<Subpattern>>,
|
|
pub bound_vars: BoundVars,
|
|
pub guard: Option<Expr>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Lit {
|
|
Nat(u64),
|
|
Int(i64),
|
|
Float(f64),
|
|
Bool(bool),
|
|
StringLit(Rc<String>),
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Func {
|
|
BuiltIn(Builtin),
|
|
UserDefined {
|
|
name: Option<Rc<String>>,
|
|
params: Vec<Rc<String>>,
|
|
body: Vec<Stmt>,
|
|
},
|
|
}
|