Skip to content

Commit

Permalink
Fix: Redesign variable referencing methods, New: Add struct parser. T…
Browse files Browse the repository at this point in the history
…odo: struct identifier
  • Loading branch information
Jaredanwolfgang committed Nov 18, 2024
1 parent 01cde0c commit a71b2c9
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 215 deletions.
29 changes: 15 additions & 14 deletions src/ast/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ impl fmt::Display for Statement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Statement::Include(s) => write!(f, "Include: {}", s),
Statement::GlobalVariable(v) => write!(f, "Global Variable: {}", v),
Statement::Struct(name, vars) => write!(f, "Struct {}: [{}]",
name,
Statement::GlobalVariable(vars) => write!(f, "GlobalVariable: [{}]",
vars.iter().map(|var| format!("{}", var)).collect::<Vec<String>>().join(", ")),
Statement::Struct(structure) => write!(f, "Struct: {}", structure),
}
}
}
Expand All @@ -44,6 +43,17 @@ impl fmt::Display for Variable {
values.iter().map(|v| format!("{}", v)).collect::<Vec<String>>().join(", "),
dims.iter().map(|d| d.to_string()).collect::<Vec<String>>().join(", ")),
Variable::VarReference(ident) => write!(f, "{}", ident),
Variable::VarAssignment(ident, expr) => write!(f, "Variable Assignment: {} = {}", ident, expr),
Variable::StructReference(ident) => write!(f, "Struct Reference: {}", ident),
Variable::StructDefinition(ident, vars) => write!(f, "Struct Definition: {} with [{}]",
ident,
vars.iter().map(|v| format!("{}", v)).collect::<Vec<String>>().join(", ")),
Variable::StructDeclaration(ident, parent, vars) => write!(f, "Struct Declaration: {} extends {} with [{}]",
ident,
parent,
vars.iter().map(|v| format!("{}", v)).collect::<Vec<String>>().join(", ")),
Variable::StructAssignment(ident, field, expr) => write!(f, "Struct Assignment: {}.{} = {}", ident, field, expr),

Variable::Error => write!(f, "[VariableError]")
}
}
Expand Down Expand Up @@ -75,15 +85,6 @@ impl fmt::Display for CompExpr {
}
}

impl fmt::Display for AssignExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AssignExpr::AssignOperation(var, expr) => write!(f, "Assignment: {} = {}", var, expr),
AssignExpr::Error => write!(f, "[AssignExprError]"),
}
}
}

impl fmt::Display for CondExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -189,12 +190,12 @@ impl fmt::Display for Expr {
match self {
Expr::If(if_expr) => write!(f, "{}", if_expr),
Expr::Loop(loop_expr) => write!(f, "{}", loop_expr),
Expr::Assign(assign_expr) => write!(f, "{}", assign_expr),
Expr::Break => write!(f, "Break"),
Expr::Continue => write!(f, "Continue"),
Expr::Return(val) => write!(f, "Return: {}", val),
Expr::FuncCall(func) => write!(f, "{}", func),
Expr::VarDec(var) => write!(f, "{}", var),
Expr::VarManagement(vars) => write!(f, "{}",
vars.iter().map(|var| format!("{}", var)).collect::<Vec<String>>().join("; ")),
Expr::Error => write!(f, "[ExprError]"),
}
}
Expand Down
37 changes: 16 additions & 21 deletions src/ast/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,33 @@ pub enum Statement {
// Statement includes Macro, however, macro
// requires special management.
Include(Box<String>),
GlobalVariable(Variable),
Struct(Box<String>, Box<Vec<Variable>>),
GlobalVariable(Vec<Variable>),
Struct(Variable),
}

#[derive(Clone, Debug, PartialEq)]
pub enum Variable {
// The last one is for the dimension list, if null than a value only.
// (identifier, values, dimensions)
// Variable can be used to declare a variable or reference a variable.
// Variable can be a single value or an array.
VarReference(Box<String>),
MemberReference(Box<String>, Box<String>),
VarDeclaration(Box<String>, Box<Vec<Value>>, Box<Vec<usize>>),
VarAssignment(Box<String>, Box<CompExpr>),

// Struct is defined to realize object.
StructReference(Box<String>),
StructDefinition(Box<String>, Box<Vec<Variable>>),
// Object type, Identifier, Variables
StructDeclaration(Box<String>, Box<String>, Box<Vec<Variable>>),
// Identifier, Field, Variable
StructAssignment(Box<String>, Box<String>, Box<CompExpr>),

MemberReference(Box<String>, Box<String>),
FormalParameter(Box<String>, Box<Vec<Value>>, Box<Vec<usize>>),
Error
}

// #[derive(Clone, Debug, PartialEq)]
// pub enum Object {
// StructReference(Box<String>, Box<Vec<Variable>>, Box<Vec<usize>>),
// StructDeclaration(Box<String>, Box<Vec<Variable>>),
// FormalParameter(Box<String>, Box<Vec<Variable>>, Box<Vec<usize>>),
// Error
// }

#[derive(Clone, Debug, PartialEq)]
pub enum Function {
// (identifier, input_params, output_params, body)
Expand All @@ -60,14 +64,6 @@ pub enum CompExpr {
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum AssignExpr {
// When assigning must check whether the variable already
// exists
AssignOperation(Box<Variable>, Box<CompExpr>),
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum CondExpr {
Bool(bool),
Expand Down Expand Up @@ -143,8 +139,7 @@ pub enum Body {
pub enum Expr{
If(If),
Loop(Loop),
VarDec(Variable),
Assign(AssignExpr),
VarManagement(Vec<Variable>),
FuncCall(Function),
Break,
Continue,
Expand Down
1 change: 1 addition & 0 deletions src/lexer/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use logos::{Logos, FilterResult};
use core::error;
use std::fmt;
use std::num::ParseIntError;

Expand Down
Loading

0 comments on commit a71b2c9

Please sign in to comment.