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

Commit

Permalink
Added boolean type and if, fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabRollin committed Oct 14, 2023
1 parent 1c2437e commit fd29ece
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 54 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.0] - 2023-10-14

### Added

- Boolean type (`true` and `false`)
- `if` function

### Fixed

- Fixed bug where two arguments cannot be provided to scope-accepting function.

## [0.5.0] - 2023-10-13

### Changed
Expand Down Expand Up @@ -60,7 +71,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.5.0...HEAD
[unreleased]: https://github.com/ArnabRollin/dwn/compare/v0.6.0...HEAD

[0.6.0]: https://github.com/ArnabRollin/dwn/compare/v0.5.0...v0.6.0

[0.5.0]: https://github.com/ArnabRollin/dwn/compare/v0.4.0...v0.5.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.4.0"
version = "0.6.0"
edition = "2021"

[dependencies]
Expand Down
57 changes: 55 additions & 2 deletions src/dwn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ pub struct Metadata<'a> {
pub scope: &'a mut u32,
pub in_scope: &'a mut bool,
pub scope_token: &'a mut String,
pub in_func: &'a mut bool,
pub func_token: &'a mut String,
pub current_tokens: &'a mut Vec<Token>,
}

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

RwLock::new(m)
};
Expand Down Expand Up @@ -567,3 +570,53 @@ fn scope(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
val: "None".to_string(),
})
}

fn if_(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
let args = get_args(tokens, meta);

if args.len() < 2 {
return Err("Not enough arguments!".to_string());
}

let condition = args[0].clone();

let result = match condition.ty {
TokenTypes::BOOL => condition.val,
ty => return Err(format!("Type {ty:?} cannot be used as condition!")),
};

if result == "false" {
return Ok(Token {
ty: TokenTypes::NONE,
modifiers: vec![],
val: "None".to_string(),
});
}

let scope = args[1].clone();
*meta.scope += 1;

for line in scope.val.lines() {
run(line.to_string(), get_funcs(), meta);
}

*meta.scope -= 1;
let mut drop_vars: Vec<String> = vec![];
let mut variables = VARIABLES.write().unwrap();

for (k, v) in variables.clone() {
if v.scope == *meta.scope + 1 {
drop_vars.push(k);
}
}

for k in drop_vars {
variables.remove(&k);
}

Ok(Token {
ty: TokenTypes::NONE,
modifiers: vec![],
val: "None".to_string(),
})
}
6 changes: 2 additions & 4 deletions src/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ pub fn idle() {
let mut scope = 0;
let mut in_scope = false;
let mut scope_token = String::new();
let mut in_func = false;
let mut func_token = String::new();
let mut current_tokens = vec![];

loop {
let mut code = String::new();
Expand Down Expand Up @@ -53,8 +52,7 @@ pub fn idle() {
scope: &mut scope,
in_scope: &mut in_scope,
scope_token: &mut scope_token,
in_func: &mut in_func,
func_token: &mut func_token,
current_tokens: &mut current_tokens,
},
);

Expand Down
6 changes: 2 additions & 4 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ pub fn interpret_file(file: Option<&String>) {
let mut scope = 0;
let mut in_scope = false;
let mut scope_token = String::new();
let mut in_func = false;
let mut func_token = String::new();
let mut current_tokens = vec![];

for (count, line) in reader.lines().enumerate() {
let line = remove_all_after(line.unwrap(), ';');
Expand All @@ -37,8 +36,7 @@ pub fn interpret_file(file: Option<&String>) {
scope: &mut scope,
in_scope: &mut in_scope,
scope_token: &mut scope_token,
in_func: &mut in_func,
func_token: &mut func_token,
current_tokens: &mut current_tokens,
},
);
}
Expand Down
100 changes: 62 additions & 38 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum TokenTypes {
NAME,
SCOPE,
NONE,
BOOL,
}

/// The token modifiers.
Expand Down Expand Up @@ -79,30 +80,27 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
let scope_token = meta.scope_token.to_string();
meta.scope_token.clear();

return vec![
Token {
ty: TokenTypes::FUNC,
modifiers: vec![],
val: if *meta.in_func {
(*meta.func_token).to_string()
} else {
eprintln!(
"Error on line {}: No function found to run scope!",
meta.line_count
);
exit(1);
},
},
Token {
let mut tokens: Vec<Token> = vec![];
for t in meta.current_tokens.clone() {
tokens.push(t);
}

match meta.current_tokens[0].ty {
TokenTypes::FUNC => tokens.push(Token {
ty: TokenTypes::SCOPE,
modifiers: if *meta.in_func {
vec![TokenModifiers::ARGS]
} else {
vec![]
},
val: (scope_token).to_string(),
},
];
modifiers: vec![TokenModifiers::ARGS],
val: scope_token,
}),
_ => {
eprintln!(
"Error on line {}: No function found to run scope!",
meta.line_count
);
exit(1);
}
}

return tokens;
} else {
meta.scope_token.push_str(&data);
meta.scope_token.push('\n');
Expand Down Expand Up @@ -155,15 +153,45 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
}

if word == "None" && !in_string {
tokens.push(Token {
ty: TokenTypes::NONE,
modifiers: if in_func {
vec![TokenModifiers::ARGS]
} else {
vec![]
},
val: "None".to_string(),
});
if !in_literal {
tokens.push(Token {
ty: TokenTypes::NONE,
modifiers: if in_func {
vec![TokenModifiers::ARGS]
} else {
vec![]
},
val: "None".to_string(),
});
}
continue;
}
if word == "true" && !in_string {
if !in_literal {
tokens.push(Token {
ty: TokenTypes::BOOL,
modifiers: if in_func {
vec![TokenModifiers::ARGS]
} else {
vec![]
},
val: "true".to_string(),
});
}
continue;
}
if word == "false" && !in_string {
if !in_literal {
tokens.push(Token {
ty: TokenTypes::BOOL,
modifiers: if in_func {
vec![TokenModifiers::ARGS]
} else {
vec![]
},
val: "false".to_string(),
});
}
continue;
}

Expand Down Expand Up @@ -413,10 +441,7 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
*meta.scope += 1;
*meta.in_scope = true;

if in_func {
*meta.in_func = true;
*meta.func_token = tokens.last().unwrap().val.to_string();
}
*meta.current_tokens = tokens.clone();

continue;
}
Expand Down Expand Up @@ -545,8 +570,7 @@ fn tokenizer() {
scope: &mut 0,
in_scope: &mut false,
scope_token: &mut String::new(),
in_func: &mut false,
func_token: &mut String::new(),
current_tokens: &mut vec![],
},
);

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
if arguments.options.contains(&"version".to_string())
|| arguments.flags.contains(&"v".to_string())
{
println!("0.4.0");
println!("0.6.0");
exit(0);
}

Expand Down
3 changes: 1 addition & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ fn line_runner() {
scope: &mut 0,
in_scope: &mut false,
scope_token: &mut String::new(),
in_func: &mut false,
func_token: &mut String::new(),
current_tokens: &mut vec![],
},
);

Expand Down

0 comments on commit fd29ece

Please sign in to comment.