class Microplane::Lib::Std

Standard library of functions.

Public Class Methods

load_words(base) click to toggle source

DICTIONARY = .freeze

# 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