[0e1846] | 1 | %{ |
---|
| 2 | /**************************************** |
---|
| 3 | * Computer Algebra System SINGULAR * |
---|
| 4 | ****************************************/ |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #include <stdlib.h> |
---|
| 8 | #include <ctype.h> |
---|
| 9 | |
---|
[a4b31c] | 10 | #include "kernel/mod2.h" |
---|
| 11 | #include "omalloc/omalloc.h" |
---|
| 12 | #include "Singular/tok.h" |
---|
| 13 | #include "Singular/stype.h" |
---|
| 14 | #include "Singular/ipshell.h" |
---|
| 15 | #include "Singular/fevoices.h" |
---|
| 16 | #include "kernel/oswrapper/feread.h" |
---|
[0e1846] | 17 | |
---|
[057e93c] | 18 | int feReadLine(char* b, int l); |
---|
[c232af] | 19 | #define ALLOC(a) omAlloc((a)) |
---|
[b1dfaf] | 20 | #ifndef NEW_FLEX |
---|
| 21 | #endif /* NEW_LEX */ |
---|
[64d729] | 22 | int blocknest = 0; |
---|
[0e1846] | 23 | extern char * yytext; |
---|
| 24 | //extern unsigned char * yytext; |
---|
| 25 | extern int yyleng; |
---|
[530bdb] | 26 | extern int inerror; |
---|
[0e1846] | 27 | |
---|
[ec7aac] | 28 | // this is to shadow the malloc/realloc |
---|
| 29 | // used by yy_flex_malloc/yy_flex_realloc |
---|
| 30 | // so that we can mark stuff as static |
---|
| 31 | static void* my_malloc(size_t size) |
---|
| 32 | { |
---|
| 33 | void* addr = omAlloc(size); |
---|
| 34 | omMarkAsStaticAddr(addr); |
---|
| 35 | return addr; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | static void* my_realloc(void* addr, size_t size) |
---|
| 39 | { |
---|
| 40 | void* new_addr = omRealloc(addr, size); |
---|
| 41 | omMarkAsStaticAddr(new_addr); |
---|
| 42 | return new_addr; |
---|
| 43 | } |
---|
| 44 | static void my_free(void* addr) |
---|
| 45 | { |
---|
| 46 | omFree(addr); |
---|
| 47 | } |
---|
| 48 | #undef malloc |
---|
| 49 | #define malloc my_malloc |
---|
[a3bc95e] | 50 | #undef realloc |
---|
[ec7aac] | 51 | #define realloc my_realloc |
---|
| 52 | #undef free |
---|
| 53 | #define free my_free |
---|
[0e1846] | 54 | static char * dupyytext() |
---|
| 55 | { |
---|
[c232af] | 56 | char* s; |
---|
[0e1846] | 57 | if (yyleng>0) yytext[yyleng-1] = '\0'; |
---|
[c232af] | 58 | s = omStrDup((char *)yytext); |
---|
| 59 | omMarkAsStaticAddr(s); |
---|
| 60 | return s; |
---|
[0e1846] | 61 | } |
---|
| 62 | |
---|
| 63 | static char * dupyytextNL() |
---|
| 64 | { |
---|
| 65 | int i = yyleng;//strlen((char *)yytext); |
---|
[c232af] | 66 | char * rc = (char*)omAlloc( 3 + i ); |
---|
| 67 | omMarkAsStaticAddr(rc); |
---|
[0e1846] | 68 | if (i>0) |
---|
| 69 | { |
---|
[e9ad8a6] | 70 | strncpy( rc, (char *)yytext, i-1 ); |
---|
[0e1846] | 71 | } |
---|
| 72 | else |
---|
[e9ad8a6] | 73 | { |
---|
[0e1846] | 74 | i++; |
---|
[e9ad8a6] | 75 | } |
---|
[0e1846] | 76 | rc[i-1] = '\n'; |
---|
| 77 | rc[i] = '\n'; |
---|
| 78 | rc[i+1] = '\0'; |
---|
| 79 | return rc; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | #undef YY_DECL |
---|
| 83 | #define YY_DECL int yylex(YYSTYPE* lvalp) |
---|
| 84 | |
---|
| 85 | #undef yywrap |
---|
[057e93c] | 86 | extern "C" { |
---|
[0e1846] | 87 | int yywrap() { return exitVoice(); } |
---|
[057e93c] | 88 | } |
---|
[0e1846] | 89 | |
---|
| 90 | #undef YY_INPUT |
---|
| 91 | #define YY_INPUT(buf,result,max_size) \ |
---|
[057e93c] | 92 | result = feReadLine( (char *) (buf), (max_size) ) |
---|
[0e1846] | 93 | |
---|
| 94 | #undef YY_USER_ACTION |
---|
| 95 | #define YY_USER_ACTION \ |
---|
[057e93c] | 96 | if ((inerror==1)&&(*yytext>=' '))\ |
---|
| 97 | { Print(" skipping text from `%s`",yytext);inerror=2; } |
---|
[0e1846] | 98 | |
---|
| 99 | %} |
---|
| 100 | |
---|
| 101 | digit [0-9] |
---|
| 102 | letter [@a-zA-Z\'] |
---|
| 103 | integer {digit}+ |
---|
| 104 | monom {letter}+{digit}* |
---|
| 105 | rgvars ({digit}+[/])*{digit}+{monom}+ |
---|
[b7b08c] | 106 | realnum {digit}*"."{digit}+("e"[+-]{digit}+)? |
---|
[0e1846] | 107 | name ({letter}({letter}*{digit}*_*)*|_) |
---|
| 108 | parname # |
---|
| 109 | |
---|
| 110 | /* %start START */ |
---|
[991705] | 111 | %option always-interactive |
---|
[0e1846] | 112 | |
---|
| 113 | %x string |
---|
| 114 | %x block |
---|
[6ae4f5] | 115 | %x blockstr |
---|
[0e1846] | 116 | %x brace |
---|
| 117 | %x bracestr |
---|
| 118 | %x bracket |
---|
| 119 | %x asstring |
---|
| 120 | |
---|
| 121 | %% |
---|
[aad4ca4] | 122 | \/\/[^\n]* |
---|
| 123 | ^[ \r\t\n]*#![^\n]* |
---|
[e9ad8a6] | 124 | "/*" { |
---|
| 125 | yy_noeof=noeof_comment; |
---|
| 126 | loop |
---|
| 127 | { |
---|
| 128 | register int c; |
---|
| 129 | while ( (c = yyinput()) != '*' && c != EOF ); |
---|
| 130 | if ( c == '*' ) |
---|
| 131 | { |
---|
| 132 | while ( (c = yyinput()) == '*' ); |
---|
| 133 | if ( c == '/' ) break; /* found the end */ |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
| 137 | break; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | yy_noeof=0; |
---|
| 141 | } |
---|
[ff2c077] | 142 | while { prompt_char='.'; |
---|
| 143 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
[0e1846] | 144 | return WHILE_CMD;} |
---|
[ff2c077] | 145 | for { prompt_char='.'; |
---|
| 146 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
[0e1846] | 147 | return FOR_CMD;} |
---|
| 148 | |
---|
[cc94b0a] | 149 | ("help"|"?")[ \t\n]* { yy_noeof = noeof_asstring; |
---|
[0e1846] | 150 | BEGIN(asstring); |
---|
| 151 | return HELP_CMD; |
---|
| 152 | } |
---|
| 153 | |
---|
[c04b94] | 154 | example[ \t\n]* { yy_noeof = noeof_asstring; |
---|
| 155 | BEGIN(asstring); |
---|
| 156 | return EXAMPLE_CMD; |
---|
| 157 | } |
---|
| 158 | |
---|
[0e1846] | 159 | proc[ \t]+{name}[ \t]*\( { |
---|
| 160 | char c; char *cp; |
---|
[c232af] | 161 | lvalp->name = omStrDup(iiProcName((char *)yytext,c,cp)); |
---|
[cc94b0a] | 162 | yy_noeof = noeof_procname; |
---|
[0e1846] | 163 | blocknest = 1; |
---|
| 164 | BEGIN(brace); |
---|
| 165 | return PROC_DEF; |
---|
| 166 | } |
---|
| 167 | <asstring>[^;\n]+ { |
---|
[c232af] | 168 | lvalp->name = omStrDup((char *)yytext); |
---|
[cc94b0a] | 169 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 170 | return STRINGTOK; |
---|
| 171 | } |
---|
| 172 | <asstring>; { |
---|
[cc94b0a] | 173 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 174 | return *yytext; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | <brace>"\"" { |
---|
[cc94b0a] | 178 | yy_noeof = noeof_string; |
---|
[0e1846] | 179 | BEGIN(bracestr); |
---|
| 180 | yymore(); |
---|
| 181 | } |
---|
| 182 | <brace>"(" { if (blocknest++) yymore(); } |
---|
| 183 | <brace>[^;\(\)] { if (blocknest) yymore(); } |
---|
| 184 | <brace>";" { |
---|
| 185 | if (blocknest) |
---|
| 186 | { |
---|
| 187 | lvalp->name = dupyytext(); |
---|
| 188 | return STRINGTOK; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | <brace>")" { |
---|
| 192 | if (--blocknest <= 0) |
---|
| 193 | { |
---|
[cc94b0a] | 194 | yy_noeof = 0; |
---|
[0e1846] | 195 | BEGIN(INITIAL); |
---|
| 196 | lvalp->name = dupyytext(); |
---|
| 197 | return STRINGTOK; |
---|
| 198 | } |
---|
| 199 | yymore(); |
---|
| 200 | } |
---|
| 201 | <bracestr>"\"" { |
---|
[cc94b0a] | 202 | yy_noeof = noeof_brace; |
---|
[0e1846] | 203 | BEGIN(brace); |
---|
| 204 | yymore(); |
---|
| 205 | } |
---|
| 206 | <bracestr>[^\"] { yymore(); } |
---|
| 207 | <bracket>"(" { return '('; } |
---|
| 208 | <bracket>"," { return ','; } |
---|
| 209 | <bracket>[ \t\n]* { ; } |
---|
| 210 | <bracket>[^\(\), \t\n]* { |
---|
[c232af] | 211 | lvalp->name = omStrDup((char *)yytext); |
---|
[0e1846] | 212 | return STRINGTOK; |
---|
| 213 | } |
---|
| 214 | <bracket>\"[^\"]*\" { |
---|
[c232af] | 215 | lvalp->name = omStrDup((char *)yytext); |
---|
[0e1846] | 216 | return STRINGTOK; |
---|
| 217 | } |
---|
| 218 | <bracket>")" { |
---|
[cc94b0a] | 219 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 220 | return ')'; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | "{" { |
---|
[cc94b0a] | 224 | yy_blocklineno = yylineno; |
---|
[0e1846] | 225 | blocknest = 1; |
---|
[cc94b0a] | 226 | yy_noeof = noeof_block; |
---|
[0e1846] | 227 | BEGIN(block); |
---|
| 228 | } |
---|
[6ae4f5] | 229 | <block>"\"" { |
---|
[cc94b0a] | 230 | yy_noeof = noeof_string; |
---|
[6ae4f5] | 231 | BEGIN(blockstr); |
---|
| 232 | yymore(); |
---|
| 233 | } |
---|
| 234 | <blockstr>[^\"] { yymore(); } |
---|
| 235 | <blockstr>"\\\\" { yymore(); } |
---|
| 236 | <blockstr>"\\\"" { yymore(); } |
---|
| 237 | <blockstr>"\"" { |
---|
[cc94b0a] | 238 | yy_noeof = noeof_block; |
---|
[6ae4f5] | 239 | BEGIN(block); |
---|
| 240 | yymore(); |
---|
| 241 | } |
---|
| 242 | <block>[^\{\}\"]* { yymore(); } |
---|
[c656484] | 243 | <block>\/\/[^\n]* { yymore(); } |
---|
[0e1846] | 244 | <block>"{" { blocknest++; yymore(); } |
---|
| 245 | <block>"}" { |
---|
| 246 | if (--blocknest <= 0) |
---|
| 247 | { |
---|
| 248 | BEGIN(INITIAL); |
---|
[cc94b0a] | 249 | yy_noeof = 0; |
---|
[0e1846] | 250 | lvalp->name = dupyytextNL(); |
---|
| 251 | return BLOCKTOK; |
---|
| 252 | } |
---|
| 253 | yymore(); |
---|
| 254 | } |
---|
[cc94b0a] | 255 | "\"" { BEGIN(string); yy_noeof = noeof_string;} |
---|
[0e1846] | 256 | ~ { return SYS_BREAK; } |
---|
| 257 | <string>[^\"] { yymore(); } |
---|
| 258 | <string>"\\\\" { yymore(); } |
---|
| 259 | <string>"\\\"" { yymore(); } |
---|
| 260 | <string>"\"" { |
---|
| 261 | char * s; |
---|
[cc94b0a] | 262 | yy_noeof = 0; |
---|
[0e1846] | 263 | BEGIN(INITIAL); |
---|
| 264 | s = lvalp->name = dupyytext(); |
---|
| 265 | while (*yytext) |
---|
| 266 | { |
---|
| 267 | if (*yytext == '\\') yytext++; |
---|
| 268 | *s++ = *yytext++; |
---|
| 269 | } |
---|
| 270 | *s++ = *yytext++; |
---|
| 271 | return STRINGTOK; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | [ \t\r\n] /* skip whitespace */ |
---|
| 275 | ".." { return DOTDOT; } |
---|
[057e93c] | 276 | "::" { return COLONCOLON; } |
---|
[0e1846] | 277 | "--" { return MINUSMINUS; } |
---|
| 278 | "++" { return PLUSPLUS ; } |
---|
| 279 | "==" { return EQUAL_EQUAL; } |
---|
[7b3094] | 280 | "&&" { lvalp->i='&'; return LOGIC_OP; } |
---|
| 281 | "||" { lvalp->i='|'; return LOGIC_OP; } |
---|
| 282 | "<=" { lvalp->i=LE; return COMP_OP; } |
---|
| 283 | ">=" { lvalp->i=GE; return COMP_OP; } |
---|
[0e1846] | 284 | "!" { return NOT; } |
---|
| 285 | "!=" { return NOTEQUAL; } |
---|
| 286 | "<>" { return NOTEQUAL; } |
---|
| 287 | "**" { return '^'; } |
---|
[6be074] | 288 | "->" { return ARROW; } |
---|
[0e1846] | 289 | \\ { return '\\'; } |
---|
| 290 | newline { |
---|
[c232af] | 291 | lvalp->name = omStrDup("\n"); |
---|
[0e1846] | 292 | return STRINGTOK; |
---|
| 293 | } |
---|
| 294 | {integer} { |
---|
| 295 | lvalp->name = (char *)yytext; |
---|
[db5523] | 296 | return INT_CONST; |
---|
[0e1846] | 297 | } |
---|
| 298 | {integer}\/{integer} { |
---|
[6bb498] | 299 | lvalp->name = (char *)yytext; |
---|
[0e1846] | 300 | return RINGVAR; |
---|
| 301 | } |
---|
[b7b08c] | 302 | \$ { |
---|
[e9ad8a6] | 303 | m2_end(-1); |
---|
[0e1846] | 304 | } |
---|
[de25e5] | 305 | (quit|exit)[ \t\n]*; { |
---|
[0e1846] | 306 | #ifdef MM_STAT |
---|
| 307 | mmStat(-500); |
---|
| 308 | #endif |
---|
[c232af] | 309 | #ifdef OM_TRACK |
---|
[7fe9e13] | 310 | #ifndef SING_NDEBUG |
---|
[de25e5] | 311 | omPrintUsedTrackAddrs(stdout, 10); |
---|
[0e1846] | 312 | #endif |
---|
[d33552] | 313 | #endif |
---|
[e9ad8a6] | 314 | m2_end(0); |
---|
[0e1846] | 315 | } |
---|
| 316 | |
---|
| 317 | {rgvars}|{realnum} { |
---|
[6bb498] | 318 | lvalp->name = (char *)yytext; |
---|
[0e1846] | 319 | return RINGVAR; |
---|
| 320 | } |
---|
[7b3094] | 321 | [0-9]+\."e"[+-][0-9]+ { |
---|
[b7b08c] | 322 | lvalp->name = (char *)yytext; |
---|
| 323 | return RINGVAR; |
---|
| 324 | } |
---|
| 325 | [0-9]+\./[^\.] { |
---|
| 326 | lvalp->name = (char *)yytext; |
---|
| 327 | return RINGVAR; |
---|
| 328 | } |
---|
[7b3094] | 329 | |
---|
[0e1846] | 330 | ({parname}|{name}) { |
---|
| 331 | /* {name} */ |
---|
| 332 | int rc=0; |
---|
| 333 | if (yytext[strlen((char *)yytext)-1] == '\n') |
---|
| 334 | { |
---|
| 335 | yytext[strlen((char *)yytext)-1] = '\0'; |
---|
| 336 | } |
---|
| 337 | if (yyleng > 1) |
---|
| 338 | { |
---|
| 339 | rc = IsCmd((char *)yytext,lvalp->i); |
---|
| 340 | if (rc) return rc; |
---|
| 341 | } |
---|
[c232af] | 342 | lvalp->name = omStrDup((char *)yytext); |
---|
[0e1846] | 343 | return UNKNOWN_IDENT; |
---|
| 344 | } |
---|
| 345 | |
---|
[057e93c] | 346 | . { |
---|
[0e1846] | 347 | /*if (*yytext == '\n') REJECT;*/ |
---|
[3b1a83c] | 348 | register char ch= *yytext; |
---|
[7b3094] | 349 | lvalp->i = ch; |
---|
[3b1a83c] | 350 | switch(ch) |
---|
| 351 | { |
---|
| 352 | /* case '&': */ |
---|
| 353 | case '|': |
---|
[7b3094] | 354 | return LOGIC_OP; |
---|
[3b1a83c] | 355 | /* case '/': */ |
---|
| 356 | case '%': |
---|
| 357 | case '*': |
---|
[7b3094] | 358 | return MULDIV_OP; |
---|
[3b1a83c] | 359 | /* case '<': */ |
---|
| 360 | case '>': |
---|
| 361 | return COMP_OP; |
---|
| 362 | default: |
---|
| 363 | break; |
---|
| 364 | } |
---|
[7b3094] | 365 | return ch; |
---|
[0e1846] | 366 | } |
---|
| 367 | %% |
---|
| 368 | |
---|
| 369 | void * myynewbuffer() |
---|
| 370 | { |
---|
[b1dfaf] | 371 | void * oldb = YY_CURRENT_BUFFER; |
---|
[057e93c] | 372 | yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE)); |
---|
[0e1846] | 373 | return oldb; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | void myyoldbuffer(void * oldb) |
---|
| 377 | { |
---|
[b1dfaf] | 378 | yy_delete_buffer(YY_CURRENT_BUFFER); |
---|
[0e1846] | 379 | yy_switch_to_buffer((YY_BUFFER_STATE)oldb); |
---|
[057e93c] | 380 | //yy_flush_buffer((YY_BUFFER_STATE)oldb); |
---|
[0e1846] | 381 | } |
---|
[8e884e] | 382 | |
---|
[310cf4] | 383 | void myychangebuffer() |
---|
| 384 | { |
---|
| 385 | yy_flush_buffer((YY_BUFFER_STATE)YY_CURRENT_BUFFER); |
---|
| 386 | yy_delete_buffer(YY_CURRENT_BUFFER); |
---|
| 387 | yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE)); |
---|
| 388 | } |
---|
| 389 | |
---|
[8e884e] | 390 | void my_yy_flush() { YY_FLUSH_BUFFER;BEGIN(0); } |
---|