-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantics.c
133 lines (112 loc) · 3.18 KB
/
semantics.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "lval.h"
#include "lenv.h"
#include "mpc.h"
#include "semantics.h"
#include "expressions.h"
#include <stdio.h>
#include <stdlib.h>
lval* lval_read(mpc_ast_t* t)
{
if (strstr(t->tag, "number")) { return lval_read_num(t); }
if (strstr(t->tag, "symbol")) { return lval_sym(t->contents); }
if (strstr(t->tag, "string")) { return lval_read_str(t); }
lval* x = NULL;
if (strcmp(t->tag, ">") == 0) { x = lval_sexpr(); }
if (strstr(t->tag, "sexpr")) { x = lval_sexpr(); }
if (strstr(t->tag, "qexpr")) { x = lval_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 = lval_add(x, lval_read(t->children[i]));
}
return x;
}
lval* lval_read_num(mpc_ast_t* t)
{
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ?
lval_num(x) : lval_err("Invalid Number");
}
lval* lval_read_str(mpc_ast_t* t)
{
// replace the end quotes with a null terminator
t->contents[strlen(t->contents)-1] = '\0';
// copy the string from 1st char after the beginning quotes
char* unescaped = malloc(strlen(t->contents+1)+1);
strcpy(unescaped, t->contents+1);
unescaped = mpcf_unescape(unescaped);
lval* str = lval_str(unescaped);
free(unescaped);
return str;
}
int number_of_nodes(mpc_ast_t* t)
{
if (t->children_num == 0)
{
return 1;
}
if (t->children_num >= 1)
{
int total = 1;
for (int i = 0; i < t->children_num; i++)
{
total = total + number_of_nodes(t->children[i]);
}
return total;
}
return 0;
}
parser_elements* get_parser(void)
{
parser_elements* ret = malloc(sizeof(parser_elements));
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Symbol = mpc_new("symbol");
mpc_parser_t* String = mpc_new("string");
mpc_parser_t* Comment = mpc_new("comment");
mpc_parser_t* Sexpr = mpc_new("sexpr");
mpc_parser_t* Qexpr = mpc_new("qexpr");
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* Phi = mpc_new("phi");
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
symbol : /[a-zA-Z0-9_+\\-*\\/\\\\=<>!&]+/ ; \
string : /\"(\\\\.|[^\"])*\"/ ; \
comment : /;[^\\r\\n]*/ ; \
sexpr : '(' <expr>* ')' ; \
qexpr : '{' <expr>* '}' ; \
expr : <number> | <symbol> | <string> | <comment> | <sexpr> | <qexpr> ; \
phi : /^/ <expr>* /$/ ; \
",
Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Phi);
ret->Number = Number;
ret->Symbol = Symbol;
ret->String = String;
ret->Comment = Comment;
ret->Sexpr = Sexpr;
ret->Qexpr = Qexpr;
ret->Expr= Expr;
ret->Phi = Phi;
return ret;
}
void free_parsers(parser_elements* _parser_elements)
{
mpc_cleanup(
8,
_parser_elements->Number,
_parser_elements->Symbol,
_parser_elements->String,
_parser_elements->Comment,
_parser_elements->Sexpr,
_parser_elements->Qexpr,
_parser_elements->Expr,
_parser_elements->Phi
);
free(_parser_elements);
}