-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.rkt
101 lines (85 loc) · 2.03 KB
/
exceptions.rkt
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
#lang racket
(require (lib "eopl.ss" "eopl"))
(require "./utils.rkt")
(define-datatype exception exception?
(new-exception (type symbol?) (message string?) (trace-back return-true))
)
(define (exception->exception-type e)
(cases exception e
(new-exception (type message trace-back) type)
)
)
(define (exception->message e)
(cases exception e
(new-exception (type message trace-back) message)
)
)
(define (exception->trace-back e)
(cases exception e
(new-exception (type message trace-back) trace-back)
)
)
(define TypeError 'TypeError)
(define NameError 'NameError)
(define SyntaxError 'SyntaxError)
(define (raise e)
(eopl:error
(exception->exception-type e)
(string-join (list (~v (exception->trace-back e)) (exception->message e)) "\n")
)
)
(define (report-not-pair iter_list trace-back)
(raise
(new-exception
TypeError
(string-join (list (~v iter_list) "object is not iterable" ))
trace-back
)
)
)
(define (report-not-boolean cond-val trace-back)
(raise
(new-exception
TypeError
(string-join (list (~v cond-val) "object is not a bool type") " ")
trace-back
)
)
)
(define (report-not-found trace-back var)
(raise
(new-exception
NameError
(string-join (list "name" (~v var) "is not defined" ) " ")
trace-back
)
)
)
(define (report-not-global trace-back var)
(raise
(new-exception
SyntaxError
(string-join (list "name" (~v var) "is assigned to before global declaration" ) " ")
trace-back
)
)
)
(define (report-break-outside-loop trace-back)
(raise
(new-exception
SyntaxError
"'break' outside loop"
trace-back
)
)
)
(define (report-continue-outside-loop trace-back)
(raise
(new-exception
SyntaxError
"'break' outside loop"
trace-back
)
)
)
(provide (all-defined-out))