Skip to content

Commit

Permalink
feat: Add location for variable expr
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Nov 23, 2024
1 parent 7ff6ca5 commit e1805ae
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 71 deletions.
6 changes: 5 additions & 1 deletion lib/src/analysis/type_analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ impl AstVisitorMut for TypeAnalyzer {
self.top_mut().derived_type = Some(literal.literal().ty())
}

fn visit_variable_expression_mut(&mut self, variable: &mut VariableExpression) {
fn visit_variable_expression_mut(
&mut self,
expr: &mut ExprInfo,
variable: &mut VariableExpression,
) {
let derived_variable = if self.top().search_local_only {
self.current_scope().find_local_variable(variable.name())
} else {
Expand Down
27 changes: 22 additions & 5 deletions lib/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::ast::{
LiteralExpression, OperatorExpression, RangeExpression, VariableExpression,
};
use crate::impl_ast_display;
use crate::parser::{LiteralValue, Operator};
use crate::prelude::*;

use smallvec::{smallvec, SmallVec};
Expand All @@ -21,7 +20,14 @@ pub enum ExprKind {

#[derive(Debug)]
pub struct Expression {
pub(crate) kind: ExprKind,
pub kind: ExprKind,
pub info: ExprInfo,
}

#[derive(Debug, Default)]
pub struct ExprInfo {
pub start: Option<Location>,
pub end: Option<Location>,
}

impl_ast_display!(Expression, visit_expression);
Expand Down Expand Up @@ -50,6 +56,7 @@ impl Expression {
pub fn assign(assign: Box<AssignExpression>) -> Self {
Self {
kind: ExprKind::Assign(assign),
info: ExprInfo::default(),
}
}

Expand All @@ -62,13 +69,15 @@ impl Expression {
pub fn call(call: Box<CallExpression>) -> Self {
Self {
kind: ExprKind::Call(call),
info: ExprInfo::default(),
}
}

#[inline]
pub fn literal(literal: Box<LiteralExpression>) -> Self {
Self {
kind: ExprKind::Literal(literal),
info: ExprInfo::default(),
}
}

Expand All @@ -81,6 +90,7 @@ impl Expression {
pub fn operator(operator: Box<OperatorExpression>) -> Self {
Self {
kind: ExprKind::Operator(operator),
info: ExprInfo::default(),
}
}

Expand All @@ -95,21 +105,27 @@ impl Expression {
}

#[inline]
pub fn variable(variable: Box<VariableExpression>) -> Self {
pub fn variable(
variable: Box<VariableExpression>,
start: Option<Location>,
end: Option<Location>,
) -> Self {
Self {
kind: ExprKind::Variable(variable),
info: ExprInfo { start, end },
}
}

#[inline]
pub fn new_variable(var: StString) -> Self {
Self::variable(Box::new(VariableExpression::new(var)))
pub fn new_variable(var: StString, start: Option<Location>, end: Option<Location>) -> Self {
Self::variable(Box::new(VariableExpression::new(var)), start, end)
}

#[inline]
pub fn compo(compo: Box<CompoAccessExpression>) -> Self {
Self {
kind: ExprKind::Compo(compo),
info: ExprInfo::default(),
}
}

Expand All @@ -122,6 +138,7 @@ impl Expression {
pub fn range(range: Box<RangeExpression>) -> Self {
Self {
kind: ExprKind::Range(range),
info: ExprInfo::default(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ mod compo_access_expression;
pub use compo_access_expression::CompoAccessExpression;

mod statement;
pub use statement::{Statement, StmtKind};
pub use statement::{Statement, StmtInfo, StmtKind};

mod expression;
pub use expression::{ExprKind, Expression};
pub use expression::{ExprInfo, ExprKind, Expression};

mod variable_expression;
pub use variable_expression::VariableExpression;
Expand Down
23 changes: 15 additions & 8 deletions lib/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub enum StmtKind {
#[derive(Debug)]
pub struct Statement {
pub kind: StmtKind,
pub info: StmtInfo,
}

#[derive(Debug, Default)]
pub struct StmtInfo {
pub start_pos: Option<Location>,
pub end_pos: Option<Location>,
}
Expand All @@ -21,8 +26,10 @@ 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;
self.info = StmtInfo {
start_pos: start,
end_pos: end,
};
}

pub fn push(self, stmt: Statement) -> Self {
Expand All @@ -38,8 +45,7 @@ impl Statement {
pub fn statement_list(stmts: Box<Vec<Statement>>) -> Self {
Self {
kind: StmtKind::Stmts(stmts),
start_pos: None,
end_pos: None,
info: StmtInfo::default(),
}
}

Expand All @@ -51,8 +57,7 @@ impl Statement {
) -> Self {
Self {
kind: StmtKind::Expr(expr),
start_pos: start,
end_pos: end,
info: StmtInfo::default(),
}
}

Expand All @@ -69,8 +74,10 @@ impl Statement {
) -> Self {
Self {
kind: StmtKind::If(if_stmt),
start_pos: start,
end_pos: end,
info: StmtInfo {
start_pos: start,
end_pos: end,
},
}
}
}
5 changes: 2 additions & 3 deletions lib/src/ast/variable_expression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ast::{AstVisitor, Type};
use crate::impl_ast_display;
use crate::ast::Type;
use crate::parser::StString;

#[derive(Debug, Clone)]
Expand All @@ -8,7 +7,7 @@ pub struct VariableExpression {
ty: Option<Type>,
}

impl_ast_display!(VariableExpression, visit_variable_expression);
// impl_ast_display!(VariableExpression, visit_variable_expression);

impl VariableExpression {
pub fn new(var: StString) -> Self {
Expand Down
52 changes: 36 additions & 16 deletions lib/src/ast/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::RangeExpression;
use crate::ast::call_expression::CallExpression;
use crate::ast::expression::ExprInfo;
use crate::ast::statement::StmtInfo;
use crate::ast::*;

use super::RangeExpression;

// Mutable visitor
pub trait DeclVisitorMut: Sized {
#[inline]
Expand Down Expand Up @@ -48,7 +49,11 @@ pub trait AstVisitorMut: Sized {
}

#[inline]
fn visit_variable_expression_mut(&mut self, variable: &mut VariableExpression) {
fn visit_variable_expression_mut(
&mut self,
expr: &mut ExprInfo,
variable: &mut VariableExpression,
) {
walk_variable_expression_mut(self, variable)
}

Expand Down Expand Up @@ -83,8 +88,8 @@ pub trait AstVisitorMut: Sized {
}

#[inline]
fn visit_if_statement_mut(&mut self, ifst: &mut IfStatement) {
walk_if_statement_mut(self, ifst)
fn visit_if_statement_mut(&mut self, info: &mut StmtInfo, ifst: &mut IfStatement) {
walk_if_statement_mut(self, info, ifst)
}

#[inline]
Expand Down Expand Up @@ -121,7 +126,9 @@ fn walk_expression_mut<V: AstVisitorMut>(vis: &mut V, expr: &mut Expression) {
ExprKind::Assign(ref mut assign) => vis.visit_assign_expression_mut(assign),
ExprKind::Operator(ref mut operator) => vis.visit_operator_expression_mut(operator),
ExprKind::Compo(ref mut compo) => vis.visit_compo_access_expression_mut(compo),
ExprKind::Variable(ref mut variable) => vis.visit_variable_expression_mut(variable),
ExprKind::Variable(ref mut variable) => {
vis.visit_variable_expression_mut(&mut expr.info, variable)
}
ExprKind::Literal(ref mut literal) => vis.visit_literal_mut(literal),
ExprKind::Call(ref mut call) => vis.visit_call_expression_mut(call),
ExprKind::Range(ref mut range) => vis.visit_range_expression_mut(range),
Expand All @@ -132,7 +139,7 @@ fn walk_expression_mut<V: AstVisitorMut>(vis: &mut V, expr: &mut Expression) {
fn walk_statement_mut<V: AstVisitorMut>(vis: &mut V, stmt: &mut Statement) {
match stmt.kind {
StmtKind::Expr(ref mut expr) => vis.visit_expr_statement_mut(expr),
StmtKind::If(ref mut ifst) => vis.visit_if_statement_mut(ifst),
StmtKind::If(ref mut ifst) => vis.visit_if_statement_mut(&mut stmt.info, ifst),
StmtKind::Stmts(ref mut v) => vis.visit_statement_list_mut(v),
}
}
Expand All @@ -150,7 +157,11 @@ fn walk_expr_statement_mut<V: AstVisitorMut>(vis: &mut V, expr: &mut ExprStateme
}

#[inline]
fn walk_if_statement_mut<V: AstVisitorMut>(vis: &mut V, ifst: &mut IfStatement) {
fn walk_if_statement_mut<V: AstVisitorMut>(
vis: &mut V,
info: &mut StmtInfo,
ifst: &mut IfStatement,
) {
vis.visit_expression_mut(ifst.condition_mut());
if let Some(ctrl) = ifst.then_controlled_mut() {
vis.visit_statement_mut(ctrl);
Expand Down Expand Up @@ -271,8 +282,12 @@ pub trait AstVisitor<'ast>: Sized {
}

#[inline]
fn visit_variable_expression(&mut self, variable: &'ast VariableExpression) {
walk_variable_expression(self, variable)
fn visit_variable_expression(
&mut self,
info: &'ast ExprInfo,
variable: &'ast VariableExpression,
) {
walk_variable_expression(self, info, variable)
}

#[inline]
Expand Down Expand Up @@ -306,8 +321,8 @@ pub trait AstVisitor<'ast>: Sized {
}

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

#[inline]
Expand All @@ -330,7 +345,12 @@ pub trait AstVisitor<'ast>: Sized {
fn walk_literal<'a, V: AstVisitor<'a>>(_: &mut V, _: &'a LiteralExpression) {}

#[inline]
fn walk_variable_expression<'a, V: AstVisitor<'a>>(_: &mut V, _: &'a VariableExpression) {}
fn walk_variable_expression<'a, V: AstVisitor<'a>>(
_: &mut V,
_: &'a ExprInfo,
_: &'a VariableExpression,
) {
}

#[inline]
fn walk_call_expression<'a, V: AstVisitor<'a>>(_: &mut V, _: &'a CallExpression) {}
Expand All @@ -344,7 +364,7 @@ fn walk_expression<'a, V: AstVisitor<'a>>(vis: &mut V, expr: &'a Expression) {
ExprKind::Assign(ref assign) => vis.visit_assign_expression(assign),
ExprKind::Operator(ref operator) => vis.visit_operator_expression(operator),
ExprKind::Compo(ref compo) => vis.visit_compo_access_expression(compo),
ExprKind::Variable(ref variable) => vis.visit_variable_expression(variable),
ExprKind::Variable(ref variable) => vis.visit_variable_expression(&expr.info, variable),
ExprKind::Literal(ref literal) => vis.visit_literal(literal),
ExprKind::Call(ref call) => vis.visit_call_expression(call),
ExprKind::Range(ref range) => vis.visit_range_expression(range),
Expand All @@ -355,7 +375,7 @@ fn walk_expression<'a, V: AstVisitor<'a>>(vis: &mut V, expr: &'a Expression) {
fn walk_statement<'a, V: AstVisitor<'a>>(vis: &mut V, stmt: &'a Statement) {
match stmt.kind {
StmtKind::Expr(ref expr) => vis.visit_expr_statement(stmt, expr),
StmtKind::If(ref ifst) => vis.visit_if_statement(stmt, ifst),
StmtKind::If(ref ifst) => vis.visit_if_statement(&stmt.info, ifst),
StmtKind::Stmts(ref v) => vis.visit_statement_list(v),
}
}
Expand All @@ -378,7 +398,7 @@ fn walk_expr_statement<'a, V: AstVisitor<'a>>(

fn walk_if_statement<'a, V: AstVisitor<'a>>(
vis: &mut V,
stmt: &'a Statement,
info: &'a StmtInfo,
ifst: &'a IfStatement,
) {
vis.visit_expression(ifst.condition());
Expand Down
18 changes: 11 additions & 7 deletions lib/src/backend/lua/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,19 @@ impl AstVisitorMut for LuaBackend {
// }
}

fn visit_variable_expression_mut(&mut self, var_expr: &mut VariableExpression) {
fn visit_variable_expression_mut(
&mut self,
expr: &mut ExprInfo,
var_expr: &mut VariableExpression,
) {
let scope = self.current_scope();
let var = scope.find_variable(var_expr.name());

trace!(
"LuaGen: variable expression: {}: {:?}",
var_expr,
var.as_ref().and_then(|x| x.ty())
);
// trace!(
// "LuaGen: variable expression: {}: {:?}",
// var_expr,
// var.as_ref().and_then(|x| x.ty())
// );

let access_mode = self.top_attribute().access_mode;
match access_mode {
Expand Down Expand Up @@ -570,7 +574,7 @@ impl AstVisitorMut for LuaBackend {
self.reg_mgr.free(&callee_reg);
}

fn visit_if_statement_mut(&mut self, ifst: &mut IfStatement) {
fn visit_if_statement_mut(&mut self, _: &mut StmtInfo, ifst: &mut IfStatement) {
trace!("LuaGen: if statement: {}", ifst.condition());

let if_exit_label = self.create_label("if-exit");
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod utils;
pub mod prelude {
pub use crate::ast::*;
pub use crate::context::*;
pub use crate::parser::StString;
pub use crate::parser::{LiteralValue, Location, Operator, StString};

pub use uuid::Uuid;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/parser/default_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,9 @@ impl<I: Iterator<Item = LexerResult>> DefaultParserImpl<I> {
let pos = self.next;

match self.next_kind()? {
TokenKind::Identifier(ident) => Ok(Some(Expression::new_variable(ident.clone()))),
TokenKind::Identifier(ident) => {
Ok(Some(Expression::new_variable(ident.clone(), None, None)))
}
_ => {
self.next = pos;
Ok(None)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/parser/lalrpop_impl/st.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub Expr: Expression = {
};

VarExpr: Expression = {
"IDENTIFIER" => Expression::variable(Box::new(VariableExpression::new(<>))),
<start: @L> <ident: "IDENTIFIER"> <end: @R> => Expression::variable(Box::new(VariableExpression::new(ident)), Some(start), Some(end)),
}

AssignExpr: AssignExpression = {
Expand Down
Loading

0 comments on commit e1805ae

Please sign in to comment.