diff options
Diffstat (limited to 'ruby')
| -rw-r--r-- | ruby/core.rb | 14 | ||||
| -rw-r--r-- | ruby/step3_env.rb | 9 | ||||
| -rw-r--r-- | ruby/step4_if_fn_do.rb | 8 | ||||
| -rw-r--r-- | ruby/step5_tco.rb | 8 | ||||
| -rw-r--r-- | ruby/step6_file.rb | 12 | ||||
| -rw-r--r-- | ruby/step7_quote.rb | 12 | ||||
| -rw-r--r-- | ruby/step8_macros.rb | 14 | ||||
| -rw-r--r-- | ruby/step9_interop.rb | 14 | ||||
| -rw-r--r-- | ruby/stepA_more.rb | 15 |
9 files changed, 48 insertions, 58 deletions
diff --git a/ruby/core.rb b/ruby/core.rb index 8823112..c7fc8c3 100644 --- a/ruby/core.rb +++ b/ruby/core.rb @@ -1,3 +1,7 @@ +require "readline" +require "reader" +require "printer" + $core_ns = { :"=" => lambda {|a,b| a == b}, :throw => lambda {|a| raise MalException.new(a), "Mal Exception"}, @@ -6,10 +10,14 @@ $core_ns = { :false? => lambda {|a| a == false}, :symbol? => lambda {|a| a.is_a? Symbol}, :symbol? => lambda {|a| a.is_a? Symbol}, + :"pr-str" => lambda {|*a| a.map {|e| _pr_str(e, true)}.join(" ")}, - :"str" => lambda {|*a| a.map {|e| _pr_str(e, false)}.join("")}, - :"prn" => lambda {|*a| puts(a.map {|e| _pr_str(e, true)}.join(" "))}, - :"println" => lambda {|*a| puts(a.map {|e| _pr_str(e, false)}.join(" "))}, + :str => lambda {|*a| a.map {|e| _pr_str(e, false)}.join("")}, + :prn => lambda {|*a| puts(a.map {|e| _pr_str(e, true)}.join(" "))}, + :println => lambda {|*a| puts(a.map {|e| _pr_str(e, false)}.join(" "))}, + :readline => lambda {|a| Readline.readline(a,true)}, + :"read-string" => lambda {|a| read_str(a)}, + :slurp => lambda {|a| File.read(a)}, :< => lambda {|a,b| a < b}, :<= => lambda {|a,b| a <= b}, :> => lambda {|a,b| a > b}, diff --git a/ruby/step3_env.rb b/ruby/step3_env.rb index ee80432..7266424 100644 --- a/ruby/step3_env.rb +++ b/ruby/step3_env.rb @@ -60,12 +60,11 @@ end # repl repl_env = Env.new REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -_ref[:+, lambda {|a,b| a + b}] -_ref[:-, lambda {|a,b| a - b}] -_ref[:*, lambda {|a,b| a * b}] -_ref[:/, lambda {|a,b| a / b}] +repl_env.set(:+, lambda {|a,b| a + b}) +repl_env.set(:-, lambda {|a,b| a - b}) +repl_env.set(:*, lambda {|a,b| a * b}) +repl_env.set(:/, lambda {|a,b| a / b}) while line = Readline.readline("user> ", true) begin diff --git a/ruby/step4_if_fn_do.rb b/ruby/step4_if_fn_do.rb index e99ed6c..559f81e 100644 --- a/ruby/step4_if_fn_do.rb +++ b/ruby/step4_if_fn_do.rb @@ -77,12 +77,12 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] while line = Readline.readline("user> ", true) diff --git a/ruby/step5_tco.rb b/ruby/step5_tco.rb index 0b28a09..c6b5782 100644 --- a/ruby/step5_tco.rb +++ b/ruby/step5_tco.rb @@ -86,12 +86,12 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] while line = Readline.readline("user> ", true) diff --git a/ruby/step6_file.rb b/ruby/step6_file.rb index 5425eac..6ca0d6d 100644 --- a/ruby/step6_file.rb +++ b/ruby/step6_file.rb @@ -86,16 +86,12 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -_ref[:"read-string", lambda {|str| read_str str}] -_ref[:eval, lambda {|ast| EVAL(ast, repl_env)}] -_ref[:slurp, lambda {|f| File.read(f) }] - -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] diff --git a/ruby/step7_quote.rb b/ruby/step7_quote.rb index a39b0ce..78ef117 100644 --- a/ruby/step7_quote.rb +++ b/ruby/step7_quote.rb @@ -106,16 +106,12 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -_ref[:"read-string", lambda {|str| read_str str}] -_ref[:eval, lambda {|ast| EVAL(ast, repl_env)}] -_ref[:slurp, lambda {|f| File.read(f) }] - -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] diff --git a/ruby/step8_macros.rb b/ruby/step8_macros.rb index 36f2a0d..db34159 100644 --- a/ruby/step8_macros.rb +++ b/ruby/step8_macros.rb @@ -131,18 +131,16 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -_ref[:"read-string", lambda {|str| read_str str}] -_ref[:eval, lambda {|ast| EVAL(ast, repl_env)}] -_ref[:slurp, lambda {|f| File.read(f) }] - -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] +RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"] +RE["(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"] if ARGV.size > 0 ARGV.each {|f| diff --git a/ruby/step9_interop.rb b/ruby/step9_interop.rb index 869a86d..ba35146 100644 --- a/ruby/step9_interop.rb +++ b/ruby/step9_interop.rb @@ -133,18 +133,16 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -_ref[:"read-string", lambda {|str| read_str str}] -_ref[:eval, lambda {|ast| EVAL(ast, repl_env)}] -_ref[:slurp, lambda {|f| File.read(f) }] - -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] +RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"] +RE["(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"] if ARGV.size > 0 ARGV.each {|f| diff --git a/ruby/stepA_more.rb b/ruby/stepA_more.rb index 974b3c7..42eac5b 100644 --- a/ruby/stepA_more.rb +++ b/ruby/stepA_more.rb @@ -148,21 +148,16 @@ end repl_env = Env.new RE = lambda {|str| EVAL(READ(str), repl_env) } REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) } -_ref = lambda {|k,v| repl_env.set(k, v) } -# Import core functions -$core_ns.each &_ref +# core.rb: defined using ruby +$core_ns.each do |k,v| repl_env.set(k,v) end +repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)}) -_ref[:"readline", lambda {|prompt| Readline.readline(prompt,true)}] -_ref[:"read-string", lambda {|str| read_str str}] -_ref[:eval, lambda {|ast| EVAL(ast, repl_env)}] -_ref[:slurp, lambda {|f| File.read(f) }] - -# Defined using the language itself +# core.mal: defined using the language itself RE["(def! not (fn* (a) (if a false true)))"] +RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"] RE["(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"] -RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"] if ARGV.size > 0 ARGV.each {|f| |
