Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Tiny BASIC
  3.  * Token handling functions
  4.  *
  5.  * Copyright (C) Damian Walker 2019
  6.  * Created: 15-Aug-2019
  7.  */
  8.  
  9.  
  10. /* includes */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "token.h"
  15.  
  16.  
  17. /*
  18.  * Internal data structures
  19.  */
  20.  
  21.  
  22. /* Private data */
  23. typedef struct {
  24.   TokenClass class; /* class of token */
  25.   int line; /* line on which token was found */
  26.   int pos; /* position within the line on which token was found */
  27.   char *content; /* representation of token */
  28. } Private;
  29.  
  30.  
  31. /*
  32.  * Internal Data
  33.  */
  34.  
  35.  
  36. /* Convenience variables */
  37. static Token *this; /* the token object */
  38. static Private *data; /* the private data */
  39.  
  40.  
  41. /*
  42.  * Public methods
  43.  */
  44.  
  45.  
  46. /*
  47.  * Return the class of the token
  48.  * params:
  49.  *   Token*   token   the token object
  50.  * returns:
  51.  *   TokenClass       the class of the token
  52.  */
  53. static TokenClass get_class (Token *token) {
  54.   this = token;
  55.   data = token->data;
  56.   return data->class;
  57. }
  58.  
  59. /*
  60.  * Return the line on which the token begins
  61.  * params:
  62.  *   Token*   token   the token object
  63.  * returns:
  64.  *   int              the line on which the token begins
  65.  */
  66. static int get_line (Token *token) {
  67.   this = token;
  68.   data = token->data;
  69.   return data->line;
  70. }
  71.  
  72. /*
  73.  * Return the character position on which the token begins
  74.  * params:
  75.  *   Token*   token   the token object
  76.  * returns:
  77.  *   int              the position on which the token begins
  78.  */
  79. static int get_pos (Token *token) {
  80.   this = token;
  81.   data = token->data;
  82.   return data->pos;
  83. }
  84.  
  85. /*
  86.  * Return the content of the token begins
  87.  * params:
  88.  *   Token*   token   the token object
  89.  * returns:
  90.  *   char*            the text content of the token
  91.  */
  92. static char *get_content (Token *token) {
  93.   this = token;
  94.   data = token->data;
  95.   return data->content;
  96. }
  97.  
  98. /*
  99.  * Set the token class
  100.  * params:
  101.  *   Token*       token   the token to set
  102.  *   TokenClass   class   the class
  103.  */
  104. static void set_class (Token *token, TokenClass class) {
  105.   this = token;
  106.   data = this->data;
  107.   data->class = class;
  108. }
  109.  
  110. /*
  111.  * Set the token line and position
  112.  * params:
  113.  *   Token*   token   the token to set
  114.  *   int      line    the line on which the token began
  115.  *   int      pos     the position on which the token began
  116.  */
  117. static void set_line_pos (Token *token, int line, int pos) {
  118.   this = token;
  119.   data = this->data;
  120.   data->line = line;
  121.   data->pos = pos;;
  122. }
  123.  
  124. /*
  125.  * Set the token's text content
  126.  * params:
  127.  *   Token*   token     the token to alter
  128.  *   char*    content   the text content to set
  129.  */
  130. static void set_content (Token *token, char *content) {
  131.   this = token;
  132.   data = this->data;
  133.   if (data->content)
  134.     free (data->content);
  135.   data->content = malloc (strlen (content) + 1);
  136.   strcpy (data->content, content);
  137. }
  138.  
  139. /*
  140.  * Set all of the values of an existing token in a single function call.
  141.  * params:
  142.  *   Token*   token   the token to update
  143.  *   TokenClass   class     class of token to initialise
  144.  *   int          line      line on which the token occurred
  145.  *   int          pos       character position on which the token occurred
  146.  *   char*        content   the textual content of the token
  147.  */
  148. static void initialise (Token *token, TokenClass class, int line, int pos,
  149.   char *content) {
  150.  
  151.   /* set convenience variables */
  152.   this = token;
  153.   data = this->data;
  154.  
  155.   /* initialise the easy members */
  156.   data->class = class ? class : TOKEN_NONE;
  157.   data->line = line ? line : 0;
  158.   data->pos = pos ? pos : 0;
  159.  
  160.   /* initialise the content */
  161.   if (content)
  162.     set_content (this, content);
  163. }
  164.  
  165. /*
  166.  * Token destructor
  167.  * params:
  168.  *   Token*   token   the doomed token
  169.  */
  170. static void destroy (Token *token) {
  171.   if ((this = token)) {
  172.     data = this->data;
  173.     if (data->content)
  174.       free (data->content);
  175.     free (data);
  176.     free (this);
  177.   }
  178. }
  179.  
  180.  
  181. /*
  182.  * Constructors
  183.  */
  184.  
  185.  
  186. /*
  187.  * Token constructor without values to initialise
  188.  * returns:
  189.  *   Token*   the created token
  190.  */
  191. Token *new_Token (void) {
  192.  
  193.   /* create the structure */
  194.   this = malloc (sizeof (Token));
  195.   this->data = data = malloc (sizeof (Private));
  196.  
  197.   /* initialise properties */
  198.   data->class = TOKEN_NONE;
  199.   data->line = data->pos = 0;
  200.   data->content = NULL;
  201.  
  202.   /* set up methods */
  203.   this->initialise = initialise;
  204.   this->get_class = get_class;
  205.   this->get_line = get_line;
  206.   this->get_pos = get_pos;
  207.   this->get_content = get_content;
  208.   this->set_class = set_class;
  209.   this->set_line_pos = set_line_pos;
  210.   this->set_content = set_content;
  211.   this->destroy = destroy;
  212.  
  213.   /* return the created structure */
  214.   return this;
  215. }
  216.  
  217. /*
  218.  * Token constructor with values to initialise
  219.  * params:
  220.  *   TokenClass   class     class of token to initialise
  221.  *   int          line      line on which the token occurred
  222.  *   int          pos       character position on which the token occurred
  223.  *   char*        content   the textual content of the token
  224.  * returns:
  225.  *   Token*                 the created token
  226.  */
  227. Token *new_Token_init (TokenClass class, int line, int pos, char *content) {
  228.  
  229.   /* create a blank token */
  230.   this = new_Token ();
  231.   this->initialise (this, class, line, pos, content);
  232.  
  233.   /* return the new token */
  234.   return this;
  235. }
  236.