From 82091e66e8eb73a5d878d3fb6edb546f117747c8 Mon Sep 17 00:00:00 2001 From: FedericoBruzzone Date: Wed, 21 Aug 2024 01:14:27 +0200 Subject: [PATCH] parser: add test for bool assign Signed-off-by: FedericoBruzzone --- src/parser/mod.rs | 24 ++++++++++---- .../id_bool_false_assign.ast.json | 33 +++++++++++++++++++ .../native_types/id_bool_true_assign.ast.json | 33 +++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 testdata/native_types/id_bool_false_assign.ast.json create mode 100644 testdata/native_types/id_bool_true_assign.ast.json diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 6ad1320..090887a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4,11 +4,10 @@ use crate::{ lexer::token::{Keyword, Literal, Token, TokenKind}, source::Source, }; -use std::iter::Peekable; use tracing::info; pub struct Parser { - lexer: Peekable, + lexer: I::IntoIter, curr_token: Option, source: Source, } @@ -20,7 +19,7 @@ impl> Parser { info!("Created Parser"); let curr_token = lexer.next(); Parser { - lexer: lexer.peekable(), + lexer, curr_token, source, } @@ -185,6 +184,16 @@ impl> Parser { location: token.location, } } + Literal::Bool => { + let bool_ = match token.lexeme.parse::() { + Ok(bool_) => bool_, + Err(_) => todo!(), // Error of invalid bool + }; + ast::Expr::Literal { + literal: ast::Literal::Bool(bool_), + location: token.location, + } + } _ => todo!(), }, _ => todo!(), @@ -205,9 +214,12 @@ pub mod tests { let fs_files = collect_fs_files("./testdata/native_types", true); assert_eq!(fs_files.len(), 16); - let fs_files = fs_files - .iter() - .filter(|p| p.ends_with("id_int_assign.fs") || p.ends_with("id_float_assign.fs")); + let fs_files = fs_files.iter().filter(|p| { + p.ends_with("id_int_assign.fs") + || p.ends_with("id_float_assign.fs") + || p.ends_with("id_bool_true_assign.fs") + || p.ends_with("id_bool_false_assign.fs") + }); for path in fs_files { info!("file -> {:?}", path); diff --git a/testdata/native_types/id_bool_false_assign.ast.json b/testdata/native_types/id_bool_false_assign.ast.json new file mode 100644 index 0000000..eb1b8c3 --- /dev/null +++ b/testdata/native_types/id_bool_false_assign.ast.json @@ -0,0 +1,33 @@ +{ + "stmts": [ + { + "Assign": { + "lhs": { + "Identifier": { + "name": "_x_bool", + "location": { + "file_path": "", + "line": 0, + "column_start": 0, + "column_end": 7 + } + } + }, + "type_": "Bool", + "rhs": { + "Literal": { + "literal": { + "Bool": false + }, + "location": { + "file_path": "", + "line": 0, + "column_start": 16, + "column_end": 21 + } + } + } + } + } + ] +} diff --git a/testdata/native_types/id_bool_true_assign.ast.json b/testdata/native_types/id_bool_true_assign.ast.json new file mode 100644 index 0000000..d68a7bb --- /dev/null +++ b/testdata/native_types/id_bool_true_assign.ast.json @@ -0,0 +1,33 @@ +{ + "stmts": [ + { + "Assign": { + "lhs": { + "Identifier": { + "name": "_x_bool", + "location": { + "file_path": "", + "line": 0, + "column_start": 0, + "column_end": 7 + } + } + }, + "type_": "Bool", + "rhs": { + "Literal": { + "literal": { + "Bool": true + }, + "location": { + "file_path": "", + "line": 0, + "column_start": 16, + "column_end": 20 + } + } + } + } + } + ] +}