gloss 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'rake'
4
+ require 'rake/extensiontask'
5
+ require 'rubygems/package_task'
6
+ require 'rspec/core/rake_task'
7
+ require "bundler/gem_tasks"
8
+
9
+ gem = Gem::Specification.load(File.dirname(__FILE__) + '/gloss.gemspec' )
10
+ Rake::ExtensionTask.new('gloss', gem )
11
+
12
+ Gem::PackageTask.new gem do |pkg|
13
+ pkg.need_zip = pkg.need_tar = false
14
+ end
15
+
16
+ RSpec::Core::RakeTask.new :spec do |spec|
17
+ spec.pattern = 'spec/**/*_spec.rb'
18
+ end
19
+
20
+ task :default => [:spec]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gloss"
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
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gloss"
5
+
6
+ Gloss::CLI.new(ARGV).run
@@ -0,0 +1,15 @@
1
+ CRYSTAL = crystal
2
+ TARGET = ../../lib/gloss.bundle
3
+
4
+ install: all
5
+
6
+ all: clean shards $(TARGET)
7
+
8
+ shards:
9
+ shards
10
+
11
+ $(TARGET): ./src/gloss.cr
12
+ $(CRYSTAL) build --link-flags "-dynamic -bundle -Wl,-undefined,dynamic_lookup" $< -o $(TARGET)
13
+
14
+ clean:
15
+ rm -f ../../**/*.bundle*
@@ -0,0 +1,32 @@
1
+ require "mkmf"
2
+ # $makefile_created = true
3
+ find_executable("crystal") or abort <<~ERR
4
+ You need crystal installed to use this gem.
5
+ Please check out https://crystal-lang.org/ for information on how to install it.
6
+ ERR
7
+ # $LDFLAGS = "-rdynamic -L/usr/local/Cellar/crystal/0.35.1_1/embedded/lib -L/usr/local/lib -lpcre -lgc -lpthread /usr/local/Cellar/crystal/0.35.1_1/src/ext/libcrystal.a -L/usr/local/Cellar/libevent/2.1.12/lib -levent -liconv -ldl -Llib -lgloss"
8
+ # $LDFLAGS << " -Wl,-undefined,dynamic_lookup -Llib -lgloss "
9
+
10
+ create_makefile "gloss"
11
+ # $(CRYSTAL) $< --link-flags "-dynamic -bundle -Wl,-undefined,dynamic_lookup" -o $(TARGET)
12
+
13
+ # I feel like a bad person
14
+ # File.open(File.join(__dir__, "Makefile"), "w") do |f|
15
+ # f.write <<~MAKEFILE
16
+ # CRYSTAL = crystal
17
+ # TARGET = ../../lib/gloss.bundle
18
+
19
+ # install: all
20
+
21
+ # all: clean shards $(TARGET)
22
+
23
+ # shards:
24
+ # shards
25
+
26
+ # $(TARGET): ./src/gloss.cr
27
+ # $(CRYSTAL) build --link-flags "-dynamic -bundle -Wl,-undefined,dynamic_lookup" $< -o $(TARGET)
28
+
29
+ # clean:
30
+ # rm -f ../../**/*.bundle*
31
+ # MAKEFILE
32
+ # end
@@ -0,0 +1,18 @@
1
+ name: gloss
2
+ version: 0.1.0
3
+
4
+ authors:
5
+ - johansenja
6
+
7
+ targets:
8
+ gloss:
9
+ main: ./gloss.cr
10
+ #
11
+ # add dependencies and development_dependencies:
12
+ # dependencies:
13
+ # pg:
14
+ # github: will/crystal-pg
15
+ # version: "~> 0.5"
16
+ #
17
+ # development_dependencies:
18
+ # ...
@@ -0,0 +1,382 @@
1
+ require "compiler/crystal/syntax/*"
2
+ require "json"
3
+
4
+ module Crystal
5
+ abstract class ASTNode
6
+ def to_rb
7
+ Rb::AST::EmptyNode.new(self.class.name)
8
+ end
9
+ end
10
+
11
+ class Nop < ASTNode
12
+ def to_rb
13
+ Rb::AST::Nop.new
14
+ end
15
+ end
16
+
17
+ class Expressions < ASTNode
18
+ def to_rb
19
+ Rb::AST::CollectionNode.new(@expressions.map(&.to_rb))
20
+ end
21
+ end
22
+
23
+ class NilLiteral < ASTNode
24
+ def to_rb
25
+ Rb::AST::LiteralNode.new("nil", Rb::AST::RbLiteral::NilClass)
26
+ end
27
+ end
28
+
29
+ class BoolLiteral < ASTNode
30
+ def to_rb
31
+ Rb::AST::LiteralNode.new(
32
+ @value.inspect,
33
+ @value ? Rb::AST::RbLiteral::TrueClass : Rb::AST::RbLiteral::FalseClass
34
+ )
35
+ end
36
+ end
37
+
38
+ class NumberLiteral < ASTNode
39
+ def to_rb
40
+ Rb::AST::LiteralNode.new(@value, Rb::AST::RbLiteral::Integer)
41
+ end
42
+ end
43
+
44
+ class CharLiteral < ASTNode
45
+ def to_rb
46
+ Rb::AST::LiteralNode.new(@value.inspect, Rb::AST::RbLiteral::String)
47
+ end
48
+ end
49
+
50
+ class StringLiteral < ASTNode
51
+ def to_rb
52
+ Rb::AST::LiteralNode.new(@value.inspect, Rb::AST::RbLiteral::String)
53
+ end
54
+ end
55
+
56
+ class StringInterpolation < ASTNode
57
+ def to_rb
58
+ Rb::AST::StringInterpolation.new(@expressions.map &.to_rb)
59
+ end
60
+ end
61
+
62
+ class SymbolLiteral < ASTNode
63
+ def to_rb
64
+ Rb::AST::LiteralNode.new(":#{@value.to_s}", Rb::AST::RbLiteral::Symbol)
65
+ end
66
+ end
67
+
68
+ class ArrayLiteral < ASTNode
69
+ def to_rb
70
+ Rb::AST::ArrayLiteral.new(@elements.map(&.to_rb))
71
+ end
72
+ end
73
+
74
+ class HashLiteral < ASTNode
75
+ def to_rb
76
+ Rb::AST::HashLiteral.new(@entries.map { |e| { e.key.to_rb, e.value.to_rb }})
77
+ end
78
+ end
79
+
80
+ class NamedTupleLiteral < ASTNode
81
+ def to_rb
82
+ Rb::AST::HashLiteral.new(@entries.map { |e| { e.key, e.value.to_rb }}, frozen: true)
83
+ end
84
+ end
85
+
86
+ class RangeLiteral < ASTNode
87
+ def to_rb
88
+ Rb::AST::RangeLiteral.new(@from.to_rb, @to.to_rb, @exclusive)
89
+ end
90
+ end
91
+
92
+ class RegexLiteral < ASTNode
93
+ def to_rb
94
+ Rb::AST::RegexLiteral.new("//")
95
+ end
96
+ end
97
+
98
+ class TupleLiteral < ASTNode
99
+ def to_rb
100
+ Rb::AST::ArrayLiteral.new(@elements.map(&.to_rb), frozen: true)
101
+ end
102
+ end
103
+
104
+ class Def < ASTNode
105
+ def to_rb
106
+ Rb::AST::DefNode.new(@name, @args.map(&.to_rb), @body.to_rb, receiver.try(&.to_rb),
107
+ return_type.try(&.to_rb), @double_splat.try(&.to_rb))
108
+ end
109
+ end
110
+
111
+ class ClassDef < ASTNode
112
+ def to_rb
113
+ Rb::AST::ClassNode.new(@name.to_rb, @body.to_rb, @superclass.try(&.to_rb), @type_vars, @abstract)
114
+ end
115
+ end
116
+
117
+ class ModuleDef < ASTNode
118
+ def to_rb
119
+ Rb::AST::ModuleNode.new(@name.to_rb, @body.to_rb, @type_vars)
120
+ end
121
+ end
122
+
123
+ class Var < ASTNode
124
+ def to_rb
125
+ Rb::AST::Var.new(@name)
126
+ end
127
+ end
128
+
129
+ class Block < ASTNode
130
+ def to_rb
131
+ Rb::AST::Block.new(@args.map(&.to_rb), @body.to_rb)
132
+ end
133
+ end
134
+
135
+ class Call < ASTNode
136
+ def to_rb
137
+ Rb::AST::Call.new(@obj.try(&.to_rb), @name, @args.map(&.to_rb), @block.try(&.to_rb),
138
+ @block_arg.try(&.to_rb))
139
+ end
140
+ end
141
+
142
+ class NamedArgument < ASTNode
143
+ def to_rb
144
+ Rb::AST::EmptyNode.new(self.class.name)
145
+ end
146
+ end
147
+
148
+ class Arg < ASTNode
149
+ property keyword_arg : Bool = false
150
+
151
+ def to_rb
152
+ Rb::AST::Arg.new(@name, @external_name, @restriction.try(&.to_rb), @default_value.try(&.to_rb))
153
+ end
154
+ end
155
+
156
+ class If < ASTNode
157
+ def to_rb
158
+ Rb::AST::If.new(@cond.to_rb, @then.to_rb, @else.to_rb)
159
+ end
160
+ end
161
+
162
+ class Assign < ASTNode
163
+ def to_rb
164
+ Rb::AST::Assign.new(@target.to_rb, @value.to_rb)
165
+ end
166
+ end
167
+
168
+ class OpAssign < ASTNode
169
+ def to_rb
170
+ Rb::AST::Assign.new(@target.to_rb, @value.to_rb, @op)
171
+ end
172
+ end
173
+
174
+ class MultiAssign < ASTNode
175
+ def to_rb
176
+ Rb::AST::EmptyNode.new(self.class.name)
177
+ end
178
+ end
179
+
180
+ class InstanceVar < ASTNode
181
+ def to_rb
182
+ Rb::AST::InstanceVar.new(@name)
183
+ end
184
+ end
185
+
186
+ class ReadInstanceVar < ASTNode
187
+ def to_rb
188
+ Rb::AST::EmptyNode.new(self.class.name)
189
+ end
190
+ end
191
+
192
+ class ClassVar < ASTNode
193
+ def to_rb
194
+ Rb::AST::EmptyNode.new(self.class.name)
195
+ end
196
+ end
197
+
198
+ class Global < ASTNode
199
+ def to_rb
200
+ Rb::AST::EmptyNode.new(self.class.name)
201
+ end
202
+ end
203
+
204
+ class Annotation < ASTNode
205
+ def to_rb
206
+ Rb::AST::EmptyNode.new(self.class.name)
207
+ end
208
+ end
209
+
210
+ class MacroExpression < ASTNode
211
+ def to_rb
212
+ Rb::AST::EmptyNode.new(self.class.name)
213
+ end
214
+ end
215
+
216
+ class MacroIf < ASTNode
217
+ def to_rb
218
+ Rb::AST::MacroIf.new(@cond.to_rb, @then.to_rb, @else.to_rb)
219
+ end
220
+ end
221
+
222
+ class MacroFor < ASTNode
223
+ def to_rb
224
+ Rb::AST::MacroFor.new(@vars.map(&.to_rb), @exp.to_rb, @body.to_rb)
225
+ end
226
+ end
227
+
228
+ class MacroVar < ASTNode
229
+ def to_rb
230
+ Rb::AST::EmptyNode.new(self.class.name)
231
+ end
232
+ end
233
+
234
+ class MacroExpression < ASTNode
235
+ def to_rb
236
+ Rb::AST::MacroExpression.new(@exp.to_rb, @output)
237
+ end
238
+ end
239
+
240
+ class MacroLiteral < ASTNode
241
+ def to_rb
242
+ Rb::AST::MacroLiteral.new(@value)
243
+ end
244
+ end
245
+
246
+ class Annotation < ASTNode
247
+ def to_rb
248
+ Rb::AST::EmptyNode.new(self.class.name)
249
+ end
250
+ end
251
+
252
+ class EnumDef < ASTNode
253
+ def to_rb
254
+ Rb::AST::Enum.new(@name, @members.map(&.to_rb))
255
+ end
256
+ end
257
+
258
+ class Path < ASTNode
259
+ def to_rb
260
+ Rb::AST::Path.new(full_name)
261
+ end
262
+
263
+ def full_name
264
+ @names.join("::")
265
+ end
266
+
267
+ delegate :to_json, to: :full_name
268
+ end
269
+
270
+ class Require < ASTNode
271
+ def to_rb
272
+ Rb::AST::Require.new(@string)
273
+ end
274
+ end
275
+
276
+ class TypeDeclaration < ASTNode
277
+ def to_rb
278
+ Rb::AST::TypeDeclaration.new(@var.to_rb, @declared_type.to_rb, @value.try(&.to_rb))
279
+ end
280
+ end
281
+
282
+ class Case < ASTNode
283
+ def to_rb
284
+ Rb::AST::Case.new(
285
+ @cond.try(&.to_rb),
286
+ @whens.map(&.to_rb),
287
+ @else.try(&.to_rb),
288
+ @exhaustive
289
+ )
290
+ end
291
+ end
292
+
293
+ class When < ASTNode
294
+ def to_rb
295
+ Rb::AST::When.new(
296
+ @conds.map(&.to_rb),
297
+ @body.to_rb,
298
+ @exhaustive
299
+ )
300
+ end
301
+ end
302
+
303
+ {% for class_name in %w[Splat DoubleSplat Not] %}
304
+ class {{class_name.id}} < UnaryExpression
305
+ def to_rb
306
+ {% if class_name == "Splat" %}
307
+ op = "*"
308
+ {% elsif class_name == "DoubleSplat" %}
309
+ op = "**"
310
+ {% else %}
311
+ op = "!"
312
+ {% end %}
313
+ # for some of the other unary expressions, parentheses are normally used by convention eg.
314
+ # pointerof - but they are definitely lower priority
315
+ requires_parentheses = true
316
+ Rb::AST::UnaryExpr.new(
317
+ @exp.to_rb,
318
+ op,
319
+ requires_parentheses
320
+ )
321
+ end
322
+ end
323
+ {% end %}
324
+
325
+ {% for class_name in %w[And Or] %}
326
+ class {{class_name.id}} < BinaryOp
327
+ def to_rb
328
+ Rb::AST::BinaryOp.new(
329
+ {% if class_name == "And" %}
330
+ "&&",
331
+ {% else %}
332
+ "||",
333
+ {% end %}
334
+ @left.to_rb,
335
+ @right.to_rb
336
+ )
337
+ end
338
+ end
339
+ {% end %}
340
+
341
+ {% for class_name in %w[Return Break Next] %}
342
+ class {{class_name.id}} < ControlExpression
343
+ def to_rb
344
+ Rb::AST::{{class_name.id}}.new(@exp.try(&.to_rb))
345
+ end
346
+ end
347
+ {% end %}
348
+
349
+ class ExceptionHandler < ASTNode
350
+ def to_rb
351
+ Rb::AST::ExceptionHandler.new(@body.to_rb, @rescues.try(&.map(&.to_rb)), @else.try(&.to_rb),
352
+ @ensure.try(&.to_rb))
353
+ end
354
+ end
355
+
356
+ class Rescue < ASTNode
357
+ def to_rb
358
+ Rb::AST::Rescue.new(@body.to_rb, @types.try(&.map(&.to_rb)), @name)
359
+ end
360
+ end
361
+
362
+ {% for class_name in %w[ProcNotation Macro OffsetOf VisibilityModifier IsA RespondsTo
363
+ Select ImplicitObj AnnotationDef While Until Generic UninitializedVar
364
+ ProcLiteral ProcPointer Union Self Yield Include
365
+ Extend LibDef FunDef TypeDef CStructOrUnionDef ExternalVar Alias
366
+ Metaclass Cast NilableCast TypeOf Annotation
367
+ Underscore MagicConstant Asm AsmOperand] %}
368
+ class {{class_name.id}} < ASTNode
369
+ def to_rb
370
+ Rb::AST::EmptyNode.new(self.class.name)
371
+ end
372
+ end
373
+ {% end %}
374
+
375
+ {% for class_name in %w[PointerOf SizeOf InstanceSizeOf Out MacroVerbatim DoubleSplat] %}
376
+ class {{class_name.id}} < UnaryExpression
377
+ def to_rb
378
+ Rb::AST::EmptyNode.new(self.class.name)
379
+ end
380
+ end
381
+ {% end %}
382
+ end