Skip to content

Instantly share code, notes, and snippets.

@russolsen
russolsen / for_calls_each.rb
Created October 3, 2016 18:36
Demo that in Ruby the for expression is just sugar around a call to the each method.
# Demo that for calls each
class ThreeOf
def initialize(value)
@value = value
end
def each(&block)
block.call(@value)
block.call(@value)
require 'oj'
require 'stringio'
require 'json'
class Handler
def hash_start
{}
end
def hash_set(h,k,v)
@russolsen
russolsen / cloforth_repl
Created September 3, 2013 12:29
The key function in cloforth
(defn repl [env]
(let [dictionary (:dictionary env)]
(if (:quit env)
env
(let [r (:in env)
compiled (comp/compile-statement r dictionary)]
(if (and (coll? compiled) (empty? compiled))
env
(let [result (exec/execute-program env compiled) ;; Get the lazy sequence of states
new-env (last result)] ;; ... and then the last one
@russolsen
russolsen / lisp_repl.rb
Created January 26, 2012 20:36
And to finish the thought, a REPL using Fogus' lithp.rb and my own s-expression parser.
require 'lithp'
require 'lisp_parser'
lisp = Lisp.new
print "> "
while not $stdin.eof?
line = readline
s_expression = SExpressionParser.new(line).parse
p lisp.eval(s_expression)
@russolsen
russolsen / lisp_parser.rb
Created January 26, 2012 20:31
An S expression (i.e. LISP) parser in 32 lines of Ruby.
class SExpressionParser
def initialize(expression)
@tokens = expression.scan /[()]|\w+|".*?"|'.*?'/
end
def peek
@tokens.first
end
def next_token