lt-core/src/expr.rs

159 lines
4.3 KiB
Rust
Raw Normal View History

2024-05-05 18:19:28 +02:00
use {
std::{
boxed::Box,
sync::{Arc, RwLock}
}
};
#[derive(Debug)]
pub enum Statement {
Assignment {
var_id: String,
val_expr: LTExpr
},
WhileLoop {
condition: Box<LTExpr>,
body: Vec<Statement>
},
Return(LTExpr),
Expr(LTExpr)
}
#[derive(Debug)]
pub enum LTExpr {
SymbolLiteral {
typ: laddertypes::TypeTerm,
symbol: String,
},
WordLiteral {
typ: laddertypes::TypeTerm,
val: tisc::VM_Word
},
CallLiteral {
typ: laddertypes::TypeTerm,
result_size: usize,
val: tisc::VM_Word
},
Application {
head: Box<LTExpr>,
body: Vec<LTExpr>
},
Abstraction {
arg_id: String,
arg_type: laddertypes::TypeTerm,
val_expr: Box<LTExpr>
},
Branch {
condition: Box<LTExpr>,
if_expr: Box<LTExpr>,
else_expr: Box<LTExpr>
},
Block {
statements: Vec<Statement>
}
}
impl LTExpr {
pub fn var_symbol(typectx: &Arc<RwLock<laddertypes::TypeDict>>, str: &str) -> Self {
LTExpr::SymbolLiteral {
typ: typectx.write().unwrap().parse("<Ref memory::Word>~Symbol~<Seq Char>").expect("parse typeterm"),
symbol: String::from(str)
}
}
pub fn call_symbol(
typectx: &Arc<RwLock<laddertypes::TypeDict>>,
//typ: laddertypes::TypeTerm,
result_size: usize,
addr: tisc::VM_Word
) -> Self {
LTExpr::CallLiteral {
typ: typectx.write().unwrap().parse("machine::Word").expect("parse typeterm"),
result_size,
val: addr
}
}
pub fn lit_uint(typectx: &Arc<RwLock<laddertypes::TypeDict>>, val: u64) -> Self {
LTExpr::WordLiteral {
typ: typectx.write().unwrap().parse("_2^64~machine::UInt64~machine::Word").expect("parse typeterm"),
val: val as tisc::VM_Word
}
}
pub fn abstraction(typectx: &Arc<RwLock<laddertypes::TypeDict>>, arg_id: &str, arg_typ: &str, val_expr: LTExpr) -> LTExpr {
LTExpr::Abstraction {
arg_id: String::from(arg_id),
arg_type: typectx.write().unwrap().parse(arg_typ).expect("parse typeterm"),
val_expr: Box::new(val_expr)
}
}
pub fn application(head: LTExpr, body: Vec<LTExpr>) -> Self {
LTExpr::Application {
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: Box::new(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()
}
}
}
}
}
*/