-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcompiler.l
51 lines (44 loc) · 1.16 KB
/
watcompiler.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
%option noyywrap nodefault yylineno
%{
#include "watcompiler.h"
#include "watcompiler.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
static int64_t strToHex(char* s);
extern int errorflag;
%}
%%
[ \t] { }
[0-9]+d { yylval.num = (int64_t)atoi(yytext); return (NUM); }
[0-9a-fA-F]+[hH] { yylval.num = strToHex(yytext); return (NUM);}
"_"[A-Z][A-Z] { yylval.sym = lookup(yytext); return (VAR); }
["].*["] { yylval.str = strdup(yytext); return (TEXT); }
"=>" { return (TK_ASSIGN); }
"(" |
")" |
"{" |
"}" |
"+" |
"-" |
"*" |
"/" |
"%" |
"^" |
";" |
"," { return (yytext[0]); }
"prints" { return (CMD_PS); }
"printh" { return (CMD_PH); }
"printd" { return (CMD_PD); }
"ife" { return (TK_IFE); }
"von" { return (TK_VON); }
\n { }
. { errorflag = 1; yyerror("Mystery character %c\n", *yytext); }
%%
static int64_t strToHex(char* s) {
int len = strlen(s);
s[len-1] = 0;
return (int64_t)( strtol(s, NULL, 16) );
}