/*****************************************************
 * PROJECT:       BBB interpreter
 * ORGANIZATION:  Microfluffy Corp.
 * LANGUAGE:      any ANSI C++ complaint
 * FILE:          bbb.h
 * DESCRIPTION:   Global header file which defines major
 *                datatypes, constants, and functions.
 *                READ THIS FILE FIRST!!!
 * VERSION:       1.0
 *****************************************************/


/****************************************************************/
/* Global constants                                             */

#define MAX_PROG       200	/* maximum number of lines in the program */
#define MAX_LINE        80	/* maximum number of characters per line  */
#define MAX_VAR         15	/* maximum length of a variable name      */
#define MAX_STRVARLEN  127	/* maximum length of a string variable    */
#define MAX_STRVAR      50	/* maximum number of string variables     */
#define MAX_INTVAR     200	/* maximum number of integer variables    */
#define MAX_STACK      200	/* maximum number of subroutine calls     */

#define NEXT_LINE      0	/* code returned by parser to indicate */
				            /* we should go to the next line. */

/* Common error messages */
#define  e_syntax     "Syntax error"
#define  e_badline    "Undefined line number"
#define  e_divzero    "Division by zero"
#define  e_type       "Type mismatch"
#define  e_nomem      "Out of memory"
#define  e_varlen     "Variable too long"


/****************************************************************/
/* Global types                                                 */

/* A boolean type (we are not in Pascal anymore) */
#ifndef boolean
#define boolean int
#define false 0
#define true 1
#endif

/* an enumeration that represents all the different tokens that can be
   in a BBB program. */
typedef enum TokenType
{
    T_VAR, T_NUM, T_STRING,

    T_EQ, T_NEQ, T_GT, T_LS, T_GE, T_LE,
    T_PLUS, T_MINUS, T_STAR, T_SLASH,
    T_LPAREN, T_RPAREN,
    T_SEMI, T_COMMA, T_COLON,

    T_ABS, T_AND, T_ELSE, T_END, T_FOR, T_GOSUB, T_GOTO, T_IF,
    T_INPUT, T_LEN, T_LET, T_MOD, T_NEXT, T_NOT, T_OR, T_PRINT,
    T_RETURN, T_STEP, T_STR, T_THEN, T_TO, T_VAL, T_WEND, T_WHILE,

    T_EOL, T_ERROR
} TokenType;

/* The result record to be passed thru the parser */
typedef struct {
    TokenType tag;
    int intResult;
    char strResult[MAX_STRVARLEN];
} Result;

/****************************************************************/
/* Global variables                                             */

extern TokenType Token;		/* current token */
extern int IntValue;		/* if current token is T_NUM, this */
				/* holds its value */
extern char StrValue[MAX_STRVARLEN]; /* if current token is T_STRING,*/
				     /* this holds its value */
extern int TotalLines;		/* Total lines in the program */
extern int CurrentIndex;	/* Index of the current line */
extern int CurrentLine;		/* Number of the current line */


/****************************************************************/
/* Function prototypes                                          */

/* Print Error message and exit */
void PrintError (char* ErrStr, int LineNum);

/* Call this procedure at the beginning of the program to initialize
  the data structures that the scanner will use. */
void InitScanner(void);

/* Call this procedure at the beginning of each line to be scanned to
  reinitialize the scanner for that line.  Pass it a string containing
  the line.  It will call NextToken to get the first token. */
void NextToken(void);

/* This procedure will get the next token in the current string.
  It places the value of the token in the Token global variable, and
  also sets the IntValue and StrValue variables as appropriate. */
void StartLine (char* Line);

/* This procedure initializes the symbol table.  Call it at the
  beginning of the program. */
void InitSymtab(void);

/* This function tests whether a variable name refers to a string
  variable or not.  It returns true if the variable is a string
  (ie, if it has a '$' on the end */
boolean IsStrVar(char* name);

/* These procedures set the value of a variable.  If the variable
  is not in the symbol table, it is created. */
void SetIntVal(char* name, int  value);
void SetStrVal(char* name, char *value);

/* These procedures lookup the value of a variable.  The value is
  returned in the value parameter.  If the variable is not in the
  symbol table, it is created and initialized to zero/empty. */
int GetIntVal(char* name);
void GetStrVal(char* name, char* value);

/* The ParseLine function takes a line of code, parses it and
  executes it.  st is the line of code.  The function returns
  which line number to execute next, or if it returns NEXT_LINE
  then the program should execute the next line. */
int ParseLine(char* st);

