mirror of
https://github.com/MeexReay/sustlang.git
synced 2025-06-24 18:43:01 +03:00
fix some and add some
This commit is contained in:
parent
6a4c92b671
commit
3c2cc0dd22
77
src/lib.rs
77
src/lib.rs
@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
char::EscapeDebug,
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
hash::Hash,
|
hash::Hash,
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
@ -688,7 +689,7 @@ fn prepare_script(text: String) -> Vec<String> {
|
|||||||
Some(s) => s.0,
|
Some(s) => s.0,
|
||||||
None => s,
|
None => s,
|
||||||
})
|
})
|
||||||
.filter(|s| !s.trim().is_empty())
|
.filter(|s| !s.trim_matches(' ').is_empty())
|
||||||
.map(|s| s.trim_end_matches(" ").to_string())
|
.map(|s| s.trim_end_matches(" ").to_string())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
@ -721,32 +722,31 @@ fn parse_commands(lines: Vec<String>) -> Vec<Command> {
|
|||||||
fn cut_funcs(commands: &mut Vec<Command>) -> Vec<Function> {
|
fn cut_funcs(commands: &mut Vec<Command>) -> Vec<Function> {
|
||||||
let mut functions: Vec<Function> = Vec::new();
|
let mut functions: Vec<Function> = Vec::new();
|
||||||
|
|
||||||
let mut now_func = None;
|
let mut now_func: Option<Function> = None;
|
||||||
|
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
for command in commands.clone() {
|
for command in commands.clone() {
|
||||||
match now_func.clone() {
|
index += 1;
|
||||||
Some(mut func) => {
|
|
||||||
if command.command_type == CommandType::FuncEnd {
|
|
||||||
commands.remove(index);
|
|
||||||
index -= 1;
|
|
||||||
|
|
||||||
functions.push(func);
|
match now_func.clone() {
|
||||||
|
Some(func) => {
|
||||||
|
index -= 1;
|
||||||
|
commands.remove(index);
|
||||||
|
|
||||||
|
if let CommandType::FuncEnd = command.command_type {
|
||||||
|
functions.push(func.clone());
|
||||||
now_func = None;
|
now_func = None;
|
||||||
} else {
|
} else {
|
||||||
commands.remove(index);
|
now_func.as_mut().unwrap().commands.push(command);
|
||||||
index -= 1;
|
|
||||||
|
|
||||||
func.commands.push(command);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
if command.command_type == CommandType::Func {
|
if let CommandType::Func = command.command_type {
|
||||||
commands.remove(index);
|
|
||||||
index -= 1;
|
index -= 1;
|
||||||
|
commands.remove(index);
|
||||||
|
|
||||||
let name = command.args[0].clone();
|
let name = command.args[1].clone();
|
||||||
let result_type = match VarType::from_name(&command.args[1]) {
|
let result_type = match VarType::from_name(&command.args[0]) {
|
||||||
Some(i) => i,
|
Some(i) => i,
|
||||||
None => {
|
None => {
|
||||||
continue;
|
continue;
|
||||||
@ -775,12 +775,10 @@ fn cut_funcs(commands: &mut Vec<Command>) -> Vec<Function> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
now_func = Some(Function::new(name, result_type, parameters, Vec::new()))
|
now_func = Some(Function::new(name, result_type, parameters, Vec::new()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
index += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
functions
|
functions
|
||||||
@ -903,6 +901,15 @@ impl RunningScript {
|
|||||||
var
|
var
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_function(&self, name: String) -> Option<Function> {
|
||||||
|
for func in &self.functions {
|
||||||
|
if func.name == name {
|
||||||
|
return Some(func.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
pub fn exec_commands(
|
pub fn exec_commands(
|
||||||
&mut self,
|
&mut self,
|
||||||
commands: Vec<Command>,
|
commands: Vec<Command>,
|
||||||
@ -1002,9 +1009,6 @@ impl RunningScript {
|
|||||||
locals.insert(name_var, Variable::Bool(VarType::Bool, Some(result)));
|
locals.insert(name_var, Variable::Bool(VarType::Bool, Some(result)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CommandType::ToString => {}
|
|
||||||
CommandType::AddInt => {}
|
|
||||||
CommandType::AddFloat => {}
|
|
||||||
CommandType::AddStr => {
|
CommandType::AddStr => {
|
||||||
let var_name = command.args[0].clone();
|
let var_name = command.args[0].clone();
|
||||||
let other_var = command.args[1].clone();
|
let other_var = command.args[1].clone();
|
||||||
@ -1075,9 +1079,6 @@ impl RunningScript {
|
|||||||
locals.insert(var_name, var);
|
locals.insert(var_name, var);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CommandType::SubStr => {}
|
|
||||||
CommandType::SubList => {}
|
|
||||||
CommandType::ListSize => {}
|
|
||||||
CommandType::Write => {
|
CommandType::Write => {
|
||||||
let name_var = command.args[0].clone();
|
let name_var = command.args[0].clone();
|
||||||
let stream_var = command.args[1].clone();
|
let stream_var = command.args[1].clone();
|
||||||
@ -1131,6 +1132,28 @@ impl RunningScript {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
CommandType::UseFunc => {
|
||||||
|
let func_name = command.args[0].clone();
|
||||||
|
let result_name = command.args[1].clone();
|
||||||
|
let args_names = command.args[2..].to_vec();
|
||||||
|
|
||||||
|
let func = self.get_function(func_name).unwrap();
|
||||||
|
let args: Vec<Variable> = args_names
|
||||||
|
.iter()
|
||||||
|
.map(|f| self.get_var(f.to_string(), locals).unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
self.exec_function(func, result_name, args)
|
||||||
|
}
|
||||||
|
CommandType::Return => {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CommandType::ToString => {}
|
||||||
|
CommandType::AddInt => {}
|
||||||
|
CommandType::AddFloat => {}
|
||||||
|
CommandType::SubStr => {}
|
||||||
|
CommandType::SubList => {}
|
||||||
|
CommandType::ListSize => {}
|
||||||
CommandType::Read => {}
|
CommandType::Read => {}
|
||||||
CommandType::ReadAll => {}
|
CommandType::ReadAll => {}
|
||||||
CommandType::ReadStr => {}
|
CommandType::ReadStr => {}
|
||||||
@ -1145,10 +1168,6 @@ impl RunningScript {
|
|||||||
CommandType::OpenTcpListener => {}
|
CommandType::OpenTcpListener => {}
|
||||||
CommandType::Sleep => {}
|
CommandType::Sleep => {}
|
||||||
CommandType::NewThread => {}
|
CommandType::NewThread => {}
|
||||||
CommandType::UseFunc => {}
|
|
||||||
CommandType::Return => {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
CommandType::Equals => {}
|
CommandType::Equals => {}
|
||||||
CommandType::More => {}
|
CommandType::More => {}
|
||||||
CommandType::Less => {}
|
CommandType::Less => {}
|
||||||
|
19
test.sus
19
test.sus
@ -1,6 +1,17 @@
|
|||||||
|
FUNC bool println text string # println command
|
||||||
|
|
||||||
|
TEMP_VAR char br 10 # line break var
|
||||||
|
ADD_STR text br # add line break to text var
|
||||||
|
WRITE text cout # write text var to console
|
||||||
|
|
||||||
|
FUNC_END # end println command
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
INIT_VAR string text # init text var
|
INIT_VAR string text # init text var
|
||||||
SET_VAR text Hello World! # set text to var
|
SET_VAR text Hello World! # set hello world to var
|
||||||
TEMP_VAR char br 10 # create temp var with line break
|
|
||||||
ADD_STR text br # add line break to text
|
TEMP_VAR bool result 0 # init temp result var
|
||||||
WRITE text cout # write text to console
|
USE_FUNC println result text # use println to print text
|
||||||
|
|
||||||
DROP_VAR text # drop text var
|
DROP_VAR text # drop text var
|
||||||
|
Loading…
x
Reference in New Issue
Block a user