feat(parser,ir,backend): Support array
This commit is contained in:
+25
-3
@@ -2,7 +2,7 @@ use petgraph::dot::{Config, Dot};
|
||||
use petgraph::graph::{Graph, NodeIndex};
|
||||
|
||||
use crate::ast::types::{
|
||||
BlockStmt, BreakStmt, CompileUnit, ContinueStmt, Expr, ExprValue, FuncDeclStmt, GlobalDeclStmt, IfStmt, Param, ReturnStmt, Statement, VarDeclStmt, VarDeclStmtValue, WhileStmt
|
||||
ArrayDimension, BlockStmt, BreakStmt, CompileUnit, ContinueStmt, Expr, ExprValue, FuncDeclStmt, GlobalDeclStmt, IfStmt, Param, ReturnStmt, Statement, VarDeclStmt, VarDeclStmtValue, WhileStmt
|
||||
};
|
||||
|
||||
pub type AstGraph = Graph<String, String>;
|
||||
@@ -73,7 +73,19 @@ impl AstGraphBuilder {
|
||||
}
|
||||
|
||||
fn add_var_decl_value(&mut self, parent: NodeIndex, value: &VarDeclStmtValue) -> NodeIndex {
|
||||
self.child(parent, value.to_string())
|
||||
let node = self.child(parent, value.to_string());
|
||||
for dimension in &value.dimensions {
|
||||
self.add_array_dimension(node, dimension);
|
||||
}
|
||||
node
|
||||
}
|
||||
|
||||
fn add_array_dimension(&mut self, parent: NodeIndex, dimension: &ArrayDimension) -> NodeIndex {
|
||||
let node = self.child(parent, dimension.to_string());
|
||||
if let Some(value) = &dimension.value {
|
||||
self.add_expr(node, value);
|
||||
}
|
||||
node
|
||||
}
|
||||
|
||||
fn add_func_decl(&mut self, parent: NodeIndex, func_decl: &FuncDeclStmt) -> NodeIndex {
|
||||
@@ -87,7 +99,11 @@ impl AstGraphBuilder {
|
||||
}
|
||||
|
||||
fn add_param(&mut self, parent: NodeIndex, param: &Param) -> NodeIndex {
|
||||
self.child(parent, param.to_string())
|
||||
let node = self.child(parent, param.to_string());
|
||||
for dimension in ¶m.dimensions {
|
||||
self.add_array_dimension(node, dimension);
|
||||
}
|
||||
node
|
||||
}
|
||||
|
||||
fn add_block_stmt(&mut self, parent: NodeIndex, block_stmt: &BlockStmt) -> NodeIndex {
|
||||
@@ -153,6 +169,12 @@ impl AstGraphBuilder {
|
||||
fn add_expr(&mut self, parent: NodeIndex, expr: &Expr) -> NodeIndex {
|
||||
match &expr.value {
|
||||
ExprValue::IntLit(_) | ExprValue::Var(_) => self.child(parent, expr.value.to_string()),
|
||||
ExprValue::ArrayAccess { array, index } => {
|
||||
let node = self.child(parent, expr.value.to_string());
|
||||
self.add_expr(node, array);
|
||||
self.add_expr(node, index);
|
||||
node
|
||||
}
|
||||
ExprValue::BinaryOp { lhs, op: _, rhs } => {
|
||||
let node = self.child(parent, expr.value.to_string());
|
||||
self.add_expr(node, lhs);
|
||||
|
||||
@@ -17,7 +17,12 @@ pub struct VarDeclStmt {
|
||||
pub struct VarDeclStmtValue {
|
||||
pub name: String,
|
||||
pub name_span: Span,
|
||||
pub dimensions: Vec<ArrayDimension>,
|
||||
}
|
||||
|
||||
pub struct ArrayDimension {
|
||||
pub value: Option<Expr>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
pub struct FuncDeclStmt {
|
||||
@@ -89,6 +94,10 @@ pub struct Expr {
|
||||
pub enum ExprValue {
|
||||
IntLit(i64),
|
||||
Var(String),
|
||||
ArrayAccess {
|
||||
array: Box<Expr>,
|
||||
index: Box<Expr>,
|
||||
},
|
||||
BinaryOp {
|
||||
lhs: Box<Expr>,
|
||||
op: BinaryOp,
|
||||
@@ -159,6 +168,7 @@ impl From<TypeIdent> for Type {
|
||||
pub struct Param {
|
||||
pub name: String,
|
||||
pub param_type: Type,
|
||||
pub dimensions: Vec<ArrayDimension>,
|
||||
pub name_span: Span,
|
||||
pub type_span: Span,
|
||||
}
|
||||
@@ -190,6 +200,12 @@ impl fmt::Display for VarDeclStmtValue {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ArrayDimension {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "ArrayDimension")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FuncDeclStmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} {}", self.return_type, self.name)
|
||||
@@ -256,6 +272,7 @@ impl fmt::Display for ExprValue {
|
||||
match self {
|
||||
ExprValue::IntLit(value) => write!(f, "IntLit({})", value),
|
||||
ExprValue::Var(name) => write!(f, "Var({})", name),
|
||||
ExprValue::ArrayAccess { .. } => write!(f, "ArrayAccess"),
|
||||
ExprValue::BinaryOp { op, .. } => write!(f, "BinaryOp({})", op),
|
||||
ExprValue::FuncCall(name, _) => write!(f, "FuncCall({})", name),
|
||||
ExprValue::Assign { .. } => write!(f, "Assign"),
|
||||
|
||||
Reference in New Issue
Block a user