-
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 #3 from Pandicon/dev
Version 0.4.0
- Loading branch information
Showing
15 changed files
with
751 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
!>|10|!<^>|10|!<[@^+\.>\,<@]: | ||
^~^!>|10|!<^>|10|!<[@^+$.>.<@] |
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 |
---|---|---|
@@ -1 +1 @@ | ||
!>|10|!<^>|10|!<\.>\,<[@^+\.>\,<@]: | ||
^~^!>|10|!<^>|10|!<$.>.<[@^+$.>.<@] |
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 |
---|---|---|
@@ -1 +1 @@ | ||
|72|!\,|29|!\,|7|!\,\,|3|!\,|67|~\,|12|~\,|87|!\,|8|~\,|3|!\,|6|~\,|8|~\,|67|~\,: | ||
|72|!.|29|!.|7|!..|3|!.|67|~.|12|~.|87|!.|8|~.|3|!.|6|~.|8|~.|67|~. |
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
77 changes: 77 additions & 0 deletions
77
the-golden/src/interpreter/versions/v0-4-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 | ||
} | ||
} |
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,69 @@ | ||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
|
||
lazy_static! { | ||
static ref COMMENT_REGEX: Regex = Regex::new("^\"").unwrap(); | ||
static ref NEW_LINE_REGEX: Regex = Regex::new(r"^\r?\n").unwrap(); | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct Lexer { | ||
text: String, | ||
rules: Vec<Regex>, | ||
line: usize, | ||
column: usize, | ||
comment: bool, | ||
file_path: std::path::PathBuf, | ||
position: usize, | ||
} | ||
|
||
impl Lexer { | ||
pub fn new(text: String, rules: Vec<Regex>, file_path: std::path::PathBuf) -> Self { | ||
Self { | ||
text, | ||
rules, | ||
line: 1, | ||
column: 1, | ||
comment: false, | ||
file_path, | ||
position: 0, | ||
} | ||
} | ||
|
||
pub fn next(&mut self) -> Result<Option<(String, usize, usize, std::path::PathBuf)>, String> { | ||
let text = &self.text.as_str()[self.position..]; | ||
if text.is_empty() { | ||
return Ok(None); | ||
} | ||
if text == "\"" { | ||
self.comment = !self.comment; | ||
} | ||
if self.comment { | ||
return Ok(None); | ||
} | ||
for rule in &self.rules { | ||
if let Some(captures) = rule.captures(text) { | ||
if let Some(capture) = captures.get(0) { | ||
let (command_line, command_column) = (self.line, self.column); | ||
let command = capture.as_str(); | ||
let command_length = capture.end() - capture.start(); | ||
self.position += command_length; | ||
if command.contains('\n') { | ||
self.line += command.matches('\n').count(); | ||
self.column = command.split('\n').last().unwrap().len() + 1; | ||
} else { | ||
self.column += command_length; | ||
} | ||
return Ok(Some((command.to_string(), command_line, command_column, self.file_path.clone()))); | ||
} | ||
} | ||
} | ||
Err(format!( | ||
"Syntax error at {}:{} in {:?} ({:?})", | ||
self.line, | ||
self.column, | ||
self.file_path.file_name().unwrap(), | ||
self.file_path.as_path() | ||
)) | ||
} | ||
} |
Oops, something went wrong.