Class: PHP::Generator

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/php/generator.rb

Class Method Summary

Instance Method Summary

Constructor Details

- (Generator) initialize

A new instance of Generator



32
33
34
35
36
37
38
# File 'lib/php/generator.rb', line 32

def initialize
  super
  self.strict          = true
  self.auto_shift_type = true
  self.require_empty   = false
  self.expected        = Node
end

Class Method Details

+ (Object) process(input, options = {}) + (Object) process(options = {}, &block) { ... }

Overloads:

  • + (Object) process(input, options = {})

    Parameters:

    • (String) input
    • (Hash{Symbol => Object}) options
  • + (Object) process(options = {}, &block) { ... }

    Parameters:

    • (Hash{Symbol => Object}) options

    Yields:

Returns:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/php/generator.rb', line 19

def self.process(input = nil, options = {}, &block)
  if block_given?
    # The return value from #to_sexp will be in the format:
    # `s(:iter, s(:call, nil, :proc, s(:arglist)), nil, s(...))`
    input = block.to_sexp.last.to_a
  else
    #input = ParseTree.translate(input)
    input = RubyParser.new.process(input).to_a
  end
  input.size > 0 ? self.new.process(input) : nil
end

Instance Method Details

- (Operator::Logical::And) process_and(exp)

Processes [:and, lhs, rhs] expressions.

Examples:

  process([:and, [:true], [:false]])

Parameters:

  • (Array(Array, Array)) exp

Returns:



468
469
470
# File 'lib/php/generator.rb', line 468

def process_and(exp)
  Operator::Logical::And.new(process(exp.shift), process(exp.shift))
end

- (Node) process_arglist(exp)

Processes [:arglist, ...] expressions.

Examples:

  process([:arglist])
  process([:arglist, [:lit, 1]])
  process([:arglist, [:lit, 1], [:lit, 2]])

Parameters:

  • (Array<Array>) exp

Returns:



314
315
316
# File 'lib/php/generator.rb', line 314

def process_arglist(exp)
  Node.new(*exp.map { |val| process(val) })
end

- (Node) process_args(exp)

Processes [:args, ...] expressions.

Examples:

  process([:args])
  process([:args, :x])
  process([:args, :x, :y])

Parameters:

  • (Array) exp

Returns:



300
301
302
# File 'lib/php/generator.rb', line 300

def process_args(exp)
  Node.new(*exp.map { |var| Variable.new(var) })
end

- (Literal::Array) process_array(exp)

Processes [:array, ...] expressions.

Examples:

  process{[1, 2, 3]} == process([:array, [:lit, 1], [:lit, 2], [:lit, 3]])

Parameters:

  • (Array<Array>) exp

Returns:



122
123
124
# File 'lib/php/generator.rb', line 122

def process_array(exp)
  Literal::Array.new(*exp.map { |element| process(element) })
end

- (Block) process_block(exp)

Processes [:block, ...] expressions.

Examples:

  process([:block, [:args], [:nil]]) # ParseTree only
  process([:block, [:nil]])

Parameters:

  • (Array<Array>) exp

Returns:



340
341
342
# File 'lib/php/generator.rb', line 340

def process_block(exp)
  Block.new(*exp.map { |exp| process(exp) })
end

- (Node) process_call(exp)

Processes [:call, ...] expressions.

Examples:

  process{foo}       == process([:call, nil, :foo, [:arglist]])
  process{add(1, 2)} == process([:call, nil, :add, [:arglist, [:lit, 1], [:lit, 2]]])

Parameters:

  • (Array) exp

Returns:



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/php/generator.rb', line 197

def process_call(exp)
  receiver, method, arglist = exp
  case
    when receiver.nil?
      Function::Call.new(method, *process(arglist))
    when op = Operator.for(method)
      op.new(process(receiver), *process(arglist))
    else
      Method::Call.new(process(receiver), method, *process(arglist))
  end
end

- (Class) process_class(exp)

Processes [:class, symbol, parent, [:scope, ...]] expressions.

Examples:

  process{class Foo; end} == process([:class, :Foo, nil, [:scope]])

Parameters:

  • (Array(Symbol, Object, Array)) exp

Returns:



364
365
366
# File 'lib/php/generator.rb', line 364

def process_class(exp)
  Class.new(exp.shift, :extends => exp.first ? process(exp.first) : nil) # TODO
end

- (Identifier) process_const(exp)

Processes [:const, symbol] expressions.

