Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Remove distinction between literals / constructors
Browse files Browse the repository at this point in the history
- Collapses `0x10 list` into the Literals section as `0x0a list`
- Collapses `0x60 list?` into the Literals subsection of Type Queries as `0x5a list?`
- `0x0a list` now requires a `u32` length value after it, rather than popping its length from the stack.
- List item handling on construction is unchanged.
  • Loading branch information
Connor Scialdone committed Jan 10, 2019
1 parent 00957a7 commit fcdb7f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
10 changes: 2 additions & 8 deletions reference.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ SECTION: Literals
0x07 f32 4
0x08 f64 8
0x09 str (len:u32)+len

SECTION: Constructors
In contrast to literals, constructors pop their initialization data from the stack like normal opcodes.
When encountered, the resulting value will be pushed to the stack.
= opcd name (params) ==============================================================================================================
0x10 list (len:int, members:any*len) -- Highest member on stack will be first in list. Errors if len < 0 or len > u32.
0x0a list (len:u32) -- Pulls `len` items from the stack. Highest on stack = first in list.

SECTION: Stack Operations
Base operations on the data stack itself.
Expand Down Expand Up @@ -67,8 +62,7 @@ SECTION: Type Queries
0x57 f32?
0x58 f64?
0x59 str?
-- Constructor types
0x60 list?
0x5a list?

TODO: Variable Operations
TODO: Control Flow
30 changes: 13 additions & 17 deletions spec/opcode_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ describe "SECTION: Literals (0x0_)" do
opcode "0x07 f32 ", [9000.1_f32], 0x07, 0x46, 0x0c, 0xa0, 0x66
opcode "0x08 f64 ", [9000.1_f64], 0x08, 0x40, 0xc1, 0x94, 0x0c, 0xcc, 0xcc, 0xcc, 0xcd
opcode "0x09 str ", ["Hello, world!"], [0x09, 0x00, 0x00, 0x00, 13].map(&.to_u8) + "Hello, world!".bytes
end

describe "SECTION: Constructors (0x1_)" do
opcode "0x10 list", [[true, false]], 0x02, 0, 0x02, 1, 0x01, 2, 0x10
opcode "0x0a list", [[true, false]], 0x02, 0, 0x02, 1, 0x0a, 0x00, 0x00, 0x00, 0x02
end

describe "SECTION: Stack Operations (0x2_)" do
Expand Down Expand Up @@ -56,19 +53,18 @@ describe "SECTION: IO Operations (0x4_)" do
}
end

describe "SECTION: Type Queries (0x5_ - 0x6_)" do
opcode "0x50 null?", [nil, true], 0x00, 0x50
opcode "0x51 byte?", [0x0_u8, true], 0x01, 0x0, 0x51
opcode "0x52 bool?", [false, true], 0x02, 0x0, 0x52
opcode "0x53 i32? ", [0_i32, true], 0x03, 0x0, 0x0, 0x0, 0x0, 0x53
opcode "0x54 i64? ", [0_i64, true], 0x04, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x54
opcode "0x55 u32? ", [0_u32, true], 0x05, 0x0, 0x0, 0x0, 0x0, 0x55
opcode "0x56 u64? ", [0_u64, true], 0x06, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x56
opcode "0x57 f32? ", [0.0_f32, true], 0x07, 0x0, 0x0, 0x0, 0x0, 0x57
opcode "0x58 f64? ", [0.0_f64, true], 0x08, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x58
opcode "0x59 str? ", ["", true], 0x09, 0x0, 0x0, 0x0, 0x0, 0x59
puts " --"
opcode "0x60 list?", [[] of SWAny, true], 0x01, 0x0, 0x10, 0x60
describe "SECTION: Type Queries (0x5_)" do
opcode "0x50 null?", [nil, true], 0x00, 0x50
opcode "0x51 byte?", [0x0_u8, true], 0x01, 0x0, 0x51
opcode "0x52 bool?", [false, true], 0x02, 0x0, 0x52
opcode "0x53 i32? ", [0_i32, true], 0x03, 0x0, 0x0, 0x0, 0x0, 0x53
opcode "0x54 i64? ", [0_i64, true], 0x04, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x54
opcode "0x55 u32? ", [0_u32, true], 0x05, 0x0, 0x0, 0x0, 0x0, 0x55
opcode "0x56 u64? ", [0_u64, true], 0x06, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x56
opcode "0x57 f32? ", [0.0_f32, true], 0x07, 0x0, 0x0, 0x0, 0x0, 0x57
opcode "0x58 f64? ", [0.0_f64, true], 0x08, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x58
opcode "0x59 str? ", ["", true], 0x09, 0x0, 0x0, 0x0, 0x0, 0x59
opcode "0x5a list?", [[] of SWAny, true], 0x0a, 0x0, 0x0, 0x0, 0x0, 0x5a
end

# TODO: Variable Operations
Expand Down
1 change: 1 addition & 0 deletions src/bytecode.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Bytecode < Array(Opcode)
when 0x03, 0x05, 0x07 then 4
when 0x04, 0x06, 0x08 then 8
when 0x09 then 4+prog[insp+1..insp+4].bitwiseConcat
when 0x0a then 4
else 0
end
end
Expand Down
9 changes: 3 additions & 6 deletions src/sherwood.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ module Sherwood
when 0x07 then stack.push(Float32.fromBytes(op.data))
when 0x08 then stack.push(Float64.fromBytes(op.data))
when 0x09 then stack.push(op.data.skip(4).map(&.chr).sum(""))

# SECTION: Constructors
when 0x10 then
(size = popType(SWInt, stack)) > 0 &&
when 0x0a then
(size = op.data.map(&.to_u32).bitwiseConcat) > 0 &&
stack.push(Array(SWAny).new(size) { stack.pop }) ||
stack.push([] of SWAny)

Expand Down Expand Up @@ -91,8 +89,7 @@ module Sherwood
when 0x57 then stack.push(stack.last.is_a?(Float32))
when 0x58 then stack.push(stack.last.is_a?(Float64))
when 0x59 then stack.push(stack.last.is_a?(String))

when 0x60 then stack.push(stack.last.is_a?(Array))
when 0x5a then stack.push(stack.last.is_a?(Array))

# TODO: Variable Operations
# TODO: Control Flow
Expand Down

0 comments on commit fcdb7f3

Please sign in to comment.