cshape 0.1.0__tar.gz → 0.2.1__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cshape
3
- Version: 0.1.0
3
+ Version: 0.2.1
4
4
  Summary: A dependency-free C AST + printer: build a tree in Python, get C11 source back
5
5
  Author-email: Alex Balan <outsiders.b@gmail.com>
6
6
  License-Expression: MIT
@@ -38,14 +38,14 @@ pip install cshape
38
38
  from cshape import *
39
39
 
40
40
  func = CStmtDefFunc(
41
- id_str="add",
42
- type=CTypeFunc(
43
- params=[CField(id_str="a", type=CTypeNamed("int")),
44
- CField(id_str="b", type=CTypeNamed("int"))],
45
- to=CTypeNamed("int"),
41
+ id="add",
42
+ type=CTypeFunction(
43
+ params=[CField(id="a", type=CTypeIdentifier("int")),
44
+ CField(id="b", type=CTypeIdentifier("int"))],
45
+ to=CTypeIdentifier("int"),
46
46
  ),
47
47
  block=CStmtBlock([
48
- CStmtReturn(CValueAdd(CValueNamed("a"), CValueNamed("b"))),
48
+ CStmtReturn(CValueAdd(CValueIdentifier("a"), CValueIdentifier("b"))),
49
49
  ]),
50
50
  )
51
51
 
@@ -20,14 +20,14 @@ pip install cshape
20
20
  from cshape import *
21
21
 
22
22
  func = CStmtDefFunc(
23
- id_str="add",
24
- type=CTypeFunc(
25
- params=[CField(id_str="a", type=CTypeNamed("int")),
26
- CField(id_str="b", type=CTypeNamed("int"))],
27
- to=CTypeNamed("int"),
23
+ id="add",
24
+ type=CTypeFunction(
25
+ params=[CField(id="a", type=CTypeIdentifier("int")),
26
+ CField(id="b", type=CTypeIdentifier("int"))],
27
+ to=CTypeIdentifier("int"),
28
28
  ),
29
29
  block=CStmtBlock([
30
- CStmtReturn(CValueAdd(CValueNamed("a"), CValueNamed("b"))),
30
+ CStmtReturn(CValueAdd(CValueIdentifier("a"), CValueIdentifier("b"))),
31
31
  ]),
32
32
  )
33
33
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cshape
3
- Version: 0.1.0
3
+ Version: 0.2.1
4
4
  Summary: A dependency-free C AST + printer: build a tree in Python, get C11 source back
5
5
  Author-email: Alex Balan <outsiders.b@gmail.com>
6
6
  License-Expression: MIT
@@ -38,14 +38,14 @@ pip install cshape
38
38
  from cshape import *
39
39
 
40
40
  func = CStmtDefFunc(
41
- id_str="add",
42
- type=CTypeFunc(
43
- params=[CField(id_str="a", type=CTypeNamed("int")),
44
- CField(id_str="b", type=CTypeNamed("int"))],
45
- to=CTypeNamed("int"),
41
+ id="add",
42
+ type=CTypeFunction(
43
+ params=[CField(id="a", type=CTypeIdentifier("int")),
44
+ CField(id="b", type=CTypeIdentifier("int"))],
45
+ to=CTypeIdentifier("int"),
46
46
  ),
47
47
  block=CStmtBlock([
48
- CStmtReturn(CValueAdd(CValueNamed("a"), CValueNamed("b"))),
48
+ CStmtReturn(CValueAdd(CValueIdentifier("a"), CValueIdentifier("b"))),
49
49
  ]),
50
50
  )
51
51
 
@@ -1,11 +1,31 @@
1
1
 
2
+
2
3
  nl_symbol = "\n"
3
4
  indent_symbol = "\t"
4
-
5
5
  indent_level = 0
6
6
 
7
7
 
8
8
 
9
+ legacy_style = {
10
+ 'LINE_BREAK_BEFORE_STRUCT_BRACE': False,
11
+ 'LINE_BREAK_BEFORE_FUNC_BRACE': False,
12
+ 'LINE_BREAK_BEFORE_BLOCK_BRACE': False,
13
+ }
14
+
15
+ modern_style = {
16
+ 'LINE_BREAK_BEFORE_STRUCT_BRACE': True,
17
+ 'LINE_BREAK_BEFORE_FUNC_BRACE': True,
18
+ 'LINE_BREAK_BEFORE_BLOCK_BRACE': True,
19
+ }
20
+
21
+ styles = {
22
+ 'legacy': legacy_style,
23
+ 'modern': modern_style,
24
+ }
25
+
26
+
27
+ styleguide = legacy_style
28
+
9
29
 
10
30
  def indent_up():
11
31
  global indent_level
@@ -17,13 +37,11 @@ def indent_down():
17
37
  indent_level = indent_level - 1
18
38
 
19
39
 
20
-
21
40
  def set_nl_symbol(x):
22
41
  global nl_symbol
23
42
  nl_symbol = x
24
43
 
25
44
 
26
-
27
45
  def str_indent():
28
46
  global indent_level
29
47
  global indent_symbol
@@ -37,34 +55,6 @@ def str_nl_indent(nl=1):
37
55
  return s
38
56
 
39
57
 
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
58
  def render(node, style='legacy', indent='\t', newline='\n'):
69
59
  global indent_level, styleguide, nl_symbol, indent_symbol
70
60
 
@@ -94,9 +84,9 @@ def wrap_if(x, cond):
94
84
  return "(%s)" % x if cond else x
95
85
 
96
86
 
97
- def str_specs(specs):
87
+ def str_specs(specifiers):
98
88
  s = ''
99
- for opt in specs:
89
+ for opt in specifiers:
100
90
  s += opt + ' '
101
91
  return s
102
92
 
@@ -107,8 +97,6 @@ def with_space(s):
107
97
  return ''
108
98
 
109
99
 
110
-
111
-
112
100
  def str_gcc_attributes(attributes):
113
101
  if attributes == None:
114
102
  return ''
@@ -159,11 +147,11 @@ def str_gcc_attributes(attributes):
159
147
 
160
148
 
161
149
  class CField():
162
- def __init__(self, id_str, type, specs=None, nl=0):
150
+ def __init__(self, id, type, specifiers=None, nl=0):
163
151
  #assert(isinstance(type, CType))
164
- self.id_str = id_str
152
+ self.id = id
165
153
  self.type = type
166
- self.specs = specs if specs != None else []
154
+ self.specifiers = specifiers if specifiers != None else []
167
155
  self.nl = nl
168
156
 
169
157
 
@@ -177,12 +165,12 @@ class CType():
177
165
  return "<type> " + text
178
166
 
179
167
 
180
- class CTypeNamed(CType):
181
- def __init__(self, id_str, specs=None):
182
- assert(isinstance(id_str, str))
168
+ class CTypeIdentifier(CType):
169
+ def __init__(self, id, specifiers=None):
170
+ assert(isinstance(id, str))
183
171
  super().__init__()
184
- self.id_str = id_str
185
- self.specs = specs if specs != None else []
172
+ self.id = id
173
+ self.specifiers = specifiers if specifiers != None else []
186
174
  self.precedence = 0
187
175
 
188
176
  def to_str(self, text='', with_qualifiers=True):
@@ -190,22 +178,22 @@ class CTypeNamed(CType):
190
178
  sstr = ''
191
179
  if with_qualifiers:
192
180
  # for: const, volatile, restrict
193
- sstr += str_specs(self.specs)
194
- return sstr + self.id_str + with_space(text)
181
+ sstr += str_specs(self.specifiers)
182
+ return sstr + self.id + with_space(text)
195
183
 
196
184
 
197
185
  class CTypePointer(CType):
198
- def __init__(self, to, specs=None):
186
+ def __init__(self, to, specifiers=None):
199
187
  super().__init__()
200
188
  self.to = to
201
- self.specs = specs if specs != None else []
189
+ self.specifiers = specifiers if specifiers != None else []
202
190
  self.precedence = 1
203
191
 
204
192
  def to_str(self, text='', with_qualifiers=True):
205
193
  # "*volatile p"
206
194
  sstr = '*'
207
195
  if with_qualifiers:
208
- sstr += str_specs(self.specs)
196
+ sstr += str_specs(self.specifiers)
209
197
  sstr += text
210
198
  sstr = wrap_if(sstr, self.to.precedence > self.precedence)
211
199
  sstr = str_ctype(self.to, sstr)
@@ -213,31 +201,31 @@ class CTypePointer(CType):
213
201
 
214
202
 
215
203
  class CTypeArray(CType):
216
- def __init__(self, of, volume=None, specs=[]):
204
+ def __init__(self, item_type, size=None, specifiers=[]):
217
205
  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 []
206
+ item_type.specifiers = specifiers # array specs is array item specs (!)
207
+ self.item_type = item_type
208
+ self.size = size
209
+ self.specifiers = specifiers if specifiers != None else []
222
210
  self.precedence = 2
223
211
 
224
212
  def to_str(self, text='', with_qualifiers=True):
225
213
  text = text + '['
226
- if self.volume != None:
227
- text += str_cvalue(self.volume)
214
+ if self.size != None:
215
+ text += str_cvalue(self.size)
228
216
  text += ']'
229
- text = wrap_if(text, self.of.precedence > self.precedence)
230
- text = str_ctype(self.of, text)
217
+ text = wrap_if(text, self.item_type.precedence > self.precedence)
218
+ text = str_ctype(self.item_type, text)
231
219
  return text
232
220
 
233
221
 
234
- class CTypeFunc(CType):
235
- def __init__(self, params, to, volume=None, extra_args=False, specs=None):
222
+ class CTypeFunction(CType):
223
+ def __init__(self, params, to, size=None, extra_args=False, specifiers=None):
236
224
  super().__init__()
237
225
  self.params = params
238
226
  self.to = to
239
227
  self.extra_args = extra_args
240
- self.specs = specs if specs != None else []
228
+ self.specifiers = specifiers if specifiers != None else []
241
229
  self.precedence = 3
242
230
 
243
231
  def to_str(self, text='', with_qualifiers=True):
@@ -246,7 +234,7 @@ class CTypeFunc(CType):
246
234
  params = self.params
247
235
  while i < len(params):
248
236
  param = params[i]
249
- p = str_ctype(param.type, text=param.id_str)
237
+ p = str_ctype(param.type, text=param.id)
250
238
  if i > 0:
251
239
  params_text += ', ' + p
252
240
  else:
@@ -265,11 +253,11 @@ class CTypeFunc(CType):
265
253
 
266
254
 
267
255
  class CTypeStruct(CType):
268
- def __init__(self, fields, tag, specs=None):
256
+ def __init__(self, fields, tag, specifiers=None):
269
257
  super().__init__()
270
258
  self.fields = fields
271
259
  self.tag = tag
272
- self.specs = specs if specs != None else []
260
+ self.specifiers = specifiers if specifiers != None else []
273
261
  self.precedence = 0
274
262
 
275
263
  def to_str(self, text='', with_qualifiers=True):
@@ -295,7 +283,7 @@ class CTypeStruct(CType):
295
283
  sstr += ' '
296
284
 
297
285
  sstr += str_nl_indent(field.nl)
298
- sstr += str_ctype(field.type, text=field.id_str) + ';'
286
+ sstr += str_ctype(field.type, text=field.id) + ';'
299
287
  i = i + 1
300
288
  else:
301
289
  sstr += 'uint8_t __placeholder;'
@@ -305,7 +293,8 @@ class CTypeStruct(CType):
305
293
 
306
294
 
307
295
 
308
- class CTypeEnumItem():
296
+
297
+ class CEnumItem():
309
298
  def __init__(self, id, value=None):
310
299
  assert(isinstance(id, str))
311
300
  assert(value == None or isinstance(value, CValue))
@@ -321,11 +310,11 @@ class CTypeEnumItem():
321
310
 
322
311
 
323
312
  class CTypeEnum(CType):
324
- def __init__(self, items, tag='', specs=None):
313
+ def __init__(self, items, tag='', specifiers=None):
325
314
  super().__init__()
326
315
  self.items = items
327
316
  self.tag = tag
328
- self.specs = specs if specs != None else []
317
+ self.specifiers = specifiers if specifiers != None else []
329
318
  self.precedence = 0
330
319
 
331
320
  def to_str(self, text='', with_qualifiers=True):
@@ -342,7 +331,7 @@ class CTypeEnum(CType):
342
331
 
343
332
  while i < nitems:
344
333
  item = self.items[i]
345
- assert(isinstance(item, CTypeEnumItem))
334
+ assert(isinstance(item, CEnumItem))
346
335
  if item.nl > 0:
347
336
  nl_end = 1
348
337
 
@@ -410,14 +399,14 @@ class CValue():
410
399
  self.mark = None
411
400
 
412
401
 
413
- class CValueNamed(CValue):
414
- def __init__(self, id_str):
402
+ class CValueIdentifier(CValue):
403
+ def __init__(self, id):
415
404
  super().__init__()
416
- self.id_str = id_str
405
+ self.id = id
417
406
  self.precedence = 15
418
407
 
419
408
  def __str__(self):
420
- return self.id_str
409
+ return self.id
421
410
 
422
411
 
423
412
 
@@ -571,7 +560,7 @@ class CValueStruct(CValue):
571
560
 
572
561
 
573
562
 
574
- class CValueSubexpr(CValue):
563
+ class CValueParen(CValue):
575
564
  def __init__(self, value):
576
565
  assert(isinstance(value, CValue))
577
566
  super().__init__()
@@ -596,30 +585,30 @@ class CValueCall(CValue):
596
585
  return '%s(%s)' % (str_cvalue(self.left, ext_precedence=self.precedence), print_list_items(self.args, str_cvalue))
597
586
 
598
587
 
599
- class CValueAccess(CValue):
600
- def __init__(self, left, field_id_str):
601
- assert(isinstance(field_id_str, str))
588
+ class CValueFieldAccess(CValue):
589
+ def __init__(self, left, field_id):
590
+ assert(isinstance(field_id, str))
602
591
  assert(isinstance(left, CValue))
603
592
  super().__init__()
604
593
  self.left = left
605
- self.field_id_str = field_id_str
594
+ self.field_id = field_id
606
595
  self.precedence = 14
607
596
 
608
597
  def __str__(self):
609
- return '%s.%s' % (str_cvalue(self.left, ext_precedence=self.precedence), self.field_id_str)
598
+ return '%s.%s' % (str_cvalue(self.left, ext_precedence=self.precedence), self.field_id)
610
599
 
611
600
 
612
- class CValueAccessPtr(CValue):
613
- def __init__(self, left, field_id_str):
614
- assert(isinstance(field_id_str, str))
601
+ class CValuePtrFieldAccess(CValue):
602
+ def __init__(self, left, field_id):
603
+ assert(isinstance(field_id, str))
615
604
  assert(isinstance(left, CValue))
616
605
  super().__init__()
617
606
  self.left = left
618
- self.field_id_str = field_id_str
607
+ self.field_id = field_id
619
608
  self.precedence = 14
620
609
 
621
610
  def __str__(self):
622
- return '%s->%s' % (str_cvalue(self.left, ext_precedence=self.precedence), self.field_id_str)
611
+ return '%s->%s' % (str_cvalue(self.left, ext_precedence=self.precedence), self.field_id)
623
612
 
624
613
 
625
614
  class CValueIndex(CValue):
@@ -650,7 +639,7 @@ class CValueCast(CValue):
650
639
  return '(%s)%s' % (str_ctype(self.type, with_qualifiers=False), vstr)
651
640
 
652
641
 
653
- class CValueRef(CValue):
642
+ class CValueReference(CValue):
654
643
  def __init__(self, value):
655
644
  assert(isinstance(value, CValue))
656
645
  super().__init__()
@@ -661,7 +650,7 @@ class CValueRef(CValue):
661
650
  return '&%s' % str_cvalue(self.value, ext_precedence=self.precedence)
662
651
 
663
652
 
664
- class CValueDeref(CValue):
653
+ class CValueDereference(CValue):
665
654
  def __init__(self, value):
666
655
  assert(isinstance(value, CValue))
667
656
  super().__init__()
@@ -672,7 +661,7 @@ class CValueDeref(CValue):
672
661
  return '*%s' % str_cvalue(self.value, ext_precedence=self.precedence)
673
662
 
674
663
 
675
- class CValueInc(CValue):
664
+ class CValueIncrement(CValue):
676
665
  def __init__(self, value):
677
666
  assert(isinstance(value, CValue))
678
667
  super().__init__()
@@ -683,7 +672,7 @@ class CValueInc(CValue):
683
672
  return '++%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
684
673
 
685
674
 
686
- class CValueDec(CValue):
675
+ class CValueDecrement(CValue):
687
676
  def __init__(self, value):
688
677
  assert(isinstance(value, CValue))
689
678
  super().__init__()
@@ -694,7 +683,7 @@ class CValueDec(CValue):
694
683
  return '--%s' % str_cvalue(self.value, ext_precedence=self.precedence)
695
684
 
696
685
 
697
- class CValuePositive(CValue):
686
+ class CValueUnaryPlus(CValue):
698
687
  def __init__(self, value):
699
688
  assert(isinstance(value, CValue))
700
689
  super().__init__()
@@ -705,7 +694,7 @@ class CValuePositive(CValue):
705
694
  return '+%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
706
695
 
707
696
 
708
- class CValueNegative(CValue):
697
+ class CValueUnaryMinus(CValue):
709
698
  def __init__(self, value):
710
699
  assert(isinstance(value, CValue))
711
700
  super().__init__()
@@ -716,7 +705,7 @@ class CValueNegative(CValue):
716
705
  return '-%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
717
706
 
718
707
 
719
- class CValueNotLogical(CValue):
708
+ class CValueLogicalNot(CValue):
720
709
  def __init__(self, value):
721
710
  assert(isinstance(value, CValue))
722
711
  super().__init__()
@@ -727,7 +716,7 @@ class CValueNotLogical(CValue):
727
716
  return '!%s' % (str_cvalue(self.value, ext_precedence=self.precedence))
728
717
 
729
718
 
730
- class CValueNotBitwise(CValue):
719
+ class CValueBitwiseNot(CValue):
731
720
  def __init__(self, value):
732
721
  assert(isinstance(value, CValue))
733
722
  super().__init__()
@@ -790,7 +779,7 @@ class CValueDiv(CValue):
790
779
  return '%s / %s' % (lx, rx)
791
780
 
792
781
 
793
- class CValueRem(CValue):
782
+ class CValueMod(CValue):
794
783
  def __init__(self, left, right):
795
784
  assert(isinstance(left, CValue))
796
785
  assert(isinstance(right, CValue))
@@ -835,7 +824,7 @@ class CValueSub(CValue):
835
824
  return '%s - %s' % (lx, rx)
836
825
 
837
826
 
838
- class CValueShl(CValue):
827
+ class CValueShiftLeft(CValue):
839
828
  def __init__(self, left, right):
840
829
  assert(isinstance(left, CValue))
841
830
  assert(isinstance(right, CValue))
@@ -859,7 +848,7 @@ class CValueShl(CValue):
859
848
  return '%s << %s' % (lx, rx)
860
849
 
861
850
 
862
- class CValueShr(CValue):
851
+ class CValueShiftRight(CValue):
863
852
  def __init__(self, left, right):
864
853
  assert(isinstance(left, CValue))
865
854
  assert(isinstance(right, CValue))
@@ -982,7 +971,7 @@ def select_prio_plus(sp, xp, n=0):
982
971
  return sp
983
972
 
984
973
 
985
- class CValueAndBitwise(CValue):
974
+ class CValueBitwiseAnd(CValue):
986
975
  def __init__(self, left, right):
987
976
  assert(isinstance(left, CValue))
988
977
  assert(isinstance(right, CValue))
@@ -997,7 +986,7 @@ class CValueAndBitwise(CValue):
997
986
  return '%s & %s' % (lx, rx)
998
987
 
999
988
 
1000
- class CValueXorBitwise(CValue):
989
+ class CValueBitwiseXor(CValue):
1001
990
  def __init__(self, left, right):
1002
991
  assert(isinstance(left, CValue))
1003
992
  assert(isinstance(right, CValue))
@@ -1012,7 +1001,7 @@ class CValueXorBitwise(CValue):
1012
1001
  return '%s ^ %s' % (lx, rx)
1013
1002
 
1014
1003
 
1015
- class CValueOrBitwise(CValue):
1004
+ class CValueBitwiseOr(CValue):
1016
1005
  def __init__(self, left, right):
1017
1006
  assert(isinstance(left, CValue))
1018
1007
  assert(isinstance(right, CValue))
@@ -1027,7 +1016,7 @@ class CValueOrBitwise(CValue):
1027
1016
  return '%s | %s' % (lx, rx)
1028
1017
 
1029
1018
 
1030
- class CValueAndLogical(CValue):
1019
+ class CValueLogicalAnd(CValue):
1031
1020
  def __init__(self, left, right):
1032
1021
  assert(isinstance(left, CValue))
1033
1022
  assert(isinstance(right, CValue))
@@ -1042,7 +1031,7 @@ class CValueAndLogical(CValue):
1042
1031
  return '%s && %s' % (lx, rx)
1043
1032
 
1044
1033
 
1045
- class CValueOrLogical(CValue):
1034
+ class CValueLogicalOr(CValue):
1046
1035
  def __init__(self, left, right):
1047
1036
  assert(isinstance(left, CValue))
1048
1037
  assert(isinstance(right, CValue))
@@ -1109,7 +1098,7 @@ class CValueVaCopy(CValue):
1109
1098
 
1110
1099
 
1111
1100
  # string concat
1112
- class CValueCat(CValue):
1101
+ class CValueStringConcat(CValue):
1113
1102
  def __init__(self, left, right):
1114
1103
  assert(isinstance(left, CValue))
1115
1104
  assert(isinstance(right, CValue))
@@ -1158,7 +1147,7 @@ class CStmt():
1158
1147
  pass
1159
1148
 
1160
1149
 
1161
- class CStmtCommentLine(CStmt):
1150
+ class CStmtLineComment(CStmt):
1162
1151
  def __init__(self, lines):
1163
1152
  assert(isinstance(lines, list))
1164
1153
  super().__init__()
@@ -1179,7 +1168,7 @@ class CStmtCommentLine(CStmt):
1179
1168
  return sstr
1180
1169
 
1181
1170
 
1182
- class CStmtCommentBlock(CStmt):
1171
+ class CStmtBlockComment(CStmt):
1183
1172
  def __init__(self, text):
1184
1173
  assert(isinstance(text, str))
1185
1174
  super().__init__()
@@ -1212,7 +1201,7 @@ class CStmtBlock(CStmt):
1212
1201
  return sstr
1213
1202
 
1214
1203
 
1215
- class CStmtValueExpr(CStmt):
1204
+ class CStmtExpr(CStmt):
1216
1205
  def __init__(self, value):
1217
1206
  assert(isinstance(value, CValue))
1218
1207
  super().__init__()
@@ -1224,7 +1213,7 @@ class CStmtValueExpr(CStmt):
1224
1213
  return sstr + str_cvalue(self.value) + ';'
1225
1214
 
1226
1215
 
1227
- class CStmtValueAssign(CStmt):
1216
+ class CStmtAssignment(CStmt):
1228
1217
  def __init__(self, lvalue, rvalue):
1229
1218
  assert(isinstance(lvalue, CValue))
1230
1219
  assert(isinstance(rvalue, CValue))
@@ -1237,7 +1226,7 @@ class CStmtValueAssign(CStmt):
1237
1226
  return sstr + "%s = %s;" % (str_cvalue(self.lvalue), str_cvalue(self.rvalue))
1238
1227
 
1239
1228
 
1240
- class CStmtInc(CStmt):
1229
+ class CStmtIncrement(CStmt):
1241
1230
  def __init__(self, value):
1242
1231
  assert(isinstance(value, CValue))
1243
1232
  super().__init__()
@@ -1247,7 +1236,7 @@ class CStmtInc(CStmt):
1247
1236
  return str_nl_indent(self.nl) + "++%s;" % str_cvalue(self.value)
1248
1237
 
1249
1238
 
1250
- class CStmtDec(CStmt):
1239
+ class CStmtDecrement(CStmt):
1251
1240
  def __init__(self, value):
1252
1241
  assert(isinstance(value, CValue))
1253
1242
  super().__init__()
@@ -1259,7 +1248,7 @@ class CStmtDec(CStmt):
1259
1248
 
1260
1249
  class CStmtDeclType(CStmt):
1261
1250
  def __init__(self, type, attributes=None):
1262
- assert(isinstance(type, CTypeNamed))
1251
+ assert(isinstance(type, CTypeIdentifier))
1263
1252
  super().__init__()
1264
1253
  self.type = type
1265
1254
  self.attributes = attributes
@@ -1272,33 +1261,33 @@ class CStmtDeclType(CStmt):
1272
1261
 
1273
1262
 
1274
1263
  class CStmtDefType(CStmt):
1275
- def __init__(self, id_str, type, attributes=None):
1276
- assert(isinstance(id_str, str))
1264
+ def __init__(self, id, type, attributes=None):
1265
+ assert(isinstance(id, str))
1277
1266
  assert(isinstance(type, CType))
1278
1267
  super().__init__()
1279
- self.id_str = id_str
1268
+ self.id = id
1280
1269
  self.type = type
1281
1270
  self.attributes = attributes
1282
1271
 
1283
1272
  def __str__(self):
1284
1273
  sstr = str_nl_indent(self.nl)
1285
1274
  sstr += str_gcc_attributes(self.attributes)
1286
- sstr += 'typedef %s;' % self.type.to_str(text=self.id_str)
1275
+ sstr += 'typedef %s;' % self.type.to_str(text=self.id)
1287
1276
  return sstr
1288
1277
 
1289
1278
 
1290
1279
 
1291
1280
  class CStmtDefVar(CStmt):
1292
- def __init__(self, id_str, type, init_value=None, storage_class='', attributes=None):
1293
- assert(isinstance(id_str, str))
1281
+ def __init__(self, id, type, initializer=None, storage_class='', attributes=None):
1282
+ assert(isinstance(id, str))
1294
1283
  assert(isinstance(type, CType))
1295
- if init_value != None:
1296
- assert(isinstance(init_value, CValue))
1284
+ if initializer != None:
1285
+ assert(isinstance(initializer, CValue))
1297
1286
  super().__init__()
1298
- self.id_str = id_str
1287
+ self.id = id
1299
1288
  self.type = type
1300
1289
  self.storage = storage_class
1301
- self.init_value = init_value
1290
+ self.initializer = initializer
1302
1291
  self.attributes = attributes
1303
1292
 
1304
1293
  def __str__(self):
@@ -1307,22 +1296,22 @@ class CStmtDefVar(CStmt):
1307
1296
  if self.storage not in (None, ''):
1308
1297
  sstr += self.storage + ' '
1309
1298
  #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)
1299
+ sstr += str_ctype(self.type, text=self.id)
1300
+ if self.initializer != None:
1301
+ sstr += ' = %s' % str_cvalue(self.initializer)
1313
1302
  return sstr + ';'
1314
1303
 
1315
1304
 
1316
1305
 
1317
1306
  class CStmtDefFunc(CStmt):
1318
- def __init__(self, id_str, type, block, storage_class='', attributes=None):
1319
- assert(isinstance(id_str, str))
1307
+ def __init__(self, id, type, block, storage_class='', attributes=None):
1308
+ assert(isinstance(id, str))
1320
1309
  assert(isinstance(type, CType))
1321
1310
  assert(isinstance(block, CStmtBlock))
1322
1311
  #if init_value != None:
1323
1312
  # assert(isinstance(init_value, CValue))
1324
1313
  super().__init__()
1325
- self.id_str = id_str
1314
+ self.id = id
1326
1315
  self.type = type
1327
1316
  self.storage = storage_class
1328
1317
  self.block = block
@@ -1334,7 +1323,7 @@ class CStmtDefFunc(CStmt):
1334
1323
  sstr += str_gcc_attributes(self.attributes)
1335
1324
  if self.storage not in (None, ''):
1336
1325
  sstr += self.storage + ' '
1337
- sstr += self.type.to_str(text=self.id_str)
1326
+ sstr += self.type.to_str(text=self.id)
1338
1327
 
1339
1328
  if styleguide['LINE_BREAK_BEFORE_FUNC_BRACE']:
1340
1329
  sstr += str_nl_indent()
@@ -1347,49 +1336,49 @@ class CStmtDefFunc(CStmt):
1347
1336
 
1348
1337
 
1349
1338
  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)))