Examples:

  process([:const, :Foo])

Parameters:

  • (Array(Symbol)) exp

Returns:



376
377
378
# File 'lib/php/generator.rb', line 376

def process_const(exp)
  Identifier.new(exp.shift)
end

- (Variable) process_dasgn_curr(exp)

Processes [:dasgn_curr, symbol] expressions.

Examples:

  process([:dasgn_curr, :x])

Parameters:

  • (Array(Symbol)) exp

Returns:



247
248
249
# File 'lib/php/generator.rb', line 247

def process_dasgn_curr(exp)
  Variable.new(exp.shift)
end

- (Function) process_defn(exp)

Processes [:defn, symbol, ...] expressions.

Examples:

Zero-arity functions

  process([:defn, :foo, [:scope, [:block, [:args], [:nil]]]]) # ParseTree only
  process([:defn, :foo, [:args], [:scope, [:block, [:nil]]]])

One-arity functions

  process([:defn, :foo, [:scope, [:block, [:args, :x], [:nil]]]]) # ParseTree only
  process([:defn, :foo, [:args, :x], [:scope, [:block, [:nil]]]])

Parameters:

  • (Array(Symbol, Array)) exp

Returns:



279
280
281
282
283
284
285
286
287
288
# File 'lib/php/generator.rb', line 279

def process_defn(exp)
  name = exp.shift
  args = (exp.size == 2 ? process(exp.shift) : []).to_a # ParseTree workaround
  if exp.first == [:scope, [:block, [:nil]]] # HACK
    body = nil
  else
    body = process(exp.shift)
  end
  Function.new(name, args, body)
end

- (Operator::String::Concatenation) process_dstr(exp)

Processes [:dstr, prefix, ...] expressions.

Examples:

  process([:dstr, "<", [:evstr, [:call, nil, :url, [:arglist]]], [:str, ">"]])

Parameters:

  • (Array) exp

Returns:



93
94
95
96
97
98
99
100
# File 'lib/php/generator.rb', line 93

def process_dstr(exp)
  prefix = exp.shift.to_s
  parts  = prefix.empty? ? [] : [Literal.new(prefix)]
  parts += exp.map { |part| process(part) }
  parts.reverse.inject(nil) do |prefix, part|
    prefix.nil? ? part : Operator::String::Concatenation.new(part, prefix)
  end
end

- (Expression) process_evstr(exp)

Processes [:evstr, operation] expressions.

Examples:

  process([:evstr, [:call, nil, :url, [:arglist]]])

Parameters:

  • (Array(Array)) exp

Returns:



110
111
112
# File 'lib/php/generator.rb', line 110

def process_evstr(exp)
  process(exp.shift)
end

- (Literal) process_false(exp)

Processes [:false] expressions.

Parameters:

  • (Array()) exp

Returns:



54
55
56
# File 'lib/php/generator.rb', line 54

def process_false(exp)
  Literal.new(false)
end

- (Loop::ForEach) process_for(exp)

Processes [:for, iterable, variables, block] expressions.

Examples:

  process([:for, [:lvar, :numbers], [:lasgn, :number]])

Parameters:

  • (Array(Array, Array, Array)) exp

Returns:



420
421
422
# File 'lib/php/generator.rb', line 420

def process_for(exp)
  Loop::ForEach.new(process(exp.shift), process(exp.shift), process(exp.shift))
end

- (Operator::Assignment) process_gasgn(exp)

Processes [:gasgn, symbol, value] expressions.

Examples:

  process{$foo = 123} == process([:gasgn, :$foo, [:lit, 123]])

Parameters:

  • (Array(Symbol, Array)) exp

Returns:



158
159
160
161
162
# File 'lib/php/generator.rb', line 158

def process_gasgn(exp)
  var = Variable.new(exp.shift.to_s[1..-1]) # NOTE: removes the leading '$' character
  val = process(exp.shift)
  val ? Operator::Assignment.new(var, val) : var
end

- (Variable) process_gvar(exp)

Processes [:gvar, symbol] expressions.

Examples:

  process{$foo} == process([:gvar, :$foo])

Parameters:

  • (Array(Symbol)) exp

Returns:



146
147
148
# File 'lib/php/generator.rb', line 146

def process_gvar(exp)
  Variable.new(exp.shift.to_s[1..-1]) # NOTE: removes the leading '$' character
end

- (Literal::Hash) process_hash(exp)

