コード例 #1
0
        private void ProcessTerm(NodeBase term)
        {
            Expect(term, NodeType.Term);
            Queue <NodeBase> children = GetChildren(term);

            if (PeekValue(children) == "(")
            {
                Expect(children.Dequeue(), NodeType.Symbol, "(");
                ProcessExpression(children.Dequeue());
                Expect(children.Dequeue(), NodeType.Symbol, ")");
            }
            else
            {
                NodeType?type = PeekType(children);
                switch (type)
                {
                case NodeType.IntegerConstant:
                    vmWriter.PushConstant(GetValue(children.Dequeue()));
                    break;

                case NodeType.StringConstant:
                    vmWriter.PushStringConstant(GetValue(children.Dequeue()));
                    break;

                case NodeType.Keyword:
                    string keywordValue = GetValue(children.Dequeue());
                    switch (keywordValue)
                    {
                    case "true":
                        vmWriter.PushTrue();
                        break;

                    case "false":
                        vmWriter.PushFalse();
                        break;

                    case "this":
                        vmWriter.PushThis();
                        break;

                    case "null":
                        vmWriter.PushConstant("0");
                        break;

                    default:
                        throw GenerateNotImplementedException(keywordValue);
                    }
                    break;

                case NodeType.Symbol:
                    string unaryOp = Expect(children.Dequeue(), NodeType.Symbol);
                    ProcessTerm(children.Dequeue());
                    vmWriter.Unary(unaryOp);
                    break;

                case NodeType.Identifier:
                    Identifier identifier = GetIdentifier(children.Dequeue());
                    if (PeekValue(children) == "." || PeekValue(children) == "(")
                    {
                        ProcessSubroutineCall(children, identifier);
                    }
                    else if (PeekValue(children) == "[")
                    {
                        Expect(children.Dequeue(), NodeType.Symbol, "[");
                        ProcessExpression(children.Dequeue());
                        vmWriter.Push(identifier);
                        vmWriter.AccessArray();
                    }
                    else
                    {
                        vmWriter.Push(identifier);
                    }
                    break;

                default:
                    throw GenerateNotImplementedException(type.ToString());
                }
            }
        }