diff --git a/src/compiler.rs b/src/compiler.rs index 7c5ebd8..2fed51e 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -166,7 +166,6 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va }, ASTPart::Function(func) => { let func_id = *next_function_id; - functions.insert(func_id, compile_function(func.body, Some(func.args), registers)); *next_function_id += 1; let reg = allocate_register(registers); @@ -185,13 +184,28 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va } ops.push(Operation { opcode: 27, arg1: Some(func), arg2: None, arg3: None }); }, + ASTPart::If(if_part) => { + let condition_reg = do_ast_op(*if_part.condition, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); + ops.push(Operation { opcode: 24, arg1: Some(condition_reg), arg2: Some(condition_reg as i64), arg3: None }); + let op_placeholder = ops.len(); + ops.push(Operation { opcode: 0, arg1: None, arg2: None, arg3: None }); + for if_op in if_part.body { + do_ast_op(if_op, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); + } + ops[op_placeholder] = Operation { + opcode: 26, + arg1: Some(condition_reg), + arg2: Some(ops.len() as i64), + arg3: None, + }; + garbage_collect_registers(registers); + }, ASTPart::Operation(op) => { let left_reg = do_ast_op(*op.left, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); let right_reg = do_ast_op(*op.right, op_count, ops, var_ids, next_var_id, strings, next_string_id, functions, next_function_id, registers); let reg = allocate_register(registers); if reg.unbind_before { ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None }); - set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count }); } let opcode = match op.operator.as_str() { "+" => 10, @@ -271,6 +285,7 @@ fn compile_function(ast: Vec, args: Option>, registers: &mu for ast_op in ast { do_ast_op(ast_op, &mut op_count, &mut ops, &mut var_ids, &mut next_var_id, &mut strings, &mut next_string_id, &mut functions, &mut next_function_id, registers); } + ops.push(Operation { opcode: 29, arg1: Some(0), arg2: None, arg3: None }); println!("Operations: {:?}\n", ops); println!("Registers: {:?}\n", registers); println!("Variable IDs: {:?}", var_ids); diff --git a/test.as b/test.as index 2d04d8e..a9b60f8 100644 --- a/test.as +++ b/test.as @@ -1,2 +1,5 @@ gethelj a = 1 -b = 2 \ No newline at end of file +ha geny (1==1) { + gethelj b = 2 +} +gethelj c = 3 \ No newline at end of file