Skip to content

Commit 8054db9

Browse files
committed
more pass by references, set err_expr only when necessary
1 parent bc391b1 commit 8054db9

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

arc.cpp

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace arc {
77
std::shared_ptr<struct env> global_env = std::make_shared<struct env>(nullptr); /* the global environment */
88
/* symbols for faster execution */
99
atom sym_t, sym_quote, sym_quasiquote, sym_unquote, sym_unquote_splicing, sym_assign, sym_fn, sym_if, sym_mac, sym_apply, sym_cons, sym_sym, sym_string, sym_num, sym__, sym_o, sym_table, sym_int, sym_char, sym_do;
10-
atom cur_expr;
10+
atom err_expr; /* for error reporting */
1111
atom thrown;
1212
std::unordered_map<std::string, std::string *> id_of_sym;
1313

@@ -32,11 +32,11 @@ namespace arc {
3232
return r;
3333
}
3434

35-
atom & car(atom & a) {
35+
atom & car(const atom & a) {
3636
return a.as<arc::cons>().car;
3737
}
3838

39-
atom & cdr(atom & a) {
39+
atom & cdr(const atom & a) {
4040
return a.as<arc::cons>().cdr;
4141
}
4242

@@ -68,7 +68,7 @@ namespace arc {
6868
return a;
6969
}
7070

71-
atom make_sym(std::string s)
71+
atom make_sym(const std::string &s)
7272
{
7373
atom a;
7474
a.type = T_SYM;
@@ -115,7 +115,7 @@ namespace arc {
115115
return ERROR_OK;
116116
}
117117

118-
atom make_string(const std::string x)
118+
atom make_string(const std::string &x)
119119
{
120120
atom a;
121121
a.type = T_STRING;
@@ -151,12 +151,12 @@ namespace arc {
151151
return a;
152152
}
153153

154-
void print_expr(atom a)
154+
void print_expr(const atom &a)
155155
{
156156
std::cout << to_string(a, 1);
157157
}
158158

159-
void pr(atom a)
159+
void pr(const atom &a)
160160
{
161161
std::cout << to_string(a, 0);
162162
}
@@ -1675,7 +1675,7 @@ A symbol can be coerced to a string.
16751675

16761676
error builtin_err(const std::vector<atom> &vargs, atom *result) {
16771677
if (vargs.size() == 0) return ERROR_ARGS;
1678-
cur_expr = nil;
1678+
err_expr = nil;
16791679
size_t i;
16801680
for (i = 0; i < vargs.size(); i++) {
16811681
std::cout << to_string(vargs[i], 0) << '\n';
@@ -1824,45 +1824,47 @@ A symbol can be coerced to a string.
18241824

18251825
/* end builtin */
18261826

1827-
std::string to_string(atom a, int write) {
1827+
std::string to_string(const atom &a, int write) {
18281828
std::string s;
18291829
switch (a.type) {
18301830
case T_NIL:
18311831
s = "nil";
18321832
break;
1833-
case T_CONS:
1834-
if (listp(a) && len(a) == 2) {
1835-
if (is(car(a), sym_quote)) {
1836-
s = "'" + to_string(car(cdr(a)), write);
1833+
case T_CONS: {
1834+
atom a2 = a;
1835+
if (listp(a2) && len(a2) == 2) {
1836+
if (is(car(a2), sym_quote)) {
1837+
s = "'" + to_string(car(cdr(a2)), write);
18371838
break;
18381839
}
1839-
else if (is(car(a), sym_quasiquote)) {
1840-
s = "`" + to_string(car(cdr(a)), write);
1840+
else if (is(car(a2), sym_quasiquote)) {
1841+
s = "`" + to_string(car(cdr(a2)), write);
18411842
break;
18421843
}
1843-
else if (is(car(a), sym_unquote)) {
1844-
s = "," + to_string(car(cdr(a)), write);
1844+
else if (is(car(a2), sym_unquote)) {
1845+
s = "," + to_string(car(cdr(a2)), write);
18451846
break;
18461847
}
1847-
else if (is(car(a), sym_unquote_splicing)) {
1848-
s = ",@" + to_string(car(cdr(a)), write);
1848+
else if (is(car(a2), sym_unquote_splicing)) {
1849+
s = ",@" + to_string(car(cdr(a2)), write);
18491850
break;
18501851
}
18511852
}
1852-
s = "(" + to_string(car(a), write);
1853-
a = cdr(a);
1854-
while (!no(a)) {
1853+
s = "(" + to_string(car(a2), write);
1854+
a2 = cdr(a2);
1855+
while (!no(a2)) {
18551856
if (a.type == T_CONS) {
1856-
s += " " + to_string(car(a), write);
1857-
a = cdr(a);
1857+
s += " " + to_string(car(a2), write);
1858+
a2 = cdr(a2);
18581859
}
18591860
else {
1860-
s += " . " + to_string(a, write);
1861+
s += " . " + to_string(a2, write);
18611862
break;
18621863
}
18631864
}
18641865
s += ")";
18651866
break;
1867+
}
18661868
case T_SYM:
18671869
s = *a.as<std::string *>();
18681870
break;
@@ -1981,7 +1983,7 @@ A symbol can be coerced to a string.
19811983
error macex(atom expr, atom *result) {
19821984
error err = ERROR_OK;
19831985

1984-
cur_expr = expr; /* for error reporting */
1986+
err_expr = expr;
19851987

19861988
if (expr.type != T_CONS || !listp(expr)) {
19871989
*result = expr;
@@ -2095,21 +2097,22 @@ A symbol can be coerced to a string.
20952097
{
20962098
error err;
20972099
start_eval:
2098-
2099-
cur_expr = expr; /* for error reporting */
2100+
21002101
if (expr.type == T_SYM) {
21012102
err = env_get(env, expr.as<std::string *>(), result);
2103+
if (err) err_expr = expr;
21022104
return err;
21032105
}
21042106
else if (expr.type != T_CONS) {
21052107
*result = expr;
21062108
return ERROR_OK;
21072109
}
21082110
else if (!listp(expr)) {
2111+
err_expr = expr;
21092112
return ERROR_SYNTAX;
21102113
}
21112114
else {
2112-
atom op = car(expr);
2115+
const atom &op = car(expr);
21132116
atom args = cdr(expr);
21142117

21152118
if (op.type == T_SYM) {
@@ -2123,6 +2126,7 @@ A symbol can be coerced to a string.
21232126
}
21242127
err = eval_expr(car(args), env, result);
21252128
if (err) {
2129+
err_expr = expr;
21262130
return err;
21272131
}
21282132
if (!no(*result)) { /* then */
@@ -2137,6 +2141,7 @@ A symbol can be coerced to a string.
21372141
else if (sym_is(op, sym_assign)) {
21382142
atom sym;
21392143
if (no(args) || no(cdr(args))) {
2144+
err_expr = expr;
21402145
return ERROR_ARGS;
21412146
}
21422147

@@ -2147,14 +2152,17 @@ A symbol can be coerced to a string.
21472152
return err;
21482153
}
21492154
err = env_assign_eq(env, sym.as<std::string *>(), *result);
2155+
if (err) err_expr = expr;
21502156
return err;
21512157
}
21522158
else {
2159+
err_expr = expr;
21532160
return ERROR_TYPE;
21542161
}
21552162
}
21562163
else if (sym_is(op, sym_quote)) {
21572164
if (no(args) || !no(cdr(args))) {
2165+
err_expr = expr;
21582166
return ERROR_ARGS;
21592167
}
21602168

@@ -2163,9 +2171,11 @@ A symbol can be coerced to a string.
21632171
}
21642172
else if (sym_is(op, sym_fn)) {
21652173
if (no(args)) {
2174+
err_expr = expr;
21662175
return ERROR_ARGS;
21672176
}
21682177
err = make_closure(env, car(args), cdr(args), result);
2178+
if (err) err_expr = expr;
21692179
return err;
21702180
}
21712181
else if (sym_is(op, sym_do)) {
@@ -2179,6 +2189,7 @@ A symbol can be coerced to a string.
21792189
}
21802190
error err = eval_expr(car(args), env, result);
21812191
if (err) {
2192+
err_expr = expr;
21822193
return err;
21832194
}
21842195
args = cdr(args);
@@ -2189,11 +2200,13 @@ A symbol can be coerced to a string.
21892200
atom name, macro;
21902201

21912202
if (no(args) || no(cdr(args)) || no(cdr(cdr(args)))) {
2203+
err_expr = expr;
21922204
return ERROR_ARGS;
21932205
}
21942206

21952207
name = car(args);
21962208
if (name.type != T_SYM) {
2209+
err_expr = expr;
21972210
return ERROR_TYPE;
21982211
}
21992212

@@ -2202,9 +2215,11 @@ A symbol can be coerced to a string.
22022215
macro.type = T_MACRO;
22032216
*result = name;
22042217
err = env_assign(env, name.as<std::string *>(), macro);
2218+
if (err) err_expr = expr;
22052219
return err;
22062220
}
22072221
else {
2222+
err_expr = expr;
22082223
return err;
22092224
}
22102225
}
@@ -2214,6 +2229,7 @@ A symbol can be coerced to a string.
22142229
atom fn;
22152230
err = eval_expr(op, env, &fn);
22162231
if (err) {
2232+
err_expr = expr;
22172233
return err;
22182234
}
22192235

@@ -2224,6 +2240,7 @@ A symbol can be coerced to a string.
22242240
atom r;
22252241
err = eval_expr(car(*p), env, &r);
22262242
if (err) {
2243+
err_expr = expr;
22272244
return err;
22282245
}
22292246
vargs.push_back(r);
@@ -2250,6 +2267,7 @@ A symbol can be coerced to a string.
22502267
atom r;
22512268
error err = eval_expr(car(body), env, &r);
22522269
if (err) {
2270+
err_expr = expr;
22532271
return err;
22542272
}
22552273
body = cdr(body);
@@ -2258,6 +2276,7 @@ A symbol can be coerced to a string.
22582276
}
22592277
else {
22602278
err = apply(fn, vargs, result);
2279+
if (err) err_expr = expr;
22612280
}
22622281
return err;
22632282
}
@@ -2367,7 +2386,7 @@ A symbol can be coerced to a string.
23672386
void print_error(error e) {
23682387
if (e != ERROR_USER) {
23692388
printf("%s : ", error_string[e]);
2370-
print_expr(cur_expr);
2389+
print_expr(err_expr);
23712390
puts("");
23722391
}
23732392
}

arc.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#endif
3535

3636
namespace arc {
37-
constexpr auto VERSION = "0.26";
37+
constexpr auto VERSION = "0.26.1";
3838

3939
enum type {
4040
T_NIL,
@@ -100,7 +100,7 @@ namespace arc {
100100
char *slurp(const char *path);
101101
error eval_expr(atom expr, std::shared_ptr<struct env> env, atom *result);
102102
error macex(atom expr, atom *result);
103-
std::string to_string(atom a, int write);
103+
std::string to_string(const atom &a, int write);
104104
error macex_eval(atom expr, atom *result);
105105
error arc_load_file(const char *path);
106106
void arc_init();
@@ -109,15 +109,15 @@ namespace arc {
109109
#endif
110110
char *readline_fp(const char *prompt, FILE *fp);
111111
error read_expr(const char *input, const char **end, atom *result);
112-
void print_expr(atom a);
112+
void print_expr(const atom &a);
113113
void print_error(error e);
114114
bool is(const atom &a, const atom &b);
115115
bool iso(const atom& a, const atom& b);
116116
atom make_table();
117117
void repl();
118118
atom make_cons(const atom &car_val, const atom &cdr_val);
119-
atom & car(atom & a);
120-
atom & cdr(atom & a);
119+
atom & car(const atom & a);
120+
atom & cdr(const atom & a);
121121
bool no(const atom & a);
122122
bool sym_is(const atom & a, const atom & b);
123123
/* end forward */

0 commit comments

Comments
 (0)