Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Whoa; I just implemented quasiquote. That was way too easy.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Nov 8, 2008
1 parent e1ec4b8 commit 2eda0f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
12 changes: 12 additions & 0 deletions lib/bus_scheme/eval.rb
Expand Up @@ -55,4 +55,16 @@ def stacktrace
def stack
@@stack
end

# TODO: unquote-splicing
def quasiquote(arg)
if !arg.is_a? Cons or arg.empty?
arg
elsif arg.is_a? Cons and arg.car == 'unquote'.sym
eval(arg.cadr)
else
Cons.new(quasiquote(arg.car),
quasiquote(arg.cdr))
end
end
end
7 changes: 2 additions & 5 deletions lib/bus_scheme/primitives.rb
Expand Up @@ -43,11 +43,8 @@ def self.special_form(identifier, value)
define 'exit', primitive { exit }
define 'quit', BusScheme['exit'.sym]


# TODO: write
special_form 'quasiquote', primitive { }
special_form 'unquote', primitive { }
special_form 'unquote-splicing', primitive { }
special_form 'quasiquote', primitive { |arg| quasiquote(arg) }
special_form 'qq', BusScheme['quasiquote'.sym]

# Primitives that can't be defined in terms of other forms:
special_form 'quote', primitive { |arg| arg }
Expand Down
5 changes: 5 additions & 0 deletions test/test_eval.rb
Expand Up @@ -87,6 +87,11 @@ def test_evals_string_ending_in_comment
;; should be four"
end

def test_quasi_quote
assert_evals_to([:hey.sym, :there.sym, "dude"].to_list,
'(qq (hey there (unquote (+ "du" "de"))))')
end

# def test_tail_call_optimization
# Timeout.timeout(1) do
# assert_nothing_raised { eval "((lambda (x) (x x)) (lambda (x) (x x)))" }
Expand Down

0 comments on commit 2eda0f6

Please sign in to comment.