adapt Abstraction variant of LTExpr to allow multiple parameters

This avoids unneccesary recursive chaining and also allows abstractions with zero parameters.
This commit is contained in:
Michael Sippel 2024-05-12 04:22:32 +02:00
parent a6282c00eb
commit f54f630b38
Signed by: senvas
GPG key ID: F96CF119C34B64A6
4 changed files with 39 additions and 27 deletions

View file

@ -39,9 +39,8 @@ pub enum LTExpr {
body: Vec<LTExpr>
},
Abstraction {
arg_id: String,
arg_type: Option< laddertypes::TypeTerm >,
val_expr: Box<LTExpr>
args: Vec<(String, Option<laddertypes::TypeTerm>)>,
body: Box<LTExpr>
},
Branch {
condition: Box<LTExpr>,
@ -68,11 +67,13 @@ impl LTExpr {
}
}
pub fn abstraction(arg_id: &str, arg_typ: &str, val_expr: LTExpr) -> LTExpr {
pub fn abstraction(args: Vec<(&str, &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)
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(val_expr)
}
}

View file

@ -80,13 +80,19 @@ fn main() {
}
};
let hello = λ _ {
let hello = λ{
print-nullterm hello-string;
print-lenprefix pfxstr;
};
let isquare = λx (i* x x);
hello 'X';
hello;
let isquare = λx (i* x x);
let magnitude2 = λx y {
i+ (isquare x) (isquare y);
};
magnitude2 8 16;
emit '\n';
emit (i+ '0' (isquare 3));
emit '\n';

View file

@ -175,13 +175,16 @@ where It: Iterator<Item = char>
Ok(LTIRToken::Lambda) => {
if children.len() == 0 {
tokens.next();
let name = parse_symbol(tokens)?;
let mut args = Vec::new();
while let Some(Ok(LTIRToken::Symbol(_))) = tokens.peek() {
args.push((parse_symbol(tokens)?, None));
}
let body = parse_expr(tokens)?;
return Ok(LTExpr::Abstraction{
arg_id: name,
arg_type: None,
val_expr: Box::new(body)
args,
body: Box::new(body)
});
} else {
return Err(ParseError::UnexpectedToken);

View file

@ -100,7 +100,7 @@ impl ProcedureCompiler {
}
Statement::LetAssign{ var_id, val_expr } => {
match val_expr {
LTExpr::Abstraction { arg_id:_, arg_type:_, val_expr:_ } => {
LTExpr::Abstraction { args:_, body:_ } => {
self.symbols.write().unwrap()
.declare_proc(
var_id.clone(),
@ -186,18 +186,20 @@ impl ProcedureCompiler {
}
self = self.compile(head);
}
LTExpr::Abstraction { arg_id: arg_name, arg_type, val_expr } => {
let id = self.symbols
.write().unwrap()
.declare_var(
arg_name.clone(),
laddertypes::TypeTerm::unit());
self.asm = self.asm
.lit( id )
.call("data-frame-set");
self = self.compile(val_expr);
LTExpr::Abstraction { args, body } => {
for (arg_name, arg_type) in args.iter() {
let id = self.symbols
.write().unwrap()
.declare_var(
arg_name.clone(),
arg_type.clone().unwrap_or(
laddertypes::TypeTerm::unit())
);
self.asm = self.asm
.lit( id )
.call("data-frame-set");
}
self = self.compile(body);
}
LTExpr::Branch { condition, if_expr, else_expr } => {
self = self.compile(condition);