Skip to content

Instantly share code, notes, and snippets.

View desireco's full-sized avatar
💭
Working /w AI on AI

Zeljko Dakic desireco

💭
Working /w AI on AI
View GitHub Profile
@desireco
desireco / forth.rb
Created April 9, 2016 21:50 — forked from deadprogram/forth.rb
Forth interpreter in 64 lines of Ruby
#!/usr/bin/env ruby
def pop; $stack.pop || raise(StackUnderflow); end
def push(expression); $stack << expression; end
def unary; -> { push(yield pop) }; end
def binary; -> { push(yield pop, pop) }; end
def unary_boolean; -> { push(if yield pop then 1 else 0 end) }; end
def binary_boolean; -> { push(if yield pop, pop then 1 else 0 end) }; end
def swap; $stack[-2,2] = $stack[-2,2].reverse; end
$stack = []