Skip to content

Commit

Permalink
Add JSON parser example (#391)
Browse files Browse the repository at this point in the history
* Handle string escapes properly; handle exponents

* Refined JSON example with documentation and name improvements

* Refined the defintion of space

* uplift PR with new changes

* Use a resource in JSON parser sample after various bug fixes

* Naming things

* Clean up IO in sample

* handle aliases of globals properly; remove sample exe

* Organise json sample; address comments

* Fix string prepend; name digits_ more clearly

* Require ! for rsourceful HO calls

* Refactor digits into a loop

* Newline EOF; casing

* remove imports

* Better document samples/json; add more tests to samples/json

* Align formatting of brainfuck with json

* $ for consistency

* Remove unnecessary type constraint; grammar

* Make pair type generic; naming things
  • Loading branch information
jimbxb authored Oct 3, 2024
1 parent 2faa9bc commit f3ed014
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 68 deletions.
28 changes: 15 additions & 13 deletions samples/brainfuck.wybe
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## brainfuck.wybe
## Author: James Barnes
#
# A Brainfuck interpreter and LL(1) parser.

## instruction type
# Enumeration of brainfuck instructions.
type instruction {
## instruction
# Enumeration of brainfuck instructions.
pub constructors incr | decr | left | right | input | output | loop(list(_))

## `=`/2
Expand All @@ -26,33 +31,30 @@ type instruction {
# ']' should not occur if at top_level
~top_level
?instrs = []
| else ::
| else ::
?instr = case c in {
'+' :: incr
| '-' :: decr
| '<' :: left
| '>' :: right
| ',' :: input
| '.' :: output
| '[' :: loop(body) where {
# parse the body of a loop, no longer top_level
parse(false, !str, ?body)
}
| '[' :: loop(body) where { parse(false, !str, ?body) }
}
parse(top_level, !str, ?instrs)
?instrs = [instr | instrs]
}
| else ::
| else ::
# str is empty, so must be at top_level
top_level
?instrs = []
}
}
}

## tape type
# Infinite length tape type, with tape expanded when required.
type tape {
## tape
# Infinite length tape type, with tape expanded when required.
pub tape(list(char), current:char, list(char))

## `=`/2
Expand All @@ -69,7 +71,7 @@ type tape {
tape(?left, ?current, ?right) = tape
if { right = [?r | ?right] ::
?tape = tape([current | left], r, right)
| else ::
| else ::
?tape = tape([current | left], '\0', [])
}
}
Expand All @@ -80,7 +82,7 @@ type tape {
tape(?left, ?current, ?right) = tape
if { left = [?l | ?left] ::
?tape = tape(left, l, [current | right])
| else ::
| else ::
?tape = tape([], '\0', [current | right])
}
}
Expand Down Expand Up @@ -130,6 +132,6 @@ def run(instrs:list(instruction), !state:tape) use !io {

if { parse(str, ?instrs) ::
!run(instrs, blank_tape)
| else ::
| else ::
!error("parse error")
}
Loading

0 comments on commit f3ed014

Please sign in to comment.