-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.h
103 lines (88 loc) · 2.42 KB
/
lexer.h
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: lexer.h
* Author: root
*
* Created on June 15, 2017, 6:54 AM
*/
#ifndef LEXER_H
#define LEXER_H
/* Reading */
smplx_val* smplx_val_read_num(mpc_ast_t* t) {
errno = 0;
double x = atof(t->contents);
return errno != ERANGE ? smplx_val_num(x) : smplx_val_err("Invalid Number.");
}
smplx_val* smplx_val_read_str(mpc_ast_t* t) {
/* Cut off the final quote character */
t->contents[strlen(t->contents) - 1] = '\0';
/* Copy the string missing out the first quote character */
char* unescaped = malloc(strlen(t->contents + 1) + 1);
strcpy(unescaped, t->contents + 1);
/* Pass through the unescape function */
unescaped = mpcf_unescape(unescaped);
/* Construct a new smplx_val using the string */
smplx_val* str = smplx_val_str(unescaped);
/* Free the string and return */
free(unescaped);
return str;
}
/**
*
* Takes the parsed code from mpc and starts evaluating it according to its content
*
* - Number
* - Symbolic Expression
* - Qouted Expression
*
* @param t
* @return
*/
smplx_val* smplx_val_read(mpc_ast_t* t) {
if (strstr(t->tag, "number")) {
return smplx_val_read_num(t);
}
if (strstr(t->tag, "string")) {
return smplx_val_read_str(t);
}
if (strstr(t->tag, "symbol")) {
return smplx_val_sym(t->contents);
}
smplx_val* x = NULL;
if (strcmp(t->tag, ">") == 0) {
x = smplx_val_sexpr();
}
if (strstr(t->tag, "sexpr")) {
x = smplx_val_sexpr();
}
if (strstr(t->tag, "qexpr")) {
x = smplx_val_qexpr();
}
for (int i = 0; i < t->children_num; i++) {
if (strcmp(t->children[i]->contents, "(") == 0) {
continue;
}
if (strcmp(t->children[i]->contents, ")") == 0) {
continue;
}
if (strcmp(t->children[i]->contents, "]") == 0) {
continue;
}
if (strcmp(t->children[i]->contents, "[") == 0) {
continue;
}
if (strcmp(t->children[i]->tag, "regex") == 0) {
continue;
}
if (strstr(t->children[i]->tag, "comment")) {
continue;
}
x = smplx_val_add(x, smplx_val_read(t->children[i]));
}
return x;
}
#endif /* LEXER_H */