mul/src/syntax.rs

92 lines
1.9 KiB
Rust

//! Syntax elements.
/// An identifier.
pub type Name = String;
/// The rough equivalent of a module.
pub type Program = Vec<Item>;
/// A fragment of code entered in, e.g., the REPL.
#[derive(Debug, Clone)]
pub enum InteractiveEntry {
Item(Item),
Binding(Binding),
Expression(Expression),
}
/// Surface syntax types.
#[derive(Debug, Clone)]
pub enum Type {
/// A type variable.
Variable(Name),
/// A function type.
Function {
/// Parameter types.
parameters: Vec<Type>,
/// The return type.
result: Box<Type>,
},
/// A type constructor.
Constructor(Name),
/// Special typing sentinel for the print builtin.
Print,
/// Special typing sentinel for the equals builtin.
Equals,
}
/// A top level definition.
#[derive(Debug, Clone)]
pub enum Item {
Fn {
name: Name,
parameters: Vec<Parameter>,
return_type: Option<Type>,
body: Block,
},
}
/// Function parameter with an optional type.
#[derive(Debug, Clone)]
pub struct Parameter {
pub name: Name,
pub annotation: Option<Type>,
}
/// Syntactic mapping from an identifier to an expression.
#[derive(Debug, Clone)]
pub struct Binding {
pub name: Name,
pub annotation: Option<Type>,
pub expression: Expression,
}
/// Some individually addressable syntax node, either atomic or compound.
#[derive(Debug, Clone)]
pub enum Expression {
Variable(Name),
Block(Box<Block>),
Lambda {
parameters: Vec<Parameter>,
return_type: Option<Type>,
result: Box<Expression>,
},
Call {
callee: Box<Expression>,
arguments: Vec<Expression>,
},
Annotation {
expression: Box<Expression>,
annotation: Type,
},
Boolean(bool),
Integer(i64),
Unit,
}
/// A sequence of bindings with a final result.
#[derive(Debug, Clone)]
pub struct Block {
pub bindings: Vec<Binding>,
pub result: Expression,
}