From 4378c351e91e444e82218e8fdd67aa5ae2bbb968 Mon Sep 17 00:00:00 2001 From: Doonv <58695417+doonv@users.noreply.github.com> Date: Wed, 13 Dec 2023 22:03:28 +0200 Subject: [PATCH] Add boolean type. --- Cargo.toml | 2 +- src/builtin_parser/lexer.rs | 5 +++++ src/builtin_parser/parser.rs | 3 +++ src/builtin_parser/runner.rs | 4 +++- src/builtin_parser/runner/value.rs | 3 +++ 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 13fb80d..141ced5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,4 +38,4 @@ opt-level = 3 [lints] clippy.useless_format = "allow" -rust.missing_docs = "warn" \ No newline at end of file +rust.missing_docs = "warn" diff --git a/src/builtin_parser/lexer.rs b/src/builtin_parser/lexer.rs index 089fe7a..a824ce0 100644 --- a/src/builtin_parser/lexer.rs +++ b/src/builtin_parser/lexer.rs @@ -54,6 +54,11 @@ pub enum Token { #[token(",")] Comma, + #[token("true")] + True, + #[token("false")] + False, + #[regex(r#""(\\[\\"]|[^"])*""#)] String, diff --git a/src/builtin_parser/parser.rs b/src/builtin_parser/parser.rs index 9c44dbe..0228103 100644 --- a/src/builtin_parser/parser.rs +++ b/src/builtin_parser/parser.rs @@ -64,6 +64,7 @@ pub enum Expression { arguments: Vec>, }, Object(HashMap>), + Boolean(bool), } #[derive(Debug, Clone)] @@ -293,6 +294,8 @@ fn parse_primary( Some(Ok(Token::Number)) => { Ok(tokens.wrap_span(Expression::Number(tokens.slice().parse().unwrap()))) } + Some(Ok(Token::True)) => Ok(tokens.wrap_span(Expression::Boolean(true))), + Some(Ok(Token::False)) => Ok(tokens.wrap_span(Expression::Boolean(false))), Some(Ok(token)) => Err(ParseError::UnexpectedToken(tokens.wrap_span(token))), Some(Err(FailedToLexCharacter)) => Err(ParseError::FailedToLexCharacter(tokens.span())), None => unreachable!("oh fuck what have i done to cause this to happen"), diff --git a/src/builtin_parser/runner.rs b/src/builtin_parser/runner.rs index c37e20d..5128927 100644 --- a/src/builtin_parser/runner.rs +++ b/src/builtin_parser/runner.rs @@ -294,6 +294,7 @@ fn eval_expression( } } Expression::None => Ok(Value::None), + Expression::Boolean(bool) => Ok(Value::Boolean(bool)), Expression::Function { name, arguments } => { environment.function_scope(&name, move |environment, function| { (function.body)( @@ -463,7 +464,8 @@ fn set_resource( let mut dyn_struct = DynamicStruct::default(); for (key, value) in map.into_iter() { match Rc::try_unwrap(value).unwrap().into_inner() { - Value::None => {} + Value::None => dyn_struct.insert(&key, ()), + Value::Boolean(boolean) => dyn_struct.insert(&key, boolean), Value::Number(number) => dyn_struct.insert(&key, number), Value::String(string) => dyn_struct.insert(&key, string.clone()), Value::Reference(..) => todo!("todo reference"), diff --git a/src/builtin_parser/runner/value.rs b/src/builtin_parser/runner/value.rs index 8b91e62..3baef6b 100644 --- a/src/builtin_parser/runner/value.rs +++ b/src/builtin_parser/runner/value.rs @@ -17,6 +17,8 @@ pub enum Value { None, /// A number, for simplicity only f64s are used. (However this will probably change in the future) Number(f64), + /// `true` or `false`. Thats it... + Boolean(bool), /// A string... there isn't much to say about this one. String(String), /// A reference. @@ -51,6 +53,7 @@ impl Value { match self { Value::None => Ok(format!("()")), Value::Number(number) => Ok(format!("{number}")), + Value::Boolean(bool) => Ok(format!("{bool}")), Value::String(string) => Ok(format!("\"{string}\"")), Value::Reference(reference) => { if let Some(rc) = reference.upgrade() {