1339
+ def __init__(self, condition, then_block, else_block):
1340
+ assert(isinstance(condition, CValue))
1341
+ assert(isinstance(then_block, CStmtBlock))
1342
+ if else_block:
1343
+ assert((isinstance(else_block, CStmtBlock) or isinstance(else_block, CStmtIf)))
1355
1344
  super().__init__()
1356
- self.value_cond = value_cond
1357
- self.block_then = block_then
1358
- self.block_else = block_else
1345
+ self.condition = condition
1346
+ self.then_block = then_block
1347
+ self.else_block = else_block
1359
1348
 
1360
1349
  def __str__(self):
1361
1350
  sstr = str_nl_indent(self.nl)
1362
- sstr += "if (%s)" % str_cvalue(self.value_cond)
1351
+ sstr += "if (%s)" % str_cvalue(self.condition)
1363
1352
  if styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1364
1353
  sstr += str_nl_indent()
1365
1354
  else:
1366
1355
  sstr += ' '
1367
- sstr += str(self.block_then)
1368
- if self.block_else != None:
1356
+ sstr += str(self.then_block)
1357
+ if self.else_block != None:
1369
1358
  if styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1370
1359
  sstr += str_nl_indent()
1371
1360
  else:
1372
1361
  sstr += ' '
1373
1362
  sstr += 'else'
1374
- if isinstance(self.block_else, CStmtBlock) and styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1363
+ if isinstance(self.else_block, CStmtBlock) and styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1375
1364
  sstr += str_nl_indent()
1376
1365
  else:
1377
1366
  sstr += ' '
1378
- sstr += str(self.block_else)
1367
+ sstr += str(self.else_block)
1379
1368
  return sstr
1380
1369
 
1381
1370
 
1382
1371
  class CStmtWhile(CStmt):
1383
- def __init__(self, value_cond, block):
1384
- assert(isinstance(value_cond, CValue))
1372
+ def __init__(self, condition, block):
1373
+ assert(isinstance(condition, CValue))
1385
1374
  assert(isinstance(block, CStmtBlock))
1386
1375
  super().__init__()
1387
- self.value_cond = value_cond
1376
+ self.condition = condition
1388
1377
  self.block = block
1389
1378
 
1390
1379
  def __str__(self):
1391
1380
  sstr = str_nl_indent(self.nl)
1392
- sstr += "while (%s)" % str_cvalue(self.value_cond)
1381
+ sstr += "while (%s)" % str_cvalue(self.condition)
1393
1382
  if styleguide['LINE_BREAK_BEFORE_BLOCK_BRACE']:
