This repository has been archived by the owner on Jan 23, 2018. It is now read-only.
/
lambda.rb
75 lines (63 loc) · 2.05 KB
/
lambda.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module BusScheme
# RecursiveHash is needed to store Lambda environments
class RecursiveHash < Hash
# takes a hash and a parent
def initialize(hash, parent)
@parent = parent
hash.each { |k, v| self[k] = v }
end
alias_method :immediate_has_key?, :has_key?
alias_method :immediate_set, :[]=
alias_method :immediate_lookup, :[]
# Just your regular has stuff, only it takes the parent into account
def has_key?(symbol)
immediate_has_key?(symbol) or @parent && @parent.has_key?(symbol)
end
def [](symbol)
immediate_lookup(symbol) or @parent && @parent[symbol]
end
def []=(symbol, value)
if !immediate_has_key?(symbol) and @parent and @parent.has_key?(symbol)
@parent[symbol] = value
else
immediate_set symbol, value
end
end
end
# Lambdas are closures.
class Lambda
@@stack = []
attr_reader :scope
attr_accessor :file, :line
# create new Lambda object
def initialize(formals, body)
@formals, @body, @enclosing_scope = [formals, body, Lambda.scope]
end
# execute Lambda with given arg_values
def call(*args)
locals = if @formals.is_a? Symbol # rest args
{ @formals => args.to_list }
else # regular arg list
raise BusScheme::ArgumentError if @formals.length != args.length
@formals.zip(args).to_hash
end
@scope = RecursiveHash.new(locals, @enclosing_scope)
@@stack << self
BusScheme.eval(@body.unshift(:begin)).affect { @@stack.pop }
end
# What's the current scope?
def self.scope
@@stack.empty? ? SYMBOL_TABLE : @@stack.last.scope
end
# shorthand for lookup in the currently relevant scope
def self.[](symbol)
self.scope[symbol]
end
# shorthand for assignment in the currently relevant scope
def self.[]=(symbol, val)
val.file = symbol.file if val.respond_to?(:file)
val.line = symbol.line if val.respond_to?(:line)
self.scope[symbol] = val
end
end
end