added function to the ASX standard
This commit is contained in:
parent
a8e174af7f
commit
598f4a0cdb
3 changed files with 126 additions and 136 deletions
|
@ -208,6 +208,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
|
|||
}
|
||||
ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some(func_id as i64), arg3: None });
|
||||
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
|
||||
return reg.register;
|
||||
},
|
||||
ASTPart::Call(call) => {
|
||||
let func = do_ast_op(*call.function, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers);
|
||||
|
@ -606,32 +607,10 @@ fn bin_to_num(bin: String) -> usize {
|
|||
return num;
|
||||
}
|
||||
|
||||
pub fn compile(ast: Vec<ASTPart>) -> Vec<u8> {
|
||||
let mut next_var_id: u32 = 1;
|
||||
let mut registers: Vec<RegisterState> = vec![];
|
||||
for i in 0..17 {
|
||||
registers.push(RegisterState {
|
||||
id: i as u8,
|
||||
used: false,
|
||||
variable: 0,
|
||||
last_used: 0,
|
||||
});
|
||||
}
|
||||
|
||||
let compiled = compile_function(ast, None, &mut registers, &mut next_var_id);
|
||||
println!("Compiled: {:?}", compiled);
|
||||
|
||||
fn compile_body(compiled: Compiled, fpos: &mut usize) -> Vec<u8> {
|
||||
let mut output: Vec<u8> = vec![];
|
||||
//doctype specifier
|
||||
append_string(&mut output, String::from("ASX"), false);
|
||||
//version
|
||||
output.push(ASXVERSION[0]);
|
||||
output.push(ASXVERSION[1]);
|
||||
output.push(ASXVERSION[2]);
|
||||
//file name
|
||||
append_string(&mut output, String::from("main.asl"), true);
|
||||
let mut additional: Vec<Vec<u8>> = vec![];
|
||||
//function
|
||||
append_string(&mut output, String::from("main"), true);
|
||||
//function headers
|
||||
append_be_num(&mut output, 4, compiled.variables.len());
|
||||
for var in compiled.variables {
|
||||
|
@ -645,10 +624,13 @@ pub fn compile(ast: Vec<ASTPart>) -> Vec<u8> {
|
|||
append_be_num(&mut output, 3, *strs as usize);
|
||||
append_string(&mut output, compiled.strings[strs].clone(), true);
|
||||
}
|
||||
append_be_num(&mut output, 4, 0);
|
||||
/*for funcs in compiled.functions.keys() {
|
||||
//not implemented yet
|
||||
}*/
|
||||
append_be_num(&mut output, 4, compiled.functions.len());
|
||||
for funcs in compiled.functions.keys() {
|
||||
*fpos += 1;
|
||||
append_be_num(&mut output, 3, *funcs as usize);
|
||||
append_be_num(&mut output, 4, *fpos);
|
||||
additional.push(compile_body(compiled.functions[funcs].clone(), fpos));
|
||||
}
|
||||
//function body
|
||||
append_be_num(&mut output, 4, compiled.operations.len());
|
||||
for ops in compiled.operations {
|
||||
|
@ -672,6 +654,39 @@ pub fn compile(ast: Vec<ASTPart>) -> Vec<u8> {
|
|||
println!("{}", op_bin);
|
||||
append_binary(&mut output, op_bin);
|
||||
}
|
||||
for addition in additional {
|
||||
output.extend_from_slice(&addition);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn compile(ast: Vec<ASTPart>) -> Vec<u8> {
|
||||
let mut next_var_id: u32 = 1;
|
||||
let mut registers: Vec<RegisterState> = vec![];
|
||||
for i in 0..17 {
|
||||
registers.push(RegisterState {
|
||||
id: i as u8,
|
||||
used: false,
|
||||
variable: 0,
|
||||
last_used: 0,
|
||||
});
|
||||
}
|
||||
|
||||
let compiled = compile_function(ast, None, &mut registers, &mut next_var_id);
|
||||
println!("Compiled: {:?}", compiled);
|
||||
|
||||
let mut output: Vec<u8> = vec![];
|
||||
//doctype specifier
|
||||
append_string(&mut output, String::from("ASX"), false);
|
||||
//version
|
||||
output.push(ASXVERSION[0]);
|
||||
output.push(ASXVERSION[1]);
|
||||
output.push(ASXVERSION[2]);
|
||||
//functions
|
||||
let mut fpos = 0;
|
||||
let body = compile_body(compiled, &mut fpos);
|
||||
output.extend_from_slice(&body);
|
||||
|
||||
return output;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue