Class: Yadriggy::Py::CodeGen
- Defined in:
- lib/yadriggy/py/codegen.rb
Constant Summary collapse
- FreeVarInitName =
The name of the function for initializing free variables.
'yadpy_initialize'
Constants included from Yadriggy
Instance Attribute Summary collapse
-
#printer ⇒ Printer
readonly
The printer.
Instance Method Summary collapse
-
#initialize(printer, checker) ⇒ CodeGen
constructor
A new instance of CodeGen.
-
#newline ⇒ Object
Starts a new line.
-
#print(an_ast) ⇒ CodeGen
Prints a given AST by #printer.
-
#print_free_vars_initializer ⇒ Array<Object>
Prints a function for initializing free variables in Python.
- #print_lambda(func) ⇒ Object
- #python_binary_op(op) ⇒ Object
Methods inherited from Checker
#ast, #ast_env, #check, #check_all, #check_later, #error!, #error_found!, #error_messages, #errors?, #last_error, #make_base_env, #proceed, rule
Methods included from Yadriggy
debug, debug=, define_syntax, reify, reset_pry
Constructor Details
#initialize(printer, checker) ⇒ CodeGen
Returns a new instance of CodeGen.
13 14 15 16 17 |
# File 'lib/yadriggy/py/codegen.rb', line 13 def initialize(printer, checker) super() @printer = printer @typechecker = checker end |
Instance Attribute Details
#printer ⇒ Printer (readonly)
Returns the printer.
9 10 11 |
# File 'lib/yadriggy/py/codegen.rb', line 9 def printer @printer end |
Instance Method Details
#newline ⇒ Object
Starts a new line.
39 40 41 |
# File 'lib/yadriggy/py/codegen.rb', line 39 def newline @printer.nl end |
#print(an_ast) ⇒ CodeGen
Prints a given AST by #printer.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/yadriggy/py/codegen.rb', line 27 def print(an_ast) check_all(an_ast) if errors? .each do |m| STDERR.puts(m) end raise RuntimeError.new('Python code generation failure') end self end |
#print_free_vars_initializer ⇒ Array<Object>
Prints a function for initializing free variables in Python.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
324 325 326 327 328 329 330 |
# 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 |
#python_binary_op(op) ⇒ Object
310 311 312 313 314 315 316 317 318 |
# File 'lib/yadriggy/py/codegen.rb', line 310 def python_binary_op(op) if op == :'&&' 'and' elsif op == :'||' 'or' else op end end |