checkpoint; builds again
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use super::{Primitive, Type, ValueOrRef};
|
||||
use crate::eval::{EvalEnvironment, EvalError, Value};
|
||||
use crate::ir::{Expression, Program, Statement, TopLevel};
|
||||
use crate::ir::{Expression, Program, TopLevel};
|
||||
|
||||
impl Program {
|
||||
impl<Type> Program<Type> {
|
||||
/// Evaluate the program, returning either an error or a string containing everything
|
||||
/// the program printed out.
|
||||
///
|
||||
@@ -15,16 +15,7 @@ impl Program {
|
||||
match stmt {
|
||||
TopLevel::Function(_, _, _, _) => unimplemented!(),
|
||||
|
||||
TopLevel::Statement(Statement::Binding(_, name, _, value)) => {
|
||||
let actual_value = value.eval(&env)?;
|
||||
env = env.extend(name.clone(), actual_value);
|
||||
}
|
||||
|
||||
TopLevel::Statement(Statement::Print(_, _, name)) => {
|
||||
let value = env.lookup(name.clone())?;
|
||||
let line = format!("{} = {}\n", name, value);
|
||||
stdout.push_str(&line);
|
||||
}
|
||||
TopLevel::Statement(_) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,17 +23,21 @@ impl Program {
|
||||
}
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
impl<T> Expression<T>
|
||||
where
|
||||
T: Clone + Into<Type>,
|
||||
{
|
||||
fn eval(&self, env: &EvalEnvironment) -> Result<Value, EvalError> {
|
||||
match self {
|
||||
Expression::Atomic(x) => x.eval(env),
|
||||
|
||||
Expression::Cast(_, t, valref) => {
|
||||
let value = valref.eval(env)?;
|
||||
let ty = t.clone().into();
|
||||
|
||||
match t {
|
||||
match ty {
|
||||
Type::Primitive(pt) => Ok(pt.safe_cast(&value)?),
|
||||
Type::Function(_, _) => Err(EvalError::CastToFunction(t.to_string())),
|
||||
Type::Function(_, _) => Err(EvalError::CastToFunction(ty.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,11 +56,19 @@ impl Expression {
|
||||
Primitive::Divide => Ok(Value::calculate("/", arg_values)?),
|
||||
}
|
||||
}
|
||||
|
||||
Expression::Block(_, _, _) => {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
Expression::Print(_, _) => unimplemented!(),
|
||||
|
||||
Expression::Bind(_, _, _, _) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueOrRef {
|
||||
impl<T> ValueOrRef<T> {
|
||||
fn eval(&self, env: &EvalEnvironment) -> Result<Value, EvalError> {
|
||||
match self {
|
||||
ValueOrRef::Value(_, _, v) => match v {
|
||||
|
||||
Reference in New Issue
Block a user