Skip to content

Commit

Permalink
add add/sub assign operators
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 20, 2024
1 parent 40d3f26 commit f92a41e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
break;
}
count = count + 1;
count += 1;
let temp = a;
a = b;
Expand All @@ -39,13 +39,13 @@ fn main() {
break;
}
counter = counter + 1;
counter += 1;
if counter == 10 {
continue;
}
n = n + 1;
n += 1;
}
let result1 = fib1(n);
Expand Down
6 changes: 6 additions & 0 deletions src/repr/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub enum Token {
*/
#[token("=")]
Eq,
#[token("+=")]
AddAssign,
#[token("-=")]
MinusAssign,
#[token("->")]
ThinArrow,
#[token(":")]
Expand Down Expand Up @@ -117,6 +121,8 @@ impl Display for Token {
Token::And => write!(f, "&&"),
Token::Or => write!(f, "||"),
Token::Eq => write!(f, "="),
Token::AddAssign => write!(f, "+="),
Token::MinusAssign => write!(f, "-="),
Token::ThinArrow => write!(f, "->"),
Token::Colon => write!(f, ":"),
Token::SemiColon => write!(f, ";"),
Expand Down
45 changes: 45 additions & 0 deletions src/stage/parse/expression/e_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,48 @@ pub fn parse_assign(compiler: &mut Compiler, tokens: &mut Lexer<'_>) -> Result<A
ty_info: None,
})
}

pub fn parse_op_assign(
compiler: &mut Compiler,
tokens: &mut Lexer<'_>,
) -> Result<Assign, ParseError> {
let (binding, binding_span) = match tokens.next_spanned().unwrap() {
(Token::Ident(ident), span) => (ident, span),
(token, _) => {
return Err(ParseError::ExpectedToken {
expected: Box::new(Token::Ident(String::new())),
found: Box::new(token),
reason: "assign must start with ident".to_string(),
});
}
};

let binding = compiler.symbols.get_or_intern(binding);

let operation = match tokens.next_token().unwrap() {
Token::AddAssign => InfixOperation::Plus,
Token::MinusAssign => InfixOperation::Minus,
token => {
return Err(ParseError::UnexpectedToken(token));
}
};

let right = parse_expression(compiler, tokens, Precedence::Lowest)?;

Ok(Assign {
span: binding_span.start..right.span().end,
binding,
value: Box::new(Expression::Infix(Infix {
span: right.span().start..right.span().end,
left: Box::new(Expression::Ident(Ident {
span: binding_span,
binding,
ty_info: None,
})),
operation,
right: Box::new(right),
ty_info: None,
})),
ty_info: None,
})
}
13 changes: 8 additions & 5 deletions src/stage/parse/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::iter;

use e_assign::parse_assign;
use e_assign::{parse_assign, parse_op_assign};
use e_loop::parse_loop;

use super::*;
Expand Down Expand Up @@ -40,10 +40,13 @@ impl Precedence {
fn parse_prefix(compiler: &mut Compiler, tokens: &mut Lexer<'_>) -> Result<Expression, ParseError> {
match tokens.peek_token().unwrap().clone() {
Token::Integer(_) => Ok(Expression::Integer(parse_integer(compiler, tokens)?)),
Token::Ident(_) if matches!(tokens.double_peek_token(), Some(Token::Eq)) => {
Ok(Expression::Assign(parse_assign(compiler, tokens)?))
}
Token::Ident(_) => Ok(Expression::Ident(parse_ident(compiler, tokens)?)),
Token::Ident(_) => match tokens.double_peek_token().unwrap() {
Token::Eq => Ok(Expression::Assign(parse_assign(compiler, tokens)?)),
Token::AddAssign | Token::MinusAssign => {
Ok(Expression::Assign(parse_op_assign(compiler, tokens)?))
}
_ => Ok(Expression::Ident(parse_ident(compiler, tokens)?)),
},
Token::True => Ok(Expression::Boolean(parse_boolean(compiler, tokens)?)),
Token::False => Ok(Expression::Boolean(parse_boolean(compiler, tokens)?)),
Token::LeftBrace => Ok(Expression::Block(parse_block(compiler, tokens)?)),
Expand Down

0 comments on commit f92a41e

Please sign in to comment.