

/*
     "under development" simple language... does not yet work.
     While writing this I found a bug - if the C code in the start
     section used an escaped double quote in a string, it's translated
     wrongly.

     input file:

var a
var x; var y; var z
var i, j
i = 1
j = 123
k = i * j

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "log.h"
#include "taccutil.h"

#include "debug.h"
#include "mmalloc.h"

/* This is the type of objects passed around as $1, $0 etc. */
#define USERTYPE char

int exit_flag = FALSE;
int printing = TRUE;
extern int _debug; /* set to true for parser diags */

char *ProgName = "language";

int verbose = FALSE;

char *copyof(char *text)
{
   char *s;
   s = stackmalloc(strlen(text)+1);
   if (s == NULL) {
      fprintf(stderr, "copyof: malloc fails - not enough room.\n");
      exit(EXIT_FAILURE);
   }
   strcpy(s, text);
   return(s);
}

void (*cur_handler)(char *s);

void ignore(char *s)
{
}

#define PLUS 1
#define MULT 2

void assign(char *s, char *n) {
  fprintf(stdout, "ASSIGN('%s', %s)\n", s, n);
}

char *add(char *l, char *r)
{
  static char str[128];
  sprintf(str, "ADD('%s', '%s')", l, r);
  fprintf(stdout, "%s\n", str);
  return(str);
}

char *mul(char *l, char *r)
{
  static char str[128];
  sprintf(str, "MUL('%s', '%s')", l, r);
  fprintf(stdout, "%s\n", str);
  return(str);
}


void declare(char *s) {
  fprintf(stdout, "DECLARE('%s')\n", s);
}


int main_1(YYTYPE *__arg);

int main_parse(YYTYPE **__arg);
static int SEEN_main_1 = FALSE;

int item_1(YYTYPE *__arg);

int item_parse(YYTYPE **__arg);
static int SEEN_item_1 = FALSE;

int statement_1(YYTYPE *__arg);

int statement_parse(YYTYPE **__arg);
static int SEEN_statement_1 = FALSE;

int statement_2(YYTYPE *__arg);
static int SEEN_statement_2 = FALSE;

int statement_3(YYTYPE *__arg);
static int SEEN_statement_3 = FALSE;

int statement_4(YYTYPE *__arg);
static int SEEN_statement_4 = FALSE;

int statement_5(YYTYPE *__arg);
static int SEEN_statement_5 = FALSE;

int multi_1(YYTYPE *__arg);

int multi_parse(YYTYPE **__arg);
static int SEEN_multi_1 = FALSE;

int declist_1(YYTYPE *__arg);

int declist_parse(YYTYPE **__arg);
static int SEEN_declist_1 = FALSE;

int declist_2(YYTYPE *__arg);
static int SEEN_declist_2 = FALSE;

int termin_1(YYTYPE *__arg);

int termin_parse(YYTYPE **__arg);
static int SEEN_termin_1 = FALSE;

int termin_2(YYTYPE *__arg);
static int SEEN_termin_2 = FALSE;

int sp_1(YYTYPE *__arg);

int sp_parse(YYTYPE **__arg);
static int SEEN_sp_1 = FALSE;

int name_1(YYTYPE *__arg);

int name_parse(YYTYPE **__arg);
static int SEEN_name_1 = FALSE;

int expr_1(YYTYPE *__arg);

int expr_parse(YYTYPE **__arg);
static int SEEN_expr_1 = FALSE;

int expr_2(YYTYPE *__arg);
static int SEEN_expr_2 = FALSE;

int term_1(YYTYPE *__arg);

int term_parse(YYTYPE **__arg);
static int SEEN_term_1 = FALSE;

int term_2(YYTYPE *__arg);
static int SEEN_term_2 = FALSE;

int number_1(YYTYPE *__arg);

int number_parse(YYTYPE **__arg);
static int SEEN_number_1 = FALSE;


extern int debug(const char *fmt, ...);
extern int debug_enter(const char *fmt, ...);
extern int debug_exit(const char *fmt, ...);

extern FILE *yyin;

int eof_parse(YYTYPE **p)
{
  int c;
  c = fgetc(yyin);
  if (c == EOF) return(TRUE);
  ungetc(c, yyin);
  return(FALSE);
}

int setdebug_parse(YYTYPE **p)
{
  _debug = TRUE;
  return(TRUE);
}

int candebug_parse(YYTYPE **p)
{
  _debug = FALSE;
  return(TRUE);
}

#define MAX_RULES 6
static patpair savepat[15] = {
   {"^", NULL},
   {"^#.*", NULL},
   {"^var", NULL},
   {"^[ \\t]*=[ \\t]*", NULL},
   {"^.*", NULL},
   {"^$", NULL},
   {"^.*;", NULL},
   {"^,", NULL},
   {"^", NULL},
   {"^;[ \\t]*", NULL},
   {"^$", NULL},
   {"^[ \\t]*", NULL},
   {"^[a-z][a-z0-9]*", NULL},
   {"^[ \\t]*([ \\t]*", NULL},
   {"^[0-9][0-9]*", NULL},
};
