cshape 0.1.0__py3-none-any.whl

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.
cshape.py ADDED
@@ -0,0 +1,1608 @@
1
+
2
+ nl_symbol = "\n"
3
+ indent_symbol = "\t"
4
+
5
+ indent_level = 0
6
+
7
+
8
+
9
+
10
+ def indent_up():
11
+ global indent_level
12
+ indent_level = indent_level + 1
13
+
14
+
15
+ def indent_down():
16
+ global indent_level
17
+ indent_level = indent_level - 1
18
+
19
+
20
+
21
+ def set_nl_symbol(x):
22
+ global nl_symbol
23
+ nl_symbol = x
24
+
25
+
26
+
27
+ def str_indent():
28
+ global indent_level
29
+ global indent_symbol
30
+ return indent_symbol * indent_level
31
+
32
+
33
+ def str_nl_indent(nl=1):
34
+ s = nl_symbol * nl
35
+ if nl > 0:
36
+ s += str_indent()
37
+ return s
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+ legacy_style = {
47
+ 'LINE_BREAK_BEFORE_STRUCT_BRACE': False,
48
+ 'LINE_BREAK_BEFORE_FUNC_BRACE': False,
49
+ 'LINE_BREAK_BEFORE_BLOCK_BRACE': False,
50
+ }
51
+
52
+ modern_style = {
53
+ 'LINE_BREAK_BEFORE_STRUCT_BRACE': True,
54
+ 'LINE_BREAK_BEFORE_FUNC_BRACE': True,
55
+ 'LINE_BREAK_BEFORE_BLOCK_BRACE': True,
56
+ }
57
+
58
+ styles = {
59
+ 'legacy': legacy_style,
60
+ 'modern': modern_style,
61
+ }
62
+
63
+
64
+ # default style is legacy
65
+ styleguide = legacy_style
66
+
67
+
68
+ def render(node, style='legacy', indent='\t', newline='\n'):
69
+ global indent_level, styleguide, nl_symbol, indent_symbol
70
+
71
+ saved = (indent_level, styleguide, nl_symbol, indent_symbol)
72
+ indent_level = 0
73
+ styleguide = styles[style]
74
+ nl_symbol = newline
75
+ indent_symbol = indent
76
+
77
+ try:
78
+ # dispatch through the same wrapper each node type's parent would use
79
+ # to print it, so mark/precedence handling is applied uniformly even
80
+ # when rendering a node standalone (str(node) alone skips it — CType
81
+ # doesn't even define __str__, only to_str())
82
+ if isinstance(node, CType):
83
+ return str_ctype(node)
84
+ if isinstance(node, CValue):
85
+ return str_cvalue(node)
86
+ if isinstance(node, CStmt):
87
+ return str_cstmt(node)
88
+ return str_cdef(node)
89
+ finally:
90
+ indent_level, styleguide, nl_symbol, indent_symbol = saved
91
+
92
+
93
+ def wrap_if(x, cond):
94
+ return "(%s)" % x if cond else x
95
+
96
+
97
+ def str_specs(specs):
98
+ s = ''
99
+ for opt in specs:
100
+ s += opt + ' '
101
+ return s
102
+
103
+
104
+ def with_space(s):
105
+ if s != '':
106
+ return ' ' + s
107
+ return ''
108
+
109
+
110
+
111
+
112
+ def str_gcc_attributes(attributes):
113
+ if attributes == None:
114
+ return ''
115
+
116
+ # Modest attribute -> GCC attribute
117
+ gcc_attributes = {
118
+ # attributes with no parameters
119
+ 'inline': 'always_inline',
120
+ 'noinline': 'noinline',
121
+ 'used': 'used',
122
+ 'unused': 'unused',
123
+ 'packed': 'packed',
124
+ 'deprecated': 'deprecated', # can be with string parameter
125
+ 'weak': 'weak',
126
+
127
+ # attributes with one parameter
128
+ 'section': 'section',
129
+ 'alignment': 'aligned',
130
+ 'optimize': 'optimize',
131
+ }
132
+
133
+ atts = []
134
+ for anno_name in attributes:
135
+ #print(":anno:" + anno_name)
136
+ asset = attributes[anno_name]
137
+ if anno_name in gcc_attributes:
138
+ gcc_att_name = gcc_attributes[anno_name]
139
+
140
+ att_arg = ""
141
+ if asset != {}:
142
+ att_arg = asset.asset
143
+ if isinstance(att_arg, str):
144
+ att_arg = '"%s"' % att_arg
145
+ att_arg = '(' + str(att_arg) + ')'
146
+
147
+ atts.append("%s%s" % (gcc_att_name, str(att_arg)))
148
+
149
+ if atts != []:
150
+ return "__attribute__((" + ", ".join(atts) + "))\n"
151
+
152
+ return ""
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ class CField():
162
+ def __init__(self, id_str, type, specs=None, nl=0):
163
+ #assert(isinstance(type, CType))
164
+ self.id_str = id_str
165
+ self.type = type
166
+ self.specs = specs if specs != None else []
167
+ self.nl = nl
168
+
169
+
170
+
171
+ class CType():
172
+ def __init__(self):
173
+ self.mark = None
174
+ pass
175
+
176
+ def to_str(self, text=''):
177
+ return "<type> " + text
178
+
179
+
180
+ class CTypeNamed(CType):
181
+ def __init__(self, id_str, specs=None):
182
+ assert(isinstance(id_str, str))
183
+ super().__init__()
184
+ self.id_str = id_str
185
+ self.specs = specs if specs != None else []
186
+ self.precedence = 0
187
+
188
+ def to_str(self, text='', with_qualifiers=True):
189
+ # "const int a"
190
+ sstr = ''
191
+ if with_qualifiers:
192
+ # for: const, volatile, restrict
193
+ sstr += str_specs(self.specs)
194
+ return sstr + self.id_str + with_space(text)
195
+
196
+
197
+ class CTypePointer(CType):
198
+ def __init__(self, to, specs=None):
199
+ super().__init__()
200
+ self.to = to
201
+ self.specs = specs if specs != None else []
202
+ self.precedence = 1
203
+
204
+ def to_str(self, text='', with_qualifiers=True):
205
+ # "*volatile p"
206
+ sstr = '*'
207
+ if with_qualifiers:
208
+ sstr += str_specs(self.specs)
209
+ sstr += text
210
+ sstr = wrap_if(sstr, self.to.precedence > self.precedence)
211
+ sstr = str_ctype(self.to, sstr)
212
+ return sstr
213
+
214
+
215
+ class CTypeArray(CType):
216
+ def __init__(self, of, volume=None, specs=[]):
217
+ super().__init__()
218
+ of.specs = specs # array specs is array item specs (!)
219
+ self.of = of
220
+ self.volume = volume
221
+ self.specs = specs if specs != None else []
222
+ self.precedence = 2
223
+
224
+ def to_str(self, text='', with_qualifiers=True):
225
+ text = text + '['
226
+ if self.volume != None:
227
+ text += str_cvalue(self.volume)
228
+ text += ']'
229
+ text = wrap_if(text, self.of.precedence > self.precedence)
230
+ text = str_ctype(self.of, text)
231
+ return text
232
+
233
+
234
+ class CTypeFunc(CType):
235
+ def __init__(self, params, to, volume=None, extra_args=False, specs=None):
236
+ super().__init__()
237
+ self.params = params
238
+ self.to = to
239
+ self.extra_args = extra_args
240
+ self.specs = specs if specs != None else []
241
+ self.precedence = 3
242
+
243
+ def to_str(self, text='', with_qualifiers=True):
244
+ params_text = ''
245
+ i = 0
246
+ params = self.params
247
+ while i < len(params):
248
+ param = params[i]
249
+ p = str_ctype(param.type, text=param.id_str)
250
+ if i > 0:
251
+ params_text += ', ' + p
252
+ else:
253
+ params_text += p
254
+ i = i + 1
255
+
256
+ if self.extra_args:
257
+ params_text += ', ...'
258
+
259
+ if params_text == '':
260
+ params_text = 'void'
261
+
262
+ text = text + '(%s)' % params_text
263
+ text = str_ctype(self.to, text)
264
+ return text
265
+
266
+
267
+ class CTypeStruct(CType):
268
+ def __init__(self, fields, tag, specs=None):
269
+ super().__init__()
270
+ self.fields = fields
271
+ self.tag = tag
272
+ self.specs = specs if specs != None else []
273
+ self.precedence = 0
274
+
275
+ def to_str(self, text='', with_qualifiers=True):
276
+ nl_end = 0
277
+ sstr = '%s' % (self.tag)
278
+ if styleguide['LINE_BREAK_BEFORE_STRUCT_BRACE']:
279
+ sstr += '\n'
280
+ else:
281
+ sstr += ' '
282
+ sstr += '{'
283
+ indent_up()
284
+ i = 0
285
+ nfields = len(self.fields)
286
+ if nfields > 0:
287
+ while i < nfields:
288
+ field = self.fields[i]
289
+ assert(isinstance(field, CField))
290
+ if field.nl > 0:
291
+ nl_end = 1
292
+
293
+ if i > 0 and field.nl == 0:
294
+ if self.fields[i-1].nl == 0:
295
+ sstr += ' '
296
+
297
+ sstr += str_nl_indent(field.nl)
298
+ sstr += str_ctype(field.type, text=field.id_str) + ';'
299
+ i = i + 1
300
+ else:
301
+ sstr += 'uint8_t __placeholder;'
302
+ indent_down()
303
+ sstr += str_nl_indent(nl_end) + '}' + with_space(text)
304
+ return sstr
305
+
306
+
307
+
308
+ class CTypeEnumItem():
309
+ def __init__(self, id, value=None):
310
+ assert(isinstance(id, str))
311
+ assert(value == None or isinstance(value, CValue))
312
+ self.id = id
313
+ self.value = value
314
+ self.nl = 1
315
+
316
+ def __str__(self):
317
+ sstr = self.id
318
+ if self.value != None:
319
+ sstr += ' = ' + str_cvalue(self.value)
320
+ return sstr
321
+
322
+
323
+ class CTypeEnum(CType):
324
+ def __init__(self, items, tag='', specs=None):
325
+ super().__init__()
326
+ self.items = items
327
+ self.tag = tag
328
+ self.specs = specs if specs != None else []
329
+ self.precedence = 0
330
+
331
+ def to_str(self, text='', with_qualifiers=True):
332
+ nl_end = 0
333
+ sstr = 'enum %s' % (self.tag)
334
+ if styleguide['LINE_BREAK_BEFORE_STRUCT_BRACE']:
335
+ sstr += '\n'
336
+ else:
337
+ sstr += ' '
338
+ sstr += '{'
339
+ indent_up()
340
+ i = 0
341
+ nitems = len(self.items)
342
+
343
+ while i < nitems:
344
+ item = self.items[i]
345
+ assert(isinstance(item, CTypeEnumItem))
346
+ if item.nl > 0:
347
+ nl_end = 1
348
+
349
+ if i > 0 and item.nl == 0:
350
+ if self.items[i-1].nl == 0:
351
+ sstr += ' '
352
+
353
+ sstr += str_nl_indent(item.nl)
354
+ sstr += str(item)
355
+ i = i + 1
356
+
357
+ indent_down()
358
+ sstr += str_nl_indent(nl_end) + '}' + with_space(text)
359
+ return sstr
360
+
361
+
362
+
363
+
364
+ def str_ctype(t, text='', with_qualifiers=True):
365
+ assert(t != None)
366
+ assert(text != None)
367
+ #assert(isinstance(t, dict))
368
+
369
+ sstr = t.to_str(text, with_qualifiers=with_qualifiers)
370
+ if t.mark:
371
+ sstr = '/*%s*/' % t.mark + sstr
372
+ return sstr
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+ def print_list_items(_list, method):
383
+ sstr = ''
384
+ i = 0
385
+ while i < len(_list):
386
+ item = _list[i]
387
+ if i > 0:
388
+ sstr += ", "
389
+ sstr += method(item)
390
+ i += 1
391
+ return sstr
392
+
393
+
394
+
395
+
396
+
397
+
398
+ valuePrecedenceMax = 15
399
+
400
+
401
+ class KV():
402
+ def __init__(self, key, value, nl):
403
+ self.key = key
404
+ self.value = value
405
+ self.nl = nl
406
+
407
+
408
+ class CValue():
409
+ def __init__(self):
410
+ self.mark = None
411
+
412
+
413
+ class CValueNamed(CValue):
414
+ def __init__(self, id_str):
415
+ super().__init__()
416
+ self.id_str = id_str
417
+ self.precedence = 15
418
+
419
+ def __str__(self):
420
+ return self.id_str
421
+
422
+
423
+
424
+
425
+ class CValueInteger(CValue):
426
+ def __init__(self, number, as_hex=False, nsigns=0, suffix=''):
427
+ super().__init__()
428
+ assert(isinstance(number, int))
429
+ assert(isinstance(as_hex, bool))
430
+ self.precedence = 15
431
+ self.number = number
432
+ self.as_hex = as_hex
433
+ self.nsigns = nsigns
434
+ self.suffix = suffix
435
+
436
+ def __str__(self):
437
+ sstr = ''
438
+ if self.as_hex:
439
+ fmt = "0x%%0%dX" % self.nsigns
440
+ sstr += (fmt % self.number)
441
+ else:
442
+ sstr += str(self.number)
443
+ return sstr + self.suffix
444
+
445
+
446
+
447
+
448
+ def string_literal_prefix(width):
449
+ if width > 16: return "U"
450
+ if width > 8: return "u"
451
+ return ""
452
+
453
+
454
+ class CValueString(CValue):
455
+ def __init__(self, string, width):
456
+ assert(isinstance(string, str))
457
+ super().__init__()
458
+ self.string = string
459
+ self.width = width
460
+ self.precedence = 15
461
+
462
+ def __str__(self):
463
+ return '%s"%s"' % (string_literal_prefix(self.width), self.string)
464
+
465
+
466
+ def code_to_char(cc):
467
+ if cc < 0x20:
468
+ if cc == 0x07: return "\\a" # bell
469
+ elif cc == 0x08: return "\\b" # backspace
470
+ elif cc == 0x09: return "\\t" # horizontal tab
471
+ elif cc == 0x0A: return "\\n" # line feed
472
+ elif cc == 0x0B: return "\\v" # vertical tab
473
+ elif cc == 0x0C: return "\\f" # form feed
474
+ elif cc == 0x0D: return "\\r" # carriage return
475
+ elif cc == 0x1B: return "\\e" # escape
476
+ else: return "\\x%X" % cc
477
+
478
+ elif cc <= 0x7E :
479
+ sym = chr(cc)
480
+ if sym == '\\': return '\\\\'
481
+ elif sym == '"': return '\\"'
482
+ else: return sym
483
+
484
+ elif cc != 0:
485
+ return chr(cc)
486
+
487
+
488
+ class CValueChar(CValue):
489
+ def __init__(self, cc, width=8):
490
+ assert(isinstance(cc, int))
491
+ super().__init__()
492
+ self.char_code = cc
493
+ self.width = width
494
+ self.precedence = 15
495
+
496
+ def __str__(self):
497
+ return "%s'%s'" % (string_literal_prefix(self.width), code_to_char(self.char_code))
498
+
499
+
500
+
501
+ class CValueArray(CValue):
502
+ def __init__(self, items):
503
+ super().__init__()
504
+ self.items = items
505
+ self.precedence = 15
506
+
507
+ def __str__(self):
508
+ indent_up()
509
+ s_items = '' #print_list_items(self.items, str_cvalue)
510
+
511
+ i = 0
512
+ while i < len(self.items):
513
+ item = self.items[i]
514
+ if i > 0:
515
+ s_items += ","
516
+ if item.nl == 0:
517
+ s_items += " "
518
+ s_items += str_nl_indent(item.nl)
519
+ s_items += str_cvalue(item)
520
+ i = i + 1
521
+ indent_down()
522
+
523
+ if s_items == '':
524
+ s_items = '0'
525
+
526
+ if len(self.items) > 0:
527
+ nn = 1 if len(self.items) > 0 and self.items[0].nl > 0 else 0
528
+ s_items += str_nl_indent(nn)
529
+
530
+ return '{%s}' % s_items
531
+
532
+
533
+ class CValueStruct(CValue):
534
+ def __init__(self, items):
535
+ super().__init__()
536
+ self.items = items
537
+ self.precedence = 15
538
+
539
+ def __str__(self):
540
+
541
+ indent_up()
542
+
543
+ s_items = ''
544
+ i = 0
545
+ item = None
546
+ need_nl = 0
547
+ while i < len(self.items):
548
+ item = self.items[i]
549
+ if i > 0:
550
+ s_items += ","
551
+ if item.nl == 0:
552
+ s_items += " "
553
+ s_items += str_nl_indent(item.nl)
554
+ s_items += ".%s = %s" % (item.key, str_cvalue(item.value))
555
+ i += 1
556
+
557
+ indent_down()
558
+
559
+ if item != None and item.nl > 0:
560
+ need_nl = 1
561
+
562
+ if s_items == '':
563
+ s_items = '0'
564
+ sstr = '{'
565
+ sstr += s_items
566
+ sstr += str_nl_indent(1 if len(self.items) > 0 and self.items[0].nl > 0 else 0)
567
+ sstr += '}'
568
+ return sstr
569
+
570
+
571
+
572
+
573
+
574
+ class CValueSubexpr(CValue):
575
+ def __init__(self, value):
576
+ assert(isinstance(value, CValue))
577
+ super().__init__()
578
+ self.value = value
579
+ self.precedence = 15
580
+
581
+ def __str__(self):
582
+ return '(%s)' % str_cvalue(self.value, ext_precedence=self.precedence)
583
+
584
+
585
+
586
+ class CValueCall(CValue):
587
+ def __init__(self, left, args):
588
+ assert(isinstance(left, CValue))
589
+ assert(isinstance(args, list))
590
+ super().__init__()
591
+ self.left = left
592
+ self.args = args
593
+ self.precedence = 14
594
+
595
+ def __str__(self):
596
+ return '%s(%s)' % (str_cvalue(self.left, ext_precedence=self.precedence), print_list_items(self.args, str_cvalue))
597
+
598
+
599
+ class CValueAccess(CValue):
600
+ def __init__(self, left, field_id_str):
601
+ assert(isinstance(field_id_str, str))
602
+ assert(isinstance(left, CValue))
603
+ super().__init__()
604
+ self.left = left
605
+ self.field_id_str = field_id_str
606
+ self.precedence = 14
607
+
608
+ def __str__(self):
609
+ return '%s.%s' % (str_cvalue(self.left, ext_precedence=self.precedence), self.field_id_str)
610
+
611
+
612
+ class CValueAccessPtr(CValue):
613
+ def __init__(self, left, field_id_str):
614
+ assert(isinstance(field_id_str, str))
615
+ assert(isinstance(left, CValue))
616
+ super().__init__()
617
+ self.left = left
618
+ self.field_id_str = field_id_str
619
+ self.precedence = 14
620
+
621
+ def __str__(self):
622
+ return '%s->%s' % (str_cvalue(self.left, ext_precedence=self.precedence), self.field_id_str)
623
+
624
+
625
+ class CValueIndex(CValue):
626
+ def __init__(self, left, index):
627
+ assert(isinstance(left, CValue))
628
+ assert(isinstance(index, CValue))
629
+ super().__init__()
630
+ self.left = left
631
+ self.index = index
632
+ self.precedence = 14
633
+
634
+ def __str__(self):
635
+ lstr = str_cvalue(self.left, ext_precedence=self.precedence)
636
+ return '%s[%s]' % (lstr, str_cvalue(self.index))
637
+
638
+
639
+ class CValueCast(CValue):
640
+ def __init__(self, type, value):
641
+ assert(isinstance(type, CType))
642
+ assert(isinstance(value, CValue))
643
+ super().__init__()
644
+ self.type = type
645
+ self.value = value
646
+ self.precedence = 13
647
+
648
+ def __str__(self):
649
+ vstr = str_cvalue(self.value, ext_precedence=self.precedence)
650
+ return '(%s)%s' % (str_ctype(self.type, with_qualifiers=False), vstr)
651
+
652
+
653
+ class CValueRef(CValue):
654
+ def __init__(self, value):
655
+ assert(isinstance(value, CValue))
656
+ super().__init__()
657
+ self.value = value
658
+ self.precedence = 13
659
+
660
+ def __str__(self):
661
+ return '&%s' % str_cvalue(self.value, ext_precedence=self.precedence)
662
+
663
+
664
+ class CValueDeref(CValue):
665
+ def __init__(self, value):
666
+ assert(isinstance(value, CValue))
667
+ super().__init__()
668
+ self.value = value
669
+ self.precedence = 13
670
+
671
+ def __str__(self):
672
+ return '*%s' % str_cvalue(self.value, ext_precedence=self.precedence)
673
+
674
+
675
+ class CValueInc(CValue):
676
+ def __init__(self, value):
677
+ assert(isinstance(value, CValue))
678
+ super().__init__()
679
+ self.value = value
680
+ self.precedence = 13
681
+
682
+ def __str__(self):
683
+ return '++%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
684
+
685
+
686
+ class CValueDec(CValue):
687
+ def __init__(self, value):
688
+ assert(isinstance(value, CValue))
689
+ super().__init__()
690
+ self.value = value
691
+ self.precedence = 13
692
+
693
+ def __str__(self):
694
+ return '--%s' % str_cvalue(self.value, ext_precedence=self.precedence)
695
+
696
+
697
+ class CValuePositive(CValue):
698
+ def __init__(self, value):
699
+ assert(isinstance(value, CValue))
700
+ super().__init__()
701
+ self.value = value
702
+ self.precedence = 13
703
+
704
+ def __str__(self):
705
+ return '+%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
706
+
707
+
708
+ class CValueNegative(CValue):
709
+ def __init__(self, value):
710
+ assert(isinstance(value, CValue))
711
+ super().__init__()
712
+ self.value = value
713
+ self.precedence = 13
714
+
715
+ def __str__(self):
716
+ return '-%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
717
+
718
+
719
+ class CValueNotLogical(CValue):
720
+ def __init__(self, value):
721
+ assert(isinstance(value, CValue))
722
+ super().__init__()
723
+ self.value = value
724
+ self.precedence = 13
725
+
726
+ def __str__(self):
727
+ return '!%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
728
+
729
+
730
+ class CValueNotBitwise(CValue):
731
+ def __init__(self, value):
732
+ assert(isinstance(value, CValue))
733
+ super().__init__()
734
+ self.value = value
735
+ self.precedence = 13
736
+
737
+ def __str__(self):
738
+ return '~%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
739
+
740
+
741
+ class CValueSizeofValue(CValue):
742
+ def __init__(self, ofvalue):
743
+ assert(isinstance(ofvalue, CValue))
744
+ super().__init__()
745
+ self.ofvalue = ofvalue
746
+ self.precedence = 13
747
+
748
+ def __str__(self):
749
+ return 'sizeof %s' % (str_cvalue(self.ofvalue))
750
+
751
+
752
+ class CValueSizeofType(CValue):
753
+ def __init__(self, oftype):
754
+ assert(isinstance(oftype, CType))
755
+ super().__init__()
756
+ self.oftype = oftype
757
+ self.precedence = 13
758
+
759
+ def __str__(self):
760
+ return 'sizeof(%s)' % (str_ctype(self.oftype))
761
+
762
+
763
+ class CValueMul(CValue):
764
+ def __init__(self, left, right):
765
+ assert(isinstance(left, CValue))
766
+ assert(isinstance(right, CValue))
767
+ super().__init__()
768
+ self.left = left
769
+ self.right = right
770
+ self.precedence = 12
771
+
772
+ def __str__(self):
773
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
774
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
775
+ return '%s * %s' % (lx, rx)
776
+
777
+
778
+ class CValueDiv(CValue):
779
+ def __init__(self, left, right):
780
+ assert(isinstance(left, CValue))
781
+ assert(isinstance(right, CValue))
782
+ super().__init__()
783
+ self.left = left
784
+ self.right = right
785
+ self.precedence = 12
786
+
787
+ def __str__(self):
788
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
789
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
790
+ return '%s / %s' % (lx, rx)
791
+
792
+
793
+ class CValueRem(CValue):
794
+ def __init__(self, left, right):
795
+ assert(isinstance(left, CValue))
796
+ assert(isinstance(right, CValue))
797
+ super().__init__()
798
+ self.left = left
799
+ self.right = right
800
+ self.precedence = 12
801
+
802
+ def __str__(self):
803
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
804
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
805
+ return '%s %% %s' % (lx, rx)
806
+
807
+
808
+ class CValueAdd(CValue):
809
+ def __init__(self, left, right):
810
+ assert(isinstance(left, CValue))
811
+ assert(isinstance(right, CValue))
812
+ super().__init__()
813
+ self.left = left
814
+ self.right = right
815
+ self.precedence = 11
816
+
817
+ def __str__(self):
818
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
819
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
820
+ return '%s + %s' % (lx, rx)
821
+
822
+
823
+ class CValueSub(CValue):
824
+ def __init__(self, left, right):
825
+ assert(isinstance(left, CValue))
826
+ assert(isinstance(right, CValue))
827
+ super().__init__()
828
+ self.left = left
829
+ self.right = right
830
+ self.precedence = 11
831
+
832
+ def __str__(self):
833
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
834
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
835
+ return '%s - %s' % (lx, rx)
836
+
837
+
838
+ class CValueShl(CValue):
839
+ def __init__(self, left, right):
840
+ assert(isinstance(left, CValue))
841
+ assert(isinstance(right, CValue))
842
+ super().__init__()
843
+ self.left = left
844
+ self.right = right
845
+ self.precedence = 10
846
+
847
+ def __str__(self):
848
+
849
+ # -Wbitwise-op-parentheses supressing
850
+ lprec = self.precedence
851
+ if self.left.precedence == self.precedence + 1:
852
+ lprec = valuePrecedenceMax
853
+ rprec = self.precedence
854
+ if self.right.precedence == self.precedence + 1:
855
+ rprec = valuePrecedenceMax
856
+
857
+ lx = str_cvalue(self.left, ext_precedence=lprec)
858
+ rx = str_cvalue(self.right, ext_precedence=rprec)
859
+ return '%s << %s' % (lx, rx)
860
+
861
+
862
+ class CValueShr(CValue):
863
+ def __init__(self, left, right):
864
+ assert(isinstance(left, CValue))
865
+ assert(isinstance(right, CValue))
866
+ super().__init__()
867
+ self.left = left
868
+ self.right = right
869
+ self.precedence = 10
870
+
871
+ def __str__(self):
872
+
873
+ # -Wbitwise-op-parentheses supressing
874
+ lprec = self.precedence
875
+ if self.left.precedence == self.precedence + 1:
876
+ lprec = valuePrecedenceMax
877
+ rprec = self.precedence
878
+ if self.right.precedence == self.precedence + 1:
879
+ rprec = valuePrecedenceMax
880
+
881
+ lx = str_cvalue(self.left, ext_precedence=lprec)
882
+ rx = str_cvalue(self.right, ext_precedence=rprec)
883
+ return '%s >> %s' % (lx, rx)
884
+
885
+
886
+ class CValueLt(CValue):
887
+ def __init__(self, left, right):
888
+ assert(isinstance(left, CValue))
889
+ assert(isinstance(right, CValue))
890
+ super().__init__()
891
+ self.left = left
892
+ self.right = right
893
+ self.precedence = 9
894
+
895
+ def __str__(self):
896
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
897
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
898
+ return '%s < %s' % (lx, rx)
899
+
900
+
901
+ class CValueGt(CValue):
902
+ def __init__(self, left, right):
903
+ assert(isinstance(left, CValue))
904
+ assert(isinstance(right, CValue))
905
+ super().__init__()
906
+ self.left = left
907
+ self.right = right
908
+ self.precedence = 9
909
+
910
+ def __str__(self):
911
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
912
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
913
+ return '%s > %s' % (lx, rx)
914
+
915
+
916
+ class CValueLE(CValue):
917
+ def __init__(self, left, right):
918
+ assert(isinstance(left, CValue))
919
+ assert(isinstance(right, CValue))
920
+ super().__init__()
921
+ self.left = left
922
+ self.right = right
923
+ self.precedence = 9
924
+
925
+ def __str__(self):
926
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
927
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
928
+ return '%s <= %s' % (lx, rx)
929
+
930
+
931
+ class CValueGE(CValue):
932
+ def __init__(self, left, right):
933
+ assert(isinstance(left, CValue))
934
+ assert(isinstance(right, CValue))
935
+ super().__init__()
936
+ self.left = left
937
+ self.right = right
938
+ self.precedence = 9
939
+
940
+ def __str__(self):
941
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
942
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
943
+ return '%s >= %s' % (lx, rx)
944
+
945
+
946
+ class CValueEq(CValue):
947
+ def __init__(self, left, right):
948
+ assert(isinstance(left, CValue))
949
+ assert(isinstance(right, CValue))
950
+ super().__init__()
951
+ self.left = left
952
+ self.right = right
953
+ self.precedence = 8
954
+
955
+ def __str__(self):
956
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
957
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
958
+ return '%s == %s' % (lx, rx)
959
+
960
+
961
+ class CValueNe(CValue):
962
+ def __init__(self, left, right):
963
+ assert(isinstance(left, CValue))
964
+ assert(isinstance(right, CValue))
965
+ super().__init__()
966
+ self.left = left
967
+ self.right = right
968
+ self.precedence = 8
969
+
970
+ def __str__(self):
971
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
972
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
973
+ return '%s != %s' % (lx, rx)
974
+
975
+
976
+ # -Wbitwise-op-parentheses supressing
977
+ # компилятор си любит ругаться на безобидные логические выражения, и сдвиги,
978
+ # все дело в том что люди часто ошибаются и поэтому, на всякий случай, компилятор просит еще и скобки
979
+ def select_prio_plus(sp, xp, n=0):
980
+ if xp > sp and xp <= sp + n:
981
+ return valuePrecedenceMax
982
+ return sp
983
+
984
+
985
+ class CValueAndBitwise(CValue):
986
+ def __init__(self, left, right):
987
+ assert(isinstance(left, CValue))
988
+ assert(isinstance(right, CValue))
989
+ super().__init__()
990
+ self.left = left
991
+ self.right = right
992
+ self.precedence = 7
993
+
994
+ def __str__(self):
995
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
996
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
997
+ return '%s & %s' % (lx, rx)
998
+
999
+
1000
+ class CValueXorBitwise(CValue):
1001
+ def __init__(self, left, right):
1002
+ assert(isinstance(left, CValue))
1003
+ assert(isinstance(right, CValue))
1004
+ super().__init__()
1005
+ self.left = left
1006
+ self.right = right
1007
+ self.precedence = 6
1008
+
1009
+ def __str__(self):
1010
+ lx = str_cvalue(self.left, ext_precedence=select_prio_plus(self.precedence, self.left.precedence, n=1))
1011
+ rx = str_cvalue(self.right, ext_precedence=select_prio_plus(self.precedence, self.right.precedence, n=1))
1012
+ return '%s ^ %s' % (lx, rx)
1013
+
1014
+
1015
+ class CValueOrBitwise(CValue):
1016
+ def __init__(self, left, right):
1017
+ assert(isinstance(left, CValue))
1018
+ assert(isinstance(right, CValue))
1019
+ super().__init__()
1020
+ self.left = left
1021
+ self.right = right
1022
+ self.precedence = 5
1023
+
1024
+ def __str__(self):
1025
+ lx = str_cvalue(self.left, ext_precedence=select_prio_plus(self.precedence, self.left.precedence, n=2))
1026
+ rx = str_cvalue(self.right, ext_precedence=select_prio_plus(self.precedence, self.right.precedence, n=2))
1027
+ return '%s | %s' % (lx, rx)
1028
+
1029
+
1030
+ class CValueAndLogical(CValue):
1031
+ def __init__(self, left, right):
1032
+ assert(isinstance(left, CValue))
1033
+ assert(isinstance(right, CValue))
1034
+ super().__init__()
1035
+ self.left = left
1036
+ self.right = right
1037
+ self.precedence = 4
1038
+
1039
+ def __str__(self):
1040
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
1041
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
1042
+ return '%s && %s' % (lx, rx)
1043
+
1044
+
1045
+ class CValueOrLogical(CValue):
1046
+ def __init__(self, left, right):
1047
+ assert(isinstance(left, CValue))
1048
+ assert(isinstance(right, CValue))
1049
+ super().__init__()
1050
+ self.left = left
1051
+ self.right = right
1052
+ self.precedence = 3
1053
+
1054
+ def __str__(self):
1055
+ lx = str_cvalue(self.left, ext_precedence=select_prio_plus(self.precedence, self.left.precedence, n=1))
1056
+ rx = str_cvalue(self.right, ext_precedence=select_prio_plus(self.precedence, self.right.precedence, n=1))
1057
+ return '%s || %s' % (lx, rx)
1058
+
1059
+
1060
+
1061
+ class CValueVaStart(CValue):
1062
+ def __init__(self, va_list, last_param):
1063
+ assert(isinstance(va_list, CValue))
1064
+ assert(isinstance(last_param, CValue))
1065
+ super().__init__()
1066
+ self.va_list = va_list
1067
+ self.last_param = last_param
1068
+ self.precedence = 14
1069
+
1070
+ def __str__(self):
1071
+ return 'va_start(%s, %s)' % (str_cvalue(self.va_list), str_cvalue(self.last_param))
1072
+
1073
+
1074
+ class CValueVaArg(CValue):
1075
+ def __init__(self, va_list, xtype):
1076
+ assert(isinstance(va_list, CValue))
1077
+ assert(isinstance(xtype, CType))
1078
+ super().__init__()
1079
+ self.va_list = va_list
1080
+ self.xtype = xtype
1081
+ self.precedence = 14
1082
+
1083
+ def __str__(self):
1084
+ return 'va_arg(%s, %s)' % (str_cvalue(self.va_list), str_ctype(self.xtype))
1085
+
1086
+
1087
+ class CValueVaEnd(CValue):
1088
+ def __init__(self, va_list):
1089
+ assert(isinstance(va_list, CValue))
1090
+ super().__init__()
1091
+ self.va_list = va_list
1092
+ self.precedence = 14
1093
+
1094
+ def __str__(self):
1095
+ return 'va_end(%s)' % (str_cvalue(self.va_list))
1096
+
1097
+
1098
+ class CValueVaCopy(CValue):
1099
+ def __init__(self, va_dst, va_src):
1100
+ assert(isinstance(va_dst, CValue))
1101
+ assert(isinstance(va_src, CValue))
1102
+ super().__init__()
1103
+ self.va_dst = va_dst
1104
+ self.va_src = va_src
1105
+ self.precedence = 14
1106
+
1107
+ def __str__(self):
1108
+ return 'va_copy(%s, %s)' % (str_cvalue(self.va_dst), str_cvalue(self.va_src))
1109
+
1110
+
1111
+ # string concat
1112
+ class CValueCat(CValue):
1113
+ def __init__(self, left, right):
1114
+ assert(isinstance(left, CValue))
1115
+ assert(isinstance(right, CValue))
1116
+ super().__init__()
1117
+ self.left = left
1118
+ self.right = right
1119
+ self.precedence = 15
1120
+
1121
+ def __str__(self):
1122
+ lx = str_cvalue(self.left, ext_precedence=self.precedence)
1123
+ rx = str_cvalue(self.right, ext_precedence=self.precedence)
1124
+ return lx + ' ' + rx
1125
+
1126
+
1127
+
1128
+ def str_cvalue(v, ext_precedence=0):
1129
+ assert(isinstance(v, CValue))
1130
+ y = str(v)
1131
+ sstr = wrap_if(y, (v.precedence < ext_precedence) or v.mark and (v.precedence < valuePrecedenceMax))
1132
+ if v.mark != None:
1133
+ sstr = '/*%s*/' % v.mark + sstr
1134
+ return sstr
1135
+
1136
+
1137
+
1138
+
1139
+
1140
+ def str_cstmt(x):
1141
+ assert(x != None)
1142
+ sstr = ''
1143
+ #if x.comment != None:
1144
+ # sstr += str_nl_indent(x.comment.nl)
1145
+ # print_comment(x.comment)
1146
+ #sstr += str_nl_indent(x.nl)
1147
+ sstr += str(x)
1148
+ if x.mark != None:
1149
+ sstr = '/*%s*/' % x.mark + sstr
1150
+ return sstr
1151
+
1152
+
1153
+ class CStmt():
1154
+ def __init__(self):
1155
+ self.comment = None
1156
+ self.nl = 1
1157
+ self.mark = None
1158
+ pass
1159
+
1160
+
1161
+ class CStmtCommentLine(CStmt):
1162
+ def __init__(self, lines):
1163
+ assert(isinstance(lines, list))
1164
+ super().__init__()
1165
+ self.lines = lines
1166
+ self.nl = 1
1167
+
1168
+ def __str__(self):
1169
+ sstr = ''
1170
+ sstr += str_nl_indent(self.nl)
1171
+ n = len(self.lines)
1172
+ i = 0
1173
+ while i < n:
1174
+ line = self.lines[i]
1175
+ sstr += "//%s" % line
1176
+ i = i + 1
1177
+ if i < n:
1178
+ sstr += str_nl_indent()
1179
+ return sstr
1180
+
1181
+
1182
+ class CStmtCommentBlock(CStmt):
1183
+ def __init__(self, text):
1184
+ assert(isinstance(text, str))
1185
+ super().__init__()
1186
+ self.text = text
1187
+ self.nl = 1
1188
+
1189
+ def __str__(self):
1190
+ sstr = str_nl_indent(self.nl)
1191
+ sstr += "/*%s*/" % self.text
1192
+ return sstr
1193
+
1194
+
1195
+ class CStmtBlock(CStmt):
1196
+ def __init__(self, stmts):
1197
+ assert(isinstance(stmts, list))
1198
+ super().__init__()
1199
+ self.nl = 0
1200
+ self.stmts = stmts
1201
+
1202
+ def __str__(self):
1203
+ sstr = ''
1204
+ sstr += "{"
1205
+ nl_end_e = 1
1206
+ indent_up()
1207
+ for stmt in self.stmts:
1208
+ sstr += str_cstmt(stmt)
1209
+ indent_down()
1210
+ sstr += str_nl_indent(nl=nl_end_e)
1211
+ sstr += "}"
1212
+ return sstr
1213
+
1214
+
1215
+ class CStmtValueExpr(CStmt):
1216
+ def __init__(self, value):
1217
+ assert(isinstance(value, CValue))
1218
+ super().__init__()
1219
+ self.value = value
1220
+ self.nl = 1
1221
+
1222
+ def __str__(self):
1223
+ sstr = str_nl_indent(self.nl)
1224
+ return sstr + str_cvalue(self.value) + ';'
1225
+
1226
+
1227
+ class CStmtValueAssign(CStmt):
1228
+ def __init__(self, lvalue, rvalue):
1229
+ assert(isinstance(lvalue, CValue))
1230
+ assert(isinstance(rvalue, CValue))
1231
+ super().__init__()
1232
+ self.lvalue = lvalue
1233
+ self.rvalue = rvalue
1234
+
1235
+ def __str__(self):
1236
+ sstr = str_nl_indent(self.nl)
1237
+ return sstr + "%s = %s;" % (str_cvalue(self.lvalue), str_cvalue(self.rvalue))
1238
+
1239
+
1240
+ class CStmtInc(CStmt):
1241
+ def __init__(self, value):
1242
+ assert(isinstance(value, CValue))
1243
+ super().__init__()
1244
+ self.value = value
1245
+
1246
+ def __str__(self):
1247
+ return str_nl_indent(self.nl) + "++%s;" % str_cvalue(self.value)
1248
+
1249
+
1250
+ class CStmtDec(CStmt):
1251
+ def __init__(self, value):
1252
+ assert(isinstance(value, CValue))
1253
+ super().__init__()
1254
+ self.value = value
1255
+
1256
+ def __str__(self):
1257
+ return str_nl_indent(self.nl) + "--%s;" % str_cvalue(self.value)
1258
+
1259
+
1260
+ class CStmtDeclType(CStmt):
1261
+ def __init__(self, type, attributes=None):
1262
+ assert(isinstance(type, CTypeNamed))
1263
+ super().__init__()
1264
+ self.type = type
1265
+ self.attributes = attributes
1266
+
1267
+ def __str__(self):
1268
+ sstr = str_nl_indent(self.nl)
1269
+ sstr += str_gcc_attributes(self.attributes)
1270
+ sstr += str_ctype(self.type) + ';'
1271
+ return sstr
1272
+
1273
+
1274
+ class CStmtDefType(CStmt):
1275
+ def __init__(self, id_str, type, attributes=None):
1276
+ assert(isinstance(id_str, str))
1277
+ assert(isinstance(type, CType))
1278
+ super().__init__()
1279
+ self.id_str = id_str
1280
+ self.type = type
1281
+ self.attributes = attributes
1282
+
1283
+ def __str__(self):
1284
+ sstr = str_nl_indent(self.nl)
1285
+ sstr += str_gcc_attributes(self.attributes)
1286
+ sstr += 'typedef %s;' % self.type.to_str(text=self.id_str)
1287
+ return sstr
1288
+
1289
+
1290
+
1291
+ class CStmtDefVar(CStmt):
1292
+ def __init__(self, id_str, type, init_value=None, storage_class='', attributes=None):
1293
+ assert(isinstance(id_str, str))
1294
+ assert(isinstance(type, CType))
1295
+ if init_value != None:
1296
+ assert(isinstance(init_value, CValue))
1297
+ super().__init__()
1298
+ self.id_str = id_str
1299
+ self.type = type
1300
+ self.storage = storage_class
1301
+ self.init_value = init_value
1302
+ self.attributes = attributes
1303
+
1304
+ def __str__(self):
1305
+ sstr = str_nl_indent(self.nl)
1306
+ sstr += str_gcc_attributes(self.attributes)
1307
+ if self.storage not in (None, ''):
1308
+ sstr += self.storage + ' '
1309
+ #mass
1310
+ sstr += str_ctype(self.type, text=self.id_str)
1311
+ if self.init_value != None:
1312
+ sstr += ' = %s' % str_cvalue(self.init_value)
1313
+ return sstr + ';'
1314
+
1315
+
1316
+
1317
+ class CStmtDefFunc(CStmt):
1318
+ def __init__(self, id_str, type, block, storage_class='', attributes=None):
1319
+ assert(isinstance(id_str, str))
1320
+ assert(isinstance(type, CType))
1321
+ assert(isinstance(block, CStmtBlock))
1322
+ #if init_value != None:
1323
+ # assert(isinstance(init_value, CValue))
1324
+ super().__init__()
1325
+ self.id_str = id_str
1326
+ self.type = type
1327
+ self.storage = storage_class
1328
+ self.block = block
1329
+ self.attributes = attributes
1330
+ self.nl = 2
1331
+
1332
+ def __str__(self):
1333
+ sstr = str_nl_indent(self.nl)
1334
+ sstr += str_gcc_attributes(self.attributes)
1335
+ if self.storage not in (None, ''):
1336
+ sstr += self.storage + ' '
1337
+ sstr += self.type.to_str(text=self.id_str)
1338
+
1339
+ if styleguide['LINE_BREAK_BEFORE_FUNC_BRACE']:
1340
+ sstr += str_nl_indent()
1341
+ else:
1342
+ sstr += ' '
1343
+
1344
+ sstr += str_cstmt(self.block)
1345
+ return sstr
1346
+
1347
+
1348
+
1349
+ class CStmtIf(CStmt):
1350
+ def __init__(self, value_cond, block_then, block_else):
1351
+ assert(isinstance(value_cond, CValue))
1352
+ assert(isinstance(block_then, CStmtBlock))
1353
+ if block_else:
1354
+ assert((isinstance(block_else, CStmtBlock) or isinstance(block_else, CStmtIf)))
1355
+ super().__init__()
1356
+ self.value_cond = value_cond
1357
+ self.block_then = block_then
1358
+ self.block_else = block_else
1359
+
1360
+ def __str__(self):
1361
+ sstr = str_nl_indent(self.nl)
1362
+ sstr += "if (%s)" % str_cvalue(self.value_cond)
1363
+ if styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1364
+ sstr += str_nl_indent()
1365
+ else:
1366
+ sstr += ' '
1367
+ sstr += str(self.block_then)
1368
+ if self.block_else != None:
1369
+ if styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1370
+ sstr += str_nl_indent()
1371
+ else:
1372
+ sstr += ' '
1373
+ sstr += 'else'
1374
+ if isinstance(self.block_else, CStmtBlock) and styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1375
+ sstr += str_nl_indent()
1376
+ else:
1377
+ sstr += ' '
1378
+ sstr += str(self.block_else)
1379
+ return sstr
1380
+
1381
+
1382
+ class CStmtWhile(CStmt):
1383
+ def __init__(self, value_cond, block):
1384
+ assert(isinstance(value_cond, CValue))
1385
+ assert(isinstance(block, CStmtBlock))
1386
+ super().__init__()
1387
+ self.value_cond = value_cond
1388
+ self.block = block
1389
+
1390
+ def __str__(self):
1391
+ sstr = str_nl_indent(self.nl)
1392
+ sstr += "while (%s)" % str_cvalue(self.value_cond)
1393
+ if styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1394
+ sstr += str_nl_indent()
1395
+ else:
1396
+ sstr += ' '
1397
+ sstr += str(self.block)
1398
+ return sstr
1399
+
1400
+
1401
+ class CStmtReturn(CStmt):
1402
+ def __init__(self, value_retval):
1403
+ if value_retval != None:
1404
+ assert(isinstance(value_retval, CValue))
1405
+ super().__init__()
1406
+ self.value_retval = value_retval
1407
+
1408
+ def __str__(self):
1409
+ sstr = str_nl_indent(self.nl)
1410
+ sstr += 'return'
1411
+ if self.value_retval != None:
1412
+ sstr += ' ' + str_cvalue(self.value_retval)
1413
+ return sstr + ";"
1414
+
1415
+
1416
+ class CStmtBreak(CStmt):
1417
+ def __init__(self):
1418
+ super().__init__()
1419
+ pass
1420
+
1421
+ def __str__(self):
1422
+ sstr = str_nl_indent(self.nl)
1423
+ sstr += "break;"
1424
+ return sstr
1425
+
1426
+
1427
+ class CStmtContinue(CStmt):
1428
+ def __init__(self):
1429
+ super().__init__()
1430
+ pass
1431
+
1432
+ def __str__(self):
1433
+ sstr = str_nl_indent(self.nl)
1434
+ sstr += "continue;"
1435
+ return sstr
1436
+
1437
+
1438
+ #__asm__ volatile (
1439
+ # "assembly code template"
1440
+ # : output operands /* optional */
1441
+ # : input operands /* optional */
1442
+ # : clobbered registers/memory /* optional */
1443
+ #);
1444
+
1445
+ class CStmtAsm(CStmt):
1446
+ def __init__(self, text, outputs, inputs, clobbers):
1447
+ super().__init__()
1448
+ assert(isinstance(text, str))
1449
+ self.text = text
1450
+ self.outputs = outputs
1451
+ self.inputs = inputs
1452
+ self.clobbers = clobbers
1453
+
1454
+
1455
+ def __str__(self):
1456
+ sstr = str_nl_indent(self.nl)
1457
+ sstr += "__asm__ volatile ("
1458
+ sstr += '"%s" ' % self.text.replace('\n', '\\n\\\n')
1459
+
1460
+ sstr += ":"
1461
+ items = ("%s (%s)" % (str(xx[0]), str(xx[1])) for xx in self.outputs)
1462
+ if len(self.outputs) > 0:
1463
+ sstr += " " + ", ".join(items)
1464
+
1465
+ if self.outputs != []:
1466
+ sstr += " "
1467
+ sstr += ":"
1468
+ items = ("%s (%s)" % (str(xx[0]), str(xx[1])) for xx in self.inputs)
1469
+ if len(self.inputs) > 0:
1470
+ sstr += " " + ", ".join(items)
1471
+
1472
+ if self.inputs != []:
1473
+ sstr += " "
1474
+ sstr += ":"
1475
+ items = (str(xx) for xx in self.clobbers)
1476
+ if len(self.clobbers) > 0:
1477
+ sstr += " " + ", ".join(items)
1478
+
1479
+ sstr += ");"
1480
+ return sstr
1481
+
1482
+
1483
+
1484
+ class CInsert(CStmt):
1485
+ def __init__(self, text):
1486
+ super().__init__()
1487
+ self.text = text
1488
+ pass
1489
+
1490
+ def __str__(self):
1491
+ return self.text
1492
+
1493
+
1494
+ class CMacrodefinition():
1495
+ def __init__(self, id, text=None):
1496
+ assert(isinstance(id, str))
1497
+ if text:
1498
+ assert(isinstance(text, str))
1499
+ self.nl = 1 #!!! (because it is not CStmt...)
1500
+ super().__init__()
1501
+ self.id = id
1502
+ self.text = text
1503
+ self.mark = None
1504
+
1505
+ def __str__(self):
1506
+ sstr = str_nl_indent(self.nl)
1507
+ sstr += "#define %s" % (self.id)
1508
+ if self.text:
1509
+ sstr += ' ' + self.text
1510
+ return sstr
1511
+
1512
+
1513
+ class CMacrodefinitionValue():
1514
+ def __init__(self, id, value):
1515
+ assert(isinstance(id, str))
1516
+ assert(isinstance(value, CValue))
1517
+ self.nl = 1 #!!! (because it is not CStmt...)
1518
+ super().__init__()
1519
+ self.id = id
1520
+ self.value = value
1521
+ self.mark = None
1522
+
1523
+ def __str__(self):
1524
+ sstr = str_nl_indent(self.nl)
1525
+ set_nl_symbol(" \\\n")
1526
+ sstr += "#define %s %s" % (self.id, str_cvalue(self.value, ext_precedence=valuePrecedenceMax))
1527
+ set_nl_symbol("\n")
1528
+ return sstr
1529
+
1530
+
1531
+ class CMacroUndef():
1532
+ def __init__(self, text):
1533
+ assert(isinstance(text, str))
1534
+ self.nl = 1 #!!! (because it is not CStmt...)
1535
+ super().__init__()
1536
+ self.text = text
1537
+ self.mark = None
1538
+
1539
+ def __str__(self):
1540
+ sstr = str_nl_indent(self.nl)
1541
+ sstr += "#undef %s" % (self.text)
1542
+ return sstr
1543
+
1544
+
1545
+ class CInclude():
1546
+ def __init__(self, text, isglobal):
1547
+ assert(isinstance(text, str))
1548
+ assert(isinstance(isglobal, bool))
1549
+ self.nl = 1 #!!! (because it is not CStmt...)
1550
+ super().__init__()
1551
+ self.text = text
1552
+ self.isglobal = isglobal
1553
+ self.mark = None
1554
+
1555
+ def __str__(self):
1556
+ #sstr = str_nl_indent(self.nl)
1557
+ if self.isglobal:
1558
+ return "\n#include <%s>" % self.text
1559
+ return "\n#include \"%s\"" % self.text
1560
+
1561
+
1562
+ def str_cdef(x):
1563
+ prefix = '/*%s*/' % x.mark if x.mark else ''
1564
+ return prefix + str(x)
1565
+
1566
+
1567
+ # pairs = ("macro text", [<defs>])
1568
+ class CIfdefRegion():
1569
+ def __init__(self, pairs, _else=None):
1570
+ self.pairs = pairs
1571
+ self._else = _else
1572
+ self.mark = None
1573
+
1574
+ def __str__(self):
1575
+ sstr = ''
1576
+ directive = '#if'
1577
+ for pair in self.pairs:
1578
+ # if directive == '#if':
1579
+ # ss = pair[0].split('()')
1580
+ # print(ss)
1581
+ sstr += "\n%s %s" % (directive, pair[0])
1582
+ # if len(pair[1]) > 5:
1583
+ # sstr += "\n"
1584
+ for xd in pair[1]:
1585
+ #print(":" + str(xd))
1586
+ sstr += str_cdef(xd)
1587
+ # if len(pair[1]) > 5:
1588
+ # sstr += "\n"
1589
+ directive = '#elif'
1590
+
1591
+ if self._else != None:
1592
+ sstr += '\n#else'
1593
+ # if len(self._else) > 5:
1594
+ # sstr += "\n"
1595
+ for xd in self._else:
1596
+ sstr += str_cdef(xd)
1597
+ # if len(self._else) > 5:
1598
+ # sstr += "\n"
1599
+ sstr += "\n#endif"
1600
+ #sstr = sstr.replace("#if defined", "#ifdef")
1601
+ #sstr = sstr.replace("#if !defined", "#ifndef")
1602
+ return sstr
1603
+
1604
+
1605
+
1606
+
1607
+
1608
+