-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Pandicon/dev
Version 0.3.0
- Loading branch information
Showing
13 changed files
with
803 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use regex::Regex; | ||
|
||
use crate::Utils; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Warnings { | ||
pub too_left_pointer: bool, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Preprocessor { | ||
pub disabled_warnings: Warnings, | ||
|
||
pub no_console: bool, | ||
pub sebek: [Option<f64>; 3], | ||
pub version: Option<String>, | ||
} | ||
|
||
impl Preprocessor { | ||
pub fn new() -> Self { | ||
Self { | ||
disabled_warnings: Warnings { too_left_pointer: false }, | ||
|
||
no_console: false, | ||
sebek: [None, None, None], | ||
version: None, | ||
} | ||
} | ||
|
||
pub fn run(&mut self, code: &str) { | ||
let rule = Regex::new(crate::PREPROCESSOR_REGEX).unwrap(); | ||
let statements = rule.find_iter(code).map(|m| m.as_str().trim()).collect::<Vec<&str>>(); | ||
for &statement in &statements { | ||
let mut statement_chars = statement.chars(); | ||
statement_chars.next(); | ||
if statement.ends_with(':') { | ||
statement_chars.next_back(); | ||
} | ||
let args = statement_chars.as_str().split(' ').collect::<Vec<&str>>(); | ||
if args.is_empty() { | ||
continue; | ||
} | ||
let args_count = args.len(); | ||
match args[0].to_lowercase().as_str() { | ||
"version" => { | ||
if args_count < 2 { | ||
continue; | ||
} | ||
self.version = Some(args[1].to_string()); | ||
} | ||
"noconsole" | "no-console" | "no_console" => { | ||
if args_count < 2 { | ||
self.no_console = true; | ||
continue; | ||
} | ||
self.no_console = args[1].to_lowercase() != "false"; | ||
} | ||
"disablewarnings" | "disable-warnings" | "disable_warnings" => { | ||
if args_count < 2 { | ||
continue; | ||
} | ||
match args[1].to_lowercase().as_str() { | ||
"too-left-pointer" | "tooleftpointer" => { | ||
self.disabled_warnings.too_left_pointer = true; | ||
} | ||
_ => {} | ||
} | ||
} | ||
"sebek" => { | ||
if args_count < 2 { | ||
continue; | ||
} | ||
self.sebek = Utils::parse_sebek(args[1]); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
the-golden/src/interpreter/versions/v0-3-0/brackets_matcher.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::collections::HashMap; | ||
|
||
pub struct BracketsMatcher { | ||
pub brackets: HashMap<String, HashMap<usize, usize>>, | ||
brackets_mem: Vec<(String, usize, isize)>, | ||
bracket_keys: HashMap<String, String>, | ||
ending_brackets_keys: HashMap<String, String> | ||
} | ||
|
||
impl BracketsMatcher { | ||
pub fn new() -> Self { | ||
Self { | ||
brackets: HashMap::from([ | ||
("while".to_string(), HashMap::new()), | ||
("do_while".to_string(), HashMap::new()), | ||
("while_local".to_string(), HashMap::new()), | ||
("do_while_local".to_string(), HashMap::new()) | ||
]), | ||
brackets_mem: vec![], | ||
bracket_keys: HashMap::from([ | ||
("[".to_string(), "while".to_string()), | ||
("]".to_string(), "while".to_string()), | ||
("[@".to_string(), "do_while".to_string()), | ||
("@]".to_string(), "do_while".to_string()), | ||
("'[".to_string(), "while_local".to_string()), | ||
("']".to_string(), "while_local".to_string()), | ||
("'[@".to_string(), "do_while_local".to_string()), | ||
("'@]".to_string(), "do_while_local".to_string()), | ||
]), | ||
ending_brackets_keys: HashMap::from([ | ||
("while".to_string(), "]".to_string()), | ||
("do_while".to_string(), "@]".to_string()), | ||
("while_local".to_string(), "']".to_string()), | ||
("do_while_local".to_string(), "'@]".to_string()), | ||
]) | ||
} | ||
} | ||
|
||
pub fn match_brackets(&mut self, code: &[String]) { | ||
let starting_brackets = ["[", "[@", "'[", "'[@", ]; | ||
let ending_brackets = ["]", "@]", "']", "'@]"]; | ||
for (i, command) in code.iter().enumerate() { | ||
let command_str = command.as_str(); | ||
if !starting_brackets.contains(&command_str) && !ending_brackets.contains(&command_str) { | ||
continue; | ||
} | ||
if starting_brackets.contains(&command_str) { | ||
self.brackets_mem.push((command.clone(), i, 0)); | ||
} | ||
let mut keys_to_remove = vec![]; | ||
for key in 0..self.brackets_mem.len() { | ||
self.brackets_mem[key].2 += self.num_equals(&self.brackets_mem[key].0, command); | ||
let wanted_end = self.ending_brackets_keys.get(self.bracket_keys.get(&self.brackets_mem[key].0).unwrap()).unwrap(); | ||
if self.brackets_mem[key].2 == 0 && command == wanted_end { | ||
let category = self.bracket_keys.get(wanted_end).unwrap(); | ||
let sub_map = self.brackets.get_mut(category).unwrap(); | ||
sub_map.insert(self.brackets_mem[key].1, i); | ||
sub_map.insert(i, self.brackets_mem[key].1); | ||
keys_to_remove.push(key); | ||
} | ||
} | ||
for key in keys_to_remove { | ||
self.brackets_mem.remove(key); | ||
} | ||
} | ||
} | ||
|
||
fn num_equals(&self, left: &String, right: &String) -> isize { | ||
if self.bracket_keys.get(left) != self.bracket_keys.get(right) { | ||
return 0; | ||
} | ||
if left == right { | ||
return 1; | ||
} | ||
-1 | ||
} | ||
} |
Oops, something went wrong.