2 * ginsh.l - GiNaC Interactive Shell, lexical analyzer definition
4 * This file must be processed with flex
20 #include <readline/readline.h>
21 #include <readline/history.h>
30 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
32 // Table of all used symbols
36 static int ginsh_input(char *buf, int max_size);
51 [ \t\n]+ /* skip whitespace */
52 \\$ /* skip line continuations */
53 "#".* /* skip comments starting with "#" */
54 ^"!".* system(yytext + 1); /* execute shell command */
57 Pi yylval = Pi; return T_LITERAL;
58 EulerGamma yylval = EulerGamma; return T_LITERAL;
59 Catalan yylval = Catalan; return T_LITERAL;
60 FAIL yylval = *new fail(); return T_LITERAL;
61 I yylval = I; return T_NUMBER;
62 Digits yylval = (long)Digits; return T_DIGITS;
65 quit|exit return T_QUIT;
69 inventory return T_INVENTORY;
77 ">=" return T_GREATEREQ;
79 /* last 1..3 expressions */
82 \"\"\" return T_QUOTE3;
84 /* matrix delimiters */
85 \[\[ return T_MATRIX_BEGIN;
86 \]\] return T_MATRIX_END;
92 {D}+{E} yylval = numeric(yytext); return T_NUMBER;
96 if (syms.find(yytext) == syms.end())
97 syms[yytext] = *(new symbol(yytext));
98 yylval = syms[yytext];
102 /* everything else */
112 static int line_length = 0;
113 static char *line_read = NULL;
114 static char *line_ptr;
116 // Input function that uses libreadline for interactive input
117 static int ginsh_input(char *buf, int max_size)
120 if (yy_current_buffer->yy_is_interactive) {
123 // Do we need to read a new line?
124 if (line_length == 0) {
130 // Read new line, prompt "> "
131 line_read = line_ptr = readline("> ");
139 // Add non-empty lines to history
140 line_length = strlen(line_read) + 1;
142 add_history(line_read);
144 // Reappend trailing '\n' which is stripped by readline()
145 line_read[line_length - 1] = '\n';
148 // Copy data to lex buffer
149 actual = line_length > max_size ? max_size : line_length;
150 memcpy(buf, line_ptr, actual);
151 line_length -= actual;
155 } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
156 YY_FATAL_ERROR("input in flex scanner failed");