schala/schala-lang/language/src/ast.rs

217 lines
4.6 KiB
Rust
Raw Normal View History

2018-06-04 19:25:40 -07:00
use std::rc::Rc;
2018-11-16 14:06:04 -08:00
use std::convert::From;
use source_map::{SourceMap};
2018-06-04 19:25:40 -07:00
use builtin::{BinOp, PrefixOp};
2019-01-04 22:58:25 -08:00
#[derive(Clone, Debug, PartialEq)]
2018-11-20 03:21:10 -08:00
pub struct Node<T> {
n: T,
meta: Meta
}
impl<T> Node<T> {
2019-01-04 22:58:25 -08:00
pub fn new(n: T) -> Node<T> {
Node { n, meta: Meta::default() }
}
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)]
2018-11-20 03:21:10 -08:00
struct Meta {
}
2018-06-04 19:25:40 -07:00
#[derive(Debug, PartialEq)]
2019-01-04 22:58:25 -08:00
pub struct AST(pub Vec<Node<Statement>>);
2018-06-04 19:25:40 -07:00
#[derive(Debug, PartialEq, Clone)]
pub enum Statement {
ExpressionStatement(Expression),
Declaration(Declaration),
}
2019-01-04 22:58:25 -08:00
pub type Block = Vec<Node<Statement>>;
2018-06-04 19:25:40 -07:00
pub type ParamName = Rc<String>;
pub type InterfaceName = Rc<String>; //should be a singleton I think??
2018-10-18 13:27:09 -07:00
pub type FormalParam = (ParamName, 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,
expr: Expression,
},
Impl {
2018-10-18 13:27:09 -07:00
type_name: TypeIdentifier,
2018-06-04 19:25:40 -07:00
interface_name: Option<InterfaceName>,
block: Vec<Declaration>,
},
Interface {
name: Rc<String>,
signatures: Vec<Signature>
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Signature {
pub name: Rc<String>,
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>),
Record(Rc<String>, Vec<(Rc<String>, TypeIdentifier)>),
2018-06-04 19:25:40 -07:00
}
#[derive(Debug, PartialEq, Clone)]
2018-10-18 13:27:09 -07:00
pub struct Expression(pub ExpressionType, pub Option<TypeIdentifier>);
2018-06-04 19:25:40 -07:00
2018-11-16 14:06:04 -08:00
impl From<Expression> for SourceMap<Expression> {
fn from(node: Expression) -> Self {
SourceMap { node, data: None }
}
}
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)]
pub enum ExpressionType {
NatLiteral(u64),
FloatLiteral(f64),
StringLiteral(Rc<String>),
BoolLiteral(bool),
BinExp(BinOp, Box<Expression>, Box<Expression>),
PrefixExp(PrefixOp, Box<Expression>),
TupleLiteral(Vec<Expression>),
Value(Rc<String>),
NamedStruct {
name: Rc<String>,
fields: Vec<(Rc<String>, Expression)>,
},
Call {
f: Box<Expression>,
arguments: Vec<Expression>,
},
Index {
indexee: Box<Expression>,
indexers: Vec<Expression>,
},
IfExpression {
discriminator: Box<Discriminator>,
body: Box<IfExpressionBody>,
},
2018-06-04 19:25:40 -07:00
WhileExpression {
condition: Option<Box<Expression>>,
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,
},
ListLiteral(Vec<Expression>),
}
2018-11-16 14:06:04 -08:00
impl From<ExpressionType> for SourceMap<ExpressionType> {
fn from(node: ExpressionType) -> Self {
SourceMap { node, data: None }
}
}
#[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>)
}
#[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>,
pub expr: ExpressionType,
}
#[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,
num: ExpressionType,
},
2018-07-01 01:26:19 -07:00
StringPattern(Rc<String>),
BoolPattern(bool),
VarPattern(Rc<String>)
}
2018-06-04 19:25:40 -07:00
#[derive(Debug, PartialEq, Clone)]
pub struct Enumerator {
pub id: Rc<String>,
pub generator: Expression,
}
#[derive(Debug, PartialEq, Clone)]
pub enum ForBody {
MonadicReturn(Expression),
StatementBlock(Block),
}