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