Skip to content

Commit b2dc4f1

Browse files
committed
convert tokenizer to generator
1 parent 4e95867 commit b2dc4f1

File tree

13 files changed

+792
-341
lines changed

13 files changed

+792
-341
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ The second integer type is `long`, this integer dynamically scales in size
3838
and is allocated on the heap, and includes SIO (Short Integer Optimization).
3939

4040
## Units system
41+
Real, rational and decimal types can be tagged with a unit, which is used
42+
for dimensional analysis.
4143

4244
```
4345
let speed = 100.0 (km/h)
4446
let duration = 15.0 min
4547
let distance = speed * duration // 25.0 km
4648
47-
fn convert(length : real (m), dpi : real (px/in)) -> real (px)
49+
fn convert(length : real #iso.length, dpi : real #(screen.length/iso.length)) -> real #screen.length
4850
{
4951
return length * dpi
5052
}

src/hkc/repository.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ void repository::scan_prologues(bool force)
5858
continue;
5959
}
6060

61-
auto cursor = file_cursor(module_path);
62-
auto it =
63-
64-
m.errors.clear();
65-
if (auto r = parse_module(cursor, m.errors, true)) {
66-
67-
}
61+
//auto cursor = file_cursor(module_path);
62+
//auto it =
63+
//
64+
//m.errors.clear();
65+
//if (auto r = parse_module(cursor, m.errors, true)) {
66+
//
67+
//}
6868
}
6969

7070
untouch(true);

src/parser/parse_module.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "consume.hpp"
55
#include "ast/nodes.hpp"
66
#include "tokenizer/tokenizer.hpp"
7+
#include "utility/lazy_vector.hpp"
78

89
namespace hk {
910

@@ -45,32 +46,11 @@ namespace hk {
4546

4647
[[nodiscard]] ast::module_node_ptr parse_module(hk::file_cursor& c, bool only_prologue)
4748
{
48-
class delegate_type : public tokenize_delegate {
49-
public:
50-
delegate_type(std::vector<token> &tokens) : _tokens(std::addressof(tokens)) {}
49+
50+
auto token_generator = hk::tokenize(c);
51+
auto lazy_tokens = lazy_vector{token_generator.cbegin(), token_generator.cend()};
5152

52-
void on_token(token t) override
53-
{
54-
if (t == '\0') {
55-
// If the token is an end-of-file token, we append extra tokens to make sure
56-
// the parser can always look ahead without checking for end-of-file.
57-
for (auto i = 0uz; i != 8; ++i) {
58-
_tokens->push_back(t);
59-
}
60-
}
61-
62-
_tokens->push_back(std::move(t));
63-
}
64-
65-
private:
66-
std::vector<token>* _tokens;
67-
};
68-
69-
auto tokens = std::vector<token>{};
70-
auto delegate = delegate_type(tokens);
71-
hk::tokenize(c, delegate);
72-
73-
auto it = tokens.cbegin();
53+
auto it = lazy_tokens.cbegin();
7454
auto m = parse_module(it, only_prologue);
7555
m->upstream_paths = c.upstream_paths();
7656
return m;

src/parser/parsers.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
#include "tokenizer/token.hpp"
66
#include "error/error_list.hpp"
77
#include "utility/file_cursor.hpp"
8+
#include "utility/lazy_vector.hpp"
9+
#include "utility/generator.hpp"
810
#include <vector>
911
#include <memory>
1012
#include <expected>
1113

1214
namespace hk {
1315

14-
using token_iterator = std::vector<token>::const_iterator;
16+
using token_vector = lazy_vector<generator<token>::const_iterator, std::default_sentinel_t, token>;
17+
using token_iterator = token_vector::const_iterator;
1518

1619
template<typename T>
1720
using parse_result = std::expected<T, error_code>;

0 commit comments

Comments
 (0)