Implement primitive procedure application

This commit is contained in:
2021-05-20 23:18:19 -04:00
parent f4c58cfa02
commit b0bf78a3bd
5 changed files with 92 additions and 21 deletions

View File

@@ -1,15 +1,15 @@
use crate::parser::Datum;
use crate::parser::Datum::*;
fn add(args: Vec<Datum>) -> Datum {
Number(42)
}
use crate::primitives::*;
use std::mem;
fn lookup_variable_value(exp: Datum) -> Datum {
if let Symbol(string) = exp {
match string.as_str() {
"+" => Procedure(add),
_ => panic!("unbound-variable {:?}", string)
"*" => Procedure(mul),
"-" => Procedure(sub),
_ => panic!("unbound-variable {:?}", string),
}
} else {
panic!("LOOKUP-VARIABLE-VALUE -- not-a-symbol {:?}", exp)
@@ -18,9 +18,19 @@ fn lookup_variable_value(exp: Datum) -> Datum {
fn application(exp: Datum) -> Datum {
if let List(vector) = exp {
vector[0]
if vector.len() == 0 {
panic!("Application needs at least a procedure")
}
let mut args: Vec<Datum> = vector.into_iter().map(interpret).collect();
if let Procedure(proc) = mem::take(&mut args[0]) {
// FIXME: call procedures with args only
proc(args)
} else {
panic!("The object {:?} is not applicable", args[0])
}
} else {
panic!("APPLICATION -- cannot-apply {:?}", exp)
panic!("Cannot apply {:?}", exp)
}
}
@@ -29,6 +39,6 @@ pub fn interpret(exp: Datum) -> Datum {
Boolean(_) | Number(_) => exp,
Symbol(_) => lookup_variable_value(exp),
List(_) => application(exp),
_ => panic!("unknown-expression {:?}", exp)
_ => panic!("unknown-expression {:?}", exp),
}
}
}