Files
dlangvm/DlangVM/dlang/parser/statements.hpp
T
2026-10-12 23:34:28 +02:00

209 lines
5.3 KiB
C++

#ifndef DLANG_STATEMENTS_HPP
#define DLANG_STATEMENTS_HPP
#include <string>
#include <iostream>
#include "expressions.hpp"
struct Statement
{
virtual ~Statement() = default;
virtual void Print(int indent = 0) const = 0;
};
struct VariableDeclaration : Statement
{
std::string name;
std::unique_ptr<Expression> initializer;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ')
<< "VariableDeclaration: " << name << '\n';
initializer->Print(indent + 2);
}
};
struct Assignment : Statement
{
std::unique_ptr<Expression> lhs;
std::unique_ptr<Expression> value;
std::string assignmentOperator;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ')
<< "Assignment" << '\n';
lhs->Print(indent + 2);
value->Print(indent + 2);
}
};
struct ExpressionStatement : Statement {
std::unique_ptr<Expression> expression;
void Print(int indent = 0) const override {
expression->Print(indent);
}
};
struct WhileStatement : Statement
{
std::unique_ptr<Expression> condition;
std::vector<std::unique_ptr<Statement>> body;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "WhileStatement\n";
std::cout << std::string(indent + 2, ' ') << "Condition:\n";
condition->Print(indent + 4);
std::cout << std::string(indent + 2, ' ') << "Body:\n";
for (const auto& stmt : body)
{
stmt->Print(indent + 4);
}
}
};
struct ForStatement : Statement
{
std::string var;
double from;
double to;
double step = 1;
std::vector<std::unique_ptr<Statement>> body;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "ForStatement\n";
std::cout << std::string(indent + 2, ' ') << "var: " << var << "\n";
std::cout << std::string(indent + 2, ' ') << "from: " << from << "\n";
std::cout << std::string(indent + 2, ' ') << "to: " << to << "\n";
std::cout << std::string(indent + 2, ' ') << "step: " << step << "\n";
for (const auto& stmt : body)
{
stmt->Print(indent + 4);
}
}
};
struct ForeachStatement : Statement
{
std::string var;
std::string list;
std::vector<std::unique_ptr<Statement>> body;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "ForeachStatement\n";
std::cout << std::string(indent + 2, ' ') << "var: " << var << "\n";
std::cout << std::string(indent + 2, ' ') << "list: " << list << "\n";
for (const auto& stmt : body)
{
stmt->Print(indent + 4);
}
}
};
struct ForeachTableStatement : Statement
{
std::string varKey;
std::string varValue;
std::string table;
std::vector<std::unique_ptr<Statement>> body;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "ForeachTableStatement\n";
std::cout << std::string(indent + 2, ' ') << "var key: " << varKey << "\n";
std::cout << std::string(indent + 2, ' ') << "var value: " << varValue << "\n";
std::cout << std::string(indent + 2, ' ') << "table: " << table << "\n";
for (const auto& stmt : body)
{
stmt->Print(indent + 4);
}
}
};
struct IfStatement : Statement
{
std::unique_ptr<Expression> expression;
std::vector<std::unique_ptr<Statement>> body;
std::vector<std::unique_ptr<Statement>> elseBody;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "IfStatement\n";
std::cout << std::string(indent + 2, ' ') << "expression:\n";
expression->Print(indent + 4);
std::cout << std::string(indent + 2, ' ') << "Body:\n";
for (const auto& stmt : body)
stmt->Print(indent + 4);
std::cout << std::string(indent + 2, ' ') << "Else Body:\n";
for (const auto& stmt : elseBody)
stmt->Print(indent + 4);
}
};
struct FunctionStatement : Statement
{
std::string name;
std::vector<std::string> parameters;
std::vector<std::unique_ptr<Statement>> body;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "FunctionStatement\n";
std::cout << std::string(indent + 2, ' ') << "name: " << name << "\n";
std::cout << std::string(indent + 2, ' ') << "Paramters:\n";
for (const auto& param : parameters)
std::cout << std::string(indent + 4, ' ') << param << "\n";
std::cout << std::string(indent + 2, ' ') << "Body:\n";
for (const auto& stmt : body)
stmt->Print(indent + 4);
}
};
struct FunctionExpression : Expression
{
std::vector<std::string> parameters;
std::vector<std::unique_ptr<Statement>> body;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "FunctionExpression\n";
std::cout << std::string(indent + 2, ' ') << "Paramters:\n";
for (const auto& param : parameters)
std::cout << std::string(indent + 4, ' ') << param << "\n";
std::cout << std::string(indent + 2, ' ') << "Body:\n";
for (const auto& stmt : body)
stmt->Print(indent + 4);
}
};
struct ReturnStatement : Statement
{
std::unique_ptr<Expression> expression;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "ReturnStatement\n";
std::cout << std::string(indent + 2, ' ') << "Expression:\n";
if(expression) expression->Print(indent + 4);
}
};
struct IncludeStatement : Statement
{
std::string path;
void Print(int indent = 0) const override
{
std::cout << std::string(indent, ' ') << "IncludeStatement\n";
std::cout << std::string(indent + 2, ' ') << "Path: " << path << "\n";
}
};
#endif