private void newCalc(NakoNodeCalc node) { NakoILCode c = new NakoILCode(); // Write_r(node.nodeL); Write_r(node.nodeR); // switch (node.calc_type) { case CalcType.NOP: c.type = NakoILType.NOP; break; // ( ... ) case CalcType.ADD: c.type = NakoILType.ADD; break; case CalcType.SUB: c.type = NakoILType.SUB; break; case CalcType.MUL: c.type = NakoILType.MUL; break; case CalcType.DIV: c.type = NakoILType.DIV; break; case CalcType.MOD: c.type = NakoILType.MOD; break; case CalcType.ADD_STR: c.type = NakoILType.ADD_STR; break; case CalcType.POWER: c.type = NakoILType.POWER; break; case CalcType.EQ: c.type = NakoILType.EQ; break; case CalcType.NOT_EQ: c.type = NakoILType.NOT_EQ; break; case CalcType.GT: c.type = NakoILType.GT; break; case CalcType.GT_EQ: c.type = NakoILType.GT_EQ; break; case CalcType.LT: c.type = NakoILType.LT; break; case CalcType.LT_EQ: c.type = NakoILType.LT_EQ; break; case CalcType.AND: c.type = NakoILType.AND; break; case CalcType.OR: c.type = NakoILType.OR; break; case CalcType.XOR: c.type = NakoILType.XOR; break; case CalcType.NEG: c.type = NakoILType.NEG; break; } result.Add(c); }
private bool _minus_flag() { tok.MoveNext(); if (Accept(NakoTokenType.INT)||Accept(NakoTokenType.NUMBER)) { _const(); NakoNodeConst c = (NakoNodeConst)calcStack.Pop(); if (c.value is long) { c.value = ((long)(c.value) * -1); } else { c.value = ((double)(c.value) * -1); } calcStack.Push(c); lastNode = c; return true; } NakoNodeCalc nc = new NakoNodeCalc(); nc.calc_type = CalcType.MUL; NakoNodeConst m1 = new NakoNodeConst(); m1.value = -1; m1.type = NakoNodeType.INT; NakoNode v = calcStack.Pop(); nc.nodeL = v; nc.nodeR = m1; calcStack.Push(nc); return true; }
//> _calc_term : _calc_expr { (PLUS|MINUS) _calc_expr } //> ; private bool _calc_term() { if (!_calc_expr()) { return false; } while (Accept(NakoTokenType.PLUS) || Accept(NakoTokenType.MINUS) || Accept(NakoTokenType.AND)) { NakoNodeCalc node = new NakoNodeCalc(); switch (tok.CurrentTokenType) { case NakoTokenType.PLUS: node.calc_type = CalcType.ADD; break; case NakoTokenType.MINUS: node.calc_type = CalcType.SUB; break; case NakoTokenType.AND: node.calc_type = CalcType.ADD_STR; break; default: throw new ApplicationException("System Error"); } TokenTry(); tok.MoveNext(); if (!_calc_expr()) { TokenBack(); return false; } TokenFinally(); node.nodeR = calcStack.Pop(); node.nodeL = calcStack.Pop(); calcStack.Push(node); lastNode = node; } return true; }
//> _calc_power : _calc_formula { POWER _calc_formula } //> ; private bool _calc_power() { if (!_calc_formula()) { return false; } while (Accept(NakoTokenType.POWER)) { TokenTry(); tok.MoveNext(); if (!_calc_formula()) { TokenBack(); return false; } TokenFinally(); NakoNodeCalc node = new NakoNodeCalc(); node.calc_type = CalcType.POWER; node.nodeR = calcStack.Pop(); node.nodeL = calcStack.Pop(); lastNode = node; calcStack.Push(node); } return true; }
//> _calc_expr : _calc_power { (MUL|DIV|MOD) _calc_power } //> ; private bool _calc_expr() { if (!_calc_power()) { return false; } while (Accept(NakoTokenType.MUL) || Accept(NakoTokenType.DIV) || Accept(NakoTokenType.MOD)) { NakoNodeCalc node = new NakoNodeCalc(); switch (tok.CurrentTokenType) { case NakoTokenType.MUL: node.calc_type = CalcType.MUL; break; case NakoTokenType.DIV: node.calc_type = CalcType.DIV; break; case NakoTokenType.MOD: node.calc_type = CalcType.MOD; break; } TokenTry(); tok.MoveNext(); if (!_calc_power()) { TokenBack(); return false; } TokenFinally(); node.nodeR = calcStack.Pop(); node.nodeL = calcStack.Pop(); calcStack.Push(node); lastNode = node; } return true; }
//> _calc_comp : _calc_term { (GT|GT_EQ|LT|LT_EQ|EQ|EQ_EQ|NOT_EQ) _calc_term } //> ; private bool _calc_comp() { if (!_calc_term()) { return false; } // パーサーの例外的な処理 // →代入文の変数取得では、比較文の "=" は無効にし、代入の "=" と見なすこと。 if (flag_set_variable && Accept(NakoTokenType.EQ)) { return true; // "="以降は読まない } while ( Accept(NakoTokenType.GT) || Accept(NakoTokenType.LT) || Accept(NakoTokenType.GT_EQ) || Accept(NakoTokenType.LT_EQ) || Accept(NakoTokenType.EQ) || Accept(NakoTokenType.EQ_EQ) || Accept(NakoTokenType.NOT_EQ) ) { NakoNodeCalc node = new NakoNodeCalc(); switch (tok.CurrentToken.Type) { case NakoTokenType.GT: node.calc_type = CalcType.GT; break; case NakoTokenType.LT: node.calc_type = CalcType.LT; break; case NakoTokenType.GT_EQ: node.calc_type = CalcType.GT_EQ; break; case NakoTokenType.LT_EQ: node.calc_type = CalcType.LT_EQ; break; case NakoTokenType.EQ: node.calc_type = CalcType.EQ; break; case NakoTokenType.EQ_EQ: node.calc_type = CalcType.EQ; break; case NakoTokenType.NOT_EQ: node.calc_type = CalcType.NOT_EQ; break; default: throw new ApplicationException("[Nako System Error] Operator not set."); } TokenTry(); tok.MoveNext(); if (!_calc_term()) { TokenBack(); return false; } TokenFinally(); node.nodeR = calcStack.Pop(); node.nodeL = calcStack.Pop(); calcStack.Push(node); lastNode = node; } return true; }