crokus 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e16a6b8011982817b059498e6c02e6ad1e93026952740a3d914401c3d0993e2
4
+ data.tar.gz: 7a08542bec75264d72199ba649b751f2291a4a678475a5841bef89c4d3b30207
5
+ SHA512:
6
+ metadata.gz: 401661cdb8f7b4838a460e44f4fc27718acc2fc19e15bde18fd3614258e49e004242f81a2d36719f0c6f461ed39357cc5ad2f5b76224c3142ea0a358d4f9b3c1
7
+ data.tar.gz: 967ed9eae447a63067c655ca266ba07ecdb4f6ac85549c8e59fe5bccb58a3c5e55e8341bbaa76906282f19e73d0c75365ba75a4c61ac0dd878822d82a076aa0d
data/bin/crokus ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/crokus'
4
+
5
+ Crokus::Runner.run(*ARGV)
data/lib/crokus.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Crokus
2
+ require_relative 'crokus/version'
3
+ require_relative 'crokus/runner'
4
+ end
data/lib/crokus/ast.rb ADDED
@@ -0,0 +1,431 @@
1
+ module Crokus
2
+
3
+ class Ast
4
+ def accept(visitor, arg=nil)
5
+ name = self.class.name.split(/::/)[1]
6
+ visitor.send("visit#{name}".to_sym, self ,arg) # Metaprograming !
7
+ end
8
+
9
+ def str
10
+ ppr=PrettyPrinter.new
11
+ self.accept(ppr)
12
+ end
13
+ end
14
+
15
+ #......... AST nodes ..........
16
+ class DesignUnit < Ast
17
+ attr_accessor :list
18
+ def initialize list=[]
19
+ @list=list
20
+ end
21
+
22
+ def <<(e)
23
+ list << e
24
+ list.flatten!
25
+ end
26
+ end
27
+
28
+ class Ident < Ast
29
+ attr_accessor :tok
30
+ def initialize tok
31
+ @tok=tok
32
+ end
33
+
34
+ def to_s
35
+ @tok.val
36
+ end
37
+ end
38
+
39
+ class Include < Ast
40
+ attr_accessor :name
41
+ attr_accessor :env
42
+ def initialize name,env=nil #local or system
43
+ @name=name
44
+ @env=env
45
+ end
46
+ end
47
+
48
+ class Define < Ast
49
+ attr_accessor :name,:expr
50
+ def initialize n,e
51
+ @name,@expr=n,e
52
+ end
53
+ end
54
+
55
+ #........ types ...........
56
+ class Type < Ast
57
+ attr_accessor :name,:precisions
58
+ def initialize name
59
+ @precisions=[]
60
+ @name=name
61
+ end
62
+ end
63
+
64
+ class Struct < Type
65
+ attr_accessor :decls
66
+ def initialize name=nil,decls=[]
67
+ super(name)
68
+ @decls=decls
69
+ end
70
+ end
71
+
72
+ class PointerTo < Type
73
+ attr_accessor :type
74
+ def initialize t
75
+ @type=t
76
+ end
77
+
78
+ def name
79
+ "#{type.name} *"
80
+ end
81
+ end
82
+
83
+ class ArrayOf < Type
84
+ attr_accessor :size
85
+ def initialize t,size=nil
86
+ super(t)
87
+ @size=size
88
+ end
89
+
90
+ alias :type :name
91
+ end
92
+
93
+ class Casting < Type
94
+ attr_accessor :type,:modifier
95
+ def initialize type,modifier
96
+ @type,@modifier=type,modifier
97
+ end
98
+ end
99
+
100
+ class CastedExpr < Type
101
+ attr_accessor :type,:expr
102
+ def initialize type,expr
103
+ @type,@expr=type,expr
104
+ end
105
+ end
106
+
107
+ class Sizeof < Ast
108
+ attr_accessor :type
109
+ def initialize t
110
+ @type=t
111
+ end
112
+ end
113
+ #..............end of types.................
114
+
115
+ class Decl < Ast
116
+ attr_accessor :var,:type,:init
117
+ def initialize type,var,init=nil
118
+ @var,@type,@init=var,type,init
119
+ end
120
+ end
121
+
122
+ class Typedef < Ast
123
+ attr_accessor :type,:name
124
+ def initialize type,name
125
+ @type,@name=type,name
126
+ end
127
+ end
128
+
129
+ class Function < Ast
130
+ attr_accessor :name,:type,:args,:body
131
+ attr_accessor :cfg
132
+ def initialize name,ret_type,args=[],body=nil
133
+ @name,@type=name,ret_type
134
+ @args=args
135
+ @body=body if body
136
+ end
137
+ end
138
+
139
+ class FunctionProto < Ast
140
+ attr_accessor :name,:type,:args
141
+ def initialize name,ret_type,args=[]
142
+ @name,@type=name,ret_type
143
+ @args=args
144
+ end
145
+ end
146
+
147
+ class FunCall < Ast
148
+ attr_accessor :name,:args
149
+ def initialize name,args=[]
150
+ @name,@args=name,args
151
+ end
152
+ end
153
+
154
+ class FormalArg < Ast
155
+ attr_accessor :name,:type
156
+ def initialize t,n
157
+ @name,@type=n,t
158
+ end
159
+ end
160
+
161
+ #......................................
162
+
163
+ class Body < Ast
164
+ attr_accessor :stmts
165
+ def initialize stmts=[]
166
+ @stmts=stmts
167
+ end
168
+
169
+ def <<(e)
170
+ @stmts << e
171
+ @stmts.flatten!
172
+ end
173
+
174
+ def each(&block)
175
+ @stmts.each(&block)
176
+ end
177
+
178
+ def collect(&block)
179
+ @stmts.collect(&block)
180
+ end
181
+ end
182
+
183
+ class Stmt < Ast
184
+ end
185
+
186
+ class LabeledStmt < Stmt
187
+ attr_accessor :label,:stmt
188
+ def initialize label,stmt
189
+ @label,@stmt=label,stmt
190
+ end
191
+ end
192
+
193
+ class SemicolonStmt < Stmt
194
+ attr_accessor :tok
195
+ def initialize tok
196
+ @tok=tok
197
+ end
198
+ end
199
+
200
+ class CommaStmt < Stmt
201
+ attr_accessor :lhs,:rhs
202
+ def initialize lhs,rhs
203
+ @lhs,@rhs=lhs,rhs
204
+ end
205
+
206
+ def to_list
207
+ list=[]
208
+ list << to_list_rec(@lhs)
209
+ list << to_list_rec(@rhs)
210
+ list.flatten
211
+ end
212
+
213
+ def to_list_rec e
214
+ ret=[]
215
+ if e.is_a? CommaStmt
216
+ ret << e.to_list
217
+ else
218
+ ret << e
219
+ end
220
+ ret
221
+ end
222
+ end
223
+
224
+ class Assign < Stmt
225
+ attr_accessor :lhs,:op,:rhs
226
+ def initialize lhs,op,rhs
227
+ @lhs,@op,@rhs=lhs,op,rhs
228
+ end
229
+ end
230
+
231
+ class PostFixAccu < Assign
232
+ def initialize lhs,tok
233
+ super(lhs,tok,nil)
234
+ end
235
+
236
+ def unfix
237
+ lhs
238
+ end
239
+ end
240
+
241
+ class PreFixAccu < Assign
242
+ def initialize tok,lhs
243
+ super(lhs,tok,nil)
244
+ end
245
+
246
+ def unfix
247
+ lhs
248
+ end
249
+ end
250
+
251
+ class CtrlStmt < Stmt
252
+ end
253
+
254
+ class For < CtrlStmt
255
+ attr_accessor :init,:cond,:increment,:body
256
+ def initialize init=[],cond=nil,increment=nil,body=nil
257
+ @init,@cond,@increment,@body=init,cond,increment,body
258
+ end
259
+ end
260
+
261
+ class While < CtrlStmt
262
+ attr_accessor :cond,:body
263
+ def initialize cond,body
264
+ @cond,@body=cond,body
265
+ end
266
+ end
267
+
268
+ class DoWhile < CtrlStmt
269
+ attr_accessor :cond,:body
270
+ def initialize cond,body=nil
271
+ @cond,@body=cond,body
272
+ end
273
+ end
274
+
275
+ class If < CtrlStmt
276
+ attr_accessor :cond,:body,:else
277
+ def initialize cond,body,else_=nil
278
+ @cond,@body,@else=cond,body,else_
279
+ end
280
+ end
281
+
282
+ class Else < CtrlStmt
283
+ attr_accessor :body
284
+ def initialize body=[]
285
+ @body=body
286
+ end
287
+ end
288
+
289
+ class Switch < CtrlStmt
290
+ attr_accessor :expr,:cases,:default
291
+ def initialize expr,cases=[],default=nil
292
+ @expr,@cases=expr,cases
293
+ @default=nil
294
+ end
295
+ end
296
+
297
+ class Case < CtrlStmt
298
+ attr_accessor :expr,:body
299
+ def initialize e,body
300
+ @expr,@body=e,body
301
+ end
302
+ end
303
+
304
+
305
+ class Return < CtrlStmt
306
+ attr_accessor :expr
307
+ def initialize e
308
+ @expr=e
309
+ end
310
+ end
311
+
312
+ class Break < CtrlStmt
313
+ end
314
+
315
+ class Continue < CtrlStmt
316
+ end
317
+
318
+ class Goto < CtrlStmt
319
+ attr_accessor :label
320
+ def initialize label
321
+ @label=label
322
+ end
323
+ end
324
+
325
+ class LabelledStmt < Stmt
326
+ attr_accessor :stmt
327
+ def initialize stmt
328
+ @stmt=stmt
329
+ end
330
+ end
331
+
332
+ #.................... expressions...................
333
+ class Expr < Ast#decorative
334
+ end
335
+
336
+ class CondExpr < Ast
337
+ attr_accessor :cond,:lhs,:rhs
338
+ def initialize cond,lhs,rhs
339
+ @cond,@lhs,@rhs = cond,lhs,rhs
340
+ end
341
+ end
342
+
343
+ class Binary < Expr
344
+ attr_accessor :op,:lhs,:rhs
345
+ def initialize lhs,op,rhs
346
+ @lhs,@op,@rhs = lhs,op,rhs
347
+ end
348
+ end
349
+
350
+ class Unary < Expr
351
+ attr_accessor :op,:rhs,:postfix
352
+ def initialize op,rhs,postfix=nil
353
+ @op,@rhs=op,rhs
354
+ @postfix=postfix
355
+ end
356
+ end
357
+
358
+ class Deref < Expr
359
+ attr_accessor :expr
360
+ def initialize e
361
+ @expr = e
362
+ end
363
+ end
364
+
365
+ class AddressOf < Expr# &
366
+ attr_accessor :expr
367
+ def initialize expr
368
+ @expr=expr
369
+ end
370
+ end
371
+
372
+ class Indexed < Expr
373
+ attr_accessor :lhs,:rhs
374
+ def initialize l,r
375
+ @lhs,@rhs=l,r
376
+ end
377
+ end
378
+
379
+ class Dotted< Expr
380
+ attr_accessor :lhs,:rhs
381
+ def initialize l,r
382
+ @lhs,@rhs=l,r
383
+ end
384
+ end
385
+
386
+ class Parenth < Expr
387
+ attr_accessor :expr
388
+ def initialize e
389
+ @expr=e
390
+ end
391
+ end
392
+
393
+ class Arrow < Expr
394
+ attr_accessor :lhs,:rhs
395
+ def initialize l,r
396
+ @lhs,@rhs=l,r
397
+ end
398
+ end
399
+
400
+ class ArrayOrStructInit < Expr
401
+ attr_accessor :elements
402
+ def initialize elements=[]
403
+ @elements=elements
404
+ end
405
+ end
406
+
407
+ # literals
408
+ class Literal < Ast
409
+ attr_accessor :tok
410
+ def initialize tok
411
+ @tok=tok
412
+ end
413
+
414
+ def to_s
415
+ @tok.val
416
+ end
417
+ end
418
+
419
+ class IntLit < Literal
420
+ end
421
+
422
+ class FloatLit < Literal
423
+ end
424
+
425
+ class StrLit < Literal
426
+ end
427
+
428
+ class CharLit < Literal
429
+ end
430
+
431
+ end #module