-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsclisp.c
More file actions
148 lines (116 loc) · 2.68 KB
/
sclisp.c
File metadata and controls
148 lines (116 loc) · 2.68 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "utils.h"
#include "mpc.h"
#include <stdio.h>
#include <stdlib.h>
#include <editline/readline.h>
static struct lval *lval_read(mpc_ast_t *ast);
static struct lval *lval_read_num(mpc_ast_t *ast);
int main(void)
{
mpc_parser_t *Number = mpc_new("number");
mpc_parser_t *Symbol = mpc_new("symbol");
mpc_parser_t *Sexpr = mpc_new("sexpr");
mpc_parser_t *Qexpr = mpc_new("qexpr");
mpc_parser_t *Expr = mpc_new("expr");
mpc_parser_t *Sclisp = mpc_new("sclisp");
mpc_parser_t *init_parsers[] = {
Number,
Symbol,
Sexpr,
Qexpr,
Expr,
Sclisp
};
size_t parsers_len = sizeof(init_parsers) / sizeof(init_parsers[0]);
mpca_lang(
MPCA_LANG_DEFAULT,
"number: /-?\\d+(\\.\\d+)?/;"
"symbol: /[a-zA-Z0-9_+\\-*\\/^%\\\\=<>!&]+/;"
"sexpr: '(' <expr>* ')';"
"qexpr: '{' <expr>* '}';"
"expr: <number> | <symbol> | <sexpr> | <qexpr>;"
"sclisp: /^/ <expr>* /$/;",
parsers_len, init_parsers
);
puts("Sclisp v0.0.1-rc3");
puts("Press ctrl-c, ctrl-d, exit command to exit\n");
const char *prompt;
char *input;
struct lenv *env = lenv_init();
lenv_builtins_init(env);
while (1) {
prompt = "sclisp> ";
input = readline(prompt);
if (input == NULL || stringcmp(input, "exit") == 0) {
free(input);
break;
}
add_history(input);
mpc_result_t result;
int parsed = mpc_parse(
"<stdin>",
input,
Sclisp,
&result
);
if (parsed) {
mpc_ast_t *ast = result.output;
struct lval *eval = lval_eval(
env,
lval_read(ast)
);
lval_println(eval);
lval_del(eval);
mpc_ast_delete(ast);
ast = NULL;
} else {
mpc_err_print(result.error);
mpc_err_delete(result.error);
}
free(input);
}
lenv_del(env);
mpc_cleanup(parsers_len, init_parsers);
return 0;
}
static struct lval *lval_read(mpc_ast_t *ast)
{
int i;
struct lval *value = NULL;
if (strstr(ast->tag, "number"))
return lval_read_num(ast);
if (strstr(ast->tag, "symbol"))
return lval_sym(ast->contents);
/*
* Symbol > is the root for the parser (?).
*/
if (*ast->tag == '>' || strstr(ast->tag, "sexpr"))
value = lval_sexpr();
if (strstr(ast->tag, "qexpr"))
value = lval_qexpr();
for (i = 0; i < ast->children_num; i++) {
if (*ast->children[i]->contents == '('
|| *ast->children[i]->contents == ')'
|| *ast->children[i]->contents == '{'
|| *ast->children[i]->contents == '}'
|| stringcmp(ast->children[i]->tag, "regex") == 0
)
continue;
value = lval_add(
value,
lval_read(ast->children[i])
);
}
return value;
}
static struct lval *lval_read_num(mpc_ast_t *ast)
{
double num = strtod(ast->contents, NULL);
return num == 0 && errno == ERANGE
? lval_err(
"invalid number",
__FILE__,
__LINE__
)
: lval_num(num);
}