-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
133 lines (100 loc) · 3.66 KB
/
main.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
133
/*
* Copyright (C) 2016 Mohamed Rashad
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* adouble with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Simplex.h"
int zstring_search_chr(const char *token,char s){
if (!token || s=='\0')
return 0;
for (;*token; token++)
if (*token == s)
return 1;
return 0;
}
char *zstring_remove_chr(char *str,const char *bad) {
char *src = str , *dst = str;
while(*src)
if(zstring_search_chr(bad,*src))
src++;
else
*dst++ = *src++; /* assign first, then incement */
*dst='\0';
return str;
}
int main(int argc, char** argv) {
char input[4096];
Number = mpc_new("number");
Symbol = mpc_new("symbol");
String = mpc_new("string");
Comment = mpc_new("comment");
Sexpr = mpc_new("sexpr");
Qexpr = mpc_new("qexpr");
Expr = mpc_new("expr");
Lispy = mpc_new("simplex");
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+(\\.[0-9]+)?/ ; \
symbol : /[a-zA-Z0-9_+\\-*\\/\\\\@=<>!&]+/ ; \
string : /\"(\\\\.|[^\"])*\"/ ; \
comment : /#[^\\r\\n]*/ ; \
sexpr : '(' <expr>* ')' | <comment>; \
qexpr : '[' <expr>* ']' ; \
expr : <number> | <symbol> | <string> \
| <comment> | <sexpr> | <qexpr>; \
simplex : /^/ <expr>* /$/ ; \
",
Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy);
smplx_scope* e = smplx_scope_new();
smplx_scope_add_builtins(e);
/* Interactive Mode */
if (argc == 1) {
puts("Simplex BETA (V0.1) -- Interactive Mode");
puts("Press Ctrl+c to Exit\n");
while (1) {
printf("Simplex > ");
fgets(input, 4096, stdin);
mpc_result_t r;
if (mpc_parse("<stdin>", zstring_remove_chr(input,";"), Lispy, &r)) {
smplx_val* expr = smplx_val_read(r.output);
smplx_val* x = smplx_val_eval(e, expr);
smplx_val_println(x);
smplx_val_del(x);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
}
}
/* Batch Mode */
if (argc >= 2) {
/* loop over each supplied filename (starting from 1) */
for (int i = 1; i < argc; i++) {
/* Argument list with a single argument, the filename */
smplx_val* args = smplx_val_add(smplx_val_sexpr(), smplx_val_str(argv[i]));
/* Pass to builtin load and get the result */
smplx_val* x = builtin_load(e, args);
/* If the result is an error be sure to print it */
if (x->type == LVAL_ERR) {
smplx_val_println(x);
}
smplx_val_del(x);
}
}
/* Cleaning up resources */
smplx_scope_del(e);
mpc_cleanup(8, Number, Symbol, String, Comment, Sexpr, Qexpr, Expr, Lispy);
return 0;
}