Start making function calls work
This commit isn't fully done yet
This commit is contained in:
parent
77f72806be
commit
f8a521fc9b
27
src/eval.rs
27
src/eval.rs
@ -21,6 +21,10 @@ impl Funcmap {
|
|||||||
let map = HashMap::new();
|
let map = HashMap::new();
|
||||||
Funcmap { map: map }
|
Funcmap { map: map }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn lookup_function(&self, name: String) -> Option<Function> {
|
||||||
|
self.map.get(&name).map(|x| x.clone())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Evaluator {
|
pub struct Evaluator {
|
||||||
@ -66,9 +70,6 @@ impl Evaluator {
|
|||||||
self.funcmap.map.insert(name, function);
|
self.funcmap.map.insert(name, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lookup_function(&mut self, name: String) -> Option<&Function> {
|
|
||||||
self.funcmap.map.get(&name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Evaluable {
|
trait Evaluable {
|
||||||
@ -208,11 +209,27 @@ impl Evaluator {
|
|||||||
|
|
||||||
fn reduce_call(&mut self, name: String, arguments: Vec<Expression>) -> Expression {
|
fn reduce_call(&mut self, name: String, arguments: Vec<Expression>) -> Expression {
|
||||||
use parser::Expression::*;
|
use parser::Expression::*;
|
||||||
let function = match self.lookup_function(name) {
|
let x = self.funcmap.lookup_function(name);
|
||||||
Some(ref func) => func.clone(),
|
let function = match x {
|
||||||
|
Some(func) => func,
|
||||||
None => return Null
|
None => return Null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if function.prototype.parameters.len() != arguments.len() {
|
||||||
|
return Null
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut frame: Varmap = Varmap::new();
|
||||||
|
for (binding, expr) in function.prototype.parameters.iter().zip(arguments.iter()) {
|
||||||
|
frame.map.insert(binding.clone(), expr.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.frames.push(frame);
|
||||||
|
for expr in function.body.iter() {
|
||||||
|
self.reduce_expr(expr.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.frames.pop();
|
||||||
Null
|
Null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ pub struct Function {
|
|||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Prototype {
|
pub struct Prototype {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub args: Vec<String>
|
pub parameters: Vec<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -194,9 +194,9 @@ impl Parser {
|
|||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
let name: String = expect_identifier!(self);
|
let name: String = expect_identifier!(self);
|
||||||
expect!(self, LParen);
|
expect!(self, LParen);
|
||||||
let args: Vec<String> = try!(self.identlist());
|
let parameters: Vec<String> = try!(self.identlist());
|
||||||
expect!(self, RParen);
|
expect!(self, RParen);
|
||||||
Ok(Prototype {name: name, args: args})
|
Ok(Prototype {name: name, parameters: parameters})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn identlist(&mut self) -> ParseResult<Vec<String>> {
|
fn identlist(&mut self) -> ParseResult<Vec<String>> {
|
||||||
|
Loading…
Reference in New Issue
Block a user