-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharity-errors.scm
102 lines (90 loc) · 2.27 KB
/
arity-errors.scm
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
(import (scheme repl))
; ------------------------------------------------
; you can't handle compile-time arity errors, like:
; 1. direct invalid primop calls
; - (cons 1), (car), (clock 1 2 3 4), etc.
; 2. wrong "let" recursions
; - (let loop ((x 1)) (loop 1 2 3 4))
; 3. direct lambda calls
; - ((lambda (x) x) 1 2 3)
; - ((lambda (x y . z) x) 1)
; but you can handle runtime arity errors
; - wrong "let" recursions
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(let loop ((x 1))
(define q loop)
(q 1 2 3))
))
; - direct lambda calls
(define f (lambda (x) 777))
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(f 1 2 3)
))
; - case-lambdas
(define g (case-lambda
((a) 1)
((a b) 2)
((a b c d . e) 4) ))
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(g 1 2 3)
))
; - olvm critical errors (error class is 'crash)
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
; don't repeat, this is a dirty hack!
((vm:cast (bytevector 0) type-bytecode) 1 2 3)
))
; - manual errors
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(raise "hello, i'm error!")
))
; - syscalls
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(syscall)
))
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(syscall 0 1 2 3 4 5 6 7 8 9)
))
; - some math (case-lambda too)
(with-exception-handler
(lambda (x)
(print "error detected. " x)
(if (error-object? x)
(print " " (error-object-message x))))
(lambda ()
(/)
))