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

Commit

Permalink
Move type queries from 0x5_ to 0x1_
Browse files Browse the repository at this point in the history
This puts them next to literals, making their symmetry more intuitive and easily maintainable.
  • Loading branch information
Connor Scialdone committed Jan 10, 2019
1 parent e4f3c3e commit 311022f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 99 deletions.
72 changes: 36 additions & 36 deletions reference.txt
Expand Up @@ -16,53 +16,53 @@ SECTION: Literals
0x09 str (len:u32)+len
0x0a list (len:u32) -- Pulls `len` items from the stack. Highest on stack = first in list.

SECTION: Type Queries
Opcodes to query the types of variables from the stack.
These opcodes operate on the top of the stack, but do not pop from it.
Each opcode returns a bool; true if the type matches, false if it does not.
= opcd name ======================================================================================================================
-- Literal types
0x10 null?
0x11 byte?
0x12 bool?
0x13 i32?
0x14 i64?
0x15 u32?
0x16 u64?
0x17 f32?
0x18 f64?
0x19 str?
0x1a list?

SECTION: Stack Operations
Base operations on the data stack itself.
= opcd name (params) ==============================================================================================================
0x10 drop (v:any) -- Removes the top of the stack.
0x11 dupe (v:any) -- Duplicates the top of the stack.
0x12 swap (a:any, b:any) -- Swaps the top of the stack with the next value down.
0x20 drop (v:any) -- Removes the top of the stack.
0x21 dupe (v:any) -- Duplicates the top of the stack.
0x22 swap (a:any, b:any) -- Swaps the top of the stack with the next value down.

SECTION: Arithmetic Operations
Basic and bitwise arithmetic operations.
= opcd name (params) ==============================================================================================================
0x20 add (a:int, b:int) -- Adds the two numbers on the top of the stack.
0x21 sub (a:int, b:int) -- Subtracts the number on the top of the stack from the number below it.
0x22 mul (a:int, b:int) -- Multiplies the two numbers on the top of the stack.
0x23 div (a:int, b:int) -- Divides the number on the top of the stack from the number below it.
0x24 mod (a:int, b:int) -- Gets the remainder of dividing the two numbers on the top of the stack.
0x25 shl (d:int, n:int) -- Shifts n left by d bits.
0x26 shr (d:int, n:int) -- Shifts n right by d bits.
0x27 not (a:int) -- Bitwise NOT of a.
0x28 and (a:int, b:int) -- Bitwise AND of a and b.
0x29 or (a:int, b:int) -- Bitwise OR of a and b.
0x2a xor (a:int, b:int) -- Bitwise XOR of a and b.
0x30 add (a:int, b:int) -- Adds the two numbers on the top of the stack.
0x31 sub (a:int, b:int) -- Subtracts the number on the top of the stack from the number below it.
0x32 mul (a:int, b:int) -- Multiplies the two numbers on the top of the stack.
0x33 div (a:int, b:int) -- Divides the number on the top of the stack from the number below it.
0x34 mod (a:int, b:int) -- Gets the remainder of dividing the two numbers on the top of the stack.
0x35 shl (d:int, n:int) -- Shifts n left by d bits.
0x36 shr (d:int, n:int) -- Shifts n right by d bits.
0x37 not (a:int) -- Bitwise NOT of a.
0x38 and (a:int, b:int) -- Bitwise AND of a and b.
0x39 or (a:int, b:int) -- Bitwise OR of a and b.
0x3a xor (a:int, b:int) -- Bitwise XOR of a and b.

SECTION: IO Operations
Basic IO interactions.
= opcd name (params) ==============================================================================================================
0x30 getc () -- Gets a char from stdin and pushes it as a u32.
0x31 getl () -- Gets a line from stdin and pushes it as a str (excluding trailing \r?\n).
0x32 putc (c:int) -- Prints a number from the stack as a character. Errors if c < 0 or c > u32.
0x33 puts (s:str) -- Prints a string from the stack.

