Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lox-compiler/src/bettercompiler/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn compile_stmt(compiler: &mut Compiler, stmt: &WithSpan<Stmt>) {
compile_class(compiler, identifier.as_ref(), extends.as_ref(), stmts)
}
Stmt::Import(path, identifiers) => compile_import(compiler, path, identifiers.as_ref()),
Stmt::Error => (),
}
}

Expand Down
2 changes: 2 additions & 0 deletions lox-syntax/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub enum Expr {
Call(Box<WithSpan<Expr>>, Vec<WithSpan<Expr>>),
Get(Box<WithSpan<Expr>>, WithSpan<Identifier>),
Set(Box<WithSpan<Expr>>, WithSpan<Identifier>, Box<WithSpan<Expr>>),
Error,
}

#[derive(Debug, PartialEq, Clone)]
Expand All @@ -63,6 +64,7 @@ pub enum Stmt {
Vec<WithSpan<Stmt>>,
),
Import(WithSpan<String>, Option<Vec<WithSpan<String>>>),
Error,
}

pub type Ast = Vec<WithSpan<Stmt>>;
4 changes: 2 additions & 2 deletions lox-syntax/src/expr_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn parse_infix(it: &mut Parser, left: WithSpan<Expr>) -> Result<WithSpan<Expr>,
TokenKind::Dot => parse_get(it, left),
_ => {
it.error(&format!("Unexpected {}", it.peek_token().value), it.peek_token().span);
Err(())
Ok(WithSpan::empty(Expr::Error))
},
}
}
Expand All @@ -90,7 +90,7 @@ fn parse_prefix(it: &mut Parser) -> Result<WithSpan<Expr>, ()> {
TokenKind::LeftParen => parse_grouping(it),
_ => {
it.error(&format!("Unexpected {}", it.peek_token().value), it.peek_token().span);
Err(())
Ok(WithSpan::empty(Expr::Error))
},
}
}
Expand Down
6 changes: 5 additions & 1 deletion lox-syntax/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ pub fn parse(code: &str) -> Result<Ast, Vec<Diagnostic>> {
let tokens = tokenize_with_context(code);
let mut parser = crate::parser::Parser::new(&tokens);
match parse(&mut parser) {
Ok(ast) => Ok(ast),
Ok(ast) if parser.diagnostics().is_empty() => Ok(ast),
Ok(ast) => {
println!("Faulty AST: {:#?}", ast);
Err(parser.diagnostics().to_vec())
},
Err(_) => Err(parser.diagnostics().to_vec()),
}
}
11 changes: 11 additions & 0 deletions lox-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ impl<'a> Parser<'a> {
}
}

pub fn expect2(&mut self, expected: TokenKind) -> Option<Span> {
let token = self.peek_token();
let kind = TokenKind::from(token);
if kind != expected {
self.error(&format!("Expected {} got {}", expected, token.value), token.span);
return None;
}

Some(self.advance().span)
}

pub fn optionally(&mut self, expected: TokenKind) -> Result<bool, ()> {
let token = self.peek();
if TokenKind::from(token) == expected {
Expand Down
6 changes: 6 additions & 0 deletions lox-syntax/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ impl Span {
pub fn union_span(a: Self, b: Self) -> Self {
use std::cmp;

if a == Span::empty() {
return b;
} else if b == Span::empty() {
return a;
}

Span {
start: cmp::min(a.start, b.start),
end: cmp::max(a.end, b.end),
Expand Down
9 changes: 7 additions & 2 deletions lox-syntax/src/stmt_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ fn parse_return_statement(it: &mut Parser) -> Result<WithSpan<Stmt>, ()> {

fn parse_expr_statement(it: &mut Parser) -> Result<WithSpan<Stmt>, ()> {
let expr = parse_expr(it)?;
let end_span = it.expect(TokenKind::Semicolon)?;
let end_span = match it.expect2(TokenKind::Semicolon) {
Some(token) => token,
None => {
return Ok(WithSpan::empty(Stmt::Error));
},
};

let span = Span::union(&expr, end_span);
let span = Span::union_span(expr.span, end_span);
Ok(WithSpan::new(Stmt::Expression(Box::new(expr)), span))
}

Expand Down
12 changes: 3 additions & 9 deletions test.lox
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
{fun fib(n) {
if (n < 2) return n;
return fib(n - 2) + fib(n - 1);
}

var start = clock();
print fib(35) == 9227465;
print clock() - start;
}
// 3 +*4;
// 3*;
print (3-);