Class: NScript::RangeNode

Inherits:
Node
  • Object
show all
Defined in:
lib/nscript/parser/nodes.rb

Constant Summary

Constants inherited from Node

Node::TAB

Instance Method Summary collapse

Methods inherited from Node

#children, children, #compile, #compile_closure, #contains?, #idt, statement, #statement?, statement_only, #statement_only?, top_sensitive, #top_sensitive?, #unwrap, #write

Constructor Details

#initialize(from, to, exclusive = false) ⇒ RangeNode

Returns a new instance of RangeNode.



384
385
386
# File 'lib/nscript/parser/nodes.rb', line 384

def initialize(from, to, exclusive=false)
  @from, @to, @exclusive = from, to, exclusive
end

Instance Method Details

#compile_array(o) ⇒ Object



410
411
412
413
414
# File 'lib/nscript/parser/nodes.rb', line 410

def compile_array(o)
  body = Expressions.wrap(LiteralNode.wrap('i'))
  arr  = Expressions.wrap(ForNode.new(body, {:source => ValueNode.new(self)}, Value.new('i')))
  ParentheticalNode.new(CallNode.new(CodeNode.new([], arr))).compile(o)
end

#compile_node(o) ⇒ Object



399
400
401
402
403
404
405
406
407
408
# File 'lib/nscript/parser/nodes.rb', line 399

def compile_node(o)
  return compile_array(o) unless o[:index]
  idx, step = o.delete(:index), o.delete(:step)
  vars      = "#{idx}=#{@from_var}"
  step      = step ? step.compile(o) : '1'
  equals    = @exclusive ? '' : '='
  compare   = "(#{@from_var} <= #{@to_var} ? #{idx} <#{equals} #{@to_var} : #{idx} >#{equals} #{@to_var})"
  incr      = "(#{@from_var} <= #{@to_var} ? #{idx} += #{step} : #{idx} -= #{step})"
  write("#{vars}; #{compare}; #{incr}")
end

#compile_variables(o) ⇒ Object



392
393
394
395
396
397
# File 'lib/nscript/parser/nodes.rb', line 392

def compile_variables(o)
  @indent = o[:indent]
  @from_var, @to_var = o[:scope].free_variable, o[:scope].free_variable
  from_val,  to_val  = @from.compile(o), @to.compile(o)
  write("#{@from_var} = #{from_val}; #{@to_var} = #{to_val};\n#{idt}")
end

#exclusive?Boolean

Returns:

  • (Boolean)


388
389
390
# File 'lib/nscript/parser/nodes.rb', line 388

def exclusive?
  @exclusive
end