Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Tiny BASIC
  3.  * Expression Handling Header
  4.  *
  5.  * Released as Public Domain by Damian Gareth Walker 2019
  6.  * Created: 16-Aug-2019
  7.  */
  8.  
  9.  
  10. #ifndef __EXPRESSION_H__
  11. #define __EXPRESSION_H__
  12.  
  13.  
  14. /* Forward Declarations */
  15. typedef struct expression_node ExpressionNode;
  16. typedef struct right_hand_term RightHandTerm;
  17. typedef struct term_node TermNode;
  18. typedef struct right_hand_factor RightHandFactor;
  19.  
  20.  
  21. /*
  22.  * Data Definitions - Factors
  23.  */
  24.  
  25.  
  26. /* The different classes of factor */
  27. typedef enum {
  28.   FACTOR_NONE, /* class as yet undetermined */
  29.   FACTOR_VARIABLE, /* a variable */
  30.   FACTOR_VALUE, /* an integer value */
  31.   FACTOR_EXPRESSION /* a bracketed expression */
  32. } FactorClass;
  33.  
  34. /* The signs */
  35. typedef enum {
  36.   SIGN_POSITIVE,
  37.   SIGN_NEGATIVE
  38. } SignClass;
  39.  
  40. /* A factor */
  41. typedef struct {
  42.   FactorClass class; /* what kind of factor is this? */
  43.   SignClass sign; /* which sign, positive or negative? */
  44.   union {
  45.     int variable;
  46.     int value;
  47.     ExpressionNode *expression;
  48.   } data;
  49. } FactorNode;
  50.  
  51.  
  52. /* The term operators */
  53. typedef enum {
  54.   TERM_OPERATOR_NONE, /* single-factor term */
  55.   TERM_OPERATOR_MULTIPLY, /* multiply */
  56.   TERM_OPERATOR_DIVIDE /* divide */
  57. } TermOperator;
  58.  
  59. /* the operator and right-hand part of a term */
  60. typedef struct right_hand_factor {
  61.   TermOperator op; /* the operator to apply: muliply or divide */
  62.   FactorNode *factor; /* the factor to multiply or divide by */
  63.   RightHandFactor *next; /* the next part of the term, if any */
  64. } RightHandFactor;
  65.  
  66. /* A term */
  67. typedef struct term_node {
  68.   FactorNode *factor; /* the first factor of an expression */
  69.   RightHandFactor *next; /* the right side term, if any */
  70. } TermNode;
  71.  
  72. /* Expression Operator */
  73. typedef enum {
  74.   EXPRESSION_OPERATOR_NONE, /* single-term expression */
  75.   EXPRESSION_OPERATOR_PLUS, /* addition */
  76.   EXPRESSION_OPERATOR_MINUS /* substraction */
  77. } ExpressionOperator;
  78.  
  79. /* the operator and right-hand part of an expression */
  80. typedef struct right_hand_term {
  81.   ExpressionOperator op; /* the operator to apply: plus or minus */
  82.   TermNode *term; /* the term to add or subtract */
  83.   RightHandTerm *next; /* next part of the expression, if any */
  84. } RightHandTerm;
  85.  
  86. /* An expression */
  87. typedef struct expression_node {
  88.   TermNode *term; /* The first term of an expression */
  89.   RightHandTerm *next; /* the right side expression, if any */
  90. } ExpressionNode;
  91.  
  92.  
  93. /*
  94.  * Function Declarations
  95.  */
  96.  
  97.  
  98. /*
  99.  * Constructor for a factor
  100.  * returns:
  101.  *   FactorNode*   the new factor
  102.  */
  103. FactorNode *factor_create (void);
  104.  
  105. /*
  106.  * Destructor for a factor
  107.  * params:
  108.  *   FactorNode*   factor   the doomed factor
  109.  */
  110. void factor_destroy (FactorNode *factor);
  111.  
  112. /*
  113.  * Constructor for a right-hand factor of a term
  114.  * returns:
  115.  *   RightHandFactor*   the new RH factor of a term
  116.  */
  117. RightHandFactor *rhfactor_create (void);
  118.  
  119. /*
  120.  * Recursive destructor for a right-hand factor of a term
  121.  * params:
  122.  *   RightHandFactor*   rhterm   the doomed RH factor of a term
  123.  */
  124. void rhfactor_destroy (RightHandFactor *rhterm);
  125.  
  126. /*
  127.  * Constructor for a term
  128.  * returns:
  129.  *   TermNode*   the new term
  130.  */
  131. TermNode *term_create (void);
  132.  
  133. /*
  134.  * Destructor for a term
  135.  * params:
  136.  *   TermNode*   term   the doomed term
  137.  */
  138. void term_destroy (TermNode *term);
  139.  
  140. /*
  141.  * Constructor for a right-hand expression
  142.  * returns:
  143.  *   RightHandTerm*   the RH term of an expression
  144.  */
  145. RightHandTerm *rhterm_create (void);
  146.  
  147. /*
  148.  * Recursive destructor for a right-hand term of an expression
  149.  * params:
  150.  *   RightHandTerm*   rhexpression   the doomed RH expression
  151.  */
  152. void rhterm_destroy (RightHandTerm *rhterm);
  153.  
  154. /*
  155.  * Constructor for an expression
  156.  * returns:
  157.  *   ExpressionNode*   the new expression
  158.  */
  159. ExpressionNode *expression_create (void);
  160.  
  161. /*
  162.  * Destructor for a factor
  163.  * params:
  164.  *   FactorNode*   factor   the doomed factor
  165.  */
  166. void factor_destroy (FactorNode *factor);
  167.  
  168. /*
  169.  * Destructor for a expression
  170.  * params:
  171.  *   ExpressionNode*   expression   the doomed expression
  172.  */
  173. void expression_destroy (ExpressionNode *expression);
  174.  
  175.  
  176. #endif
  177.