Skip to content
Merged
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
13 changes: 13 additions & 0 deletions front/parser/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ pub enum FormatPart {
Placeholder,
}

#[derive(Debug, Clone)]
pub enum IncDecKind {
PreInc,
PreDec,
PostInc,
PostDec,
}

#[derive(Debug, Clone)]
pub enum Expression {
StructLiteral {
Expand Down Expand Up @@ -126,6 +134,10 @@ pub enum Expression {
operator: Operator,
expr: Box<Expression>,
},
IncDec {
kind: IncDecKind,
target: Box<Expression>,
},
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -220,6 +232,7 @@ pub enum StatementNode {
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Copy)]
pub enum Mutability {
Var,
Let,
Expand Down
90 changes: 89 additions & 1 deletion front/parser/src/parser/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ast::Expression::Variable;
use crate::ast::{AssignOperator, Expression, FormatPart, Literal, Operator};
use crate::ast::{AssignOperator, Expression, FormatPart, IncDecKind, Literal, Operator};
use lexer::{Token, TokenType};
use std::iter::Peekable;
use std::slice::Iter;
Expand Down Expand Up @@ -33,6 +33,36 @@ pub fn parse_format_string(s: &str) -> Vec<FormatPart> {
parts
}

fn is_assignable(expr: &Expression) -> bool {
match expr {
Expression::Variable(_) => true,
Expression::Deref(_) => true,
Expression::FieldAccess { .. } => true,
Expression::IndexAccess { .. } => true,

Expression::Grouped(inner) => is_assignable(inner),

_ => false,
}
}

fn desugar_incdec(line: usize, target: Expression, is_inc: bool) -> Option<Expression> {
if !is_assignable(&target) {
println!("Error: ++/-- target must bee assignable (line {})", line);
return None;
}

Some(Expression::AssignOperation {
target: Box::new(target),
operator: if is_inc {
AssignOperator::AddAssign
} else {
AssignOperator::SubAssign
},
value: Box::new(Expression::Literal(Literal::Number(1))),
})
}

pub fn parse_expression<'a, T>(tokens: &mut std::iter::Peekable<T>) -> Option<Expression>
where
T: Iterator<Item = &'a Token>,
Expand Down Expand Up @@ -321,6 +351,30 @@ where
let inner = parse_unary_expression(tokens)?;
return Some(Expression::Deref(Box::new(inner)));
}
TokenType::Increment => {
let tok = tokens.next()?; // '++'
let inner = parse_unary_expression(tokens)?;
if !is_assignable(&inner) {
println!("Error: ++ target must be assignable (line {})", tok.line);
return None;
}
return Some(Expression::IncDec {
kind: IncDecKind::PreInc,
target: Box::new(inner),
});
}
TokenType::Decrement => {
let tok = tokens.next()?; // '--'
let inner = parse_unary_expression(tokens)?;
if !is_assignable(&inner) {
println!("Error: -- target must be assignable (line {})", tok.line);
return None;
}
return Some(Expression::IncDec {
kind: IncDecKind::PreDec,
target: Box::new(inner),
});
}
_ => {}
}
}
Expand Down Expand Up @@ -762,6 +816,40 @@ where
});
}

Some(TokenType::Increment) => {
let line = tokens.peek().unwrap().line;
tokens.next(); // consume '++'

let base = expr.take()?;
if !is_assignable(&base) {
println!("Error: postfix ++ target must be assignable (line {})", line);
return None;
}

expr = Some(Expression::IncDec {
kind: IncDecKind::PostInc,
target: Box::new(base),
});
break;
}

Some(TokenType::Decrement) => {
let line = tokens.peek().unwrap().line;
tokens.next(); // consume '--'

let base = expr.take()?;
if !is_assignable(&base) {
println!("Error: postfix -- target must be assignable (line {})", line);
return None;
}

expr = Some(Expression::IncDec {
kind: IncDecKind::PostDec,
target: Box::new(base),
});
break;
}

_ => break,
}
}
Expand Down
1 change: 1 addition & 0 deletions front/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ pub mod import;
pub mod parser;
pub mod stdlib;
pub mod type_system;
mod verification;

pub use parser::*;
6 changes: 6 additions & 0 deletions front/parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use regex::Regex;
use std::collections::HashSet;
use std::iter::Peekable;
use std::slice::Iter;
use crate::parser::verification::validate_program;

pub fn parse(tokens: &Vec<Token>) -> Option<Vec<ASTNode>> {
let mut iter = tokens.iter().peekable();
Expand Down Expand Up @@ -68,6 +69,11 @@ pub fn parse(tokens: &Vec<Token>) -> Option<Vec<ASTNode>> {
}
}

if let Err(e) = validate_program(&nodes) {
println!("❌ {}", e);
return None;
}

Some(nodes)
}

Expand Down
Loading