cleanup wow

This commit is contained in:
MeexReay 2024-07-28 18:37:16 +03:00
parent cc4b5bc6c9
commit 31fac01e46
14 changed files with 374 additions and 291 deletions

View File

@ -1 +1,3 @@
mod sustlang; pub mod sustlang;
pub use sustlang::*;

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,5 @@
use super::super::script::ScriptError;
#[derive(PartialEq, Clone, Debug, Copy, Hash)] #[derive(PartialEq, Clone, Debug, Copy, Hash)]
pub enum CommandType { pub enum CommandType {
/// Инициализировать переменную `name_var` с типом `type_var` /// Инициализировать переменную `name_var` с типом `type_var`

View File

@ -1,2 +1,5 @@
mod command; pub mod command;
mod command_type; pub mod command_type;
pub use command::*;
pub use command_type::*;

View File

@ -1,3 +1,7 @@
mod command; pub mod command;
mod script; pub mod script;
mod var; pub mod var;
pub use command::*;
pub use script::*;
pub use var::*;

View File

@ -1,3 +1,5 @@
use std::{error::Error, fmt::Display};
#[derive(Debug)] #[derive(Debug)]
pub enum ScriptError { pub enum ScriptError {
ParseVarError, ParseVarError,

View File

@ -1,9 +1,16 @@
use super::super::command::{Command, CommandType};
use super::super::script::ScriptError;
use super::super::var::{VarType, Variable};
use super::RunningScript;
use std::collections::HashMap;
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
pub struct Function { pub struct Function {
name: String, pub name: String,
result_type: VarType, pub result_type: VarType,
parameters: HashMap<String, VarType>, pub parameters: HashMap<String, VarType>,
commands: Vec<Command>, pub commands: Vec<Command>,
} }
impl Function { impl Function {
@ -21,46 +28,53 @@ impl Function {
} }
} }
pub fn execute_function( pub fn execute(
&self, &self,
function: Function, script: &mut RunningScript,
result_var: String, result_var: String,
args: Vec<Variable>, args: Vec<Variable>,
globals: &mut HashMap<String, Variable>, globals: &mut HashMap<String, Variable>,
is_global: bool, is_global: bool,
) -> Result<(), ScriptError> { ) -> Result<(), (ScriptError, Command)> {
let mut locals: HashMap<String, Variable> = HashMap::new(); let mut locals: HashMap<String, Variable> = HashMap::new();
let mut index = 0; let mut index = 0;
for (k, _) in function.parameters { for (k, _) in self.parameters.clone() {
locals.insert(k, args[index].clone()); locals.insert(k, args[index].clone());
index += 1; index += 1;
} }
locals.insert( locals.insert(
"result".to_string(), "result".to_string(),
Variable::empty_var(function.result_type)?, Variable::empty_var(self.result_type.clone()).unwrap(),
); );
let mut temp_vars: Vec<String> = Vec::new(); let mut temp_vars: Vec<String> = Vec::new();
for command in function.commands { for command in self.commands.clone() {
if let CommandType::Return = command.command_type { if let CommandType::Return = command.command_type {
return Ok(()); return Ok(());
} }
self.exec_command(command.clone(), false, &mut locals, &mut temp_vars)?; command
.execute(script, is_global, &mut locals, globals, &mut temp_vars)
.map_err(|f| (f, command.clone()))?;
if let CommandType::TempVar = command.command_type { if let CommandType::TempVar = command.command_type {
continue; continue;
} }
for ele in temp_vars.clone() { for ele in temp_vars.clone() {
self.variables.remove(&ele); script.drop_var(ele, &mut locals);
} }
} }
if result_var != "null" { if result_var != "null" {
self.variables script.set_var(
.insert(result_var, locals.get("result").unwrap().clone()); result_var,
locals.get("result").unwrap().clone(),
is_global,
false,
&mut locals,
);
} }
Ok(()) Ok(())

View File

@ -1,4 +1,9 @@
mod error; pub mod error;
mod function; pub mod function;
mod running_script; pub mod running_script;
mod script; pub mod script;
pub use error::*;
pub use function::*;
pub use running_script::*;
pub use script::*;

View File

@ -1,3 +1,11 @@
use super::super::command::Command;
use super::super::script::{Function, Script, ScriptError};
use super::super::var::{VarType, Variable};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::sync::{Arc, Mutex};
pub struct RunningScript { pub struct RunningScript {
main_function: Function, main_function: Function,
functions: Vec<Function>, functions: Vec<Function>,
@ -11,7 +19,7 @@ impl RunningScript {
variables: HashMap::new(), variables: HashMap::new(),
main_function: Function::new( main_function: Function::new(
"main".to_string(), "main".to_string(),
"null".to_string(), VarType::Null,
HashMap::new(), HashMap::new(),
script.commands, script.commands,
), ),
@ -166,7 +174,7 @@ impl RunningScript {
Err(ScriptError::UnknownVarError) Err(ScriptError::UnknownVarError)
} }
fn set_var( pub fn set_var(
&mut self, &mut self,
name: String, name: String,
value: Variable, value: Variable,
@ -280,6 +288,9 @@ impl RunningScript {
} }
pub fn run(&mut self) -> Result<(), (ScriptError, Command)> { pub fn run(&mut self) -> Result<(), (ScriptError, Command)> {
self.exec_commands(self.commands.clone(), true, &mut HashMap::new()) let globals = &mut self.variables.clone();
let main_function = self.main_function.clone();
main_function.execute(self, "null".to_string(), Vec::new(), globals, true)
} }
} }

View File

@ -1,3 +1,9 @@
use super::super::command::{Command, CommandType};
use super::super::script::{Function, ScriptError};
use super::super::var::VarType;
use std::collections::HashMap;
fn prepare_script(text: String) -> Vec<String> { fn prepare_script(text: String) -> Vec<String> {
text.lines() text.lines()
.map(|s| match s.split_once("#") { .map(|s| match s.split_once("#") {
@ -98,8 +104,8 @@ fn cut_funcs(commands: &mut Vec<Command>) -> Result<Vec<Function>, (ScriptError,
} }
pub struct Script { pub struct Script {
commands: Vec<Command>, pub commands: Vec<Command>,
functions: Vec<Function>, pub functions: Vec<Function>,
} }
impl Script { impl Script {

View File

@ -1,2 +1,5 @@
mod var_type; pub mod var_type;
mod variable; pub mod variable;
pub use var_type::*;
pub use variable::*;

View File

@ -1,3 +1,5 @@
use super::super::script::ScriptError;
#[derive(PartialEq, Clone, Debug, Hash)] #[derive(PartialEq, Clone, Debug, Hash)]
pub enum VarType { pub enum VarType {
Bool, Bool,

View File

@ -1,3 +1,12 @@
use super::super::script::ScriptError;
use super::var_type::VarType;
use std::collections::HashMap;
use std::hash::Hash;
use std::io::{Read, Write};
use std::ptr::hash;
use std::sync::{Arc, Mutex};
#[derive(Clone)] #[derive(Clone)]
pub enum Variable { pub enum Variable {
Bool(VarType, Option<bool>), Bool(VarType, Option<bool>),

View File

@ -1,8 +1,6 @@
INIT_VAR char space INIT_VAR char space
SET_VAR space 32 SET_VAR space 32
SET_VAR new_line 10
INIT_VAR int one INIT_VAR int one
SET_VAR one 1 SET_VAR one 1