ikra 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ast/builder.rb +100 -0
  3. data/lib/ast/lexical_variables_enumerator.rb +34 -0
  4. data/lib/ast/method_definition.rb +37 -0
  5. data/lib/ast/nodes.rb +208 -0
  6. data/lib/ast/printer.rb +99 -0
  7. data/lib/ast/translator.rb +264 -0
  8. data/lib/ast/visitor.rb +173 -0
  9. data/lib/config/configuration.rb +18 -0
  10. data/lib/config/os_configuration.rb +56 -0
  11. data/lib/entity.rb +11 -0
  12. data/lib/ikra.rb +7 -0
  13. data/lib/parsing.rb +32 -0
  14. data/lib/resources/cuda/block_function_head.cpp +1 -0
  15. data/lib/resources/cuda/env_builder_copy_array.cpp +4 -0
  16. data/lib/resources/cuda/header.cpp +46 -0
  17. data/lib/resources/cuda/kernel.cpp +8 -0
  18. data/lib/resources/cuda/kernel_launcher.cpp +28 -0
  19. data/lib/resources/cuda/soa_header.cpp +4 -0
  20. data/lib/scope.rb +166 -0
  21. data/lib/sourcify/Gemfile +10 -0
  22. data/lib/sourcify/HISTORY.txt +88 -0
  23. data/lib/sourcify/LICENSE +20 -0
  24. data/lib/sourcify/README.rdoc +352 -0
  25. data/lib/sourcify/Rakefile +111 -0
  26. data/lib/sourcify/lib/sourcify.rb +44 -0
  27. data/lib/sourcify/lib/sourcify/common/parser/converter.rb +29 -0
  28. data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/comment.rb +23 -0
  29. data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/counter.rb +43 -0
  30. data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/dstring.rb +58 -0
  31. data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb +140 -0
  32. data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/heredoc.rb +26 -0
  33. data/lib/sourcify/lib/sourcify/common/parser/source_code.rb +45 -0
  34. data/lib/sourcify/lib/sourcify/common/ragel/common.rl +5 -0
  35. data/lib/sourcify/lib/sourcify/common/ragel/expressions.rl +38 -0
  36. data/lib/sourcify/lib/sourcify/common/ragel/machines.rl +317 -0
  37. data/lib/sourcify/lib/sourcify/errors.rb +4 -0
  38. data/lib/sourcify/lib/sourcify/method.rb +138 -0
  39. data/lib/sourcify/lib/sourcify/method/methods.rb +3 -0
  40. data/lib/sourcify/lib/sourcify/method/methods/to_raw_source.rb +30 -0
  41. data/lib/sourcify/lib/sourcify/method/methods/to_sexp.rb +30 -0
  42. data/lib/sourcify/lib/sourcify/method/methods/to_source.rb +30 -0
  43. data/lib/sourcify/lib/sourcify/method/parser.rb +110 -0
  44. data/lib/sourcify/lib/sourcify/method/parser/converter.rb +8 -0
  45. data/lib/sourcify/lib/sourcify/method/parser/raw_scanner.rb +2494 -0
  46. data/lib/sourcify/lib/sourcify/method/parser/raw_scanner.rl +144 -0
  47. data/lib/sourcify/lib/sourcify/method/parser/raw_scanner_extensions.rb +68 -0
  48. data/lib/sourcify/lib/sourcify/method/parser/scanner.rb +52 -0
  49. data/lib/sourcify/lib/sourcify/method/parser/source_code.rb +8 -0
  50. data/lib/sourcify/lib/sourcify/patches.rb +63 -0
  51. data/lib/sourcify/lib/sourcify/proc.rb +183 -0
  52. data/lib/sourcify/lib/sourcify/proc/methods.rb +3 -0
  53. data/lib/sourcify/lib/sourcify/proc/methods/source_location.rb +61 -0
  54. data/lib/sourcify/lib/sourcify/proc/methods/to_raw_source.rb +20 -0
  55. data/lib/sourcify/lib/sourcify/proc/methods/to_sexp.rb +40 -0
  56. data/lib/sourcify/lib/sourcify/proc/methods/to_source.rb +48 -0
  57. data/lib/sourcify/lib/sourcify/proc/parser.rb +51 -0
  58. data/lib/sourcify/lib/sourcify/proc/parser/converter.rb +8 -0
  59. data/lib/sourcify/lib/sourcify/proc/parser/normalizer.rb +43 -0
  60. data/lib/sourcify/lib/sourcify/proc/parser/raw_scanner.rb +2498 -0
  61. data/lib/sourcify/lib/sourcify/proc/parser/raw_scanner.rl +149 -0
  62. data/lib/sourcify/lib/sourcify/proc/parser/raw_scanner_extensions.rb +74 -0
  63. data/lib/sourcify/lib/sourcify/proc/parser/scanner.rb +49 -0
  64. data/lib/sourcify/lib/sourcify/proc/parser/source_code.rb +8 -0
  65. data/lib/sourcify/lib/sourcify/version.rb +3 -0
  66. data/lib/sourcify/sourcify.gemspec +31 -0
  67. data/lib/sourcify/spec/dump_object_space_procs.rb +84 -0
  68. data/lib/sourcify/spec/method/encoding_from_def_end_block_spec.rb +33 -0
  69. data/lib/sourcify/spec/method/encoding_from_define_method_spec.rb +37 -0
  70. data/lib/sourcify/spec/method/others_from_def_end_block_spec.rb +49 -0
  71. data/lib/sourcify/spec/method/others_from_define_method_spec.rb +63 -0
  72. data/lib/sourcify/spec/method/raw_scanner/block_comment_spec.rb +8 -0
  73. data/lib/sourcify/spec/method/raw_scanner/double_colons_spec.rb +8 -0
  74. data/lib/sourcify/spec/method/raw_scanner/double_quote_str_w_interpolation_spec.rb +8 -0
  75. data/lib/sourcify/spec/method/raw_scanner/double_quote_str_wo_interpolation_spec.rb +8 -0
  76. data/lib/sourcify/spec/method/raw_scanner/heredoc_w_indent_spec.rb +8 -0
  77. data/lib/sourcify/spec/method/raw_scanner/heredoc_wo_indent_spec.rb +8 -0
  78. data/lib/sourcify/spec/method/raw_scanner/kw_block_start_alias1_spec.rb +20 -0
  79. data/lib/sourcify/spec/method/raw_scanner/kw_block_start_alias2_spec.rb +20 -0
  80. data/lib/sourcify/spec/method/raw_scanner/per_line_comment_spec.rb +8 -0
  81. data/lib/sourcify/spec/method/raw_scanner/single_quote_str_spec.rb +8 -0
  82. data/lib/sourcify/spec/method/raw_scanner/slash_operator_spec.rb +8 -0
  83. data/lib/sourcify/spec/method/raw_scanner/spec_helper.rb +80 -0
  84. data/lib/sourcify/spec/method/spec_helper.rb +1 -0
  85. data/lib/sourcify/spec/method/to_raw_source_spec.rb +31 -0
  86. data/lib/sourcify/spec/method/to_raw_source_w_specified_strip_enclosure_spec.rb +148 -0
  87. data/lib/sourcify/spec/method/to_sexp_from_def_end_block_w_variables_spec.rb +46 -0
  88. data/lib/sourcify/spec/method/to_sexp_from_def_end_block_within_irb_spec.rb +38 -0
  89. data/lib/sourcify/spec/method/to_sexp_from_def_end_block_within_pry_spec.rb +38 -0
  90. data/lib/sourcify/spec/method/to_sexp_from_define_method_w_multi_blocks_and_specified_attached_to_spec.rb +56 -0
  91. data/lib/sourcify/spec/method/to_sexp_from_define_method_w_variables_spec.rb +52 -0
  92. data/lib/sourcify/spec/method/to_sexp_from_define_method_within_irb_spec.rb +42 -0
  93. data/lib/sourcify/spec/method/to_sexp_from_define_method_within_pry_spec.rb +42 -0
  94. data/lib/sourcify/spec/method/to_sexp_w_specified_strip_enclosure_spec.rb +74 -0
  95. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_19_extras_spec.rb +23 -0
  96. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_begin_spec.rb +35 -0
  97. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_case_spec.rb +35 -0
  98. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_class_spec.rb +51 -0
  99. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_do_end_block_spec.rb +33 -0
  100. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_for_spec.rb +126 -0
  101. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_if_spec.rb +83 -0
  102. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_literal_keyword_spec.rb +141 -0
  103. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_method_spec.rb +33 -0
  104. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_module_spec.rb +59 -0
  105. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_unless_spec.rb +83 -0
  106. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_until_spec.rb +179 -0
  107. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_while_spec.rb +179 -0
  108. data/lib/sourcify/spec/method/to_source_from_def_end_block_w_singleton_method_spec.rb +19 -0
  109. data/lib/sourcify/spec/method/to_source_from_def_end_block_within_irb_spec.rb +30 -0
  110. data/lib/sourcify/spec/method/to_source_from_def_end_block_within_pry_spec.rb +45 -0
  111. data/lib/sourcify/spec/method/to_source_from_def_end_w_multi_blocks_and_many_matches_spec.rb +30 -0
  112. data/lib/sourcify/spec/method/to_source_from_def_end_w_multi_blocks_and_single_match_spec.rb +36 -0
  113. data/lib/sourcify/spec/method/to_source_from_define_method_w_braced_block_spec.rb +113 -0
  114. data/lib/sourcify/spec/method/to_source_from_define_method_w_do_end_block_spec.rb +145 -0
  115. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_many_matches_spec.rb +56 -0
  116. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_single_match_spec.rb +73 -0
  117. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_many_matches_spec.rb +36 -0
  118. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_no_match_spec.rb +36 -0
  119. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_single_match_spec.rb +28 -0
  120. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_spec.rb +103 -0
  121. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_many_matches_spec.rb +36 -0
  122. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_no_match_spec.rb +36 -0
  123. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_single_match_spec.rb +28 -0
  124. data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_ignore_nested_spec.rb +36 -0
  125. data/lib/sourcify/spec/method/to_source_from_define_method_within_irb_spec.rb +32 -0
  126. data/lib/sourcify/spec/method/to_source_from_define_method_within_pry_spec.rb +49 -0
  127. data/lib/sourcify/spec/method/to_source_magic_file_var_spec.rb +176 -0
  128. data/lib/sourcify/spec/method/to_source_magic_line_var_spec.rb +298 -0
  129. data/lib/sourcify/spec/method/to_source_w_specified_strip_enclosure_spec.rb +39 -0
  130. data/lib/sourcify/spec/no_method/unsupported_platform_spec.rb +26 -0
  131. data/lib/sourcify/spec/proc/19x_extras.rb +27 -0
  132. data/lib/sourcify/spec/proc/created_on_the_fly_proc_spec.rb +80 -0
  133. data/lib/sourcify/spec/proc/encoding_spec.rb +36 -0
  134. data/lib/sourcify/spec/proc/others_spec.rb +40 -0
  135. data/lib/sourcify/spec/proc/raw_scanner/block_comment_spec.rb +8 -0
  136. data/lib/sourcify/spec/proc/raw_scanner/double_colons_spec.rb +8 -0
  137. data/lib/sourcify/spec/proc/raw_scanner/double_quote_str_w_interpolation_spec.rb +8 -0
  138. data/lib/sourcify/spec/proc/raw_scanner/double_quote_str_wo_interpolation_spec.rb +8 -0
  139. data/lib/sourcify/spec/proc/raw_scanner/heredoc_w_indent_spec.rb +8 -0
  140. data/lib/sourcify/spec/proc/raw_scanner/heredoc_wo_indent_spec.rb +8 -0
  141. data/lib/sourcify/spec/proc/raw_scanner/kw_block_start_alias1_spec.rb +20 -0
  142. data/lib/sourcify/spec/proc/raw_scanner/kw_block_start_alias2_spec.rb +20 -0
  143. data/lib/sourcify/spec/proc/raw_scanner/per_line_comment_spec.rb +8 -0
  144. data/lib/sourcify/spec/proc/raw_scanner/single_quote_str_spec.rb +8 -0
  145. data/lib/sourcify/spec/proc/raw_scanner/slash_operator_spec.rb +8 -0
  146. data/lib/sourcify/spec/proc/raw_scanner/spec_helper.rb +63 -0
  147. data/lib/sourcify/spec/proc/readme +5 -0
  148. data/lib/sourcify/spec/proc/spec_helper.rb +1 -0
  149. data/lib/sourcify/spec/proc/to_raw_source_spec.rb +33 -0
  150. data/lib/sourcify/spec/proc/to_raw_source_w_specified_strip_enclosure_spec.rb +69 -0
  151. data/lib/sourcify/spec/proc/to_sexp_from_multi_blocks_w_specified_attached_to_spec.rb +46 -0
  152. data/lib/sourcify/spec/proc/to_sexp_variables_spec.rb +146 -0
  153. data/lib/sourcify/spec/proc/to_sexp_w_specified_strip_enclosure_spec.rb +60 -0
  154. data/lib/sourcify/spec/proc/to_sexp_within_irb_spec.rb +146 -0
  155. data/lib/sourcify/spec/proc/to_sexp_within_pry_spec.rb +149 -0
  156. data/lib/sourcify/spec/proc/to_source_from_braced_block_w_nested_braced_block_spec.rb +33 -0
  157. data/lib/sourcify/spec/proc/to_source_from_braced_block_w_nested_hash_spec.rb +82 -0
  158. data/lib/sourcify/spec/proc/to_source_from_braced_block_wo_nesting_complication_spec.rb +46 -0
  159. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_begin_spec.rb +35 -0
  160. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_case_spec.rb +35 -0
  161. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_class_spec.rb +89 -0
  162. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_do_end_block_spec.rb +33 -0
  163. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_for_spec.rb +132 -0
  164. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_if_spec.rb +87 -0
  165. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb +103 -0
  166. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_method_spec.rb +33 -0
  167. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_module_spec.rb +49 -0
  168. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_unless_spec.rb +87 -0
  169. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_until_spec.rb +189 -0
  170. data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_while_spec.rb +189 -0
  171. data/lib/sourcify/spec/proc/to_source_from_do_end_block_wo_nesting_complication_spec.rb +46 -0
  172. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_many_matches_spec.rb +43 -0
  173. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_single_match_spec.rb +20 -0
  174. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_and_many_matches_spec.rb +45 -0
  175. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_and_no_match_spec.rb +45 -0
  176. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_and_single_match_spec.rb +22 -0
  177. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_spec.rb +84 -0
  178. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_body_matcher_and_many_matches_spec.rb +45 -0
  179. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_body_matcher_and_no_match_spec.rb +45 -0
  180. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_body_matcher_and_single_match_spec.rb +22 -0
  181. data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_ignore_nested_spec.rb +43 -0
  182. data/lib/sourcify/spec/proc/to_source_from_multi_do_end_blocks_w_single_match_spec.rb +31 -0
  183. data/lib/sourcify/spec/proc/to_source_magic_file_var_spec.rb +127 -0
  184. data/lib/sourcify/spec/proc/to_source_magic_line_var_spec.rb +127 -0
  185. data/lib/sourcify/spec/proc/to_source_variables_spec.rb +29 -0
  186. data/lib/sourcify/spec/proc/to_source_w_specified_strip_enclosure_spec.rb +33 -0
  187. data/lib/sourcify/spec/proc/to_source_within_irb_spec.rb +38 -0
  188. data/lib/sourcify/spec/proc/to_source_within_pry_spec.rb +61 -0
  189. data/lib/sourcify/spec/raw_scanner/block_comment_shared_spec.rb +57 -0
  190. data/lib/sourcify/spec/raw_scanner/double_colons_shared_spec.rb +11 -0
  191. data/lib/sourcify/spec/raw_scanner/double_quote_str_w_interpolation_shared_spec.rb +60 -0
  192. data/lib/sourcify/spec/raw_scanner/double_quote_str_wo_interpolation_shared_spec.rb +86 -0
  193. data/lib/sourcify/spec/raw_scanner/heredoc_w_indent_shared_spec.rb +69 -0
  194. data/lib/sourcify/spec/raw_scanner/heredoc_wo_indent_shared_spec.rb +70 -0
  195. data/lib/sourcify/spec/raw_scanner/kw_block_start_alias1_shared_spec.rb +73 -0
  196. data/lib/sourcify/spec/raw_scanner/kw_block_start_alias2_shared_spec.rb +73 -0
  197. data/lib/sourcify/spec/raw_scanner/per_line_comment_shared_spec.rb +32 -0
  198. data/lib/sourcify/spec/raw_scanner/shared_specs.rb +3 -0
  199. data/lib/sourcify/spec/raw_scanner/single_quote_str_shared_spec.rb +79 -0
  200. data/lib/sourcify/spec/raw_scanner/slash_operator_shared_spec.rb +71 -0
  201. data/lib/sourcify/spec/run_build.sh +25 -0
  202. data/lib/sourcify/spec/spec_helper.rb +130 -0
  203. data/lib/symbolic/symbolic.rb +248 -0
  204. data/lib/symbolic/visitor.rb +51 -0
  205. data/lib/translator/block_translator.rb +123 -0
  206. data/lib/translator/command_translator.rb +421 -0
  207. data/lib/translator/last_returns_visitor.rb +57 -0
  208. data/lib/translator/local_variables_enumerator.rb +35 -0
  209. data/lib/translator/method_translator.rb +24 -0
  210. data/lib/translator/translator.rb +49 -0
  211. data/lib/type_aware_array.rb +71 -0
  212. data/lib/types/array_type.rb +51 -0
  213. data/lib/types/class_type.rb +128 -0
  214. data/lib/types/object_tracer.rb +162 -0
  215. data/lib/types/primitive_type.rb +73 -0
  216. data/lib/types/ruby_extension.rb +67 -0
  217. data/lib/types/ruby_type.rb +45 -0
  218. data/lib/types/type_inference.rb +382 -0
  219. data/lib/types/union_type.rb +155 -0
  220. metadata +321 -0
