Skip to content

Commit

Permalink
Add: Struct definition Fix: some bugs on function TODO: Further impro…
Browse files Browse the repository at this point in the history
…vement on struct referencing and obj management
  • Loading branch information
Jaredanwolfgang committed Nov 18, 2024
1 parent 2e63a42 commit 621adbd
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 62 deletions.
32 changes: 28 additions & 4 deletions src/ast/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use std::fmt;
use crate::tree::*;

impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Program::Program(parts) => write!(f, "{}", parts.iter().map(|part| format!("{}", part)).collect::<Vec<String>>().join(", ")),
Program::Error => write!(f, "[ProgramError]"),
}
}
}

impl fmt::Display for ProgramPart {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand All @@ -15,7 +24,8 @@ impl fmt::Display for Statement {
match self {
Statement::Include(s) => write!(f, "Include: {}", s),
Statement::GlobalVariable(v) => write!(f, "Global Variable: {}", v),
Statement::Struct(vars) => write!(f, "Struct: [{}]",
Statement::Struct(name, vars) => write!(f, "Struct {}: [{}]",
name,
vars.iter().map(|var| format!("{}", var)).collect::<Vec<String>>().join(", ")),
}
}
Expand All @@ -28,11 +38,13 @@ impl fmt::Display for Variable {
ident,
values.iter().map(|v| format!("{}", v)).collect::<Vec<String>>().join(", "),
dims.iter().map(|d| d.to_string()).collect::<Vec<String>>().join(", ")),
Variable::MemberReference(ident, member) => write!(f, "{}.{}", ident, member),
Variable::FormalParameter(ident, values, dims) => write!(f, "Formal Parameter: {} = [{}] with dimensions [{}]",
ident,
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::Error => write!(f, "[VariableError]")
}
}
}
Expand All @@ -45,6 +57,7 @@ impl fmt::Display for Function {
input_params.iter().map(|v| format!("{}", v)).collect::<Vec<String>>().join(", ")
),
Function::FuncDeclaration(ident, _input_params, _output_param, body) => write!(f, "Function: {}:[{}]", ident, body),
Function::Error => write!(f, "[FunctionError]"),
}
}
}
Expand All @@ -56,7 +69,8 @@ impl fmt::Display for CompExpr {
CompExpr::Variable(var) => write!(f, "{}", var),
CompExpr::UnaryOperation(op, expr) => write!(f, "({} {})", op, expr),
CompExpr::BinaryOperation(left, op, right) => write!(f, "({} {} {})", left, op, right),
CompExpr::Error => write!(f, "MissingTermError"),
CompExpr::FuncCall(func) => write!(f, "{}", func),
CompExpr::Error => write!(f, "[CompExprError]"),
}
}
}
Expand All @@ -65,6 +79,7 @@ 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]"),
}
}
}
Expand All @@ -75,6 +90,7 @@ impl fmt::Display for CondExpr {
CondExpr::Bool(b) => write!(f, "Condition: {}", b),
CondExpr::UnaryCondition(op, expr) => write!(f, "Condition: {} {}", op, expr),
CondExpr::Condition(left, op, right) => write!(f, "Condition: {} {} {}", left, op, right),
CondExpr::Error => write!(f, "[CondExprError]"),
}
}
}
Expand All @@ -88,6 +104,7 @@ impl fmt::Display for Value {
Value::Char(c) => write!(f, "{}: char", c),
Value::Bool(b) => write!(f, "{}: bool", b),
Value::Null => write!(f, "null"),
Value::Error => write!(f, "[ValueError]"),
}
}
}
Expand All @@ -103,6 +120,7 @@ impl fmt::Display for BinaryOperator {
BinaryOperator::Mod => write!(f, "%"),
BinaryOperator::And => write!(f, "&&"),
BinaryOperator::Or => write!(f, "||"),
BinaryOperator::Error => write!(f, "[BinaryOperatorError]"),
}
}
}
Expand All @@ -114,6 +132,7 @@ impl fmt::Display for UnaryOperator {
UnaryOperator::Not => write!(f, "!"),
UnaryOperator::Inc => write!(f, "++"),
UnaryOperator::Dec => write!(f, "--"),
UnaryOperator::Error => write!(f, "[UnaryOperatorError]"),
}
}
}
Expand All @@ -127,6 +146,7 @@ impl fmt::Display for JudgeOperator {
JudgeOperator::LE => write!(f, "<="),
JudgeOperator::EQ => write!(f, "=="),
JudgeOperator::NE => write!(f, "!="),
JudgeOperator::Error => write!(f, "[JudgeOperatorError]"),
}
}
}
Expand All @@ -137,6 +157,7 @@ impl fmt::Display for If {
If::IfExpr(cond, body) => write!(f, "If: {} then {}", cond, body),
If::IfElseExpr(cond, body, opt_body) =>
write!(f, "If: {} then {} else {}", cond, body, opt_body),
If::Error => write!(f, "[IfError]"),
}
}
}
Expand All @@ -145,7 +166,8 @@ impl fmt::Display for Loop {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Loop::WhileExpr(cond, body) => write!(f, "While Loop ({}): \n do {}", cond, body),
Loop::ForExpr(init, cond, update, body) => write!(f, "For Loop ({}; {}; {}): \n do {}", init, cond, update, body),
Loop::ForExpr(init, cond, update, body) => write!(f, "For Loop ([Initial] {}; [Condition] {}; [Increment] {}): \n do {}", init, cond, update, body),
Loop::Error => write!(f, "[LoopError]"),
}
}
}
Expand All @@ -156,7 +178,8 @@ impl fmt::Display for Body {
Body::Body(expressions) => {
write!(f, "Body: [{}]",
expressions.iter().map(|expr| format!("{}", expr)).collect::<Vec<String>>().join(", "))
}
},
Body::Error => write!(f, "[BodyError]"),
}
}
}
Expand All @@ -172,6 +195,7 @@ impl fmt::Display for Expr {
Expr::Return(val) => write!(f, "Return: {}", val),
Expr::FuncCall(func) => write!(f, "{}", func),
Expr::VarDec(var) => write!(f, "{}", var),
Expr::Error => write!(f, "[ExprError]"),
}
}
}
49 changes: 37 additions & 12 deletions src/ast/src/tree.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#[derive(Clone, Debug, PartialEq)]
pub struct Program(pub Vec<ProgramPart>);
pub enum Program{
Program(Vec<ProgramPart>),
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum ProgramPart {
Statement(Box<Statement>),
Function(Box<Function>)
Function(Box<Function>),
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -13,29 +16,41 @@ pub enum Statement {
// requires special management.
Include(Box<String>),
GlobalVariable(Variable),
Struct(Box<Vec<Variable>>)
Struct(Box<String>, Box<Vec<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)
VarReference(Box<String>),
MemberReference(Box<String>, Box<String>),
VarDeclaration(Box<String>, Box<Vec<Value>>, Box<Vec<usize>>),
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)
FuncReference(Box<String>, Box<Vec<Box<CompExpr>>>),
FuncDeclaration(Box<String>, Box<Vec<Variable>>, Box<Value>, Body),
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum CompExpr {
Value(Value),
Variable(Variable),
FuncCall(Function),
// Mind that UnaryOperator can only operate on
// Integer, Float and Bool. (Remember might need
// to cope with array types)
Expand All @@ -50,13 +65,15 @@ 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),
UnaryCondition(UnaryOperator, Box<CompExpr>),
Condition(Box<CompExpr>, JudgeOperator, Box<CompExpr>)
Condition(Box<CompExpr>, JudgeOperator, Box<CompExpr>),
Error
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -66,7 +83,8 @@ pub enum Value {
String(String),
Char(char),
Bool(bool),
Null
Null,
Error
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -78,14 +96,16 @@ pub enum BinaryOperator {
Pow, // ^
Mod, // %
And, // &&
Or // ||
Or, // ||
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum UnaryOperator {
Not, // !
Inc, // ++
Dec // --
Dec, // --
Error
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -95,24 +115,28 @@ pub enum JudgeOperator {
LT, // <
LE, // <=
EQ, // ==
NE // !=
NE, // !=
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum If {
IfExpr(Box<CondExpr>, Body),
IfElseExpr(Box<CondExpr>, Body, Body)
IfElseExpr(Box<CondExpr>, Body, Body),
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum Loop {
WhileExpr(Box<CondExpr>, Body),
ForExpr(Box<Expr>, Box<CondExpr>, Box<Expr>, Body)
ForExpr(Box<Expr>, Box<CondExpr>, Box<Expr>, Body),
Error
}

#[derive(Clone, Debug, PartialEq)]
pub enum Body {
Body(Vec<Expr>)
Body(Vec<Expr>),
Error
}

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -124,5 +148,6 @@ pub enum Expr{
FuncCall(Function),
Break,
Continue,
Return(CompExpr)
Return(CompExpr),
Error
}
2 changes: 2 additions & 0 deletions src/lexer/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub enum Token {
DeclarationStruct,
#[token("fn")]
DeclarationFunction,
#[token("#include")]
DeclarationInclude,

// Type
#[token("bool")]
Expand Down
Loading

0 comments on commit 621adbd

Please sign in to comment.