1394
1383
  sstr += str_nl_indent()
1395
1384
  else:
@@ -1399,17 +1388,17 @@ class CStmtWhile(CStmt):
1399
1388
 
1400
1389
 
1401
1390
  class CStmtReturn(CStmt):
1402
- def __init__(self, value_retval):
1403
- if value_retval != None:
1404
- assert(isinstance(value_retval, CValue))
1391
+ def __init__(self, return_value):
1392
+ if return_value != None:
1393
+ assert(isinstance(return_value, CValue))
1405
1394
  super().__init__()
1406
- self.value_retval = value_retval
1395
+ self.return_value = return_value
1407
1396
 
1408
1397
  def __str__(self):
1409
1398
  sstr = str_nl_indent(self.nl)
1410
1399
  sstr += 'return'
1411
- if self.value_retval != None:
1412
- sstr += ' ' + str_cvalue(self.value_retval)
1400
+ if self.return_value != None:
1401
+ sstr += ' ' + str_cvalue(self.return_value)
1413
1402
  return sstr + ";"
1414
1403
 
1415
1404
 
@@ -1442,7 +1431,7 @@ class CStmtContinue(CStmt):
1442
1431
  # : clobbered registers/memory /* optional */
1443
1432
  #);
1444
1433
 
1445
- class CStmtAsm(CStmt):
1434
+ class CStmtInlineAsm(CStmt):
1446
1435
  def __init__(self, text, outputs, inputs, clobbers):
