Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Tiny BASIC
  3.  * Statement Handling Header
  4.  *
  5.  * Released as Public Domain by Damian Gareth Walker 2019
  6.  * Created: 15-Aug-2019
  7.  */
  8.  
  9.  
  10. #ifndef __STATEMENT_H__
  11. #define __STATEMENT_H__
  12.  
  13.  
  14. /* Pre-requisite headers */
  15. #include "expression.h"
  16.  
  17. /* Forward Declarations */
  18. typedef struct program_line_node ProgramLineNode;
  19. typedef struct statement_node StatementNode;
  20. typedef struct output_node OutputNode;
  21. typedef struct variable_list_node VariableListNode;
  22.  
  23.  
  24. /*
  25.  * Data Definitions
  26.  */
  27.  
  28.  
  29. /* The types of output allowed in a PRINT statement */
  30. typedef enum {
  31.   OUTPUT_STRING, /* a literal string */
  32.   OUTPUT_EXPRESSION /* an expression */
  33. } OutputClass;
  34.  
  35. /* The relational operators */
  36. typedef enum {
  37.   RELOP_EQUAL, /* = */
  38.   RELOP_UNEQUAL, /* <> or >< */
  39.   RELOP_LESSTHAN, /* < */
  40.   RELOP_LESSOREQUAL, /* <= */
  41.   RELOP_GREATERTHAN, /* > */
  42.   RELOP_GREATEROREQUAL /* >= */
  43. } RelationalOperator;
  44.  
  45. /* Expressions or strings to output */
  46. typedef struct output_node {
  47.   OutputClass class; /* string or expression */
  48.   union {
  49.     char *string; /* a literal string to output */
  50.     ExpressionNode *expression; /* an expression to output */
  51.   } output;
  52.   OutputNode *next; /* the next output node, if any */
  53. } OutputNode;
  54.  
  55. /* List of variables to input */
  56. typedef struct variable_list_node {
  57.   int variable; /* the variable */
  58.   VariableListNode *next; /* next variable, if any */
  59. } VariableListNode;
  60.  
  61. /*
  62.  * Structures for statements in general
  63.  */
  64.  
  65. /* Let Statement Node */
  66. typedef struct {
  67.   int variable; /* the variable to assign, 1..26 for A..Z */
  68.   ExpressionNode *expression; /* the expression to assign to it */
  69. } LetStatementNode;
  70.  
  71. /* If Statement Node */
  72. typedef struct {
  73.   ExpressionNode *left; /* the left-hand expression */
  74.   RelationalOperator op; /* the comparison operator used */
  75.   ExpressionNode *right; /* the right-hand expression */
  76.   StatementNode *statement; /* statement to execute if condition is true */
  77. } IfStatementNode;
  78.  
  79. /* Print Statement Node */
  80. typedef struct {
  81.   OutputNode *first; /* the first expression to print */
  82. } PrintStatementNode;
  83.  
  84. /* Input Statement Node */
  85. typedef struct {
  86.   VariableListNode *first; /* the first variable to input */
  87. } InputStatementNode;
  88.  
  89. /* Goto Statement Node */
  90. typedef struct {
  91.   ExpressionNode *label; /* an expression that computes the label */
  92. } GotoStatementNode;
  93.  
  94. /* Gosub Statement Node */
  95. typedef struct {
  96.   ExpressionNode *label; /* an expression that computes the label */
  97. } GosubStatementNode;
  98.  
  99. /* Statement classes */
  100. typedef enum {
  101.   STATEMENT_NONE, /* unknown statement */
  102.   STATEMENT_LET, /* LET variable=expression */
  103.   STATEMENT_IF, /* IF condition THEN statement */
  104.   STATEMENT_GOTO, /* GOTO expression */
  105.   STATEMENT_GOSUB, /* GOSUB expression */
  106.   STATEMENT_RETURN, /* RETURN */
  107.   STATEMENT_END, /* END */
  108.   STATEMENT_PRINT, /* PRINT print-list */
  109.   STATEMENT_INPUT /* INPUT var-list */
  110. } StatementClass;
  111.  
  112. /* Common Statement Node */
  113. typedef struct statement_node {
  114.   StatementClass class; /* which type of statement this is */
  115.   union {
  116.     LetStatementNode *letn; /* a LET statement */
  117.     IfStatementNode *ifn; /* an IF statement */
  118.     GotoStatementNode *goton; /* a GOTO statement */
  119.     GosubStatementNode *gosubn; /* a GOSUB statement */
  120.     /* a RETURN statement requires no extra data */
  121.     /* an END statement requires no extra data */
  122.     PrintStatementNode *printn; /* a PRINT statement */
  123.     InputStatementNode *inputn; /* an INPUT statement */
  124.   } statement;
  125. } StatementNode;
  126.  
  127. /* a program line */
  128. typedef struct program_line_node {
  129.   int label; /* line label */
  130.   StatementNode *statement; /* the current statement */
  131.   ProgramLineNode *next; /* the next statement */
  132. } ProgramLineNode;
  133.  
  134.  
  135. /* the program */
  136. typedef struct {
  137.   ProgramLineNode *first; /* first program statement */
  138. } ProgramNode;
  139.  
  140.  
  141. /*
  142.  * Function Declarations
  143.  */
  144.  
  145.  
  146. /*
  147.  * LET statement constructor
  148.  * returns:
  149.  *   LetStatementNode*   the created LET statement
  150.  */
  151. LetStatementNode *statement_create_let (void);
  152.  
  153. /*
  154.  * IF statement constructor
  155.  * returns:
  156.  *   IfStatementNode*   the created IF statement
  157.  */
  158. IfStatementNode *statement_create_if (void);
  159.  
  160. /*
  161.  * GOTO Statement Constructor
  162.  * returns:
  163.  *   GotoStatementNode*   the new GOTO statement
  164.  */
  165. GotoStatementNode *statement_create_goto (void);
  166.  
  167. /*
  168.  * GOSUB Statement Constructor
  169.  * returns:
  170.  *   GosubStatementNode*   the new GOSUB statement
  171.  */
  172. GosubStatementNode *statement_create_gosub (void);
  173.  
  174. /*
  175.  * PRINT statement constructor
  176.  * returns:
  177.  *   PrintStatementNode*   the created PRINT statement
  178.  */
  179. PrintStatementNode *statement_create_print (void);
  180.  
  181. /*
  182.  * INPUT statement constructor
  183.  * returns:
  184.  *   InputStatementNode*   initialised INPUT statement data
  185.  */
  186. InputStatementNode *statement_create_input (void);
  187.  
  188. /*
  189.  * Statement constructor
  190.  * returns:
  191.  *   StatementNode*   the newly-created blank statement
  192.  */
  193. StatementNode *statement_create (void);
  194.  
  195. /*
  196.  * Statement destructor
  197.  * params:
  198.  *   StatementNode*   statement   the doomed statement
  199.  */
  200. void statement_destroy (StatementNode *statement);
  201.  
  202. /*
  203.  * Program Line Constructor
  204.  * returns:
  205.  *   ProgramLineNode*   the new program line
  206.  */
  207. ProgramLineNode *program_line_create (void);
  208.  
  209. /*
  210.  * Program Line Destructor
  211.  * params:
  212.  *   ProgramLineNode*   program_line   the doomed program line
  213.  * params:
  214.  *   ProgramLineNode*                  the next program line
  215.  */
  216. ProgramLineNode *program_line_destroy (ProgramLineNode *program_line);
  217.  
  218. /*
  219.  * Program Constructor
  220.  * returns:
  221.  *   ProgramNode*   the constructed program
  222.  */
  223. ProgramNode *program_create (void);
  224.  
  225. /*
  226.  * Program Destructor
  227.  * params:
  228.  *   ProgramNode*   program   the doomed program
  229.  */
  230. void program_destroy (ProgramNode *program);
  231.  
  232.  
  233. #endif
  234.