Skip to content

Commit

Permalink
Merge pull request #14 from savi-lang/update
Browse files Browse the repository at this point in the history
Update enum declarations for latest Savi version.
  • Loading branch information
jemc authored Mar 26, 2022
2 parents db27e2e + 936e999 commit 985ce3a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
9 changes: 9 additions & 0 deletions manifest.savi
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
:dependency Spec v0
:from "github:savi-lang/Spec"
:depends on Map
:depends on Time
:depends on Timer

:transitive dependency Map v0
:from "github:savi-lang/Map"

:transitive dependency Time v0
:from "github:savi-lang/Time"

:transitive dependency Timer v0
:from "github:savi-lang/Timer"
:depends on Time
28 changes: 13 additions & 15 deletions src/_Op.savi
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
:enum _Op
:const bit_width U8: 8

:fun non from!(byte U8): @from_u64!(byte.u64)

// We reserve everything below the minimum opcode to match the specified byte.
:fun non min_op U8: 0xF3

// This could theoretically be used for any byte, but we only use it for
// bytes from min_op and higher; all lower op codes indicate the byte itself.
:member _OpByte: 0xF3
:member Byte 0xF3

:member _OpMatch: 0xF4
:member _OpRecursiveMatch: 0xF5 // For lookahead
:member _OpJump: 0xF6
:member _OpSplit: 0xF7
:member _OpSplitMany: 0xF8
:member _OpAnyByte: 0xF9
:member _OpByteRange: 0xFA
:member _OpSaveStart: 0xFB
:member _OpSaveFinish: 0xFC
:member _OpBackrefCompare: 0xFD
:member _OpInlineZWA: 0xFE
:member _OpRecursiveZWA: 0xFF
:member Match 0xF4
:member RecursiveMatch 0xF5 // For lookahead
:member Jump 0xF6
:member Split 0xF7
:member SplitMany 0xF8
:member AnyByte 0xF9
:member ByteRange 0xFA
:member SaveStart 0xFB
:member SaveFinish 0xFC
:member BackrefCompare 0xFD
:member InlineZWA 0xFE
:member RecursiveZWA 0xFF

26 changes: 13 additions & 13 deletions src/_ProgramWriter.savi
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

:fun ref compile(pattern _Pattern)
@_pattern(pattern)
@_op(_OpMatch)
@_op(_Op.Match)
@

:fun ref _pattern(pattern _Pattern)
case pattern <: (
| _PatternAnyByte |
@_op(_OpAnyByte)
@_op(_Op.AnyByte)

| _PatternByte |
@_op(_OpByte)
@_op(_Op.Byte)
@_byte(pattern.byte)

| _PatternByteRange |
@_op(_OpByteRange)
@_op(_Op.ByteRange)
@_byte(pattern.low)
@_byte(pattern.high)

Expand All @@ -35,7 +35,7 @@
if (byte < _Op.min_op) (
@_byte(byte)
|
@_op(_OpByte)
@_op(_Op.Byte)
@_byte(byte)
)
)
Expand All @@ -44,7 +44,7 @@
pattern.children.each -> (child | @_pattern(child))

| _PatternOptional |
@_op(_OpSplit)
@_op(_Op.Split)
primary_addr = @_later_addr
secondary_addr = @_later_addr

Expand All @@ -58,7 +58,7 @@

| _PatternZeroOrMore |
start_cursor = @_cursor
@_op(_OpSplit)
@_op(_Op.Split)
primary_addr = @_later_addr
secondary_addr = @_later_addr

Expand All @@ -68,7 +68,7 @@
try @_store_current_cursor_at!(present_addr)
@_pattern(pattern.child)

@_op(_OpJump)
@_op(_Op.Jump)
@_addr(start_cursor)

try @_store_current_cursor_at!(absent_addr)
Expand All @@ -77,7 +77,7 @@
start_cursor = @_cursor
@_pattern(pattern.child)

