forked from tomstuart/nothing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'nothing' | ||
require 'support/pair' | ||
require 'support/helpers' | ||
require 'support/matchers' | ||
|
||
RSpec.configure do |c| | ||
c.include Helpers | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require 'nothing' | ||
require 'support/pair' | ||
|
||
module Nothing | ||
module FFI | ||
def to_integer(n) | ||
TIMES[n][-> integer { integer + 1 }][0] | ||
end | ||
|
||
def from_integer(integer) | ||
n = ZERO | ||
|
||
integer.times do | ||
n = INCREMENT[n] | ||
end | ||
|
||
n | ||
end | ||
|
||
def to_boolean(b) | ||
IF[b][true][false] | ||
end | ||
|
||
def from_boolean(boolean) | ||
if boolean then TRUE else FALSE end | ||
end | ||
|
||
def to_pair(p) | ||
Pair.new(LEFT[p], RIGHT[p]) | ||
end | ||
|
||
def from_pair(pair) | ||
PAIR[pair.left][pair.right] | ||
end | ||
|
||
def to_array(l) | ||
array = [] | ||
|
||
until to_boolean(IS_EMPTY[l]) | ||
array.push(FIRST[l]) | ||
l = REST[l] | ||
end | ||
|
||
array | ||
end | ||
|
||
def from_array(array) | ||
l = EMPTY | ||
|
||
until array.empty? | ||
l = UNSHIFT[l][array.last] | ||
array.pop | ||
end | ||
|
||
l | ||
end | ||
|
||
CHARSET = '0123456789BFiuz'.chars.entries # for encoding digits, "Fizz" and "Buzz" | ||
|
||
def to_char(c) | ||
CHARSET.at(to_integer(c)) | ||
end | ||
|
||
def from_char(char) | ||
from_integer(CHARSET.index(char)) | ||
end | ||
|
||
def to_string(s) | ||
to_array(s).map { |c| to_char(c) }.join | ||
end | ||
|
||
def from_string(string) | ||
from_array(string.chars.map { |char| from_char(char) }) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'support/ffi' | ||
require 'support/pair' | ||
|
||
module Helpers | ||
include Nothing::FFI | ||
|
||
def representation_of(value) | ||
case value | ||
when Integer | ||
from_integer value | ||
when TrueClass, FalseClass | ||
from_boolean value | ||
when Pair | ||
pair = Pair.new(representation_of(value.left), representation_of(value.right)) | ||
from_pair pair | ||
when Array | ||
array = value.map { |v| representation_of v } | ||
from_array array | ||
when String | ||
from_string value | ||
else | ||
fail "don't know how to represent #{value.inspect}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'support/ffi' | ||
|
||
RSpec::Matchers.define :represent do |value| | ||
include Nothing::FFI | ||
|
||
def represents?(representation, value) | ||
case value | ||
when Integer | ||
to_integer(representation) == value | ||
when TrueClass, FalseClass | ||
to_boolean(representation) == value | ||
when Pair | ||
pair = to_pair(representation) | ||
represents?(pair.left, value.left) && represents?(pair.right, value.right) | ||
when Array | ||
array = to_array(representation) | ||
(array.length == value.length) && array.zip(value).all? { |r, v| represents?(r, v) } | ||
when String | ||
to_string(representation) == value | ||
else | ||
fail "don't know how to represent #{value.inspect}" | ||
end | ||
end | ||
|
||
match do |representation| | ||
represents?(representation, value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Pair = Struct.new :left, :right |