File tree Expand file tree Collapse file tree 2 files changed +18
-9
lines changed Expand file tree Collapse file tree 2 files changed +18
-9
lines changed Original file line number Diff line number Diff line change @@ -73,13 +73,18 @@ class OperatorExpression(string op) : Expression
73
73
}
74
74
}
75
75
76
- private enum str = " left.evaluate()" ~ op ~ " right.evaluate()" ;
77
76
override real evaluate () {
78
- return mixin (str);
77
+ const rhs = right.evaluate();
78
+ static if (op == " /" ) {
79
+ if (rhs == 0 ) {
80
+ throw new ParseException(" Devide by zero" );
81
+ }
82
+ }
83
+ return mixin (" left.evaluate()" ~ op ~ " rhs" );
79
84
}
80
85
81
86
override string toString () @safe const {
82
- return str ;
87
+ return left.toString() ~ op ~ right.toString() ;
83
88
}
84
89
}
85
90
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ private:
35
35
36
36
public :
37
37
Token [] input;
38
+ private static const syntaxError = new ParseException(" Syntax error" );
38
39
39
40
this () {
40
41
lexer = new Lexer();
@@ -83,15 +84,14 @@ public:
83
84
}
84
85
85
86
if (start && input.length > 0 && input.front().type != TokenType.EOL ) {
86
- input.length = 0 ;
87
- throw new ParseException(" Syntax error" );
87
+ error();
88
88
}
89
89
return left;
90
90
91
91
} else {
92
- input.length = 0 ;
93
- throw new ParseException(" Syntax error" );
92
+ error();
94
93
}
94
+ assert (false );
95
95
}
96
96
97
97
private Precedence getPrecedence () {
@@ -106,14 +106,18 @@ public:
106
106
} else if (t == TokenType.EOL ) {
107
107
return Precedence.START ;
108
108
} else {
109
- input.length = 0 ;
110
- throw new ParseException(" Invalid syntax" );
109
+ error();
111
110
}
112
111
}
113
112
114
113
return Precedence.START ;
115
114
}
116
115
116
+ private void error () {
117
+ input.length = 0 ;
118
+ throw syntaxError;
119
+ }
120
+
117
121
void expect (TokenType t) {
118
122
if ((input.length > 0 && input.front().type != t) || input.length == 0 ) {
119
123
import std.format : format;
You can’t perform that action at this time.
0 commit comments