-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_parse.c
More file actions
99 lines (76 loc) · 1.95 KB
/
js_parse.c
File metadata and controls
99 lines (76 loc) · 1.95 KB
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
#include "js.h"
// find node's root node
firstNode *findFirstNode(Node *table, uint32_t slot) {
uint32_t start = 0;
while(start < slot) {
firstNode *fn = (firstNode *)(table + start);
start += fn->moduleSize;
if( start > slot )
return fn;
}
return (firstNode *)table;
}
uint32_t newNode (parseData *pd, nodeType type, uint32_t size, bool zero) {
uint32_t blks = (size + sizeof(Node) - 1)/sizeof(Node);
uint32_t addr = pd->tableNext, newSize[1];
Node *node;
if( blks + pd->tableNext >= pd->tableSize ) {
if( pd->tableSize )
pd->tableSize *= 2;
else
pd->tableSize = 4090;
*newSize = pd->tableSize * sizeof(Node);
if (pd->table)
pd->table = js_realloc (pd->table, newSize, false);
else
pd->table = js_alloc (*newSize, false);
pd->tableSize = *newSize / sizeof(Node);
}
node = pd->table + pd->tableNext;
if (zero)
memset (node, 0, size);
node->bits = type;
node->lineNo = pd->lineNo;
pd->tableNext += blks;
return addr;
}
char convLit(char quoted) {
switch(quoted) {
case 0x0a: return 0;
case 0x0d: return 0;
case 'f': return 0x0c;
case 'r': return 0x0d;
case 'n': return 0x0a;
case 't': return 0x09;
case 'b': return 0x08;
}
return quoted;
}
uint32_t newStrNode (parseData *pd, char *text, uint32_t size) {
uint32_t len = 0, addr, off = 0, max, idx;
stringNode *sn;
char c;
for(max = 1; max < size; max++)
switch(text[max]) {
case '\\': continue;
case 0x0a: pd->lineNo++; continue;
case 0x0d: continue;
default: len++; continue;
}
max--; // discard trailing quote mark
len--; // discard trailing quote mark
addr = newNode(pd, node_string, sizeof(stringNode) + len + 1, false);
sn = (stringNode *)(pd->table + addr);
sn->str.len = len;
for(idx = 1; idx < max && off < len; idx++)
switch((c = text[idx])) {
case '\\':
if ((sn->str.val[off] = convLit(text[++idx])))
off++;
continue;
case 0x0a: continue;
case 0x0d: continue;
default: sn->str.val[off++] = c;
}
return addr;
}