Example #1
0
 private void _if(NakoNodeIf node)
 {
     int labelId = GetLableId();
     // (1) 条件文をコードにする
     Write_r(node.nodeCond);
     // (2) コードの結果により分岐する
     // 分岐先をラベルとして作成
      NakoILCode label_endif = createLABEL("ENDIF" + labelId.ToString());
      NakoILCode label_else = createLABEL("ELSE" + labelId.ToString());
     result.Add(new NakoILCode(NakoILType.BRANCH_FALSE, label_else));
     // (3) TRUE
     if (node.nodeTrue != null)
     {
         Write_r(node.nodeTrue);
         result.Add(createJUMP(label_endif));
     }
     // (4) FALSE
     result.Add(label_else);
     if (node.nodeFalse != null)
     {
         Write_r(node.nodeFalse);
     }
     result.Add(label_endif);
 }
Example #2
0
        //> _if_stmt : IF _value THEN [EOL] _scope_or_statement [ ELSE _scope_or_statement ]
        //>          ;
        private bool _if_stmt()
        {
            if (!Accept(NakoTokenType.IF)) return false;
            tok.MoveNext(); // skip IF

            NakoNodeIf ifnode = new NakoNodeIf();

            // _value
            NakoToken t = tok.CurrentToken;
            if (!_value())
            {
                throw new NakoParserException("もし文で比較式がありません。", t);
            }
            ifnode.nodeCond = calcStack.Pop();

            // THEN (日本語では、助詞なのでたぶんないはずだが...)
            if (Accept(NakoTokenType.THEN)) tok.MoveNext();
            while (Accept(NakoTokenType.EOL)) tok.MoveNext();

            // TRUE
            ifnode.nodeTrue = _scope_or_statement();
            while (Accept(NakoTokenType.EOL)) tok.MoveNext();

            // FALSE
            if (Accept(NakoTokenType.ELSE))
            {
                tok.MoveNext(); // skip ELSE
                while (Accept(NakoTokenType.EOL)) tok.MoveNext();
                ifnode.nodeFalse = _scope_or_statement();
            }
            this.parentNode.AddChild(ifnode);
            this.lastNode = ifnode;
            return true;
        }