⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yabasic.h

📁 一个小巧的BASIC解释器的源代码很小可它的确做到了
💻 H
📖 第 1 页 / 共 2 页
字号:
/*
     YABASIC --- a tiny integrated Basic Compiler/Interpreter

     header-file
     
     this Program is subject to the GNU General Public License;
     see the file yabasic.c for details.
*/

#define YABASIC_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
#include <math.h>
#include <time.h>

#ifdef WINDOWS
#include <windows.h>
#include <io.h>
#endif

#ifdef UNIX
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <unistd.h>
#endif

#include <signal.h>
#include <ctype.h>


/*-------------- variables needed in all files ------------------------ */

extern char *string;
extern int errorlevel;
extern int end_of_file;
extern struct command *lastinput; /* last input-command */
extern int interactive; /* true, if commands come from stdin */	


/*-------------------------- defs and undefs ------------------------*/

/* undef symbols */
#undef FATAL
#undef ERROR
#undef WARNING
#undef NOTE
#undef DIAGNOSTIC

#if !defined(TRUE)
#define TRUE (1==1)
#endif

#ifndef FALSE
#define FALSE (1!=1)
#endif

/* I've been told, that some symbols are missing under SunOs ... */
#ifndef RAND_MAX
#define RAND_MAX 32767
#endif
#ifndef FOPEN_MAX
#define FOPEN_MAX 20
#endif

/* length of buffers for system() and input */
#define SYSBUFFLEN 100
#define INBUFFLEN 1000

/* ---------------------- enum types ------------------------------- */

enum error {  /* error levels  */
  FATAL,ERROR,WARNING,NOTE,DIAGNOSTIC
};

enum functions { /* functions in yabasic (sorted by number of arguments) */
  fRAN2,fDATE,fTIME,fZEROARGS,fINKEY,fSIN,fASIN,fCOS,fACOS,fTAN,
  fATAN,fSYSTEM,fSYSTEM2,fPEEK,fPEEK2,fEXP,fLOG,fLEN,fSTR,
  fSQRT,fSQR,fFRAC,fABS,fSIG,fRAN,fINT,fVAL,fASC,fUPPER,fLOWER,
  fLTRIM,fRTRIM,fTRIM,fCHR,fXMAP,fYMAP,fONEARGS,fATAN2,fLEFT,
  fRIGHT,fINSTR,fSTR2,fMOD,fMIN,fMAX,fTWOARGS,fMID
};

enum arraymode { /* type of array access */
  CALLARRAY,ASSIGNARRAY,CALLSTRINGARRAY,ASSIGNSTRINGARRAY,GETSTRINGPOINTER
};

enum cmd_type { /* type of command */
  cFIRST_COMMAND, /* no command, just marks start of list */
  
  cLABEL,cGOTO,cQGOTO,cGOSUB,cQGOSUB,cRETURN,  /* flow control */
  cEND,cDECIDE,cSKIPPER,cNOP,cFINDNOP,cEXCEPTION,
  cSKIPONCE,cRESETSKIPONCE,
  
  cDIM,cFUNCTION,cDOARRAY,                        /* everything with "()" */
  
  cSTARTFOR,cFORCHECK,cFORINCREMENT,              /* for for-loops */

  cDBLADD,cDBLMIN,cDBLMUL,cDBLDIV,cDBLPOW,            /* double operations */
  cNEGATE,cPUSHDBLSYM,cPOPDBLSYM,cPUSHDBL,
  
  cPOKE,cSWAP,cDUPLICATE,                           /* internals */
  
  cAND,cOR,cNOT,cLT,cGT,cLE,cGE,cEQ,cNE,            /* comparisons */
  cSTREQ,cSTRNE,cSTRLT,cSTRLE,cSTRGT,cSTRGE,
  
  cPUSHSTRSYM,cPOPSTRSYM,cPUSHSTR,cCONCAT,           /* string operations */
  cPUSHSTRPTR,cCHANGESTRING,cTOKEN,
  
  cPRINT,cREAD,cRESTORE,cQRESTORE,cONESTRING,         /* i/o operations */
  cREADDATA,cDATA,cOPEN,cCLOSE,cSWITCH,cTESTEOF,
  cWAIT,cBELL,cMOVE,cCLEARSCR,cREVERT,cCHKPROMPT,
  
  cOPENWIN,cDOT,cLINE,cCIRCLE,cTEXT,cCLOSEWIN,cCLEARWIN,   /* grafics */
  cOPENPRN,cCLOSEPRN,cMAKEMAP,cTICK,cMAP,cMOVEORIGIN,cRECT,
  cMARKER,
  
  cLAST_COMMAND /* no command, just marks end of list */
};

enum stackentries { /* different types of stackentries */
  stGOTO,stSTRING,stNUMBER,stLABEL,stRETADD,stFREE
};

enum symbols { /* different types of symbols */
  sySTRING,syNUMBER,syFREE,syARRAY
};

enum states { /* current state of program */
  HATCHED,INITIALIZED,COMPILING,RUNNING,FINISHED
};

/* ------------- global types ---------------- */ 

struct stackentry { /* one element on stack */ 
  int type;     /* contents of entry */
  struct stackentry *next;
  struct stackentry *prev;
  void *pointer; /* multiuse ptr */
  double value;  /* double value, only one of pointer or value is used */
};

struct symbol {   /* general symbol; either variable, string, label or call */
  int type;
  struct symbol *next;
  char *name;
  void *pointer;   /* general pointer */
  char *args;      /* used to store number of arguments for functions/array */
  double value;
};

struct command { /* one interpreter command */
  int type;    /* type of command */
  struct command *next;  /* link to next command */
  void *pointer;       /* pointer to command specific data */
  int args;  /* number of arguments for function/array call */
             /* or stream number for open/close             */
  char tag;  /* letter to pass some information */
  int line; /* line this command has been created for */
};

struct array { /* data structure for arrays */
  int bounds[10];  /* index boundaries */
  int dimension; /* dimension of array */
  int total; /* product of all dimensions */
  int dimed;      /* Flag to mark if array has been dimed already */
  void *pointer; /* contents of array */
  char type;  /* decide between string- ('s') and double-Arrays ('d') */
};

struct buff_chain { /* buffer chain for system-input */
  char buff[SYSBUFFLEN]; /* content of buffer */
  struct buff_chain *next; /* next buffer in chain */
};


/* ------------- function prototypes for ... ---------------- */
/* ------------- main program and auxiliary functions ---------------- */
int main(int,char **);
int yyparse(void); /* yyparse is supplied by BISON */
struct symbol *get_sym(char *,int,int); /* find and/or add a symbol */
struct command *add_command(int); /* get room for new command */
void parse_arguments(int,char *argv[]); /* parse arguments from command line */
void initialize(void); /* give correct values to pointers etc ... */
void reset(void); /* reset pointers to their initial values */
void signal_handler(int);  /* handle various signals */
void create_exception(int); /* create command 'exception' */
void exception(struct command *); /* change handling of exceptions */
void error(int,char *); /* reports an error and possibly exits */
char *my_strdup(char *); /* my own version of strdup */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -