defs.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #define M_PI 3.1415926535897932384626433832795
  2. #define M_E 2.71828182845904523536028747135266250
  3. // size of the symbol table
  4. #define NSYM 500
  5. // Symbolic expressions are built by connecting U structs.
  6. //
  7. // For example, (a b + c) is built like this:
  8. //
  9. // _______ _______ _______
  10. // |CONS |--->|CONS |----------------------------->|CONS |
  11. // | | | | | |
  12. // |_______| |_______| |_______|
  13. // | | |
  14. // ___v___ ___v___ _______ _______ ___v___
  15. // |ADD | |CONS |--->|CONS |--->|CONS | |SYM c |
  16. // | | | | | | | | | |
  17. // |_______| |_______| |_______| |_______| |_______|
  18. // | | |
  19. // ___v___ ___v___ ___v___
  20. // |MUL | |SYM a | |SYM b |
  21. // | | | | | |
  22. // |_______| |_______| |_______|
  23. typedef struct U {
  24. union {
  25. struct {
  26. struct U *car; // pointing down
  27. struct U *cdr; // pointing right
  28. } cons;
  29. char *printname;
  30. char *str;
  31. struct tensor *tensor;
  32. struct {
  33. unsigned int *a, *b; // rational number a over b
  34. } q;
  35. double d;
  36. } u;
  37. unsigned char k, tag;
  38. } U;
  39. // the following enum is for struct U, member k
  40. enum {
  41. CONS,
  42. NUM,
  43. DOUBLE,
  44. STR,
  45. TENSOR,
  46. SYM,
  47. };
  48. // the following enum is for indexing the symbol table
  49. enum {
  50. // standard functions first, then nil, then everything else
  51. ABS,
  52. ADD,
  53. ADJ,
  54. AND,
  55. ARCCOS,
  56. ARCCOSH,
  57. ARCSIN,
  58. ARCSINH,
  59. ARCTAN,
  60. ARCTANH,
  61. ARG,
  62. ATOMIZE,
  63. BESSELJ,
  64. BESSELY,
  65. BINDING,
  66. BINOMIAL,
  67. CEILING,
  68. CHECK,
  69. CHOOSE,
  70. CIRCEXP,
  71. CLEAR,
  72. CLOCK,
  73. COEFF,
  74. COFACTOR,
  75. CONDENSE,
  76. CONJ,
  77. CONTRACT,
  78. COS,
  79. COSH,
  80. DECOMP,
  81. DEFINT,
  82. DEGREE,
  83. DENOMINATOR,
  84. DERIVATIVE,
  85. DET,
  86. DIM,
  87. DIRAC,
  88. DISPLAY,
  89. DIVISORS,
  90. DO,
  91. DOT,
  92. DRAW,
  93. DSOLVE,
  94. EIGEN,
  95. EIGENVAL,
  96. EIGENVEC,
  97. ERF,
  98. ERFC,
  99. EVAL,
  100. EXP,
  101. EXPAND,
  102. EXPCOS,
  103. EXPSIN,
  104. FACTOR,
  105. FACTORIAL,
  106. FACTORPOLY,
  107. FILTER,
  108. FLOATF,
  109. FLOOR,
  110. FOR,
  111. GAMMA,
  112. GCD,
  113. HERMITE,
  114. HILBERT,
  115. IMAG,
  116. INDEX,
  117. INNER,
  118. INTEGRAL,
  119. INV,
  120. INVG,
  121. ISINTEGER,
  122. ISPRIME,
  123. LAGUERRE,
  124. // LAPLACE,
  125. LCM,
  126. LEADING,
  127. LEGENDRE,
  128. LOG,
  129. MAG,
  130. MOD,
  131. MULTIPLY,
  132. NOT,
  133. NROOTS,
  134. NUMBER,
  135. NUMERATOR,
  136. OPERATOR,
  137. OR,
  138. OUTER,
  139. POLAR,
  140. POWER,
  141. PRIME,
  142. PRINT,
  143. PRODUCT,
  144. QUOTE,
  145. QUOTIENT,
  146. // RANDOM, // by gbl08ma
  147. RANK,
  148. RATIONALIZE,
  149. REAL,
  150. YYRECT,
  151. ROOTS,
  152. SETQ,
  153. SGN,
  154. SIMPLIFY,
  155. SIN,
  156. SINH,
  157. SQRT,
  158. STOP,
  159. SUBST,
  160. SUM,
  161. TAN,
  162. TANH,
  163. TAYLOR,
  164. TEST,
  165. TESTEQ,
  166. TESTGE,
  167. TESTGT,
  168. TESTLE,
  169. TESTLT,
  170. TRANSPOSE,
  171. UNIT,
  172. ZERO,
  173. NIL, // nil goes here, after standard functions
  174. AUTOEXPAND,
  175. BAKE,
  176. LAST,
  177. TRACE,
  178. TTY,
  179. YYE,
  180. DRAWX, // special purpose internal symbols
  181. METAA,
  182. METAB,
  183. METAX,
  184. SECRETX,
  185. PI,
  186. SYMBOL_A,
  187. SYMBOL_B,
  188. SYMBOL_C,
  189. SYMBOL_D,
  190. SYMBOL_I,
  191. SYMBOL_J,
  192. SYMBOL_N,
  193. SYMBOL_R,
  194. SYMBOL_S,
  195. SYMBOL_T,
  196. SYMBOL_X,
  197. SYMBOL_Y,
  198. SYMBOL_Z,
  199. C1,
  200. C2,
  201. C3,
  202. C4,
  203. C5,
  204. C6,
  205. USR_SYMBOLS, // this must be last
  206. };
  207. #define E YYE
  208. // TOS cannot be arbitrarily large because the OS seg faults on deep recursion.
  209. // For example, a circular evaluation like x=x+1 can cause a seg fault.
  210. // At this setting (100,000) the evaluation stack overruns before seg fault.
  211. #define TOS 500
  212. #define MAXPRIMETAB 10000
  213. #include <stdio.h>
  214. #include <stdlib.h>
  215. #include <ctype.h>
  216. //#include <fcntl.h>
  217. #include <string.h>
  218. #include "setjmp.h"
  219. #include <math.h>
  220. #include <errno.h>
  221. //#include "asm.h"
  222. #define MAXDIM 24
  223. typedef struct tensor {
  224. int ndim;
  225. int dim[MAXDIM];
  226. int nelem;
  227. U *elem[1];
  228. } T;
  229. struct display {
  230. int h, w, n;
  231. struct {
  232. int c, x, y;
  233. } a[1]; // a for array
  234. };
  235. struct text_metric {
  236. int ascent, descent, width;
  237. };
  238. #define symbol(x) (symtab + (x))
  239. #define iscons(p) ((p)->k == CONS)
  240. #define isrational(p) ((p)->k == NUM)
  241. #define isdouble(p) ((p)->k == DOUBLE)
  242. #define isnum(p) (isrational(p) || isdouble(p))
  243. #define isstr(p) ((p)->k == STR)
  244. #define istensor(p) ((p)->k == TENSOR)
  245. #define issymbol(p) ((p)->k == SYM)
  246. #define iskeyword(p) (issymbol(p) && symnum(p) < NIL)
  247. #define car(p) (iscons(p) ? (p)->u.cons.car : symbol(NIL))
  248. #define cdr(p) (iscons(p) ? (p)->u.cons.cdr : symbol(NIL))
  249. #define caar(p) car(car(p))
  250. #define cadr(p) car(cdr(p))
  251. #define cdar(p) cdr(car(p))
  252. #define cddr(p) cdr(cdr(p))
  253. #define caadr(p) car(car(cdr(p)))
  254. #define caddr(p) car(cdr(cdr(p)))
  255. #define cadar(p) car(cdr(car(p)))
  256. #define cdadr(p) cdr(car(cdr(p)))
  257. #define cddar(p) cdr(cdr(car(p)))
  258. #define cdddr(p) cdr(cdr(cdr(p)))
  259. #define caaddr(p) car(car(cdr(cdr(p))))
  260. #define cadadr(p) car(cdr(car(cdr(p))))
  261. #define caddar(p) car(cdr(cdr(car(p))))
  262. #define cdaddr(p) cdr(car(cdr(cdr(p))))
  263. #define cadddr(p) car(cdr(cdr(cdr(p))))
  264. #define cddddr(p) cdr(cdr(cdr(cdr(p))))
  265. #define caddddr(p) car(cdr(cdr(cdr(cdr(p)))))
  266. #define cadaddr(p) car(cdr(car(cdr(cdr(p)))))
  267. #define cddaddr(p) cdr(cdr(car(cdr(cdr(p)))))
  268. #define caddadr(p) car(cdr(cdr(car(cdr(p)))))
  269. #define cdddaddr(p) cdr(cdr(cdr(car(cdr(cdr(p))))))
  270. #define caddaddr(p) car(cdr(cdr(car(cdr(cdr(p))))))
  271. #define isadd(p) (car(p) == symbol(ADD))
  272. #define ispower(p) (car(p) == symbol(POWER))
  273. #define isfactorial(p) (car(p) == symbol(FACTORIAL))
  274. #define MSIGN(p) (((int *) (p))[-2])
  275. #define MLENGTH(p) (((int *) (p))[-1])
  276. #define MZERO(p) (MLENGTH(p) == 1 && (p)[0] == 0)
  277. #define MEQUAL(p, n) (MLENGTH(p) == 1 && (long long) MSIGN(p) * (p)[0] == (n))
  278. extern int tos;
  279. extern int expanding;
  280. extern int fmt_x;
  281. extern int fmt_index;
  282. extern int fmt_level;
  283. extern int verbosing;
  284. extern const int primetab[MAXPRIMETAB];
  285. extern int esc_flag;
  286. extern int draw_flag;
  287. extern int mtotal;
  288. extern int trigmode;
  289. extern char *logbuf;
  290. extern U *symtab;
  291. extern U **binding;
  292. extern U **arglist;
  293. extern U **stack;
  294. extern U **frame;
  295. extern U *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
  296. extern U *zero, *one, *imaginaryunit;
  297. extern U *symtab;
  298. //extern char out_buf[];
  299. extern int out_count;
  300. extern int test_flag;
  301. extern jmp_buf draw_stop_return;
  302. extern int endian;
  303. #define little_endian() (*((unsigned char *) &endian))
  304. #include "prototypes.h"