summaryrefslogtreecommitdiffhomepage
path: root/include/CompilerKit/AST.h
blob: f45d6fdcca53ce6376b17c415ade72585bde1cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (See accompanying
// file LICENSE or copy at http://www.apache.org/licenses/LICENSE-2.0)
// Official repository: https://github.com/nekernel-org/nectar

#ifndef NECTAR_COMPILERKIT_AST_H
#define NECTAR_COMPILERKIT_AST_H

#include <CompilerKit/CodeGenerator.h>
#include <vector>

#define CK_COMPILER_FRONTEND : public ::CompilerKit::ICompilerFrontend

namespace CompilerKit {
inline static constexpr auto kInvalidFrontend = "(null)";

struct SyntaxLeafList;
struct SyntaxLeafList;
struct SyntaxKeyword;

/// =========================================================== ///
/// @note we want to do that to separate keywords.
/// =========================================================== ///

/// \brief The type of keyword we are dealing with.
enum struct KeywordKind {
  kKeywordKindReserved  = 0,
  kKeywordKindNamespace = 100,
  kKeywordKindFunctionStart,
  kKeywordKindFunctionEnd,
  kKeywordKindVariable,
  kKeywordKindExpressionBegin,
  kKeywordKindExpressionEnd,
  kKeywordKindArgSeparator,
  kKeywordKindBodyStart,
  kKeywordKindBodyEnd,
  kKeywordKindImpl,
  kKeywordKindNew,
  kKeywordKindDelete,
  kKeywordKindAccess,
  kKeywordKindIf,
  kKeywordKindElse,
  kKeywordKindElseIf,
  kKeywordKindVariableAssign,
  kKeywordKindVariableDec,
  kKeywordKindVariableInc,
  kKeywordKindConstant,
  kKeywordKindTypedef,
  kKeywordKindEndLine,  // Optional in Nectar.
  kKeywordKindSpecifier,
  kKeywordKindInvalid,
  kKeywordKindReturn,
  kKeywordKindCommentInline,
  kKeywordKindCommentMultiLineStart,
  kKeywordKindCommentMultiLineEnd,
  kKeywordKindEq,
  kKeywordKindNotEq,
  kKeywordKindGreaterEq,
  kKeywordKindLessEq,
  kKeywordKindPtr,
  kKeywordKindClassStart,
  kKeywordKindClassEnd,
  kKeywordKindMethodStart,
  kKeywordKindMethodEnd,
  kKeywordKindCount = kKeywordKindPtr - kKeywordKindNamespace + 1,
};

/// =========================================================== ///
/// \brief Compiler keyword information struct.
/// =========================================================== ///
struct SyntaxKeyword {
  SyntaxKeyword(const STLString& name, KeywordKind kind) : fKeywordName(name), fKeywordKind(kind) {}
  ~SyntaxKeyword() = default;
  SyntaxKeyword()  = delete;

  NECTAR_COPY_DEFAULT(SyntaxKeyword);

  STLString   fKeywordName{};
  KeywordKind fKeywordKind{KeywordKind::kKeywordKindInvalid};
};

struct SyntaxLeafList final {
  struct SyntaxLeaf;

  struct SyntaxLeaf {
    using Ptr            = SyntaxLeaf*;
    using Reference      = SyntaxLeaf&;
    using ConstReference = const SyntaxLeaf&;

    /// \brief User data type.
    Int32 fUserType{};

    /// \brief User data buffer.
    SyntaxKeyword fUserData{{}, KeywordKind::kKeywordKindInvalid};

    /// \brief User data value
    STLString fUserValue{};

    /// \brief Next user data on list.
    Ptr fNext{nullptr};
  };

  using ArrayType = std::vector<SyntaxLeaf>;

  ArrayType fLeafList;
  SizeType  fNumLeafs{0};

 public:
  const SizeType&  NumLeafs() { return fNumLeafs; }
  const SizeType&  NumLeafs() const { return fNumLeafs; }
  const ArrayType& Get() const { return fLeafList; }
  ArrayType&       Get() { return fLeafList; }
  // \brief You can't get a reference of a leaf in a const context.
  const SyntaxLeaf& At(const SizeType& index) const = delete;
  // \breif Grab leaf from index.
  SyntaxLeaf& At(const SizeType& index) { return fLeafList[index]; }
};

/// =========================================================== ///
/// find the perfect matching word in a haystack.
/// \param haystack base string
/// \param needle the string we search for.
/// \return if we found it or not.
/// =========================================================== ///
bool ast_find_needle(STLString haystack, STLString needle) noexcept;

/// =========================================================== ///
/// find a word within strict conditions and returns a range of it.
/// \param haystack
/// \param needle
/// \return position of needle.
/// =========================================================== ///
SizeType ast_find_needle_range(STLString haystack, STLString needle) noexcept;

/// =========================================================== ///
/// @brief Compiler backend, implements a frontend, such as C, C++...
/// See Toolchain, for some examples.
/// =========================================================== ///
class ICompilerFrontend {
 public:
  explicit ICompilerFrontend() = default;
  virtual ~ICompilerFrontend() = default;

  NECTAR_COPY_DEFAULT(ICompilerFrontend)

  /// =========================================================== ///
  /// NOTE: cast this to your user defined ast.
  /// =========================================================== ///
  using AstType = VoidPtr;

  /// =========================================================== ///
  //! @brief Compile a syntax tree ouf of the text.
  //! Also takes the source file name for metadata.
  /// =========================================================== ///
  virtual SyntaxLeafList::SyntaxLeaf Compile(STLString& text, const STLString& file) { return {}; }

  /// =========================================================== ///
  //! @brief What language are we dealing with?
  /// =========================================================== ///
  virtual const char* Language();

  /// =========================================================== ///
  /// @brief Checks if language is a valid frontend.
  /// =========================================================== ///
  virtual bool IsValid();
};
}  // namespace CompilerKit

#include <CompilerKit/AST.inl>

#endif  // NECTAR_COMPILERKIT_AST_H