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> body: Vec<LTExpr>
}, },
Abstraction { Abstraction {
arg_id: String, args: Vec<(String, Option<laddertypes::TypeTerm>)>,
arg_type: Option< laddertypes::TypeTerm >, body: Box<LTExpr>
val_expr: Box<LTExpr>
}, },
Branch { Branch {
condition: Box<LTExpr>, 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 { LTExpr::Abstraction {
arg_id: String::from(arg_id), args: args.into_iter().map(|(arg_name, arg_type)|
arg_type: None,//typectx.write().unwrap().parse(arg_typ).expect("parse typeterm"), ( arg_name.into(), None )
val_expr: Box::new(val_expr) //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-nullterm hello-string;
print-lenprefix pfxstr; 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 '\n';
emit (i+ '0' (isquare 3)); emit (i+ '0' (isquare 3));
emit '\n'; emit '\n';

View file

@ -175,13 +175,16 @@ where It: Iterator<Item = char>
Ok(LTIRToken::Lambda) => { Ok(LTIRToken::Lambda) => {
if children.len() == 0 { if children.len() == 0 {
tokens.next(); 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)?; let body = parse_expr(tokens)?;
return Ok(LTExpr::Abstraction{ return Ok(LTExpr::Abstraction{
arg_id: name, args,
arg_type: None, body: Box::new(body)
val_expr: Box::new(body)
}); });
} else { } else {
return Err(ParseError::UnexpectedToken); return Err(ParseError::UnexpectedToken);

View file

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