Switch interpreter to use new base value types
The commit changes the interpreter and related code to use the new `BaeValue` and `Number` types, moving away from direct floats. The main changes include: - Replace LiteralValue with BaeValue across the codebase - Add Number enum for type-safe numeric values - Move AST definitions to common module - Update operators to handle new numeric types - Add 'is' token type and parser support Switch to common base_value library for value types This commit represents a significant refactoring to switch the interpreter to use a new shared base value type system. The main changes include: 1. Move value types to a new common/base_value.rs module 2. Add richer numeric type support with promotion rules 3. Update interpreter to use new BaseValue enum 4. Move AST types to common module for sharing 5. Consolidate value operations like Add, Sub etc 6. Add proper test coverage for numeric operations The commit improves type safety and adds more robust numeric handling while keeping the interpreter's core logic clean.
This commit is contained in:
+47
-31
@@ -1,11 +1,13 @@
|
||||
use crate::{
|
||||
frontend::{
|
||||
common::{
|
||||
ast::{AstNode, Expr, Stmt},
|
||||
source_registry::{SourcePosition, SourceSlice},
|
||||
tokens::{LiteralValue, LoxFunction, Token, TokenType},
|
||||
base_value::{BaseValue, LoxFunction},
|
||||
lox_result::{parse_error, runtime_error, LoxResult},
|
||||
},
|
||||
frontend::{
|
||||
source_registry::SourceSlice,
|
||||
tokens::{Token, TokenType},
|
||||
},
|
||||
logging::display_ast::{pretty_print_with_config, PrettyConfig},
|
||||
result::{parse_error, runtime_error, LoxResult},
|
||||
};
|
||||
|
||||
pub struct Parser {
|
||||
@@ -21,30 +23,14 @@ impl Parser {
|
||||
pub fn parse(&mut self) -> LoxResult<Vec<AstNode<Stmt>>> {
|
||||
let mut statements = Vec::new();
|
||||
let mut errors = Vec::new();
|
||||
println!("\n \n ++++++++++++++++++++++++++++++++++++\nStarting parsing");
|
||||
// Con configurazione personalizzata
|
||||
let config = PrettyConfig {
|
||||
indent: " ".to_string(),
|
||||
max_depth: None,
|
||||
show_positions: false,
|
||||
..Default::default()
|
||||
};
|
||||
while !self.is_at_end() {
|
||||
match self.statement() {
|
||||
Ok(stmt) => {
|
||||
println!(
|
||||
"Parsed Successfully: {}",
|
||||
pretty_print_with_config(&stmt.node, &config)
|
||||
);
|
||||
|
||||
statements.push(stmt.clone());
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
errors.push(err);
|
||||
let old_current = self.current;
|
||||
self.synchronize();
|
||||
println!("Synchronized from {} to {}", old_current, self.current);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,7 +203,7 @@ impl Parser {
|
||||
let name = self.consume(TokenType::Identifier, "Expect variable name.")?;
|
||||
let name_lexeme = name.lexeme.clone();
|
||||
|
||||
let mut _type_annotation = LiteralValue::Nil;
|
||||
let mut _type_annotation = BaseValue::Nil;
|
||||
self.consume(TokenType::Colon, "Expect column")?;
|
||||
|
||||
match self.peek().token_type {
|
||||
@@ -254,16 +240,18 @@ impl Parser {
|
||||
self.advance();
|
||||
}
|
||||
}
|
||||
self.advance();
|
||||
self.consume(TokenType::RightParen, "Expected ')' after parameters")?;
|
||||
let end_position = self.peek().source_slice.clone();
|
||||
let combine_position = SourceSlice {
|
||||
source_id: start_slice.source_id,
|
||||
start_position: start_slice.start_position.clone(),
|
||||
end_position: end_position.end_position,
|
||||
};
|
||||
|
||||
let mut guard: Option<Box<Expr>> = None;
|
||||
if self.peek().token_type == TokenType::RightBrace {
|
||||
self.advance();
|
||||
if self.peek().token_type == TokenType::LeftBrace {
|
||||
self.consume(TokenType::LeftBrace, "Expected '{' after guard expression")?;
|
||||
println!("ho trovato una guradia");
|
||||
guard = Some(Box::new(self.expression()?.node.clone()));
|
||||
self.consume(TokenType::RightBrace, "Expected '}' after guard expression")?;
|
||||
}
|
||||
@@ -271,7 +259,7 @@ impl Parser {
|
||||
let body = self.statement()?;
|
||||
let node = AstNode {
|
||||
node: Expr::Literal {
|
||||
value: LiteralValue::Function(LoxFunction {
|
||||
value: BaseValue::Function(LoxFunction {
|
||||
parameters,
|
||||
body,
|
||||
closure: None,
|
||||
@@ -303,7 +291,7 @@ impl Parser {
|
||||
}
|
||||
let mut value = AstNode {
|
||||
node: Expr::Literal {
|
||||
value: LiteralValue::Nil,
|
||||
value: BaseValue::Nil,
|
||||
},
|
||||
source_slice: start_slice.clone(),
|
||||
};
|
||||
@@ -453,9 +441,36 @@ impl Parser {
|
||||
}
|
||||
|
||||
fn or_and(&mut self) -> LoxResult<AstNode<Expr>> {
|
||||
let mut expr = self.equality()?;
|
||||
let mut expr = self.logical_is()?;
|
||||
|
||||
while [TokenType::Or, TokenType::And].contains(&self.peek().token_type) {
|
||||
let start_slice = expr.source_slice.clone();
|
||||
let operator = self.peek().token_type.clone();
|
||||
self.advance();
|
||||
let right = self.logical_is()?;
|
||||
let end_slice = right.source_slice.clone();
|
||||
let combined_slice = SourceSlice::from_positions(
|
||||
start_slice.source_id,
|
||||
start_slice.start_position,
|
||||
end_slice.end_position,
|
||||
);
|
||||
expr = AstNode::new(
|
||||
Expr::Binary {
|
||||
left: Box::new(expr),
|
||||
operator,
|
||||
right: Box::new(right),
|
||||
},
|
||||
combined_slice,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(expr)
|
||||
}
|
||||
|
||||
fn logical_is(&mut self) -> LoxResult<AstNode<Expr>> {
|
||||
let mut expr = self.equality()?;
|
||||
|
||||
while self.peek().token_type == TokenType::Is {
|
||||
let start_slice = expr.source_slice.clone();
|
||||
let operator = self.peek().token_type.clone();
|
||||
self.advance();
|
||||
@@ -478,6 +493,7 @@ impl Parser {
|
||||
|
||||
Ok(expr)
|
||||
}
|
||||
|
||||
fn equality(&mut self) -> LoxResult<AstNode<Expr>> {
|
||||
let mut expr = self.comparison()?;
|
||||
|
||||
@@ -667,7 +683,7 @@ impl Parser {
|
||||
self.advance();
|
||||
Ok(AstNode::new(
|
||||
Expr::Literal {
|
||||
value: LiteralValue::Boolean(false),
|
||||
value: BaseValue::Boolean(false),
|
||||
},
|
||||
source_slice,
|
||||
))
|
||||
@@ -677,7 +693,7 @@ impl Parser {
|
||||
self.advance();
|
||||
Ok(AstNode::new(
|
||||
Expr::Literal {
|
||||
value: LiteralValue::Boolean(true),
|
||||
value: BaseValue::Boolean(true),
|
||||
},
|
||||
source_slice,
|
||||
))
|
||||
@@ -687,7 +703,7 @@ impl Parser {
|
||||
self.advance();
|
||||
Ok(AstNode::new(
|
||||
Expr::Literal {
|
||||
value: LiteralValue::Nil,
|
||||
value: BaseValue::Nil,
|
||||
},
|
||||
source_slice,
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user