159 lines
4.1 KiB
Rust
159 lines
4.1 KiB
Rust
|
||
use {
|
||
std::{
|
||
boxed::Box,
|
||
sync::{Arc, RwLock}
|
||
}
|
||
};
|
||
|
||
#[derive(Clone, Debug)]
|
||
pub enum Statement {
|
||
Assignment {
|
||
var_id: String,
|
||
val_expr: LTExpr
|
||
},
|
||
LetAssign {
|
||
typ: Option<TypeTag>,
|
||
var_id: String,
|
||
val_expr: LTExpr,
|
||
},
|
||
WhileLoop {
|
||
condition: LTExpr,
|
||
body: Vec<Statement>
|
||
},
|
||
Return(LTExpr),
|
||
Expr(LTExpr)
|
||
}
|
||
|
||
#[derive(Clone, Debug)]
|
||
pub enum TypeError {
|
||
ParseError(laddertypes::parser::ParseError),
|
||
Mismatch {
|
||
expected: laddertypes::TypeTerm,
|
||
received: laddertypes::TypeTerm
|
||
}
|
||
}
|
||
|
||
pub type TypeTag = Result< laddertypes::TypeTerm, TypeError >;
|
||
|
||
#[derive(Clone, Debug)]
|
||
pub enum LTExpr {
|
||
Literal {
|
||
typ: Option<TypeTag>,
|
||
val: tisc::VM_Word
|
||
},
|
||
Symbol {
|
||
typ: Option<TypeTag>,
|
||
symbol: String,
|
||
},
|
||
Application {
|
||
typ: Option<TypeTag>,
|
||
head: Box<LTExpr>,
|
||
body: Vec<LTExpr>
|
||
},
|
||
Abstraction {
|
||
args: Vec<(String, Option<TypeTag>)>,
|
||
body: Box<LTExpr>
|
||
},
|
||
Branch {
|
||
condition: Box<LTExpr>,
|
||
if_expr: Box<LTExpr>,
|
||
else_expr: Box<LTExpr>
|
||
},
|
||
Block {
|
||
statements: Vec<Statement>
|
||
}
|
||
}
|
||
|
||
impl LTExpr {
|
||
pub fn symbol(str: &str) -> Self {
|
||
LTExpr::Symbol {
|
||
typ: None,//typectx.write().unwrap().parse("<Ref memory::Word>~Symbol~<Seq Char>").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(args: Vec<(&str, &str)>, body: LTExpr) -> LTExpr {
|
||
LTExpr::Abstraction {
|
||
args: args.into_iter().map(|(arg_name, arg_type)|
|
||
( arg_name.into(), None )
|
||
//typectx.write().unwrap().parse(t).expect("parse typeterm")
|
||
).collect(),
|
||
body: Box::new(body)
|
||
}
|
||
}
|
||
|
||
pub fn application(head: LTExpr, body: Vec<LTExpr>) -> Self {
|
||
LTExpr::Application {
|
||
typ: None,
|
||
head: Box::new( head ),
|
||
body: body
|
||
}
|
||
}
|
||
|
||
pub fn block(body: Vec<Statement>) -> Self {
|
||
LTExpr::Block {
|
||
statements: body
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Statement {
|
||
pub fn while_loop(cond: LTExpr, body: Vec<Statement>) -> 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()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
*/
|
||
|