zevv / npeg

PEGs for Nim, another take
MIT License
330 stars 22 forks source link

Implement slicing operators for captures #40

Closed PMunch closed 1 year ago

PMunch commented 2 years ago

Someone commented on an old YouTube video of mine showing Advent of Code solutions using NPeg. After https://github.com/zevv/npeg/commit/0e297ac30790c0cb76d2a17dd968db41b4e22e3b the Captures type is no longer just an alias for a sequence, but rather an object. The object has [] defined for it, but not for slices which I used in my solution. The fix is simple enough, but for backwards compatibility and future usability it would be nice to have.

zevv commented 1 year ago

Hey @PMunch, better late then never, eh?

NPeg does support [] for captures, so afaik this should just work; is this issue still relevant?

https://github.com/zevv/npeg/blob/91343a68b8995fdc6adb539ae4ebe72638f0280d/src/npeg/capture.nim#L86

PMunch commented 1 year ago

Definitely! The problem isn't with BackwardsIndex though, and accessing elements with [^2] works fine for example. But accessing them with a slice like [1..^1] no longer works. The exact error is:

/tmp/aoc2020/day16/part1.nim(8, 18) template/generic instantiation of `peg` from here
/home/peter/.nimble/pkgs/npeg-0.26.0/npeg/codegen.nim(403, 33) template/generic instantiation of `fn`gensym99` from here
/home/peter/.nimble/pkgs/npeg-0.26.0/npeg/patt.nim(90, 18) Error: type mismatch: got <Captures[system.char], HSlice[system.int, system.BackwardsIndex]>
zevv commented 1 year ago

Ah right, that makes sense. I'll see if I can put that in.

PMunch commented 1 year ago

Fantastic! Just in time for the next advent of code :)

zevv commented 1 year ago

Quoting Peter Munch-Ellingsen (2022-11-07 09:19:10)

Fantastic! Just in time for the next advent of code :)

Wow is it that time of year already, I still need to pick a new language!

PMunch commented 1 year ago

You've still got about a month, looking for anything in particular?

zevv commented 1 year ago

Quoting Peter Munch-Ellingsen (2022-11-07 10:34:45)

You've still got about a month, looking for anything in particular?

Nah, it'll pobably be rust. I tried doing new projects in Rust a few times but never persued i because I felt I wasn't really enjoying myself enough - it felt like the language is trying to actively discourage me to use it. Starting again with small stuff might help, as it force one to dive into the language and stdlib docs and learn the idioms.

We'll see where I end up :)

-- :wq ^X^Cy^K^X^C^C^C^C

PMunch commented 1 year ago

I've barely touched Rust when I was trying out various microcontroller solutions. Was left with much the same feeling you appear to have. Zig on the other hand looked pretty neat.

zevv commented 1 year ago

Quoting Peter Munch-Ellingsen (2022-11-07 11:13:33)

I've barely touched Rust when I was trying out various microcontroller solutions. Was left with much the same feeling you appear to have. Zig on the other hand looked pretty neat.

If Zig only had closures, it'd by my next choice.

PMunch commented 1 year ago

Oh it doesn't? That never came up when I was doing microcontroller stuff, for fairly obvious reasons. Could still be worth a try, I'd really like Nim to have interfaces, but I live without them just fine.