Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Tiny BASIC
  3.  * Expression Handling Module
  4.  *
  5.  * Released as Public Domain by Damian Gareth Walker 2019
  6.  * Created: 16-Aug-2019
  7.  */
  8.  
  9.  
  10. /* included headers */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "parser.h"
  15. #include "expression.h"
  16. #include "errors.h"
  17.  
  18.  
  19. /*
  20.  * Functions for Dealing with Factors
  21.  */
  22.  
  23.  
  24. /*
  25.  * Constructor for a factor
  26.  * returns:
  27.  *   FactorNode*   the new factor
  28.  */
  29. FactorNode *factor_create (void) {
  30.  
  31.   /* local variables */
  32.   FactorNode *factor; /* the new factor */
  33.  
  34.   /* allocate memory and initialise members */
  35.   factor = malloc (sizeof (FactorNode));
  36.   factor->class = FACTOR_NONE;
  37.   factor->sign = SIGN_POSITIVE;
  38.  
  39.   /* return the factor */
  40.   return factor;
  41. }
  42.  
  43. /*
  44.  * Destructor for a factor
  45.  * params:
  46.  *   FactorNode*   factor   the doomed factor
  47.  */
  48. void factor_destroy (FactorNode *factor) {
  49.   if (factor->class == FACTOR_EXPRESSION && factor->data.expression) {
  50.     expression_destroy (factor->data.expression);
  51.   }
  52.   free (factor);
  53. }
  54.  
  55.  
  56. /*
  57.  * Functions for Dealing with Terms
  58.  */
  59.  
  60.  
  61. /*
  62.  * Constructor for a right-hand factor of a term
  63.  * returns:
  64.  *   RightHandFactor*   the new RH factor of a term
  65.  */
  66. RightHandFactor *rhfactor_create (void) {
  67.  
  68.   /* local variables */
  69.   RightHandFactor *rhfactor; /* the RH factor of a term to create */
  70.  
  71.   /* allocate memory and initialise members */
  72.   rhfactor = malloc (sizeof (RightHandFactor));
  73.   rhfactor->op = TERM_OPERATOR_NONE;
  74.   rhfactor->factor = NULL;
  75.   rhfactor->next = NULL;
  76.  
  77.   /* return the new RH term */
  78.   return rhfactor;
  79. }
  80.  
  81. /*
  82.  * Recursive destructor for a right-hand factor of a term
  83.  * params:
  84.  *   RightHandFactor*   rhfactor   the doomed RH factor of a term
  85.  */
  86. void rhfactor_destroy (RightHandFactor *rhfactor) {
  87.   if (rhfactor->next)
  88.     rhfactor_destroy (rhfactor->next);
  89.   if (rhfactor->factor)
  90.     factor_destroy (rhfactor->factor);
  91.   free (rhfactor);
  92. }
  93.  
  94. /*
  95.  * Constructor for a term
  96.  * returns:
  97.  *   TermNode*   the new term
  98.  */
  99. TermNode *term_create (void) {
  100.  
  101.   /* local variables */
  102.   TermNode *term; /* the new term */
  103.  
  104.   /* allocate memory and initialise members */
  105.   term = malloc (sizeof (TermNode));
  106.   term->factor = NULL;
  107.   term->next = NULL;
  108.  
  109.   /* return the new term */
  110.   return term;
  111. }
  112.  
  113. /*
  114.  * Destructor for a term
  115.  * params:
  116.  *   TermNode*   term   the doomed term
  117.  */
  118. void term_destroy (TermNode *term) {
  119.  
  120.   /* destroy the consituent parts of the term */
  121.   if (term->factor)
  122.     factor_destroy (term->factor);
  123.   if (term->next)
  124.     rhfactor_destroy (term->next);
  125.  
  126.   /* destroy the term itself */
  127.   free (term);
  128. }
  129.  
  130.  
  131. /*
  132.  * Functions for dealing with Expressions
  133.  */
  134.  
  135.  
  136. /*
  137.  * Constructor for a right-hand expression
  138.  * returns:
  139.  *   RightHandTerm*   the RH term of an expression
  140.  */
  141. RightHandTerm *rhterm_create (void) {
  142.  
  143.   /* local variables */
  144.   RightHandTerm *rhterm; /* the new RH expression */
  145.  
  146.   /* allocate memory and initialise members */
  147.   rhterm = malloc (sizeof (RightHandTerm));
  148.   rhterm->op = EXPRESSION_OPERATOR_NONE;
  149.   rhterm->term = NULL;
  150.   rhterm->next = NULL;
  151.  
  152.   /* return the new right-hand expression */
  153.   return rhterm;
  154. }
  155.  
  156. /*
  157.  * Recursive destructor for a right-hand term of an expression
  158.  * params:
  159.  *   RightHandTerm*   rhterm   the doomed RH expression
  160.  */
  161. void rhterm_destroy (RightHandTerm *rhterm) {
  162.   if (rhterm->next)
  163.     rhterm_destroy (rhterm->next);
  164.   if (rhterm->term)
  165.     term_destroy (rhterm->term);
  166.   free (rhterm);
  167. }
  168.  
  169. /*
  170.  * Constructor for an expression
  171.  * returns:
  172.  *   ExpressionNode*   the new expression
  173.  */
  174. ExpressionNode *expression_create (void) {
  175.  
  176.   /* local variables */
  177.   ExpressionNode *expression; /* the new expression */
  178.  
  179.   /* allocate memory and initialise members */
  180.   expression = malloc (sizeof (ExpressionNode));
  181.   expression->term = NULL;
  182.   expression->next = NULL;
  183.  
  184.   /* return the new expression */
  185.   return expression;
  186. }
  187.  
  188. /*
  189.  * Destructor for a expression
  190.  * params:
  191.  *   ExpressionNode*   expression   the doomed expression
  192.  */
  193. void expression_destroy (ExpressionNode *expression) {
  194.  
  195.   /* destroy the consituent parts of the expression */
  196.   if (expression->term)
  197.     term_destroy (expression->term);
  198.   if (expression->next)
  199.     rhterm_destroy (expression->next);
  200.  
  201.   /* destroy the expression itself */
  202.   free (expression);
  203. }
  204.