SECTION: Type Queries
Opcodes to query the types of variables from the stack.
These opcodes operate on the top of the stack, but do not pop from it.
Each opcode returns a bool; true if the type matches, false if it does not.
= opcd name ======================================================================================================================
-- Literal types
0x40 null?
0x41 byte?
0x42 bool?
0x43 i32?
0x44 i64?
0x45 u32?
0x46 u64?
0x47 f32?
0x48 f64?
0x49 str?
0x4a list?
0x40 getc () -- Gets a char from stdin and pushes it as a u32.
0x41 getl () -- Gets a line from stdin and pushes it as a str (excluding trailing \r?\n).
0x42 putc (c:int) -- Prints a number from the stack as a character. Errors if c < 0 or c > u32.
0x43 puts (s:str) -- Prints a string from the stack.

TODO: Variable Operations
TODO: Control Flow
64 changes: 32 additions & 32 deletions spec/opcode_spec.cr
Expand Up @@ -14,58 +14,58 @@ describe "SECTION: Literals (0x0_)" do
opcode "0x0a list", [[true, false]], 0x02, 0, 0x02, 1, 0x0a, 0x00, 0x00, 0x00, 0x02
end

describe "SECTION: Type Queries (0x1_)" do
opcode "0x10 null?", [nil, true], 0x00, 0x10
opcode "0x11 byte?", [0x0_u8, true], 0x01, 0x0, 0x11
opcode "0x12 bool?", [false, true], 0x02, 0x0, 0x12
opcode "0x13 i32? ", [0_i32, true], 0x03, 0x0, 0x0, 0x0, 0x0, 0x13
opcode "0x14 i64? ", [0_i64, true], 0x04, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x14
opcode "0x15 u32? ", [0_u32, true], 0x05, 0x0, 0x0, 0x0, 0x0, 0x15
opcode "0x16 u64? ", [0_u64, true], 0x06, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x16
opcode "0x17 f32? ", [0.0_f32, true], 0x07, 0x0, 0x0, 0x0, 0x0, 0x17
opcode "0x18 f64? ", [0.0_f64, true], 0x08, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x18
opcode "0x19 str? ", ["", true], 0x09, 0x0, 0x0, 0x0, 0x0, 0x19
opcode "0x1a list?", [[] of SWAny, true], 0x0a, 0x0, 0x0, 0x0, 0x0, 0x1a
end

describe "SECTION: Stack Operations (0x2_)" do
opcode "0x10 drop", [] of SWAny, 0x00, 0x10
opcode "0x11 dupe", [nil, nil], 0x00, 0x11
opcode "0x12 swap", [true, false], 0x02, 0, 0x02, 1, 0x12
opcode "0x20 drop", [] of SWAny, 0x00, 0x20
opcode "0x21 dupe", [nil, nil], 0x00, 0x21
opcode "0x22 swap", [true, false], 0x02, 0, 0x02, 1, 0x22
end

describe "SECTION: Arithmetic Operations (0x3_)" do
opcode "0x20 add ", [5], 0x01, 2, 0x01, 3, 0x20
opcode "0x21 sub ", [1], 0x01, 3, 0x01, 2, 0x21
opcode "0x22 mul ", [6], 0x01, 2, 0x01, 3, 0x22
opcode "0x23 div ", [2], 0x01, 4, 0x01, 2, 0x23
opcode "0x24 mod ", [1], 0x01, 5, 0x01, 2, 0x24
opcode "0x25 shl ", [0b10010000], 0x01, 0b00100100, 0x01, 2, 0x25
opcode "0x26 shr ", [0b00001001], 0x01, 0b00100100, 0x01, 2, 0x26
opcode "0x27 not ", [0b11011011], 0x01, 0b00100100, 0x27
opcode "0x28 and ", [0b00100000], 0x01, 0b00100100, 0x01, 0b11111011, 0x28
opcode "0x29 or ", [0b10110100], 0x01, 0b00100100, 0x01, 0b10010000, 0x29
opcode "0x2a xor ", [0b10110000], 0x01, 0b00100100, 0x01, 0b10010100, 0x2a
opcode "0x30 add ", [5], 0x01, 2, 0x01, 3, 0x30
opcode "0x31 sub ", [1], 0x01, 3, 0x01, 2, 0x31
opcode "0x32 mul ", [6], 0x01, 2, 0x01, 3, 0x32
opcode "0x33 div ", [2], 0x01, 4, 0x01, 2, 0x33
opcode "0x34 mod ", [1], 0x01, 5, 0x01, 2, 0x34
opcode "0x35 shl ", [0b10010000], 0x01, 0b00100100, 0x01, 2, 0x35
opcode "0x36 shr ", [0b00001001], 0x01, 0b00100100, 0x01, 2, 0x36
opcode "0x37 not ", [0b11011011], 0x01, 0b00100100, 0x37
opcode "0x38 and ", [0b00100000], 0x01, 0b00100100, 0x01, 0b11111011, 0x38
opcode "0x39 or ", [0b10110100], 0x01, 0b00100100, 0x01, 0b10010000, 0x39
opcode "0x3a xor ", [0b10110000], 0x01, 0b00100100, 0x01, 0b10010100, 0x3a
end

