ikra 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ast/builder.rb +100 -0
- data/lib/ast/lexical_variables_enumerator.rb +34 -0
- data/lib/ast/method_definition.rb +37 -0
- data/lib/ast/nodes.rb +208 -0
- data/lib/ast/printer.rb +99 -0
- data/lib/ast/translator.rb +264 -0
- data/lib/ast/visitor.rb +173 -0
- data/lib/config/configuration.rb +18 -0
- data/lib/config/os_configuration.rb +56 -0
- data/lib/entity.rb +11 -0
- data/lib/ikra.rb +7 -0
- data/lib/parsing.rb +32 -0
- data/lib/resources/cuda/block_function_head.cpp +1 -0
- data/lib/resources/cuda/env_builder_copy_array.cpp +4 -0
- data/lib/resources/cuda/header.cpp +46 -0
- data/lib/resources/cuda/kernel.cpp +8 -0
- data/lib/resources/cuda/kernel_launcher.cpp +28 -0
- data/lib/resources/cuda/soa_header.cpp +4 -0
- data/lib/scope.rb +166 -0
- data/lib/sourcify/Gemfile +10 -0
- data/lib/sourcify/HISTORY.txt +88 -0
- data/lib/sourcify/LICENSE +20 -0
- data/lib/sourcify/README.rdoc +352 -0
- data/lib/sourcify/Rakefile +111 -0
- data/lib/sourcify/lib/sourcify.rb +44 -0
- data/lib/sourcify/lib/sourcify/common/parser/converter.rb +29 -0
- data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/comment.rb +23 -0
- data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/counter.rb +43 -0
- data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/dstring.rb +58 -0
- data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/extensions.rb +140 -0
- data/lib/sourcify/lib/sourcify/common/parser/raw_scanner/heredoc.rb +26 -0
- data/lib/sourcify/lib/sourcify/common/parser/source_code.rb +45 -0
- data/lib/sourcify/lib/sourcify/common/ragel/common.rl +5 -0
- data/lib/sourcify/lib/sourcify/common/ragel/expressions.rl +38 -0
- data/lib/sourcify/lib/sourcify/common/ragel/machines.rl +317 -0
- data/lib/sourcify/lib/sourcify/errors.rb +4 -0
- data/lib/sourcify/lib/sourcify/method.rb +138 -0
- data/lib/sourcify/lib/sourcify/method/methods.rb +3 -0
- data/lib/sourcify/lib/sourcify/method/methods/to_raw_source.rb +30 -0
- data/lib/sourcify/lib/sourcify/method/methods/to_sexp.rb +30 -0
- data/lib/sourcify/lib/sourcify/method/methods/to_source.rb +30 -0
- data/lib/sourcify/lib/sourcify/method/parser.rb +110 -0
- data/lib/sourcify/lib/sourcify/method/parser/converter.rb +8 -0
- data/lib/sourcify/lib/sourcify/method/parser/raw_scanner.rb +2494 -0
- data/lib/sourcify/lib/sourcify/method/parser/raw_scanner.rl +144 -0
- data/lib/sourcify/lib/sourcify/method/parser/raw_scanner_extensions.rb +68 -0
- data/lib/sourcify/lib/sourcify/method/parser/scanner.rb +52 -0
- data/lib/sourcify/lib/sourcify/method/parser/source_code.rb +8 -0
- data/lib/sourcify/lib/sourcify/patches.rb +63 -0
- data/lib/sourcify/lib/sourcify/proc.rb +183 -0
- data/lib/sourcify/lib/sourcify/proc/methods.rb +3 -0
- data/lib/sourcify/lib/sourcify/proc/methods/source_location.rb +61 -0
- data/lib/sourcify/lib/sourcify/proc/methods/to_raw_source.rb +20 -0
- data/lib/sourcify/lib/sourcify/proc/methods/to_sexp.rb +40 -0
- data/lib/sourcify/lib/sourcify/proc/methods/to_source.rb +48 -0
- data/lib/sourcify/lib/sourcify/proc/parser.rb +51 -0
- data/lib/sourcify/lib/sourcify/proc/parser/converter.rb +8 -0
- data/lib/sourcify/lib/sourcify/proc/parser/normalizer.rb +43 -0
- data/lib/sourcify/lib/sourcify/proc/parser/raw_scanner.rb +2498 -0
- data/lib/sourcify/lib/sourcify/proc/parser/raw_scanner.rl +149 -0
- data/lib/sourcify/lib/sourcify/proc/parser/raw_scanner_extensions.rb +74 -0
- data/lib/sourcify/lib/sourcify/proc/parser/scanner.rb +49 -0
- data/lib/sourcify/lib/sourcify/proc/parser/source_code.rb +8 -0
- data/lib/sourcify/lib/sourcify/version.rb +3 -0
- data/lib/sourcify/sourcify.gemspec +31 -0
- data/lib/sourcify/spec/dump_object_space_procs.rb +84 -0
- data/lib/sourcify/spec/method/encoding_from_def_end_block_spec.rb +33 -0
- data/lib/sourcify/spec/method/encoding_from_define_method_spec.rb +37 -0
- data/lib/sourcify/spec/method/others_from_def_end_block_spec.rb +49 -0
- data/lib/sourcify/spec/method/others_from_define_method_spec.rb +63 -0
- data/lib/sourcify/spec/method/raw_scanner/block_comment_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/double_colons_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/double_quote_str_w_interpolation_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/double_quote_str_wo_interpolation_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/heredoc_w_indent_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/heredoc_wo_indent_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/kw_block_start_alias1_spec.rb +20 -0
- data/lib/sourcify/spec/method/raw_scanner/kw_block_start_alias2_spec.rb +20 -0
- data/lib/sourcify/spec/method/raw_scanner/per_line_comment_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/single_quote_str_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/slash_operator_spec.rb +8 -0
- data/lib/sourcify/spec/method/raw_scanner/spec_helper.rb +80 -0
- data/lib/sourcify/spec/method/spec_helper.rb +1 -0
- data/lib/sourcify/spec/method/to_raw_source_spec.rb +31 -0
- data/lib/sourcify/spec/method/to_raw_source_w_specified_strip_enclosure_spec.rb +148 -0
- data/lib/sourcify/spec/method/to_sexp_from_def_end_block_w_variables_spec.rb +46 -0
- data/lib/sourcify/spec/method/to_sexp_from_def_end_block_within_irb_spec.rb +38 -0
- data/lib/sourcify/spec/method/to_sexp_from_def_end_block_within_pry_spec.rb +38 -0
- data/lib/sourcify/spec/method/to_sexp_from_define_method_w_multi_blocks_and_specified_attached_to_spec.rb +56 -0
- data/lib/sourcify/spec/method/to_sexp_from_define_method_w_variables_spec.rb +52 -0
- data/lib/sourcify/spec/method/to_sexp_from_define_method_within_irb_spec.rb +42 -0
- data/lib/sourcify/spec/method/to_sexp_from_define_method_within_pry_spec.rb +42 -0
- data/lib/sourcify/spec/method/to_sexp_w_specified_strip_enclosure_spec.rb +74 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_19_extras_spec.rb +23 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_begin_spec.rb +35 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_case_spec.rb +35 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_class_spec.rb +51 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_do_end_block_spec.rb +33 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_for_spec.rb +126 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_if_spec.rb +83 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_literal_keyword_spec.rb +141 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_method_spec.rb +33 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_module_spec.rb +59 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_unless_spec.rb +83 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_until_spec.rb +179 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_nested_while_spec.rb +179 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_w_singleton_method_spec.rb +19 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_within_irb_spec.rb +30 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_block_within_pry_spec.rb +45 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_w_multi_blocks_and_many_matches_spec.rb +30 -0
- data/lib/sourcify/spec/method/to_source_from_def_end_w_multi_blocks_and_single_match_spec.rb +36 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_braced_block_spec.rb +113 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_do_end_block_spec.rb +145 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_many_matches_spec.rb +56 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_single_match_spec.rb +73 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_many_matches_spec.rb +36 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_no_match_spec.rb +36 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_and_single_match_spec.rb +28 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_attached_to_spec.rb +103 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_many_matches_spec.rb +36 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_no_match_spec.rb +36 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_body_matcher_and_single_match_spec.rb +28 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_w_multi_blocks_and_specified_ignore_nested_spec.rb +36 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_within_irb_spec.rb +32 -0
- data/lib/sourcify/spec/method/to_source_from_define_method_within_pry_spec.rb +49 -0
- data/lib/sourcify/spec/method/to_source_magic_file_var_spec.rb +176 -0
- data/lib/sourcify/spec/method/to_source_magic_line_var_spec.rb +298 -0
- data/lib/sourcify/spec/method/to_source_w_specified_strip_enclosure_spec.rb +39 -0
- data/lib/sourcify/spec/no_method/unsupported_platform_spec.rb +26 -0
- data/lib/sourcify/spec/proc/19x_extras.rb +27 -0
- data/lib/sourcify/spec/proc/created_on_the_fly_proc_spec.rb +80 -0
- data/lib/sourcify/spec/proc/encoding_spec.rb +36 -0
- data/lib/sourcify/spec/proc/others_spec.rb +40 -0
- data/lib/sourcify/spec/proc/raw_scanner/block_comment_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/double_colons_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/double_quote_str_w_interpolation_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/double_quote_str_wo_interpolation_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/heredoc_w_indent_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/heredoc_wo_indent_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/kw_block_start_alias1_spec.rb +20 -0
- data/lib/sourcify/spec/proc/raw_scanner/kw_block_start_alias2_spec.rb +20 -0
- data/lib/sourcify/spec/proc/raw_scanner/per_line_comment_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/single_quote_str_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/slash_operator_spec.rb +8 -0
- data/lib/sourcify/spec/proc/raw_scanner/spec_helper.rb +63 -0
- data/lib/sourcify/spec/proc/readme +5 -0
- data/lib/sourcify/spec/proc/spec_helper.rb +1 -0
- data/lib/sourcify/spec/proc/to_raw_source_spec.rb +33 -0
- data/lib/sourcify/spec/proc/to_raw_source_w_specified_strip_enclosure_spec.rb +69 -0
- data/lib/sourcify/spec/proc/to_sexp_from_multi_blocks_w_specified_attached_to_spec.rb +46 -0
- data/lib/sourcify/spec/proc/to_sexp_variables_spec.rb +146 -0
- data/lib/sourcify/spec/proc/to_sexp_w_specified_strip_enclosure_spec.rb +60 -0
- data/lib/sourcify/spec/proc/to_sexp_within_irb_spec.rb +146 -0
- data/lib/sourcify/spec/proc/to_sexp_within_pry_spec.rb +149 -0
- data/lib/sourcify/spec/proc/to_source_from_braced_block_w_nested_braced_block_spec.rb +33 -0
- data/lib/sourcify/spec/proc/to_source_from_braced_block_w_nested_hash_spec.rb +82 -0
- data/lib/sourcify/spec/proc/to_source_from_braced_block_wo_nesting_complication_spec.rb +46 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_begin_spec.rb +35 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_case_spec.rb +35 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_class_spec.rb +89 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_do_end_block_spec.rb +33 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_for_spec.rb +132 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_if_spec.rb +87 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_literal_keyword_spec.rb +103 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_method_spec.rb +33 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_module_spec.rb +49 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_unless_spec.rb +87 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_until_spec.rb +189 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_w_nested_while_spec.rb +189 -0
- data/lib/sourcify/spec/proc/to_source_from_do_end_block_wo_nesting_complication_spec.rb +46 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_many_matches_spec.rb +43 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_single_match_spec.rb +20 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_and_many_matches_spec.rb +45 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_and_no_match_spec.rb +45 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_and_single_match_spec.rb +22 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_attached_to_spec.rb +84 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_body_matcher_and_many_matches_spec.rb +45 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_body_matcher_and_no_match_spec.rb +45 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_body_matcher_and_single_match_spec.rb +22 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_blocks_w_specified_ignore_nested_spec.rb +43 -0
- data/lib/sourcify/spec/proc/to_source_from_multi_do_end_blocks_w_single_match_spec.rb +31 -0
- data/lib/sourcify/spec/proc/to_source_magic_file_var_spec.rb +127 -0
- data/lib/sourcify/spec/proc/to_source_magic_line_var_spec.rb +127 -0
- data/lib/sourcify/spec/proc/to_source_variables_spec.rb +29 -0
- data/lib/sourcify/spec/proc/to_source_w_specified_strip_enclosure_spec.rb +33 -0
- data/lib/sourcify/spec/proc/to_source_within_irb_spec.rb +38 -0
- data/lib/sourcify/spec/proc/to_source_within_pry_spec.rb +61 -0
- data/lib/sourcify/spec/raw_scanner/block_comment_shared_spec.rb +57 -0
- data/lib/sourcify/spec/raw_scanner/double_colons_shared_spec.rb +11 -0
- data/lib/sourcify/spec/raw_scanner/double_quote_str_w_interpolation_shared_spec.rb +60 -0
- data/lib/sourcify/spec/raw_scanner/double_quote_str_wo_interpolation_shared_spec.rb +86 -0
- data/lib/sourcify/spec/raw_scanner/heredoc_w_indent_shared_spec.rb +69 -0
- data/lib/sourcify/spec/raw_scanner/heredoc_wo_indent_shared_spec.rb +70 -0
- data/lib/sourcify/spec/raw_scanner/kw_block_start_alias1_shared_spec.rb +73 -0
- data/lib/sourcify/spec/raw_scanner/kw_block_start_alias2_shared_spec.rb +73 -0
- data/lib/sourcify/spec/raw_scanner/per_line_comment_shared_spec.rb +32 -0
- data/lib/sourcify/spec/raw_scanner/shared_specs.rb +3 -0
- data/lib/sourcify/spec/raw_scanner/single_quote_str_shared_spec.rb +79 -0
- data/lib/sourcify/spec/raw_scanner/slash_operator_shared_spec.rb +71 -0
- data/lib/sourcify/spec/run_build.sh +25 -0
- data/lib/sourcify/spec/spec_helper.rb +130 -0
- data/lib/symbolic/symbolic.rb +248 -0
- data/lib/symbolic/visitor.rb +51 -0
- data/lib/translator/block_translator.rb +123 -0
- data/lib/translator/command_translator.rb +421 -0
- data/lib/translator/last_returns_visitor.rb +57 -0
- data/lib/translator/local_variables_enumerator.rb +35 -0
- data/lib/translator/method_translator.rb +24 -0
- data/lib/translator/translator.rb +49 -0
- data/lib/type_aware_array.rb +71 -0
- data/lib/types/array_type.rb +51 -0
- data/lib/types/class_type.rb +128 -0
- data/lib/types/object_tracer.rb +162 -0
- data/lib/types/primitive_type.rb +73 -0
- data/lib/types/ruby_extension.rb +67 -0
- data/lib/types/ruby_type.rb +45 -0
- data/lib/types/type_inference.rb +382 -0
- data/lib/types/union_type.rb +155 -0
- 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
|
data/lib/ast/visitor.rb
ADDED
@@ -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
|