it takes shape...

This commit is contained in:
numzero 2025-04-19 12:25:28 +03:00
parent b0e201fccb
commit b93674fe3f

View File

@ -31,7 +31,7 @@ enum Statement {
#[derive(Debug, Clone)]
enum Location {
Variable {
name: Ident,
id: Ident,
},
Field {
table: Box<Location>,
@ -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)
}