/*
 * BUILDING YOUR OWN C INTERPRETER
 * by Herbert Schildt
 * From Dr. Dobbs Journal - August 1989.
 * Adapted to the Amiga by Jeff Lydiatt.
 */


/****** Internal Library Functions *******/

/* Add more of your own, here. */

/*#include "conio.h"*/  /* if your compiler does not 
                       support this  header file, 
                       remove it */
#include "stdio.h"
#include "stdlib.h"

extern char *prog; /* points to current location in program */
extern char token[80]; /* holds string representation of token */
extern char token_type; /* contains type of token */
extern char tok; /* holds the internal representation of token */

enum tok_types {DELIMITER, IDENTIFIER, NUMBER, COMMAND, STRING,
	        QUOTE, VARIABLE, BLOCK, FUNCTION};
/* These are the constants used to call sntx_err() when
   a syntax error occurs.  Add more if you like.
   NOTE: SYNTAX is a generic error message used when
   nothing else seems appropriate.
*/
enum error_msg 
     {SYNTAX, UNBAL_PARENS, NO_EXP, EQUALS_EXPECTED,
      NOT_VAR, PARAM_ERR, SEMI_EXPECTED,
      UNBAL_BRACES, FUNC_UNDEF, TYPE_EXPECTED,
      NEST_FUNC, RET_NOCALL, PAREN_EXPECTED,
      WHILE_EXPECTED, QUOTE_EXPECTED, NOT_STRING,
      TOO_MANY_LVARS};
int get_token(void);
void sntx_err(int error), eval_exp(int *result);
void putback(void);
/* Get a character from console.  (Use getchar()) if
   your compiler does not support getche().) */
call_getche()
{
  char ch;
  ch = getchar(); /* was ch = getche() */
  while(*prog!=')') prog++;
  prog++;   /* advance to end of line */
  return ch;
}
/* Put a character to the display.  (Use putchar()
   if your compiler does not support putch().) */
call_putch()
{
  int value;
  eval_exp(&value);
  printf("%c", value);
  fflush(stdout);
  return value;
}
/* Call puts(). */
call_puts(void)
{
  get_token();
  if(*token!='(') sntx_err(PAREN_EXPECTED);
  get_token();
  if(token_type!=QUOTE) sntx_err(QUOTE_EXPECTED);
  puts(token);
  fflush(stdout);
  get_token();
  if(*token!=')') sntx_err(PAREN_EXPECTED);
  get_token();
  if(*token!=';') sntx_err(SEMI_EXPECTED);
  putback();
  return 0;
}
/* A built-in console output function. */
int print(void)
{
  int i;
  get_token();
  if(*token!='(')  sntx_err(PAREN_EXPECTED);
  get_token();
  if(token_type==QUOTE) { /* output a string */
    printf("%s ", token);
    fflush(stdout);
  }
  else {  /* output a number */
   putback();
   eval_exp(&i);   
   printf("%d ", i);
   fflush(stdout);
  }
  get_token();
  if(*token!=')') sntx_err(PAREN_EXPECTED);
  get_token();
  if(*token!=';') sntx_err(SEMI_EXPECTED);
  putback();
  return 0;
}
/* Read an integer from the keyboard. */
getnum(void)
{
  char s[80];
  gets(s);
  while(*prog!=')') prog++;
  prog++;  /* advance to end of line */
  return atoi(s);
}
