begin adding try/catch
This commit is contained in:
parent
8dee35c68f
commit
1a98959be0
6 changed files with 82 additions and 17 deletions
|
@ -31,7 +31,8 @@ struct Compiled {
|
|||
operations: Vec<Operation>,
|
||||
variables: Vec<Variable>,
|
||||
strings: HashMap<u32, String>,
|
||||
functions: HashMap<u32, Compiled>
|
||||
functions: HashMap<u32, Compiled>,
|
||||
try_catch: Option<u32>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -238,7 +239,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
|
|||
variables: variables.clone(),
|
||||
previous: Some(Box::new(traceback.clone())),
|
||||
};
|
||||
functions.insert(func_id, compile_function(func.body, Some(func.args), registers, next_var_id, ctx, &self_tb));
|
||||
functions.insert(func_id, compile_function(func.body, Some(func.args), registers, next_var_id, ctx, &self_tb, None));
|
||||
*next_function_id += 1;
|
||||
let reg = allocate_register(registers);
|
||||
if reg.unbind_before {
|
||||
|
@ -658,7 +659,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
|
|||
};
|
||||
let lexed = lex(data, &imp_ctx);
|
||||
let ast = parse(lexed, &imp_ctx);
|
||||
let compiled = compile_function(ast, None, registers, next_var_id, &imp_ctx, &self_tb);
|
||||
let compiled = compile_function(ast, None, registers, next_var_id, &imp_ctx, &self_tb, None);
|
||||
functions.insert(*next_function_id, compiled);
|
||||
*next_function_id += 1;
|
||||
let reg = allocate_register(registers);
|
||||
|
@ -677,12 +678,33 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
|
|||
}
|
||||
}
|
||||
},
|
||||
ASTPart::TryCatch(tc) => {
|
||||
let catch_f = compile_function(tc.catch_block.body, None, registers, next_var_id, ctx, traceback, None);
|
||||
let mut try_f = compile_function(tc.try_block.body, None, registers, next_var_id, ctx, traceback, Some(0));
|
||||
let mut nextt_f = 1;
|
||||
for (fid, _) in &try_f.functions {
|
||||
if fid >= &nextt_f {
|
||||
nextt_f = fid+1;
|
||||
}
|
||||
}
|
||||
try_f.functions.insert(nextt_f, catch_f);
|
||||
try_f.try_catch = Some(nextt_f);
|
||||
functions.insert(*next_function_id, try_f);
|
||||
*next_function_id += 1;
|
||||
let reg = allocate_register(registers);
|
||||
if reg.unbind_before {
|
||||
ops.push(Operation { opcode: 8, arg1: Some(reg.register), arg2: None, arg3: None, pos: tc.pos as u32 });
|
||||
}
|
||||
ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some((*next_function_id-1) as f64), arg3: None, pos: tc.pos as u32 });
|
||||
ops.push(Operation { opcode: 27, arg1: Some(reg.register), arg2: None, arg3: None, pos: tc.pos as u32 });
|
||||
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
fn compile_function(ast: Vec<ASTPart>, args: Option<Vec<String>>, registers: &mut Vec<RegisterState>, next_var_id: &mut u32, ctx: &Context, traceback: &PrevFunc) -> Compiled {
|
||||
fn compile_function(ast: Vec<ASTPart>, args: Option<Vec<String>>, registers: &mut Vec<RegisterState>, next_var_id: &mut u32, ctx: &Context, traceback: &PrevFunc, try_catch: Option<u32>) -> Compiled {
|
||||
let mut ops: Vec<Operation> = vec![];
|
||||
|
||||
let mut variables: Vec<Variable> = vec![];
|
||||
|
@ -727,6 +749,7 @@ fn compile_function(ast: Vec<ASTPart>, args: Option<Vec<String>>, registers: &mu
|
|||
variables,
|
||||
strings,
|
||||
functions,
|
||||
try_catch
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -805,6 +828,14 @@ fn compile_body(compiled: Compiled, fpos: &mut usize, ctx: &Context) -> (Vec<u8>
|
|||
contexts.extend_from_slice(&context);
|
||||
additional.push(compiled);
|
||||
}
|
||||
match compiled.try_catch {
|
||||
Some(tc) => {
|
||||
append_be_num(&mut output, 4, tc as usize);
|
||||
},
|
||||
None => {
|
||||
append_be_num(&mut output, 4, 0);
|
||||
}
|
||||
}
|
||||
//function body
|
||||
append_be_num(&mut output, 4, compiled.operations.len());
|
||||
for ops in compiled.operations {
|
||||
|
@ -851,7 +882,7 @@ pub fn compile(ast: Vec<ASTPart>, ctx: &Context) -> (Vec<u8>, Vec<Context>) {
|
|||
variables: vec![],
|
||||
previous: None,
|
||||
};
|
||||
let compiled = compile_function(ast, None, &mut registers, &mut next_var_id, ctx, &empty_tb);
|
||||
let compiled = compile_function(ast, None, &mut registers, &mut next_var_id, ctx, &empty_tb, None);
|
||||
|
||||
let mut output: Vec<u8> = vec![];
|
||||
//doctype specifier
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue