Skip to content

Commit a37e46b

Browse files
committed
feat: AST loading options via cxxopts to parse arguments
1 parent d2509bf commit a37e46b

File tree

8 files changed

+150
-366
lines changed

8 files changed

+150
-366
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/build*
22
/.vscode
33
/cpm_modules
4-
.DS_Store
4+
.DS_Store
5+
ast.json
6+
.ast_cache
7+
.roxas_cache

CMakeLists.txt

+11-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ CPMAddPackage(
6363
VERSION 3.11.3
6464
GITHUB_REPOSITORY simdjson/simdjson
6565
OPTIONS
66-
"SIMDJSON_BUILD_STATIC_LIB OFF"
66+
# "SIMDJSON_BUILD_STATIC_LIB ON"
67+
# "BUILD_SHARED_LIBS ON"
68+
)
69+
70+
CPMAddPackage(
71+
NAME cxxopts
72+
VERSION 3.2.0
73+
GITHUB_REPOSITORY jarro2783/cxxopts
6774
)
6875

6976
# Create standalone executable
@@ -80,5 +87,6 @@ target_include_directories(
8087
$<INSTALL_INTERFACE:${PROJECT_NAME}-${PROJECT_VERSION}>
8188
)
8289

83-
target_include_directories(${PROJECT_NAME} PUBLIC simdjson::simdjson Python3::Python)
84-
target_link_libraries(${PROJECT_NAME} PUBLIC simdjson::simdjson Python3::Python)
90+
target_include_directories(${PROJECT_NAME} PUBLIC ${PYTHON_INCLUDE_DIRS})
91+
92+
target_link_libraries(${PROJECT_NAME} PUBLIC simdjson::simdjson Python3::Python cxxopts::cxxopts)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ git submodule update --init --recursive
2626
#### Windows (mingw/msys)
2727

2828
```bash
29-
pacman -S git wget mingw-w64-x86_64-clang mingw-w64-x86_64-gcc mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake make mingw-w64-x86_64-python3 autoconf libtool
29+
pacman -S git wget mingw-w64-x86_64-clang mingw-w64-x86_64-gcc mingw-w64-x86_64-ninja mingw-w64-x86_64-cmake make mingw-w64-x86_64-python3 autoconf libtool mingw-w64-x86_64-unicode-character-database
3030
mkdir build
3131
cd build
3232
cmake .. -DCMAKE_BUILD_TYPE=Debug

roxas/ast.cc

+2-66
Original file line numberDiff line numberDiff line change
@@ -19,72 +19,8 @@
1919

2020
namespace roxas {
2121

22-
/**
23-
* @brief Construct a new Abstract_Syntax_Tree object
24-
*
25-
* @param parse_tree A parse tree from the ParseTreeModuleLoader
26-
*/
27-
Abstract_Syntax_Tree::Abstract_Syntax_Tree(std::string_view parse_tree)
28-
: parse_tree_(parse_tree)
29-
{
30-
}
31-
32-
/**
33-
* @brief Get the AST
34-
*
35-
* @return ast_type the AST data structure
36-
*/
37-
38-
const Abstract_Syntax_Tree::ast_type&
39-
Abstract_Syntax_Tree::get_ast_definitions() const
40-
{
41-
return ast_;
42-
}
43-
44-
/**
45-
* @brief The rvalue node print function
46-
*
47-
*/
48-
void ast::rvalue_node::print() const
49-
{
50-
std::cout << "rvalue: " << std::endl;
51-
std::cout << "\ttype: " << type_ << std::endl;
52-
std::cout << "\tvalue: " << rvalue_ << std::endl;
53-
}
22+
namespace json {
5423

55-
/**
56-
* @brief The lvalue node print function
57-
*
58-
*/
59-
void ast::lvalue_node::print() const
60-
{
61-
std::cout << "lvalue: " << std::endl;
62-
std::cout << "\tvalue: " << identifier_ << std::endl;
63-
}
64-
65-
/**
66-
* @brief The expression node print function
67-
*
68-
*/
69-
void ast::expression_node::print() const
70-
{
71-
for (auto const& item : expr_) {
72-
std::visit(
73-
ast::overload{
74-
[](std::monostate) {},
75-
[&](ast::lvalue_node const&) { std::get<1>(item).print(); },
76-
[&](ast::rvalue_node const&) { std::get<2>(item).print(); } },
77-
item);
78-
}
79-
}
80-
81-
/**
82-
* @brief The statement node print function
83-
*
84-
*/
85-
void ast::statement_node::print() const
86-
{
87-
// @TODO
88-
}
24+
} // namespace json
8925

9026
} // namespace roxas

roxas/ast.h

+3-266
Original file line numberDiff line numberDiff line change
@@ -16,274 +16,11 @@
1616

1717
#pragma once
1818

19-
#include <memory>
20-
#include <optional>
21-
#include <string>
22-
#include <tuple>
23-
#include <variant>
24-
#include <vector>
19+
#include <simdjson.h>
2520

2621
namespace roxas {
2722

28-
// Link to the grammar can be found here:
29-
// https://github.com/jahan-addison/xion/blob/master/xion/grammar.lark
23+
namespace json {
3024

31-
namespace ast {
32-
33-
struct definition;
34-
class node;
35-
class expression_node;
36-
class statement_node;
37-
class lvalue_node;
38-
class rvalue_node;
39-
40-
enum class literal : int
41-
{
42-
Number,
43-
Constant,
44-
String,
45-
Unknown
46-
};
47-
48-
using literal_node = std::tuple<literal, std::string>;
49-
50-
} // namespace ast
51-
52-
/**
53-
* @brief
54-
*
55-
* The AST object that holds a representation of a parse tree from
56-
* the ParseTreeModuleLoader object suitable to construct a B program.
57-
*
58-
* Constructs child nodes with recursive descent on ast data types,
59-
* prepares data for future compiler passes.
60-
*
61-
*/
62-
class Abstract_Syntax_Tree
63-
{
64-
public:
65-
Abstract_Syntax_Tree(Abstract_Syntax_Tree const&) = delete;
66-
Abstract_Syntax_Tree& operator=(Abstract_Syntax_Tree const&) = delete;
67-
68-
using ast_type = std::vector<ast::definition>;
69-
70-
public:
71-
/**
72-
* @brief Construct a new Abstract_Syntax_Tree object
73-
*
74-
* @param parse_tree A parse tree from the ParseTreeModuleLoader
75-
*/
76-
explicit Abstract_Syntax_Tree(std::string_view parse_tree);
77-
~Abstract_Syntax_Tree() = default;
78-
79-
public:
80-
/**
81-
* @brief Get the AST
82-
*
83-
* @return ast_type the AST data structure
84-
*/
85-
const ast_type& get_ast_definitions() const;
86-
87-
private:
88-
ast::definition construct_definition_ast_();
89-
ast::statement_node construct_statement_node_();
90-
ast::expression_node construct_expression_node_();
91-
92-
private:
93-
ast::lvalue_node construct_lvalue_node_();
94-
ast::rvalue_node construct_rvalue_node_();
95-
96-
private:
97-
ast::literal_node construct_constant_ast_();
98-
99-
private:
100-
std::string_view parse_tree_;
101-
ast_type ast_;
102-
};
103-
104-
namespace ast {
105-
106-
// The overload pattern
107-
template<class... Ts>
108-
struct overload : Ts...
109-
{
110-
using Ts::operator()...;
111-
};
112-
template<class... Ts>
113-
overload(Ts...) -> overload<Ts...>;
114-
115-
/**
116-
* @brief A representation of a Definition (vector or function) in the B
117-
* language
118-
*
119-
*/
120-
struct definition
121-
{
122-
using ptr = std::unique_ptr<node>;
123-
enum class type : int
124-
{
125-
Function,
126-
Vector,
127-
Unknown
128-
};
129-
130-
type type{ type::Unknown };
131-
std::vector<ptr> children{};
132-
};
133-
134-
/**
135-
* @brief
136-
*
137-
* The AST node abstract class
138-
*
139-
*/
140-
class node
141-
{
142-
public:
143-
node(node const&) = delete;
144-
node& operator=(node const&) = delete;
145-
virtual ~node() = default;
146-
147-
protected:
148-
node() = default;
149-
150-
public:
151-
virtual void print() const = 0;
152-
};
153-
154-
/**
155-
* @brief The lvalue node
156-
*
157-
* Provides a string to its identifer
158-
*
159-
*/
160-
class lvalue_node final : public node
161-
{
162-
public:
163-
/**
164-
* @brief Construct a new lvalue node
165-
*
166-
* @param type lvalue type, per the grammar
167-
*/
168-
explicit lvalue_node(std::string type)
169-
: identifier_(std::move(type))
170-
{
171-
}
172-
/**
173-
* @brief The lvalue node print function
174-
*
175-
*/
176-
void print() const override;
177-
178-
private:
179-
std::string identifier_;
180-
};
181-
182-
/**
183-
* @brief The rvalue node
184-
*
185-
* Holds a pointer to its data expression
186-
*
187-
*/
188-
class rvalue_node final : public node
189-
{
190-
public:
191-
/**
192-
* @brief Construct a new rvalue node
193-
*
194-
* @param type rvalue type, per te grammar
195-
* @param node the rvalue expression
196-
*/
197-
explicit rvalue_node(std::string type, std::string rvalue)
198-
: type_(std::move(type))
199-
, rvalue_(std::move(rvalue))
200-
201-
{
202-
}
203-
204-
/**
205-
* @brief The rvalue node print function
206-
*
207-
*/
208-
void print() const override;
209-
210-
private:
211-
std::string type_;
212-
std::string rvalue_;
213-
};
214-
215-
/**
216-
* @brief The expression node
217-
*
218-
* An expression can be one or many of an lvalue, or rvalue
219-
*
220-
*/
221-
class expression_node final : public node
222-
{
223-
public:
224-
using datatype =
225-
std::vector<std::variant<std::monostate, lvalue_node, rvalue_node>>;
226-
/**
227-
* @brief Construct a new expression node
228-
*
229-
* @param type expression type, per the grammar
230-
* @param expr the lvalue or rvalue of the expression
231-
*/
232-
explicit expression_node(std::string type, datatype expr)
233-
: type_(std::move(type))
234-
, expr_(std::move(expr))
235-
{
236-
}
237-
238-
/**
239-
* @brief The expression node print function
240-
*
241-
*/
242-
void print() const override;
243-
244-
private:
245-
std::string type_;
246-
datatype expr_;
247-
};
248-
249-
/**
250-
* @brief The statement node
251-
*
252-
* A statement may hold construct one of a string, expression, or literal.
253-
* Note that statements may have branches (such as if statements or switch/case)
254-
*
255-
*/
256-
class statement_node final : public node
257-
{
258-
public:
259-
using ptr = std::unique_ptr<statement_node>;
260-
using datatype = std::
261-
variant<std::monostate, std::string, expression_node, literal_node>;
262-
/**
263-
* @brief Construct a new statement node
264-
*
265-
* @param type statement type, per the grammar
266-
* @param branches a vector of the statement branches
267-
*/
268-
explicit statement_node(std::string type,
269-
std::vector<statement_node::ptr> branches)
270-
: type_(std::move(type))
271-
, branches_(std::move(branches))
272-
273-
{
274-
}
275-
276-
/**
277-
* @brief The statement node print function
278-
*
279-
*/
280-
void print() const override;
281-
282-
private:
283-
std::string type_;
284-
std::vector<statement_node::ptr> branches_;
285-
datatype data_;
286-
};
287-
288-
} // namespace ast
25+
} // namespace json
28926
} // namespace roxas

0 commit comments

Comments
 (0)