From 12e2fab066006f90d5973fe74654dd2f06e35ec9 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sat, 14 Mar 2026 17:36:01 +0200 Subject: [PATCH] Parse number literals --- examples/test.lua | 2 +- src/ast.rs | 17 +++++++++++++++-- src/main.rs | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/test.lua b/examples/test.lua index c300cd3..7dff1e4 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -1 +1 @@ -local c = max(a, b) \ No newline at end of file +c = max(5, 7) \ No newline at end of file diff --git a/src/ast.rs b/src/ast.rs index aa6caf5..7b3ac8b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -5,6 +5,8 @@ use crate::token_stream::{ lexer::{Keyword, Position, Token}, }; +pub type LuaNumber = f64; + #[derive(Debug, Clone)] pub struct Node { pub kind: T, @@ -273,6 +275,7 @@ pub enum Expression { BinOp(BinaryOperator, Box>, Box>), FunctionDefinition(Vec>, Block), FunctionCall(Box>, Node), + Literal(Literal), } impl Parse for Expression { @@ -283,7 +286,7 @@ impl Parse for Expression { } #[derive(Debug, Clone)] -pub struct ExpressionList(Vec>); +pub struct ExpressionList(pub Vec>); impl Parse for ExpressionList { fn parse(mut stream: TokenStream) -> Result { @@ -311,7 +314,7 @@ impl BinaryOperator { pub fn precedence(&self) -> u32 { match self { BinaryOperator::Lt => 100, - BinaryOperator::Gt => 105, + BinaryOperator::Gt => 100, } } } @@ -324,6 +327,11 @@ impl Parse for PrimaryExpression { let mut expression = if peeked == Some(Token::Keyword(Keyword::Function)) { let function = stream.parse::>()?; Expression::FunctionDefinition(function.kind.params, function.kind.block) + } else if let Some(Token::DecimalValue(value)) = peeked { + stream.next(); // Consume decimal value + Expression::Literal(Literal::Number( + u64::from_str_radix(&value, 10).unwrap() as f64 + )) } else { Expression::ValueRef(stream.parse()?) }; @@ -398,3 +406,8 @@ impl Parse for BinaryOperator { } } } + +#[derive(Debug, Clone, Copy)] +pub enum Literal { + Number(LuaNumber), +} diff --git a/src/main.rs b/src/main.rs index 7eaa5d2..ea591df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,10 +20,10 @@ fn main() { dbg!(&tokens); - let block = stream.parse::().unwrap(); + let chunk = stream.parse::().unwrap(); stream.expect(Token::Eof).unwrap(); - dbg!(block); + dbg!(chunk); println!("Hello, world!"); }