Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Somfic committed Oct 29, 2024
1 parent 71721d2 commit a030b0d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
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 = "true if 12 % 2 == 0 else false;\n";
let input = "{(1); (3); (2);}";

let mut parser = Parser::new(input);
let symbol = match parser.parse() {
Expand Down
2 changes: 2 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum Primitive<'de> {
Integer(i64),
Decimal(f64),
String(Cow<'de, str>),
Identifier(Cow<'de, str>),
Character(char),
Boolean(bool),
Unit,
}
Expand Down
39 changes: 39 additions & 0 deletions src/parser/expression/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,42 @@ pub fn boolean<'de>(parser: &mut Parser) -> Result<Expression<'de>> {

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

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

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

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

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

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

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

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

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

Ok(Expression::Primitive(Primitive::Identifier(value)))
}
3 changes: 3 additions & 0 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ impl<'de> Default for Lookup<'de> {
.add_expression_handler(TokenKind::Integer, expression::primitive::integer)
.add_expression_handler(TokenKind::Decimal, expression::primitive::decimal)
.add_expression_handler(TokenKind::Boolean, expression::primitive::boolean)
.add_expression_handler(TokenKind::Character, expression::primitive::character)
.add_expression_handler(TokenKind::String, expression::primitive::string)
.add_expression_handler(TokenKind::Identifier, expression::primitive::identifier)
.add_expression_handler(TokenKind::ParenOpen, group)
.add_left_expression_handler(TokenKind::If, BindingPower::None, if_)
.add_expression_handler(TokenKind::Not, expression::unary::negate)
Expand Down

0 comments on commit a030b0d

Please sign in to comment.