From 867799fec381965ba80f52cdb1eacffc5f53077e Mon Sep 17 00:00:00 2001
From: afonya2 <adamhir@freemail.hu>
Date: Sun, 8 Jun 2025 22:16:01 +0200
Subject: [PATCH] error system now works for multiple functions

---
 src/compiler.rs | 18 ++++++++-----
 src/errors.rs   | 69 ++++++++++++++++++++++++++-----------------------
 src/main.rs     |  8 ++++--
 test.as         |  5 +++-
 4 files changed, 59 insertions(+), 41 deletions(-)

diff --git a/src/compiler.rs b/src/compiler.rs
index cb092d8..82e2242 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -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);
 }
\ No newline at end of file
diff --git a/src/errors.rs b/src/errors.rs
index 6bcbd07..514ad39 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -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(&current);
     
-    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(&current);
+        
+        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);
 }
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 2e159f6..e2b7076 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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);
diff --git a/test.as b/test.as
index 714029a..201343c 100644
--- a/test.as
+++ b/test.as
@@ -1,2 +1,5 @@
 ugass(szaft"test"szaft, ugass)
-gethelj a = 1 + piszv
\ No newline at end of file
+gethelj a = lőcsve() {
+    gethelj b = 1 + piszv
+}
+a()
\ No newline at end of file