Class: Microplane::Lib::Std

Inherits:
Object
  • Object
show all
Defined in:
lib/microplane/lib/std.rb

Overview

Standard library of functions.

Class Method Summary collapse

Class Method Details

.load_words(base) ⇒ Object

DICTIONARY = .freeze



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/microplane/lib/std.rb', line 9

def self.load_words(base)
  base.instance_eval do
    @dictionary.merge!(
      '+' => -> { push(pop + pop) },
      '-' => -> { push(pop - pop) },
      '*' => -> { push(pop * pop) },
      '/' => -> { push(pop / pop) },
      '%' => -> { push(pop % pop) },
      '<' => -> { push(pop < pop) },
      '<=' => -> { push(pop <= pop) },
      '=' => -> { push(pop == pop) },
      '>=' => -> { push(pop >= pop) },
      '>' => -> { push(pop > pop) },
      'true' => -> { push true },
      'false' => -> { push false },
      '|' => -> { push(pop || pop) },
      'not' => -> { push(!pop) },
      'neg' => -> { push(-pop) },
      'pop' => -> { pop },
      'if' => -> { @skip = true },
      'fi' => -> { @skip = false },
      'dup' => lambda do
        popped = pop
        push(popped)
        push(popped)
      end,
      'over' => lambda do
        first = pop
        second = pop
        push(first)
        push(second)
      end
    )
  end
end