Processes [:hash, ...] expressions.

Examples:

  process{{:a => 1, :b => 2}} == process([:hash, [:lit, :a], [:lit, 1], [:lit, :b], [:lit, 2]])

Parameters:

  • (Array<Array>) exp

Returns:



134
135
136
# File 'lib/php/generator.rb', line 134

def process_hash(exp)
  Literal::Hash.new(*exp.map { |element| process(element) }.enum_slice(2))
end

- (Statement::If) process_if(exp)

Processes [:if, condition, then_statement, else_statement] expressions.

Examples:

  process([:if, [:true], [:lit, 1], [:lit, 0]])

Parameters:

  • (Array(Array, Array, Array)) exp

Returns:



400
401
402
403
404
405
406
407
408
409
410
# File 'lib/php/generator.rb', line 400

def process_if(exp)
  condition, then_branch, else_branch = exp
  case
    when else_branch.nil?
      Statement::If.new(process(condition), process(then_branch))
    when then_branch.nil?
      Statement::If.new(Operator::Logical::Not.new(process(condition)), process(else_branch))
    else
      Statement::If.new(process(condition), process(then_branch), process(else_branch))
  end
end

- (Variable) process_iter(exp)

Processes [:iter, ...] expressions.

Parameters:

  • (Array<Object>) exp

Returns:



214
215
216
217
218
219
220
221
222
223
# File 'lib/php/generator.rb', line 214

def process_iter(exp)
  spec, args, body = exp
  if args.nil?
    Function.new(nil)
  else
    args = process(args)
    args = args.is_a?(Variable) ? [args] : args.to_a
    Function.new(nil, args) # FIXME
  end
end

- (Variable, Operator::Assignment) process_lasgn(exp)

Processes [:lasgn, symbol, value] expressions.

Examples:

  process([:lasgn, :x])
  process([:lasgn, :x, [:lit, 123]])

Parameters:

  • (Array(Symbol), Array(Symbol, Array)) exp

Returns:



234
235
236
237
# File 'lib/php/generator.rb', line 234

def process_lasgn(exp)
  var, val = Variable.new(exp.shift), process(exp.shift)
  val ? Operator::Assignment.new(var, val) : var
end

- (Literal) process_lit(exp)

Processes [:lit, value] expressions.

Parameters:

  • (Array(Object)) exp

Returns:



72
73
74
# File 'lib/php/generator.rb', line 72

def process_lit(exp)
  Literal.new(exp.shift)
end

- (Variable) process_lvar(exp)

Processes [:lvar, symbol] expressions.

Examples:

  process([:lvar, :foo])

Parameters:

  • (Array(Symbol)) exp

Returns:



172
173
174
# File 'lib/php/generator.rb', line 172

def process_lvar(exp)
  Variable.new(exp.shift)
end

- (Node) process_masgn(exp)

Processes [:masgn, ...] expressions.

Examples:

  process([:masgn, [:array, [:dasgn_curr, :x], [:dasgn_curr, :y]], nil, nil]) # ParseTree only
  process([:masgn, [:array, [:lasgn, :x], [:lasgn, :y]]])

Parameters:

  • (Array(Symbol)) exp

Returns:



260
261
262
263
264
# File 'lib/php/generator.rb', line 260

def process_masgn(exp)
  exp = exp.first
  exp.shift # removes the initial :array element
  Node.new(*exp.map { |asgn| process(asgn) }) # FIXME
end

- (Operator::Regex::Match) process_match2(exp)

Processes [:match2, pattern, subject] expressions.

Examples:

  process{/a-z/ =~ "a"} == process([:match2, [:lit, /a-z/], [:str, "a"]])
  process{/a-z/ =~ $x } == process([:match2, [:lit, /a-z/], [:gvar, :$x]])

Parameters:

  • (Array(Array, Array)) exp

Returns:



505
506
507
# File 'lib/php/generator.rb', line 505

def process_match2(exp)
  process_match3(exp)
end

- (Operator::Regex::Match) process_match3(exp)

Processes [:match3, pattern, subject] expressions.

Examples:

  process{"a" =~ /a-z/} == process([:match3, [:lit, /a-z/], [:str, "a"]])
  process{$x  =~ /a-z/} == process([:match3, [:lit, /a-z/], [:gvar, :$x]])

Parameters:

  • (Array(Array, Array)) exp

Returns:



518
519
520
# File 'lib/php/generator.rb', line 518

