|
|
|
@ -71,7 +71,9 @@ pub(super) fn generate_type(t: Either<Variable, Option<Type>>) -> String {
|
|
|
|
|
fn generate_function(func: Function) -> String { |
|
|
|
|
let mut buf = String::new(); |
|
|
|
|
buf += &format!("{} ", &generate_function_signature(func.clone())); |
|
|
|
|
buf += &generate_block(func.body); |
|
|
|
|
if let Statement::Block(statements, scope) = func.body { |
|
|
|
|
buf += &generate_block(statements, scope); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buf |
|
|
|
|
} |
|
|
|
@ -87,15 +89,10 @@ fn generate_function_signature(func: Function) -> String {
|
|
|
|
|
format!("{T} {N}({A})", T = t, N = func.name, A = arguments) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn generate_block(block: Statement) -> String { |
|
|
|
|
fn generate_block(block: Vec<Statement>, scope: Vec<Variable>) -> String { |
|
|
|
|
let mut generated = String::from("{\n"); |
|
|
|
|
|
|
|
|
|
let statements = match block { |
|
|
|
|
Statement::Block(blk) => blk, |
|
|
|
|
_ => panic!("Block body should be of type Statement::Block"), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
for statement in statements { |
|
|
|
|
for statement in block { |
|
|
|
|
generated += &generate_statement(statement); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -113,7 +110,7 @@ fn generate_statement(statement: Statement) -> String {
|
|
|
|
|
generate_conditional(expr, *if_state, else_state.map(|x| *x)) |
|
|
|
|
} |
|
|
|
|
Statement::Assign(name, state) => generate_assign(*name, *state), |
|
|
|
|
Statement::Block(_) => generate_block(statement), |
|
|
|
|
Statement::Block(statements, scope) => generate_block(statements, scope), |
|
|
|
|
Statement::While(expr, body) => generate_while_loop(expr, *body), |
|
|
|
|
Statement::For(ident, expr, body) => todo!(), |
|
|
|
|
Statement::Continue => todo!(), |
|
|
|
@ -142,7 +139,10 @@ fn generate_while_loop(expr: Expression, body: Statement) -> String {
|
|
|
|
|
|
|
|
|
|
out_str += &generate_expression(expr); |
|
|
|
|
out_str += ") "; |
|
|
|
|
out_str += &generate_block(body); |
|
|
|
|
|
|
|
|
|
if let Statement::Block(statements, scope) = body { |
|
|
|
|
out_str += &generate_block(statements, scope); |
|
|
|
|
} |
|
|
|
|
out_str |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -175,7 +175,7 @@ fn generate_conditional(
|
|
|
|
|
let expr_str = generate_expression(expr); |
|
|
|
|
|
|
|
|
|
let body = match if_state { |
|
|
|
|
Statement::Block(blk) => blk, |
|
|
|
|
Statement::Block(blk, _) => blk, |
|
|
|
|
_ => panic!("Conditional body should be of type block"), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|