From b93674fe3ffee14d23caa55af71ca45ace94ffc7 Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 19 Apr 2025 12:25:28 +0300 Subject: [PATCH] it takes shape... --- src/twopass.rs | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/twopass.rs b/src/twopass.rs index 074b919..e5682ad 100644 --- a/src/twopass.rs +++ b/src/twopass.rs @@ -31,7 +31,7 @@ enum Statement { #[derive(Debug, Clone)] enum Location { Variable { - name: Ident, + id: Ident, }, Field { table: Box, @@ -123,7 +123,15 @@ impl BuildContext<'_> { } } -fn build_in_scope(parent: &BuildContext<'_>, code: ast::Function) -> Function { +fn lookup(scope: &BuildContext<'_>, loc: &ast::Location) -> Location { + todo!() +} + +fn build_expression(scope: &BuildContext<'_>, code: &ast::Expression) -> Expression { + todo!() +} + +fn build_in_scope(parent: &BuildContext<'_>, code: &ast::Function) -> Function { let scope = BuildContext { parent: Some(parent), scope: Default::default(), @@ -131,13 +139,43 @@ fn build_in_scope(parent: &BuildContext<'_>, code: ast::Function) -> Function { for (index, name) in code.args.iter().enumerate() { scope.scope.borrow_mut().add_arg(name, index); } - todo!() + let mut body = vec![]; + for statement in &code.body { + match statement { + ast::Statement::Assign { target, source } => { + let target = lookup(&scope, target); + body.push(Statement::Assign { + target, + source: Box::new(build_expression(&scope, source)), + }); + } + ast::Statement::Call { callee, args } => todo!(), + ast::Statement::Local { name, init } => { + let init = init.as_ref().map(|init| build_expression(&scope, &init)); + let id = scope.scope.borrow_mut().add_local(&name); + if let Some(init) = init { + body.push(Statement::Assign { + target: Location::Variable { id }, + source: Box::new(init), + }); + } + } + } + } + Function { + name: code.name.clone(), + args: code.args.clone(), + locals: vec![], + upvalues: vec![], + body, + ret: Box::new(Expression::Constant(ast::Constant::Nil)), + } } -pub fn build(code: ast::Function) -> Function { +pub fn build(code: &ast::Function) -> Function { let mut root = BuildContext { parent: None, scope: RefCell::new(Scope::new_toplevel()), }; - build_in_scope(&mut root, code) + build_in_scope(&mut root, &code) }