Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / Taumath.cpp
blob6fc16fb721e49d5955aca6530b6c94ac6d9e0146
1 #include <stdio.h>
2 #include <setjmp.h>
4 #include "defs.h"
5 extern "C"{
6 #include "console.h"
7 #include "fxlib.h"
8 #include "syscalls.h"
9 #include "tex/TeX.h"
10 #include "memory.h"
11 #include "config.h"
12 #define EXPR_BUF_SIZE 256
14 #define USER_FUNCTIONS "\\\\fls0\\USER.eig"
15 #define USER_FUNCTIONS_MAX_LENGTH 200
17 extern U ** mem;
18 extern unsigned int **free_stack;
20 int
21 initialize_tuamath()
23 // modified by anderain
24 free_stack = (unsigned int**) calloc(500,sizeof(unsigned int*));
25 mem = (U**) calloc(100,sizeof(U*));
26 stack = (U**) calloc(TOS,sizeof(U*));
27 symtab = (U*) calloc(NSYM,sizeof(U));
28 binding = (U**) calloc(NSYM,sizeof(U*));
29 arglist = (U**) calloc(NSYM,sizeof(U*));
30 logbuf = (char*) calloc(256,1);
33 void TeX_init(void)
35 Txt_Init(FONT_SYSTEM);
38 void TeX_quit(void)
40 Txt_Quit();
43 const unsigned char *Setup[]={
44 "logab(a,b)=log(b)/log(a)",
45 "log10(x)=log(x)/log(10)",
46 "ln(x)=log(x)",
47 "cis(x)=cos(x)+i*sin(x)",
48 "cot(x)=1/tan(x)",
49 "coth(x)=cosh(x)/sinh(x)",
50 "arccot(x)=arctan(1/x)",
51 "arccoth(x)=arctanh(1/x)",
52 "sec(x)=1/cos(x)",
53 "sech(x)=1/cosh(x)",
54 "arcsec(x)=arccos(1/x)",
55 "arcsech(x)=arccosh(1/x)",
56 "csc(x)=1/sin(x)",
57 "csch(x)=1/sinh(x)",
58 "arccsc(x)=arcsin(1/x)",
59 "arccsch(x)=arcsinh(1/x)",
60 "npr(n,r)=(n!)/(n-r)!",
61 "ncr(n,r)=n!/(r!(n-r)!)",
62 "xor(x,y)=or(and(x,not(y)),and(not(x),y))",
63 "derive(f)=d(f)",
64 NULL
67 int AddIn_main(int isAppli, unsigned short OptionNum)
69 unsigned int key;
70 unsigned char *expr;
71 unsigned char *user_functions;
73 int i = 0, j = 0;
75 unsigned char *line = (unsigned char *)malloc(USER_FUNCTIONS_MAX_LENGTH*sizeof(unsigned char));
77 initialize_tuamath();
79 TeX_init();
80 Console_Init();
81 Console_Disp();
82 load_config();
84 // initialize failed ?
85 if (!(line && free_stack && mem && stack && symtab && binding && arglist && logbuf))
86 return 0;
88 while(Setup[i] != NULL) {
89 run((char *)Setup[i++]);
92 i = 0;
94 user_functions = (unsigned char*)memory_load(USER_FUNCTIONS);
96 // Just extracting each line of the file containing user functions and running them one by one
98 if(user_functions != NULL) {
99 int line_count = 0;
100 do {
101 j = 0;
102 line_count++;
104 memset(line, '\0', USER_FUNCTIONS_MAX_LENGTH * sizeof(unsigned char));
106 while (i < strlen((char*)user_functions) && j < USER_FUNCTIONS_MAX_LENGTH
107 && user_functions[i] != '\n' && user_functions[i] != '\r'
108 && user_functions[i] != '\0') {
109 line[j++] = user_functions[i++];
112 run((char *)line);
114 // Printing the error message if needed
116 if(Console_GetEditLine()[0] != '\0') {
117 char line_number[15] = ""; // That should be enough...
119 // Printing the actual error message
120 Console_NewLine(LINE_TYPE_OUTPUT,1);
122 // More details on where the error is
123 Console_Output("\xE6\x92 USER.eig line : ");
124 sprintf(line_number, "%d", line_count);
125 strcat(line_number, " ");
126 Console_Output((unsigned char*)line_number);
127 Console_NewLine(LINE_TYPE_INPUT,1);
128 Console_Disp();
130 i+=2;
131 } while(line[0] != '\0');
133 free(user_functions);
135 Console_Output((unsigned char*)" ");
136 Console_NewLine(LINE_TYPE_OUTPUT,1);
137 Console_Disp();
140 free(line);
142 while(1)
144 if((expr=Console_GetLine())==NULL) stop("memory error");
145 run((char *)expr);
146 //print_mem_info();
147 Console_NewLine(LINE_TYPE_OUTPUT,1);
148 //GetKey(&key);
149 Console_Disp();
152 TeX_quit();
153 for(;;)GetKey(&key);
154 return 1;
157 #pragma section _BR_Size
158 unsigned long BR_Size;
159 #pragma section
161 #pragma section _TOP
162 int InitializeSystem(int isAppli, unsigned short OptionNum)
164 return INIT_ADDIN_APPLICATION(isAppli, OptionNum);
166 #pragma section