error system now works for multiple functions
This commit is contained in:
parent
414b740ebc
commit
867799fec3
4 changed files with 59 additions and 41 deletions
|
@ -676,9 +676,10 @@ fn bin_to_num(bin: String) -> usize {
|
|||
return num;
|
||||
}
|
||||
|
||||
fn compile_body(compiled: Compiled, fpos: &mut usize, ctx: &Context) -> Vec<u8> {
|
||||
fn compile_body(compiled: Compiled, fpos: &mut usize, ctx: &Context) -> (Vec<u8>, Vec<Context>) {
|
||||
let mut output: Vec<u8> = vec![];
|
||||
let mut additional: Vec<Vec<u8>> = vec![];
|
||||
let mut contexts: Vec<Context> = vec![ctx.clone()];
|
||||
//function
|
||||
//function headers
|
||||
append_be_num(&mut output, 4, compiled.variables.len());
|
||||
|
@ -698,7 +699,12 @@ fn compile_body(compiled: Compiled, fpos: &mut usize, ctx: &Context) -> Vec<u8>
|
|||
*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, ctx));
|
||||
let (compiled, mut context) = compile_body(compiled.functions[funcs].clone(), fpos, ctx);
|
||||
for c in &mut context {
|
||||
c.c_funcid = *fpos
|
||||
}
|
||||
contexts.extend_from_slice(&context);
|
||||
additional.push(compiled);
|
||||
}
|
||||
//function body
|
||||
append_be_num(&mut output, 4, compiled.operations.len());
|
||||
|
@ -727,10 +733,10 @@ fn compile_body(compiled: Compiled, fpos: &mut usize, ctx: &Context) -> Vec<u8>
|
|||
output.extend_from_slice(&addition);
|
||||
}
|
||||
|
||||
return output;
|
||||
return (output, contexts);
|
||||
}
|
||||
|
||||
pub fn compile(ast: Vec<ASTPart>, ctx: &Context) -> Vec<u8> {
|
||||
pub fn compile(ast: Vec<ASTPart>, ctx: &Context) -> (Vec<u8>, Vec<Context>) {
|
||||
let mut next_var_id: u32 = 1;
|
||||
let mut registers: Vec<RegisterState> = vec![];
|
||||
for i in 0..17 {
|
||||
|
@ -758,7 +764,7 @@ pub fn compile(ast: Vec<ASTPart>, ctx: &Context) -> Vec<u8> {
|
|||
output.push(0);
|
||||
//functions
|
||||
let mut fpos = 0;
|
||||
let body = compile_body(compiled, &mut fpos, ctx);
|
||||
let (body, contexts) = compile_body(compiled, &mut fpos, ctx);
|
||||
output.extend_from_slice(&body);
|
||||
|
||||
//update function count
|
||||
|
@ -768,5 +774,5 @@ pub fn compile(ast: Vec<ASTPart>, ctx: &Context) -> Vec<u8> {
|
|||
output[8] = ((fpos >> 8) & 0xFF) as u8;
|
||||
output[9] = (fpos & 0xFF) as u8;
|
||||
|
||||
return output;
|
||||
return (output, contexts);
|
||||
}
|
|
@ -169,47 +169,52 @@ fn get_sorrunding_lines(file: &String, line: usize) -> (String, String, String)
|
|||
pub fn print_error(error: &ASLError, ctx: &Context) {
|
||||
let mut out = String::new();
|
||||
out.push_str(&convert_types_to_string(&error.typ));
|
||||
if error.message.len() > 0 {
|
||||
if error.message.len() < 1 {
|
||||
out.push_str(&convert_subtypes_to_string(&error.subtype));
|
||||
} else {
|
||||
out.push_str(&error.message);
|
||||
}
|
||||
out.push_str(" at position ");
|
||||
|
||||
let (line, column) = get_exact_pos(&ctx.raw_file, error.position);
|
||||
out.push_str(&ctx.file);
|
||||
out.push_str(":");
|
||||
out.push_str(&line.to_string());
|
||||
out.push_str(":");
|
||||
out.push_str(&column.to_string());
|
||||
|
||||
out.push_str("\n");
|
||||
let (before, current, after) = get_sorrunding_lines(&ctx.raw_file, line);
|
||||
if line > 1 {
|
||||
out.push_str(&(line-1).to_string());
|
||||
out.push_str(" | ");
|
||||
} else {
|
||||
out.push_str(" | ");
|
||||
}
|
||||
out.push_str(&before);
|
||||
out.push_str("\n");
|
||||
out.push_str(&line.to_string());
|
||||
out.push_str(" | ");
|
||||
out.push_str(¤t);
|
||||
|
||||
out.push_str("\n");
|
||||
out.push_str(" ");
|
||||
out.push_str(&" ".repeat(column - 1));
|
||||
out.push_str("^ ");
|
||||
out.push_str(&error.message);
|
||||
if ctx.known {
|
||||
out.push_str(" at position ");
|
||||
|
||||
out.push_str("\n");
|
||||
out.push_str(&(line+1).to_string());
|
||||
out.push_str(" | ");
|
||||
out.push_str(&after);
|
||||
let (line, column) = get_exact_pos(&ctx.raw_file, error.position);
|
||||
out.push_str(&ctx.file);
|
||||
out.push_str(":");
|
||||
out.push_str(&line.to_string());
|
||||
out.push_str(":");
|
||||
out.push_str(&column.to_string());
|
||||
|
||||
out.push_str("\n");
|
||||
let (before, current, after) = get_sorrunding_lines(&ctx.raw_file, line);
|
||||
if line > 1 {
|
||||
out.push_str(&(line-1).to_string());
|
||||
out.push_str(" | ");
|
||||
} else {
|
||||
out.push_str(" | ");
|
||||
}
|
||||
out.push_str(&before);
|
||||
out.push_str("\n");
|
||||
out.push_str(&line.to_string());
|
||||
out.push_str(" | ");
|
||||
out.push_str(¤t);
|
||||
|
||||
out.push_str("\n");
|
||||
out.push_str(" ");
|
||||
out.push_str(&" ".repeat(column - 1));
|
||||
out.push_str("^ ");
|
||||
out.push_str(&error.message);
|
||||
|
||||
out.push_str("\n");
|
||||
out.push_str(&(line+1).to_string());
|
||||
out.push_str(" | ");
|
||||
out.push_str(&after);
|
||||
}
|
||||
|
||||
out.push_str("\n");
|
||||
out.push_str("Error Code: ");
|
||||
out.push_str(&error.code);
|
||||
out.push_str(":");
|
||||
out.push_str(&ctx.c_funcid.to_string());
|
||||
println!("{}", out);
|
||||
}
|
|
@ -139,6 +139,8 @@ fn log_ast_part(part: &ASTPart, prefix: String) {
|
|||
struct Context {
|
||||
file: String,
|
||||
raw_file: String,
|
||||
c_funcid: usize,
|
||||
known: bool
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -148,6 +150,8 @@ fn main() {
|
|||
let ctx = Context {
|
||||
file: String::from("./test.as"),
|
||||
raw_file: data.clone(),
|
||||
c_funcid: 0,
|
||||
known: true
|
||||
};
|
||||
let lexed = lexer::lex(data, &ctx);
|
||||
println!("Lexer output: ");
|
||||
|
@ -159,9 +163,9 @@ fn main() {
|
|||
for part in &ast {
|
||||
log_ast_part(part, String::from(" "));
|
||||
}
|
||||
let compiled = compiler::compile(ast, &ctx);
|
||||
let (compiled, contexts) = compiler::compile(ast, &ctx);
|
||||
println!("Compiled output: {:?}", compiled);
|
||||
let mut vm = Machine::new(vec![ctx]);
|
||||
let mut vm = Machine::new(contexts);
|
||||
vm.load(compiled);
|
||||
vm.run();
|
||||
println!("Registers: {:?}", vm.registers);
|
||||
|
|
5
test.as
5
test.as
|
@ -1,2 +1,5 @@
|
|||
ugass(szaft"test"szaft, ugass)
|
||||
gethelj a = 1 + piszv
|
||||
gethelj a = lőcsve() {
|
||||
gethelj b = 1 + piszv
|
||||
}
|
||||
a()
|
Loading…
Add table
Add a link
Reference in a new issue