use { std::{ boxed::Box, sync::{Arc, RwLock} } }; #[derive(Clone, Debug)] pub enum Statement { Assignment { var_id: String, val_expr: LTExpr }, WhileLoop { condition: LTExpr, body: Vec }, Return(LTExpr), Expr(LTExpr) } #[derive(Clone, Debug)] pub enum LTExpr { Literal { typ: Option< laddertypes::TypeTerm >, val: tisc::VM_Word }, Symbol { typ: Option< laddertypes::TypeTerm >, symbol: String, }, Application { head: Box, body: Vec }, Abstraction { arg_id: String, arg_type: Option< laddertypes::TypeTerm >, val_expr: Box }, Let { name: String, val: Box, body: Box }, Branch { condition: Box, if_expr: Box, else_expr: Box }, Block { statements: Vec } } impl LTExpr { pub fn symbol(str: &str) -> Self { LTExpr::Symbol { typ: None,//typectx.write().unwrap().parse("~Symbol~").expect("parse typeterm"), symbol: String::from(str) } } pub fn lit_uint(val: u64) -> Self { LTExpr::Literal { typ: None,//typectx.write().unwrap().parse("ℤ_2^64~machine::UInt64~machine::Word").expect("parse typeterm"), val: val as tisc::VM_Word } } pub fn abstraction(arg_id: &str, arg_typ: &str, val_expr: LTExpr) -> LTExpr { LTExpr::Abstraction { arg_id: String::from(arg_id), arg_type: None,//typectx.write().unwrap().parse(arg_typ).expect("parse typeterm"), val_expr: Box::new(val_expr) } } pub fn let_expr(name: &str, val: LTExpr, body: LTExpr) -> Self { LTExpr::Let { name: String::from(name), val: Box::new(val), body: Box::new(body) } } pub fn application(head: LTExpr, body: Vec) -> Self { LTExpr::Application { head: Box::new( head ), body: body } } pub fn block(body: Vec) -> Self { LTExpr::Block { statements: body } } } impl Statement { pub fn while_loop(cond: LTExpr, body: Vec) -> Self { Statement::WhileLoop { condition: cond, body } } } /* impl LTExpr { fn get_type(&self, dict: &laddertypes::dict::TypeDict) -> laddertypes::TypeTerm { match self { LTExpr::StringLiteral{ val:_, typ } => { typ.clone() } LTExpr::MemoryLiteral{ val:_, typ } => { typ.clone() } LTExpr::Abstraction{ arg_type, val_expr } => { laddertypes::TypeTerm::App(vec![ laddertypes::TypeTerm::TypeID(dict.get_typeid(&"Fn".into()).expect("expected function type")), arg_type.clone(), val_expr.get_type(dict) ]) } LTExpr::Application{ head, body } => { match head.deref() { LTExpr::Abstraction{ arg_type, val_expr } => { val_expr.get_type(dict) } _ => { panic!("invalid application"); } } } LTExpr::Block{ statements } => { if let Some(last_statement) = statements.last() { match last_statement { Statement::Return(ret_expr) | Statement::Expr(ret_expr) => { ret_expr.get_type(dict) } _ => { laddertypes::TypeTerm::unit() } } } else { laddertypes::TypeTerm::unit() } } } } } */