Skip to content

Commit

Permalink
Make s prefix read current cell by default
Browse files Browse the repository at this point in the history
  • Loading branch information
daniero committed May 27, 2017
1 parent 3ba84dd commit a305c3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
4 changes: 3 additions & 1 deletion lib/braingasm/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def zero
end

def signed
push_prefix ->(m) { m.last_write >= 0 ? 0 : 1 }
read_cell if @prefixes.empty?

push_prefix @prefixes.fix_params(->(n, m) { n < 0 ? 1 : 0 })
end

def parity
Expand Down
24 changes: 0 additions & 24 deletions spec/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,29 +228,5 @@ module Braingasm
end
end

describe "#signed" do
before { allow(machine).to receive(:last_write).and_return(0) }
include_examples "generated prefix", :signed
include_examples "simple instruction generation", :signed, :last_write

it "returns 1 if machine's last write instruction is negative (signed)" do
expect(machine).to receive(:last_write).and_return(-1)

expect(subject.signed.call(machine)).to be 1
end

it "returns 0 if machine's last write instruction is 0 (unsigned)" do
expect(machine).to receive(:last_write).and_return(0)

expect(subject.signed.call(machine)).to be 0
end

it "returns 0 if machine's last write instruction is positive (unsigned)" do
expect(machine).to receive(:last_write).and_return(1)

expect(subject.signed.call(machine)).to be 0
end
end

end
end
20 changes: 18 additions & 2 deletions spec/features/prefixes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,27 @@

describe "s" do
context "without prefix" do
it "returns 1 if the current cell is negative (signed), 0 otherwise"
it "returns 1 if the current cell is negative (signed), 0 otherwise" do
@machine.tape = [-2, -1, 0, 1, 2]

run "s, > s, > s, > s, > s,"

expect(@machine.tape).to be == [1, 1, 0, 0, 0]
end
end

context "with integer prefix" do
it "checks the prefix instead"
it "returns 0 if the given prefix is zero or positive" do
run "#s, > #s,"

expect(@machine.tape[0..1]).to be == [0, 0]
end

it "returns 1 if the given prefix is negative" do
run "< #s,"

expect(@machine.cell).to be == 1
end
end
end

Expand Down

0 comments on commit a305c3b

Please sign in to comment.