From 3e592392a8415ff7ee5daa85c98b427061db3cb0 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 16 Dec 2023 20:47:42 -0500 Subject: [PATCH] Change syntax from val/var to let (mut) --- src/lexic/scanner/identifier.rs | 16 +++++------ src/lexic/token.rs | 4 +-- src/syntax/binding.rs | 50 ++++++++++++++++----------------- src/syntax/statement.rs | 2 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/lexic/scanner/identifier.rs b/src/lexic/scanner/identifier.rs index 99b7114..ae9420a 100755 --- a/src/lexic/scanner/identifier.rs +++ b/src/lexic/scanner/identifier.rs @@ -4,8 +4,8 @@ use crate::lexic::{token::Token, utils, LexResult}; /// Checks if a String is a keyword, and returns its TokenType fn str_is_keyword(s: &String) -> Option { match s.as_str() { - "var" => Some(TokenType::VAR), - "val" => Some(TokenType::VAL), + "let" => Some(TokenType::LET), + "mut" => Some(TokenType::MUT), "fun" => Some(TokenType::FUN), _ => None, } @@ -141,23 +141,23 @@ mod tests { // Should scan keywords #[test] fn test_4() { - let input = str_to_vec("var"); + let input = str_to_vec("mut"); let start_pos = 0; if let LexResult::Some(token, next) = scan(*input.get(0).unwrap(), &input, start_pos) { assert_eq!(3, next); - assert_eq!(TokenType::VAR, token.token_type); - assert_eq!("var", token.value); + assert_eq!(TokenType::MUT, token.token_type); + assert_eq!("mut", token.value); assert_eq!(0, token.position); } else { panic!() } - let input = str_to_vec("val"); + let input = str_to_vec("let"); let start_pos = 0; if let LexResult::Some(token, next) = scan(*input.get(0).unwrap(), &input, start_pos) { assert_eq!(3, next); - assert_eq!(TokenType::VAL, token.token_type); - assert_eq!("val", token.value); + assert_eq!(TokenType::LET, token.token_type); + assert_eq!("let", token.value); assert_eq!(0, token.position); } else { panic!() diff --git a/src/lexic/token.rs b/src/lexic/token.rs index 6243f73..1d479a3 100755 --- a/src/lexic/token.rs +++ b/src/lexic/token.rs @@ -15,8 +15,8 @@ pub enum TokenType { Comment, INDENT, DEDENT, - VAR, - VAL, + LET, + MUT, EOF, FUN, } diff --git a/src/syntax/binding.rs b/src/syntax/binding.rs index 11a2d29..f9367ed 100644 --- a/src/syntax/binding.rs +++ b/src/syntax/binding.rs @@ -8,22 +8,22 @@ use crate::utils::Result3; pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult { let mut current_pos = pos; + // TODO: Detect if the binding starts with a datatype + /* - * val/var keyword + * let keyword */ - let (is_val, binding_token, next_pos) = { - let res1 = parse_token_type(tokens, current_pos, TokenType::VAL); - match res1 { - ParseResult::Ok(val_token, next) => (true, val_token, next), - _ => { - let res2 = parse_token_type(tokens, current_pos, TokenType::VAR); - match res2 { - ParseResult::Ok(var_token, next) => (false, var_token, next), - // Neither VAL nor VAR were matched, the caller should try - // other constructs - _ => return ParseResult::Unmatched, + let (is_mutable, binding_token, next_pos) = { + let let_token = parse_token_type(tokens, current_pos, TokenType::LET); + match let_token { + ParseResult::Ok(let_token, next_let) => { + let mut_token = parse_token_type(tokens, next_let, TokenType::MUT); + match mut_token { + ParseResult::Ok(_mut_token, next_mut) => (true, let_token, next_mut), + _ => (false, let_token, next_let), } } + _ => return ParseResult::Unmatched, } }; current_pos = next_pos; @@ -50,7 +50,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult(tokens: &'a Vec, pos: usize) -> ParseResult panic!("Error expected"), } - let tokens = get_tokens(&String::from("val \"hello\"")).unwrap(); + let tokens = get_tokens(&String::from("let \"hello\"")).unwrap(); let binding = try_parse(&tokens, 0); match binding { @@ -218,7 +218,7 @@ mod tests { #[test] fn should_return_error_when_equal_op_is_wrong() { - let tokens = get_tokens(&String::from("val id \"error\"")).unwrap(); + let tokens = get_tokens(&String::from("let id \"error\"")).unwrap(); let binding = try_parse(&tokens, 0); match binding { diff --git a/src/syntax/statement.rs b/src/syntax/statement.rs index c32a568..a7dfd8c 100644 --- a/src/syntax/statement.rs +++ b/src/syntax/statement.rs @@ -39,7 +39,7 @@ mod tests { #[test] fn should_parse_binding() { - let input = String::from("val identifier = 20"); + let input = String::from("let identifier = 20"); let tokens = crate::lexic::get_tokens(&input).unwrap(); let statement = try_parse(&tokens, 0);