2018-06-04 19:25:40 -07:00
|
|
|
use std::rc::Rc;
|
2018-11-16 14:06:04 -08:00
|
|
|
|
2019-09-18 02:15:45 -07:00
|
|
|
use crate::derivative::Derivative;
|
2018-06-04 19:25:40 -07:00
|
|
|
|
2019-09-26 02:29:20 -07:00
|
|
|
mod walker;
|
2019-09-25 18:41:07 -07:00
|
|
|
mod visitor;
|
2019-09-26 02:29:20 -07:00
|
|
|
mod visitor_test;
|
2019-08-14 07:25:45 -07:00
|
|
|
mod operators;
|
|
|
|
pub use operators::*;
|
2019-10-15 19:02:48 -07:00
|
|
|
pub use visitor::ASTVisitor;
|
|
|
|
pub use walker::walk_ast;
|
2019-08-14 07:25:45 -07:00
|
|
|
|
2019-09-18 01:51:23 -07:00
|
|
|
/// An abstract identifier for an AST node
|
2019-09-19 02:58:52 -07:00
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
2019-09-18 01:51:23 -07:00
|
|
|
pub struct ItemId {
|
|
|
|
idx: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemId {
|
2019-09-18 09:56:11 -07:00
|
|
|
fn new(n: u32) -> ItemId {
|
2019-09-18 01:51:23 -07:00
|
|
|
ItemId { idx: n }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 09:56:11 -07:00
|
|
|
pub struct ItemIdStore {
|
|
|
|
last_idx: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemIdStore {
|
|
|
|
pub fn new() -> ItemIdStore {
|
|
|
|
ItemIdStore { last_idx: 0 }
|
|
|
|
}
|
|
|
|
/// Always returns an ItemId with internal value zero
|
2019-09-18 14:15:05 -07:00
|
|
|
#[cfg(test)]
|
2019-09-18 09:56:11 -07:00
|
|
|
pub fn new_id() -> ItemId {
|
|
|
|
ItemId { idx: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This limits the size of the AST to 2^32 tree elements
|
|
|
|
pub fn fresh(&mut self) -> ItemId {
|
|
|
|
let idx = self.last_idx;
|
|
|
|
self.last_idx += 1;
|
|
|
|
ItemId::new(idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 02:15:45 -07:00
|
|
|
#[derive(Derivative, Debug)]
|
|
|
|
#[derivative(PartialEq)]
|
2019-09-11 19:06:00 -07:00
|
|
|
pub struct AST {
|
2019-09-18 02:15:45 -07:00
|
|
|
#[derivative(PartialEq="ignore")]
|
|
|
|
pub id: ItemId,
|
2019-09-20 02:21:39 -07:00
|
|
|
pub statements: Vec<Statement>
|
2019-09-11 19:06:00 -07:00
|
|
|
}
|
2018-06-04 19:25:40 -07:00
|
|
|
|
2019-09-18 10:07:20 -07:00
|
|
|
#[derive(Derivative, Debug, Clone)]
|
|
|
|
#[derivative(PartialEq)]
|
2019-09-17 02:25:11 -07:00
|
|
|
pub struct Statement {
|
2019-09-18 10:07:20 -07:00
|
|
|
#[derivative(PartialEq="ignore")]
|
|
|
|
pub id: ItemId,
|
2019-09-17 02:25:11 -07:00
|
|
|
pub kind: StatementKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum StatementKind {
|
2019-09-20 02:21:39 -07:00
|
|
|
Expression(Expression),
|
2019-09-21 02:30:28 -07:00
|
|
|
Declaration(Declaration),
|
|
|
|
Import(ImportSpecifier),
|
2019-10-22 03:15:14 -07:00
|
|
|
Module(ModuleSpecifier),
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-09-20 02:21:39 -07:00
|
|
|
pub type Block = Vec<Statement>;
|
2018-06-04 19:25:40 -07:00
|
|
|
pub type ParamName = Rc<String>;
|
2019-06-16 14:56:52 -07:00
|
|
|
|
2019-09-19 01:34:21 -07:00
|
|
|
#[derive(Debug, Derivative, Clone)]
|
|
|
|
#[derivative(PartialEq)]
|
|
|
|
pub struct QualifiedName {
|
|
|
|
#[derivative(PartialEq="ignore")]
|
|
|
|
pub id: ItemId,
|
|
|
|
pub components: Vec<Rc<String>>,
|
|
|
|
}
|
2019-08-31 23:39:01 -07:00
|
|
|
|
2019-06-16 14:56:52 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct FormalParam {
|
|
|
|
pub name: ParamName,
|
2019-09-20 02:21:39 -07:00
|
|
|
pub default: Option<Expression>,
|
2019-06-16 14:56:52 -07:00
|
|
|
pub anno: Option<TypeIdentifier>
|
|
|
|
}
|
2018-06-04 19:25:40 -07:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum Declaration {
|
|
|
|
FuncSig(Signature),
|
|
|
|
FuncDecl(Signature, Block),
|
2018-07-12 02:07:52 -07:00
|
|
|
TypeDecl {
|
|
|
|
name: TypeSingletonName,
|
|
|
|
body: TypeBody,
|
|
|
|
mutable: bool
|
|
|
|
},
|
2019-09-28 02:42:18 -07:00
|
|
|
//TODO this needs to be more sophisticated
|
|
|
|
TypeAlias {
|
|
|
|
alias: Rc<String>,
|
|
|
|
original: Rc<String>,
|
|
|
|
},
|
2018-06-04 19:25:40 -07:00
|
|
|
Binding {
|
|
|
|
name: Rc<String>,
|
|
|
|
constant: bool,
|
2019-02-20 22:44:45 -08:00
|
|
|
type_anno: Option<TypeIdentifier>,
|
2019-09-20 02:21:39 -07:00
|
|
|
expr: Expression,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
|
|
|
Impl {
|
2018-10-18 13:27:09 -07:00
|
|
|
type_name: TypeIdentifier,
|
2019-01-08 00:51:56 -08:00
|
|
|
interface_name: Option<TypeSingletonName>,
|
2018-06-04 19:25:40 -07:00
|
|
|
block: Vec<Declaration>,
|
|
|
|
},
|
|
|
|
Interface {
|
|
|
|
name: Rc<String>,
|
|
|
|
signatures: Vec<Signature>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct Signature {
|
|
|
|
pub name: Rc<String>,
|
2018-08-24 16:49:59 -07:00
|
|
|
pub operator: bool,
|
2018-06-04 19:25:40 -07:00
|
|
|
pub params: Vec<FormalParam>,
|
2018-10-18 13:27:09 -07:00
|
|
|
pub type_anno: Option<TypeIdentifier>,
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct TypeBody(pub Vec<Variant>);
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum Variant {
|
|
|
|
UnitStruct(Rc<String>),
|
2018-10-18 13:27:09 -07:00
|
|
|
TupleStruct(Rc<String>, Vec<TypeIdentifier>),
|
2019-01-24 20:47:20 -08:00
|
|
|
Record {
|
|
|
|
name: Rc<String>,
|
|
|
|
members: Vec<(Rc<String>, TypeIdentifier)>,
|
|
|
|
}
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-09-18 14:15:05 -07:00
|
|
|
#[derive(Debug, Derivative, Clone)]
|
|
|
|
#[derivative(PartialEq)]
|
2019-07-10 18:52:25 -07:00
|
|
|
pub struct Expression {
|
2019-09-18 14:15:05 -07:00
|
|
|
#[derivative(PartialEq="ignore")]
|
2019-09-18 10:09:33 -07:00
|
|
|
pub id: ItemId,
|
2019-07-10 18:52:25 -07:00
|
|
|
pub kind: ExpressionKind,
|
|
|
|
pub type_anno: Option<TypeIdentifier>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Expression {
|
2019-09-18 10:09:33 -07:00
|
|
|
pub fn new(id: ItemId, kind: ExpressionKind) -> Expression {
|
|
|
|
Expression { id, kind, type_anno: None }
|
2019-07-10 18:52:25 -07:00
|
|
|
}
|
2018-06-04 19:25:40 -07:00
|
|
|
|
2019-09-18 10:09:33 -07:00
|
|
|
pub fn with_anno(id: ItemId, kind: ExpressionKind, type_anno: TypeIdentifier) -> Expression {
|
|
|
|
Expression { id, kind, type_anno: Some(type_anno) }
|
2019-07-10 18:52:25 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 14:06:04 -08:00
|
|
|
|
2018-06-04 19:25:40 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2018-10-18 13:27:09 -07:00
|
|
|
pub enum TypeIdentifier {
|
|
|
|
Tuple(Vec<TypeIdentifier>),
|
2018-06-04 19:25:40 -07:00
|
|
|
Singleton(TypeSingletonName)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct TypeSingletonName {
|
|
|
|
pub name: Rc<String>,
|
2018-10-18 13:27:09 -07:00
|
|
|
pub params: Vec<TypeIdentifier>,
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-02-21 01:26:51 -08:00
|
|
|
pub enum ExpressionKind {
|
2018-06-04 19:25:40 -07:00
|
|
|
NatLiteral(u64),
|
|
|
|
FloatLiteral(f64),
|
|
|
|
StringLiteral(Rc<String>),
|
|
|
|
BoolLiteral(bool),
|
2019-09-20 02:21:39 -07:00
|
|
|
BinExp(BinOp, Box<Expression>, Box<Expression>),
|
|
|
|
PrefixExp(PrefixOp, Box<Expression>),
|
|
|
|
TupleLiteral(Vec<Expression>),
|
2019-09-20 01:57:48 -07:00
|
|
|
Value(QualifiedName),
|
2018-06-04 19:25:40 -07:00
|
|
|
NamedStruct {
|
2019-09-20 02:21:39 -07:00
|
|
|
name: QualifiedName,
|
|
|
|
fields: Vec<(Rc<String>, Expression)>,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
|
|
|
Call {
|
2019-09-20 02:21:39 -07:00
|
|
|
f: Box<Expression>,
|
2019-09-02 14:41:09 -07:00
|
|
|
arguments: Vec<InvocationArgument>,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
|
|
|
Index {
|
2019-09-20 02:21:39 -07:00
|
|
|
indexee: Box<Expression>,
|
|
|
|
indexers: Vec<Expression>,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
2018-06-19 02:05:25 -07:00
|
|
|
IfExpression {
|
2019-10-10 03:29:28 -07:00
|
|
|
discriminator: Option<Box<Expression>>,
|
2019-10-10 10:34:54 -07:00
|
|
|
body: Box<IfExpressionBody>,
|
2018-06-19 02:05:25 -07:00
|
|
|
},
|
2018-06-04 19:25:40 -07:00
|
|
|
WhileExpression {
|
2019-09-20 02:21:39 -07:00
|
|
|
condition: Option<Box<Expression>>,
|
2018-06-04 19:25:40 -07:00
|
|
|
body: Block,
|
|
|
|
},
|
|
|
|
ForExpression {
|
|
|
|
enumerators: Vec<Enumerator>,
|
|
|
|
body: Box<ForBody>,
|
|
|
|
},
|
|
|
|
Lambda {
|
|
|
|
params: Vec<FormalParam>,
|
2018-11-05 19:10:34 -08:00
|
|
|
type_anno: Option<TypeIdentifier>,
|
2018-06-04 19:25:40 -07:00
|
|
|
body: Block,
|
|
|
|
},
|
2019-09-20 02:21:39 -07:00
|
|
|
ListLiteral(Vec<Expression>),
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
2018-11-16 14:06:04 -08:00
|
|
|
|
2019-06-11 17:20:20 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum InvocationArgument {
|
2019-09-20 02:21:39 -07:00
|
|
|
Positional(Expression),
|
2019-06-11 17:20:20 -07:00
|
|
|
Keyword {
|
|
|
|
name: Rc<String>,
|
2019-09-20 02:21:39 -07:00
|
|
|
expr: Expression,
|
2019-06-11 17:20:20 -07:00
|
|
|
},
|
|
|
|
Ignored
|
|
|
|
}
|
|
|
|
|
2018-06-19 02:05:25 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum IfExpressionBody {
|
2019-10-10 03:29:28 -07:00
|
|
|
SimpleConditional {
|
|
|
|
then_case: Block,
|
|
|
|
else_case: Option<Block>
|
|
|
|
},
|
|
|
|
SimplePatternMatch {
|
|
|
|
pattern: Pattern,
|
|
|
|
then_case: Block,
|
|
|
|
else_case: Option<Block>
|
|
|
|
},
|
|
|
|
CondList(Vec<ConditionArm>)
|
2018-06-19 02:05:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-10-10 03:29:28 -07:00
|
|
|
pub struct ConditionArm {
|
|
|
|
pub condition: Condition,
|
|
|
|
pub guard: Option<Expression>,
|
2018-07-13 21:50:38 -07:00
|
|
|
pub body: Block,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-10-10 03:29:28 -07:00
|
|
|
pub enum Condition {
|
|
|
|
Pattern(Pattern),
|
2019-10-10 10:34:54 -07:00
|
|
|
TruncatedOp(BinOp, Expression),
|
|
|
|
Expression(Expression),
|
2019-10-10 03:29:28 -07:00
|
|
|
Else,
|
2018-06-19 02:05:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2018-07-01 01:26:19 -07:00
|
|
|
pub enum Pattern {
|
2018-07-10 03:43:14 -07:00
|
|
|
Ignored,
|
2018-07-01 01:26:19 -07:00
|
|
|
TuplePattern(Vec<Pattern>),
|
|
|
|
Literal(PatternLiteral),
|
2019-09-20 02:03:10 -07:00
|
|
|
TupleStruct(QualifiedName, Vec<Pattern>),
|
|
|
|
Record(QualifiedName, Vec<(Rc<String>, Pattern)>),
|
|
|
|
VarOrName(QualifiedName),
|
2018-07-01 01:26:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum PatternLiteral {
|
2018-08-21 19:57:45 -07:00
|
|
|
NumPattern {
|
|
|
|
neg: bool,
|
2019-02-21 01:26:51 -08:00
|
|
|
num: ExpressionKind,
|
2018-08-21 19:57:45 -07:00
|
|
|
},
|
2018-07-01 01:26:19 -07:00
|
|
|
StringPattern(Rc<String>),
|
|
|
|
BoolPattern(bool),
|
2018-06-19 02:05:25 -07:00
|
|
|
}
|
2018-06-04 19:25:40 -07:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct Enumerator {
|
|
|
|
pub id: Rc<String>,
|
2019-09-20 02:05:57 -07:00
|
|
|
pub generator: Expression,
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum ForBody {
|
2019-09-20 02:05:57 -07:00
|
|
|
MonadicReturn(Expression),
|
2018-06-04 19:25:40 -07:00
|
|
|
StatementBlock(Block),
|
|
|
|
}
|
2019-09-21 02:30:28 -07:00
|
|
|
|
|
|
|
#[derive(Debug, Derivative, Clone)]
|
|
|
|
#[derivative(PartialEq)]
|
|
|
|
pub struct ImportSpecifier {
|
|
|
|
#[derivative(PartialEq="ignore")]
|
|
|
|
pub id: ItemId,
|
|
|
|
pub path_components: Vec<Rc<String>>,
|
|
|
|
pub imported_names: ImportedNames
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum ImportedNames {
|
|
|
|
All,
|
|
|
|
LastOfPath,
|
|
|
|
List(Vec<Rc<String>>)
|
|
|
|
}
|
|
|
|
|
2019-10-22 03:15:14 -07:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct ModuleSpecifier {
|
|
|
|
pub name: Rc<String>,
|
|
|
|
pub contents: Vec<Statement>,
|
|
|
|
}
|
|
|
|
|