From e03ce4c4a77ef07f7a3fe8eef45c6b19e97a2c45 Mon Sep 17 00:00:00 2001 From: otya128 Date: Tue, 6 Sep 2016 00:09:34 +0900 Subject: [PATCH] =?UTF-8?q?=E9=A0=85=E3=81=8C=E3=81=99=E3=81=B9=E3=81=A6?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E3=81=AE=E5=BC=8F=E3=82=92=E5=AE=9F=E8=A1=8C?= =?UTF-8?q?=E5=89=8D=E3=81=AB=E8=A8=88=E7=AE=97=E3=82=92=E6=B8=88=E3=81=BE?= =?UTF-8?q?=E3=81=9B=E3=81=A6=E5=AE=9F=E8=A1=8C=E6=99=82=E3=81=AB=E3=81=AF?= =?UTF-8?q?1=E5=80=8B=E3=81=AE=E5=AE=9A=E6=95=B0=E3=81=A8=E3=81=97?= =?UTF-8?q?=E3=81=A6=E6=89=B1=E3=81=86=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/parser.d | 61 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/SMILEBASIC/parser.d b/SMILEBASIC/parser.d index 7c98aaf..3335f98 100644 --- a/SMILEBASIC/parser.d +++ b/SMILEBASIC/parser.d @@ -1624,6 +1624,50 @@ class Parser } return print; } + //compilerでやるべきな気がする + bool constcalc(Value left, Value right, TokenType type, out double result) + { + switch (type) + { + case TokenType.Plus: + result = (left.castDouble + right.castDouble); + return true; + case TokenType.Minus: + result = (left.castDouble - right.castDouble); + return true; + case TokenType.Mul: + result = (left.castDouble * right.castDouble); + return true; + case TokenType.Div: + result = (left.castDouble / right.castDouble); + return true; + case TokenType.IntDiv: + //TODO:範囲外だとOverflow + result = (left.castInteger / right.castInteger); + return true; + case TokenType.Mod: + result = (left.castDouble % right.castDouble); + return true; + case TokenType.And: + result = (left.castInteger & right.castInteger); + return true; + case TokenType.Or: + result = (left.castInteger | right.castInteger); + return true; + case TokenType.Xor: + result = (left.castInteger ^ right.castInteger); + return true; + case TokenType.LeftShift: + result = (left.castInteger << right.castInteger); + return true; + case TokenType.RightShift: + result = (left.castInteger >> right.castInteger); + return true; + default: + result = double.init; + return !true; + } + } Expression expression() { Expression node = null; @@ -1657,11 +1701,26 @@ class Parser op.item2 = term(order - 1, node); } token = lex.front(); + bool constexpr; + Expression op__ = op; + if (op.item1.type == NodeType.Constant && op.item2.type == NodeType.Constant) + { + double result; + if (constcalc((cast(Constant)op.item1).value, (cast(Constant)op.item2).value, op.operator, result)) + { + op__ = new Constant(Value(result), lex.location); + constexpr = true; + if(order != getOPRank(token.type)) + return op__; + op = new BinaryOperator(op__, lex.location); + continue; + } + } version(none) write(tt, " "); if(order == getOPRank(token.type)) { - BinaryOperator op2 = new BinaryOperator(op, lex.location); + BinaryOperator op2 = new BinaryOperator(op__, lex.location); op.item1 = op2; } version(none)stdout.flush();