use std::rc::Rc; use std::convert::From; use crate::builtin::{BinOp, PrefixOp}; use crate::typechecking::TypeData; #[derive(Clone, Debug, PartialEq)] pub struct Meta { n: T, source_map: SourceMap, type_data: TypeData, } impl Meta { pub fn new(n: T) -> Meta { Meta { n, source_map: SourceMap::default(), type_data: TypeData::new() } } pub fn node(&self) -> &T { &self.n } } //TODO this PartialEq is here to make tests work - find a way to make it not necessary #[derive(Clone, Debug, Default, PartialEq)] struct SourceMap { } impl From for Meta { fn from(expr: Expression) -> Meta { Meta { n: expr, source_map: SourceMap::default(), type_data: TypeData::new() } } } #[derive(Debug, PartialEq)] pub struct AST(pub Vec>); #[derive(Debug, PartialEq, Clone)] pub enum Statement { ExpressionStatement(Meta), Declaration(Declaration), } pub type Block = Vec>; pub type ParamName = Rc; pub type FormalParam = (ParamName, Option); #[derive(Debug, PartialEq, Clone)] pub enum Declaration { FuncSig(Signature), FuncDecl(Signature, Block), TypeDecl { name: TypeSingletonName, body: TypeBody, mutable: bool }, TypeAlias(Rc, Rc), //should have TypeSingletonName in it, or maybe just String, not sure Binding { name: Rc, constant: bool, type_anno: Option, expr: Meta, }, Impl { type_name: TypeIdentifier, interface_name: Option, block: Vec, }, Interface { name: Rc, signatures: Vec } } #[derive(Debug, PartialEq, Clone)] pub struct Signature { pub name: Rc, pub operator: bool, pub params: Vec, pub type_anno: Option, } #[derive(Debug, PartialEq, Clone)] pub struct TypeBody(pub Vec); #[derive(Debug, PartialEq, Clone)] pub enum Variant { UnitStruct(Rc), TupleStruct(Rc, Vec), Record { name: Rc, members: Vec<(Rc, TypeIdentifier)>, } } #[derive(Debug, PartialEq, Clone)] pub struct Expression(pub ExpressionKind, pub Option); #[derive(Debug, PartialEq, Clone)] pub enum TypeIdentifier { Tuple(Vec), Singleton(TypeSingletonName) } #[derive(Debug, PartialEq, Clone)] pub struct TypeSingletonName { pub name: Rc, pub params: Vec, } #[derive(Debug, PartialEq, Clone)] pub enum ExpressionKind { NatLiteral(u64), FloatLiteral(f64), StringLiteral(Rc), BoolLiteral(bool), BinExp(BinOp, Box>, Box>), PrefixExp(PrefixOp, Box>), TupleLiteral(Vec>), Value(Rc), NamedStruct { name: Rc, fields: Vec<(Rc, Meta)>, }, Call { f: Box>, arguments: Vec>, }, Index { indexee: Box>, indexers: Vec>, }, IfExpression { discriminator: Box, body: Box, }, WhileExpression { condition: Option>>, body: Block, }, ForExpression { enumerators: Vec, body: Box, }, Lambda { params: Vec, type_anno: Option, body: Block, }, ListLiteral(Vec>), } #[derive(Debug, PartialEq, Clone)] pub enum Discriminator { Simple(Expression), BinOp(Expression, BinOp) } #[derive(Debug, PartialEq, Clone)] pub enum IfExpressionBody { SimpleConditional(Block, Option), SimplePatternMatch(Pattern, Block, Option), GuardList(Vec) } #[derive(Debug, PartialEq, Clone)] 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, pub expr: ExpressionKind, } #[derive(Debug, PartialEq, Clone)] pub enum Pattern { Ignored, TuplePattern(Vec), Literal(PatternLiteral), TupleStruct(Rc, Vec), Record(Rc, Vec<(Rc, Pattern)>), } #[derive(Debug, PartialEq, Clone)] pub enum PatternLiteral { NumPattern { neg: bool, num: ExpressionKind, }, StringPattern(Rc), BoolPattern(bool), VarPattern(Rc) } #[derive(Debug, PartialEq, Clone)] pub struct Enumerator { pub id: Rc, pub generator: Meta, } #[derive(Debug, PartialEq, Clone)] pub enum ForBody { MonadicReturn(Meta), StatementBlock(Block), }