172 lines
3.2 KiB
C++
172 lines
3.2 KiB
C++
#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 |