1447
1436
  super().__init__()
1448
1437
  assert(isinstance(text, str))
@@ -1481,7 +1470,7 @@ class CStmtAsm(CStmt):
1481
1470
 
1482
1471
 
1483
1472
 
1484
- class CInsert(CStmt):
1473
+ class CRawText(CStmt):
1485
1474
  def __init__(self, text):
1486
1475
  super().__init__()
1487
1476
  self.text = text
@@ -1491,7 +1480,7 @@ class CInsert(CStmt):
1491
1480
  return self.text
1492
1481
 
1493
1482
 
1494
- class CMacrodefinition():
1483
+ class CMacroDef():
1495
1484
  def __init__(self, id, text=None):
1496
1485
  assert(isinstance(id, str))
1497
1486
  if text:
@@ -1510,7 +1499,7 @@ class CMacrodefinition():
1510
1499
  return sstr
1511
1500
 
1512
1501
 
1513
- class CMacrodefinitionValue():
1502
+ class CMacroDefValue():
1514
1503
  def __init__(self, id, value):
1515
1504
  assert(isinstance(id, str))
1516
1505
  assert(isinstance(value, CValue))
@@ -1543,18 +1532,18 @@ class CMacroUndef():
1543
1532
 
1544
1533
 
1545
1534
  class CInclude():
