Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extract scope into itw own file

  • Loading branch information...
commit 0676ee6a00887c6fde52aa603d39644f39bbb9dc 1 parent c9151b9
@txus authored
Showing with 67 additions and 62 deletions.
  1. +2 −62 lib/lambra/syntax/ast.rb
  2. +65 −0 lib/lambra/syntax/scope.rb
View
64 lib/lambra/syntax/ast.rb
@@ -1,67 +1,7 @@
+require_relative 'scope'
+
module Lambra
module AST
- module Scope
- attr_accessor :parent
-
- def self.included(base)
- base.send :include, Rubinius::Compiler::LocalVariables
- end
-
- def nest_scope(scope)
- scope.parent = self
- end
-
- # A nested scope is looking up a local variable. If the variable exists
- # in our local variables hash, return a nested reference to it. If it
- # exists in an enclosing scope, increment the depth of the reference
- # when it passes through this nested scope (i.e. the depth of a
- # reference is a function of the nested scopes it passes through from
- # the scope it is defined in to the scope it is used in).
- def search_local(name)
- if variable = variables[name]
- variable.nested_reference
- elsif block_local?(name)
- new_local name
- elsif reference = @parent.search_local(name)
- reference.depth += 1
- reference
- end
- end
-
- def block_local?(name)
- @locals.include?(name) if @locals
- end
-
- def new_local(name)
- variable = Rubinius::Compiler::LocalVariable.new allocate_slot
- variables[name] = variable
- end
-
- def new_nested_local(name)
- new_local(name).nested_reference
- end
-
- # If the local variable exists in this scope, set the local variable
- # node attribute to a reference to the local variable. If the variable
- # exists in an enclosing scope, set the local variable node attribute to
- # a nested reference to the local variable. Otherwise, create a local
- # variable in this scope and set the local variable node attribute.
- def assign_local_reference(var)
- if variable = variables[var.name]
- var.variable = variable.reference
- elsif block_local?(var.name)
- variable = new_local var.name
- var.variable = variable.reference
- elsif reference = @parent.search_local(var.name)
- reference.depth += 1
- var.variable = reference
- else
- variable = new_local var.name
- var.variable = variable.reference
- end
- end
- end
-
module Visitable
def accept(visitor)
name = self.class.name.split("::").last
View
65 lib/lambra/syntax/scope.rb
@@ -0,0 +1,65 @@
+module Lambra
+ module AST
+ module Scope
+ attr_accessor :parent
+
+ def self.included(base)
+ base.send :include, Rubinius::Compiler::LocalVariables
+ end
+
+ def nest_scope(scope)
+ scope.parent = self
+ end
+
+ # A nested scope is looking up a local variable. If the variable exists
+ # in our local variables hash, return a nested reference to it. If it
+ # exists in an enclosing scope, increment the depth of the reference
+ # when it passes through this nested scope (i.e. the depth of a
+ # reference is a function of the nested scopes it passes through from
+ # the scope it is defined in to the scope it is used in).
+ def search_local(name)
+ if variable = variables[name]
+ variable.nested_reference
+ elsif block_local?(name)
+ new_local name
+ elsif reference = @parent.search_local(name)
+ reference.depth += 1
+ reference
+ end
+ end
+
+ def block_local?(name)
+ @locals.include?(name) if @locals
+ end
+
+ def new_local(name)
+ variable = Rubinius::Compiler::LocalVariable.new allocate_slot
+ variables[name] = variable
+ end
+
+ def new_nested_local(name)
+ new_local(name).nested_reference
+ end
+
+ # If the local variable exists in this scope, set the local variable
+ # node attribute to a reference to the local variable. If the variable
+ # exists in an enclosing scope, set the local variable node attribute to
+ # a nested reference to the local variable. Otherwise, create a local
+ # variable in this scope and set the local variable node attribute.
+ def assign_local_reference(var)
+ if variable = variables[var.name]
+ var.variable = variable.reference
+ elsif block_local?(var.name)
+ variable = new_local var.name
+ var.variable = variable.reference
+ elsif reference = @parent.search_local(var.name)
+ reference.depth += 1
+ var.variable = reference
+ else
+ variable = new_local var.name
+ var.variable = variable.reference
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.