From 25dc5ab2289c83f61c24fdc835bd1d08bb186adf Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 19 Apr 2025 13:54:51 +0300 Subject: [PATCH] eval statements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At this point there isn’t any real difference between expressions and statements --- src/twopass.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/twopass.rs b/src/twopass.rs index 2770c6e..591805e 100644 --- a/src/twopass.rs +++ b/src/twopass.rs @@ -334,6 +334,31 @@ impl Eval for Expression { } } +impl Eval for Statement { + fn eval(&self, ctx: &RunContext) -> types::Value { + match self { + Statement::Assign { target, source } => { + let source = source.eval(ctx); + match target { + Location::Variable { id } => ctx.var(*id).set(source), + Location::Field { table, index } => { + let table = table.eval(ctx).expect("attempt to index a nil value"); + let index = index.eval(ctx).expect("attempt to index with a nil value"); + let types::ValueInner::Table(table) = table else { + panic!("attempt to index a non-table"); + }; + table.set(index, source) + } + } + } + Statement::Call(call) => { + call.eval(ctx); + } + }; + None + } +} + impl Function { fn call(&self, upvalues: &[types::Value], args: &[types::Value]) -> types::Value { assert_eq!(upvalues.len(), self.upvalues.len());