Browse Source

Sol Part 64.1: YOU DIVIDED BY ZERO DIDN'T YOU

Graham Northup 3 years ago
parent
commit
563077defa
Signed by: Graham Northup <grissess@nexusg.org> GPG Key ID: 5D000E6F539376FB
13 changed files with 401 additions and 282 deletions
  1. 1
    1
      .gitignore
  2. 1
    0
      ast.h
  3. 79
    18
      builtins.c
  4. 19
    8
      lex.yy.c
  5. 247
    238
      parser.tab.c
  6. 4
    0
      parser.y
  7. 11
    0
      programs/solid_run.sol
  8. 4
    4
      programs/test.sol
  9. 3
    3
      sol.h
  10. 2
    2
      state.c
  11. 6
    0
      tests/crasher_apply_noargs.sol
  12. 5
    0
      tests/crasher_range_noargs.sol
  13. 19
    8
      tokenizer.lex

+ 1
- 1
.gitignore View File

@@ -15,4 +15,4 @@ doxyxml
15 15
 valgrind.log
16 16
 *_INFO
17 17
 prof
18
-/afl
18
+/afl*

+ 1
- 0
ast.h View File

@@ -301,6 +301,7 @@ sol_object_t *sol_new_exprnode(sol_state_t *, expr_node *);
301 301
 // runtime.c
302 302
 
303 303
 stmt_node *sol_compile(const char *);
304
+stmt_node *sol_compile_buffer(const char *, size_t);
304 305
 stmt_node *sol_compile_file(FILE *);
305 306
 expr_node *sol_comp_as_expr(stmt_node *);
306 307
 void sol_compile_free(stmt_node *);

+ 79
- 18
builtins.c View File

