Skip to content
Browse files

cleanup

  • Loading branch information...
1 parent 821983d commit 997aaab0a12cdfdd29e10c88ae7f124de95b29de @txus committed Feb 13, 2014
Showing with 45 additions and 19 deletions.
  1. +5 −2 bin/lambra
  2. +2 −1 lib/lambra/bootstrap.rb
  3. +12 −4 lib/lambra/bytecode_compiler.rb
  4. +6 −7 lib/lambra/library/code_loader.rb
  5. +12 −3 lib/lambra/process.rb
  6. +8 −2 spec/compiler_spec.rb
View
7 bin/lambra
@@ -106,6 +106,8 @@ class Lambra::Script
load_history
start_prompt
+ env = Object.new
+
begin
snippet = ""
while str = Readline.readline(prompt)
@@ -114,14 +116,15 @@ class Lambra::Script
snippet << str
begin
- value = Lambra::CodeLoader.evaluate snippet
+ value = Lambra::CodeLoader.evaluate snippet, env
p value
record_history snippet
snippet = ""
start_prompt
rescue => e
- STDERR.puts e
+ snippet = ""
+ STDERR.puts "Error: #{e}"
end
end
ensure
View
3 lib/lambra/bootstrap.rb
@@ -57,7 +57,8 @@ module GlobalScope
:- => PrimitiveFunction.new { |*args| args.inject(:-) },
:/ => PrimitiveFunction.new { |a, b| a / b },
:* => PrimitiveFunction.new { |a, b| a * b },
- :send => PrimitiveFunction.new { |pid, *args| Process[pid].push(*args) }
+ :send => PrimitiveFunction.new { |pid, *args| Process[pid].push(*args) },
+ :sleep => PrimitiveFunction.new { |seconds| sleep(seconds) }
})
class Keyword
View
16 lib/lambra/bytecode_compiler.rb
@@ -4,13 +4,13 @@ class BytecodeCompiler
alias g generator
SPECIAL_FORMS = %w(def fn let spawn)
- PRIMITIVE_FORMS = %w(println + - / *)
+ PRIMITIVE_FORMS = %w(println + - / * send sleep)
def initialize(generator=nil)
@generator = generator || RBX::Generator.new
end
- def compile(ast, debugging=false)
+ def compile(ast, variables=nil, debugging=false)
if debugging
require 'pp'
pp ast.to_sexp
@@ -25,7 +25,11 @@ def compile(ast, debugging=false)
line = ast.line || 1
g.set_line line
- g.push_state RBX::AST::ClosedScope.new(line)
+ if variables
+ g.push_state variables
+ else
+ g.push_state RBX::AST::ClosedScope.new(line)
+ end
ast.accept(self)
@@ -148,7 +152,11 @@ def visit_PrimitiveForm(car, cdr)
def visit_Symbol(o)
set_line(o)
local = g.state.scope.search_local(o.name)
- local.get_bytecode(g)
+ if local
+ local.get_bytecode(g)
+ else
+ raise "unbound symbol #{o.name}"
+ end
end
def visit_Number(o)
View
13 lib/lambra/library/code_loader.rb
@@ -2,26 +2,25 @@
module Lambra
class CodeLoader
- def self.evaluate(string)
+ def self.evaluate(string, env=GlobalScope)
ast = Lambra::Parser.parse string
- execute(ast)
+ execute(ast, env)
end
- def self.execute(ast)
+ def self.execute(ast, env=GlobalScope)
+ binding = env.send(:binding)
visitor = BytecodeCompiler.new
- gen = visitor.compile(ast)
+ gen = visitor.compile(ast, binding.variables)
gen.encode
cm = gen.package Rubinius::CompiledCode
- env = GlobalScope
-
file = if ast.respond_to?(:filename) && ast.filename
ast.filename
else
'(eval)'
end
- line, binding, instance = ast.line, env.send(:binding), env
+ line, binding, instance = ast.line, binding, env
# cm = Noscript::Compiler.compile_eval(code, binding.variables, file, line)
cm.scope = Rubinius::ConstantScope.new(GlobalScope)
View
15 lib/lambra/process.rb
@@ -10,6 +10,10 @@ def initialize(*args)
class Process
@processes = {}
+ def self.pids
+ @processes.keys
+ end
+
def self.[](pid)
@processes[pid]
end
@@ -18,13 +22,18 @@ def self.[]=(pid, process)
@processes[pid] = process
end
+ def self.count
+ @processes.count
+ end
+
def self.spawn(&fn)
- Thread.new do
+ Thread.new {
new(&fn).tap { |process|
- self[Thread.current.object_id] = process
+ pid = Thread.current.object_id
+ self[pid] = process
Thread.current[:process] = process
}.call
- end
+ }.object_id
end
def initialize(&fn)
View
10 spec/compiler_spec.rb
@@ -16,7 +16,7 @@
'(def x 42) x'.should eval_to 42
end
- describe '#fn' do
+ describe 'fn' do
it 'simply works' do
'((fn [x] (* x x)) 3)'.should eval_to 9
end
@@ -26,9 +26,15 @@
end
end
- describe '#let' do
+ describe 'let' do
it 'simply works' do
'(let [x 2 y 3] (* x y))'.should eval_to 6
end
end
+
+ describe 'spawn' do
+ it 'spawns a new process' do
+ '(spawn (fn [] (sleep 10)))'.should eval_to(Lambra::Process.pids)
+ end
+ end
end

0 comments on commit 997aaab

Please sign in to comment.
Something went wrong with that request. Please try again.