1546
- def __init__(self, text, isglobal):
1535
+ def __init__(self, text, is_system):
1547
1536
  assert(isinstance(text, str))
1548
- assert(isinstance(isglobal, bool))
1537
+ assert(isinstance(is_system, bool))
1549
1538
  self.nl = 1 #!!! (because it is not CStmt...)
1550
1539
  super().__init__()
1551
1540
  self.text = text
1552
- self.isglobal = isglobal
1541
+ self.is_system = is_system
1553
1542
  self.mark = None
1554
1543
 
1555
1544
  def __str__(self):
1556
1545
  #sstr = str_nl_indent(self.nl)
1557
- if self.isglobal:
1546
+ if self.is_system:
1558
1547
  return "\n#include <%s>" % self.text
1559
1548
  return "\n#include \"%s\"" % self.text
1560
1549
 
@@ -1565,7 +1554,7 @@ def str_cdef(x):
1565
1554
 
1566
1555
 
1567
1556
  # pairs = ("macro text", [<defs>])
1568
- class CIfdefRegion():
1557
+ class CConditionalRegion():
1569
1558
  def __init__(self, pairs, _else=None):
1570
1559
  self.pairs = pairs
1571
1560
  self._else = _else
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cshape"
7
- version = "0.1.0"
7
+ version = "0.2.1"
8
8
  description = "A dependency-free C AST + printer: build a tree in Python, get C11 source back"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -1,22 +1,22 @@