@_op(_OpSplit)
@_op(_Op.Split)
if pattern.is_non_greedy (
after_addr = @_later_addr
@_addr(start_cursor)
Expand All @@ -90,30 +90,30 @@

| _PatternChoice |
if (pattern.children.size == 2) (
@_op(_OpSplit)
@_op(_Op.Split)
primary_addr = @_later_addr
secondary_addr = @_later_addr

try @_store_current_cursor_at!(primary_addr)
try @_pattern(pattern.children[0]!)

@_op(_OpJump)
@_op(_Op.Jump)
after_addr = @_later_addr

try @_store_current_cursor_at!(secondary_addr)
try @_pattern(pattern.children[1]!)

try @_store_current_cursor_at!(after_addr)
|
@_op(_OpSplitMany)
@_op(_Op.SplitMany)
branch_addrs Array(U32) = []
pattern.children.each -> (child | branch_addrs << @_later_addr)
@_addr(0) // branch list terminator

after_addrs Array(U32) = []
pattern.children.each_with_index -> (child, index |
if (index > 0) (
@_op(_OpJump)
@_op(_Op.Jump)
after_addrs << @_later_addr
)
try @_store_current_cursor_at!(branch_addrs[index]!)
Expand Down
30 changes: 15 additions & 15 deletions src/_SimpleVM.savi
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@
run_threads.each_until -> (address |
program_byte = @program[address]!
case (
// For _OpAnyByte, consume the byte of input regardless of what it is,
// For _Op.AnyByte, consume the byte of input regardless of what it is,
// then continue with the next byte of the program.
| _OpAnyByte.u8 == program_byte |
| _Op.AnyByte.u8 == program_byte |
@_continue!(next_threads, address +! 1)

// For _OpByteRange, we read the next two bytes in the program,
// For _Op.ByteRange, we read the next two bytes in the program,
// continuing only if the input byte is within that range.
| _OpByteRange.u8 == program_byte |
| _Op.ByteRange.u8 == program_byte |
low = @program[address +! 1]!
high = @program[address +! 2]!
if (input_byte >= low && input_byte <= high) (
@_continue!(next_threads, address +! 3)
)

// For _OpByte, we read the next byte in the bytecode program,
// For _Op.Byte, we read the next byte in the bytecode program,
// and compare it to the input byte to see if it fails to match.
| _OpByte.u8 == program_byte |
| _Op.Byte.u8 == program_byte |
if (input_byte == @program[address +! 1]!) (
@_continue!(next_threads, address +! 2)
)

// If the program byte is below the op code range, then it is
// an implied _OpByte op code with that byte as the expected byte,
// an implied _Op.Byte op code with that byte as the expected byte,
// so we compare it against the input byte same as we did above.
| _Op.min_op > program_byte |
if (input_byte == program_byte) (
Expand Down Expand Up @@ -97,19 +97,19 @@
// that are ready to consume input in the main input-consuming loop.
program_byte = @program[address]!
case (
// _OpJump jumps to the given address by enqueuing that address.
| _OpJump.u8 == program_byte |
// _Op.Jump jumps to the given address by enqueuing that address.
| _Op.Jump.u8 == program_byte |
@_continue!(threads, @program.read_native_u32!(address +! 1).usize)

// _OpSplit jumps to the both of the given addresses simultaneously
// _Op.Split jumps to the both of the given addresses simultaneously
// by enqueuing them both as threads with the given priority order.
| _OpSplit.u8 == program_byte |
| _Op.Split.u8 == program_byte |
@_continue!(threads, @program.read_native_u32!(address +! 1).usize)
@_continue!(threads, @program.read_native_u32!(address +! 5).usize)

// _OpSplitMany jumps to the all of the given addresses simultaneously
// _Op.SplitMany jumps to the all of the given addresses simultaneously
// by enqueuing them all as threads with the given priority order.
| _OpSplitMany.u8 == program_byte |
| _Op.SplitMany.u8 == program_byte |
next_address = address +! 1
keep_going = True
while keep_going (
Expand All @@ -123,9 +123,9 @@
)
)

// If this is an _OpMatch, we've completed a full match.
// If this is an _Op.Match, we've completed a full match.
// There is no need to continue the program in such a case.
| _OpMatch.u8 == program_byte |
| _Op.Match.u8 == program_byte |
@did_match = True

// If it's an opcode we don't handle here, we put the address on the
Expand Down

0 comments on commit 985ce3a

Please sign in to comment.