inital commit
This commit is contained in:
1 parent
e9773c9161
commit
e8b8565773
36 files changed
+40468
No files matched your search
@@ -0,0 +1,172 @@
|
||||
#ifndef DLANG_EXPRESSIONS_HPP
|
||||
#define DLANG_EXPRESSIONS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "../types/include.hpp"
|
||||
|
||||
struct Expression {
|
||||
virtual ~Expression() = default;
|
||||
virtual void Print(int indent = 0) const = 0;
|
||||
};
|
||||
|
||||
struct NumberExpression : Expression {
|
||||
double value;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "Number: " << value << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
struct BooleanExpression : Expression {
|
||||
bool value;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "Boolean: " << (value ? "true" : "false") << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
struct StringExpression : Expression {
|
||||
std::string value;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "string: " << value << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
struct IdentifierExpression : Expression {
|
||||
std::string name;
|
||||
std::string keyName;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "Identifier: " << name << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
struct BinaryExpression : Expression {
|
||||
std::unique_ptr<Expression> left;
|
||||
std::string op;
|
||||
std::unique_ptr<Expression> right;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "Binary: " << op << '\n';
|
||||
|
||||
left->Print(indent + 2);
|
||||
right->Print(indent + 2);
|
||||
}
|
||||
};
|
||||
|
||||
struct CallExpression : Expression
|
||||
{
|
||||
std::unique_ptr<Expression> callee;
|
||||
std::vector< std::unique_ptr<Expression>> expressions;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ') << " Call" << "\n";
|
||||
callee->Print(indent + 2);
|
||||
for (const auto& expr : expressions)
|
||||
expr->Print(indent + 2);
|
||||
}
|
||||
};
|
||||
|
||||
struct TableLiteralExpression : Expression
|
||||
{
|
||||
std::vector<std::pair<std::string, std::unique_ptr<Expression>>> fields;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "TableLiteral {\n";
|
||||
|
||||
for (const auto& [key, expr] : fields)
|
||||
{
|
||||
std::cout << std::string(indent + 2, ' ')
|
||||
<< key << ":\n";
|
||||
if (expr)
|
||||
{
|
||||
expr->Print(indent + 4);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "}\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct ListLiteralExpression : Expression
|
||||
{
|
||||
std::vector<std::unique_ptr<Expression>> fields;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "TableLiteral {\n";
|
||||
|
||||
for (const auto& val : fields)
|
||||
val->Print(indent + 2);
|
||||
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "}\n";
|
||||
}
|
||||
};
|
||||
|
||||
struct MemberAccessExpression : Expression
|
||||
{
|
||||
std::unique_ptr<Expression> object;
|
||||
std::string memberName;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "MemberAccess: ." << memberName << '\n';
|
||||
|
||||
if (object)
|
||||
{
|
||||
object->Print(indent + 2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct IndexAccessExpression : Expression
|
||||
{
|
||||
std::unique_ptr<Expression> object;
|
||||
std::unique_ptr<Expression> memberIndex;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "IndexAccess: ." << '\n';
|
||||
|
||||
memberIndex->Print(indent);
|
||||
|
||||
if (object)
|
||||
{
|
||||
object->Print(indent + 2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct SizeExpression : Expression
|
||||
{
|
||||
std::string identifier;
|
||||
|
||||
void Print(int indent = 0) const override
|
||||
{
|
||||
std::cout << std::string(indent, ' ')
|
||||
<< "Identifier: " << identifier << '\n';
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,209 @@
|
||||
#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
|
||||
Reference in new issue
Block a user