cnodes.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """
  2. AST nodes specific to the C family of languages
  3. """
  4. from sympy.codegen.ast import (
  5. Attribute, Declaration, Node, String, Token, Type, none,
  6. FunctionCall, CodeBlock
  7. )
  8. from sympy.core.basic import Basic
  9. from sympy.core.containers import Tuple
  10. from sympy.core.sympify import sympify
  11. void = Type('void')
  12. restrict = Attribute('restrict') # guarantees no pointer aliasing
  13. volatile = Attribute('volatile')
  14. static = Attribute('static')
  15. def alignof(arg):
  16. """ Generate of FunctionCall instance for calling 'alignof' """
  17. return FunctionCall('alignof', [String(arg) if isinstance(arg, str) else arg])
  18. def sizeof(arg):
  19. """ Generate of FunctionCall instance for calling 'sizeof'
  20. Examples
  21. ========
  22. >>> from sympy.codegen.ast import real
  23. >>> from sympy.codegen.cnodes import sizeof
  24. >>> from sympy import ccode
  25. >>> ccode(sizeof(real))
  26. 'sizeof(double)'
  27. """
  28. return FunctionCall('sizeof', [String(arg) if isinstance(arg, str) else arg])
  29. class CommaOperator(Basic):
  30. """ Represents the comma operator in C """
  31. def __new__(cls, *args):
  32. return Basic.__new__(cls, *[sympify(arg) for arg in args])
  33. class Label(Node):
  34. """ Label for use with e.g. goto statement.
  35. Examples
  36. ========
  37. >>> from sympy import ccode, Symbol
  38. >>> from sympy.codegen.cnodes import Label, PreIncrement
  39. >>> print(ccode(Label('foo')))
  40. foo:
  41. >>> print(ccode(Label('bar', [PreIncrement(Symbol('a'))])))
  42. bar:
  43. ++(a);
  44. """
  45. __slots__ = ('name', 'body')
  46. defaults = {'body': none}
  47. _construct_name = String
  48. @classmethod
  49. def _construct_body(cls, itr):
  50. if isinstance(itr, CodeBlock):
  51. return itr
  52. else:
  53. return CodeBlock(*itr)
  54. class goto(Token):
  55. """ Represents goto in C """
  56. __slots__ = ('label',)
  57. _construct_label = Label
  58. class PreDecrement(Basic):
  59. """ Represents the pre-decrement operator
  60. Examples
  61. ========
  62. >>> from sympy.abc import x
  63. >>> from sympy.codegen.cnodes import PreDecrement
  64. >>> from sympy import ccode
  65. >>> ccode(PreDecrement(x))
  66. '--(x)'
  67. """
  68. nargs = 1
  69. class PostDecrement(Basic):
  70. """ Represents the post-decrement operator """
  71. nargs = 1
  72. class PreIncrement(Basic):
  73. """ Represents the pre-increment operator """
  74. nargs = 1
  75. class PostIncrement(Basic):
  76. """ Represents the post-increment operator """
  77. nargs = 1
  78. class struct(Node):
  79. """ Represents a struct in C """
  80. __slots__ = ('name', 'declarations')
  81. defaults = {'name': none}
  82. _construct_name = String
  83. @classmethod
  84. def _construct_declarations(cls, args):
  85. return Tuple(*[Declaration(arg) for arg in args])
  86. class union(struct):
  87. """ Represents a union in C """