Bash++
Bash++ compiler internal documentation
bpp.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Andrew S. Rightenburg
3 * Bash++: Bash with classes
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7#pragma once
8
9#include <string>
10#include <vector>
11#include <unordered_map>
12#include <memory>
13
14#include <include/EntityMap.h>
15#include <include/BashVersion.h>
16#include <AST/Nodes/Program.h>
17
18namespace bpp {
19
20enum class bpp_scope : uint8_t {
25};
26
27enum class reference_type : uint8_t {
31};
32
39
47struct diagnostic {
49 std::string message;
50 uint32_t start_line;
51 uint32_t start_column;
52 uint32_t end_line;
53 uint32_t end_column;
54};
55
56// Forward declarations
57
58class bpp_entity;
59class bpp_code_entity;
60class bpp_string;
61class bpp_method;
63class bpp_class;
64class bpp_object;
65class bpp_datamember;
66class bpp_program;
67
68// Statement types
71class bash_if;
72class bash_if_branch;
73class bash_case;
76class bash_function;
79class bpp_pointer_dereference;
83class bpp_object_address;
84
89static const char bpp_nullptr[] = "0";
90
95inline constexpr std::array<std::string_view, 18> protected_keywords = {
96 "class", "constructor", "delete", "destructor",
97 "dynamic_cast", "include", "include_once", "local",
98 "method", "new", "nullptr","private",
99 "protected", "public", "super", "this",
100 "typeof", "virtual"
101};
102
107inline bool is_protected_keyword(const std::string& keyword) {
108 return std::ranges::contains(protected_keywords, keyword);
109}
110
116inline bool is_valid_identifier(const std::string& identifier) {
117 // Verify it's not empty, and not a reserved keyword
118 if (identifier.empty() || is_protected_keyword(identifier)) {
119 return false;
120 }
121
122 // Verify it doesn't contain two consecutive underscores
123 if (identifier.contains("__")) return false;
124
125 // Verify it starts with a letter or underscore, and contains only letters, digits, and underscores
126 if (!isalpha(identifier[0]) && identifier[0] != '_') {
127 return false;
128 }
129
130 for (char c : identifier) {
131 if (!isalnum(c) && c != '_') {
132 return false;
133 }
134 }
135
136 // If all checks passed, it's a valid identifier
137 return true;
138}
139
141 std::string file;
142 uint64_t line = 0;
143 uint64_t column = 0;
144
145 SymbolPosition() = default;
146 SymbolPosition(const std::string& file, uint64_t line, uint64_t column)
147 : file(file), line(line), column(column) {}
148};
149
150template <class T>
152 private:
153 std::vector<std::shared_ptr<T>> entities;
154 std::unordered_map<std::string, size_t> name_to_index;
155 public:
156 bool add(std::shared_ptr<T> entity) {
157 const std::string& name = entity->get_name();
158 if (name_to_index.contains(name)) return false; // Entity with this name already exists
159 entities.push_back(entity);
160 name_to_index[name] = entities.size() - 1;
161 return true;
162 }
163
164 std::shared_ptr<T> find(const std::string& name, size_t max_visible_index = SIZE_MAX) {
165 auto it = name_to_index.find(name);
166 if (it == name_to_index.end()) return nullptr; // No entity with this name
167 size_t index = it->second;
168 if (index > max_visible_index) return nullptr; // Entity exists but is out of bounds
169 return entities[index];
170 }
171
172 size_t size() const {
173 return entities.size();
174 }
175
176 const std::vector<std::shared_ptr<T>>& get_entities() const {
177 return entities;
178 }
179};
180
181} // namespace bpp
Definition bpp.h:151
const std::vector< std::shared_ptr< T > > & get_entities() const
Definition bpp.h:176
std::shared_ptr< T > find(const std::string &name, size_t max_visible_index=SIZE_MAX)
Definition bpp.h:164
size_t size() const
Definition bpp.h:172
std::vector< std::shared_ptr< T > > entities
Definition bpp.h:153
bool add(std::shared_ptr< T > entity)
Definition bpp.h:156
std::unordered_map< std::string, size_t > name_to_index
Definition bpp.h:154
A pattern for a case statement in Bash++.
Definition bash_case.h:45
A case statement in Bash++.
Definition bash_case.h:27
A for loop or select statement in Bash++.
Definition bash_for_or_select.h:25
A normal Bash function.
Definition bash_function.h:23
A branch of an if statement in Bash++.
Definition bash_if.h:58
An if statement in Bash++.
Definition bash_if.h:38
The condition for a while/until loop in Bash++.
Definition bash_while_or_until_loop.h:51
A while/until loop in Bash++.
Definition bash_while_or_until_loop.h:32
A class in Bash++.
Definition bpp_class.h:23
An entity which can contain code.
Definition bpp_code_entity.h:35
A data member in a class.
Definition bpp_datamember.h:21
A delete statement in Bash++.
Definition bpp_delete_statement.h:27
A dynamic_cast statement in Bash++.
Definition bpp_dynamic_cast_statement.h:27
The base class for all entities in the Bash++ compiler.
Definition bpp_entity.h:25
A parameter in a method.
Definition bpp_method.h:55
A method in a class.
Definition bpp_method.h:23
An object assignment statement in Bash++.
Definition bpp_object_assignment.h:24
An object reference in Bash++.
Definition bpp_object_reference.h:23
An object in Bash++.
Definition bpp_object.h:22
The main program.
Definition bpp_program.h:28
The practical difference between bpp_code_entity and bpp_string is how we handle the code buffers.
Definition bpp_string.h:77
A value assignment statement in Bash++.
Definition bpp_value_assignment.h:23
Definition bash_case.cpp:9
bpp_scope
Definition bpp.h:20
static const char bpp_nullptr[]
The secret internal value of '@nullptr' in Bash++.
Definition bpp.h:89
bool is_valid_identifier(const std::string &identifier)
Check if a string is a valid identifier in Bash++.
Definition bpp.h:116
bool is_protected_keyword(const std::string &keyword)
Check if a string matches any of our protected keywords.
Definition bpp.h:107
constexpr std::array< std::string_view, 18 > protected_keywords
A list of keywords that are reserved and cannot be used as identifiers in Bash++.
Definition bpp.h:95
reference_type
Definition bpp.h:27
diagnostic_type
Definition bpp.h:33
Definition bpp.h:140
std::string file
Definition bpp.h:141
SymbolPosition()=default
uint64_t column
Definition bpp.h:143
SymbolPosition(const std::string &file, uint64_t line, uint64_t column)
Definition bpp.h:146
uint64_t line
Definition bpp.h:142
Represents a diagnostic message (error, warning, info, hint)
Definition bpp.h:47
uint32_t start_line
Definition bpp.h:50
uint32_t end_line
Definition bpp.h:52
diagnostic_type type
Definition bpp.h:48
std::string message
Definition bpp.h:49
uint32_t end_column
Definition bpp.h:53
uint32_t start_column
Definition bpp.h:51