rgo-lang 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitattributes +2 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +9 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +21 -0
- data/Rakefile +28 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/rgo +15 -0
- data/lib/rgo.rb +7 -0
- data/lib/rgo/compile.rb +340 -0
- data/lib/rgo/node.rb +11 -0
- data/lib/rgo/parser.rb +847 -0
- data/lib/rgo/tokenizer.rb +105 -0
- data/lib/rgo/version.rb +3 -0
- data/parse.y +224 -0
- data/rgo.gemspec +29 -0
- data/samples/1_hello_world.go +7 -0
- data/samples/1_hello_world.rgo +7 -0
- data/samples/3_variables.go +17 -0
- data/samples/3_variables.rgo +17 -0
- data/samples/4_if_else.go +15 -0
- data/samples/4_if_else.rgo +15 -0
- data/samples/5_constants.go +15 -0
- data/samples/5_constants.rgo +15 -0
- data/samples/6_functions.go +21 -0
- data/samples/6_functions.rgo +23 -0
- data/samples/7_methods.go +25 -0
- data/samples/7_methods.rgo +28 -0
- data/std/fmt.rgo +8 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 49cf1b3909a9929f80a298023b6b670175718b022dfde44a50b244debfb21796
|
4
|
+
data.tar.gz: 18fb793ded646ec98e10bac37ad0977784d02de29ad0251c5cf6e59a8d0be3dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f03d84532af30021f877913dbbcbeabd9de3c4fd047995aebe2cb283ed31ee4e52ea493dca0f50d7fe54650bd368d58c8360a6732986dd18d29190c8076b8fad
|
7
|
+
data.tar.gz: 6bea210b2aeea3ee35e3e88bb9a49050e2a0d40b3d3ae99b8df38d66556dc477efadbfd8475a7a86f50d64e0126dad32f191576326218c6003c10026dc0f5e19
|
data/.gitattributes
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rgo-lang (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
racc (1.4.16)
|
11
|
+
rake (12.3.3)
|
12
|
+
rspec (3.9.0)
|
13
|
+
rspec-core (~> 3.9.0)
|
14
|
+
rspec-expectations (~> 3.9.0)
|
15
|
+
rspec-mocks (~> 3.9.0)
|
16
|
+
rspec-core (3.9.1)
|
17
|
+
rspec-support (~> 3.9.1)
|
18
|
+
rspec-expectations (3.9.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.9.0)
|
21
|
+
rspec-mocks (3.9.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.9.0)
|
24
|
+
rspec-support (3.9.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
racc (~> 1.4.16)
|
31
|
+
rake (~> 12.0)
|
32
|
+
rgo-lang!
|
33
|
+
rspec (~> 3.0)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Mohsen Alizadeh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Rgo
|
2
|
+
|
3
|
+
Ruby like programming language, syntax by Ruby, Performance by Go
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
not published as a gem yet
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
$ rgo hello_world.rgo
|
12
|
+
|
13
|
+
checkout [Samples](samples)
|
14
|
+
|
15
|
+
## TODO
|
16
|
+
|
17
|
+
* function call without parentheses
|
18
|
+
|
19
|
+
## License
|
20
|
+
|
21
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "rgo"
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :generate_parser do
|
8
|
+
`racc parse.y -o lib/rgo/parser.rb`
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :spec
|
12
|
+
|
13
|
+
task :dev do
|
14
|
+
`racc parse.y -o lib/rgo/parser.rb`
|
15
|
+
statements = Rgo::Parser.new.parse(File.read("./samples/4_if_else.rgo"))
|
16
|
+
|
17
|
+
require 'pp'
|
18
|
+
|
19
|
+
pp statements
|
20
|
+
|
21
|
+
compile = Rgo::Compile.new(statements)
|
22
|
+
out = compile.compile
|
23
|
+
|
24
|
+
puts out
|
25
|
+
|
26
|
+
puts compile.functions
|
27
|
+
puts compile.function_to_module_map
|
28
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rgo"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/rgo
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rgo'
|
3
|
+
|
4
|
+
unless ARGV.count == 1
|
5
|
+
puts "help: "
|
6
|
+
puts " rgo hello_world.rgo"
|
7
|
+
|
8
|
+
exit
|
9
|
+
end
|
10
|
+
|
11
|
+
filepath = ARGV.first
|
12
|
+
|
13
|
+
statements = Rgo::Parser.new.parse(File.read(filepath))
|
14
|
+
puts Rgo::Compile.new(statements).compile
|
15
|
+
|
data/lib/rgo.rb
ADDED
data/lib/rgo/compile.rb
ADDED
@@ -0,0 +1,340 @@
|
|
1
|
+
module Rgo
|
2
|
+
class Compile
|
3
|
+
attr_reader :functions, :function_to_module_map, :aliases
|
4
|
+
|
5
|
+
def initialize(statements)
|
6
|
+
@statements = statements
|
7
|
+
@functions = { private: [], public: [] }
|
8
|
+
@function_access_type = :public
|
9
|
+
@current_module = nil
|
10
|
+
@aliases = {}
|
11
|
+
@function_to_module_map = {}
|
12
|
+
@next_func_type = nil
|
13
|
+
@local_variables = []
|
14
|
+
@next_instance_variable_type = nil
|
15
|
+
@current_class = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def compile
|
19
|
+
compile_statements(@statements)
|
20
|
+
end
|
21
|
+
|
22
|
+
def compile_statements(statements, indent = 0)
|
23
|
+
statements = Array(statements) unless statements.is_a? Array
|
24
|
+
statements.flatten!
|
25
|
+
|
26
|
+
out = []
|
27
|
+
|
28
|
+
statements.each do |statement|
|
29
|
+
out << send("compile_#{statement.type}", statement, indent)
|
30
|
+
end
|
31
|
+
|
32
|
+
pretty out, indent
|
33
|
+
end
|
34
|
+
|
35
|
+
def compile_module(node, indent)
|
36
|
+
@current_module = node.name
|
37
|
+
|
38
|
+
out = []
|
39
|
+
out << "package #{node.name.downcase}"
|
40
|
+
out << ""
|
41
|
+
out << compile_statements(node.children, indent)
|
42
|
+
out << ""
|
43
|
+
|
44
|
+
pretty out, indent
|
45
|
+
end
|
46
|
+
|
47
|
+
def compile_include(node, indent)
|
48
|
+
# compile included module to get list of functions
|
49
|
+
|
50
|
+
statements = Rgo::Parser.new.parse(File.read("std/" + node.name.downcase + ".rgo"))
|
51
|
+
compile = Rgo::Compile.new(statements)
|
52
|
+
compile.compile
|
53
|
+
|
54
|
+
compile.functions[:public].each do |func|
|
55
|
+
@function_to_module_map[func] = [node.name, func]
|
56
|
+
end
|
57
|
+
|
58
|
+
compile.aliases.each do |name, func|
|
59
|
+
@function_to_module_map[name] = [node.name, func]
|
60
|
+
end
|
61
|
+
|
62
|
+
"import \"#{node.name.downcase}\""
|
63
|
+
end
|
64
|
+
|
65
|
+
def pretty(lines, indent = 0)
|
66
|
+
out = []
|
67
|
+
|
68
|
+
lines.each do |line|
|
69
|
+
next if line.nil?
|
70
|
+
|
71
|
+
if line.empty?
|
72
|
+
out << ""
|
73
|
+
else
|
74
|
+
out << (" " * indent) + line
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
out.join("\n")
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def compile_integer(node, indent)
|
83
|
+
node.name.to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
def compile_func_call(node, indent)
|
87
|
+
mod, func = @function_to_module_map[node.name]
|
88
|
+
|
89
|
+
out = ""
|
90
|
+
|
91
|
+
if mod.nil?
|
92
|
+
out << func
|
93
|
+
else
|
94
|
+
out << mod.downcase + "." + func.capitalize
|
95
|
+
end
|
96
|
+
|
97
|
+
out << "(" + compile_args(node.children) + ")"
|
98
|
+
|
99
|
+
out
|
100
|
+
end
|
101
|
+
|
102
|
+
def compile_args(nodes)
|
103
|
+
out = []
|
104
|
+
|
105
|
+
nodes.each do |node|
|
106
|
+
out << compile_expression(node, 0)
|
107
|
+
end
|
108
|
+
|
109
|
+
out.join(", ")
|
110
|
+
end
|
111
|
+
|
112
|
+
def compile_func_def(node, indent)
|
113
|
+
@functions[@function_access_type] << node.name
|
114
|
+
@function_to_module_map[node.name] = [nil, node.name]
|
115
|
+
|
116
|
+
args = compile_func_def_args(node.children[0])
|
117
|
+
return_type = @next_func_type.nil? ? "" : @next_func_type[:return].to_s + " "
|
118
|
+
|
119
|
+
|
120
|
+
method_receiver =
|
121
|
+
if @current_class.nil?
|
122
|
+
""
|
123
|
+
else
|
124
|
+
"(s *#{@current_class.downcase}) "
|
125
|
+
end
|
126
|
+
|
127
|
+
out = []
|
128
|
+
out << "func #{method_receiver}#{node.name}(#{args}) #{return_type}{"
|
129
|
+
out << compile_statements(node.children[1], indent + 1)
|
130
|
+
out << "}"
|
131
|
+
|
132
|
+
@next_func_type = nil
|
133
|
+
@local_variables = []
|
134
|
+
|
135
|
+
|
136
|
+
pretty out, indent
|
137
|
+
end
|
138
|
+
|
139
|
+
def compile_func_def_args(nodes)
|
140
|
+
return if nodes.nil? || nodes.empty?
|
141
|
+
|
142
|
+
raise "@next_func_type is empty" if @next_func_type.nil?
|
143
|
+
|
144
|
+
pp nodes
|
145
|
+
|
146
|
+
out = []
|
147
|
+
nodes.each_with_index do |node, i|
|
148
|
+
out << node.name + " " + @next_func_type[:args][i]
|
149
|
+
@local_variables << node.name
|
150
|
+
end
|
151
|
+
|
152
|
+
out.join(", ")
|
153
|
+
end
|
154
|
+
|
155
|
+
def compile_comment(node, indent)
|
156
|
+
if node.name.start_with?(" type (")
|
157
|
+
@next_func_type = {
|
158
|
+
args: node.name.split("(")[1].split(")")[0].split(",").map(&:strip),
|
159
|
+
return: node.name.split("->")[1].strip
|
160
|
+
}
|
161
|
+
|
162
|
+
return
|
163
|
+
elsif node.name.start_with?(" type ")
|
164
|
+
@next_instance_variable_type = node.name.split("type")[1].strip
|
165
|
+
|
166
|
+
return
|
167
|
+
end
|
168
|
+
"//#{node.name}"
|
169
|
+
end
|
170
|
+
|
171
|
+
def compile_assignment(node, indent)
|
172
|
+
out = ""
|
173
|
+
|
174
|
+
unless @local_variables.include?(node.name)
|
175
|
+
out << "var "
|
176
|
+
@local_variables << node.name
|
177
|
+
end
|
178
|
+
|
179
|
+
out << "#{node.name} = #{compile_expression(node.children.first, 0)}"
|
180
|
+
|
181
|
+
out
|
182
|
+
end
|
183
|
+
|
184
|
+
def compile_constant_assignment(node, indent)
|
185
|
+
"const #{node.name.downcase} = #{compile_expression(node.children.first, 0)}"
|
186
|
+
end
|
187
|
+
|
188
|
+
def compile_expression(node, indent)
|
189
|
+
send("compile_#{node.type}", node, indent)
|
190
|
+
end
|
191
|
+
|
192
|
+
def compile_string(node, indent)
|
193
|
+
"\"#{node.name}\""
|
194
|
+
end
|
195
|
+
|
196
|
+
def compile_variable(node, indent)
|
197
|
+
node.name
|
198
|
+
end
|
199
|
+
|
200
|
+
def compile_constant(node, indent)
|
201
|
+
node.name.downcase
|
202
|
+
end
|
203
|
+
|
204
|
+
def compile_boolean(node, indent)
|
205
|
+
node.name.to_s
|
206
|
+
end
|
207
|
+
|
208
|
+
def compile_blank_line(node, indent)
|
209
|
+
""
|
210
|
+
end
|
211
|
+
|
212
|
+
def compile_plus(node, indent)
|
213
|
+
compile_node(node.children[0]) + " + " + compile_node(node.children[1])
|
214
|
+
end
|
215
|
+
|
216
|
+
def compile_multiply(node, indent)
|
217
|
+
compile_node(node.children[0]) + " * " + compile_node(node.children[1])
|
218
|
+
end
|
219
|
+
|
220
|
+
def compile_minus(node, indent)
|
221
|
+
compile_node(node.children[0]) + " - " + compile_node(node.children[1])
|
222
|
+
end
|
223
|
+
|
224
|
+
def compile_divide(node, indent)
|
225
|
+
compile_node(node.children[0]) + " / " + compile_node(node.children[1])
|
226
|
+
end
|
227
|
+
|
228
|
+
def compile_less(node, indent)
|
229
|
+
compile_node(node.children[0]) + " < " + compile_node(node.children[1])
|
230
|
+
end
|
231
|
+
|
232
|
+
def compile_greater(node, indent)
|
233
|
+
compile_node(node.children[0]) + " > " + compile_node(node.children[1])
|
234
|
+
end
|
235
|
+
|
236
|
+
def compile_equal(node, indent)
|
237
|
+
compile_node(node.children[0]) + " == " + compile_node(node.children[1])
|
238
|
+
end
|
239
|
+
|
240
|
+
def compile_mod(node, indent)
|
241
|
+
compile_node(node.children[0]) + " % " + compile_node(node.children[1])
|
242
|
+
end
|
243
|
+
|
244
|
+
def compile_node(node)
|
245
|
+
send("compile_#{node.type}", node, 0)
|
246
|
+
end
|
247
|
+
|
248
|
+
def compile_if(node, indent)
|
249
|
+
out = []
|
250
|
+
out << "if " + compile_node(node.name) + " {"
|
251
|
+
out << " " + compile_node(node.children)
|
252
|
+
out << "}"
|
253
|
+
|
254
|
+
pretty out, indent
|
255
|
+
end
|
256
|
+
|
257
|
+
def compile_if_else(node, indent)
|
258
|
+
out = []
|
259
|
+
out << "if " + compile_node(node.name) + " {"
|
260
|
+
out << " " + compile_node(node.children[0])
|
261
|
+
out << "} else {"
|
262
|
+
out << " " + compile_node(node.children[1])
|
263
|
+
out << "}"
|
264
|
+
|
265
|
+
pretty out, indent
|
266
|
+
end
|
267
|
+
|
268
|
+
def compile_alias(node, indent)
|
269
|
+
@aliases[node.name] = node.children
|
270
|
+
|
271
|
+
nil
|
272
|
+
end
|
273
|
+
|
274
|
+
def compile_return(node, indent)
|
275
|
+
"return " + compile_expression(node.children, indent)
|
276
|
+
end
|
277
|
+
|
278
|
+
def compile_class(node, indent)
|
279
|
+
@current_class = node.name.downcase
|
280
|
+
|
281
|
+
out = []
|
282
|
+
|
283
|
+
out << "type #{node.name.downcase} struct {"
|
284
|
+
out << compile_statements(node.children[0], indent + 1)
|
285
|
+
out << "}"
|
286
|
+
out << ""
|
287
|
+
|
288
|
+
out << compile_statements(node.children[1], indent)
|
289
|
+
|
290
|
+
@current_class = nil
|
291
|
+
|
292
|
+
pretty out, indent
|
293
|
+
end
|
294
|
+
|
295
|
+
def compile_instance_variable_def(node, indent)
|
296
|
+
raise "type missing for instance variable @#{node.name}" if @next_instance_variable_type.nil?
|
297
|
+
|
298
|
+
out = node.name + " " + @next_instance_variable_type
|
299
|
+
|
300
|
+
@next_instance_variable_type = nil
|
301
|
+
|
302
|
+
pretty [out], indent
|
303
|
+
end
|
304
|
+
|
305
|
+
def compile_instance_variable_get(node, indent)
|
306
|
+
"s." + node.name
|
307
|
+
end
|
308
|
+
|
309
|
+
def compile_class_method_call(node, indent)
|
310
|
+
method_name = node.children[0]
|
311
|
+
|
312
|
+
raise "method #{method_name} is not implemented" unless method_name == "new"
|
313
|
+
|
314
|
+
args = node.children[1]
|
315
|
+
|
316
|
+
out = ""
|
317
|
+
out << node.name.downcase
|
318
|
+
out << "{"
|
319
|
+
|
320
|
+
out << args.map do |arg|
|
321
|
+
arg[0].to_s + ": " + compile_expression(arg[1], 0)
|
322
|
+
end.join(", ")
|
323
|
+
|
324
|
+
out << "}"
|
325
|
+
|
326
|
+
out
|
327
|
+
end
|
328
|
+
|
329
|
+
def compile_class_instance_method_call(node, indent)
|
330
|
+
out = node.name
|
331
|
+
out << "."
|
332
|
+
out << node.children[0]
|
333
|
+
|
334
|
+
out << "("
|
335
|
+
out << ")"
|
336
|
+
|
337
|
+
out
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|