@@ -148,6 +148,12 @@ sol_object_t *sol_f_try(sol_state_t *state, sol_object_t *args) {
148 148
 
149 149
 sol_object_t *sol_f_apply(sol_state_t *state, sol_object_t *args) {
150 150
 	sol_object_t *func = sol_list_get_index(state, args, 0), *arglist = sol_list_get_index(state, args, 1), *rest = sol_list_sublist(state, args, 2);
151
+	if(!sol_is_list(arglist)) {
152
+		sol_obj_free(func);
153
+		sol_obj_free(arglist);
154
+		sol_obj_free(rest);
155
+		return sol_set_error_string(state, "apply with non-list");
156
+	}
151 157
 	sol_list_append(state, rest, arglist);
152 158
 	sol_obj_free(arglist);
153 159
 	sol_list_insert(state, rest, 0, func);
@@ -354,15 +360,23 @@ sol_object_t *sol_f_rawset(sol_state_t *state, sol_object_t *args) {
354 360
 }
355 361
 
356 362
 sol_object_t *sol_f_range(sol_state_t *state, sol_object_t *args) {
357
-	sol_object_t *res = sol_new_list(state), *bound = sol_cast_int(state, sol_list_get_index(state, args, 0));
363
+	sol_object_t *res = sol_new_list(state), *bound = sol_list_get_index(state, args, 0);
364
+	sol_object_t *boundi = sol_cast_int(state, bound);
358 365
 	int i;
359
-	for(i = 0; i < bound->ival; i++) {
366
+	sol_obj_free(bound);
367
+	if(sol_has_error(state)) {
368
+		sol_obj_free(boundi);
369
+		sol_obj_free(res);
370
+		return sol_incref(state->None);
371
+	}
372
+	for(i = 0; i < boundi->ival; i++) {
360 373
 		sol_list_insert(state, res, sol_list_len(state, res), sol_new_int(state, i));
361 374
 	}
362
-	sol_obj_free(bound);
375
+	sol_obj_free(boundi);
363 376
 	return res;
364 377
 }
365 378
 
379
+/*
366 380
 sol_object_t *sol_f_exec(sol_state_t *state, sol_object_t *args) {
367 381
 	sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
368 382
 	stmt_node *program;
@@ -392,6 +406,7 @@ sol_object_t *sol_f_eval(sol_state_t *state, sol_object_t *args) {
392 406
 
393 407
 	return sol_eval(state, program->stmtlist->stmt->expr);
394 408
 }
409
+*/
395 410
 
396 411
 sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
397 412
 	sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
@@ -421,17 +436,28 @@ sol_object_t *sol_f_execfile(sol_state_t *state, sol_object_t *args) {
421 436
 	if(!program) {
422 437
 		return sol_set_error_string(state, "Compilation failure");
423 438
 	}
424
-	// XXX should st_free(program);
425 439
 
426 440
 	sol_exec(state, program);
441
+	st_free(program);
427 442
 	return sol_incref(state->None);
428 443
 }
429 444
 
430 445
 sol_object_t *sol_f_parse(sol_state_t *state, sol_object_t *args) {
431
-	sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr = sol_cast_string(state, prg);
432
-	stmt_node *program = sol_compile(prgstr->str);
446
+	sol_object_t *prg = sol_list_get_index(state, args, 0), *prgstr;
447
+	stmt_node *program;
448
+	if(sol_is_buffer(prg)) {
449
+		if(prg->sz >= 0) {
450
+			program = sol_compile_buffer(prg->buffer, prg->sz);
451
+		} else {
452
+			sol_obj_free(prg);
453
+			return sol_set_error_string(state, "parse unsized buffer");
454
+		}
455
+	} else {
456
+		prgstr = sol_cast_string(state, prg);
457
+		program = sol_compile(prgstr->str);
458
+		sol_obj_free(prgstr);
459
+	}
433 460
 	sol_obj_free(prg);
434
-	sol_obj_free(prgstr);
435 461
 	if(!program) {
436 462
 		return sol_set_error_string(state, "Compilation failure");
437 463
 	}
@@ -698,6 +724,7 @@ sol_object_t *sol_f_int_add(sol_state_t *state, sol_object_t *args) {
698 724
 	sol_object_t *res = sol_new_int(state, a->ival + bint->ival);
699 725
 	sol_obj_free(a);
700 726
 	sol_obj_free(b);
727
+	sol_obj_free(bint);
701 728
 	if(sol_has_error(state)) {
702 729
 		sol_obj_free(res);
703 730
 		return sol_incref(state->None);
@@ -710,6 +737,7 @@ sol_object_t *sol_f_int_sub(sol_state_t *state, sol_object_t *args) {
710 737
 	sol_object_t *res = sol_new_int(state, a->ival - bint->ival);
711 738
 	sol_obj_free(a);
712 739
 	sol_obj_free(b);
740
+	sol_obj_free(bint);
713 741
 	if(sol_has_error(state)) {
714 742
 		sol_obj_free(res);
715 743
 		return sol_incref(state->None);
@@ -722,6 +750,7 @@ sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
722 750
 	sol_object_t *res = sol_new_int(state, a->ival * bint->ival);
723 751
 	sol_obj_free(a);
724 752
 	sol_obj_free(b);
753
+	sol_obj_free(bint);
725 754
 	if(sol_has_error(state)) {
726 755
 		sol_obj_free(res);
727 756
 		return sol_incref(state->None);
@@ -731,9 +760,16 @@ sol_object_t *sol_f_int_mul(sol_state_t *state, sol_object_t *args) {
731 760
 
732 761
 sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
733 762
 	sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
734
-	sol_object_t *res = sol_new_int(state, a->ival / bint->ival);
735
-	sol_obj_free(a);
763
+	sol_object_t *res;
736 764
 	sol_obj_free(b);
765
+	if(bint->ival == 0) {
766
+		sol_obj_free(a);
767
+		sol_obj_free(bint);
768
+		return sol_set_error_string(state, "integer divide by zero");
769
+	}
770
+	res = sol_new_int(state, a->ival / bint->ival);
771
+	sol_obj_free(a);
772
+	sol_obj_free(bint);
737 773
 	if(sol_has_error(state)) {
738 774
 		sol_obj_free(res);
739 775
 		return sol_incref(state->None);
@@ -743,9 +779,16 @@ sol_object_t *sol_f_int_div(sol_state_t *state, sol_object_t *args) {
743 779
 
744 780
 sol_object_t *sol_f_int_mod(sol_state_t *state, sol_object_t *args) {
745 781
 	sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bint = sol_cast_int(state, b);
746
-	sol_object_t *res = sol_new_int(state, a->ival % bint->ival);
747
-	sol_obj_free(a);
782
+	sol_object_t *res;
748 783
 	sol_obj_free(b);
784
+	if(bint->ival == 0) {
785
+		sol_obj_free(a);
786
+		sol_obj_free(bint);
787
+		return sol_set_error_string(state, "integer modulus by zero");
788
+	}
789
+	res = sol_new_int(state, a->ival % bint->ival);
790
+	sol_obj_free(a);
791
+	sol_obj_free(bint);
749 792
 	if(sol_has_error(state)) {
750 793
 		sol_obj_free(res);
751 794
 		return sol_incref(state->None);
@@ -758,6 +801,7 @@ sol_object_t *sol_f_int_pow(sol_state_t *state, sol_object_t *args) {
758 801
 	sol_object_t *res = sol_new_int(state, (long) pow((double) a->ival, bint->ival));
759 802
 	sol_obj_free(a);
760 803
 	sol_obj_free(b);
804
+	sol_obj_free(bint);
761 805
 	if(sol_has_error(state)) {
762 806
 		sol_obj_free(res);
763 807
 		return sol_incref(state->None);
@@ -770,6 +814,7 @@ sol_object_t *sol_f_int_band(sol_state_t *state, sol_object_t *args) {
770 814
 	sol_object_t *res = sol_new_int(state, a->ival & bint->ival);
771 815
 	sol_obj_free(a);
772 816
 	sol_obj_free(b);
817
+	sol_obj_free(bint);
773 818
 	if(sol_has_error(state)) {
774 819
 		sol_obj_free(res);
775 820
 		return sol_incref(state->None);
@@ -782,6 +827,7 @@ sol_object_t *sol_f_int_bor(sol_state_t *state, sol_object_t *args) {
782 827
 	sol_object_t *res = sol_new_int(state, a->ival | bint->ival);
783 828
 	sol_obj_free(a);
784 829
 	sol_obj_free(b);
830
+	sol_obj_free(bint);
785 831
 	if(sol_has_error(state)) {
786 832
 		sol_obj_free(res);
787 833
 		return sol_incref(state->None);
@@ -794,6 +840,7 @@ sol_object_t *sol_f_int_bxor(sol_state_t *state, sol_object_t *args) {
794 840
 	sol_object_t *res = sol_new_int(state, a->ival ^ bint->ival);
795 841
 	sol_obj_free(a);
796 842
 	sol_obj_free(b);
843
+	sol_obj_free(bint);
797 844
 	if(sol_has_error(state)) {
798 845
 		sol_obj_free(res);
799 846
 		return sol_incref(state->None);
@@ -806,6 +853,7 @@ sol_object_t *sol_f_int_blsh(sol_state_t *state, sol_object_t *args) {
806 853
 	sol_object_t *res = sol_new_int(state, a->ival << bint->ival);
807 854
 	sol_obj_free(a);
808 855
 	sol_obj_free(b);
856
+	sol_obj_free(bint);
809 857
 	if(sol_has_error(state)) {
810 858
 		sol_obj_free(res);
811 859
 		return sol_incref(state->None);
@@ -818,6 +866,7 @@ sol_object_t *sol_f_int_brsh(sol_state_t *state, sol_object_t *args) {
818 866
 	sol_object_t *res = sol_new_int(state, a->ival >> bint->ival);
819 867
 	sol_obj_free(a);
820 868
 	sol_obj_free(b);
869
+	sol_obj_free(bint);
821 870
 	if(sol_has_error(state)) {
822 871
 		sol_obj_free(res);
823 872
 		return sol_incref(state->None);
@@ -870,6 +919,7 @@ sol_object_t *sol_f_float_add(sol_state_t *state, sol_object_t *args) {
870 919
 	sol_object_t *res = sol_new_float(state, a->fval + bflt->fval);
871 920
 	sol_obj_free(a);
872 921
 	sol_obj_free(b);
922
+	sol_obj_free(bflt);
873 923
 	if(sol_has_error(state)) {
874 924
 		sol_obj_free(res);
875 925
 		return sol_incref(state->None);
@@ -882,6 +932,7 @@ sol_object_t *sol_f_float_sub(sol_state_t *state, sol_object_t *args) {
882 932
 	sol_object_t *res = sol_new_float(state, a->fval - bflt->fval);
883 933
 	sol_obj_free(a);
884 934
 	sol_obj_free(b);
935
+	sol_obj_free(bflt);
885 936
 	if(sol_has_error(state)) {
886 937
 		sol_obj_free(res);
887 938
 		return sol_incref(state->None);
@@ -894,6 +945,7 @@ sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
894 945
 	sol_object_t *res = sol_new_float(state, a->fval * bflt->fval);
895 946
 	sol_obj_free(a);
896 947
 	sol_obj_free(b);
948
+	sol_obj_free(bflt);
897 949
 	if(sol_has_error(state)) {
898 950
 		sol_obj_free(res);
899 951
 		return sol_incref(state->None);
@@ -903,9 +955,16 @@ sol_object_t *sol_f_float_mul(sol_state_t *state, sol_object_t *args) {
903 955
 
904 956
 sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
905 957
 	sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
906
-	sol_object_t *res = sol_new_float(state, a->fval / bflt->fval);
907
-	sol_obj_free(a);
958
+	sol_object_t *res;
908 959
 	sol_obj_free(b);
960
+	if(bflt->fval == 0.0) {
961
+		sol_obj_free(a);
962
+		sol_obj_free(bflt);
963
+		return sol_set_error_string(state, "floating division by zero");
964
+	}
965
+	res = sol_new_float(state, a->fval / bflt->fval);
966
+	sol_obj_free(a);
967
+	sol_obj_free(bflt);
909 968
 	if(sol_has_error(state)) {
910 969
 		sol_obj_free(res);
911 970
 		return sol_incref(state->None);
@@ -916,8 +975,9 @@ sol_object_t *sol_f_float_div(sol_state_t *state, sol_object_t *args) {
916 975
 sol_object_t *sol_f_float_pow(sol_state_t *state, sol_object_t *args) {
917 976
 	sol_object_t *a = sol_list_get_index(state, args, 0), *b = sol_list_get_index(state, args, 1), *bflt = sol_cast_float(state, b);
918 977
 	sol_object_t *res = sol_new_float(state, pow(a->fval, bflt->fval));
919
-	sol_obj_free(a);
920 978
 	sol_obj_free(b);
979
+	sol_obj_free(a);
980
+	sol_obj_free(bflt);
921 981
 	if(sol_has_error(state)) {
922 982
 		sol_obj_free(res);
923 983
 		return sol_incref(state->None);
@@ -963,6 +1023,7 @@ sol_object_t *sol_f_str_add(sol_state_t *state, sol_object_t *args) {
963 1023
 	sol_object_t *res = sol_string_concat(state, a, bstr);
964 1024
 	sol_obj_free(a);
965 1025
 	sol_obj_free(b);
1026
+	sol_obj_free(bstr);
966 1027
 	if(sol_has_error(state)) {
967 1028
 		sol_obj_free(res);
968 1029
 		return sol_incref(state->None);
@@ -2415,7 +2476,7 @@ sol_object_t *sol_f_buffer_index(sol_state_t *state, sol_object_t *args) {
2415 2476
 	if(sol_is_name(key)) {
2416 2477
 		res = sol_map_get(state, funcs, key);
2417 2478
 	} else if(sol_is_int(key)) {
2418
-		res = sol_new_buffer(state, a->buffer, (a->sz < 0) ? a->sz : (a->sz - key->ival), OWN_NONE, NULL, NULL);
2479
+		res = sol_new_buffer(state, ((char *) a->buffer) + key->ival, (a->sz < 0) ? a->sz : (a->sz - key->ival), OWN_NONE, NULL, NULL);
2419 2480
 	} else {
2420 2481
 		res = sol_f_not_impl(state, args);
2421 2482
 	}
@@ -2545,14 +2606,14 @@ sol_object_t *sol_f_buffer_repr(sol_state_t *state, sol_object_t *args) {
2545 2606
 
2546 2607
 sol_object_t *sol_f_buffer_toint(sol_state_t *state, sol_object_t *args) {
2547 2608
 	sol_object_t *a = sol_list_get_index(state, args, 0);
2548
-	sol_object_t *res = sol_new_int(state, atoi(a->buffer));
2609
+	sol_object_t *res = sol_new_int(state, a->buffer ? atoi(a->buffer) : 0);
2549 2610
 	sol_obj_free(a);
2550 2611
 	return res;
2551 2612
 }
2552 2613
 
2553 2614
 sol_object_t *sol_f_buffer_tofloat(sol_state_t *state, sol_object_t *args) {
2554 2615
 	sol_object_t *a = sol_list_get_index(state, args, 0);
2555
-	sol_object_t *res = sol_new_float(state, atof(a->buffer));
2616
+	sol_object_t *res = sol_new_float(state, a->buffer ? atof(a->buffer) : 0.0);
2556 2617
 	sol_obj_free(a);
2557 2618
 	return res;
2558 2619
 }
@@ -2864,7 +2925,7 @@ sol_object_t *sol_f_buffer_sub(sol_state_t *state, sol_object_t *args) {
2864 2925
 		return sol_new_buffer(state, NULL, 0, OWN_NONE, NULL, NULL);
2865 2926
 	}
2866 2927
 	b = malloc(sizeof(char) * (h - l));
2867
-	memcpy(b, buf->buffer, h - l);
2928
+	memcpy(b, ((char *) buf->buffer) + l, h - l);
2868 2929
 	sol_obj_free(buf);
2869 2930
 	return sol_new_buffer(state, b, h - l, OWN_FREE, NULL, NULL);
2870 2931
 }

+ 19
- 8
lex.yy.c View File

@@ -572,7 +572,7 @@ char *yytext;
572 572
 #include <string.h>
573 573
 #include <stdio.h>
574 574
 
575
-void yyerror(YYLTYPE *, char *);
575
+void yyerror(YYLTYPE *, stmt_node **, char *);
576 576
 int yywrap(void);
577 577
 
578 578
 char *str, *curptr;
@@ -600,11 +600,11 @@ void str_putc(char c) {
600 600
 /* Many thanks to hugomg and David Elson! */
601 601
 
602 602
 static void update_loc(YYLTYPE *yylloc, char *yytext){
603
-  static int curr_line = 1;
604
-  static int curr_col  = 1;
603
+ int curr_line;
604
+ int curr_col;
605 605
 
606
-  yylloc->first_line   = curr_line;
607
-  yylloc->first_column = curr_col;
606
+  curr_line = yylloc->first_line   = yylloc->last_line;
607
+  curr_col = yylloc->first_column = yylloc->last_column;
608 608
 
609 609
   {char * s; for(s = yytext; *s != '\0'; s++){
610 610
     if(*s == '\n'){
@@ -2322,9 +2322,12 @@ int yywrap(void) {
2322 2322
 	return 1;
2323 2323
 }
2324 2324
 
2325
-void yyerror(YYLTYPE *locp, char *err) {
2326
-	puts(err);
2327
-	printf("(at lines %d-%d, cols %d-%d)\n", locp->first_line, locp->last_line, locp->first_column, locp->last_column);
2325
+void yyerror(YYLTYPE *locp, stmt_node **prog, char *err) {
2326
+	fputs(err, stderr);
2327
+	fprintf(stderr, "\n(at lines %d-%d, cols %d-%d)\n", locp->first_line, locp->last_line, locp->first_column, locp->last_column);
2328
+	if(prog && *prog) {
2329
+		fprintf(stderr, "(while building a stmt of type %d)\n", (*prog)->type);
2330
+	}
2328 2331
 }
2329 2332
 
2330 2333
 stmt_node *sol_compile(const char *prgstr) {
@@ -2335,6 +2338,14 @@ stmt_node *sol_compile(const char *prgstr) {
2335 2338
     return program;
2336 2339
 }
2337 2340
 
2341
+stmt_node *sol_compile_buffer(const char *prgbuf, size_t sz) {
2342
+	stmt_node *program = NULL;
2343
+	YY_BUFFER_STATE buf = yy_scan_bytes(prgbuf, sz);
2344
+	yyparse(&program);
2345
+	yy_delete_buffer(buf);
2346
+	return program;
2347
+}
2348
+
2338 2349
 stmt_node *sol_compile_file(FILE *prgfile) {
2339 2350
     stmt_node *program = NULL;
2340 2351
     YY_BUFFER_STATE buf = yy_create_buffer(prgfile, YY_BUF_SIZE);

+ 247
- 238
parser.tab.c
File diff suppressed because it is too large
View File


+ 4
- 0
parser.y View File

@@ -37,6 +37,10 @@ void yyerror(loc_t *, stmt_node **, char *);
37 37
 
38 38
 %debug
39 39
 %locations
40
+%initial-action {
41
+	@$.first_line = 1; @$.first_column = 0;
42
+	@$.last_line = 1; @$.last_column = 1;
43
+}
40 44
 
41 45
 %%
42 46
 

+ 11
- 0
programs/solid_run.sol View File

@@ -0,0 +1,11 @@
1
+execfile('solid.sol')
2
+io.stdout:write('Enter filename to run: ')
3
+__fname = io.stdin:read(io.LINE):sub(0, -1)
4
+__result = try(execfile, __fname)
5
+if __result[0] then
6
+	print('Subprogram exited successfully')
7
+else
8
+	print('Subprogram terminated with an error: ', __result[1])
9
+	__debst = new_debug_state(__result)
10
+	postmortem(__debst)
11
+end

+ 4
- 4
programs/test.sol View File

@@ -182,10 +182,10 @@ print('--- Map/filter chain')
182 182
 print([1 2 3 4 5]:map(func (i) return i * 3 end):filter(func (i) return i & 1 end))
183 183
 
184 184
 print('--- Exec/eval')
185
-
186
-exec('print("Hello from exec!")')
187
-print(eval('5 + 3'))
188
-execfile('programs/subtest.sol')
185
+print('(removed from core language)')
186
+--exec('print("Hello from exec!")')
187
+--print(eval('5 + 3'))
188
+--execfile('programs/subtest.sol')
189 189
 
190 190
 print('--- Modulus')
191 191
 

+ 3
- 3
sol.h View File

@@ -10,7 +10,7 @@
10 10
 #include "dsl/dsl.h"
11 11
 
12 12
 /** The version of the project, as made available through `debug.version`. */
13
-#define SOL_VERSION "0.4a0"
13
+#define SOL_VERSION "0.4a1"
14 14
 /** The hexadecimal version of the project, formatted 0xAAIIRPP where:
15 15
  * 
16 16
  * - AA is the two-digit major version
@@ -739,9 +739,9 @@ sol_object_t *sol_f_rawset(sol_state_t *, sol_object_t *);
739 739
 /// Built-in function range
740 740
 sol_object_t *sol_f_range(sol_state_t *, sol_object_t *);
741 741
 /// Built-in function exec
742
-sol_object_t *sol_f_exec(sol_state_t *, sol_object_t *);
742
+//sol_object_t *sol_f_exec(sol_state_t *, sol_object_t *);
743 743
 /// Built-in function eval
744
-sol_object_t *sol_f_eval(sol_state_t *, sol_object_t *);
744
+//sol_object_t *sol_f_eval(sol_state_t *, sol_object_t *);
745 745
 /// Built-in function execfile
746 746
 sol_object_t *sol_f_execfile(sol_state_t *, sol_object_t *);
747 747
 /// Built-in function parse

+ 2
- 2
state.c View File

@@ -263,8 +263,8 @@ int sol_state_init(sol_state_t *state) {
263 263
 	sol_map_borrow_name(state, globals, "rawget", sol_new_cfunc(state, sol_f_rawget, "rawget"));
264 264
 	sol_map_borrow_name(state, globals, "rawset", sol_new_cfunc(state, sol_f_rawset, "rawset"));
265 265
 	sol_map_borrow_name(state, globals, "range", sol_new_cfunc(state, sol_f_range, "range"));
266
-	sol_map_borrow_name(state, globals, "exec", sol_new_cfunc(state, sol_f_exec, "exec"));
267
-	sol_map_borrow_name(state, globals, "eval", sol_new_cfunc(state, sol_f_eval, "eval"));
266
+	//sol_map_borrow_name(state, globals, "exec", sol_new_cfunc(state, sol_f_exec, "exec"));
267
+	//sol_map_borrow_name(state, globals, "eval", sol_new_cfunc(state, sol_f_eval, "eval"));
268 268
 	sol_map_borrow_name(state, globals, "execfile", sol_new_cfunc(state, sol_f_execfile, "execfile"));
269 269
 	sol_map_borrow_name(state, globals, "parse", sol_new_cfunc(state, sol_f_parse, "parse"));
270 270
 	sol_map_borrow_name(state, globals, "ord", sol_new_cfunc(state, sol_f_ord, "ord"));

+ 6
- 0
tests/crasher_apply_noargs.sol View File

@@ -0,0 +1,6 @@
1
+execfile("tests/_lib.sol")
2
+
3
+assert_eq(try(apply)[0], 0, "apply with no args causes error")
4
+assert_eq(try(apply, print, 7)[0], 0, "apply with non-list (int) causes error")
5
+assert_eq(try(apply, print, {})[0], 0, "apply with non-list (map) causes error")
6
+assert_eq(try(apply, print, "foo")[0], 0, "apply with non-list (strbuf) causes error")

+ 5
- 0
tests/crasher_range_noargs.sol View File

@@ -0,0 +1,5 @@
1
+execfile('tests/_lib.sol')
2
+
3
+assert_eq(try(range)[0], 0, "range with no args causes error")
4
+assert_eq(try(range, [])[0], 0, "range with list arg causes error")
5
+assert_eq(try(range, {})[0], 0, "range with map arg causes error")

+ 19
- 8
tokenizer.lex View File

@@ -8,7 +8,7 @@
8 8
 #include <string.h>
9 9
 #include <stdio.h>
10 10
 
11
-void yyerror(YYLTYPE *, char *);
11
+void yyerror(YYLTYPE *, stmt_node **, char *);
12 12
 int yywrap(void);
13 13
 
14 14
 char *str, *curptr;
@@ -36,11 +36,11 @@ void str_putc(char c) {
36 36
 /* Many thanks to hugomg and David Elson! */
37 37
 
38 38
 static void update_loc(YYLTYPE *yylloc, char *yytext){
39
-  static int curr_line = 1;
40
-  static int curr_col  = 1;
39
+ int curr_line;
40
+ int curr_col;
41 41
 
42
-  yylloc->first_line   = curr_line;
43
-  yylloc->first_column = curr_col;
42
+  curr_line = yylloc->first_line   = yylloc->last_line;
43
+  curr_col = yylloc->first_column = yylloc->last_column;
44 44
 
45 45
   {char * s; for(s = yytext; *s != '\0'; s++){
46 46
     if(*s == '\n'){
@@ -235,9 +235,12 @@ int yywrap(void) {
235 235
 	return 1;
236 236
 }
237 237
 
238
-void yyerror(YYLTYPE *locp, char *err) {
239
-	puts(err);
240
-	printf("(at lines %d-%d, cols %d-%d)\n", locp->first_line, locp->last_line, locp->first_column, locp->last_column);
238
+void yyerror(YYLTYPE *locp, stmt_node **prog, char *err) {
239
+	fputs(err, stderr);
240
+	fprintf(stderr, "\n(at lines %d-%d, cols %d-%d)\n", locp->first_line, locp->last_line, locp->first_column, locp->last_column);
241
+	if(prog && *prog) {
242
+		fprintf(stderr, "(while building a stmt of type %d)\n", (*prog)->type);
243
+	}
241 244
 }
242 245
 
243 246
 stmt_node *sol_compile(const char *prgstr) {
@@ -248,6 +251,14 @@ stmt_node *sol_compile(const char *prgstr) {
248 251
     return program;
249 252
 }
250 253
 
254
+stmt_node *sol_compile_buffer(const char *prgbuf, size_t sz) {
255
+	stmt_node *program = NULL;
256
+	YY_BUFFER_STATE buf = yy_scan_bytes(prgbuf, sz);
257
+	yyparse(&program);
258
+	yy_delete_buffer(buf);
259
+	return program;
260
+}
261
+
251 262
 stmt_node *sol_compile_file(FILE *prgfile) {
252 263
     stmt_node *program = NULL;
253 264
     YY_BUFFER_STATE buf = yy_create_buffer(prgfile, YY_BUF_SIZE);

Loading…
Cancel
Save