From 2a5be64923dfe45c82ebb98f330b406d309bd253 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 31 Oct 2024 17:30:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20debug=20typing=20printing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit semver: patch --- src/main.rs | 39 +++++++++++++++++++++++++++++---------- src/parser/ast.rs | 20 ++++++++++---------- src/parser/statement.rs | 4 ---- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index a6d139d..a7bd084 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,14 @@ fn main() { })) .unwrap(); - let input = " fn main() { + let input = " + fn main() { fib(9999); } fn fib(n ~ int) ~ int { + let n = 12; + if n < 2 return n; fib(n - 1) + fib(n - 20) } @@ -43,7 +46,7 @@ fn main() { } }; - match symbol { + match &symbol { parser::ast::Symbol::Statement(statement) => print_statement(statement), parser::ast::Symbol::Expression(expression) => print_expression(expression), } @@ -59,25 +62,41 @@ fn main() { // } } -fn print_expression(expression: Expression) { - println!( - "{:?} of type ({:?})", - expression, - expression.possible_types() - ); +fn print_expression(expression: &Expression) { + match &expression { + Expression::Block { + statements, + return_value, + } => { + for statement in statements { + print_statement(statement); + } + print_expression(return_value); + } + Expression::Group(expression) => print_expression(expression), + e => println!("{:?} typeof {:?}", e, e.possible_types()), + } } -fn print_statement(statement: Statement) { +fn print_statement(statement: &Statement) { match statement { Statement::Block(statements) => { for statement in statements { print_statement(statement); } } + Statement::Function { + header, + body, + explicit_return_type, + } => { + print_expression(body); + } Statement::Return(expression) => print_expression(expression), Statement::Expression(expression) => print_expression(expression), + Statement::Assignment { name, value } => print_expression(value), _ => {} - } + }; } struct SomHighlighter {} diff --git a/src/parser/ast.rs b/src/parser/ast.rs index 37d1f4a..b8afe58 100644 --- a/src/parser/ast.rs +++ b/src/parser/ast.rs @@ -1,12 +1,12 @@ use std::borrow::Cow; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Symbol<'de> { Statement(Statement<'de>), Expression(Expression<'de>), } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Statement<'de> { Block(Vec>), Expression(Expression<'de>), @@ -39,7 +39,7 @@ pub enum Statement<'de> { }, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Expression<'de> { Primitive(Primitive<'de>), Binary { @@ -67,31 +67,31 @@ pub enum Expression<'de> { }, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct FunctionHeader<'de> { pub name: Cow<'de, str>, pub parameters: Vec>, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ParameterDeclaration<'de> { pub name: Cow<'de, str>, pub explicit_type: Type<'de>, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct StructMemberDeclaration<'de> { pub name: Cow<'de, str>, pub explicit_type: Type<'de>, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct EnumMemberDeclaration<'de> { pub name: Cow<'de, str>, pub value_type: Option>, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Primitive<'de> { Integer(i64), Decimal(f64), @@ -102,7 +102,7 @@ pub enum Primitive<'de> { Unit, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum BinaryOperator { Add, Subtract, @@ -119,7 +119,7 @@ pub enum BinaryOperator { Or, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum UnaryOperator { Negate, Negative, diff --git a/src/parser/statement.rs b/src/parser/statement.rs index 1b7e766..9f7eb4d 100644 --- a/src/parser/statement.rs +++ b/src/parser/statement.rs @@ -65,10 +65,6 @@ pub fn let_<'de>(parser: &mut Parser<'de>) -> Result> { .expect(TokenKind::Equal, "expected an equal sign")?; let expression = expression::parse(parser, BindingPower::None)?; - parser - .lexer - .expect(TokenKind::Semicolon, "expected a semicolon")?; - Ok(Statement::Assignment { name: identifier, value: expression,