Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Added arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabRollin committed Oct 27, 2023
1 parent e75ec13 commit 898df32
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 15 deletions.
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dwn"
version = "0.10.0"
version = "0.11.0"
edition = "2021"

[dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
Expand Down
53 changes: 46 additions & 7 deletions src/dwn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

use crate::{
lexer::{Token, TokenModifiers, TokenTypes},
lexer::{tokenize, Token, TokenModifiers, TokenTypes},
runner::run,
};

Expand Down Expand Up @@ -137,6 +137,10 @@ lazy_static! {
"vars",
vars as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"format_array",
format_array as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);

RwLock::new(m)
};
Expand Down Expand Up @@ -302,6 +306,24 @@ fn run_scope(token: &Token, meta: &mut Metadata) -> Token {
}
}

fn read_array(token: &Token, meta: &mut Metadata) -> Vec<Token> {
let mut array: Vec<Token> = 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:
Expand Down Expand Up @@ -339,9 +361,7 @@ fn say(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

for arg in args {
match arg.ty {
_ => print!("{} ", arg.val),
};
print!("{} ", arg.val);
}

println!();
Expand All @@ -357,9 +377,7 @@ fn short_say(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

for arg in args {
match arg.ty {
_ => print!("{} ", arg.val),
};
print!("{} ", arg.val);
}

Ok(Token {
Expand Down Expand Up @@ -1294,3 +1312,24 @@ fn vars(_tokens: Vec<Token>, _meta: &mut Metadata) -> Result<Token, String> {
val: "None".to_string(),
});
}

fn format_array(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
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(),
})
}
55 changes: 54 additions & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub enum TokenTypes {
SCOPE,
NONE,
BOOL,
ARRAY,
}

/// The token modifiers.
Expand Down Expand Up @@ -72,6 +73,9 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
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();

Expand Down Expand Up @@ -123,13 +127,20 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
}

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 {
Expand Down Expand Up @@ -166,6 +177,48 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
}
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down

0 comments on commit 898df32

Please sign in to comment.