We have blocks & scopes
This commit is contained in:
+30
-12
@@ -31,12 +31,16 @@ macro_rules! some_or_error {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_value(v: Value, scope: &HashMap<String, VarType>) -> Result<TeaValue, InterpreterError> {
|
||||
fn get_value(v: Value, scopes: &mut [Scope]) -> Result<TeaValue, InterpreterError> {
|
||||
match v {
|
||||
Value::Lit(t) => Ok(t),
|
||||
Value::Id(s) => match scope.get(&s) {
|
||||
None => Err(InterpreterError::new("undefined variable")),
|
||||
Some(VarType::Const(t)) => Ok(t.clone()),
|
||||
Value::Id(s) => {
|
||||
match scopes.iter().rev().skip_while(|scope| !scope.contains_key(&s)).next() {
|
||||
None => Err(InterpreterError::new("undefined variable")),
|
||||
Some(scope) => match scope.get(&s).unwrap() {
|
||||
VarType::Const(t) => Ok(t.clone()),
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -58,20 +62,28 @@ macro_rules! apply_op {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret(ast: Vec<ASTNode>) -> Result<Vec<Value>, InterpreterError> {
|
||||
type Scope = HashMap<String, VarType>;
|
||||
|
||||
fn interpret_block(ast: Vec<ASTNode>, scopes: &mut Vec<Scope>) -> Result<Vec<Value>, InterpreterError> {
|
||||
let mut ast = ast.clone();
|
||||
ast.reverse();
|
||||
let mut stack = vec![];
|
||||
let mut global_scope: HashMap<String, VarType> = HashMap::new();
|
||||
//let mut scope = Scope::new();
|
||||
loop {
|
||||
match ast.pop() {
|
||||
None => return Ok(stack),
|
||||
Some(ASTNode::Block(tokens)) => {
|
||||
scopes.push(HashMap::new());
|
||||
let mut result = interpret_block(tokens, scopes)?;
|
||||
scopes.pop();
|
||||
stack.append(&mut result);
|
||||
},
|
||||
Some(ASTNode::Val(v)) => stack.push(v),
|
||||
Some(ASTNode::BinOp(op)) => {
|
||||
let b = some_or_error!(stack.pop(), InterpreterError::new("right value needed"));
|
||||
let a = some_or_error!(stack.pop(), InterpreterError::new("left value needed"));
|
||||
let b: TeaValue = get_value(b, &global_scope)?;
|
||||
let a: TeaValue = get_value(a, &global_scope)?;
|
||||
let b: TeaValue = get_value(b, scopes)?;
|
||||
let a: TeaValue = get_value(a, scopes)?;
|
||||
stack.push(Value::Lit(match op {
|
||||
Operator::Plus => {
|
||||
apply_op!(a, b, +)
|
||||
@@ -89,7 +101,7 @@ pub fn interpret(ast: Vec<ASTNode>) -> Result<Vec<Value>, InterpreterError> {
|
||||
},
|
||||
Some(ASTNode::UnaryMinus) => {
|
||||
let x = some_or_error!(stack.pop(), InterpreterError::new("value needed"));
|
||||
let x: TeaValue = get_value(x, &global_scope)?;
|
||||
let x: TeaValue = get_value(x, scopes)?;
|
||||
stack.push(Value::Lit(match x {
|
||||
TeaValue::Float(f) => TeaValue::Float(-f),
|
||||
TeaValue::Int(i) => TeaValue::Int(-i),
|
||||
@@ -100,14 +112,15 @@ pub fn interpret(ast: Vec<ASTNode>) -> Result<Vec<Value>, InterpreterError> {
|
||||
Some(ASTNode::ConstDecl) => {
|
||||
let rhs = some_or_error!(stack.pop(), InterpreterError::new("rhs value needed"));
|
||||
let lhs = some_or_error!(stack.pop(), InterpreterError::new("lhs value needed"));
|
||||
let rhs: TeaValue = get_value(rhs, &global_scope)?;
|
||||
let rhs: TeaValue = get_value(rhs, scopes)?;
|
||||
//let current_scope = .unwrap();
|
||||
match lhs {
|
||||
Value::Lit(_) => return Err(InterpreterError::new("invalid left hand side")),
|
||||
Value::Id(s) => {
|
||||
if global_scope.contains_key(&s) {
|
||||
if scopes.last().unwrap().contains_key(&s) {
|
||||
return Err(InterpreterError::new("already defined constant"));
|
||||
} else {
|
||||
global_scope.insert(s, VarType::Const(rhs));
|
||||
scopes.last_mut().unwrap().insert(s, VarType::Const(rhs));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,3 +133,8 @@ pub fn interpret(ast: Vec<ASTNode>) -> Result<Vec<Value>, InterpreterError> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interpret(ast: Vec<ASTNode>) -> Result<Vec<Value>, InterpreterError> {
|
||||
let mut scopes = vec![HashMap::new()];
|
||||
interpret_block(ast, &mut scopes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user