Evaluator work

This commit is contained in:
Greg Shuflin 2021-10-24 06:36:16 -07:00
parent 7a7e4ec0f2
commit 3383921c6b
2 changed files with 31 additions and 11 deletions

View File

@ -271,10 +271,10 @@ pub enum Expression {
#[derive(Debug)] #[derive(Debug)]
pub struct FunctionDefinition { pub struct FunctionDefinition {
body: Vec<Statement> pub body: Vec<Statement>
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum Function { pub enum Function {
Builtin(Builtin), Builtin(Builtin),
UserDefined(DefId) UserDefined(DefId)

View File

@ -1,6 +1,7 @@
use crate::reduced_ir::{ReducedIR, Expression, Lookup, Function, FunctionDefinition, Statement, Literal}; use crate::reduced_ir::{ReducedIR, Expression, Lookup, Function, FunctionDefinition, Statement, Literal};
use crate::symbol_table::{DefId}; use crate::symbol_table::{DefId};
use crate::util::ScopeStack; use crate::util::ScopeStack;
use crate::builtin::Builtin;
use std::fmt::Write; use std::fmt::Write;
use std::convert::From; use std::convert::From;
@ -101,7 +102,7 @@ impl RuntimeValue {
enum Primitive { enum Primitive {
Tuple(Vec<Primitive>), Tuple(Vec<Primitive>),
Literal(Literal), Literal(Literal),
Callable(DefId), Callable(Function),
Unimplemented, Unimplemented,
/* /*
PrimObject { PrimObject {
@ -117,7 +118,7 @@ impl Primitive {
match self { match self {
Primitive::Tuple(items) => Expression::Tuple(items.iter().map(|item| item.to_expr()).collect()), Primitive::Tuple(items) => Expression::Tuple(items.iter().map(|item| item.to_expr()).collect()),
Primitive::Literal(lit) => Expression::Literal(lit.clone()), Primitive::Literal(lit) => Expression::Literal(lit.clone()),
Primitive::Callable(defid) => Expression::Callable(Function::UserDefined(defid.clone())), Primitive::Callable(function) => Expression::Callable(function.clone()),
Primitive::Unimplemented => Expression::Unimplemented, Primitive::Unimplemented => Expression::Unimplemented,
} }
} }
@ -177,12 +178,10 @@ impl<'a> State<'a> {
Expression::Lookup { ref id, kind } => { Expression::Lookup { ref id, kind } => {
let mem = id.into(); let mem = id.into();
match kind { match kind {
Lookup::Function => { Lookup::Function => match self.environments.lookup(&mem) {
if self.environments.lookup(&mem).is_some() { //TODO is this right? not sure
Primitive::Callable(id.clone()) Some(RuntimeValue::Primitive(prim)) => prim.clone(),
} else { _ => return Err(format!("Function not found for id: {}", id)),
return Err(format!("Function not found for id: {}", id));
}
}, },
kind @ Lookup::LocalVar | kind @ Lookup::GlobalVar | kind @ Lookup::Param => { kind @ Lookup::LocalVar | kind @ Lookup::GlobalVar | kind @ Lookup::Param => {
match self.environments.lookup(&mem) { match self.environments.lookup(&mem) {
@ -205,7 +204,28 @@ impl<'a> State<'a> {
} }
fn call_expression(&mut self, f: Expression, args: Vec<Expression>) -> EvalResult<Primitive> { fn call_expression(&mut self, f: Expression, args: Vec<Expression>) -> EvalResult<Primitive> {
Err("Call expression not implemented".to_string().into()) let func = match self.expression(f)? {
Primitive::Callable(func) => func,
other => return Err(format!("Trying to call non-function value: {:?}", other)),
};
match func {
Function::Builtin(builtin) => self.apply_builtin(builtin, args),
Function::UserDefined(def_id) => self.apply_function(def_id, args),
}
}
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expression>) -> EvalResult<Primitive> {
return Err("unimplemented apply-builtin".to_string());
}
fn apply_function(&mut self, def_id: DefId, args: Vec<Expression>) -> EvalResult<Primitive> {
let mem = (&def_id).into();
Ok(match self.environments.lookup(&mem) {
Some(RuntimeValue::Function(FunctionDefinition { body })) => {
return Err("unimplemented apply-function".to_string());
},
e => return Err(format!("Error looking up function with id {}: {:?}", def_id, e)),
})
} }
} }