From 8559220aa35ee5850284c9bd88bafdeb02f21d5b Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 20 Apr 2025 13:42:57 +0300 Subject: [PATCH] use Value --- src/twopass.rs | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/twopass.rs b/src/twopass.rs index 9883886..b019793 100644 --- a/src/twopass.rs +++ b/src/twopass.rs @@ -1,6 +1,9 @@ use std::{cell::RefCell, rc::Rc}; -use crate::{ast, types}; +use crate::{ + ast, + types::{self, Nil, Value}, +}; #[derive(Debug, Clone)] pub struct OpenFunction(Function); @@ -273,14 +276,14 @@ pub fn build(code: &ast::Function) -> OpenFunction { } #[derive(Debug, Clone)] -struct Variable(RefCell); +struct Variable(RefCell); impl Variable { - fn get(&self) -> types::Value { + fn get(&self) -> Value { self.0.borrow().clone() } - fn set(&self, value: types::Value) { + fn set(&self, value: Value) { *self.0.borrow_mut() = value } } @@ -307,28 +310,28 @@ impl RunContext { } trait Eval { - fn eval(&self, ctx: &RunContext) -> types::Value; + fn eval(&self, ctx: &RunContext) -> Value; } impl Eval for ast::Constant { - fn eval(&self, _ctx: &RunContext) -> types::Value { + fn eval(&self, _ctx: &RunContext) -> Value { match self { - ast::Constant::Nil => types::Value::Nil, - ast::Constant::Boolean(value) => types::Value::Boolean(*value), - ast::Constant::Number(value) => types::Value::Number(*value), - ast::Constant::String(value) => types::Value::String(value.clone()), + ast::Constant::Nil => Nil, + ast::Constant::Boolean(value) => Value::Boolean(*value), + ast::Constant::Number(value) => Value::Number(*value), + ast::Constant::String(value) => Value::String(value.clone()), } } } impl Eval for Location { - fn eval(&self, ctx: &RunContext) -> types::Value { + fn eval(&self, ctx: &RunContext) -> Value { match self { Location::Variable { id } => ctx.var(*id).get(), Location::Field { table, index } => { let table = table.eval(ctx); let index = index.eval(ctx); - let types::Value::Table(table) = table else { + let Value::Table(table) = table else { panic!("attempt to index a non-table"); }; table.get(index) @@ -338,10 +341,10 @@ impl Eval for Location { } impl Eval for Call { - fn eval(&self, ctx: &RunContext) -> types::Value { + fn eval(&self, ctx: &RunContext) -> Value { let callee = self.callee.eval(ctx); let args: Vec<_> = self.args.iter().map(|arg| arg.eval(ctx)).collect(); - let types::Value::Function(f) = callee else { + let Value::Function(f) = callee else { panic!("attempt to call a non-function"); }; f.call(&args) @@ -349,7 +352,7 @@ impl Eval for Call { } impl Eval for Function { - fn eval(&self, ctx: &RunContext) -> types::Value { + fn eval(&self, ctx: &RunContext) -> Value { let upvalues: Vec<_> = self .upvalues .iter() @@ -361,7 +364,7 @@ impl Eval for Function { } impl Eval for Table { - fn eval(&self, ctx: &RunContext) -> types::Value { + fn eval(&self, ctx: &RunContext) -> Value { let table = types::Table::default(); for field in &self.fields { let key = field.key.eval(ctx); @@ -373,7 +376,7 @@ impl Eval for Table { } impl Eval for Expression { - fn eval(&self, ctx: &RunContext) -> types::Value { + fn eval(&self, ctx: &RunContext) -> Value { match self { Expression::Constant(inner) => inner.eval(ctx), Expression::Variable(inner) => inner.eval(ctx), @@ -385,7 +388,7 @@ impl Eval for Expression { } impl Eval for Statement { - fn eval(&self, ctx: &RunContext) -> types::Value { + fn eval(&self, ctx: &RunContext) -> Value { match self { Statement::Assign { target, source } => { let source = source.eval(ctx); @@ -394,7 +397,7 @@ impl Eval for Statement { Location::Field { table, index } => { let table = table.eval(ctx); let index = index.eval(ctx); - let types::Value::Table(table) = table else { + let Value::Table(table) = table else { panic!("attempt to index a non-table"); }; table.set(index, source) @@ -405,23 +408,21 @@ impl Eval for Statement { call.eval(ctx); } }; - types::Value::Nil + Nil } } -fn new_var(value: &types::Value) -> VariableRef { +fn new_var(value: &Value) -> VariableRef { Rc::new(Variable(RefCell::new(value.clone()))) } impl Function { - fn call(&self, upvalues: &[VariableRef], args: &[types::Value]) -> types::Value { + fn call(&self, upvalues: &[VariableRef], args: &[Value]) -> Value { assert_eq!(upvalues.len(), self.upvalues.len()); assert_eq!(args.len(), self.args.len()); let ctx = RunContext { args: args.iter().map(new_var).collect(), - locals: (0..self.locals.len()) - .map(|_| new_var(&types::Value::Nil)) - .collect(), + locals: (0..self.locals.len()).map(|_| new_var(&Nil)).collect(), upvalues: upvalues.to_vec(), }; for statement in &self.body { @@ -432,7 +433,7 @@ impl Function { } impl OpenFunction { - pub fn call(&self, env: types::Value, args: &[types::Value]) -> types::Value { + pub fn call(&self, env: Value, args: &[Value]) -> Value { match self.0.upvalues.as_slice() { [] => self.0.call(&[], args), [up] if up.0 == "_ENV" => self.0.call(&[new_var(&env)], args),