Skip to content

Commit

Permalink
More improvements, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
triallax committed Nov 15, 2023
1 parent 8a9632c commit 4a2c976
Show file tree
Hide file tree
Showing 3 changed files with 819 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lexers/embedded/hare.xml
Expand Up @@ -7,17 +7,11 @@
</config>
<rules>
<state name="whitespace">
<rule pattern="^use\s.+;">
<token type="CommentPreproc"/>
<rule pattern="[\s\n]+">
<token type="TextWhitespace"/>
</rule>
<rule pattern="@[a-z]+">
<token type="CommentPreproc"/>
</rule>
<rule pattern="\n">
<token type="Text"/>
</rule>
<rule pattern="\s+">
<token type="Text"/>
<token type="NameDecorator"/>
</rule>
<rule pattern="//.*\n">
<token type="CommentSingle"/>
Expand Down Expand Up @@ -64,6 +58,9 @@
<rule pattern="[()\[\],.{};]">
<token type="Punctuation"/>
</rule>
<rule pattern="use\b">
<token type="KeywordNamespace"/>
</rule>
<rule pattern="(_|align|break|const|continue|else|enum|export|for|if|return|size|static|struct|offset|union|fn|free|assert|abort|alloc|let|len|def|type|match|switch|case|append|delete|insert|defer|yield|vastart|vaarg|vaend)\b">
<token type="Keyword"/>
</rule>
Expand Down
66 changes: 66 additions & 0 deletions lexers/testdata/hare.actual
@@ -0,0 +1,66 @@
use bufio;
use fmt;
use io;
use linux::timerfd;
use os;
use os::exec;
use time;
use unix::poll;

type period = struct {
comment: str,
duration: time::duration,
};

const periods: [_]period = [
period { comment = "Time to start working!", duration = 25 * time::MINUTE },
period { comment = "Time to start resting!", duration = 5 * time::MINUTE },
period { comment = "Time to start working!", duration = 25 * time::MINUTE },
period { comment = "Time to start resting!", duration = 5 * time::MINUTE },
period { comment = "Time to start working!", duration = 25 * time::MINUTE },
period { comment = "Time to start resting!", duration = 5 * time::MINUTE },
period { comment = "Time to start working!", duration = 25 * time::MINUTE },
period { comment = "Time to take a nap!" , duration = 15 * time::MINUTE },
];

export fn main() void = {
for (let i = 0z, paused = false; true; i = (i + 1) % len(periods)) {
let period = periods[i];

let cmd = exec::cmd("notify-send", "--", "theo", period.comment)!;
exec::start(&cmd)!;

let tfd = timerfd::new(time::clock::MONOTONIC, timerfd::new_flag::NONE)!;
defer {
timerfd::unset(tfd)!;
io::close(tfd)!;
};
timerfd::set(tfd, time::SECOND: timerfd::interval, timerfd::set_flag::NONE)!;

let fds = [poll::pollfd { fd = tfd, events = poll::event::POLLIN, ... }, poll::pollfd { fd = os::stdin_file, events = poll::event::POLLIN, ... }];

for (let remaining = period.duration; remaining >= 0; poll::poll(fds, poll::INDEF)!) {
if (fds[1].revents != 0) {
match (bufio::read_line(os::stdin_file)) {
case let arr: []u8 => free(arr);
case => void;
};

paused = !paused;
};

if (paused) {
// TODO: just disable timer instead of sleeping.
time::sleep(time::SECOND);
continue;
};

if (fds[0].revents != 0) {
timerfd::read(tfd)!;
remaining -= time::SECOND;
let remaining_secs = remaining / time::SECOND;
fmt::printfln("{} ({:02}:{:02} remaining)", period.comment, remaining_secs / 60, remaining_secs % 60)!;
};
};
};
};

0 comments on commit 4a2c976

Please sign in to comment.