-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfn.l
92 lines (87 loc) · 1.6 KB
/
fn.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
dreal ([0-9]*\.[0-9]+)
ereal ([0-9]*\.[0-9]+[eE][+-]?[0-9]+)
int [0-9]+
real ({dreal}|{ereal}|{int})
acos [Aa][Cc][Oo][Ss]
asin [Aa][Ss][Ii][Nn]
atan [Aa][Tt][Aa][Nn]
cos [Cc][Oo][Ss]
sin [Ss][Ii][Nn]
tan [Tt][Aa][Nn]
exp [Ee][Xx][Pp]
log [Ll][Oo][Gg]
log10 {log}10
sqrt [Ss][Qq][Rr][Tt]
ceil [Cc][Ee][Ii][Ll]
fabs [Ff][Aa][Bb][Ss]
floor [Ff][Ll][Oo][Oo][Rr]
function (cos|sin|tan|acos|asin|atan|exp|log|log10|sqrt|ceil|fabs|floor)
%{
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "fn.h"
#include "fn_.h"
%}
%%
{real} {
Cats(yytext);
yylval = new_mem(yytext);
return INDEX;
}
sin {
Cats(yytext);
return yylval = SIN;
}
cos {
Cats(yytext);
return yylval = COS;
}
tan {
Cats(yytext);
return yylval = TAN;
}
[a-z] {
Cats(yytext);
yylval = *yytext-'a';
return INDEX;
}
[A-Z] {
Cats(yytext);
yylval = *yytext-'A';
return INDEX;
}
("+"|"-"|"/"|"*"|"^") {
Cats(yytext);
yylval = *yytext;
return *yytext;
}
("("|")") {
Cats(yytext);
return *yytext;
}
\n return '\n';
. ;
%%
static int new_mem(char *s) {
if (memn >= MAXMEM) yyerror("new_mem");
mem[memn] = atof(s);
return memn++;
}
static int new_code(int i) {
if (coden >= MAXCODE) yyerror("new_code");
code[coden] = i;
return coden++;
}
unsigned char Catstring[MAX_CATSTRING];
void Cats(unsigned char s[])
{
strcat((char*)Catstring, (char*)s);
}
// From https://stackoverflow.com/questions/39133560/how-to-parse-a-c-string-with-bison
void set_input_string(const char* in) {
yy_scan_string(in);
}
void end_lexical_scan(void) {
yy_delete_buffer(YY_CURRENT_BUFFER);
}