Change syntax from val/var to let (mut)
This commit is contained in:
parent
7379c469d3
commit
3e592392a8
@ -4,8 +4,8 @@ use crate::lexic::{token::Token, utils, LexResult};
|
|||||||
/// Checks if a String is a keyword, and returns its TokenType
|
/// Checks if a String is a keyword, and returns its TokenType
|
||||||
fn str_is_keyword(s: &String) -> Option<TokenType> {
|
fn str_is_keyword(s: &String) -> Option<TokenType> {
|
||||||
match s.as_str() {
|
match s.as_str() {
|
||||||
"var" => Some(TokenType::VAR),
|
"let" => Some(TokenType::LET),
|
||||||
"val" => Some(TokenType::VAL),
|
"mut" => Some(TokenType::MUT),
|
||||||
"fun" => Some(TokenType::FUN),
|
"fun" => Some(TokenType::FUN),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
@ -141,23 +141,23 @@ mod tests {
|
|||||||
// Should scan keywords
|
// Should scan keywords
|
||||||
#[test]
|
#[test]
|
||||||
fn test_4() {
|
fn test_4() {
|
||||||
let input = str_to_vec("var");
|
let input = str_to_vec("mut");
|
||||||
let start_pos = 0;
|
let start_pos = 0;
|
||||||
if let LexResult::Some(token, next) = scan(*input.get(0).unwrap(), &input, start_pos) {
|
if let LexResult::Some(token, next) = scan(*input.get(0).unwrap(), &input, start_pos) {
|
||||||
assert_eq!(3, next);
|
assert_eq!(3, next);
|
||||||
assert_eq!(TokenType::VAR, token.token_type);
|
assert_eq!(TokenType::MUT, token.token_type);
|
||||||
assert_eq!("var", token.value);
|
assert_eq!("mut", token.value);
|
||||||
assert_eq!(0, token.position);
|
assert_eq!(0, token.position);
|
||||||
} else {
|
} else {
|
||||||
panic!()
|
panic!()
|
||||||
}
|
}
|
||||||
|
|
||||||
let input = str_to_vec("val");
|
let input = str_to_vec("let");
|
||||||
let start_pos = 0;
|
let start_pos = 0;
|
||||||
if let LexResult::Some(token, next) = scan(*input.get(0).unwrap(), &input, start_pos) {
|
if let LexResult::Some(token, next) = scan(*input.get(0).unwrap(), &input, start_pos) {
|
||||||
assert_eq!(3, next);
|
assert_eq!(3, next);
|
||||||
assert_eq!(TokenType::VAL, token.token_type);
|
assert_eq!(TokenType::LET, token.token_type);
|
||||||
assert_eq!("val", token.value);
|
assert_eq!("let", token.value);
|
||||||
assert_eq!(0, token.position);
|
assert_eq!(0, token.position);
|
||||||
} else {
|
} else {
|
||||||
panic!()
|
panic!()
|
||||||
|
@ -15,8 +15,8 @@ pub enum TokenType {
|
|||||||
Comment,
|
Comment,
|
||||||
INDENT,
|
INDENT,
|
||||||
DEDENT,
|
DEDENT,
|
||||||
VAR,
|
LET,
|
||||||
VAL,
|
MUT,
|
||||||
EOF,
|
EOF,
|
||||||
FUN,
|
FUN,
|
||||||
}
|
}
|
||||||
|
@ -8,22 +8,22 @@ use crate::utils::Result3;
|
|||||||
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Binding, ()> {
|
pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Binding, ()> {
|
||||||
let mut current_pos = pos;
|
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 (is_mutable, binding_token, next_pos) = {
|
||||||
let res1 = parse_token_type(tokens, current_pos, TokenType::VAL);
|
let let_token = parse_token_type(tokens, current_pos, TokenType::LET);
|
||||||
match res1 {
|
match let_token {
|
||||||
ParseResult::Ok(val_token, next) => (true, val_token, next),
|
ParseResult::Ok(let_token, next_let) => {
|
||||||
_ => {
|
let mut_token = parse_token_type(tokens, next_let, TokenType::MUT);
|
||||||
let res2 = parse_token_type(tokens, current_pos, TokenType::VAR);
|
match mut_token {
|
||||||
match res2 {
|
ParseResult::Ok(_mut_token, next_mut) => (true, let_token, next_mut),
|
||||||
ParseResult::Ok(var_token, next) => (false, var_token, next),
|
_ => (false, let_token, next_let),
|
||||||
// Neither VAL nor VAR were matched, the caller should try
|
|
||||||
// other constructs
|
|
||||||
_ => return ParseResult::Unmatched,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => return ParseResult::Unmatched,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
current_pos = next_pos;
|
current_pos = next_pos;
|
||||||
@ -50,7 +50,7 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Binding,
|
|||||||
return ParseResult::Err(SyntaxError {
|
return ParseResult::Err(SyntaxError {
|
||||||
reason: format!(
|
reason: format!(
|
||||||
"There should be an identifier after a `{}` token",
|
"There should be an identifier after a `{}` token",
|
||||||
if is_val { "val" } else { "var" }
|
if is_mutable { "val" } else { "var" }
|
||||||
),
|
),
|
||||||
error_start: binding_token.position,
|
error_start: binding_token.position,
|
||||||
error_end: binding_token.get_end_position(),
|
error_end: binding_token.get_end_position(),
|
||||||
@ -95,7 +95,7 @@ pub fn try_parse<'a>(tokens: &'a Vec<Token>, pos: usize) -> ParseResult<Binding,
|
|||||||
};
|
};
|
||||||
current_pos = next_pos;
|
current_pos = next_pos;
|
||||||
|
|
||||||
let binding = if is_val {
|
let binding = if !is_mutable {
|
||||||
Binding::Val(ValBinding {
|
Binding::Val(ValBinding {
|
||||||
datatype: None,
|
datatype: None,
|
||||||
identifier: Box::new(identifier.value.clone()),
|
identifier: Box::new(identifier.value.clone()),
|
||||||
@ -119,7 +119,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_parse_val_binding() {
|
fn should_parse_val_binding() {
|
||||||
let tokens = get_tokens(&String::from("val identifier = 20")).unwrap();
|
let tokens = get_tokens(&String::from("let identifier = 20")).unwrap();
|
||||||
let ParseResult::Ok(Binding::Val(binding), _) = try_parse(&tokens, 0) else {
|
let ParseResult::Ok(Binding::Val(binding), _) = try_parse(&tokens, 0) else {
|
||||||
panic!()
|
panic!()
|
||||||
};
|
};
|
||||||
@ -129,11 +129,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_parse_val() {
|
fn should_parse_val() {
|
||||||
let tokens = get_tokens(&String::from("val")).unwrap();
|
let tokens = get_tokens(&String::from("let")).unwrap();
|
||||||
let token = *try_token_type(&tokens, 0, TokenType::VAL).unwrap();
|
let token = *try_token_type(&tokens, 0, TokenType::LET).unwrap();
|
||||||
|
|
||||||
assert_eq!(TokenType::VAL, token.token_type);
|
assert_eq!(TokenType::LET, token.token_type);
|
||||||
assert_eq!("val", token.value);
|
assert_eq!("let", token.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -175,8 +175,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_return_correct_error() {
|
fn should_return_correct_error() {
|
||||||
let tokens = get_tokens(&String::from("val")).unwrap();
|
let tokens = get_tokens(&String::from("let")).unwrap();
|
||||||
assert_eq!(TokenType::VAL, tokens[0].token_type);
|
assert_eq!(TokenType::LET, tokens[0].token_type);
|
||||||
assert_eq!(0, tokens[0].position);
|
assert_eq!(0, tokens[0].position);
|
||||||
let binding = try_parse(&tokens, 0);
|
let binding = try_parse(&tokens, 0);
|
||||||
|
|
||||||
@ -191,8 +191,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_return_error_when_identifier_is_wrong() {
|
fn should_return_error_when_identifier_is_wrong() {
|
||||||
let tokens = get_tokens(&String::from("val 322")).unwrap();
|
let tokens = get_tokens(&String::from("let 322")).unwrap();
|
||||||
assert_eq!(TokenType::VAL, tokens[0].token_type);
|
assert_eq!(TokenType::LET, tokens[0].token_type);
|
||||||
assert_eq!(0, tokens[0].position);
|
assert_eq!(0, tokens[0].position);
|
||||||
let binding = try_parse(&tokens, 0);
|
let binding = try_parse(&tokens, 0);
|
||||||
|
|
||||||
@ -204,7 +204,7 @@ mod tests {
|
|||||||
_ => panic!("Error expected"),
|
_ => 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);
|
let binding = try_parse(&tokens, 0);
|
||||||
|
|
||||||
match binding {
|
match binding {
|
||||||
@ -218,7 +218,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_return_error_when_equal_op_is_wrong() {
|
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);
|
let binding = try_parse(&tokens, 0);
|
||||||
|
|
||||||
match binding {
|
match binding {
|
||||||
|
@ -39,7 +39,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_parse_binding() {
|
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 tokens = crate::lexic::get_tokens(&input).unwrap();
|
||||||
let statement = try_parse(&tokens, 0);
|
let statement = try_parse(&tokens, 0);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user