Skip to content

Commit

Permalink
🚧 wip
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Lucas de Jong committed Oct 28, 2024
1 parent e92bb2c commit a6a6a78
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl<'de> Lexer<'de> {
Some(Ok(token)) if expected == token.kind => Ok(token),
Some(Ok(token)) => Err(miette::miette! {
labels = vec![
token.label(format!("Expected {} here", expected))
token.label(format!("expected {} here", expected))
],
help = format!("Expected {}, got {}", expected, token.kind),
help = format!("expected {}, got {} instead", expected, token.kind),
"{unexpected}",
}
.with_source_code(self.whole.to_string())),
Expand Down Expand Up @@ -59,14 +59,14 @@ impl<'de> Lexer<'de> {
labels = vec![
token.label("here")
],
help = format!("Expected {token:?}"),
help = format!("expected {token:?}"),
"{unexpected}",
}
.with_source_code(self.whole.to_string())),
Some(Err(e)) => Err(e),
None => Err(miette::miette! {
labels = vec![
LabeledSpan::at_offset(self.byte_offset - 1, "Expected more source code here")
LabeledSpan::at_offset(self.byte_offset - 1, "expected more source code here")
],
help = "more source code was expected, but none was found",
"{unexpected}",
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod lexer;
pub mod parser;

fn main() {
let input = "1 + 1111;";
let input = "1 * 2 + 3;";

let mut parser = Parser::new(input);
let symbol = match parser.parse() {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum Expression<'de> {
#[derive(Debug)]
pub enum Primitive<'de> {
Integer(i64),
Float(f64),
Decimal(f64),
String(Cow<'de, str>),
Boolean(bool),
}
Expand Down
10 changes: 5 additions & 5 deletions src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ pub fn parse<'de>(
Some(Err(err)) => return Err(miette::miette!(err.to_string())), // FIXME: better error handling
None => {
return Err(miette::miette! {
help = "expected a new expression",
"expected a new expression, found EOF"
help = "expected an expression",
"expected an expression"
}
.with_source_code(parser.source.to_string()))
}
};

let handler = parser.lookup.expression_lookup.get(&token.kind).ok_or(
miette::miette! {
labels = vec![token.label("expected a new expression")],
help = format!("cannot parse {} into a new expression", token.kind),
"expected a new expression, found {}", token.kind
labels = vec![token.label("expected an expression")],
help = format!("{} is not an expression", token.kind),
"expected an expression, found {}", token.kind
}
.with_source_code(parser.source.to_string()),
)?;
Expand Down
33 changes: 33 additions & 0 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ impl<'de> Default for Lookup<'de> {
// left_type_lookup: HashMap::new(),
}
.add_expression_handler(TokenKind::Integer, integer)
.add_expression_handler(TokenKind::Decimal, decimal)
.add_left_expression_handler(TokenKind::Plus, BindingPower::Additive, addition)
.add_left_expression_handler(
TokenKind::Star,
BindingPower::Multiplicative,
multiplication,
)
}
}

Expand All @@ -127,6 +133,19 @@ fn integer<'de>(parser: &mut Parser) -> Result<Expression<'de>> {
Ok(Expression::Primitive(Primitive::Integer(value)))
}

fn decimal<'de>(parser: &mut Parser) -> Result<Expression<'de>> {
let token = parser
.lexer
.expect(TokenKind::Decimal, "expected a decimal")?;

let value = match token.value {
TokenValue::Decimal(v) => v,
_ => unreachable!(),
};

Ok(Expression::Primitive(Primitive::Decimal(value)))
}

fn addition<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
Expand All @@ -140,3 +159,17 @@ fn addition<'de>(
right: Box::new(rhs),
})
}

fn multiplication<'de>(
parser: &mut Parser<'de>,
lhs: Expression<'de>,
bp: BindingPower,
) -> Result<Expression<'de>> {
let rhs = expression::parse(parser, bp)?;

Ok(Expression::Binary {
operator: BinaryOperator::Multiply,
left: Box::new(lhs),
right: Box::new(rhs),
})
}
7 changes: 4 additions & 3 deletions src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use miette::{Context, Result};

pub fn parse<'de>(parser: &mut Parser<'de>) -> Result<Statement<'de>> {
let expression = expression::parse(parser, BindingPower::None)?;
parser
.lexer
.expect(TokenKind::Semicolon, "expected a semicolon")?;
parser.lexer.expect(
TokenKind::Semicolon,
"expected a semicolon at the end of an expression",
)?;

Ok(Statement::Expression(expression))
}

0 comments on commit a6a6a78

Please sign in to comment.