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

Commit

Permalink
Added scope runner, fixed variable drop
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnabRollin committed Oct 11, 2023
1 parent a4a6de4 commit 82c11db
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 27 deletions.
75 changes: 75 additions & 0 deletions src/dwn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ lazy_static! {
"forever",
forever as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);
m.insert(
"scope",
scope as for<'a> fn(Vec<Token>, &'a mut Metadata) -> Result<Token, String>,
);

RwLock::new(m)
};
Expand Down Expand Up @@ -155,6 +159,14 @@ fn get_args(tokens: Vec<Token>, meta: &mut Metadata) -> Vec<Token> {
val: val.val.to_string(),
}
}
TokenTypes::NAME => {
eprintln!(
"Error on line {}: Name '{}' does not exist!",
meta.line_count + 1,
token.val
);
exit(1);
}
_ => token,
};

Expand All @@ -163,9 +175,23 @@ fn get_args(tokens: Vec<Token>, meta: &mut Metadata) -> Vec<Token> {
args
}

/// Runs a scope in Dawn (dwn)
///
/// Examples:
///
/// ```rust
/// let stat: Option<String> = run_scope(token, meta);
///
/// match stat {
/// Some(stat) => println!("Breaking scope!"),
/// None => println!("It's fine..."),
/// }
/// ```
fn run_scope(token: &Token, meta: &mut Metadata) -> Option<String> {
match token.ty {
TokenTypes::SCOPE => {
*meta.scope += 1;

for line in token.val.lines() {
if line.trim() == "break" {
return Some(String::from("break"));
Expand All @@ -174,6 +200,20 @@ fn run_scope(token: &Token, meta: &mut Metadata) -> Option<String> {
}
}

*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);
}

return None;
}
_ => {
Expand Down Expand Up @@ -487,3 +527,38 @@ fn forever(tokens: Vec<Token>, meta: &mut Metadata) -> Result<Token, String> {
val: "None".to_string(),
})
}

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

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

let scope = args[0].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::STRING,
modifiers: vec![],
val: "None".to_string(),
})
}
69 changes: 42 additions & 27 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
let mut in_literal = false;

let functions = get_funcs();
let mut variables = VARIABLES.write().unwrap();
let variables = VARIABLES.read().unwrap();

if *meta.in_scope {
if data.starts_with('}') {
Expand All @@ -82,7 +82,15 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
Token {
ty: TokenTypes::FUNC,
modifiers: vec![],
val: (*meta.func_token).to_string(),
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 {
ty: TokenTypes::SCOPE,
Expand Down Expand Up @@ -194,15 +202,21 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
let first = match first {
Some(token) => token,
None => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '+' !",
meta.line_count
);
exit(1);
}
};

match first.ty {
TokenTypes::INT | TokenTypes::FLOAT | TokenTypes::VARIABLE => {}
_ => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '+' !",
meta.line_count
);
exit(1);
}
}
Expand Down Expand Up @@ -230,15 +244,21 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
let first = match first {
Some(token) => token,
None => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '-' !",
meta.line_count
);
exit(1);
}
};

match first.ty {
TokenTypes::INT | TokenTypes::FLOAT | TokenTypes::VARIABLE => {}
_ => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '-' !",
meta.line_count
);
exit(1);
}
}
Expand Down Expand Up @@ -266,15 +286,21 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
let first = match first {
Some(token) => token,
None => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '*' !",
meta.line_count
);
exit(1);
}
};

match first.ty {
TokenTypes::INT | TokenTypes::FLOAT | TokenTypes::VARIABLE => {}
_ => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '*' !",
meta.line_count
);
exit(1);
}
}
Expand Down Expand Up @@ -302,15 +328,21 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
let first = match first {
Some(token) => token,
None => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '/' !",
meta.line_count
);
exit(1);
}
};

match first.ty {
TokenTypes::INT | TokenTypes::FLOAT | TokenTypes::VARIABLE => {}
_ => {
eprintln!("Error: No first number for operator!");
eprintln!(
"Error on line {}: No first number for operator '/' !",
meta.line_count
);
exit(1);
}
}
Expand Down Expand Up @@ -384,23 +416,6 @@ pub fn tokenize(data: String, meta: &mut Metadata) -> Vec<Token> {
continue;
}

if word == "}" && !in_string {
*meta.scope -= 1;
let mut drop_vars: Vec<String> = vec![];

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

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

continue;
}

if word.starts_with('"') {
if in_string {
string_token.push(' ');
Expand Down

0 comments on commit 82c11db

Please sign in to comment.