def process_match3(exp)
  Operator::Regex::Match.new(process(exp.shift), process(exp.shift))
end

- (Class) process_module(exp)

Processes [:module, symbol, [:scope, ...]] expressions.

Examples:

  process{module Foo; end} == process([:module, :Foo, [:scope]])

Parameters:

  • (Array(Symbol, Array)) exp

Returns:



352
353
354
# File 'lib/php/generator.rb', line 352

def process_module(exp)
  Interface.new(exp.shift) # TODO
end

- (Literal) process_nil(exp)

Processes [:nil] expressions.

Parameters:

  • (Array()) exp

Returns:



45
46
47
# File 'lib/php/generator.rb', line 45

def process_nil(exp)
  Literal.new(nil)
end

- (Operator::Logical::Not) process_not(exp)

Processes [:not, operand] expressions.

Examples:

  process([:not, [:true]])

Parameters:

  • (Array(Array)) exp

Returns:



456
457
458
# File 'lib/php/generator.rb', line 456

def process_not(exp)
  Operator::Logical::Not.new(process(exp.shift))
end

- (Operator::Logical::Or) process_or(exp)

Processes [:or, lhs, rhs] expressions.

Examples:

  process([:or, [:true], [:false]])

Parameters:

  • (Array(Array, Array)) exp

Returns:



480
481
482
# File 'lib/php/generator.rb', line 480

def process_or(exp)
  Operator::Logical::Or.new(process(exp.shift), process(exp.shift))
end

- (Statement::Return) process_return(exp)

Processes [:return, [...]] expressions.

Examples:

  process([:return, [:nil]])

Parameters:

  • (Array) exp

Returns:



388
389
390
# File 'lib/php/generator.rb', line 388

def process_return(exp)
  Statement::Return.new(exp.empty? ? nil : process(exp.shift))
end

- (Node) process_scope(exp)

Processes [:scope, [...]] expressions.

Examples:

  process([:scope, [:block, [:args], [:nil]]]) # ParseTree only
  process([:scope, [:block, [:nil]]])

Parameters:

  • (Array(Array)) exp

Returns:



327
328
329
# File 'lib/php/generator.rb', line 327

def process_scope(exp)
  process(exp.first)
end

- (Literal) process_str(exp)

Processes [:str, value] expressions.

Parameters:

  • (Array(String)) exp

Returns:



81
82
83
# File 'lib/php/generator.rb', line 81

def process_str(exp)
  Literal.new(exp.shift)
end

- (Literal) process_true(exp)

Processes [:true] expressions.

Parameters:

  • (Array()) exp

Returns:



63
64
65
# File 'lib/php/generator.rb', line 63

def process_true(exp)
  Literal.new(true)
end

- (Loop::While) process_until(exp)

Processes [:until, condition, block] expressions.

Examples:

  process([:until, [:false], [:call, nil, :puts, [:arglist, [:str, "looping..."]]], true])

Parameters:

  • (Array(Array, Array, Boolean)) exp

Returns:



444
445
446
# File 'lib/php/generator.rb', line 444

def process_until(exp)
  Loop::While.new(Operator::Logical::Not.new(process(exp.shift)), process(exp.shift))
end

- (Variable) process_vcall(exp)

Processes [:vcall, symbol] expressions.

Examples:

  process{foo} == process([:vcall, :foo]) # ParseTree only

Parameters:

  • (Array(Symbol)) exp

Returns:

Raises:

  • (NotImplementedError)


184
185
186
# File 'lib/php/generator.rb', line 184

def process_vcall(exp) # FIXME
  raise NotImplementedError.new(exp.inspect) # TODO
end

- (Loop::While) process_while(exp)

Processes [:while, condition, block] expressions.

Examples:

  process([:while, [:true], [:call, nil, :puts, [:arglist, [:str, "looping..."]]], true])

Parameters:

  • (Array(Array, Array, Boolean)) exp

Returns:



432
433
434
# File 'lib/php/generator.rb', line 432

def process_while(exp)
  Loop::While.new(process(exp.shift), process(exp.shift))
end

- (Operator::Execution) process_xstr(exp)

Processes [:xstr, command] expressions.

Examples:

  process{`hostname`} == process([:xstr, 'hostname'])

Parameters:

  • (Array(String)) exp

Returns:



492
493
494
# File 'lib/php/generator.rb', line 492

def process_xstr(exp)
  Operator::Execution.new(exp.shift)
end