mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-09 00:18:46 +00:00
expression nodes finished
This commit is contained in:
parent
100779d2d4
commit
4276a966a7
10 changed files with 842 additions and 137 deletions
81
include/basic_nodes.hpp
Normal file
81
include/basic_nodes.hpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
namespace nodes {
|
||||
|
||||
class Node {
|
||||
public:
|
||||
Node(std::pair<size_t, size_t> start_position,
|
||||
std::pair<size_t, size_t> end_position)
|
||||
: start_position_(start_position), end_position_(end_position) {}
|
||||
|
||||
std::pair<size_t, size_t> get_start_position() { return start_position_; }
|
||||
|
||||
std::pair<size_t, size_t> get_end_position() { return end_position_; }
|
||||
|
||||
protected:
|
||||
std::pair<size_t, size_t> start_position_;
|
||||
std::pair<size_t, size_t> end_position_;
|
||||
};
|
||||
|
||||
struct unit {};
|
||||
struct null {};
|
||||
|
||||
class Literal : public Node {
|
||||
public:
|
||||
template <typename T>
|
||||
Literal(Node node, T &&value) : Node(node), value_(std::forward<T>(value)) {}
|
||||
|
||||
template <typename T> std::optional<T *> get() {
|
||||
if (std::holds_alternative<T>(value_)) {
|
||||
return &std::get<T>(value_);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
template <typename T> std::optional<const T *> get() const {
|
||||
if (std::holds_alternative<T>(value_)) {
|
||||
return &std::get<T>(value_);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
std::variant<double, long long, std::string, char, bool, unit, null> value_;
|
||||
};
|
||||
|
||||
class Identifier : public Node {
|
||||
public:
|
||||
enum IdentifierType {
|
||||
SIMPLE_NAME,
|
||||
SIMPLE_TYPE,
|
||||
TYPECLASS,
|
||||
ARGUMENT_NAME,
|
||||
ARGUMENT_TYPE,
|
||||
// ANNOTATION, used as std::string
|
||||
OPERATOR,
|
||||
PLACEHOLDER,
|
||||
};
|
||||
|
||||
Identifier(Node node, IdentifierType type, std::string &&value)
|
||||
: Node(node), type_(type), value_(std::move(value)) {}
|
||||
|
||||
Identifier(Node node, IdentifierType type, const std::string &value)
|
||||
: Node(node), type_(type), value_(value) {}
|
||||
|
||||
IdentifierType get_type() { return type_; }
|
||||
|
||||
std::string *get() { return &value_; }
|
||||
|
||||
const std::string *get() const { return &value_; }
|
||||
|
||||
private:
|
||||
IdentifierType type_;
|
||||
std::string value_;
|
||||
};
|
||||
|
||||
} // namespace nodes
|
||||
Loading…
Add table
Add a link
Reference in a new issue