1
1
  import unittest
2
2
 
3
3
  from cshape import (
4
- CField, CStmtBlock, CStmtDefFunc, CStmtReturn, CTypeFunc, CTypeNamed,
5
- CValueAdd, CValueNamed, render,
4
+ CField, CStmtBlock, CStmtDefFunc, CStmtReturn, CTypeFunction, CTypeIdentifier,
5
+ CValueAdd, CValueIdentifier, render,
6
6
  )
7
7
 
8
8
 
9
9
  class SmokeTest(unittest.TestCase):
10
10
  def test_render_function(self):
11
11
  func = CStmtDefFunc(
12
- id_str='add',
13
- type=CTypeFunc(
14
- params=[CField(id_str='a', type=CTypeNamed('int')),
15
- CField(id_str='b', type=CTypeNamed('int'))],
16
- to=CTypeNamed('int'),
12
+ id='add',
13
+ type=CTypeFunction(
14
+ params=[CField(id='a', type=CTypeIdentifier('int')),
15
+ CField(id='b', type=CTypeIdentifier('int'))],
16
+ to=CTypeIdentifier('int'),
17
17
  ),
18
18
  block=CStmtBlock([
19
- CStmtReturn(CValueAdd(CValueNamed('a'), CValueNamed('b'))),
19
+ CStmtReturn(CValueAdd(CValueIdentifier('a'), CValueIdentifier('b'))),
20
20
  ]),
21
21
  )
22
22
 
@@ -27,12 +27,12 @@ class SmokeTest(unittest.TestCase):
27
27
 
28
28
  def test_render_is_reentrant(self):
29
29
  # render() must not leak indent/style state between independent calls
30
- named = CTypeNamed('int')
30
+ named = CTypeIdentifier('int')
31
31
  render(named, style='modern')
32
32
  self.assertEqual(render(named, style='legacy'), 'int')
33
33
 
34
34
  def test_mark_shows_up_as_comment(self):
35
- v = CValueNamed('x')
35
+ v = CValueIdentifier('x')
36
36
  v.mark = 'debug-note'
37
37
  self.assertIn('/*debug-note*/', render(v))
38
38
 
File without changes
File without changes