2018-06-04 19:25:40 -07:00
|
|
|
use std::rc::Rc;
|
2018-11-16 14:06:04 -08:00
|
|
|
use std::convert::From;
|
|
|
|
|
2019-01-07 13:00:37 -08:00
|
|
|
use crate::builtin::{BinOp, PrefixOp};
|
2019-02-21 01:46:27 -08:00
|
|
|
use crate::typechecking::TypeData;
|
2018-06-04 19:25:40 -07:00
|
|
|
|
2019-01-04 22:58:25 -08:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2019-02-21 01:49:15 -08:00
|
|
|
pub struct Meta<T> {
|
2018-11-20 03:21:10 -08:00
|
|
|
n: T,
|
2019-02-21 01:46:27 -08:00
|
|
|
source_map: SourceMap,
|
|
|
|
type_data: TypeData,
|
2018-11-20 03:21:10 -08:00
|
|
|
}
|
|
|
|
|
2019-02-21 01:49:15 -08:00
|
|
|
impl<T> Meta<T> {
|
|
|
|
pub fn new(n: T) -> Meta<T> {
|
|
|
|
Meta { n, source_map: SourceMap::default(), type_data: TypeData::new() }
|
2019-01-04 22:58:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn node(&self) -> &T {
|
2018-11-20 03:21:10 -08:00
|
|
|
&self.n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 22:58:25 -08:00
|
|
|
//TODO this PartialEq is here to make tests work - find a way to make it not necessary
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
2019-01-05 15:35:51 -08:00
|
|
|
struct SourceMap {
|
2018-11-20 03:21:10 -08:00
|
|
|
}
|
|
|
|
|
2019-02-21 01:49:15 -08:00
|
|
|
impl From<Expression> for Meta<Expression> {
|
|
|
|
fn from(expr: Expression) -> Meta<Expression> {
|
|
|
|
Meta { n: expr, source_map: SourceMap::default(), type_data: TypeData::new() }
|
2019-01-05 15:47:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:25:40 -07:00
|
|
|
#[derive(Debug, PartialEq)]
|
2019-02-21 01:49:15 -08:00
|
|
|
pub struct AST(pub Vec<Meta<Statement>>);
|
2018-06-04 19:25:40 -07:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum Statement {
|
2019-02-21 01:49:15 -08:00
|
|
|
ExpressionStatement(Meta<Expression>),
|
2018-06-04 19:25:40 -07:00
|
|
|
Declaration(Declaration),
|
|
|
|
}
|
|
|
|
|
2019-02-21 01:49:15 -08:00
|
|
|
pub type Block = Vec<Meta<Statement>>;
|
2018-06-04 19:25:40 -07:00
|
|
|
pub type ParamName = Rc<String>;
|
2019-06-16 14:56:52 -07:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct FormalParam {
|
|
|
|
pub name: ParamName,
|
|
|
|
pub default: Option<Expression>,
|
|
|
|
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
|
|
|
|
},
|
2018-06-04 19:25:40 -07:00
|
|
|
TypeAlias(Rc<String>, Rc<String>), //should have TypeSingletonName in it, or maybe just String, not sure
|
|
|
|
Binding {
|
|
|
|
name: Rc<String>,
|
|
|
|
constant: bool,
|
2019-02-20 22:44:45 -08:00
|
|
|
type_anno: Option<TypeIdentifier>,
|
2019-02-21 19:07:07 -08:00
|
|
|
expr: Meta<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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-07-10 18:52:25 -07:00
|
|
|
pub struct Expression {
|
|
|
|
pub kind: ExpressionKind,
|
|
|
|
pub type_anno: Option<TypeIdentifier>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Expression {
|
|
|
|
pub fn new(kind: ExpressionKind) -> Expression {
|
|
|
|
Expression { kind, type_anno: None }
|
|
|
|
}
|
2018-06-04 19:25:40 -07:00
|
|
|
|
2019-07-10 18:52:25 -07:00
|
|
|
pub fn with_anno(kind: ExpressionKind, type_anno: TypeIdentifier) -> Expression {
|
|
|
|
Expression { kind, type_anno: Some(type_anno) }
|
|
|
|
}
|
|
|
|
}
|
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-02-21 01:49:15 -08:00
|
|
|
BinExp(BinOp, Box<Meta<Expression>>, Box<Meta<Expression>>),
|
|
|
|
PrefixExp(PrefixOp, Box<Meta<Expression>>),
|
|
|
|
TupleLiteral(Vec<Meta<Expression>>),
|
2018-06-04 19:25:40 -07:00
|
|
|
Value(Rc<String>),
|
|
|
|
NamedStruct {
|
|
|
|
name: Rc<String>,
|
2019-02-21 19:07:07 -08:00
|
|
|
fields: Vec<(Rc<String>, Meta<Expression>)>,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
|
|
|
Call {
|
2019-02-21 19:07:07 -08:00
|
|
|
f: Box<Meta<Expression>>,
|
2019-06-11 17:20:20 -07:00
|
|
|
arguments: Vec<Meta<InvocationArgument>>,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
|
|
|
Index {
|
2019-02-21 19:07:07 -08:00
|
|
|
indexee: Box<Meta<Expression>>,
|
|
|
|
indexers: Vec<Meta<Expression>>,
|
2018-06-04 19:25:40 -07:00
|
|
|
},
|
2018-06-19 02:05:25 -07:00
|
|
|
IfExpression {
|
|
|
|
discriminator: Box<Discriminator>,
|
|
|
|
body: Box<IfExpressionBody>,
|
|
|
|
},
|
2018-06-04 19:25:40 -07:00
|
|
|
WhileExpression {
|
2019-02-21 19:07:07 -08:00
|
|
|
condition: Option<Box<Meta<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-02-21 19:07:07 -08:00
|
|
|
ListLiteral(Vec<Meta<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 {
|
|
|
|
Positional(Expression),
|
|
|
|
Keyword {
|
|
|
|
name: Rc<String>,
|
|
|
|
expr: Expression,
|
|
|
|
},
|
|
|
|
Ignored
|
|
|
|
}
|
|
|
|
|
2018-06-19 02:05:25 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum Discriminator {
|
|
|
|
Simple(Expression),
|
|
|
|
BinOp(Expression, BinOp)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum IfExpressionBody {
|
|
|
|
SimpleConditional(Block, Option<Block>),
|
2018-06-20 02:07:11 -07:00
|
|
|
SimplePatternMatch(Pattern, Block, Option<Block>),
|
2018-07-13 21:50:38 -07:00
|
|
|
GuardList(Vec<GuardArm>)
|
2018-06-19 02:05:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2018-07-13 21:50:38 -07:00
|
|
|
pub struct GuardArm {
|
|
|
|
pub guard: Guard,
|
|
|
|
pub body: Block,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum Guard {
|
|
|
|
Pat(Pattern),
|
|
|
|
HalfExpr(HalfExpr)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct HalfExpr {
|
|
|
|
pub op: Option<BinOp>,
|
2019-02-21 01:26:51 -08:00
|
|
|
pub expr: ExpressionKind,
|
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),
|
|
|
|
TupleStruct(Rc<String>, Vec<Pattern>),
|
|
|
|
Record(Rc<String>, Vec<(Rc<String>, Pattern)>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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),
|
|
|
|
VarPattern(Rc<String>)
|
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-02-21 19:07:07 -08:00
|
|
|
pub generator: Meta<Expression>,
|
2018-06-04 19:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum ForBody {
|
2019-02-21 19:07:07 -08:00
|
|
|
MonadicReturn(Meta<Expression>),
|
2018-06-04 19:25:40 -07:00
|
|
|
StatementBlock(Block),
|
|
|
|
}
|