Big Object naming refactor
* trait Obj -> Object * Remove *Inst suffix from all object types. ObjInst -> Obj, IntInst -> Int, etc * Type -> Ty, type_inst() -> ty(), type_name() -> ty_name() Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -8,7 +8,7 @@ use common_macros::hash_map;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::ast::*;
|
||||
use crate::obj::function::UserFunctionInst;
|
||||
use crate::obj::function::UserFunction;
|
||||
use crate::obj::*;
|
||||
use crate::token::TokenKind;
|
||||
use crate::vm::*;
|
||||
@@ -665,11 +665,7 @@ impl StmtVisitor for Compiler {
|
||||
// object created, if it were a function object, will be what we're assigning it to, but I
|
||||
// want to be 100% sure instead of 99%.
|
||||
let obj = self.constants.last().unwrap().as_ref();
|
||||
if let Some(fun) = obj
|
||||
.borrow_mut()
|
||||
.as_any_mut()
|
||||
.downcast_mut::<UserFunctionInst>()
|
||||
{
|
||||
if let Some(fun) = obj.borrow_mut().as_any_mut().downcast_mut::<UserFunction>() {
|
||||
fun.set_name(Rc::new(name.to_string()));
|
||||
}
|
||||
|
||||
@@ -678,7 +674,7 @@ impl StmtVisitor for Compiler {
|
||||
|
||||
fn visit_set_stmt(&mut self, stmt: &SetStmt) -> Result<()> {
|
||||
self.compile_expr(&stmt.expr)?;
|
||||
let name = self.insert_constant(StrInst::create(&stmt.name.text))?;
|
||||
let name = self.insert_constant(Str::create(&stmt.name.text))?;
|
||||
self.compile_expr(&stmt.rhs)?;
|
||||
self.emit(stmt_line_number(stmt), Op::SetAttr(name));
|
||||
Ok(())
|
||||
@@ -697,7 +693,7 @@ impl StmtVisitor for Compiler {
|
||||
if let Some(expr) = &stmt.expr {
|
||||
self.compile_expr(expr)?;
|
||||
} else {
|
||||
let nil = self.insert_constant(NilInst::create())?;
|
||||
let nil = self.insert_constant(Nil::create())?;
|
||||
self.emit(stmt_line_number(stmt), Op::PushConstant(nil));
|
||||
}
|
||||
self.emit(stmt_line_number(stmt), Op::Return);
|
||||
@@ -707,7 +703,7 @@ impl StmtVisitor for Compiler {
|
||||
// condition
|
||||
self.compile_expr(&stmt.condition)?;
|
||||
// call obj.to_bool()
|
||||
let bool_attr = self.insert_constant(StrInst::create("to_bool"))?;
|
||||
let bool_attr = self.insert_constant(Str::create("to_bool"))?;
|
||||
self.emit(expr_line_number(&*stmt.condition), Op::GetAttr(bool_attr));
|
||||
self.emit(expr_line_number(&*stmt.condition), Op::Call(0));
|
||||
let condition_patch_index = self.chunk().code.len();
|
||||
@@ -779,7 +775,7 @@ impl ExprVisitor for Compiler {
|
||||
let mut exit_patch_index = 0;
|
||||
|
||||
if let TokenKind::And | TokenKind::Or = expr.op.kind {
|
||||
let constant_id = self.insert_constant(StrInst::create("to_bool"))?;
|
||||
let constant_id = self.insert_constant(Str::create("to_bool"))?;
|
||||
self.emit(expr_line_number(&*expr.lhs), Op::GetAttr(constant_id));
|
||||
self.emit(expr_line_number(&*expr.lhs), Op::Call(0));
|
||||
exit_patch_index = self.chunk().code.len();
|
||||
@@ -793,14 +789,14 @@ impl ExprVisitor for Compiler {
|
||||
let name = OP_NAMES
|
||||
.get(&expr.op.kind)
|
||||
.expect("invalid binary operator");
|
||||
let constant_id = self.insert_constant(StrInst::create(name))?;
|
||||
let constant_id = self.insert_constant(Str::create(name))?;
|
||||
self.emit(expr_line_number(expr), Op::GetAttr(constant_id));
|
||||
|
||||
self.compile_expr(&expr.rhs)?;
|
||||
|
||||
// convert RHS to a bool if we're doing AND or OR
|
||||
if let TokenKind::And | TokenKind::Or = expr.op.kind {
|
||||
let constant_id = self.insert_constant(StrInst::create("to_bool"))?;
|
||||
let constant_id = self.insert_constant(Str::create("to_bool"))?;
|
||||
self.emit(expr_line_number(&*expr.rhs), Op::GetAttr(constant_id));
|
||||
self.emit(expr_line_number(&*expr.rhs), Op::Call(0));
|
||||
}
|
||||
@@ -838,7 +834,7 @@ impl ExprVisitor for Compiler {
|
||||
});
|
||||
self.compile_expr(&expr.expr)?;
|
||||
let name = OP_NAMES.get(&expr.op.kind).expect("invalid unary operator");
|
||||
let constant_id = self.insert_constant(StrInst::create(name))?;
|
||||
let constant_id = self.insert_constant(Str::create(name))?;
|
||||
self.emit(expr_line_number(expr), Op::GetAttr(constant_id));
|
||||
self.emit(expr_line_number(expr), Op::Call(0));
|
||||
Ok(())
|
||||
@@ -862,7 +858,7 @@ impl ExprVisitor for Compiler {
|
||||
|
||||
fn visit_get_expr(&mut self, expr: &GetExpr) -> Result<()> {
|
||||
self.compile_expr(&expr.expr)?;
|
||||
let constant_id = self.insert_constant(StrInst::create(&expr.name.text))?;
|
||||
let constant_id = self.insert_constant(Str::create(&expr.name.text))?;
|
||||
self.emit(expr_line_number(expr), Op::GetAttr(constant_id));
|
||||
Ok(())
|
||||
}
|
||||
@@ -888,29 +884,28 @@ impl ExprVisitor for Compiler {
|
||||
}
|
||||
TokenKind::Number => {
|
||||
let obj = if expr.token.text.contains('.') {
|
||||
FloatInst::create(expr.token.text.parse().unwrap())
|
||||
Float::create(expr.token.text.parse().unwrap())
|
||||
} else if expr.token.text.starts_with("0x") || expr.token.text.starts_with("0X") {
|
||||
IntInst::create(i64::from_str_radix(&expr.token.text[2..], 16).unwrap())
|
||||
Int::create(i64::from_str_radix(&expr.token.text[2..], 16).unwrap())
|
||||
} else if expr.token.text.starts_with("0b") || expr.token.text.starts_with("0B") {
|
||||
IntInst::create(i64::from_str_radix(&expr.token.text[2..], 2).unwrap())
|
||||
Int::create(i64::from_str_radix(&expr.token.text[2..], 2).unwrap())
|
||||
} else {
|
||||
IntInst::create(expr.token.text.parse().unwrap())
|
||||
Int::create(expr.token.text.parse().unwrap())
|
||||
};
|
||||
let constant_id = self.insert_constant(obj)?;
|
||||
self.emit(expr_line_number(expr), Op::PushConstant(constant_id));
|
||||
}
|
||||
TokenKind::String => {
|
||||
let constant_id =
|
||||
self.insert_constant(StrInst::create(unescape(&expr.token.text)))?;
|
||||
let constant_id = self.insert_constant(Str::create(unescape(&expr.token.text)))?;
|
||||
self.emit(expr_line_number(expr), Op::PushConstant(constant_id));
|
||||
}
|
||||
TokenKind::True | TokenKind::False => {
|
||||
let constant_id =
|
||||
self.insert_constant(BoolInst::create(expr.token.kind == TokenKind::True))?;
|
||||
self.insert_constant(Bool::create(expr.token.kind == TokenKind::True))?;
|
||||
self.emit(expr_line_number(expr), Op::PushConstant(constant_id));
|
||||
}
|
||||
TokenKind::Nil => {
|
||||
let constant_id = self.insert_constant(NilInst::create())?;
|
||||
let constant_id = self.insert_constant(Nil::create())?;
|
||||
self.emit(expr_line_number(expr), Op::PushConstant(constant_id));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
@@ -968,7 +963,7 @@ impl ExprVisitor for Compiler {
|
||||
}
|
||||
|
||||
// always end with a "return nil"
|
||||
let nil = self.insert_constant(NilInst::create())?;
|
||||
let nil = self.insert_constant(Nil::create())?;
|
||||
self.emit(end_line, Op::PushConstant(nil));
|
||||
self.emit(end_line, Op::Return);
|
||||
|
||||
@@ -976,7 +971,7 @@ impl ExprVisitor for Compiler {
|
||||
|
||||
// create the function
|
||||
let chunk = self.chunks.pop().unwrap();
|
||||
let fun = UserFunctionInst::create(chunk, expr.params.len() as Argc);
|
||||
let fun = UserFunction::create(chunk, expr.params.len() as Argc);
|
||||
|
||||
// register the function as a constant
|
||||
let fun_constant = self.insert_constant(fun)?;
|
||||
|
||||
Reference in New Issue
Block a user