-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.l
144 lines (128 loc) · 2.71 KB
/
lex.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
%{
/*
* This file is licensed under BSD 3-Clause.
* All license information is available in the included COPYING file.
*/
/*
* lex.l
*
* Author : Finn Rayment <[email protected]>
* Date created : 21/07/2022
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "incl.h"
#include "y.tab.h"
extern void yyerror(char *, ...);
extern int yyrunning;
char errmsg[256];
char *buf;
YY_BUFFER_STATE strbuf;
int argv_redirect;
%}
%option noyywrap
%option yylineno
%%
/* whitespace and control characters */
<<EOF>> { yyrunning = 0; return END; }
[eE][xX][iI][tT] { yyrunning = 0; return EXIT; }
[qQ][uU][iI][tT] { yyrunning = 0; return EXIT; }
[ \t\r\f] { /* ignore whitespace */ }
"\n" { return END; }
";" { return LINEEND; }
"(" { return LPAREN; }
")" { return RPAREN; }
"," { return COMMA; }
/* symbols */
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULTIPLY; }
"/" { return DIVIDE; }
"^" { return EXPONENT; }
"%" { return MODULO; }
"!" { return EXPONENTIAL; }
/* numbers */
[0-9]+ { yylval = atof(yytext); return NUMBER; }
[0-9]*\.[0-9]+ { yylval = atof(yytext); return NUMBER; }
[0-9]+\.[0-9]* { yylval = atof(yytext); return NUMBER; }
/* words */
[pP][iI] { return PI; }
[eE] { return E; }
[sS][iI][nN] { return SIN; }
[cC][oO][sS] { return COS; }
[tT][aA][nN] { return TAN; }
[aA][sS][iI][nN] { return ASIN; }
[aA][cC][oO][sS] { return ACOS; }
[aA][tT][aA][nN] { return ATAN; }
[aA][tT][aA][nN]2 { return ATAN2; }
[sS][qQ][rR][tT] { return SQRT; }
[cC][bB][rR][tT] { return CBRT; }
[lL][oO][gG]2 { return LOG2; }
[lL][oO][gG]10 { return LOG10; }
[lL][oO][gG] { return LOG; }
[lL][nN] { return LN; }
[dD][eE][gG] { return DEG; }
[rR][aA][dD] { return RAD; }
/* other */
. { yyerror("unrecognized symbol \"%s\"", yytext); }
%%
void
yyerror(char *fmt,
...)
{
va_list va;
va_start(va, fmt);
fprintf(stderr, "Error at line %d: ", yylineno);
vfprintf(stderr, fmt, va);
fputc('\n', stderr);
va_end(va);
exit(EXIT_FAILURE);
}
void
switch_to_buffer(char *buf)
{
strbuf = yy_scan_string(buf);
yy_switch_to_buffer(strbuf);
}
void
clean_buffer(void)
{
yy_delete_buffer(strbuf);
}
char *
argv_parse_redirect(int argc,
char **argv)
{
int i;
size_t arglen;
for (i = optind, arglen = 0; i < argc; ++i)
arglen += 1 + strlen(*(argv+i));
buf = malloc(arglen);
if (!buf)
{
perror("malloc");
abort();
}
*buf = 0;
for (i = optind; i < argc; ++i)
{
if (i > optind)
strcat(buf, " ");
strcat(buf, *(argv+i));
}
switch_to_buffer(buf);
argv_redirect = 1;
return buf;
}
void
argv_parse_cleanup(void)
{
if (argv_redirect)
{
argv_redirect = 0;
clean_buffer();
}
free(buf);
}