Skip to content

Commit 7130c18

Browse files
committed
pre merge
1 parent b5060eb commit 7130c18

File tree

9 files changed

+130
-14
lines changed

9 files changed

+130
-14
lines changed

src/ast/node.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace hk::ast {
77

88
std::unexpected<hkc_error> node::_add(hkc_error error, std::string message)
99
{
10-
return std::unexpected{source().errors().add(source().lines(), first, last, error, std::move(message))};
10+
auto const [it, _] = source().errors().add(source().lines(), first, last, error, std::move(message));
11+
return std::unexpected{it->code()};
1112
}
1213

1314
}

src/error/error_item.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cassert>
99
#include <filesystem>
1010
#include <expected>
11+
#include <compare>
1112

1213
namespace hk {
1314

@@ -24,9 +25,20 @@ class error_item {
2425
{
2526
}
2627

27-
operator std::unexpected<hkc_error>() const
28+
[[nodiscard]] constexpr friend bool operator==(error_item const& lhs, error_item const& rhs) noexcept
2829
{
29-
return std::unexpected{_code};
30+
return lhs._first == rhs._first and lhs._last == rhs._last and lhs._code == rhs._code;
31+
}
32+
33+
[[nodiscard]] constexpr friend auto operator<=>(error_item const& lhs, error_item const& rhs) noexcept
34+
{
35+
if (auto r = lhs._first <=> rhs._first; r != std::strong_ordering::equal) {
36+
return r;
37+
} else if (auto r = lhs._last <=> rhs._last; r != std::strong_ordering::equal) {
38+
return r;
39+
} else {
40+
return lhs._code <=> rhs._code;
41+
}
3042
}
3143

3244
[[nodiscard]] constexpr char const* first() const noexcept

src/error/error_list.hpp

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,96 @@
88
#include <format>
99
#include <concepts>
1010
#include <expected>
11+
#include <algorithm>
1112

1213
namespace hk {
1314

1415
class error_list : public std::vector<error_item> {
1516
public:
1617
using element_type = error_item;
1718

18-
hkc_error add(line_table const& lines, char const* first, char const* last, hkc_error code, std::string message = std::string{})
19+
/** Add an error.
20+
*
21+
* This add will make sure the error_list is sorted by file order.
22+
* Parsing the file multiple times will not introduce duplicate errors.
23+
*
24+
* @param lines The line table for this file. Used for print the error
25+
* message to stderr.
26+
* @param first Pointer to the first character causing the error.
27+
* @param last Pointer to beyond the last character causing the error.
28+
* @param error The error code.
29+
* @param message An extra message to print.
30+
* @return The iterator to the error, true if a new error was inserted.
31+
*/
32+
std::pair<iterator, bool> add(line_table const& lines, char const* first, char const* last, hkc_error error, std::string message = std::string{})
1933
{
20-
auto const& e = this->emplace_back(code, first, last, std::move(message));
21-
e.print(lines);
22-
return code;
34+
auto e = error_item{error, first, last, std::move(message)};
35+
36+
auto it = std::lower_bound(begin(), end(), e, [](auto const& item, auto const& x) {
37+
return item < x;
38+
});
39+
if (it != end() and *it == e) {
40+
return {it, false};
41+
}
42+
43+
it = insert(it, std::move(e));
44+
it->print(lines);
45+
return {it, true};
2346
}
2447

48+
/** Add an error.
49+
*
50+
* This add will make sure the error_list is sorted by file order.
51+
* Parsing the file multiple times will not introduce duplicate errors.
52+
*
53+
* @param lines The line table for this file. Used for print the error
54+
* message to stderr.
55+
* @param first Pointer to the first character causing the error.
56+
* @param last Pointer to beyond the last character causing the error.
57+
* @param error The error code.
58+
* @param fmt Formatting string for an extra error message (optional)
59+
* @param args... Arguments for formatting.
60+
* @return The iterator to the error, true if a new error was inserted.
61+
*/
2562
template<typename... Args>
26-
hkc_error add(line_table const& lines, char const* first, char const* last, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args)
63+
std::pair<iterator, bool> add(line_table const& lines, char const* first, char const* last, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args)
2764
{
2865
return add(lines, first, last, code, std::format(std::move(fmt), std::forward<Args>(args)...));
2966
}
3067

68+
/** Add an error.
69+
*
70+
* This add will make sure the error_list is sorted by file order.
71+
* Parsing the file multiple times will not introduce duplicate errors.
72+
*
73+
* @param lines The line table for this file. Used for print the error
74+
* message to stderr.
75+
* @param first Pointer to the first character causing the error.
76+
* @param error The error code.
77+
* @param fmt Formatting string for an extra error message (optional)
78+
* @param args... Arguments for formatting.
79+
* @return The iterator to the error, true if a new error was inserted.
80+
*/
3181
template<typename... Args>
32-
hkc_error add(line_table const& lines, char const* first, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args)
82+
std::pair<iterator, bool> add(line_table const& lines, char const* first, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args)
3383
{
3484
return add(lines, first, nullptr, code, std::format(std::move(fmt), std::forward<Args>(args)...));
3585
}
3686

87+
/** Add an error.
88+
*
89+
* This add will make sure the error_list is sorted by file order.
90+
* Parsing the file multiple times will not introduce duplicate errors.
91+
*
92+
* @param lines The line table for this file. Used for print the error
93+
* message to stderr.
94+
* @param error The error code.
95+
* @param fmt Formatting string for an extra error message (optional)
96+
* @param args... Arguments for formatting.
97+
* @return The iterator to the error, true if a new error was inserted.
98+
*/
3799
template<typename... Args>
38-
hkc_error add(line_table const& lines, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args)
100+
std::pair<iterator, bool> add(line_table const& lines, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args)
39101
{
40102
return add(lines, nullptr, nullptr, code, std::format(std::move(fmt), std::forward<Args>(args)...));
41103
}

src/parser/parse_context.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class parse_context {
2626

2727
std::unexpected<hkc_error> add(char const* first, char const* last, hkc_error error, std::string message = std::string{})
2828
{
29-
return std::unexpected{errors().add(lines(), first, last, error, std::move(message))};
29+
auto const [it, _] = errors().add(lines(), first, last, error, std::move(message));
30+
return std::unexpected{it->code()};
3031
}
3132

3233
/** Add an error to the list.

src/repository/repository_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ TEST_CASE(recursive_repository_scan)
3434
REQUIRE(repository.child_repositories().size() == 2);
3535
REQUIRE(repository.child_repositories()[0]->remote.url() == "https://github.com/hikoworks/hikolang-test-a.git");
3636
REQUIRE(repository.child_repositories()[1]->remote.url() == "https://github.com/hikoworks/hikolang-test-b.git");
37+
38+
REQUIRE(repository.anchors().size() == 2);
3739
}
3840

3941
//TEST_CASE(parse_repository)

src/utility/fqname.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010

1111
namespace hk {
1212

13+
class fqname_long {
14+
public:
15+
private:
16+
uint8_t _capacity;
17+
uint8_t _component_positions[16];
18+
uint8_t _text[];
19+
};
20+
21+
class fqname {
22+
public:
23+
24+
private:
25+
union {
26+
fqname_long *_ptr;
27+
uint8_t _text[sizeof(fqname_long *)];
28+
};
29+
uint8_t _component_positions[7];
30+
uint8_t _prefix_dots : 4 = 0;
31+
uint8_t _is_long : 1 = 0;
32+
};
33+
34+
1335
class fqname {
1436
public:
1537
class const_iterator {
@@ -457,7 +479,16 @@ class fqname {
457479
private:
458480
uint8_t _num_prefix_dots = 0;
459481
uint8_t _num_components = 0;
482+
uint8_t _component_positions[6];
460483
std::string _name = {};
484+
485+
union {
486+
char const *_ptr;
487+
uint8_t _buffer[sizeof(char const *)];
488+
};
489+
uint8_t _component_positions[15];
490+
uint8_t _num_prefix_dots : 3 = 0;
491+
uint8_t _capacity : 5 = 0;
461492
};
462493

463494
}

t.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
for i in range(4):
4+
n = i
5+
i = i >> 1
6+
n = n & i
7+
n = n + i
8+
print(n)

test_data/return42/main.hkm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11

22
program "return42"
33

4-
5-
fn main() -> __builtin_i32
6-
ccc
4+
fn main()
75
{
86
return 42
97
}

test_data/simple.hkm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
module com.example.foo application "bar"
33

44
import git "https://github.com/example/baz" "main"
5+

0 commit comments

Comments
 (0)