lt-core/src/expr.rs

154 lines
4 KiB
Rust
Raw Normal View History

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