Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created September 24, 2008 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defunkt/12456 to your computer and use it in GitHub Desktop.
Save defunkt/12456 to your computer and use it in GitHub Desktop.
(# examples i used to develop the never-completed Oil)
(# treetop parser is at http://gist.github.com/12454)
(# a simple math class
(= Math (Object.clone
(def square (x) (* x x))
(= square (do (x) (* x x)))
(def abs (x) (
if (>= x 0)
(x)
else
(- 0 x)
))
(def abs (x) (if (>= x 0) (x) (- 0 x)))
(def abs (x) (if (>= x 0) then (x) else (- 0 x)))
))
)
(puts (-3.abs))
(# Math.abs -3) (# 3)
(puts (+ 1 2))
(= a 1)
(= b 2)
(puts (+ a b))
(puts (+ 1 (+ 1 1)))
(= name "chris")
(puts name)
(# recursive nightmare)
(= factorial (do (n) (if (== n 0)
(1)
(else
(puts (slots))
(* n (factorial (- n 1)))
)
)
))
(factorial 5)
(# puts (factorial 5))
(# total => 120)
(# 5 * 24)
(# 4 * 6)
(# 3 * 2)
(# 2 * 1)
(# 1 * 1)
(# 1)
(puts "Hello world!")
(if (== 1 1) (puts "true") (puts "false"))
(= hello (do (
(puts "hello world")
(puts "nicely done")
(puts a)
(puts (stack))
(= s (stack))
(puts s)
)))
(= ret (do (1)))
(puts (+ 2 (ret)))
(puts "----")
(= a (+ 1 1))
(hello)
(# No z yet! Sucks in calling scope but doesn't modify it!)
(= say (do (x y) (
(puts "I want to say...")
(puts x)
(puts y)
(puts z)
)))
(= z "haha!")
(say "hi mom" "you're the best")
(#
# Uncommenting this should raise an exception -
# proof that our lambda isn't bleeding into the calling
# scope.
#)
( # (puts y) )
(def yell (
(puts "I'm gonna yell...")
(puts z)
))
(yell)
(= a (+ 1 1))
(= b (+ 1 2 3 4 5))
(= c (+ 1 (2 * 2)))
(= d (/ 100 10))
(puts a b c d)
(def assert (expected actual) (
(if (!= expected actual)
(then
(puts "Failed!")
(puts "-> Expected: #{expected}")
(puts "-> Actual: #{actual}")
)
(else
(puts "Passed!")
(puts "-> #{actual} was #{expected}")
)
)
))
(= name "chris")
(assert "My name is chris" "My name is #{name}")
(assert "strip me" (" strip me ".strip))
(assert "chomp" ("chomp!".chomp "!"))
(assert "chris wanstrath" ("chris wanstrath".squeeze " "))
(# "chris wanstrath" must equal ("chris wanstrath".squeeze " "))
(assert "chris wanstrath" ("chris" + " wanstrath"))
(# what happens here?)
(# name << " wanstrath")
(# assert "chris wanstrath" name)
(# what gets returned here?)
(# puts ("chris sammy".scan "s"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment