Skip to content

Commit

Permalink
Ensure all case are exhaustive
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Nov 2, 2019
1 parent 905fbce commit e057f69
Show file tree
Hide file tree
Showing 93 changed files with 664 additions and 260 deletions.
1 change: 1 addition & 0 deletions samples/brainfuck.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Program
when '.'; print tape.get.chr
when '['; pc = @bracket_map[pc] if tape.get == 0
when ']'; pc = @bracket_map[pc] if tape.get != 0
else # skip
end
pc += 1
end
Expand Down
6 changes: 5 additions & 1 deletion samples/llvm/brainfuck.cr
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Program
program = [] of Instruction
i = from
while i < to
case source[i]
case char = source[i]
when '+'
program << Increment.new(1)
when '-'
Expand All @@ -167,6 +167,8 @@ class Program
i = matching_close_index
when ']'
abort "Unmatched ']' at position #{i}"
else
# skip
end
i += 1
end
Expand All @@ -181,6 +183,8 @@ class Program
open_count += 1
when ']'
open_count -= 1
else
# go on
end

if open_count == 0
Expand Down
14 changes: 10 additions & 4 deletions samples/mt_gc_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,16 @@ def run(threads_num, fibers_num, loops_num, log)
context.log "Done"
end

enum Mode
Run
Ips
Measure
end

threads_num = 4
fibers_num = 1_000
loops_num = 20
mode = :run
mode : Mode = :run

OptionParser.parse do |parser|
parser.on("-i", "--ips", "Benchmark with ips") { mode = :ips }
Expand All @@ -218,12 +224,12 @@ OptionParser.parse do |parser|
end

case mode
when :run
when .run?
run(threads_num, fibers_num, loops_num, true)
when :ips
when .ips?
Benchmark.ips do |x|
x.report("run") { run(threads_num, fibers_num, loops_num, false) }
end
when :measure
when .measure?
puts Benchmark.measure { run(threads_num, fibers_num, loops_num, false) }
end
18 changes: 10 additions & 8 deletions samples/pretty_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,34 @@ class PrettyPrinter

def read_any
case @pull.kind
when :null
when .null?
with_color.bold.surround(@output) do
@pull.read_null.to_json(@output)
end
when :bool
when .bool?
with_color.light_blue.surround(@output) do
@pull.read_bool.to_json(@output)
end
when :int
when .int?
with_color.red.surround(@output) do
@pull.read_int.to_json(@output)
end
when :float
when .float?
with_color.red.surround(@output) do
@pull.read_float.to_json(@output)
end
when :string
when .string?
with_color.yellow.surround(@output) do
@pull.read_string.to_json(@output)
end
when :begin_array
when .begin_array?
read_array
when :begin_object
when .begin_object?
read_object
when :EOF
when .eof?
# We are done
when .end_array?, .end_object?
raise "Bug: shouldn't happen"
end
end

Expand Down
6 changes: 6 additions & 0 deletions samples/sdl/fire.cr
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ while true
speed_down[3] = true
when LibSDL::Key::L
turn_right[3] = true
else
# ignore
end
when LibSDL::KEYUP
case event.key.key_sym.sym
Expand Down Expand Up @@ -421,7 +423,11 @@ while true
speed_down[3] = false
when LibSDL::Key::L
turn_right[3] = false
else
# ignore
end
else
# ignore
end
end

Expand Down
23 changes: 17 additions & 6 deletions samples/sdl/tv.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require "./sdl/sdl"

class ColorMaker
enum State
BlueUp
BlueDown
GreenUp
GreenDown
RedUp
RedDown
end

@state : State

def initialize(@delay : Int32)
@r = 0
@g = 255
Expand All @@ -19,22 +30,22 @@ class ColorMaker

def next_state
case @state
when :green_up
when .green_up?
@g += 1
@state = :red_down if @g == 255
when :red_down
when .red_down?
@r -= 1
@state = :blue_up if @r == 0
when :blue_up
when .blue_up?
@b += 1
@state = :green_down if @b == 255
when :green_down
when .green_down?
@g -= 1
@state = :red_up if @g == 0
when :red_up
when .red_up?
@r += 1
@state = :blue_down if @r == 255
when :blue_down
when .blue_down?
@b -= 1
@state = :green_up if @b == 0
end
Expand Down
6 changes: 6 additions & 0 deletions spec/compiler/semantic/splat_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ describe "Semantic: splat" do
expect_splat "x", 0, 10, 0
when 1
expect_splat "y", 1, 20, 1
else
fail "shouldn't happen"
end
i += 1
end
Expand All @@ -560,6 +562,8 @@ describe "Semantic: splat" do
expect_splat "a1", 0, 10, 0
when 1
expect_splat "a2", 1, 20, 1
else
fail "shouldn't happen"
end
i += 1
end
Expand All @@ -576,6 +580,8 @@ describe "Semantic: splat" do
expect_splat "a3", 2, 50, 4
when 3
expect_splat "a3", 2, 60, 5
else
fail "shouldn't happen"
end
i += 1
end
Expand Down
Loading

0 comments on commit e057f69

Please sign in to comment.