Interpreter - Obtain and set a class' superclass
This commit is contained in:
parent
031efc33b1
commit
9307fde4b3
1 changed files with 23 additions and 4 deletions
|
@ -225,7 +225,22 @@ impl StmtNode {
|
||||||
/// Handle a class declaration
|
/// Handle a class declaration
|
||||||
fn on_class_decl(&self, es: &mut InterpreterState, decl: &ClassDecl) -> InterpreterResult {
|
fn on_class_decl(&self, es: &mut InterpreterState, decl: &ClassDecl) -> InterpreterResult {
|
||||||
es.environment.borrow_mut().define(&decl.name, None)?;
|
es.environment.borrow_mut().define(&decl.name, None)?;
|
||||||
let class = Class::new(decl.name.lexeme.clone(), extract_members(es, decl));
|
let superclass = match &decl.superclass {
|
||||||
|
None => None,
|
||||||
|
Some(superclass) => {
|
||||||
|
let sc_value = superclass.interpret(es)?.result();
|
||||||
|
if let Some(sc_ref) = sc_value.as_class_ref() {
|
||||||
|
Some(sc_ref)
|
||||||
|
} else {
|
||||||
|
return error(&superclass.token, "superclass must be a class");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let class = Class::new(
|
||||||
|
decl.name.lexeme.clone(),
|
||||||
|
superclass,
|
||||||
|
extract_members(es, decl),
|
||||||
|
);
|
||||||
es.environment
|
es.environment
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.assign(&decl.name, class.into())?;
|
.assign(&decl.name, class.into())?;
|
||||||
|
@ -362,9 +377,7 @@ impl Interpretable for ExprNode {
|
||||||
ExprNode::Unary { operator, right } => self.on_unary(es, operator, right),
|
ExprNode::Unary { operator, right } => self.on_unary(es, operator, right),
|
||||||
ExprNode::Grouping { expression } => expression.interpret(es),
|
ExprNode::Grouping { expression } => expression.interpret(es),
|
||||||
ExprNode::Litteral { value } => self.on_litteral(value),
|
ExprNode::Litteral { value } => self.on_litteral(value),
|
||||||
ExprNode::Variable(var_expr) | ExprNode::This(var_expr) => {
|
ExprNode::Variable(var_expr) | ExprNode::This(var_expr) => var_expr.interpret(es),
|
||||||
Ok(es.lookup_var(var_expr)?.into())
|
|
||||||
}
|
|
||||||
ExprNode::Call {
|
ExprNode::Call {
|
||||||
callee,
|
callee,
|
||||||
right_paren,
|
right_paren,
|
||||||
|
@ -380,6 +393,12 @@ impl Interpretable for ExprNode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Interpretable for VariableExpr {
|
||||||
|
fn interpret(&self, es: &mut InterpreterState) -> InterpreterResult {
|
||||||
|
Ok(es.lookup_var(self)?.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ExprNode {
|
impl ExprNode {
|
||||||
/// Evaluate a logical operator.
|
/// Evaluate a logical operator.
|
||||||
fn on_logic(
|
fn on_logic(
|
||||||
|
|
Loading…
Reference in a new issue