Parse number literals
This commit is contained in:
parent
6340f67152
commit
12e2fab066
@ -1 +1 @@
|
|||||||
local c = max(a, b)
|
c = max(5, 7)
|
||||||
17
src/ast.rs
17
src/ast.rs
@ -5,6 +5,8 @@ use crate::token_stream::{
|
|||||||
lexer::{Keyword, Position, Token},
|
lexer::{Keyword, Position, Token},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub type LuaNumber = f64;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Node<T: Clone + Debug> {
|
pub struct Node<T: Clone + Debug> {
|
||||||
pub kind: T,
|
pub kind: T,
|
||||||
@ -273,6 +275,7 @@ pub enum Expression {
|
|||||||
BinOp(BinaryOperator, Box<Node<Expression>>, Box<Node<Expression>>),
|
BinOp(BinaryOperator, Box<Node<Expression>>, Box<Node<Expression>>),
|
||||||
FunctionDefinition(Vec<Node<String>>, Block),
|
FunctionDefinition(Vec<Node<String>>, Block),
|
||||||
FunctionCall(Box<Node<Expression>>, Node<ExpressionList>),
|
FunctionCall(Box<Node<Expression>>, Node<ExpressionList>),
|
||||||
|
Literal(Literal),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for Expression {
|
impl Parse for Expression {
|
||||||
@ -283,7 +286,7 @@ impl Parse for Expression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ExpressionList(Vec<Node<Expression>>);
|
pub struct ExpressionList(pub Vec<Node<Expression>>);
|
||||||
|
|
||||||
impl Parse for ExpressionList {
|
impl Parse for ExpressionList {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, TokenStreamError> {
|
fn parse(mut stream: TokenStream) -> Result<Self, TokenStreamError> {
|
||||||
@ -311,7 +314,7 @@ impl BinaryOperator {
|
|||||||
pub fn precedence(&self) -> u32 {
|
pub fn precedence(&self) -> u32 {
|
||||||
match self {
|
match self {
|
||||||
BinaryOperator::Lt => 100,
|
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 mut expression = if peeked == Some(Token::Keyword(Keyword::Function)) {
|
||||||
let function = stream.parse::<Node<Function>>()?;
|
let function = stream.parse::<Node<Function>>()?;
|
||||||
Expression::FunctionDefinition(function.kind.params, function.kind.block)
|
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 {
|
} else {
|
||||||
Expression::ValueRef(stream.parse()?)
|
Expression::ValueRef(stream.parse()?)
|
||||||
};
|
};
|
||||||
@ -398,3 +406,8 @@ impl Parse for BinaryOperator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum Literal {
|
||||||
|
Number(LuaNumber),
|
||||||
|
}
|
||||||
|
|||||||
@ -20,10 +20,10 @@ fn main() {
|
|||||||
|
|
||||||
dbg!(&tokens);
|
dbg!(&tokens);
|
||||||
|
|
||||||
let block = stream.parse::<Block>().unwrap();
|
let chunk = stream.parse::<Block>().unwrap();
|
||||||
stream.expect(Token::Eof).unwrap();
|
stream.expect(Token::Eof).unwrap();
|
||||||
|
|
||||||
dbg!(block);
|
dbg!(chunk);
|
||||||
|
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user