Beispiel #1
0
        /// <summary>
        /// Output atom values
        /// </summary>
        private static void EmitLeafNode(Node root, bool debug, Scope parameterNames, Context compileContext, NanCodeWriter wr)
        {
            double leafValue  = 0;
            bool   substitute = false;
            var    valueName  = root.Text;
            var    nameHash   = NanTags.GetCrushedName(valueName);

            if (parameterNames.CanResolve(nameHash))
            {
                substitute = true;
                leafValue  = parameterNames.Resolve(nameHash);
            }

            // An unwrapped variable name?
            if (IsUnwrappedIdentifier(valueName, root, compileContext))
            {
                if (debug)
                {
                    wr.Comment("// treating '" + valueName + "' as an implicit get()");
                }
                if (substitute)
                {
                    wr.Memory('g', leafValue);
                }
                else
                {
                    wr.Memory('g', valueName, 0);
                }

                return;
            }

            if (debug)
            {
                wr.Comment("// Value : \"" + root + "\"\r\n");
                if (substitute)
                {
                    wr.Comment("// Parameter reference redefined as '" + valueName + "'\r\n");
                }
            }

            switch (root.NodeType)
            {
            case NodeType.Numeric:
                wr.LiteralNumber(double.Parse(valueName.Replace("_", "")));
                break;

            case NodeType.StringLiteral:
                wr.LiteralString(valueName);
                break;

            case NodeType.Atom:
                if (valueName == "true")
                {
                    wr.LiteralInt32(-1);
                }
                else if (valueName == "false")
                {
                    wr.LiteralInt32(0);
                }
                else if (substitute)
                {
                    wr.RawToken(leafValue);
                }
                else
                {
                    wr.VariableReference(valueName);
                }
                break;

            default:
                throw new Exception("Unexpected compiler state");
            }
        }