rename MultiplyDefined error to VariableDefinedMultipleTimes

This commit is contained in:
Aaron Blankstein
2019-02-19 10:40:22 -06:00
parent 74e45cbcb8
commit bb203956e7
7 changed files with 42 additions and 39 deletions

View File

@@ -54,7 +54,7 @@ impl PublicFunction {
return Err(Error::TypeError(format!("{:?}", type_sig), value.clone()))
}
if let Some(_) = context.variables.insert(arg.clone(), value.clone()) {
return Err(Error::MultiplyDefined(arg.clone()))
return Err(Error::VariableDefinedMultipleTimes(arg.clone()))
}
}
eval(&self.body, env, &context)
@@ -74,7 +74,7 @@ impl PrivateFunction {
let arg_iterator = self.arguments.iter().zip(args.iter());
for (arg, value) in arg_iterator {
if let Some(_) = context.variables.insert(arg.clone(), value.clone()) {
return Err(Error::MultiplyDefined(arg.clone()))
return Err(Error::VariableDefinedMultipleTimes(arg.clone()))
}
}
eval(&self.body, env, &context)

View File

@@ -26,7 +26,7 @@ pub enum Error {
BadSymbolicRepresentation(String),
ReservedName(String),
InterpreterError(String),
MultiplyDefined(String)
VariableDefinedMultipleTimes(String)
}
pub type InterpreterResult <R> = Result<R, Error>;

View File

@@ -19,7 +19,7 @@ fn check_legal_define(name: &str, global_context: &GlobalContext) -> Result<()>
if is_reserved(name) {
Err(Error::ReservedName(name.to_string()))
} else if global_context.variables.contains_key(name) || global_context.functions.contains_key(name) {
Err(Error::MultiplyDefined(name.to_string()))
Err(Error::VariableDefinedMultipleTimes(name.to_string()))
} else {
Ok(())
}

View File

@@ -78,7 +78,7 @@ fn special_let(args: &[SymbolicExpression], env: &mut Environment, context: &Loc
}
let value = eval(&binding_exps[1], env, context)?;
match inner_context.variables.insert((*var_name).clone(), value) {
Some(_val) => return Err(Error::MultiplyDefined(var_name.to_string())),
Some(_val) => return Err(Error::VariableDefinedMultipleTimes(var_name.to_string())),
_ => continue
}
} else {

View File

@@ -171,3 +171,38 @@ pub fn execute(program: &str) -> Result<Value> {
let parsed = parser::parse(program)?;
eval_all(&parsed, &mut *db_instance, &mut global_context)
}
mod test {
#[test]
fn test_simple_user_function() {
//
// test program:
// (define (do_work x) (+ 5 x))
// (define a 59)
// (do_work a)
//
let content = [ SymbolicExpression::List(
Box::new([ SymbolicExpression::Atom("do_work".to_string()),
SymbolicExpression::Atom("a".to_string()) ])) ];
let func_body = SymbolicExpression::List(
Box::new([ SymbolicExpression::Atom("+".to_string()),
SymbolicExpression::AtomValue(Value::Int(5)),
SymbolicExpression::Atom("x".to_string())]));
let func_args = vec!["x".to_string()];
let user_function = PrivateFunction::new(func_args, func_body);
let context = LocalContext::new();
let mut global_context = GlobalContext::new();
let mut db = MemoryContractDatabase::new();
global_context.variables.insert("a".to_string(), Value::Int(59));
global_context.functions.insert("do_work".to_string(), user_function);
let mut env = Environment::new(&global_context, &mut db);
assert_eq!(Ok(Value::Int(64)), eval(&content[0], &mut env, &context));
}
}

View File

@@ -39,7 +39,7 @@ fn test_bad_define_names() {
assert_eq!(Err(Error::ReservedName("*".to_string())), execute(&test1));
assert_eq!(Err(Error::InvalidArguments("Illegal operation: attempted to re-define a value type.".to_string())),
execute(&test2));
assert_eq!(Err(Error::MultiplyDefined("foo".to_string())),
assert_eq!(Err(Error::VariableDefinedMultipleTimes("foo".to_string())),
execute(&test3));
}

View File

@@ -6,38 +6,6 @@ use vm::callables::PrivateFunction;
use vm::representations::SymbolicExpression;
use vm::parser::parse;
#[test]
fn test_simple_user_function() {
//
// test program:
// (define (do_work x) (+ 5 x))
// (define a 59)
// (do_work a)
//
let content = [ SymbolicExpression::List(
Box::new([ SymbolicExpression::Atom("do_work".to_string()),
SymbolicExpression::Atom("a".to_string()) ])) ];
let func_body = SymbolicExpression::List(
Box::new([ SymbolicExpression::Atom("+".to_string()),
SymbolicExpression::AtomValue(Value::Int(5)),
SymbolicExpression::Atom("x".to_string())]));
let func_args = vec!["x".to_string()];
let user_function = PrivateFunction::new(func_args, func_body);
let context = LocalContext::new();
let mut global_context = GlobalContext::new();
let mut db = MemoryContractDatabase::new();
global_context.variables.insert("a".to_string(), Value::Int(59));
global_context.functions.insert("do_work".to_string(), user_function);
let mut env = Environment::new(&global_context, &mut db);
assert_eq!(Ok(Value::Int(64)), eval(&content[0], &mut env, &context));
}
#[test]
fn test_simple_let() {
/*
@@ -262,7 +230,7 @@ fn test_bad_lets() {
let expectations: &[Result<Value, Error>] = &[
Err(Error::ReservedName("tx-sender".to_string())),
Err(Error::ReservedName("*".to_string())),
Err(Error::MultiplyDefined("a".to_string()))];
Err(Error::VariableDefinedMultipleTimes("a".to_string()))];
tests.iter().zip(expectations.iter())
.for_each(|(program, expectation)| assert_eq!(*expectation, execute(program)));