/*                            BASIC_H
                              =======

        This file contains declarations for interfacing at the 
        SUPERBASIC level.    It has defines functions which are
        directly equivalent to SUPERBASIC keywords, and also
        defines the SUPERBASIC variables area.
*/
#ifndef _BASIC_H
#define _BASIC_H

#ifndef _SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef __STDC__
#define _P_(params) params
#else
#define _P_(params) ()
#endif

/**
 *
 *  The following are the offsets within the SuperBasic Variables Area
 *  (NOTE.  This area is also defined alter in terms of C structures )
 *
**/
#define BV_START    0               /* Start of Pointers */
#define BV_BFBAS    0x00            /* LONG:  Buffer Base */
#define BV_BUFP     0x04            /* LONG:  Buffer Runniung Pointer */
#define BV_TKBAS    0x08            /* LONG:  Token List */
#define BV_TKP      0x0c            /* LONG: */
#define BV_PFBAS    0x10            /* LONG:  Program File */
#define BV_PFP      0x14            /* LONG: */
#define BV_NTBAS    0x18            /* LONG:  Name Table base */
#define BV_NTP      0x1c            /* LONG: */
#define BV_NLBAS    0x20            /* LONG: Name List */
#define BV_NLP      0x24            /* LONG: */
#define BV_VVBAS    0x28            /* LONG: Variable Values */
#define BV_VVP      0x2c            /* LONG: */
#define BV_CHBAS    0x30            /* LONG: Channel Table */
#define BV_CHP      0x34            /* LONG: */
#define BV_RTBAS    0x38            /* LONG: Return Table */
#define BV_RTP      0x3c            /* LONG: */
#define BV_LNBAS    0x40            /* LONG: Line Number Table */
#define BV_LNP      0x44            /* LONG: */

#define BV_CHANGE   0x48            /* Change of direction marker */

#define BV_BTP      0x48            /* LONG: Backtrack stack during parsing */
#define BV_BTBAS    0x4c            /* LONG: */
#define BV_TGP      0x50            /* LONG: Temporary graph stack during parsing */
#define BV_TGBAS    0x54            /* LONG: */
#define BV_RIP      0x58            /* LONG: Arithmetic Stack */
#define BV_RIBAS    0x5c            /* LONG: */
#define BV_SSP      0x60            /* LONG: System Stack */
#define BV_SSBAS    0x64            /* LONG: */

#define BV_EBDPT    0x64            /* End of Pointers */

#define BV_LINUM    0x68            /* WORD: Current Line Number */
#define BV_LENGTH   0x6a            /* WORD: Current Length */
#define BV_STMNT    0x6c            /* BYTE: Current Statement on line */
#define BV_CONT     0x6d            /* BYTE: continue 0x80 or stop processing */
#define BV_INLIN    0x6e            /* BYTE: Processing in-line clause or not */
                                    /*       loop(1), other(0xff), not (0)    */
#define BV_SING     0x6f            /* BYTE: Single line execution on (0xff) off (0x0) */
#define BV_INDEX    0x70            /* WORD: Name table row of last in-line loop index read */
#define BV_VVFREE   0x72            /* LONG: First free space in variable value table */
#define BV_SSSAV    0x76            /* LONG: Saved SP for out/mem to go back to */

#define BV_RAND     0x80            /* LONG: Random Number */
#define BV_COMCH    0x84            /* LONG: Command Channel */

#define BV_NXLIN    0x88            /* WORD: Which line number to start from */
#define BV_NXSTM    0x8a            /* BYTE: Which statement to start after */
#define BV_COMLN    0x8b            /* BYTE: Command line saved (0xff) or not (0x0) */
#define BV_STOPN    0x8c            /* WORD: Which stop number set */
#define BV_EDIT     0x8e            /* BYTE: Program has been editied (0xff) or not (0x0) */
#define BV_BRK      0x8f            /* BYTE: There has been a brreak (0x0) or not (0x80) */
#define BV_UNRVL    0x90            /* BYTE: Need to unravel (0xff) or not (0x0) */
#define BV_CNSTM    0x91            /* BYTE: Statement to continue from */
#define BV_CNLNO    0x92            /* WORD: line to CONTINUE from */
#define BV_DALNO    0x94            /* WORD: current DATA line number */
#define BV_DASTM    0x96            /* BYTE: current DATA statment number */
#define BV_DAITM    0x97            /* BYTE: next DATA item to read */

#define BV_CNIND    0x98            /* WORD: in-line loop index to CONTINUE with */
#define BV_CBINL    0x9a            /* BYTE: in-line loop flag for CONTINUE */
#define BV_LSANY    0x9b            /* BYTE: whether checking list (0xFF) or not (0x0) */
#define BV_LSBEF    0x9c            /* WORD: Invisible top line */
#define BV_LSBAS    0x9e            /* WORD: bottom line in window */
#define BV_LSAFT    0xa0            /* WORD: Invisible bottom line */
#define BV_LENLN    0xa2            /* WORD: length of window line */
#define BV_MAXLN    0xa4            /* WORD: max number of window lines */
#define BV_TOTLN    0xa6            /* WORD: numvber of window lines so far */

#define BV_AUTO     0xaa            /* BYTE: Whether AUTO/EDIT on (0xff) or off (0x0) */
#define BV_PRINT    0xab            /* BYTE: print from prtok (0xff) or leave in buffer (0x0) */
#define BV_EDLIN    0xac            /* WORD: line number to edit next */
#define BV_EDINC    0xae            /* WORD: increment on edit range */
#define BV_TKPOS    0xb0            /* LONG: pos of A4 in tklist on entry to PROC */
#define BV_PTEMP    0xb4            /* LONG: temp pointer for GO_PROX */
#define BV_UNDO     0xb8            /* BYTE: Undort stack IMMEDIATELY the redo procedure */

