string GetValue(Constant identEx)
        {
            if (identEx == null) return null;
            if (identEx.Value != null)
                return identEx.Value;
            StringBuilder retVal = new StringBuilder();
            if (identEx is VectorConstant)
            {
                VectorConstant vc = (VectorConstant) identEx;

                retVal.Append(Generate(string.Format("new {0}(", vc.Type), vc));
                retVal.Append(GenerateNode((SYMBOL) vc.kids[0]));
                retVal.Append(Generate(", "));
                retVal.Append(GenerateNode((SYMBOL) vc.kids[1]));
                retVal.Append(Generate(", "));
                retVal.Append(GenerateNode((SYMBOL) vc.kids[2]));
                retVal.Append(Generate(")"));

                return retVal.ToString();
            }
            if (identEx is RotationConstant)
            {
                RotationConstant rc = (RotationConstant) identEx;

                retVal.Append(Generate(string.Format("new {0}(", rc.Type), rc));
                retVal.Append(GenerateNode((SYMBOL) rc.kids[0]));
                retVal.Append(Generate(", "));
                retVal.Append(GenerateNode((SYMBOL) rc.kids[1]));
                retVal.Append(Generate(", "));
                retVal.Append(GenerateNode((SYMBOL) rc.kids[2]));
                retVal.Append(Generate(", "));
                retVal.Append(GenerateNode((SYMBOL) rc.kids[3]));
                retVal.Append(Generate(")"));

                return retVal.ToString();
            }
            if (identEx is ListConstant)
                return GenerateListConstant((ListConstant) identEx);
            return null;
        }
Exemple #2
0
 public ConstantExpression(Parser yyp, Constant c) : base((yyp))
 {
     kids.Add(c);
 }
        /// <summary>
        ///     Generates the code for a Constant node.
        /// </summary>
        /// <param name="c">The Constant node.</param>
        /// <returns>String containing C# code for Constant c.</returns>
        string GenerateConstant(Constant c)
        {
            // Supprt LSL's weird acceptance of floats with no trailing digits
            // after the period. Turn float x = 10.; into float x = 10.0;
            if ("LSL_Types.LSLFloat" == c.Type)
            {
                int dotIndex = c.Value.IndexOf('.') + 1;
                if (0 < dotIndex && (dotIndex == c.Value.Length || !char.IsDigit(c.Value[dotIndex])))
                    c.Value = c.Value.Insert(dotIndex, "0");
                c.Value = "new LSL_Types.LSLFloat(" + c.Value + ")";
            }
            else if ("LSL_Types.LSLInteger" == c.Type)
            {
                c.Value = "new LSL_Types.LSLInteger(" + c.Value + ")";
            }
            else if ("LSL_Types.LSLString" == c.Type)
            {
                c.Value = "new LSL_Types.LSLString(\"" + c.Value + "\")";
            }

            return Generate(c.Value, c);
        }
 /// <summary>
 ///     Generates the node structure required to generate a default
 ///     initialization.
 /// </summary>
 /// <param name="p">
 ///     Tools.Parser instance to use when instantiating nodes.
 /// </param>
 /// <param name="constantType">String describing the datatype.</param>
 /// <returns>
 ///     A SYMBOL node containing the appropriate structure for initializing a
 ///     constantType.
 /// </returns>
 SYMBOL GetZeroConstant(Parser p, string constantType)
 {
     switch (constantType)
     {
         case "integer":
             return new Constant(p, constantType, "0");
         case "float":
             return new Constant(p, constantType, "0.0");
         case "string":
         case "key":
             return new Constant(p, constantType, "");
         case "list":
             ArgumentList al = new ArgumentList(p);
             return new ListConstant(p, al);
         case "vector":
             Constant vca = new Constant(p, "float", "0.0");
             Constant vcb = new Constant(p, "float", "0.0");
             Constant vcc = new Constant(p, "float", "0.0");
             ConstantExpression vcea = new ConstantExpression(p, vca);
             ConstantExpression vceb = new ConstantExpression(p, vcb);
             ConstantExpression vcec = new ConstantExpression(p, vcc);
             return new VectorConstant(p, vcea, vceb, vcec);
         case "rotation":
             Constant rca = new Constant(p, "float", "0.0");
             Constant rcb = new Constant(p, "float", "0.0");
             Constant rcc = new Constant(p, "float", "0.0");
             Constant rcd = new Constant(p, "float", "0.0");
             ConstantExpression rcea = new ConstantExpression(p, rca);
             ConstantExpression rceb = new ConstantExpression(p, rcb);
             ConstantExpression rcec = new ConstantExpression(p, rcc);
             ConstantExpression rced = new ConstantExpression(p, rcd);
             return new RotationConstant(p, rcea, rceb, rcec, rced);
         default:
             return null; // this will probably break stuff
     }
 }