home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / apl / apl.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-28  |  9.8 KB  |  485 lines

  1. /*
  2.  *    UNIX APL\11
  3.  *
  4.  *
  5.  *    UNIX APL was originally written by Ken Thompson at Bell Labs.
  6.  *    It spent some time at Yale and finally arrived at Purdue
  7.  *    University.  Since 1976 it has been modified by Jim Besemer
  8.  *    and John Bruner at the School of Electrical Engineering, Purdue,
  9.  *    under the direction of Dr. Anthony P. Reeves.  Mike Cain got a
  10.  *  copy from John and used it extensively.  Many years later, Mike
  11.  *  was motivated to clean the source up to be more or less portable.
  12.  *  Mike currently maintains this version.
  13.  */
  14.  
  15. #ifdef _SIGNAL_H
  16. #define extern
  17. #endif
  18. #include <sys/types.h>
  19. #ifdef quad
  20. //#undef quad
  21. #endif
  22.  
  23. #include <sys/stat.h>
  24. #include <time.h>
  25. #include <setjmp.h>
  26.  
  27. /*
  28.  * Configuration information
  29.  *
  30.  * APL2            generate single-precision version
  31.  */
  32.  
  33. #undef  APL2
  34. #define    NFDS    20        /* Number of available fd's */
  35. #define    MAXEOT    8        /* Number of input EOT's before panic */
  36.  
  37. /*
  38.  * Temp file names
  39.  */
  40.  
  41. #define WSFILE    ws_file        /* work space file */
  42.  
  43. /*
  44.  * Magic Numbers
  45.  */
  46.  
  47. #define MRANK    8
  48. #define CANBS    300
  49. #define STKS    500
  50. #define NLS        200
  51. #define NAMS    40
  52. #define OBJS    500
  53. #define MAXLAB    30
  54.  
  55. #ifdef APL2
  56. #define    data    float
  57. #else
  58. #define    data    double
  59. #endif
  60.  
  61. /*
  62.  * derived constants
  63.  */
  64.  
  65. #define    SDAT    sizeof(data)
  66. #define    SINT    sizeof(int)
  67.  
  68. /*
  69.  * Interpreter Op Codes
  70.  */
  71.  
  72. #define EOF        (-1)
  73. #define EOL        0
  74.  
  75. #define ADD        1
  76. #define PLUS    2
  77. #define SUB        3
  78. #define MINUS    4
  79. #define MUL        5
  80. #define SGN        6
  81. #define DIV        7
  82. #define RECIP    8
  83. #define MOD        9
  84. #define ABS        10
  85. #define MIN        11
  86. #define FLOOR    12
  87. #define MAX        13
  88. #define CEIL    14
  89. #define PWR        15
  90. #define EXP        16
  91. #define LOG        17
  92. #define LOGE    18
  93. #define CIR        19
  94. #define PI        20
  95. #define COMB    21
  96. #define FAC        22
  97.  
  98. #define DEAL    23
  99. #define RAND    24
  100. #define DRHO    25
  101. #define MRHO    26
  102. #define DIOT    27
  103. #define MIOT    28
  104. #define ROT0    29
  105. #define REV0    30
  106. #define DTRN    31
  107. #define MTRN    32
  108. #define DIBM    33
  109. #define MIBM    34
  110.  
  111. #define GDU        35
  112. #define GDUK    36
  113. #define GDD        37
  114. #define GDDK    38
  115. #define EXD        39
  116. #define SCAN    40
  117. #define EXDK    41
  118. #define SCANK    42
  119. #define IPROD    43
  120. #define OPROD    44
  121. #define QUAD    45
  122. #define QQUAD    46
  123. #define BRAN0    47
  124. #define BRAN    48
  125. #define DDOM    49
  126. #define MDOM    50
  127.  
  128. #define COM        51
  129. #define RED        52
  130. #define COMK    53
  131. #define REDK    54
  132. #define ROT        55
  133. #define REV        56
  134. #define ROTK    57
  135. #define REVK    58
  136. #define CAT        59
  137. #define RAV        60
  138. #define CATK    61
  139. #define RAVK    62
  140.  
  141. #define PRINT    63
  142. #define QUOT    64
  143. #define ELID    65
  144. #define CQUAD    66
  145. #define COMNT    67
  146. #define INDEX    68
  147. #define HPRINT    69
  148.  
  149. #define LT        71
  150. #define LE        72
  151. #define GT        73
  152. #define GE        74
  153. #define EQ        75
  154. #define NE        76
  155. #define AND        77
  156. #define OR        78
  157. #define NAND    79
  158. #define NOR        80
  159. #define NOT        81
  160. #define EPS        82
  161. #define MEPS    83
  162. #define REP        84
  163. #define TAKE    85
  164. #define DROP    86
  165. #define ASGN    88
  166. #define IMMED    89
  167.  
  168.  
  169. #define NAME    90
  170. #define CONST    91
  171. #define FUN        92
  172. #define ARG1    93
  173. #define ARG2    94
  174. #define AUTO    95
  175. #define REST    96
  176.  
  177. #define COM0    97
  178. #define RED0    98
  179. #define EXD0    99
  180. #define SCAN0    100
  181. #define BASE    101
  182. #define MENC    102    /*    monadic    encode    */
  183. #define LABEL    103    /* statement label */
  184. #define PSI        104    /* PSI input character */
  185. #define PSI1    105    /* PSI monadic half */
  186. #define PSI2    106    /* PSI dyadic half */
  187. #define ISP        107    /* ISP input code */
  188. #define ISP1    108    /* ISP monadic half */
  189. #define ISP2    109    /* ISP dyadic half */
  190. #define QWID    110    /* quad fn1 */
  191. #define QFUZZ    111
  192. #define QRUN    112
  193. #define QFORK    113
  194. #define QWAIT    114
  195. #define QEXEC    115
  196. #define FDEF    116
  197. #define QEXIT    117
  198. #define QPIPE    118
  199. #define QCHDIR    119
  200. #define QOPEN    120
  201. #define QCLOSE    121
  202. #define QREAD    122
  203. #define QWRITE    123
  204. #define QCREAT    124
  205. #define QSEEK    125
  206. #define QUNLNK    126
  207. #define QRD        127
  208. #define QDUP    128
  209. #define QAP        129
  210. #define QKILL    130
  211. #define QCRP    131
  212. #define DFMT    132
  213. #define MFMT    133
  214. #define QNC        134
  215. #define NILRET    135
  216. #define XQUAD    136
  217. #define SICLR    137
  218. #define SICLR0    138
  219. #define RVAL    139
  220. #define QSIGNL    140
  221. #define    QFLOAT    141        /* Float character string to data */
  222. #define    QNL        142        /* Produce namelist */
  223.  
  224. /*
  225.  * Immediate sub-op codes
  226.  */
  227.  
  228. #define    CLEAR    1
  229. #define    DIGITS    2
  230. #define    EDIT    3
  231. #define    ERASE    4
  232. #define    FNS        5
  233. #define    FUZZ    6
  234. #define    READ    7
  235. #define    ORIGIN    8
  236. #define    VARS    9
  237. #define    WIDTH    10
  238. #define    DEBUG    11
  239. #define OFF        12
  240. #define LOAD    13
  241. #define SAVE    14
  242. #define COPY    15
  243. #define CONTIN    16
  244. #define LIB        17
  245. #define DROPC    18
  246. #define VSAVE    19
  247. #define SCRIPT    20
  248. #define EDITF    21
  249. #define TRACE    22
  250. #define UNTRACE    23
  251. #define WRITE    24
  252. #define RESET    25
  253. #define SICOM    26
  254. #define CODE    27
  255. #define    DEL        28
  256. #define    SHELL    29
  257. #define    LIST    30
  258. #define    PRWS    31
  259. #define MEMORY    32
  260. #define PSAVE    33
  261. #define PLOAD    34
  262.  
  263. struct chrstrct
  264. {
  265.     char c[2];
  266. };
  267.  
  268. union uci
  269. {
  270.     char    cv[sizeof(int)];    /* character array */
  271.     unsigned i;                    /* unsigned integer value */
  272. };
  273.  
  274. extern data    zero;
  275. extern data    one;
  276. extern data    pi;
  277. extern data    maxexp;        /* the largest value such that exp(maxexp) is defined */
  278. extern data    datum;
  279. data    getdat();
  280. extern int        funtrace;    /* function trace enabled */
  281. extern int        labgen;        /* label processing being done */
  282. extern int        apl_term;    /* flag set if apl terminal mapping req'd */
  283. extern jmp_buf    gbl_env;    /* Used for setexit/reset */
  284. extern jmp_buf reset_env;
  285.  
  286. /*
  287.  * Several unrelated values, which appear
  288.  * together in the header of an apl workspace file.
  289.  */
  290.  
  291. extern struct
  292. {
  293.     double    fuzz;
  294.     int    iorg;
  295.     int    digits;
  296.     int    width;
  297. } thread;
  298.  
  299. /*
  300.  * Data types
  301.  * Each new type should be accomodated for 
  302.  * in dealloc [a0.c]
  303.  */
  304.  
  305. #define    DA        1
  306. #define    CH        2
  307. #define    LV        3
  308. #define    QD        4
  309. #define    QQ        5
  310. #define    IN        6
  311. #define    EL        7
  312. #define    NF        8
  313. #define    MF        9
  314. #define    DF        10
  315. #define    QC        11
  316. #define    QV        12    /* quad variables */
  317. #define DU        13    /* dummy -- causes fetch error except on print */
  318. #define QX        14    /* latent expr. quad "Llx" */
  319. #define LBL        15    /* locked label value */
  320. #define    NTYPES    16    /* number of defined types */
  321.  
  322. /*
  323.  * This is a descriptor for apl data, allocated by "newdat".
  324.  * The actual data starts at item.dim[item.rank], and thus
  325.  * &item.dim[item.rank] should always == item.datap.
  326.  * See the comment in "newdat" (a0.c) about "dim".
  327.  *
  328.  * A null item is a vector(!), and is rank==1, size==0.
  329.  *
  330.  * the stack is the operand stack, and sp is the pointer to the
  331.  * top of  the stack.
  332.  */
  333.  
  334. extern struct item
  335. {
  336.     int rank;
  337.     int type;
  338.     int    size;
  339.     int    index;
  340.     data    *datap;
  341.     int    dim[MRANK];
  342. } *stack[STKS], **sp;
  343.  
  344. /*
  345.  * variable/fn (and file name) descriptor block.
  346.  * contains useful information about all LVals.
  347.  * Also kludged up to handle file names (only nlist.namep 
  348.  * is then used.)
  349.  *
  350.  * For fns, nlist.itemp is an array of pointers to character
  351.  * strings which are the compiled code for a line of the fn.
  352.  * (Itemp == 0) means that the fn has not yet been compiled .
  353.  * nlist.itemp[0] == the number of lines in the fn, and
  354.  * nlist.itemp[1] == the function startup code, and
  355.  * nlist.itemp[max] == the close down shop code.
  356.  */
  357.  
  358. extern struct nlist
  359. {
  360.     int use;
  361.     int type;
  362.     struct item  *itemp;
  363.     char    *namep;
  364.     int    label;
  365. } nlist[NLS];
  366.  
  367. /*
  368.  * This is the structure used to implement the
  369.  * APL state indicator.
  370.  *
  371.  * The structure is allocated dynamically in ex_fun (ai.c),
  372.  * but not explicitly.   Ex_fun declares a single, local
  373.  * structure (allocated by C, itself), and links it to
  374.  * previous instances of the structure.  SI is used for
  375.  * two basic things:
  376.  *
  377.  *    1) error traceback (Including ")SI" stuff).
  378.  *    2) Restoration of the global variable environment
  379.  *       (or any other, pending environment).
  380.  *
  381.  * The global variable "gsip" is a pointer to the
  382.  * head of a chain of these structures, one for each
  383.  * instance of an activated function.  (Gsip == 0) implies
  384.  * an empty list, (gsip->sip == 0) implies the end of the list,
  385.  * and (gsip->np == 0) implies a state indicator seperator.
  386.  * (A new function was evoked with an old one pending.)
  387.  *
  388.  * Note that "gsip->funlc" is the same as the old global
  389.  * variable "funlc", and 
  390.  *
  391.  *    (gsip && gsip->sip ? gsip->sip->funlc : 0)
  392.  *
  393.  * is the value of the old global, "ibeam36".
  394.  */
  395.  
  396. extern struct si {
  397.     int    suspended;            /* fn is suspended <=1, pending <= 0 */
  398.     struct si *sip;            /* previous fn activation */
  399.     struct nlist *np;        /* current fn vital stats. */
  400.     int funlc;                /* current fn current line number */
  401.     struct item **oldsp;    /* top of operand stack upon fn entry */
  402.     char *oldpcp;            /* execution string upon fn entry */
  403.     jmp_buf    env;            /* for restoration of local fn activation record */
  404. } *gsip;
  405.  
  406. /*
  407.  * exop[i] is the address of the i'th action routine.
  408.  * Because of a "symbol table overflow" problem with C,
  409.  * the table was moved from a1.c to it's own at.c
  410.  */
  411.  
  412. #ifndef _SIGNAL_H
  413. extern int    (*exop[])();
  414. #endif
  415.  
  416. double    floor();
  417. double    fabs();
  418. double    ceil();
  419. double    log();
  420. double    sin();
  421. double    cos();
  422. double    atan();
  423. double    atan2();
  424. double    sqrt();
  425. double    exp();
  426. double    gamma();
  427. double    ltod();
  428. char    *rline();
  429. char    *alloc();
  430. char    *compile();
  431. struct nlist *nlook();
  432. struct item *fetch(), *fetch1(), *fetch2(), *extend();
  433. struct item *newdat(), *dupdat();
  434.  
  435. extern int        integ;
  436. #ifndef _SIGNAL_H
  437. extern int        signgam;
  438. #endif
  439. extern int        column;
  440. extern int        intflg;
  441. extern int        echoflg;
  442. extern int        offexit;        /* if != 0, require ")off" to exit */
  443. extern int        prwsflg;
  444. extern int        ifile;
  445. extern int        wfile;
  446. extern int        debug;
  447. extern int        ttystat[3];
  448. extern long    startTime;
  449. extern char    *pcp;            /* global copy of arg to exec */
  450. extern int     rowsz;
  451. extern int        mencflg;
  452. extern int        aftrace;
  453. extern char    *mencptr;
  454. extern int        oldlb[MAXLAB];
  455. extern int        pt;
  456. extern int        syze;
  457. extern int        pas1;
  458. extern int        ibeam36;
  459. extern int        protofile;
  460. extern int        lastop;            /* last (current) operator exec'ed */
  461. extern char    *scr_file;        /* scratch file name */
  462. extern char    *ws_file;        /* apl workspace file */
  463. extern int     lineNumber;
  464.  
  465.  
  466. extern struct
  467. {
  468.     char    rank;
  469.     char    type;
  470.     int        size;
  471.     int        dimk;
  472.     int        delk;
  473.     int        dim[MRANK];
  474.     int        del[MRANK];
  475.     int        idx[MRANK];
  476. } idx;
  477.  
  478. #define    setexit() setjmp(gbl_env)        /* "setexit" equivalent */
  479. #define    reset()    longjmp(gbl_env, 0)        /* "reset" equivalent */
  480. #define equal(a,b) (0 == strcmp(a,b))    /*    character string equality  */
  481. #ifdef _SIGNAL_H
  482. #undef extern
  483. #define extern extern
  484. #endif
  485.