Class: ZunScript::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/zunscript/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug: false) ⇒ Environment

Returns a new instance of Environment.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zunscript/environment.rb', line 74

def initialize debug: false
  @rootscope = ZRootScope.new
  @debug = debug

  crutch = Crutch.new
  @rootscope.set "print", ZExternBlock.new( crutch, :print_, self )
  @rootscope.set "puts", ZExternBlock.new( crutch, :puts_, self )
  @rootscope.set "add", ZExternBlock.new( crutch, :add, self )
  @rootscope.set "multiply", ZExternBlock.new( crutch, :multiply, self )
  @rootscope.set "for", ZExternBlock.new( crutch, :range, self )
  @rootscope.set "if", ZExternBlock.new( crutch, :if, self )
end

Instance Attribute Details

#rootscopeObject (readonly)

Returns the value of attribute rootscope.



73
74
75
# File 'lib/zunscript/environment.rb', line 73

def rootscope
  @rootscope
end

Instance Method Details

#resolve(node, scope = @rootscope) ⇒ Object



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
# File 'lib/zunscript/environment.rb', line 38

def resolve node, scope = @rootscope
  puts "#{node.line}:  #{node.inspect}".colorize :yellow if @debug
  case node.name
  when "INVOKE"
    block = resolve node[0], scope
    block = block.get if block.is_a? VariableReference
    throw "trying to invoke non-block: #{block.inspect} (#{node.line})" if not block.is_a? ZBlock

    return block.call (node[1..-1].map do |c| resolve c, scope end)
  when "BIND"
    block = resolve node[0], scope
    block = block.get if block.is_a? VariableReference

    throw "trying to bind non-block: #{block.inspect} (#{node.line})" if not block.is_a? ZBlock
    return block.bind (node[1..-1].map do |c| resolve c, scope end)
  when "ASSIGN"
    lval = resolve node[0]
    throw "invalid LVALUE in assignment (#{node.line})" if not lval.is_a? VariableReference

    return lval.assign (resolve node[1])
  when "NUM"
    return ZNum.new node[0].text.to_f, self
  when "STR"
    return ZString.new node[0].text[1..-2], self
  when "BOOL"
    return ZBool.new node[0].text == "true", self
  when "BLK"
    return ZBlock.new node[-1], node[0..-2], scope, self
  when "VARIABLE"
    x = node[0]
   # puts "#{x.inspect}, #{node.inspect}"
    return scope.find x.text
  end
end

#run_line(string) ⇒ Object



100
101
102
# File 'lib/zunscript/environment.rb', line 100

def run_line string
  resolve ZunScript::Parser.new(string).line.tree
end

#run_script(script) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zunscript/environment.rb', line 87

def run_script script
  tree = (ZunScript::Parser.new script).script.tree
  if tree.type == 0 then
    tree.each do |n|
      res = resolve n
      yield res if block_given?
    end
  else
    res = interpret tree
    yield res if block_given?
  end
end