From 17ffb11f344735377acb51733c77a21ad2e043e5 Mon Sep 17 00:00:00 2001 From: afonya Date: Wed, 18 Jun 2025 14:46:59 +0200 Subject: [PATCH] prepare for coroutine --- src/compiler.rs | 18 +++---- src/enviroment.rs | 2 +- src/errors.rs | 12 +++-- src/lexer.rs | 4 +- src/main.rs | 34 +++++++++++-- src/parser.rs | 112 +++++++++++++++++++++--------------------- src/virtualmachine.rs | 46 +---------------- test.asl | 9 +--- 8 files changed, 107 insertions(+), 130 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 45425fb..82508a3 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -310,7 +310,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ASTPart::Else(else_part) => { if get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).is_none() { let err = create_error(&format!("Else used without an if statement before it"), else_part.pos, ErrorType::SemanticError, ErrorSubType::ElseWithoutIf, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let reg = get_register_by_variable(registers, get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").clone()); @@ -358,7 +358,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ASTPart::ElseIf(elseif_part) => { if get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).is_none() { let err = create_error(&format!("Else if used without an if statement before it"), elseif_part.pos, ErrorType::SemanticError, ErrorSubType::ElseWithoutIf, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let reg = get_register_by_variable(registers, get_variable_by_name(variables, "__LASTIF", ops.len(), traceback).expect("__LASTIF should exist").clone()); @@ -472,12 +472,12 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va }, ASTPart::Break(brk) => { let err = create_error(&format!("Unexpected break outside of loop"), brk.pos, ErrorType::SemanticError, ErrorSubType::BreakContinueWithoutLoop, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, ASTPart::Continue(cont) => { let err = create_error(&format!("Unexpected continue outside of loop"), cont.pos, ErrorType::SemanticError, ErrorSubType::BreakContinueWithoutLoop, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, ASTPart::For(for_part) => { @@ -575,7 +575,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va }, _ => { let err = create_error(&format!("Unknown operator `{}`", op.operator), op.pos, ErrorType::SyntaxError, ErrorSubType::UnknownOperation, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, }; @@ -586,7 +586,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ASTPart::VarUpdate(upd) => { if get_variable_by_name(variables, &upd.variable, ops.len(), traceback).is_none() { let err = create_error(&format!("Variable `{}` does not exist", upd.variable), upd.pos, ErrorType::SemanticError, ErrorSubType::VariableNotFound, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let value_reg = do_ast_op(*upd.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); @@ -602,7 +602,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va ASTPart::Assigment(asign) => { if get_variable_by_name(variables, &asign.variable, ops.len(), traceback).is_some() { let err = create_error(&format!("Variable `{}` already exists", asign.variable), asign.pos, ErrorType::SemanticError, ErrorSubType::VariableAlreadyExists, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let reg = do_ast_op(*asign.value, op_count, ops, variables, next_var_id, strings, next_string_id, functions, next_function_id, registers, ctx, traceback); @@ -673,7 +673,7 @@ fn do_ast_op(ast_op: ASTPart, op_count: &mut usize, ops: &mut Vec, va }, Err(e) => { let err = create_error(&format!("Failed to read file `{}`: {}", impr.path, e), impr.pos, ErrorType::IOError, ErrorSubType::FileError, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } @@ -727,7 +727,7 @@ fn compile_function(ast: Vec, args: Option>, registers: &mu for arg in arg_list { if get_variable_by_name(&variables, &arg, 0, &empty_tb).is_some() { let err = create_error(&format!("Argument `{}` already exists", arg), 0, ErrorType::SemanticError, ErrorSubType::ArgumentDuplication, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } variables.push( Variable { name: arg, id: *next_var_id, start: 0, end: 0, no_end: true }); diff --git a/src/enviroment.rs b/src/enviroment.rs index 57a2488..c33fff6 100644 --- a/src/enviroment.rs +++ b/src/enviroment.rs @@ -61,7 +61,7 @@ fn error(msg: String, machine: &Machine, op: &DecompiledOperation) { let curr = machine.call_stack.len()-1; let func = machine.call_stack[curr].func; let err = create_error(&msg, op.pos, ErrorType::MachineError, ErrorSubType::RuntimeError, &machine.ctx[func]); - print_error(&err, &machine.ctx[func]); + print_error(&err); process::exit(1); } fn set_mem_tbl_val(tbl: &mut VMMemoryTable, key: VMMemory, value: VMMemory) { diff --git a/src/errors.rs b/src/errors.rs index 8238889..30b51d6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -53,6 +53,7 @@ pub struct ASLError { pub typ: ErrorType, pub subtype: ErrorSubType, pub code: String, + ctx: Context } pub fn convert_types_to_string(typ: &ErrorType) -> String { @@ -190,6 +191,7 @@ pub fn create_error(message: &str, position: usize, typ: ErrorType, stype: Error typ, subtype: stype, code, + ctx: ctx.clone() } } @@ -232,7 +234,7 @@ fn get_sorrunding_lines(file: &String, line: usize) -> (String, String, String) (before, current, after) } -pub fn print_error(error: &ASLError, ctx: &Context) { +pub fn print_error(error: &ASLError) { let mut out = String::new(); out.push_str(&convert_types_to_string(&error.typ)); if error.message.len() < 1 { @@ -241,18 +243,18 @@ pub fn print_error(error: &ASLError, ctx: &Context) { out.push_str(&error.message); } - if ctx.known { + if error.ctx.known { out.push_str(" at position "); - let (line, column) = get_exact_pos(&ctx.raw_file, error.position); - out.push_str(&ctx.file); + let (line, column) = get_exact_pos(&error.ctx.raw_file, error.position); + out.push_str(&error.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); + let (before, current, after) = get_sorrunding_lines(&error.ctx.raw_file, line); if line > 1 { out.push_str(&(line-1).to_string()); out.push_str(" | "); diff --git a/src/lexer.rs b/src/lexer.rs index 5fbd48e..22fb976 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -71,7 +71,7 @@ fn read_string(splitted: &Vec<&str>, pos: &mut usize, out: &mut Vec, ctx: } if !success { let err = create_error("Unexpected end of string", *pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 5; @@ -95,7 +95,7 @@ fn read_comment(splitted: &Vec<&str>, pos: &mut usize, is_multiline: bool, ctx: } if !success { let err = create_error("Unexpected end of comment", *pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; diff --git a/src/main.rs b/src/main.rs index 00e2567..4d14a0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{env, fs, time::Instant}; use virtualmachine::Machine; -use crate::{decompiler::{operation_to_name, process}, errors::{create_error, print_error, reverse_subtype_short, reverse_type_short}}; +use crate::{decompiler::{operation_to_name, process}, errors::{create_error, print_error, reverse_subtype_short, reverse_type_short}, virtualmachine::VMState}; mod lexer; mod parser; @@ -12,7 +12,7 @@ mod decompiler; const CLIVER: [u8; 3] = [0, 3, 0]; -#[derive(Clone)] +#[derive(Debug, Clone)] struct Context { file: String, raw_file: String, @@ -59,7 +59,19 @@ fn main() { println!("Build successful. Took: {}ms", ntime.as_millis()); let mut vm = Machine::new(contexts); vm.load(&compiled); - vm.resume(); + while let VMState::Paused = vm.state { + vm.resume(); + } + match vm.state { + VMState::Finished => { + println!("Execution finished successfully."); + }, + VMState::Error(err) => { + print_error(&err); + panic!("Execution failed with an error."); + }, + _ => {} + } }, Result::Err(err) => { panic!("Can't read file: {}", err); @@ -76,7 +88,19 @@ fn main() { } let mut vm = Machine::new(contexts); vm.load(&data); - vm.resume(); + while let VMState::Paused = vm.state { + vm.resume(); + } + match vm.state { + VMState::Finished => { + println!("Execution finished successfully."); + }, + VMState::Error(err) => { + print_error(&err); + panic!("Execution failed with an error."); + }, + _ => {} + } }, Result::Err(err) => { panic!("Can't read file: {}", err); @@ -149,7 +173,7 @@ fn main() { } let func_ctx = contexts[errcode_data[2].parse::().unwrap()].clone(); let error = create_error("", errcode_data[3].parse().unwrap(), reverse_type_short(errcode_data[0].to_string()), reverse_subtype_short(errcode_data[1].to_string()), &func_ctx); - print_error(&error, &func_ctx); + print_error(&error); return; }, Result::Err(err) => { diff --git a/src/parser.rs b/src/parser.rs index e312670..e52fee6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -278,7 +278,7 @@ fn shunt(input: Vec, ctx: &Context) -> ASTPart { println!("{:?}", output); if i < 1 { let err = create_error(&format!("Unexpected operation `{}`", op.operator), op.pos, ErrorType::SemanticError, ErrorSubType::UnexpectedOperation, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let left = output[i-1].clone(); @@ -292,7 +292,7 @@ fn shunt(input: Vec, ctx: &Context) -> ASTPart { } else { if i < 2 { let err = create_error(&format!("Unexpected operation `{}`", op.operator), op.pos, ErrorType::SemanticError, ErrorSubType::UnexpectedOperation, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let left = output[i-2].clone(); @@ -315,7 +315,7 @@ fn shunt(input: Vec, ctx: &Context) -> ASTPart { } if output.len() == 0 { let err = create_error(&format!("No expressions found after applying order of operations"), 0, ErrorType::SemanticError, ErrorSubType::NoExpression, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } return output[0].clone(); @@ -326,7 +326,7 @@ fn read_function(input: &Vec, pos: &mut usize, with_args: bool, ctx: &Con if with_args { if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "(" { let err = create_error(&format!("Expected `(`"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -343,20 +343,20 @@ fn read_function(input: &Vec, pos: &mut usize, with_args: bool, ctx: &Con args.push(token.value.clone()); } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" { let err = create_error(&format!("Unexpected end of arguments"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "{" { let err = create_error(&format!("Expected {{"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -371,7 +371,7 @@ fn read_function(input: &Vec, pos: &mut usize, with_args: bool, ctx: &Con let body = parse_internal(input, &op_ends, &parse_ends, pos, ctx); if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "}" { let err = create_error(&format!("Unexpected end of function"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -404,25 +404,25 @@ fn read_table(input: &Vec, pos: &mut usize, ctx: &Context) -> ASTPart { match keyy { ASTPart::Table(_) => { let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, ASTPart::Function(_) => { let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, _ => {} } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" { let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "=" { let err = create_error(&format!("Expected `=` after key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -444,7 +444,7 @@ fn read_table(input: &Vec, pos: &mut usize, ctx: &Context) -> ASTPart { continue; } else { let err = create_error(&format!("Unexpected end of table"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } @@ -504,7 +504,7 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: expressions.push(func); } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } else if token.typ == TokenType::IDENTIFIER { @@ -522,19 +522,19 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: match keyy { ASTPart::Table(_) => { let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, ASTPart::Function(_) => { let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, _ => {} } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" { let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -546,7 +546,7 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: let keyy = &input[*pos]; if keyy.typ != TokenType::IDENTIFIER { let err = create_error(&format!("Expected identifier after `.`"), keyy.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -568,7 +568,7 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: *pos += 1; } else { let err = create_error(&format!("Unclosed parenthesis"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Unclosed, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } expressions.push(exp); @@ -577,12 +577,12 @@ fn read_exp(pos: &mut usize, input: &Vec, ends: &Vec, parse_ends: expressions.push(tbl); } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } @@ -612,19 +612,19 @@ fn check_continue(pos: &mut usize, input: &Vec, prev: ASTPart, op_ends: & match keyy { ASTPart::Table(_) => { let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, ASTPart::Function(_) => { let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, _ => {} } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" { let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -636,7 +636,7 @@ fn check_continue(pos: &mut usize, input: &Vec, prev: ASTPart, op_ends: & let keyy = &input[*pos]; if keyy.typ != TokenType::IDENTIFIER { let err = create_error(&format!("Expected identifier after `.`"), keyy.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -669,7 +669,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par *pos += 1; if variable.typ != TokenType::IDENTIFIER { let err = create_error(&format!("Unexpected `{:?}`", variable.typ,), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } let eq = &input[*pos]; @@ -681,7 +681,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par } else if token.value == "ha geny" { if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" { let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -691,7 +691,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par let condition = read_exp(pos, input, &condition_end, &condition_end, ctx); if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" { let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -700,7 +700,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(func) => func.body, _ => { let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; @@ -708,7 +708,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par } else if token.value == "amíg geny" { if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" { let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -718,7 +718,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par let condition = read_exp(pos, input, &condition_end, &condition_end, ctx); if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" { let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -727,7 +727,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(func) => func.body, _ => { let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; @@ -737,7 +737,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par } else if token.value == "kopva" { if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" { let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -748,31 +748,31 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par let init = parse_internal(input, &ends, &ends, pos, ctx); if init.len() != 1 { let err = create_error(&format!("Only one expression is expected for init"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } if input[*pos].typ != TokenType::OPEND || input[*pos].value != ";" { let err = create_error(&format!("Unexpected end of init"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; let condition = read_exp(pos, input, &ends, &ends, ctx); if input[*pos].typ != TokenType::OPEND || input[*pos].value != ";" { let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; let update = parse_internal(input, &ends, &ends, pos, ctx); if update.len() != 1 { let err = create_error(&format!("Only one expression is expected for update"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" { let err = create_error(&format!("Unexpected end of update"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -781,7 +781,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(func) => func.body, _ => { let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; @@ -791,7 +791,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par } else if token.value == "ha nem geny akkor geny" { if next_token.typ != TokenType::SEPARATOR || next_token.value != "(" { let err = create_error(&format!("Expected `(`"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -801,7 +801,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par let condition = read_exp(pos, input, &condition_end, &condition_end, ctx); if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != ")" { let err = create_error(&format!("Unexpected end of condition"), token.pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -810,7 +810,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(func) => func.body, _ => { let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; @@ -821,7 +821,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(func) => func.body, _ => { let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; @@ -838,19 +838,19 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par *pos += 1; if var.typ != TokenType::IDENTIFIER { let err = create_error(&format!("Expected identifier after hámozd"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } if input[*pos].typ != TokenType::KEYWORD || (input[*pos].value != "be" && input[*pos].value != "ba") { let err = create_error(&format!("Expected `be`/`ba` after hámozd"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; let path = &input[*pos]; if path.typ != TokenType::STRING { let err = create_error(&format!("Expected string for hámozd"), path.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -870,13 +870,13 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(f) => f, _ => { let err = create_error(&format!("Expected function body"), token.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; if input[*pos].typ != TokenType::KEYWORD || input[*pos].value != "csecs" { let err = create_error(&format!("Expected `csecs` after piszolj"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -885,14 +885,14 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par ASTPart::Function(f) => f, _ => { let err = create_error(&format!("Expected function body"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } }; return ASTPart::TryCatch(AstTryCatch { try_block: tryp, catch_block: catchp, pos: token.pos }); } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } else if token.typ == TokenType::IDENTIFIER { @@ -906,7 +906,7 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par let keyy = &input[*pos]; if keyy.typ != TokenType::IDENTIFIER { let err = create_error(&format!("Expected identifier after `.`"), keyy.pos, ErrorType::SyntaxError, ErrorSubType::Expected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -921,19 +921,19 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par match keyy { ASTPart::Table(_) => { let err = create_error(&format!("Table keys cannot be tables"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, ASTPart::Function(_) => { let err = create_error(&format!("Table keys cannot be functions"), input[*pos].pos, ErrorType::SemanticError, ErrorSubType::InvalidTableKeys, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); }, _ => {} } if input[*pos].typ != TokenType::SEPARATOR || input[*pos].value != "]" { let err = create_error(&format!("Unexpected end of key"), input[*pos].pos, ErrorType::SyntaxError, ErrorSubType::UnexpectedEnd, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } *pos += 1; @@ -951,12 +951,12 @@ fn next_operation(pos: &mut usize, input: &Vec, op_ends: &Vec, par return ASTPart::VarUpdate(AstVarUpdate { variable: token.value.clone(), value: Box::new(value), pos: token.pos }); } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } else { let err = create_error(&format!("Unexpected `{:?}({})`", token.typ, token.value), token.pos, ErrorType::SyntaxError, ErrorSubType::Unexpected, ctx); - print_error(&err, &ctx); + print_error(&err); process::exit(1); } } diff --git a/src/virtualmachine.rs b/src/virtualmachine.rs index 757dcc3..efbb4aa 100644 --- a/src/virtualmachine.rs +++ b/src/virtualmachine.rs @@ -1,5 +1,5 @@ use std::{any::Any, collections::HashMap, vec}; -use crate::{decompiler::{operation_to_name, process, DecompiledFunction, DecompiledOperation}, enviroment, errors::{convert_subtypes_to_string, convert_types_to_string, create_error, print_error, ASLError, ErrorSubType, ErrorType}, Context}; +use crate::{decompiler::{operation_to_name, process, DecompiledFunction, DecompiledOperation}, enviroment, errors::{convert_subtypes_to_string, convert_types_to_string, create_error, ASLError, ErrorSubType, ErrorType}, Context}; #[derive(Debug, Clone)] pub enum VMMemory { @@ -65,7 +65,7 @@ pub struct CallStack { } #[derive(Debug, Clone)] -enum VMState { +pub enum VMState { Running, Paused, Finished, @@ -242,7 +242,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -255,7 +254,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -280,7 +278,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -317,7 +314,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -341,7 +337,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -368,7 +363,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -389,7 +383,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -402,7 +395,6 @@ fn do_operation_operation(machine: &mut Machine, operation: &DecompiledOperation fallback_to_catch(memory, &mut machine.state, &mut machine.functions, &mut machine.call_stack, executed_stack, executed_func, err); return; } else { - print_error(&err, &ctx); machine.state = VMState::Error(err); return; } @@ -496,7 +488,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -515,7 +506,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -528,7 +518,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -580,7 +569,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -594,7 +582,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -625,7 +612,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -638,7 +624,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -664,7 +649,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -688,7 +672,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -701,7 +684,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -718,7 +700,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -748,7 +729,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -762,7 +742,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -781,7 +760,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -798,7 +776,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -812,7 +789,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -832,7 +808,6 @@ impl Machine { nocon = true; break; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -853,7 +828,6 @@ impl Machine { nocon = true; break; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -880,7 +854,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -904,7 +877,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -921,7 +893,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -935,7 +906,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -954,7 +924,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -967,7 +936,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -999,7 +967,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1012,7 +979,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1035,7 +1001,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1062,7 +1027,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1076,7 +1040,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1100,7 +1063,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1128,7 +1090,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1143,7 +1104,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1162,7 +1122,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } @@ -1176,7 +1135,6 @@ impl Machine { executed_func = &func_clone[self.call_stack[executed_stack].func]; continue; } else { - print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); self.state = VMState::Error(err); return; } diff --git a/test.asl b/test.asl index 451416f..dfffe62 100644 --- a/test.asl +++ b/test.asl @@ -1,8 +1 @@ -lőcsve test(a,b) { - ugass(a,b) -} -piszolj { - test(0) -} csecs (e) { - ugass(e) -} \ No newline at end of file +gethelj test = szaft"a"szaft \ No newline at end of file