Skip to content

Commit

Permalink
Add bool
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeRoggenbuck committed Sep 5, 2024
1 parent c506668 commit cd12619
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum TokenType {
// NOT: .3, 54
NumericDecLiteral = 2,

BoolLiteral,

LeftBrace,
RightBrace,
LeftBracket,
Expand Down Expand Up @@ -108,6 +110,7 @@ pub enum TokenType {
TypeImaginaryKeyword,
TypeSizeKeyword,
TypeSqrtKeyword,
TypeBoolKeyword,

Function,

Expand All @@ -129,7 +132,10 @@ fn is_type(maybe_type: &str) -> TokenType {
"imaginary" => TokenType::TypeImaginaryKeyword,
"size" => TokenType::TypeSizeKeyword,
"sqrt" => TokenType::TypeSqrtKeyword,
"bool" => TokenType::TypeBoolKeyword,
"fn" => TokenType::Function,
"true" => TokenType::BoolLiteral,
"false" => TokenType::BoolLiteral,

// Other types
// "literal" | "type" | "option" | "string" => true,
Expand Down
33 changes: 33 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ impl Parser for ParserState {
self.stack.push(token);
}

TokenType::BoolLiteral => match token.value.as_str() {
"true" => self.stack.push(Token {
token_type: TokenType::BoolLiteral,
value: "1".to_string(),
}),
"false" => self.stack.push(Token {
token_type: TokenType::BoolLiteral,
value: "0".to_string(),
}),
_ => {}
},

TokenType::Identifier => {
let var = self.local_memory.get(&token.value);
let func = self.function_memory.get(&token.value);
Expand Down Expand Up @@ -201,6 +213,27 @@ impl Parser for ParserState {
}
}

TokenType::TypeBoolKeyword => {
let first = self.variable_check_pop();

match first {
Some(a) => match a.token_type {
TokenType::NumericIntLiteral | TokenType::NumericDecLiteral => {
let a_float_res = a.value.parse::<f64>();
match a_float_res {
Ok(a_val) => self.stack.push(Token {
token_type: TokenType::BoolLiteral,
value: (a_val != 0.0).to_string(),
}),
Err(_) => wrong_type_error_first(a.value, token.value),
}
}
_ => invalid_type_cast_error(String::from("BoolLiteral"), a, token),
},
None => stack_empty_error(),
}
}

// Simple two argument operations/functions
TokenType::Addition
| TokenType::Multiplication
Expand Down

0 comments on commit cd12619

Please sign in to comment.