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:
parent
a6282c00eb
commit
f54f630b38
4 changed files with 39 additions and 27 deletions
15
src/expr.rs
15
src/expr.rs
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -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';
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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 } => {
|
||||||
|
for (arg_name, arg_type) in args.iter() {
|
||||||
let id = self.symbols
|
let id = self.symbols
|
||||||
.write().unwrap()
|
.write().unwrap()
|
||||||
.declare_var(
|
.declare_var(
|
||||||
arg_name.clone(),
|
arg_name.clone(),
|
||||||
laddertypes::TypeTerm::unit());
|
arg_type.clone().unwrap_or(
|
||||||
|
laddertypes::TypeTerm::unit())
|
||||||
|
);
|
||||||
self.asm = self.asm
|
self.asm = self.asm
|
||||||
.lit( id )
|
.lit( id )
|
||||||
.call("data-frame-set");
|
.call("data-frame-set");
|
||||||
|
}
|
||||||
self = self.compile(val_expr);
|
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);
|
||||||
|
|
Loading…
Reference in a new issue