Skip to content

Commit

Permalink
Add new member function template take_until and take_while
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Jun 10, 2024
1 parent 742652d commit bbe7e51
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.202_amd64.deb
sudo apt install build/meevax_0.5.203_amd64.deb
```

or
Expand Down Expand Up @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.202.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.203.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.202_amd64.deb`
| `package` | Generate debian package `meevax_0.5.203_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.202
0.5.203
30 changes: 26 additions & 4 deletions include/meevax/kernel/textual_input_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,35 @@ inline namespace kernel
*/
auto take_character() -> character;

auto take_digits(character = {}) -> std::string;
auto take_nested_block_comment(character = {}, character = {}) -> void;

auto take_line(std::istream::char_type = '\n') -> std::string;
auto take_token(character = {}) -> std::string;

template <typename F>
auto take_until(F satisfy, character c = {})
{
auto s = static_cast<std::string>(c);

auto take_nested_block_comment(character = {}, character = {}) -> void; // TODO return std::string
while (get_ready() and not satisfy(c = take_character()))
{
s += c;
}

auto take_token(character = {}) -> std::string;
return s;
}

template <typename F>
auto take_while(F satisfy, character c = {})
{
auto s = static_cast<std::string>(c);

while (get_ready() and satisfy(peek_character()))
{
s += take_character();
}

return s;
}

virtual auto istream() -> std::istream & = 0;

Expand Down
47 changes: 15 additions & 32 deletions src/kernel/textual_input_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ inline namespace kernel

auto textual_input_port::get_line() -> object
{
if (auto s = take_line(); istream().eof())
if (auto s = take_until([](auto c) { return c == '\n'; }); istream().eof())
{
return eof_object;
}
Expand Down Expand Up @@ -192,6 +192,11 @@ inline namespace kernel

auto textual_input_port::read(character c0) -> object
{
auto is_digit = [](auto c)
{
return std::isdigit(c);
};

while (not peek_character().is_eof())
{
switch (auto const c1 = c0 ? c0 : take_character())
Expand Down Expand Up @@ -221,7 +226,7 @@ inline namespace kernel
}
else
{
take_line();
take_until([](auto c) { return c == '\n'; });
}

return read();
Expand All @@ -247,7 +252,7 @@ inline namespace kernel
case '8':
case '9':
{
auto n = take_digits(c2);
auto n = take_while(is_digit, c2);

switch (auto c = take_character())
{
Expand Down Expand Up @@ -312,7 +317,7 @@ inline namespace kernel
return exact(read()); // NOTE: Same as #,(exact (read))

case 'f':
switch (std::stoi(take_digits()))
switch (std::stoi(take_while(is_digit, character('0'))))
{
case 32:
return make<f32vector>(read());
Expand All @@ -339,7 +344,7 @@ inline namespace kernel
}

case 's':
switch (auto const n = take_digits(); std::stoi(n))
switch (auto n = take_while(is_digit); std::stoi(n))
{
case 8:
return make<s8vector>(read());
Expand All @@ -363,7 +368,7 @@ inline namespace kernel
return t;

case 'u':
switch (auto const n = take_digits(); std::stoi(n))
switch (auto const n = take_while(is_digit); std::stoi(n))
{
case 8:
return make<u8vector>(read());
Expand Down Expand Up @@ -428,7 +433,7 @@ inline namespace kernel
catch (std::integral_constant<char, '.'> const&)
{
let const x = read();
take_line(')');
take_until([](auto c) { return c == ')'; });
return x;
}

Expand All @@ -446,7 +451,7 @@ inline namespace kernel
}

case ';': // 0x3B
take_line();
take_until([](auto c) { return c == '\n'; });
break;

case '`': // 0x60
Expand Down Expand Up @@ -558,14 +563,11 @@ inline namespace kernel
case 'r': s.vector.emplace_back('\r'); break;
case 't': s.vector.emplace_back('\t'); break;
case 'v': s.vector.emplace_back('\v'); break;
case 'x': s.vector.emplace_back(lexical_cast<character::int_type>(std::hex, take_line(';'))); break;
case 'x': s.vector.emplace_back(lexical_cast<character::int_type>(std::hex, take_until([](auto c) { return c == ';'; }))); break;

case '\n':
case '\r':
while (std::isspace(peek_character()))
{
take_character();
}
take_while([](auto c) { return std::isspace(c); });
break;

default:
Expand Down Expand Up @@ -620,25 +622,6 @@ inline namespace kernel
}
}

auto textual_input_port::take_digits(character c) -> std::string
{
auto s = static_cast<std::string>(c);

while (std::isdigit(peek_character()))
{
s += take_character();
}

return s.length() ? s : "0";
}

auto textual_input_port::take_line(std::istream::char_type delimiter) -> std::string
{
auto s = std::string();
std::getline(istream(), s, delimiter);
return s;
}

auto textual_input_port::take_nested_block_comment(character sharp, character vertical_line) -> void
{
while (not peek_character().is_eof())
Expand Down

0 comments on commit bbe7e51

Please sign in to comment.