Exemplo n.º 1
0
        void EvaluateFunctionReturn(ParseNode functionNode)
        {
            var parentFunctionDeclaration = functionNode.Parent.States;
            if (parentFunctionDeclaration[0].Token != TokenKind.TypeName)
                return;
            var functionTypeName = parentFunctionDeclaration[0].Content;
            if (functionTypeName == "void")
                return;
            var blockStates = functionNode.States;
            var expectedReturnToken = blockStates[blockStates.Count - 2];
            if (expectedReturnToken.Rule == RuleKind.Return)
                return;
            var returnNode = new ParseNode(RuleKind.Return);

            ParseNode defaultValue;
            switch (functionTypeName)
            {
                case TypeNames.Bool:
                    defaultValue = TokenKind.Identifier.BuildTokenFromId("false");
                    break;
                case TypeNames.Int:
                    defaultValue = TokenKind.Int.BuildTokenFromId("0");
                    break;
                case TypeNames.Double:
                    defaultValue = TokenKind.Float.BuildTokenFromId("0.0");
                    break;
                case TypeNames.String:
                    defaultValue = TokenKind.QuotedString.BuildTokenFromId("\"\"");
                    break;
                case TypeNames.Color:
                    defaultValue = TokenKind.Identifier.BuildTokenFromId("Black");
                    break;
                default:
                    throw new InvalidDataException("case not handled");
            }
            var nodesToAdd = new[]
                {
                    TokenKind.Comment.BuildTokenFromId("// this is added by the translator. Consider it adding in your script "),
                    TokenKind.Return.BuildTokenFromId(),
                    TokenKind.OpenParen.BuildTokenFromId(),
                    defaultValue,
                    TokenKind.CloseParen.BuildTokenFromId(),
                    TokenKind.SemiColon.BuildTokenFromId(),
                };
            returnNode.AddRange(nodesToAdd);
            blockStates.Insert(blockStates.Count - 1, returnNode);
        }
        static void DeclareVariable(ParseNode functionBody, ParseNode typeNameNode, ParseNode variableNameNode)
        {
            var nodesToAdd = new List<ParseNode>
                {
                    typeNameNode.Clone(),
                    TokenKind.Space.BuildTokenFromId(" "),
                    variableNameNode.Clone(),
                    TokenKind.SemiColon.BuildTokenFromId()
                };

            var declarationNode = new ParseNode(RuleKind.DeclareVariable);
            declarationNode.AddRange(nodesToAdd);
            functionBody.Insert(1, declarationNode);
            if (typeNameNode.Parent.Rule == RuleKind.DeclareVariable)
            {
                typeNameNode.Parent.Rule = RuleKind.InstructionCode;
            }
            typeNameNode.RemoveFromParent();
        }
Exemplo n.º 3
0
 public ParseNode BuildTree()
 {
     var result = new ParseNode(RuleKind.Root);
     result.AddRange(Nodes);
     return result;
 }