From a030b0dc1bba48003d007bfa6afe6d13509fcaf3 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 29 Oct 2024 21:44:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit semver: chore --- src/main.rs | 2 +- src/parser/ast.rs | 2 ++ src/parser/expression/primitive.rs | 39 ++++++++++++++++++++++++++++++ src/parser/lookup.rs | 3 +++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 0917102..9f9d917 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ pub mod lexer; pub mod parser; fn main() { - let input = "true if 12 % 2 == 0 else false;\n"; + let input = "{(1); (3); (2);}"; let mut parser = Parser::new(input); let symbol = match parser.parse() { diff --git a/src/parser/ast.rs b/src/parser/ast.rs index a5c615d..f19c4ef 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -40,6 +40,8 @@ pub enum Primitive<'de> { Integer(i64), Decimal(f64), String(Cow<'de, str>), + Identifier(Cow<'de, str>), + Character(char), Boolean(bool), Unit, } diff --git a/src/parser/expression/primitive.rs b/src/parser/expression/primitive.rs index e71156a..a234364 100644 --- a/src/parser/expression/primitive.rs +++ b/src/parser/expression/primitive.rs @@ -45,3 +45,42 @@ pub fn boolean<'de>(parser: &mut Parser) -> Result> { Ok(Expression::Primitive(Primitive::Boolean(value))) } + +pub fn character<'de>(parser: &mut Parser) -> Result> { + let token = parser + .lexer + .expect(TokenKind::Character, "expected a character")?; + + let value = match token.value { + TokenValue::Character(v) => v, + _ => unreachable!(), + }; + + Ok(Expression::Primitive(Primitive::Character(value))) +} + +pub fn string<'de>(parser: &mut Parser<'de>) -> Result> { + let token = parser + .lexer + .expect(TokenKind::String, "expected a string")?; + + let value = match token.value { + TokenValue::String(v) => v, + _ => unreachable!(), + }; + + Ok(Expression::Primitive(Primitive::String(value))) +} + +pub fn identifier<'de>(parser: &mut Parser<'de>) -> Result> { + let token = parser + .lexer + .expect(TokenKind::Identifier, "expected an identifier")?; + + let value = match token.value { + TokenValue::Identifier(v) => v, + _ => unreachable!(), + }; + + Ok(Expression::Primitive(Primitive::Identifier(value))) +} diff --git a/src/parser/lookup.rs b/src/parser/lookup.rs index cc15390..a1e9485 100644 --- a/src/parser/lookup.rs +++ b/src/parser/lookup.rs @@ -112,6 +112,9 @@ impl<'de> Default for Lookup<'de> { .add_expression_handler(TokenKind::Integer, expression::primitive::integer) .add_expression_handler(TokenKind::Decimal, expression::primitive::decimal) .add_expression_handler(TokenKind::Boolean, expression::primitive::boolean) + .add_expression_handler(TokenKind::Character, expression::primitive::character) + .add_expression_handler(TokenKind::String, expression::primitive::string) + .add_expression_handler(TokenKind::Identifier, expression::primitive::identifier) .add_expression_handler(TokenKind::ParenOpen, group) .add_left_expression_handler(TokenKind::If, BindingPower::None, if_) .add_expression_handler(TokenKind::Not, expression::unary::negate)