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

Simplify generated code #61

Merged
merged 3 commits into from
Aug 14, 2023
Merged

Conversation

funny-falcon
Copy link
Contributor

  • make get_byte accept char range
  • abuse choice variants reset position by themself
  • abuse optional rule reset position by itself
  • simplify sequence with resetting position after block instead of after each element
  • unify + and numbered repetition
  • strip actions body

Also fix issue with nested repetition collection:

  • names _ary and _count were erroneously reused

Branch is made on top of #60, so first 2 commits are the same.

Obsoletes #59

tiny_markdown example fails otherwise.
It were broken with mine
cf8f511 "add more tests to position handling and fix them"
@funny-falcon funny-falcon changed the title Some simplifications Simplify generated code Aug 13, 2023
Copy link
Owner

@evanphx evanphx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions mostly!

def get_byte
def get_byte(char_range = nil)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only call get_byte with a range one place, seems better to just have a separate method for that, perhaps check_byte_in_range.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do.
Looks like it were single place where result of get_byte were used. So get_byte should be also renamed.
match_dot ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. match_dot is best.

code << indentify(" break unless _tmp\n", indent)
code << indentify(" end\n", indent)
code << indentify(" _tmp = true\n", indent)
code << indentify("_tmp = #{cnt} >= #{op.min} || nil\n", indent)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why include the || nil here? _tmp being false is totally fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did it with NotPredicate: tmp = tmp ? nil : true . I thought falsey tmp should be nil.
Ok, it will simplify things.

Comment on lines -232 to +209
code << indentify("while true # sequence\n", indent)
code << indentify("begin # sequence\n", indent)
op.ops.each_with_index do |n, idx|
output_op code, n, (indent+1)

if idx == op.ops.size - 1
code << indentify(" unless _tmp\n", indent)
code << indentify(" self.pos = #{ss}\n", indent)
code << indentify(" end\n", indent)
code << indentify(" break\n", indent)
else
code << indentify(" unless _tmp\n", indent)
code << indentify(" self.pos = #{ss}\n", indent)
code << indentify(" break\n", indent)
code << indentify(" end\n", indent)
if idx > 0
code << indentify(" break unless _tmp\n", indent)
end
output_op code, n, (indent+1)
end
code << indentify("end while false\n", indent)
code << indentify("unless _tmp\n", indent)
code << indentify(" self.pos = #{ss}\n", indent)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great cleanup.

Comment on lines -142 to -150
code << indentify("#{ss} = self.pos\n", indent)
code << indentify("while true # choice\n", indent)
code << indentify("begin # choice\n", indent)
op.ops.each_with_index do |n,idx|
output_op code, n, (indent+1)

code << indentify(" break if _tmp\n", indent)
code << indentify(" self.pos = #{ss}\n", indent)
if idx == op.ops.size - 1
code << indentify(" break\n", indent)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally get the logic behind not restoring the save here, but we should double check that all the rules, if they consume input, perform the reset themselves. This was always a bit of a failsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think double check pays the bill here. Rules are thoroughly tested by using them within compiller itself. "What could go wrong?" :-)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! fair enough. You're right through, the metacircularity is the best test bed.

- rename `get_byte`->`match_dot`, add `match_char_range`
- abuse choice variants reset position by themself
- abuse optional rule reset position by itself
- simplify sequence with resetting position after block instead of after each element
- unify `+` and numbered repetition
- strip actions body

Also fix issue with nested repetition collection:
- names `_ary` and `_count` were erroneously reused
@evanphx
Copy link
Owner

evanphx commented Aug 14, 2023

Thanks a bunch!

@evanphx evanphx merged commit 01fb95d into evanphx:master Aug 14, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants