Module: Whitespace::Util

Defined in:
lib/whitespace/util.rb

Constant Summary collapse

BINOPS =
{
  add: lambda { |l, r| l + r },
  sub: lambda { |l, r| l - r },
  mul: lambda { |l, r| l * r },
  div: lambda { |l, r| l / r },
  mod: lambda { |l, r| l % r }
}

Class Method Summary collapse

Class Method Details

.find_label(instructions, name) ⇒ Object

Raises:



20
21
22
23
24
25
26
# File 'lib/whitespace/util.rb', line 20

def find_label(instructions, name)
  instructions.each_with_index do |instr, i|
    return i if instr.instance_of?(ISA::Label) && instr.name == name
  end

  raise LabelError, "missing: \"#{name}\""
end

.is_ascii?(n) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/whitespace/util.rb', line 8

def is_ascii?(n)
  n == 10 || n == 13 || (n >= 32 && n <= 127)
end

.is_binop?(op) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/whitespace/util.rb', line 12

def is_binop?(op)
  BINOPS.include? op
end

.is_integer?(n) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/whitespace/util.rb', line 4

def is_integer?(n)
  n.is_a? Integer
end

.is_label?(name) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/whitespace/util.rb', line 16

def is_label?(name)
  name.instance_of?(String) && !/\A[ \t]+\z/.match(name).nil?
end