Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TODO: Add support for numbered repetitions i.e. {n,m}, {n,}, {,m} #1

Closed
eriknyquist opened this issue May 16, 2016 · 0 comments
Closed

Comments

@eriknyquist
Copy link
Owner

eriknyquist commented May 16, 2016

Should look like this:

a{n,m} : Match n-m (inclusive) repetitions of a.
a{n,} : Match n or more (inclusive) repetitions of a.
a{,m} : Match m or less (inclusive) repetitions of a.

This can be done (I think) by adding just one new VM instruction, "jlt" or "Jump if Less Than".
This instruction takes two parameters; the index of an instruction in the VM program (jump location), and a count value. For example:

jlt 5 6

The first parameter "5" is the jump location. The second parameter "6" is the count value. The VM must keep count of how many times this instruction is executed within the containing program; if that count is less than the jlt instruction's count value, then execution will jump to the specified location, and if both values are equal then the VM will stop keeping count and execution will carry on to the next instruction as normal. The effect, in this example, is that for the first 6 times this instruction is executed it will cause execution to jump to the instruction at "5", and every time after that it will have no effect and execution will proceed normally.

These can be used with branch instructions to build the desired functionality. Some examples:

abc{n,m}

0 char a
1 char b
2 jlt 4 n
3 branch 4 6
4 char c
5 jlt 2 m+1
6 match

abc{n,}

0 char a
1 char b
2 char c
3 jlt 2 n+1
4 branch 2 5
5 match

abc{,m}

0 char a
1 char b
2 branch 3 5
3 char c
4 jlt 2 m+1
5 match

abc{n}

0 char a
1 char b
2 char c
3 jlt 2 n+1
4 match

@eriknyquist eriknyquist changed the title TODO: add support for numbered repetitions i.e. {n,m}, {n,}, {,m} TODO: Add support for numbered repetitions i.e. {n,m}, {n,}, {,m} May 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant