diff options
Diffstat (limited to 'src/ast.rs')
-rw-r--r-- | src/ast.rs | 73 |
1 files changed, 63 insertions, 10 deletions
@@ -1,3 +1,5 @@ +use std::ops::Deref; + #[derive(Debug)] pub struct WithLocation<T> { pub value: T, @@ -35,6 +37,14 @@ impl<T> WithLocation<T> { } } +impl<T> Deref for WithLocation<T> { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.value + } +} + #[derive(Debug)] pub struct Program(pub Box<WithLocation<Expression>>); @@ -64,7 +74,7 @@ pub enum BinOp { #[derive(Debug)] pub enum Expression { - Let(Vec<WithLocation<VarDec>>, Box<WithLocation<Expression>>), + Let(Vec<WithLocation<Declaration>>, Box<WithLocation<Expression>>), UnaryOp(UnaryOp, Box<WithLocation<Expression>>), BinOp(BinOp, Box<WithLocation<Expression>>, Box<WithLocation<Expression>>), Number(u64), @@ -74,19 +84,62 @@ pub enum Expression { } #[derive(Debug)] -pub struct VarDec { +pub struct RecordField { + name: WithLocation<String>, + ty: WithLocation<String>, +} + +impl RecordField { + pub fn new(name: WithLocation<String>, ty: WithLocation<String>) -> RecordField { + RecordField { name, ty } + } +} + +#[derive(Debug)] +pub enum Ty { + Name(String), + Array(Box<Ty>), + Record(Vec<WithLocation<RecordField>>), +} + +#[derive(Debug)] +pub struct Declaration { pub name: WithLocation<String>, - pub type_: Option<String>, - pub value: Box<WithLocation<Expression>>, + pub declaration: DeclarationBody, +} + +#[derive(Debug)] +pub enum DeclarationBody { + Var { + ty: Option<WithLocation<Ty>>, + value: Box<WithLocation<Expression>>, + }, + Ty { + ty: WithLocation<Ty>, + }, + Fun { + ty: Box<WithLocation<Ty>>, + }, } -impl VarDec { - pub fn new(name: WithLocation<String>, type_: Option<String>, - value: Box<WithLocation<Expression>>) -> VarDec { - VarDec { +impl Declaration { + pub fn new_var(name: WithLocation<String>, type_: Option<WithLocation<Ty>>, + value: Box<WithLocation<Expression>>) -> Declaration { + Declaration { name: name, - type_: type_, - value: value, + declaration: DeclarationBody::Var { + ty: type_, + value: value, + } + } + } + + pub fn new_ty(name: WithLocation<String>, type_: WithLocation<Ty>) -> Declaration { + Declaration { + name: name, + declaration: DeclarationBody::Ty { + ty: type_, + } } } } |