diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ee6179..f05136c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.11.0] - 2023-10-27 + +### Added + +- Arrays +- Array pretty-print function (`format_array`) + +### Changed + +- Literals now return the value provided (if there is no function or operator involved) instead of `None`. + +### Removed + +- Unnecessary `match` in `say` and `short_say` functions. + ## [0.10.0] - 2023-10-26 ### Added @@ -124,7 +139,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Simple function and string parsing -[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.10.0...HEAD +[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.11.0...HEAD + +[0.11.0]: https://github.com/ArnabRollin/dwn/compare/v0.10.0...v0.11.0 [0.10.0]: https://github.com/ArnabRollin/dwn/compare/v0.9.0...v0.10.0 diff --git a/Cargo.lock b/Cargo.lock index 3bcab0d..849a4e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "dwn" -version = "0.10.0" +version = "0.11.0" dependencies = [ "lazy_static", ] diff --git a/Cargo.toml b/Cargo.toml index 872c7d8..017e91f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dwn" -version = "0.10.0" +version = "0.11.0" edition = "2021" [dependencies] diff --git a/src/bytecode.rs b/src/bytecode.rs index 4209e9d..7268326 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -22,6 +22,7 @@ lazy_static! { m.insert("sc", TokenTypes::SCOPE); m.insert("st", TokenTypes::STRING); m.insert("v", TokenTypes::VARIABLE); + m.insert("a", TokenTypes::ARRAY); m }; } diff --git a/src/dwn.rs b/src/dwn.rs index 793b2e9..2a129c5 100644 --- a/src/dwn.rs +++ b/src/dwn.rs @@ -8,7 +8,7 @@ use std::{ }; use crate::{ - lexer::{Token, TokenModifiers, TokenTypes}, + lexer::{tokenize, Token, TokenModifiers, TokenTypes}, runner::run, }; @@ -137,6 +137,10 @@ lazy_static! { "vars", vars as for<'a> fn(Vec, &'a mut Metadata) -> Result, ); + m.insert( + "format_array", + format_array as for<'a> fn(Vec, &'a mut Metadata) -> Result, + ); RwLock::new(m) }; @@ -302,6 +306,24 @@ fn run_scope(token: &Token, meta: &mut Metadata) -> Token { } } +fn read_array(token: &Token, meta: &mut Metadata) -> Vec { + let mut array: Vec = vec![]; + + let array_items: Vec<&str> = token.val.split('\x05').collect(); + + for array_item in array_items { + let tokens = tokenize(array_item.to_string(), meta); + + for token in tokens { + if !token.val.is_empty() { + array.push(token); + } + } + } + + array +} + /// Gets the functions HashMap /// /// Examples: @@ -339,9 +361,7 @@ fn say(tokens: Vec, meta: &mut Metadata) -> Result { let args = get_args(tokens, meta); for arg in args { - match arg.ty { - _ => print!("{} ", arg.val), - }; + print!("{} ", arg.val); } println!(); @@ -357,9 +377,7 @@ fn short_say(tokens: Vec, meta: &mut Metadata) -> Result { let args = get_args(tokens, meta); for arg in args { - match arg.ty { - _ => print!("{} ", arg.val), - }; + print!("{} ", arg.val); } Ok(Token { @@ -1294,3 +1312,24 @@ fn vars(_tokens: Vec, _meta: &mut Metadata) -> Result { val: "None".to_string(), }); } + +fn format_array(tokens: Vec, meta: &mut Metadata) -> Result { + let args = get_args(tokens, meta); + + if args.len() < 1 { + return Err("(format_array) Not enough arguments!".to_string()); + } + + let array = read_array(&args[0], meta); + println!("{:?}", array); + + for token in array { + println!("{}", token.val); + } + + Ok(Token { + ty: TokenTypes::NONE, + modifiers: vec![], + val: "None".to_string(), + }) +} diff --git a/src/lexer.rs b/src/lexer.rs index 75b83cc..88f3623 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -17,6 +17,7 @@ pub enum TokenTypes { SCOPE, NONE, BOOL, + ARRAY, } /// The token modifiers. @@ -72,6 +73,9 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec { let mut literal = String::new(); let mut in_literal = false; + let mut array = String::new(); + let mut in_array = false; + let functions = get_funcs(); let variables = VARIABLES.read().unwrap(); @@ -123,13 +127,20 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec { } for raw_word in data.split(' ') { - let word = if raw_word.starts_with('(') && !in_string { + let word1 = if raw_word.starts_with('(') && !in_string { in_literal = true; &raw_word[1..] } else { raw_word }; + let word = if word1.starts_with('[') && !in_string { + in_array = true; + &word1[1..] + } else { + word1 + }; + if in_literal { if raw_word.ends_with(')') { if in_string { @@ -166,6 +177,48 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec { } } + if in_array { + if word1.ends_with(']') { + if in_string { + if word1.strip_suffix(']').unwrap().ends_with('"') { + in_string = false; + } + } + } + + if !word1.starts_with('[') { + array.push(' '); + } + + if word1.ends_with(']') && !in_string { + in_array = false; + array.push_str(&word[..word.len() - 1]); + + tokens.push(Token { + ty: TokenTypes::ARRAY, + modifiers: if in_func || in_compare { + vec![TokenModifiers::ARGS] + } else { + vec![] + }, + val: array.clone(), + }); + + array.clear(); + + continue; + } else { + if word.ends_with(',') { + array.push_str(&word[..word.len() - 1]); + array.push('\x05'); + } else { + array.push_str(word); + } + + continue; + } + } + if word == "None" && !in_string { if !in_literal { tokens.push(Token { diff --git a/src/main.rs b/src/main.rs index e2ed547..01da657 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,7 @@ fn main() { if arguments.options.contains(&"version".to_string()) || arguments.flags.contains(&"v".to_string()) { - println!("0.10.0"); + println!("0.11.0"); exit(0); } diff --git a/src/runner.rs b/src/runner.rs index f25e1c1..4aaaf2b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -87,11 +87,11 @@ pub fn run_tokens( } } } - _ => { + ty => { return Token { - ty: TokenTypes::NONE, + ty, modifiers: vec![], - val: "None".to_string(), + val: tokens[0].val.to_string(), } } }