-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlexer.cc
73 lines (64 loc) · 1.31 KB
/
lexer.cc
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
#include <stdlib.h>
#include <string.h>
#include "lexer.h"
#include "parser.h"
#define INTEGER() { \
tk = new Token(TK_INTEGER, ts, te); \
Parse(parser, TK_INTEGER, tk, &context); \
}
#define FLOAT() {\
tk = new Token(TK_FLOAT, ts, te); \
Parse(parser, TK_FLOAT, tk, &context); \
}
#define KEYWORD(ID) { \
tk = new Token(ID, ts, te); \
Parse(parser, ID, tk, &context); \
}
#define SYMBOL(ID) { \
tk = new Token(ID, ts, te); \
Parse(parser, ID, tk, &context); \
}
#define STRING() { \
tk = new Token(TK_STRING, ts + 1, te - 1); \
Parse(parser, TK_STRING, tk, &context); \
}
#define ID() { \
tk = new Token(TK_ID, ts, te); \
Parse(parser, TK_ID, tk, &context); \
}
#include "parser.c"
#include "lexer.c"
Token::Token(int tid, char *s, char *e) {
id = tid;
strncpy(str, s, e - s);
str[e - s] = '\0';
switch (id) {
case TK_INTEGER:
value = atoi(str);
break;
case TK_FLOAT:
fvalue = atof(str);
break;
case TK_ASC:
case TK_DESC:
value = id;
break;
default:
break;
}
}
Variant *Token::ToVariant() {
Variant *ret = NULL;
switch (id) {
case TK_INTEGER:
ret = new Int32(value);
break;
case TK_FLOAT:
ret = new Float(fvalue);
break;
default:
ret = new String(str);
break;
}
return ret;
}