/* MasC interpreter/compiler
   (Master Assembly Script Container)
   written by Jared Bruni
   
   After reading Writing Compilers and Interpreters A approach using C++ by Ronald Mak
   and the Dragon Book
   code completly re-written but using concepts teached in this book
*/

// backend header file (icode execution)
// if it makes you feel alright


#ifndef _backend_H_
#define _backend_H_
#include<memory.h>

#include "stack.h"
#include "symboltable.h"
#include "error.h"
#include "globals.h"
#include "textin.h"
#ifdef _WIN32
#include "parser.h"
#endif

static const int NOT_FOUND = 256;
static const char End = 55;


static char *syntax_str[] = { "add","and","begin","call","cmp","dec","div","end","exit","in","inc","je","jg",
"jge","jl","jle", "jne","jmp","loop","loopn0","mas","mov", "mul","not","or","pop","print","proc", "push", "reset","ret","scat",
"sloop", "sub", "var", "var&","var$","var!","xor", "{", "}" , "=", "<<",">>",NULL,NULL
};

enum token_valuez {
  ADD, AND, BEGIN,CALL,CMP,DEC, DIV, END,EXIT,IN, INC,JE,JG,JGE,JL,JLE,JNE,JMP,LOOP,LOOPN0,MAS, MOV, MUL, NOT, OR,POP,PRINT,_PROC,
  PUSH,RESET,RET,SCAT,SLOOP,SUB, VAR, VAR_INT, VAR_STR, VAR_FLOAT, XOR, OB, CB ,EQ, OSTREAM,ISTREAM
};

extern char outfile[256];
extern int finished;

struct Token {
       char code;
       char *text;
       int line;
       float value;
       enum var_type vtype;
	   char num_text[25];
	   int callback;
};


struct BackEnd {
       char obj_name[256];
       int incp,inc_len,ince;    
       struct Stack st;
	   struct Stack ist;
	   struct Stack the_stack;
       struct StrStack str_st;
       struct symTbl g_stable;
	   int reg[8];
};

struct convertHolder {
	struct BackEnd *bend;
	struct Token *tokens;
	int rt_val;
};

extern int (__stdcall *callback)(const char *_PROC_name, struct BackEnd *bend, struct Token *ptr_tok);
extern int i_p;
extern int finished;
extern int bend_offset;

void executeIcode(int,struct Token *tokenz, struct BackEnd *bend);
float calculateValue(struct BackEnd *bend,char code);
void freeIcode(struct Token *tokens, struct BackEnd *bend);
struct convertHolder convertIcode(const char *fname,struct Token *tokens, struct BackEnd *bend);
void printTokens(struct Token *tokens, int tlen);
void CMP_compare(struct Token *tokens, struct BackEnd *bend, struct symNode *node_ptr);
void CMP_comparenptr(struct symNode *node_ptr, struct symNode *snode_ptr, struct BackEnd *bend);
void CMP_strcomparenptr(struct symNode *node_ptr, struct symNode *snode_ptr, struct BackEnd *bend);
char *print_FixString(char *str);
char *stream_Data(char *str, struct Token *tokenz, struct BackEnd *bend, int *cip);
#endif