describe "SECTION: IO Operations (0x4_)" do
withStdin("aabc\ntest") {
opcode "0x30 getc", ['a'.ord], 0x30
opcode "0x31 getl", ["abc"], 0x31
opcode "0x40 getc", ['a'.ord], 0x40
opcode "0x41 getl", ["abc"], 0x41
}

withStdout {
opcode "0x32 putc", [] of SWAny, 0x01, 97, 0x32 {
opcode "0x42 putc", [] of SWAny, 0x01, 97, 0x42 {
Sherwood.stdout.rewind.to_s.should eq("a")
}
}

withStdout {
opcode "0x33 putl", [] of SWAny, [0x09, 0x00, 0x00, 0x00, 14].map(&.to_u8) + "Hello, world!\n".bytes + [0x33_u8] {
opcode "0x43 putl", [] of SWAny, [0x09, 0x00, 0x00, 0x00, 14].map(&.to_u8) + "Hello, world!\n".bytes + [0x43_u8] {
Sherwood.stdout.rewind.to_s.should eq("Hello, world!\n")
}
}
end

describe "SECTION: Type Queries (0x5_)" do
opcode "0x40 null?", [nil, true], 0x00, 0x40
opcode "0x41 byte?", [0x0_u8, true], 0x01, 0x0, 0x41
opcode "0x42 bool?", [false, true], 0x02, 0x0, 0x42
opcode "0x43 i32? ", [0_i32, true], 0x03, 0x0, 0x0, 0x0, 0x0, 0x43
opcode "0x44 i64? ", [0_i64, true], 0x04, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x44
opcode "0x45 u32? ", [0_u32, true], 0x05, 0x0, 0x0, 0x0, 0x0, 0x45
opcode "0x46 u64? ", [0_u64, true], 0x06, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46
opcode "0x47 f32? ", [0.0_f32, true], 0x07, 0x0, 0x0, 0x0, 0x0, 0x47
opcode "0x48 f64? ", [0.0_f64, true], 0x08, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x48
opcode "0x49 str? ", ["", true], 0x09, 0x0, 0x0, 0x0, 0x0, 0x49
opcode "0x4a list?", [[] of SWAny, true], 0x0a, 0x0, 0x0, 0x0, 0x0, 0x4a
end

# TODO: Variable Operations
# TODO: Control Flow
62 changes: 31 additions & 31 deletions src/sherwood.cr
Expand Up @@ -48,48 +48,48 @@ module Sherwood
(size = op.data.map(&.to_u32).bitwiseConcat) > 0 &&
stack.push(Array(SWAny).new(size) { stack.pop }) ||
stack.push([] of SWAny)

# SECTION: Type Queries
when 0x10 then stack.push(stack.last.nil?)
when 0x11 then stack.push(stack.last.is_a?(Byte))
when 0x12 then stack.push(stack.last.is_a?(Bool))
when 0x13 then stack.push(stack.last.is_a?(Int32))
when 0x14 then stack.push(stack.last.is_a?(Int64))
when 0x15 then stack.push(stack.last.is_a?(UInt32))
when 0x16 then stack.push(stack.last.is_a?(UInt64))
when 0x17 then stack.push(stack.last.is_a?(Float32))
when 0x18 then stack.push(stack.last.is_a?(Float64))
when 0x19 then stack.push(stack.last.is_a?(String))
when 0x1a then stack.push(stack.last.is_a?(Array))

# SECTION: Stack Operations
when 0x10 then stack.pop()
when 0x11 then stack.push(stack.last)
when 0x12 then stack.push(stack.pop(), stack.pop())
when 0x20 then stack.pop()
when 0x21 then stack.push(stack.last)
when 0x22 then stack.push(stack.pop(), stack.pop())

# SECTION: Arithmetic Operations
when 0x20 then stack.push(popType(SWNum, stack) + popType(SWNum, stack))
when 0x21 then stack.push((b = popType(SWNum, stack); popType(SWNum, stack)) - b)
when 0x22 then stack.push(popType(SWNum, stack) * popType(SWNum, stack))
when 0x23 then stack.push((b = popType(SWNum, stack); popType(SWNum, stack)) / b)
when 0x24 then stack.push((b = popType(SWInt, stack); popType(SWInt, stack)) % b)
when 0x25 then stack.push((b = popType(SWInt, stack); popType(SWInt, stack)) << b)
when 0x26 then stack.push((b = popType(SWInt, stack); popType(SWInt, stack)) >> b)
when 0x27 then stack.push(~popType(SWInt, stack))
when 0x28 then stack.push(popType(SWInt, stack) & popType(SWInt, stack))
when 0x29 then stack.push(popType(SWInt, stack) | popType(SWInt, stack))
when 0x2a then stack.push(popType(SWInt, stack) ^ popType(SWInt, stack))
when 0x30 then stack.push(popType(SWNum, stack) + popType(SWNum, stack))
when 0x31 then stack.push((b = popType(SWNum, stack); popType(SWNum, stack)) - b)
when 0x32 then stack.push(popType(SWNum, stack) * popType(SWNum, stack))
when 0x33 then stack.push((b = popType(SWNum, stack); popType(SWNum, stack)) / b)
when 0x34 then stack.push((b = popType(SWInt, stack); popType(SWInt, stack)) % b)
when 0x35 then stack.push((b = popType(SWInt, stack); popType(SWInt, stack)) << b)
when 0x36 then stack.push((b = popType(SWInt, stack); popType(SWInt, stack)) >> b)
when 0x37 then stack.push(~popType(SWInt, stack))
when 0x38 then stack.push(popType(SWInt, stack) & popType(SWInt, stack))
when 0x39 then stack.push(popType(SWInt, stack) | popType(SWInt, stack))
when 0x3a then stack.push(popType(SWInt, stack) ^ popType(SWInt, stack))

# SECTION: IO Operations
when 0x30 then
when 0x40 then
if (csi = @@stdin).is_a?(IO::FileDescriptor) && csi.tty?
stack.push(csi.raw &.read_char.try(&.ord))
else
stack.push(csi.read_char.try(&.ord))
end
when 0x31 then stack.push(@@stdin.gets)
when 0x32 then @@stdout.print popType(SWInt, stack).chr
when 0x33 then @@stdout.print popType(String, stack)

# SECTION: Type Queries
when 0x40 then stack.push(stack.last.nil?)
when 0x41 then stack.push(stack.last.is_a?(Byte))
when 0x42 then stack.push(stack.last.is_a?(Bool))
when 0x43 then stack.push(stack.last.is_a?(Int32))
when 0x44 then stack.push(stack.last.is_a?(Int64))
when 0x45 then stack.push(stack.last.is_a?(UInt32))
when 0x46 then stack.push(stack.last.is_a?(UInt64))
when 0x47 then stack.push(stack.last.is_a?(Float32))
when 0x48 then stack.push(stack.last.is_a?(Float64))
when 0x49 then stack.push(stack.last.is_a?(String))
when 0x4a then stack.push(stack.last.is_a?(Array))
when 0x41 then stack.push(@@stdin.gets)
when 0x42 then @@stdout.print popType(SWInt, stack).chr
when 0x43 then @@stdout.print popType(String, stack)

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

0 comments on commit 311022f

Please sign in to comment.