Skip to content

Commit

Permalink
feat: Add location for Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Nov 23, 2024
1 parent dac3b3e commit 587bf07
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 76 deletions.
4 changes: 1 addition & 3 deletions lib/src/ast/expr_statement.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::ast::*;
use crate::{impl_ast_display, impl_into_statement};

#[derive(Debug)]
pub struct ExprStatement(Expression);

impl_into_statement!(ExprStatement, |x| Statement::expr(Box::new(x)));
impl_ast_display!(ExprStatement, visit_expr_statement);
// impl_into_statement!(ExprStatement, |x| Statement::expr(Box::new(x)));

impl ExprStatement {
pub fn new(expr: Expression) -> Self {
Expand Down
11 changes: 5 additions & 6 deletions lib/src/ast/expression.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::ast::{
AssignExpression, AstVisitor, AstVisitorMut, CallExpression, CompoAccessExpression,
ExprStatement, IntoStatement, LiteralExpression, OperatorExpression, RangeExpression,
Statement, VariableExpression,
LiteralExpression, OperatorExpression, RangeExpression, VariableExpression,
};
use crate::impl_ast_display;
use crate::parser::{LiteralValue, Operator};
use crate::prelude::*;
use crate::{impl_ast_display, impl_into_statement};

use smallvec::{smallvec, SmallVec};

Expand All @@ -26,9 +25,9 @@ pub struct Expression {
}

impl_ast_display!(Expression, visit_expression);
impl_into_statement!(Expression, |x| Statement::expr(Box::new(
ExprStatement::new(x)
)));
// impl_into_statement!(Expression, |x| Statement::expr(Box::new(
// ExprStatement::new(x)
// )));

impl Expression {
#[inline]
Expand Down
4 changes: 1 addition & 3 deletions lib/src/ast/if_statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ast::*;
use crate::{impl_ast_display, impl_into_statement};

#[derive(Debug)]
pub struct IfStatement {
Expand All @@ -9,8 +8,7 @@ pub struct IfStatement {
else_if_list: Vec<ElseIfStatement>,
}

impl_ast_display!(IfStatement, visit_if_statement);
impl_into_statement!(IfStatement, |x| Statement::if_stmt(Box::new(x)));
// impl_into_statement!(IfStatement, |x| Statement::if_stmt(Box::new(x)));

impl IfStatement {
#[allow(unused)]
Expand Down
30 changes: 15 additions & 15 deletions lib/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ macro_rules! impl_ast_display {
};
}

pub trait IntoStatement {
fn into_statement(self) -> Statement;
}

#[macro_export]
macro_rules! impl_into_statement {
($ty:ident, $closure:expr) => {
impl IntoStatement for $ty {
fn into_statement(self) -> Statement {
let f: &dyn Fn(Self) -> Statement = &$closure;
f(self)
}
}
};
}
// pub trait IntoStatement {
// fn into_statement(self) -> Statement;
// }
//
// #[macro_export]
// macro_rules! impl_into_statement {
// ($ty:ident, $closure:expr) => {
// impl IntoStatement for $ty {
// fn into_statement(self) -> Statement {
// let f: &dyn Fn(Self) -> Statement = &$closure;
// f(self)
// }
// }
// };
// }

// #[macro_export]
// macro_rules! impl_accept {
Expand Down
42 changes: 28 additions & 14 deletions lib/src/ast/statement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::ast::{AstVisitor, ExprStatement, IfStatement, IntoStatement};
use crate::ast::{AstVisitor, ExprStatement, IfStatement};
use crate::impl_ast_display;
use crate::parser::Location;
use crate::prelude::*;

#[derive(Debug)]
Expand All @@ -12,47 +13,60 @@ pub enum StmtKind {
#[derive(Debug)]
pub struct Statement {
pub kind: StmtKind,
pub start_pos: Option<Location>,
pub end_pos: Option<Location>,
}

impl_ast_display!(Statement, visit_statement);

impl Statement {
pub fn update_pos(&mut self, start: Option<Location>, end: Option<Location>) {
self.start_pos = start;
self.end_pos = end;
}

pub fn push(self, stmt: Statement) -> Self {
let x = match self.kind {
StmtKind::Stmts(mut stmts) => {
stmts.push(stmt);
return Self::statement_list(stmts);
}
StmtKind::If(x) => x.into_statement(),
StmtKind::Expr(x) => x.into_statement(),
};

Self::statement_list(Box::new(vec![x, stmt]))
if let StmtKind::Stmts(mut stmts) = self.kind {
stmts.push(stmt);
return Self::statement_list(stmts);
}

Self::statement_list(Box::new(vec![self, stmt]))
}

#[inline]
pub fn statement_list(stmts: Box<Vec<Statement>>) -> Self {
Self {
kind: StmtKind::Stmts(stmts),
start_pos: None,
end_pos: None,
}
}

#[inline]
pub fn expr(expr: Box<ExprStatement>) -> Self {
pub fn expr(expr: Box<ExprStatement>, start: Option<Location>, end: Option<Location>) -> Self {
Self {
kind: StmtKind::Expr(expr),
start_pos: start,
end_pos: end,
}
}

#[inline]
pub fn new_expr(expr: Expression) -> Self {
Self::expr(Box::new(ExprStatement::new(expr)))
Self::expr(Box::new(ExprStatement::new(expr)), None, None)
}

#[inline]
pub fn if_stmt(if_stmt: Box<IfStatement>) -> Self {
pub fn if_stmt(
if_stmt: Box<IfStatement>,
start: Option<Location>,
end: Option<Location>,
) -> Self {
Self {
kind: StmtKind::If(if_stmt),
start_pos: start,
end_pos: end,
}
}
}
24 changes: 16 additions & 8 deletions lib/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ pub trait AstVisitor<'ast>: Sized {
}

#[inline]
fn visit_expr_statement(&mut self, expr: &'ast ExprStatement) {
walk_expr_statement(self, expr)
fn visit_expr_statement(&mut self, stmt: &'ast Statement, expr: &'ast ExprStatement) {
walk_expr_statement(self, stmt, expr)
}

#[inline]
fn visit_if_statement(&mut self, ifst: &'ast IfStatement) {
walk_if_statement(self, ifst)
fn visit_if_statement(&mut self, stmt: &'ast Statement, ifst: &'ast IfStatement) {
walk_if_statement(self, stmt, ifst)
}

#[inline]
Expand Down Expand Up @@ -354,8 +354,8 @@ fn walk_expression<'a, V: AstVisitor<'a>>(vis: &mut V, expr: &'a Expression) {
#[inline]
fn walk_statement<'a, V: AstVisitor<'a>>(vis: &mut V, stmt: &'a Statement) {
match stmt.kind {
StmtKind::Expr(ref expr) => vis.visit_expr_statement(expr),
StmtKind::If(ref ifst) => vis.visit_if_statement(ifst),
StmtKind::Expr(ref expr) => vis.visit_expr_statement(stmt, expr),
StmtKind::If(ref ifst) => vis.visit_if_statement(stmt, ifst),
StmtKind::Stmts(ref v) => vis.visit_statement_list(v),
}
}
Expand All @@ -368,11 +368,19 @@ fn walk_statement_list<'a, V: AstVisitor<'a>>(vis: &mut V, stmts: &'a Vec<Statem
}

#[inline]
fn walk_expr_statement<'a, V: AstVisitor<'a>>(vis: &mut V, expr: &'a ExprStatement) {
fn walk_expr_statement<'a, V: AstVisitor<'a>>(
vis: &mut V,
stmt: &'a Statement,
expr: &'a ExprStatement,
) {
vis.visit_expression(expr.expr())
}

fn walk_if_statement<'a, V: AstVisitor<'a>>(vis: &mut V, ifst: &'a IfStatement) {
fn walk_if_statement<'a, V: AstVisitor<'a>>(
vis: &mut V,
stmt: &'a Statement,
ifst: &'a IfStatement,
) {
vis.visit_expression(ifst.condition());
if let Some(ctrl) = ifst.then_controlled() {
vis.visit_statement(ctrl);
Expand Down
16 changes: 10 additions & 6 deletions lib/src/parser/default_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,11 @@ impl<I: Iterator<Item = LexerResult>> DefaultParserImpl<I> {
match self.next_kind()? {
// IF .. THEN .. END_IF
TokenKind::EndIf => {
return Ok(Statement::if_stmt(Box::new(IfStatement::from_then(
cond, then_ctrl,
))));
return Ok(Statement::if_stmt(
Box::new(IfStatement::from_then(cond, then_ctrl)),
None,
None,
));
}
// IF .. THEN .. ELSE .. END_IF
TokenKind::Else => {
Expand All @@ -643,9 +645,11 @@ impl<I: Iterator<Item = LexerResult>> DefaultParserImpl<I> {
};
let _ = self.except_one(TokenKind::EndIf)?;

return Ok(Statement::if_stmt(Box::new(IfStatement::from_then_else(
cond, then_ctrl, else_ctrl,
))));
return Ok(Statement::if_stmt(
Box::new(IfStatement::from_then_else(cond, then_ctrl, else_ctrl)),
None,
None,
));
}
_ => self.next = pos,
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/parser/lalrpop_impl/st.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ SmallComma3<T>: SmallVec3<T> = {

/// Top-Level ST decl + impl body
pub StPOU: (Declaration, Statement) = {
Declaration StatementList => (<>),
Declaration StatementList,
}

/// Top-level ST function body
Expand All @@ -123,8 +123,8 @@ StatementList: Statement = {

/// Single statement
Statement: Statement = {
<e:Expr> ";" => Statement::expr(Box::new(ExprStatement::new(<>))),
IfStatement => Statement::if_stmt(Box::new(<>)),
<start: @L> <e:Expr> ";" <end: @R> => Statement::expr(Box::new(ExprStatement::new(e)), Some(start), Some(end)),
<start: @L> <e:IfStatement> <end: @R> => Statement::if_stmt(Box::new(e), Some(start), Some(end)),
}

IfStatement: IfStatement = {
Expand Down
Loading

0 comments on commit 587bf07

Please sign in to comment.