#define BV_ARROW    0xb9            /* BYTE: down (0xff) or up (0x01) or no (0x0) arrow */

#define BV_LSFIL    0xba            /* WORD: fill window when relisting at least to here */
#define BV_WRLNO    0xbc            /* WORD: when error line number */
#define BV_WRSTM    0xbe            /* BYTE: when error statement */
#define BV_WRINL    0xbf            /* BYTE: when error in-line (0xff) or not (0x0) */
#define BV_WHERR    0xc0            /* BYTE: processing when error (0x80) or not (0x0) */
#define BV_ERROR    0xc2            /* LONG: last error code */
#define BV_ERLIN    0xc6            /* WORD: line number of last error */
#define BV_WVNUM    0xc8            /* WORD: number of watched (WHEN) variables */
#define BV_WVBAS    0xca            /* LONG: base of WHEN variable table wrt VVBAS */

#define BV_END      0x100           /* Top of Basic Variables area */
/*
   Structure and #defs defining the NameTable
*/

struct _NT_ENT   {                     /* name table entry                       */
   char  name_type;                    /* name type                              */
   char  var_type;                     /* variable type                          */
   short name_ptr;                     /* offset of name in the namelist         */
   long  value_ptr;                    /* pointer to associated infomation       */
};

#define  NT_unset    0                 /* nametype - unset                       */
#define  NT_stack    1                 /* nametype - value on arithmetic stack   */
#define  NT_var      2                 /* nametype - simple variable             */
#define  NT_array    3                 /* nametype - array                       */
#define  NT_proc_sb  4                 /* nametype - superbasic procedure        */
#define  NT_fun_sb   5                 /* nametype - superbasic function         */
#define  NT_rep      6                 /* nametype - REP loop index              */
#define  NT_for      7                 /* nametype - FOR loop index              */
#define  NT_proc_mc  8                 /* nametype - machine code procedure      */
#define  NT_fun_mc   9                 /* nametype - machine code function       */

#define  VT_null     0                 /* variabletype - none                    */
#define  VT_string   1                 /* variabletype - string                  */
#define  VT_float    2                 /* variabletype - floating point          */
#define  VT_int      3                 /* variabletype - interger                */

/*
    Structure define the name list
    (N.B.  These are actually variable length entries)
 */
struct _NL_ENT {
    char    length;
    char    name[1];
    };
/**
 *
 *   The following is the structure of the SuperBasic area as a C structure
 *   NOTE.>  This is not yet complete, and some of the pointers should really
 *          be pointers to structures defined earlier in thsi file
 *
**/
struct BV_AREA {
    char *  bv_bfbas;
    char *  bv_bfp;
    char *  bv_tkbas;
    char *  bv_tkp;
    char *  bv_pfbas;
    char *  bv_pfp;
    struct _NT_ENT * bv_ntbas;
    struct _NT_ENT * bv_ntp;
    struct _NL_ENT * bv_nlbas;
    struct _NL_ENT * bv_nlp;
    char *  bv_vvbas;
    char *  bv_vvp;
    char *  bv_chbas;
    char *  bv_chp;
    char *  bv_rtbas;
    char *  bv_rtp;
    char *  bv_lnbas;
    char *  bv_lnp;
    };
/*
 *  Function declarations that can be defined as macros
 *  since they map onto existing routines in the standard
 *  LIBC_A library.
 */
#define AT(chanid,row,col)          sd_pos(chanid,(timeout_t)(-1),col,row)
#define BORDER(chanid,width,colour) sd_bordr(chanid,(timeout_t)(-1),colour,width)
#define CSIZE(chanid,width,height)  sd_setsz(chanid,(timeout_t)(-1),width,height)
#define CURSOR(chanid,x,y)          sd_pixp(chanid,(timeout_t)(-1),x,y) 
#define CURSOR_OFF(chanid)          sd_curs(chanid,(timeout_t)(-1))
#define CURSOR_ON(chanid)           sd_cure(chanid,(timeout_t)(-1)) 
#define INK(chanid,colour)          sd_setin(chanid,(timeout_t)(-1),colour)
#define KEYROW(row)                 keyrow(row)
#define MODE(val)                   mt_dmode(mode,(timeout_t)(-1))
#define OVER(chanid,mode)           sd_setmd(chanid,(timeout_t)(-1),mode)
#define PAPER(chanid,colour)        sd_setpa(chaind,(timeout_t)(-1),colour)
#define POSITION(chanid)            fs_pos(chan_id,0,1)
#define SET_POSITION(chanid,pos)    fs_pos(chanid,pos,0)
#define STRIP(chanid,colour)        sd_setst(chanid,(timeout_t)(-1),colour)
#define TAB(chanid,col)             sd_tab(chanid,(timeout_t)(-1),col) 
#define UNDER(chanid,mode)          sd_setul(chanid,(timeout_t)(-1),mode)

/*
 *  Functions that need supporting code, and are thus part of
 *  the libbasic_a library.
 */
void  ADATE     _P_((long));
void  BLOCK     _P_((long, int, int, int, int, int));
int   CLS       _P_((long, int));
long  DATE      _P_((void));
#ifdef STRFUNC
char *DATE$     _P_((long, char *));
char *DAY$      _P_((long, char *));
int   INKEY$    _P_((long, int));
#endif
int   PAN       _P_((long, int, int));
void  REPORT    _P_((long, int));
int   SCROLL    _P_((long, int, int));
void  SDATE     _P_((long));
int   WINDOW    _P_((long, int ,int, int, int));

#undef _P_

#endif /* _BASIC_H */

