added imports

This commit is contained in:
afonya2 2025-06-11 15:35:25 +02:00
parent ce93fb10d4
commit b4f930d1c5
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
8 changed files with 91 additions and 14 deletions

View file

@ -1,5 +1,5 @@
use std::{collections::HashMap, process, vec};
use crate::{errors::{create_error, print_error, ErrorType, ErrorSubType}, parser::ASTPart, Context};
use std::{collections::HashMap, fs, process, vec};
use crate::{errors::{create_error, print_error, ErrorSubType, ErrorType}, lexer::lex, parser::{parse, ASTPart}, Context};
const ASXVERSION: [u8; 3] = [0,1,0];
@ -607,6 +607,42 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec<Operation>, va
let value_reg = do_ast_op(*tbl_set.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback);
ops.push(Operation { opcode: 31, arg1: Some(table_reg), arg2: Some(key_reg as i64), arg3: Some(value_reg), pos: tbl_set.pos as u32 });
},
ASTPart::Import(impr) => {
let fi = fs::read_to_string(&impr.path);
match fi {
Ok(data) => {
let imp_ctx = Context {
file: String::from(&impr.path),
raw_file: data.clone(),
c_funcid: 0,
known: true
};
let self_tb = PrevFunc {
variables: variables.clone(),
previous: Some(Box::new(traceback.clone())),
};
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);
functions.insert(*next_function_id, compiled);
*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: impr.pos as u32 });
}
ops.push(Operation { opcode: 5, arg1: Some(reg.register), arg2: Some((*next_function_id-1) as i64), arg3: None, pos: impr.pos as u32 });
ops.push(Operation { opcode: 27, arg1: Some(reg.register), arg2: Some(reg.register as i64), arg3: None, pos: impr.pos as u32 });
set_register(registers, RegisterState { id: reg.register, used: true, variable: 0, last_used: *op_count });
return reg.register;
},
Err(e) => {
let err = create_error(&format!("Failed to read file `{}`: {}", impr.path, e), impr.pos, ErrorType::IOError, ErrorSubType::FileError);
print_error(&err, &ctx);
process::exit(1);
}
}
},
_ => {}
}
return 0;