Beispiel #1
0
    public override AstNode ShallowClone()
    {
        var res = new AstArray(Source, Start, End);

        res.Elements.AddRange(Elements);
        return(res);
    }
Beispiel #2
0
    public static AstNode ToAst(object?o)
    {
        if (o is AstNode node)
        {
            return(node);
        }
        if (o is string str)
        {
            return(new AstString(str));
        }
        if (o is double num)
        {
            if (double.IsNaN(num))
            {
                return(AstNaN.Instance);
            }
            if (double.IsPositiveInfinity(num))
            {
                return(AstInfinity.Instance);
            }
            if (double.IsNegativeInfinity(num))
            {
                return(AstInfinity.NegativeInstance);
            }
            return(new AstNumber(num));
        }

        if (o is int i)
        {
            return(new AstNumber(i));
        }

        if (o is uint u)
        {
            return(new AstNumber(u));
        }

        if (o is bool b)
        {
            return(b ? AstTrue.Instance : (AstNode)AstFalse.Instance);
        }
        if (o is Dictionary <object, object> dict)
        {
            var res = new AstObject();
            foreach (var pair in dict)
            {
                res.Properties.Add(new AstObjectKeyVal(ToAst(pair.Key), ToAst(pair.Value)));
            }

            return(res);
        }

        if (o is IList <object> list)
        {
            var res = new AstArray();
            foreach (var item in list)
            {
                res.Elements.Add(ToAst(item));
            }

            return(res);
        }

        throw new NotImplementedException();
    }