class Yadriggy::Py::CodeGen
Constants
- FreeVarInitName
The name of the function for initializing free variables.
Attributes
printer[R]
@return [Printer] the printer.
Public Class Methods
new(printer, checker)
click to toggle source
@param [Printer] printer @param [PyTypeChecker] checker
Calls superclass method
Yadriggy::Checker::new
# File lib/yadriggy/py/codegen.rb, line 13 def initialize(printer, checker) super() @printer = printer @typechecker = checker end
Public Instance Methods
error_group()
click to toggle source
@api private
# File lib/yadriggy/py/codegen.rb, line 20 def error_group 'code generation' end
is_omitted?(ast)
click to toggle source
@api private
- :n
-
in Python is written as [_..n] in Ruby.
# File lib/yadriggy/py/codegen.rb, line 188 def is_omitted?(ast) ast.is_a?(Name) && ast.name == '_' end
method_name(name)
click to toggle source
@api private
# File lib/yadriggy/py/codegen.rb, line 448 def method_name(name) if name == 'initialize' '__init__' else name end end
newline()
click to toggle source
Starts a new line.
# File lib/yadriggy/py/codegen.rb, line 39 def newline @printer.nl end
print(an_ast)
click to toggle source
Prints a given AST by {#printer}. @param [ASTree|ASTnode] an_ast the AST. @return [CodeGen] the `self` object.
# File lib/yadriggy/py/codegen.rb, line 27 def print(an_ast) check_all(an_ast) if errors? error_messages.each do |m| STDERR.puts(m) end raise RuntimeError.new('Python code generation failure') end self end
print_binary(an_ast, op)
click to toggle source
@api private
# File lib/yadriggy/py/codegen.rb, line 239 def print_binary(an_ast, op) @printer << '(' print(an_ast.receiver) @printer << ')' @printer << op @printer << '(' print(an_ast.args[0]) @printer << ')' end
print_each(array, first, &block)
click to toggle source
@api private
# File lib/yadriggy/py/codegen.rb, line 250 def print_each(array, first, &block) array.each do |e| if e if first first = false else @printer << ', ' end if block.nil? print(e) else block.call(e) end end end first end
print_free_vars_initializer()
click to toggle source
Prints a function for initializing free variables in Python. @return [Array<Object>] the arguments to the function.
# File lib/yadriggy/py/codegen.rb, line 48 def print_free_vars_initializer @printer << 'def ' << FreeVarInitName << '(_yadpy_values):' << :nl @printer << ' global ' @typechecker.references.each_with_index do |pair, i| @printer << ', ' if i > 0 @printer << pair[1] end @printer << :nl args = [] i = 0 @typechecker.references.each do |pair| @printer << ' ' << pair[1] << ' = ' << '_yadpy_values[' << i.to_s << ']' << :nl args << pair[0] i += 1 end @printer << :nl return args end
print_lambda(func)
click to toggle source
# File lib/yadriggy/py/codegen.rb, line 324 def print_lambda(func) @printer << '(lambda ' print_parameters(func.params) @printer << ': ' print(func.body) # has to be a simple expression? @printer << ')' end
print_parameters(params)
click to toggle source
@api private
# File lib/yadriggy/py/codegen.rb, line 440 def print_parameters(params) params.each_with_index do |p, i| @printer << ', ' if i > 0 @printer << p.name end end
print_tuple(an_ast)
click to toggle source
@api private
# File lib/yadriggy/py/codegen.rb, line 231 def print_tuple(an_ast) @printer << '(' print_each(an_ast.args, true) @printer << ', ' if an_ast.args.size == 1 @printer << ')' end
python_binary_op(op)
click to toggle source
# File lib/yadriggy/py/codegen.rb, line 310 def python_binary_op(op) if op == :'&&' 'and' elsif op == :'||' 'or' else op end end