aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-13 21:08:17 +0200
committerBad Diode <bd@badd10de.dev>2021-10-13 21:08:17 +0200
commit00cf382196f81e22256e22e5c79a9d3503db5e91 (patch)
tree861137232ac8199beffd5c8d53284ec7be751fba
parentd38ae947933fe26773a810d91fba3b23766d4d92 (diff)
downloadbdl-00cf382196f81e22256e22e5c79a9d3503db5e91.tar.gz
bdl-00cf382196f81e22256e22e5c79a9d3503db5e91.zip
Add supress-errors primitive and variable tests
-rwxr-xr-xMakefile1
-rw-r--r--examples/variables.bdl16
-rw-r--r--src/bootstrap/errors.c1
-rwxr-xr-xsrc/bootstrap/main.c10
-rw-r--r--src/bootstrap/primitives.c31
-rw-r--r--tests/variables_expected.txt11
6 files changed, 67 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index ebb1bfb..50e0828 100755
--- a/Makefile
+++ b/Makefile
@@ -51,6 +51,7 @@ tests: $(BIN)
51 ./$(BIN) examples/booleans.bdl | diff tests/booleans_expected.txt - 51 ./$(BIN) examples/booleans.bdl | diff tests/booleans_expected.txt -
52 ./$(BIN) examples/lists.bdl | diff tests/lists_expected.txt - 52 ./$(BIN) examples/lists.bdl | diff tests/lists_expected.txt -
53 ./$(BIN) examples/types.bdl | diff tests/types_expected.txt - 53 ./$(BIN) examples/types.bdl | diff tests/types_expected.txt -
54 ./$(BIN) examples/variables.bdl | diff tests/variables_expected.txt -
54 55
55# Remove build directory. 56# Remove build directory.
56clean: 57clean:
diff --git a/examples/variables.bdl b/examples/variables.bdl
new file mode 100644
index 0000000..6097368
--- /dev/null
+++ b/examples/variables.bdl
@@ -0,0 +1,16 @@
1;;
2;; Variable declarations and updates
3;;
4
5(supress-errors true)
6(print "(error? (def a 1)) -> ") (error? (def a 1))
7(print "a -> ") a
8(print "(error? (def a 300)) -> ") (error? (def a 300))
9(print "a -> ") a
10(print "(error? (def a \"strings\")) -> ") (error? (def a "strings"))
11(print "a -> ") a
12(print "(error? (def a 1)) -> ") (error? (def a '(quoted symbols 123 or "strings")))
13(print "a -> ") a
14(print "(error? (set! a 42)) -> ") (error? (set! a 42))
15(print "a -> ") a
16(print "(error? (set! b 99)) -> ") (error? (set! b 99))
diff --git a/src/bootstrap/errors.c b/src/bootstrap/errors.c
index 13a2f3c..61ea902 100644
--- a/src/bootstrap/errors.c
+++ b/src/bootstrap/errors.c
@@ -46,6 +46,7 @@ static const char* error_msgs[] = {
46#define ERR_MAX_NUMBER 16 46#define ERR_MAX_NUMBER 16
47static Error errors[ERR_MAX_NUMBER]; 47static Error errors[ERR_MAX_NUMBER];
48static size_t errors_n = 0; 48static size_t errors_n = 0;
49static bool supress_errors = false;
49 50
50void 51void
51error_push(Error error) { 52error_push(Error error) {
diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c
index 052f1c0..c589b2d 100755
--- a/src/bootstrap/main.c
+++ b/src/bootstrap/main.c
@@ -61,6 +61,7 @@ init(void) {
61 MAKE_ENV_PROC(global_env, "fixnum?", proc_is_fixnum); 61 MAKE_ENV_PROC(global_env, "fixnum?", proc_is_fixnum);
62 MAKE_ENV_PROC(global_env, "pair?", proc_is_pair); 62 MAKE_ENV_PROC(global_env, "pair?", proc_is_pair);
63 MAKE_ENV_PROC(global_env, "procedure?", proc_is_procedure); 63 MAKE_ENV_PROC(global_env, "procedure?", proc_is_procedure);
64 MAKE_ENV_PROC(global_env, "error?", proc_is_error);
64 MAKE_ENV_PROC(global_env, "not", proc_not); 65 MAKE_ENV_PROC(global_env, "not", proc_not);
65 MAKE_ENV_PROC(global_env, "and", proc_and); 66 MAKE_ENV_PROC(global_env, "and", proc_and);
66 MAKE_ENV_PROC(global_env, "or", proc_or); 67 MAKE_ENV_PROC(global_env, "or", proc_or);
@@ -74,6 +75,9 @@ init(void) {
74 MAKE_ENV_PROC(global_env, "eq?", proc_equal); 75 MAKE_ENV_PROC(global_env, "eq?", proc_equal);
75 MAKE_ENV_PROC(global_env, "def", proc_define); 76 MAKE_ENV_PROC(global_env, "def", proc_define);
76 MAKE_ENV_PROC(global_env, "set!", proc_set); 77 MAKE_ENV_PROC(global_env, "set!", proc_set);
78
79 // Runtime procedures.
80 MAKE_ENV_PROC(global_env, "supress-errors", proc_supress_errors);
77} 81}
78 82
79void 83void
@@ -125,7 +129,7 @@ run_repl(void) {
125 process_source(&sv); 129 process_source(&sv);
126 130
127 // Check if there were any errors. 131 // Check if there were any errors.
128 if (errors_n != 0) { 132 if (errors_n != 0 && !supress_errors) {
129 for (size_t i = 0; i < errors_n; i++) { 133 for (size_t i = 0; i < errors_n; i++) {
130 Error err = errors[i]; 134 Error err = errors[i];
131 for (size_t j = 0; j < err.col + sizeof(REPL_PROMPT) - 2; j++) { 135 for (size_t j = 0; j < err.col + sizeof(REPL_PROMPT) - 2; j++) {
@@ -168,7 +172,7 @@ run_file(char *file_name) {
168 process_source(&sv); 172 process_source(&sv);
169 173
170 // Check if there were any errors. 174 // Check if there were any errors.
171 if (errors_n != 0) { 175 if (errors_n != 0 && !supress_errors) {
172 for (size_t i = 0; i < errors_n; i++) { 176 for (size_t i = 0; i < errors_n; i++) {
173 Error err = errors[i]; 177 Error err = errors[i];
174 fprintf(stderr, "%s", file_name); 178 fprintf(stderr, "%s", file_name);
@@ -210,7 +214,7 @@ run_stdin(void) {
210 process_source(&sv); 214 process_source(&sv);
211 215
212 // Check if there were any errors. 216 // Check if there were any errors.
213 if (errors_n != 0) { 217 if (errors_n != 0 && !supress_errors) {
214 for (size_t i = 0; i < errors_n; i++) { 218 for (size_t i = 0; i < errors_n; i++) {
215 Error err = errors[i]; 219 Error err = errors[i];
216 fprintf(stderr, "stdin"); 220 fprintf(stderr, "stdin");
diff --git a/src/bootstrap/primitives.c b/src/bootstrap/primitives.c
index a3b69f6..3afeef6 100644
--- a/src/bootstrap/primitives.c
+++ b/src/bootstrap/primitives.c
@@ -319,6 +319,22 @@ proc_is_procedure(Environment *env, Object *obj) {
319 return obj->type == OBJ_TYPE_PROCEDURE ? obj_true : obj_false; 319 return obj->type == OBJ_TYPE_PROCEDURE ? obj_true : obj_false;
320} 320}
321 321
322Object *
323proc_is_error(Environment *env, Object *obj) {
324 if (obj == obj_nil) {
325 error_push((Error){
326 .type = ERR_TYPE_RUNTIME,
327 .value = ERR_NOT_ENOUGH_ARGS,
328 });
329 return obj_err;
330 }
331 obj = eval(env, obj->car);
332 if (obj == obj_err) {
333 return obj_true;
334 }
335 return obj_false;
336}
337
322// 338//
323// Boolean/conditional procedures. 339// Boolean/conditional procedures.
324// 340//
@@ -693,6 +709,21 @@ proc_eval(Environment *env, Object *obj) {
693 return eval(env, eval(env, obj->car)); 709 return eval(env, eval(env, obj->car));
694} 710}
695 711
712Object *
713proc_supress_errors(Environment *env, Object *obj) {
714 Object *car = extract_car_with_type(env, obj, OBJ_TYPE_BOOL);
715 if (car == obj_err) {
716 return obj_err;
717 }
718
719 if (car == obj_true) {
720 supress_errors = true;
721 } else if (car == obj_false) {
722 supress_errors = false;
723 }
724 return obj_nil;
725}
726
696// TODO: map 727// TODO: map
697// TODO: apply 728// TODO: apply
698// TODO: filter 729// TODO: filter
diff --git a/tests/variables_expected.txt b/tests/variables_expected.txt
new file mode 100644
index 0000000..2e6e2be
--- /dev/null
+++ b/tests/variables_expected.txt
@@ -0,0 +1,11 @@
1(error? (def a 1)) -> false
2a -> 1
3(error? (def a 300)) -> false
4a -> 300
5(error? (def a "strings")) -> false
6a -> "strings"
7(error? (def a 1)) -> false
8a -> (:quoted :symbols 123 :or "strings")
9(error? (set! a 42)) -> false
10a -> 42
11(error? (set! b 99)) -> true