@@ -0,0 +1,264 @@
1
+ require_relative "nodes.rb"
2
+ require_relative "../translator/translator"
3
+
4
+ # Rule: every statement ends with newline
5
+
6
+ module Ikra
7
+ module AST
8
+ class Node
9
+ @@next_temp_identifier_id = 0
10
+
11
+ def translate_statement
12
+ translate_expression + ";\n"
13
+ end
14
+
15
+ protected
16
+
17
+ def statements_as_expression(str)
18
+ "[&]{ #{str} }()"
19
+ end
20
+
21
+ def indent_block(str)
22
+ str.split("\n").map do |line| " " + line end.join("\n")
23
+ end
24
+
25
+ def wrap_in_c_block(str)
26
+ "{\n" + indent_block(str) + "\n}\n"
27
+ end
28
+
29
+ def temp_identifier_id
30
+ @@next_temp_identifier_id += 1
31
+ @@next_temp_identifier_id
32
+ end
33
+
34
+ # Generates code that assigns the value of a node to a newly-defined variable.
35
+ def define_assign_variable(name, node)
36
+ type = node.get_type.to_c_type
37
+ "#{type} #{name} = #{node.translate_expression};"
38
+ end
39
+ end
40
+
41
+ class RootNode
42
+ def translate_statement
43
+ child.translate_statement
44
+ end
45
+
46
+ def translate_expression
47
+ child.translate_expression
48
+ end
49
+ end
50
+
51
+ class ConstNode
52
+ def translate_expression
53
+ raise "Not implemented"
54
+ end
55
+ end
56
+
57
+ class LVarReadNode
58
+ def translate_expression
59
+ identifier.to_s
60
+ end
61
+ end
62
+
63
+ class LVarWriteNode
64
+ def translate_expression
65
+ "#{identifier.to_s} = #{value.translate_expression}"
66
+ end
67
+ end
68
+
69
+ class IVarReadNode
70
+ def translate_expression
71
+ array_identifier = class_owner.to_ikra_type.inst_var_array_name(identifier)
72
+ "#{Translator::Constants::ENV_IDENTIFIER}->#{array_identifier}[#{Constants::SELF_IDENTIFIER}]"
73
+ end
74
+ end
75
+
76
+ class IntNode
77
+ def translate_expression
78
+ value.to_s
79
+ end
80
+ end
81
+
82
+ class FloatNode
83
+ def translate_expression
84
+ value.to_s
85
+ end
86
+ end
87
+
88
+ class BoolNode
89
+ def translate_expression
90
+ value.to_s
91
+ end
92
+ end
93
+
94
+ class ForNode
95
+ def translate_statement
96
+ loop_header = "for (#{iterator_identifier.to_s} = #{range_from.translate_expression}; #{iterator_identifier.to_s} <= #{range_to.translate_expression}; #{iterator_identifier.to_s}++)"
97
+ loop_header + "\n" + body_stmts.translate_statement + "#{iterator_identifier.to_s}--;\n"
98
+ end
99
+
100
+ def translate_expression
101
+ # TODO: return value should be range
102
+ loop_header = "for (#{iterator_identifier.to_s} = #{range_from.translate_expression}; #{iterator_identifier.to_s} <= #{range_to.translate_expression}; #{iterator_identifier.to_s}++)"
103
+ full_loop = loop_header + "\n" + body_stmts.translate_statement + "#{iterator_identifier.to_s}--;\nreturn 0;"
104
+ statements_as_expression(full_loop)
105
+ end
106
+ end
107
+
108
+ class BreakNode
109
+ def translate_expression
110
+ raise "Not implemented yet"
111
+ end
112
+
113
+ def translate_statement
114
+ "break;\n"
115
+ end
116
+ end
117
+
118
+ class IfNode
119
+ def translate_statement
120
+ header = "if (#{condition.translate_expression})\n"
121
+
122
+ if false_body_stmts == nil
123
+ header + true_body_stmts.translate_statement
124
+ else
125
+ header + true_body_stmts.translate_statement + "else\n" + false_body_stmts.translate_statement
126
+ end
127
+ end
128
+
129
+ def translate_expression
130
+ header = "if (#{condition.translate_expression})\n"
131
+ true_body_translated = nil
132
+ false_body_translated = nil
133
+
134
+ if true_body_stmts.is_begin_node?
135
+ true_body_translated = true_body_stmts.translate_statement_last_returns
136
+ else
137
+ true_body_translated = "return " + true_body_stmts.translate_expression + ";\n"
138
+ end
139
+
140
+ if false_body_stmts != nil
141
+ # Can be begin node or anything else
142
+ if false_body_stmts.is_begin_node?
143
+ false_body_translated = false_body_stmts.translate_statement_last_returns
144
+ else
145
+ false_body_translated = "return " + false_body_stmts.translate_expression + ";\n"
146
+ end
147
+ end
148
+
149
+ if false_body_translated == nil
150
+ statements_as_expression(header + true_body_translated)
151
+ else
152
+ statements_as_expression(header + true_body_translated + "else\n" + false_body_translated)
153
+ end
154
+ end
155
+ end
156
+
157
+ class BeginNode
158
+ def translate_statement
159
+ if body_stmts.size == 0
160
+ return ""
161
+ end
162
+
163
+ body_translated = body_stmts.map do |stmt|
164
+ stmt.translate_statement
165
+ end.join("")
166
+
167
+ wrap_in_c_block(body_translated)
168
+ end
169
+
170
+ def translate_statement_last_returns
171
+ if body_stmts.size == 0
172
+ raise "Cannot return empty BeginNode"
173
+ end
174
+
175
+ body_translated = BeginNode.new(body_stmts[0...-1]).translate_statement
176
+ body_translated + "return #{body_stmts.last.translate_expression};\n"
177
+ wrap_in_c_block(body_translated)
178
+ end
179
+
180
+ def translate_expression
181
+ if body_stmts.size == 0
182
+ raise "Empty BeginNode cannot be an expression"
183
+ elsif body_stmts.size == 1
184
+ # Preserve brackets
185
+ "(#{body_stmts.first.translate_expression})"
186
+ else
187
+ # Wrap in lambda
188
+ # Do not worry about scope of varibles, they will all be declared at the beginning of the function
189
+ statements_as_expression(translate_statement_last_returns)
190
+ end
191
+ end
192
+ end
193
+
194
+ class SendNode
195
+ BinarySelectors = [:+, :-, :*, :/, :%, :<, :<=, :>, :>=, :==, :!=, :&, :'&&', :|, :'||', :^]
196
+
197
+ def translate_expression
198
+ if BinarySelectors.include?(selector)
199
+ if arguments.size != 1
200
+ raise "Expected 1 argument for binary selector (#{arguments.size} given)"
201
+ end
202
+
203
+ "(#{receiver.translate_expression} #{selector.to_s} #{arguments.first.translate_expression})"
204
+ else
205
+ if receiver.get_type.is_singleton? and
206
+ receiver.get_type.singleton_type.to_ruby_type.singleton_methods.include?(("_ikra_c_" + selector.to_s).to_sym)
207
+ # TODO: support multiple types for receiver
208
+ receiver.get_type.singleton_type.to_ruby_type.send(("_ikra_c_" + selector.to_s).to_sym, receiver.translate_expression)
209
+ else
210
+ # TODO: generate argument code only once
211
+
212
+ if receiver.get_type.is_singleton?
213
+ self_argument = []
214
+ if receiver.get_type.singleton_type.should_generate_type?
215
+ self_argument = [receiver]
216
+ end
217
+
218
+ args = ([Translator::Constants::ENV_IDENTIFIER] + (self_argument + arguments).map do |arg|
219
+ arg.translate_expression
220
+ end).join(", ")
221
+
222
+ "#{receiver.get_type.singleton_type.mangled_method_name(selector)}(#{args})"
223
+ else
224
+ # Polymorphic case
225
+ # TODO: This is not an expression anymore!
226
+ poly_id = temp_identifier_id
227
+ receiver_identifier = "_polytemp_recv_#{poly_id}"
228
+ result_identifier = "_polytemp_result_#{poly_id}"
229
+ header = "#{define_assign_variable(receiver_identifier, receiver)}\n#{get_type.to_c_type} #{result_identifier};\nswitch (#{receiver_identifier}.class_id)\n"
230
+ case_statements = []
231
+
232
+ receiver.get_type.types.each do |type|
233
+ self_argument = []
234
+ if type.should_generate_type?
235
+ # No need to pass type as subtypes are regarded as entirely new types
236
+ self_argument = ["#{receiver_identifier}.object_id"]
237
+ end
238
+
239
+ args = ([Translator::Constants::ENV_IDENTIFIER] + self_argument + arguments.map do |arg|
240
+ arg.translate_expression
241
+ end).join(", ")
242
+
243
+ case_statements.push("case #{type.class_id}: #{result_identifier} = #{type.mangled_method_name(selector)}(#{args}); break;")
244
+ end
245
+
246
+ # TODO: compound statements only work with the GNU C++ compiler
247
+ "(" + wrap_in_c_block(header + wrap_in_c_block(case_statements.join("\n")) + result_identifier + ";")[0..-2] + ")"
248
+ end
249
+ end
250
+ end
251
+ end
252
+ end
253
+
254
+ class ReturnNode
255
+ def translate_expression
256
+ raise "ReturnNode is never an expression"
257
+ end
258
+
259
+ def translate_statement
260
+ "return #{value.translate_expression};\n"
261
+ end
262
+ end
263
+ end
264
+ end
@@ -0,0 +1,173 @@
1
+ require_relative "nodes.rb"
2
+
3
+ module Ikra
4
+ module AST
5
+ class Node
6
+ def accept(visitor)
7
+ visitor.visit_node(self)
8
+ end
9
+ end
10
+
11
+ class ConstNode
12
+ def accept(visitor)
13
+ visitor.visit_const_node(self)
14
+ end
15
+ end
16
+
17
+ class RootNode
18
+ def accept(visitor)
19
+ visitor.visit_root_node(self)
20
+ end
21
+ end
22
+
23
+ class LVarReadNode
24
+ def accept(visitor)
25
+ visitor.visit_lvar_read_node(self)
26
+ end
27
+ end
28
+
29
+ class LVarWriteNode
30
+ def accept(visitor)
31
+ visitor.visit_lvar_write_node(self)
32
+ end
33
+ end
34
+
35
+ class IVarReadNode
36
+ def accept(visitor)
37
+ visitor.visit_ivar_read_node(self)
38
+ end
39
+ end
40
+
41
+ class IntNode
42
+ def accept(visitor)
43
+ visitor.visit_int_node(self)
44
+ end
45
+ end
46
+
47
+ class FloatNode
48
+ def accept(visitor)
49
+ visitor.visit_float_node(self)
50
+ end
51
+ end
52
+
53
+ class BoolNode
54
+ def accept(visitor)
55
+ visitor.visit_bool_node(self)
56
+ end
57
+ end
58
+
59
+ class ForNode
60
+ def accept(visitor)
61
+ visitor.visit_for_node(self)
62
+ end
63
+ end
64
+
65
+ class BreakNode
66
+ def accept(visitor)
67
+ visitor.visit_break_node(self)
68
+ end
69
+ end
70
+
71
+ class IfNode
72
+ def accept(visitor)
73
+ visitor.visit_if_node(self)
74
+ end
75
+ end
76
+
77
+ class BeginNode
78
+ def accept(visitor)
79
+ visitor.visit_begin_node(self)
80
+ end
81
+ end
82
+
83
+ class SendNode
84
+ def accept(visitor)
85
+ visitor.visit_send_node(self)
86
+ end
87
+ end
88
+
89
+ class ReturnNode
90
+ def accept(visitor)
91
+ visitor.visit_return_node(self)
92
+ end
93
+ end
94
+
95
+ class Visitor
96
+ def visit_node(node)
97
+
98
+ end
99
+
100
+ def visit_const_node(node)
101
+
102
+ end
103
+
104
+ def visit_root_node(node)
105
+ node.child.accept(self)
106
+ end
107
+
108
+ def visit_lvar_read_node(node)
109
+
110
+ end
111
+
112
+ def visit_lvar_write_node(node)
113
+ node.value.accept(self)
114
+ end
115
+
116
+ def visit_ivar_read_node(node)
117
+
118
+ end
119
+
120
+ def visit_int_node(node)
121
+
122
+ end
123
+
124
+ def visit_float_node(node)
125
+
126
+ end
127
+
128
+ def visit_bool_node(node)
129
+
130
+ end
131
+
132
+ def visit_for_node(node)
133
+ node.range_from.accept(self)
134
+ node.range_to.accept(self)
135
+ node.body_stmts.accept(self)
136
+ end
137
+
138
+ def visit_break_node(node)
139
+
140
+ end
141
+
142
+ def visit_if_node(node)
143
+ node.condition.accept(self)
144
+ node.true_body_stmts.accept(self)
145
+
146
+ if node.false_body_stmts != nil
147
+ node.false_body_stmts.accept(self)
148
+ end
149
+ end
150
+
151
+ def visit_begin_node(node)
152
+ node.body_stmts.each do |stmt|
153
+ stmt.accept(self)
154
+ end
155
+ end
156
+
157
+ def visit_send_node(node)
158
+ # Receiver might be nil for self sends
159
+ if node.receiver != nil
160
+ node.receiver.accept(self)
161
+ end
162
+
163
+ node.arguments.each do |arg|
164
+ arg.accept(self)
165
+ end
166
+ end
167
+
168
+ def visit_return_node(node)
169
+ node.value.accept(self)
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,18 @@
1
+ module Ikra
2
+ module Configuration
3
+ JOB_REORDERING = true
4
+ @@expect_file_name = "last_generated"
5
+
6
+ def self.resource_file_name(file_name)
7
+ File.expand_path("resources/cuda/#{file_name}", File.dirname(File.dirname(File.expand_path(__FILE__))))
8
+ end
9
+
10
+ def self.codegen_expect_file_name
11
+ File.expand_path("gen/codegen_expect/#{@@expect_file_name}.cu", File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))))
12
+ end
13
+
14
+ def self.codegen_expect_file_name=(value)
15
+ @@expect_file_name = value
16
+ end
17
+ end
18
+ end