Guest User

Untitled

a guest
Nov 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. //system of functional nodes
  4.  
  5. #define Wind_BASE struct WindNode* next; \
  6. enum WindType type;
  7.  
  8.  
  9. enum WindType
  10. {
  11. WindType_INT,
  12. WindType_ADD
  13. };
  14.  
  15.  
  16. //base type struct for inheritance
  17. struct WindNode
  18. {
  19. Wind_BASE
  20. };
  21.  
  22.  
  23. struct WindAdd
  24. {
  25. Wind_BASE
  26. struct WindNode* first;
  27. struct WindNode* second;
  28. };
  29.  
  30.  
  31. struct WindInt
  32. {
  33. Wind_BASE
  34. int val;
  35. };
  36.  
  37. //method to add and return result of add node
  38. int addTest(struct WindNode* node)
  39. {
  40. if(node->type == WindType_ADD)
  41. {
  42. struct WindNode* a = ((struct WindAdd*)node)->first;
  43. struct WindNode* b = ((struct WindAdd*)node)->second;
  44. return ((struct WindInt*)a)->val + ((struct WindInt*)b)->val;
  45. }
  46. else return 0;
  47. }
  48.  
  49.  
  50. int main(int argc, char const *argv[])
  51. {
  52. //init phase
  53. struct WindAdd add;
  54. struct WindAdd* addp = &add;
  55. add.type = WindType_ADD;
  56.  
  57. struct WindInt a;
  58. struct WindInt* ap = &a;
  59. a.val = 3;
  60. struct WindInt b;
  61. struct WindInt* bp = &b;
  62. b.val = 3;
  63.  
  64. add.first = (struct WindNode*)ap;
  65. add.second = (struct WindNode*)bp;
  66.  
  67. //treats an ASt-like addition node as computation
  68. printf("The result is %d\n", addTest((struct WindNode*)addp));
  69. return 0;
  70. }
Add Comment
Please, Sign In to add comment