blaze lisp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
5.2 KiB

2 years ago
/*
* Copyright (C) 2023 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <cstdint> // int64_t
#include <functional> // std::function
#include <span>
2 years ago
#include <string>
#include <string_view>
#include <typeinfo> // typeid
#include <vector>
#include "ruc/format/formatter.h"
2 years ago
namespace blaze {
class ASTNode {
public:
virtual ~ASTNode() = default;
std::string className() const { return typeid(*this).name(); }
template<typename T>
bool fastIs() const = delete;
virtual bool isCollection() const { return false; }
2 years ago
virtual bool isVector() const { return false; }
virtual bool isHashMap() const { return false; }
virtual bool isList() const { return false; }
virtual bool isString() const { return false; }
virtual bool isKeyword() const { return false; }
2 years ago
virtual bool isNumber() const { return false; }
virtual bool isValue() const { return false; }
2 years ago
virtual bool isSymbol() const { return false; }
virtual bool isFunction() const { return false; }
protected:
ASTNode() {}
2 years ago
};
// -----------------------------------------
class Collection : public ASTNode {
2 years ago
public:
virtual ~Collection() override;
2 years ago
virtual bool isCollection() const override { return true; }
void addNode(ASTNode* node);
const std::vector<ASTNode*>& nodes() const { return m_nodes; }
size_t size() const { return m_nodes.size(); }
bool empty() const { return m_nodes.size() == 0; }
protected:
Collection() {}
2 years ago
private:
std::vector<ASTNode*> m_nodes;
};
// -----------------------------------------
// []
class Vector final : public Collection {
public:
Vector() = default;
virtual ~Vector() = default;
virtual bool isCollection() const override { return false; }
virtual bool isVector() const override { return true; }
};
// -----------------------------------------
2 years ago
// {}
class HashMap final : public Collection {
2 years ago
public:
HashMap() = default;
virtual ~HashMap() = default;
2 years ago
virtual bool isCollection() const override { return false; }
2 years ago
virtual bool isHashMap() const override { return true; }
};
// -----------------------------------------
// ()
class List final : public Collection {
2 years ago
public:
List() = default;
virtual ~List() = default;
2 years ago
virtual bool isCollection() const override { return false; }
2 years ago
virtual bool isList() const override { return true; }
};
// -----------------------------------------
// "string"
class String final : public ASTNode {
public:
String(const std::string& data);
virtual ~String() = default;
virtual bool isString() const override { return true; }
const std::string& data() const { return m_data; }
private:
std::string m_data;
};
// -----------------------------------------
// :keyword
class Keyword final : public ASTNode {
public:
Keyword(const std::string& data);
virtual ~Keyword() = default;
virtual bool isKeyword() const override { return true; }
const std::string& keyword() const { return m_data; }
private:
std::string m_data;
};
// -----------------------------------------
2 years ago
// 123
class Number final : public ASTNode {
public:
Number(int64_t number);
virtual ~Number() = default;
virtual bool isNumber() const override { return true; }
int64_t number() const { return m_number; }
private:
int64_t m_number { 0 };
};
// -----------------------------------------
// Symbols
class Symbol final : public ASTNode {
2 years ago
public:
Symbol(const std::string& symbol);
virtual ~Symbol() = default;
2 years ago
virtual bool isSymbol() const override { return true; }
const std::string& symbol() const { return m_symbol; }
2 years ago
private:
std::string m_symbol;
};
// -----------------------------------------
// true, false, nil
class Value final : public ASTNode {
2 years ago
public:
Value(const std::string& value);
virtual ~Value() = default;
2 years ago
virtual bool isValue() const override { return true; }
2 years ago
const std::string& value() const { return m_value; }
2 years ago
private:
std::string m_value;
};
// -----------------------------------------
using Lambda = std::function<ASTNode*(std::span<ASTNode*>)>;
class Function final : public ASTNode {
public:
Function(Lambda lambda);
virtual ~Function() = default;
virtual bool isFunction() const override { return true; }
Lambda lambda() const { return m_lambda; }
private:
Lambda m_lambda;
2 years ago
};
// -----------------------------------------
// clang-format off
template<>
inline bool ASTNode::fastIs<Collection>() const { return isCollection(); }
2 years ago
template<>
inline bool ASTNode::fastIs<Vector>() const { return isVector(); }
template<>
inline bool ASTNode::fastIs<HashMap>() const { return isHashMap(); }
template<>
inline bool ASTNode::fastIs<List>() const { return isList(); }
template<>
inline bool ASTNode::fastIs<String>() const { return isString(); }
template<>
inline bool ASTNode::fastIs<Keyword>() const { return isKeyword(); }
2 years ago
template<>
inline bool ASTNode::fastIs<Number>() const { return isNumber(); }
2 years ago
template<>
inline bool ASTNode::fastIs<Symbol>() const { return isSymbol(); }
template<>
inline bool ASTNode::fastIs<Value>() const { return isValue(); }
template<>
inline bool ASTNode::fastIs<Function>() const { return isFunction(); }
2 years ago
// clang-format on
} // namespace blaze
// -----------------------------------------
template<>
struct ruc::format::Formatter<blaze::ASTNode*> : public Formatter<std::string> {
void format(Builder& builder, blaze::ASTNode* value) const;
};