Class: Refi::MinilangEnv

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

Constant Summary collapse

@@rf_cache =
{}
@@funs =
{
  "prepare" => proc{|env,args|
    env.prepare(args[:n],args[:stmt])
  },
  "run" => proc{|env,args|
    next env.prepare_run(args[:n])
  },
  "set" => proc{|env,args|
    deref = args[:deref] || []
    env.set(args[:n],args[:v],deref)
  },
  "get" => proc{|env,args|
    next env.get(args[:n])
  },
  "get_m" => proc{|env,args|
    next env.get_m(args[:n])
  },
  "get_l" => proc{|env,args|
    next env.get_l(args[:n])
  },
  "get_e" => proc{|env,args|
    next env.get_e(args[:n])
  },
  "insert" => proc{|env,args|
    env.text << args[:v].to_s
  },
  "read_file" => proc{|env,args|
    pn = args[:pn]
    rf = @@rf_cache[pn]
    unless rf
      rf = env.read_file args[:pn].to_s
      @@rf_cache[pn] = rf
    end
    next rf
    #next env.read_file args[:pn].to_s

  },
  "print" => proc{|env,args|
    puts args[:v].to_s
  },
  "pp" => proc{|env,args|
    pp env
  },
  "concat" => proc{|env,args|
    ary = args[:ary]
    concat_str = ary.inject(''){|memo,i|
      memo << i
    }
    next concat_str
  },
  "get_export_type" => proc{|env,args|
    next env.get_export_type
  },
  "get_keys" => proc{|env,args|
    m = args[:map]
    res = m.keys
    next res
  },
  "regex_select" => proc{|env,args|
    from = args[:from]
    where = args[:where]
    res = []
    from.each{|e|
      if e.match(where) 
        res << e
      end
    }
    next res
  },
  "group" => proc{|env,args|
    ary = args[:ary].dup
    ary_size = ary.size
    amount = args[:amount]
    fill = args[:fill]
    res = []
    while !ary.empty?
      res.push ary.shift(amount)
    end
    if fill
      fills = ary_size % amount
      fills.times{
        res[-1].push(fill)
      }
    end
    next res
  },
  "loop" => proc{|env,args|
    l_var_name = args[:l_var]
    ary = args[:ary].dup
    env.create_loop(l_var_name,ary)
    next nil
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(creator, p) ⇒ MinilangEnv

Returns a new instance of MinilangEnv.



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/minilang_refi.rb', line 131

def initialize(creator,p)
  @creator = creator
  @export_type = p[:export_type] || $Log.tlog({id: 'err1030'})
  #dperr( 'E777', 'export type not passed to minilang env init')

  @text = ''
  @vars = {} # local

  @vars_m = {} # var_map

  @vars_l = {} # loop

  @vars_e = {} # each

  @iter_vars = {}
  @prep_stmts = {}
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



130
131
132
# File 'lib/minilang_refi.rb', line 130

def text
  @text
end

Instance Method Details

#call_fun(fun_name, fun_args) ⇒ Object



159
160
161
162
# File 'lib/minilang_refi.rb', line 159

def call_fun(fun_name,fun_args)
  res = @@funs[fun_name].call(self,fun_args)
  return res
end

#consume_textObject



143
144
145
146
147
# File 'lib/minilang_refi.rb', line 143

def consume_text
  t = @text
  @text = ''
  return t
end

#create_loop(l_var_name, ary) ⇒ Object



182
183
184
# File 'lib/minilang_refi.rb', line 182

def create_loop(l_var_name,ary)
  @creator.create_loop(l_var_name,ary)
end

#get(name) ⇒ Object



163
164
165
# File 'lib/minilang_refi.rb', line 163

def get name
  return @vars[name]
end

#get_e(name) ⇒ Object



173
174
175
# File 'lib/minilang_refi.rb', line 173

def get_e name
  return @vars_e[name]
end

#get_l(name) ⇒ Object



176
177
178
# File 'lib/minilang_refi.rb', line 176

def get_l name
  return @vars_l[name]
end

#get_m(name) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/minilang_refi.rb', line 166

def get_m name
  unless name
    return @vars_m
  else
    return @vars_m[name]
  end
end

#parse_str(str) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/minilang_refi.rb', line 229

def parse_str str
  parse_res = MinilangScript.new(str)
  if(parse_res.valid)
    return parse_res
  else
    dparn 1223, "invalid string (was not executed)"
    parse_res.report_errors
    return nil
  end
end

#prepare(name, str) ⇒ Object



239
240
241
# File 'lib/minilang_refi.rb', line 239

def prepare(name,str)
  @prep_stmts[name] = parse_str(str)
end

#prepare_run(name) ⇒ Object



242
243
244
# File 'lib/minilang_refi.rb', line 242

def prepare_run(name)
  @prep_stmts[name].run(self)
end

#pretty_print(pp) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/minilang_refi.rb', line 151

def pretty_print(pp)
  puts "<vars>"
  pp @vars
  puts "</vars>"
  puts "<text>"
  pp @text
  puts "</text>"
end

#read_file(pn) ⇒ Object



179
180
181
# File 'lib/minilang_refi.rb', line 179

def read_file pn
  return @creator.read_file pn
end

#run_str(str) ⇒ Object



245
246
247
248
# File 'lib/minilang_refi.rb', line 245

def run_str str
  parse_res = parse_str(str)
  return parse_res.run(self)
end

#set(var_name, var_value, deref) ⇒ Object

else v_e = deref.pop v = @vars_l deref.each{|d| v = v } v = var_value return end



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/minilang_refi.rb', line 213

def set(var_name,var_value,deref)
  if deref == []
    @vars[var_name] = var_value
  else
    v_e = deref.pop
    v = @vars[var_name]
    deref.each{|d|
      v = v[d]
    }
    v[v_e] = var_value
  end
  return nil
end

#set_e(var_name, var_value, deref) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/minilang_refi.rb', line 185

def set_e(var_name,var_value,deref)
  if deref == []
    @vars_e[var_name] = var_value
    return
  else
    v_e = deref.pop
    v = @vars_e[var_name]
    deref.each{|d|
      v = v[d]
    }
    v[v_e] = var_value
    return
  end
end

#set_iter_var(var_ref, val) ⇒ Object



226
227
228
# File 'lib/minilang_refi.rb', line 226

def set_iter_var(var_ref,val)
  @iter_vars[var_ref] = val
end

#set_l(var_name, var_value) ⇒ Object

,deref)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/minilang_refi.rb', line 199

def set_l(var_name,var_value) #,deref)

  #if deref == []

    @vars_l[var_name] = var_value
    return
  #else

  #  v_e = deref.pop

  #  v = @vars_l[var_name]

  #  deref.each{|d|

  #    v = v[d]

  #  }

  #  v[v_e] = var_value

  #  return

  #end

end

#set_var_map(var_map) ⇒ Object



148
149
150
# File 'lib/minilang_refi.rb', line 148

def set_var_map var_map
  @vars_m = var_map
end