mirror of
https://github.com/MeexReay/sustlang.git
synced 2025-06-24 02:23:00 +03:00
cleanup wow
This commit is contained in:
parent
cc4b5bc6c9
commit
31fac01e46
@ -1 +1,3 @@
|
||||
mod sustlang;
|
||||
pub mod sustlang;
|
||||
|
||||
pub use sustlang::*;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,5 @@
|
||||
use super::super::script::ScriptError;
|
||||
|
||||
#[derive(PartialEq, Clone, Debug, Copy, Hash)]
|
||||
pub enum CommandType {
|
||||
/// Инициализировать переменную `name_var` с типом `type_var`
|
||||
|
@ -1,2 +1,5 @@
|
||||
mod command;
|
||||
mod command_type;
|
||||
pub mod command;
|
||||
pub mod command_type;
|
||||
|
||||
pub use command::*;
|
||||
pub use command_type::*;
|
||||
|
@ -1,3 +1,7 @@
|
||||
mod command;
|
||||
mod script;
|
||||
mod var;
|
||||
pub mod command;
|
||||
pub mod script;
|
||||
pub mod var;
|
||||
|
||||
pub use command::*;
|
||||
pub use script::*;
|
||||
pub use var::*;
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::{error::Error, fmt::Display};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ScriptError {
|
||||
ParseVarError,
|
||||
|
@ -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)]
|
||||
pub struct Function {
|
||||
name: String,
|
||||
result_type: VarType,
|
||||
parameters: HashMap<String, VarType>,
|
||||
commands: Vec<Command>,
|
||||
pub name: String,
|
||||
pub result_type: VarType,
|
||||
pub parameters: HashMap<String, VarType>,
|
||||
pub commands: Vec<Command>,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
@ -21,46 +28,53 @@ impl Function {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute_function(
|
||||
pub fn execute(
|
||||
&self,
|
||||
function: Function,
|
||||
script: &mut RunningScript,
|
||||
result_var: String,
|
||||
args: Vec<Variable>,
|
||||
globals: &mut HashMap<String, Variable>,
|
||||
is_global: bool,
|
||||
) -> Result<(), ScriptError> {
|
||||
) -> Result<(), (ScriptError, Command)> {
|
||||
let mut locals: HashMap<String, Variable> = HashMap::new();
|
||||
let mut index = 0;
|
||||
for (k, _) in function.parameters {
|
||||
for (k, _) in self.parameters.clone() {
|
||||
locals.insert(k, args[index].clone());
|
||||
index += 1;
|
||||
}
|
||||
locals.insert(
|
||||
"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();
|
||||
|
||||
for command in function.commands {
|
||||
for command in self.commands.clone() {
|
||||
if let CommandType::Return = command.command_type {
|
||||
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 {
|
||||
continue;
|
||||
}
|
||||
|
||||
for ele in temp_vars.clone() {
|
||||
self.variables.remove(&ele);
|
||||
script.drop_var(ele, &mut locals);
|
||||
}
|
||||
}
|
||||
|
||||
if result_var != "null" {
|
||||
self.variables
|
||||
.insert(result_var, locals.get("result").unwrap().clone());
|
||||
script.set_var(
|
||||
result_var,
|
||||
locals.get("result").unwrap().clone(),
|
||||
is_global,
|
||||
false,
|
||||
&mut locals,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -1,4 +1,9 @@
|
||||
mod error;
|
||||
mod function;
|
||||
mod running_script;
|
||||
mod script;
|
||||
pub mod error;
|
||||
pub mod function;
|
||||
pub mod running_script;
|
||||
pub mod script;
|
||||
|
||||
pub use error::*;
|
||||
pub use function::*;
|
||||
pub use running_script::*;
|
||||
pub use script::*;
|
||||
|
@ -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 {
|
||||
main_function: Function,
|
||||
functions: Vec<Function>,
|
||||
@ -11,7 +19,7 @@ impl RunningScript {
|
||||
variables: HashMap::new(),
|
||||
main_function: Function::new(
|
||||
"main".to_string(),
|
||||
"null".to_string(),
|
||||
VarType::Null,
|
||||
HashMap::new(),
|
||||
script.commands,
|
||||
),
|
||||
@ -166,7 +174,7 @@ impl RunningScript {
|
||||
Err(ScriptError::UnknownVarError)
|
||||
}
|
||||
|
||||
fn set_var(
|
||||
pub fn set_var(
|
||||
&mut self,
|
||||
name: String,
|
||||
value: Variable,
|
||||
@ -280,6 +288,9 @@ impl RunningScript {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
text.lines()
|
||||
.map(|s| match s.split_once("#") {
|
||||
@ -98,8 +104,8 @@ fn cut_funcs(commands: &mut Vec<Command>) -> Result<Vec<Function>, (ScriptError,
|
||||
}
|
||||
|
||||
pub struct Script {
|
||||
commands: Vec<Command>,
|
||||
functions: Vec<Function>,
|
||||
pub commands: Vec<Command>,
|
||||
pub functions: Vec<Function>,
|
||||
}
|
||||
|
||||
impl Script {
|
||||
|
@ -1,2 +1,5 @@
|
||||
mod var_type;
|
||||
mod variable;
|
||||
pub mod var_type;
|
||||
pub mod variable;
|
||||
|
||||
pub use var_type::*;
|
||||
pub use variable::*;
|
||||
|
@ -1,3 +1,5 @@
|
||||
use super::super::script::ScriptError;
|
||||
|
||||
#[derive(PartialEq, Clone, Debug, Hash)]
|
||||
pub enum VarType {
|
||||
Bool,
|
||||
|
@ -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)]
|
||||
pub enum Variable {
|
||||
Bool(VarType, Option<bool>),
|
||||
|
Loading…
x
Reference in New Issue
Block a user