-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.d
108 lines (78 loc) · 2.13 KB
/
exceptions.d
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
103
104
105
106
107
108
module exceptions;
import utils, common;
@safe nothrow:
interface IExceptionPrinter
{
nothrow void print (Except ex);
}
struct Except
{
dstring name;
dstring text;
}
final class ExceptionPrinter : IExceptionPrinter
{
nothrow:
private IPrinter printer;
this (IPrinter printer) { this.printer = printer; }
void print (Except ex)
{
printer.print("Exception: [");
printer.print(ex.name);
printer.print("] ");
printer.println(ex.text);
}
}
final class ExceptionCollector : IExceptionPrinter
{
nothrow:
Except[] exceptions;
void print (Except ex) { exceptions ~= ex; }
void clear () { exceptions = null; }
}
final class InterpreterException : Exception
{
nothrow:
this (string msg) { super (msg); }
}
final class Thrower
{
private IExceptionPrinter ep;
EvalStrategy evalStrategy;
nothrow this (IExceptionPrinter ep) { this.ep = ep; }
private void e(string name, dstring text)
{
auto lastName = lastItemInList(name, '.');
ep.print(Except(lastName.toDString(), text));
if (evalStrategy == EvalStrategy.throwing)
throw new InterpreterException("[" ~ lastName ~ "] " ~ text.toString());
}
void cannotEvalError ()
{
e(__FUNCTION__, "Expression or its part has and error");
}
void cannotEvalMissing ()
{
e(__FUNCTION__, "Expression or its part is missing");
}
void integerOwerflow ()
{
e(__FUNCTION__, "Integer overflowed over maximum value");
}
void integerUnderflow ()
{
e(__FUNCTION__, "Integer underflowed over minimum value");
}
void opeatorIsUndefined (dstring op)
{
e(__FUNCTION__, "Operator '" ~ op ~ "' is undefined.");
}
void undefinedVariable (dstring op)
{
e(__FUNCTION__, "Variable '" ~ op ~ "' is undefined.");
}
void cannotLoadFile (dstring path, Exception ex)
{
e(__FUNCTION__, "Failed to load file '" ~ path ~ "': " ~ ex.msg.toDString());
}
}