Skip to content

Commit

Permalink
🐛 Fix debug typing printing
Browse files Browse the repository at this point in the history
semver: patch
  • Loading branch information
Somfic committed Oct 31, 2024
1 parent 8a4f44b commit 2a5be64
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
39 changes: 29 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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),
}
Expand All @@ -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 {}
Expand Down
20 changes: 10 additions & 10 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
@@ -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<Statement<'de>>),
Expression(Expression<'de>),
Expand Down Expand Up @@ -39,7 +39,7 @@ pub enum Statement<'de> {
},
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Expression<'de> {
Primitive(Primitive<'de>),
Binary {
Expand Down Expand Up @@ -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<ParameterDeclaration<'de>>,
}

#[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<Type<'de>>,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Primitive<'de> {
Integer(i64),
Decimal(f64),
Expand All @@ -102,7 +102,7 @@ pub enum Primitive<'de> {
Unit,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum BinaryOperator {
Add,
Subtract,
Expand All @@ -119,7 +119,7 @@ pub enum BinaryOperator {
Or,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum UnaryOperator {
Negate,
Negative,
Expand Down
4 changes: 0 additions & 4 deletions src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ pub fn let_<'de>(parser: &mut Parser<'de>) -> Result<Statement<'de>> {
.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,
Expand Down

0 comments on commit 2a5be64

Please sign in to comment.