Example #1
0
 public static ElaValue FromObject(object value)
 {
     if (value == null)
     {
         return(new ElaValue(ElaUnit.Instance));
     }
     else if (value is ElaObject)
     {
         return(new ElaValue((ElaObject)value));
     }
     else if (value is Int32)
     {
         return(new ElaValue((Int32)value));
     }
     else if (value is Int64)
     {
         return(new ElaValue((Int64)value));
     }
     else if (value is Single)
     {
         return(new ElaValue((Single)value));
     }
     else if (value is Double)
     {
         return(new ElaValue((Double)value));
     }
     else if (value is Boolean)
     {
         return(new ElaValue((Boolean)value));
     }
     else if (value is Char)
     {
         return(new ElaValue((Char)value));
     }
     else if (value is String)
     {
         return(new ElaValue((String)value));
     }
     else if (value is ElaValue)
     {
         return((ElaValue)value);
     }
     else if (value is IEnumerable)
     {
         return(new ElaValue(ElaList.FromEnumerable((IEnumerable)value)));
     }
     else if (value is Delegate)
     {
         return(new ElaValue(new DynamicDelegateFunction("<f>", (Delegate)value)));
     }
     else
     {
         throw new InvalidCastException(String.Format("Type '{0}' cannot be converted to Ela runtime value.", value.GetType().FullName));
     }
 }
Example #2
0
        public ElaList AssemblyModules(ElaModule mod)
        {
            var vm   = mod.GetCurrentMachine();
            var asm  = vm.Assembly;
            var list = new List <ElaValue>();

            for (var i = 0; i < asm.ModuleCount; i++)
            {
                list.Add(new ElaValue(new ElaModule(i, vm)));
            }

            return(ElaList.FromEnumerable(list));
        }
Example #3
0
        private string PrintValue(ElaValue value)
        {
            try
            {
                var result = String.Empty;
                var hdl    = new AutoResetEvent(false);
                var th     = new Thread(() =>
                {
                    var obj = value.AsObject();

                    if (obj is ElaLazy)
                    {
                        result = ((ElaLazy)obj).Force().AsObject().ToString();
                    }
                    else if (obj is ElaLazyList)
                    {
                        var lalist = (ElaLazyList)obj;
                        result     = ElaList.FromEnumerable(lalist.Take(20)).ToString() + " (lazy)";
                    }
                    else
                    {
                        result = value.ToString();
                    }

                    hdl.Set();
                });
                th.Start();

                if (!hdl.WaitOne(500))
                {
                    th.Abort();
                    result = "<evaluation timeout>";
                }

                if (result == null)
                {
                    return("_|_");
                }
                else if (result.Trim().Length == 0)
                {
                    result = "[" + value.GetTypeName() + "]";
                }

                return(result);
            }
            catch (Exception)
            {
                return("<evaluation error>");
            }
        }
Example #4
0
        private ElaList BuildList(object[] array)
        {
            var vals = new List <ElaValue>();

            foreach (var o in array)
            {
                if (o is Dictionary <String, Object> )
                {
                    vals.Add(new ElaValue(BuildRecord((Dictionary <String, Object>)o)));
                }
                else
                {
                    vals.Add(ElaValue.FromObject(o));
                }
            }

            return(ElaList.FromEnumerable(vals));
        }
Example #5
0
        private ElaRecord ReadNode(XmlReader xr)
        {
            var list = new List <ElaRecordField>();
            var sxr  = xr.ReadSubtree();

            sxr.Read();

            if (sxr.AttributeCount > 0)
            {
                for (var i = 0; i < sxr.AttributeCount; i++)
                {
                    sxr.MoveToAttribute(i);
                    list.Add(new ElaRecordField(sxr.Name, new ElaValue(sxr.Value)));
                }
            }

            var clist = new List <ElaRecord>();
            var sb    = new StringBuilder();

            while (sxr.Read())
            {
                if (sxr.NodeType == XmlNodeType.Element)
                {
                    clist.Add(new ElaRecord(new ElaRecordField(sxr.Name, ReadNode(sxr))));
                }
                if (sxr.NodeType == XmlNodeType.Text || sxr.NodeType == XmlNodeType.CDATA)
                {
                    sb.Append(sxr.ReadString());
                }
            }

            if (clist.Count > 0)
            {
                list.Add(new ElaRecordField("'children", ElaList.FromEnumerable(clist)));
            }

            if (sb.Length > 0)
            {
                list.Add(new ElaRecordField("'value", sb.ToString()));
            }

            return(new ElaRecord(list.ToArray()));
        }
Example #6
0
 public ElaList ModuleReferences(ElaModule mod)
 {
     return(ElaList.FromEnumerable(mod.GetReferences()));
 }
Example #7
0
 public ElaList ModuleNames(ElaModule mod)
 {
     return(ElaList.FromEnumerable(mod.GetVariables()));
 }
Example #8
0
 public ElaList FunAppliedArgs(ElaFunction fun)
 {
     return(ElaList.FromEnumerable(fun.GetAppliedArguments()));
 }
Example #9
0
 public ElaList GetFields(ElaRecord rec)
 {
     return(ElaList.FromEnumerable(rec.GetKeys()));
 }
Example #10
0
        public static ElaObject GetFormats(string format)
        {
            try
            {
                var fmt    = 0;
                var sb     = new StringBuilder();
                var fsb    = default(StringBuilder);
                var buffer = format.ToCharArray();
                var len    = buffer.Length;
                var dict   = new Dictionary <Int32, List <String> >();
                var num    = String.Empty;

                for (var i = 0; i < len; i++)
                {
                    var c   = buffer[i];
                    var end = i == len - 1;

                    if (fmt > 0 && c == '}' && (end || buffer[i + 1] != '}'))
                    {
                        var i4   = Int32.Parse(num);
                        var list = default(List <String>);

                        if (!dict.TryGetValue(i4, out list))
                        {
                            list = new List <String>();
                            dict.Add(i4, list);
                        }

                        sb.Append(num + ":" + list.Count);
                        sb.Append(c);
                        list.Add((fsb ?? empty).ToString());
                        num = String.Empty;
                        fsb = null;
                        fmt = 0;
                    }
                    else if (fmt == 1 && c == ':')
                    {
                        fsb = new StringBuilder();
                        fmt = 2;
                    }
                    else if (fmt == 1)
                    {
                        num += c;
                    }
                    else if (fmt == 2)
                    {
                        fsb.Append(c);
                    }
                    else if (fmt == 0 && c == '{' && !end && buffer[i + 1] != '{' && (i == 0 || buffer[i - 1] != '{'))
                    {
                        sb.Append(c);
                        fmt = 1;
                    }
                    else
                    {
                        sb.Append(c);
                    }
                }

                var shift = 0;

                for (var n = 0; n < dict.Count; n++)
                {
                    var val = dict[n];

                    for (var i = 0; i < val.Count; i++)
                    {
                        if (i > 0)
                        {
                            shift++;
                        }

                        var mask = "{" + n + ":" + i + "}";
                        sb.Replace(mask, "{" + (n + shift) + "}");
                    }
                }

                var lst = new ElaList[dict.Count];

                foreach (var kv in dict)
                {
                    lst[kv.Key] = ElaList.FromEnumerable(kv.Value);
                }

                var elaStr  = new ElaValue(sb.ToString());
                var elaList = new ElaValue(ElaList.FromEnumerable(lst));
                return(new ElaTuple(elaStr, elaList));
            }
            catch (Exception)
            {
                return(ElaUnit.Instance);
            }
        }
Example #11
0
        private ElaValue Read(ElaExpression exp, string str)
        {
            switch (exp.Type)
            {
            case ElaNodeType.ListLiteral:
            {
                var n   = (ElaListLiteral)exp;
                var arr = new ElaValue[n.Values.Count];

                for (var i = 0; i < arr.Length; i++)
                {
                    arr[i] = Read(n.Values[i], str);
                }

                return(new ElaValue(ElaList.FromEnumerable(arr)));
            }

            case ElaNodeType.TupleLiteral:
            {
                var n   = (ElaTupleLiteral)exp;
                var arr = new ElaValue[n.Parameters.Count];

                for (var i = 0; i < arr.Length; i++)
                {
                    arr[i] = Read(n.Parameters[i], str);
                }

                return(new ElaValue(new ElaTuple(arr)));
            }

            case ElaNodeType.RecordLiteral:
            {
                var n   = (ElaRecordLiteral)exp;
                var arr = new ElaRecordField[n.Fields.Count];

                for (var i = 0; i < arr.Length; i++)
                {
                    var f = n.Fields[i];
                    arr[i] = new ElaRecordField(f.FieldName, Read(f.FieldValue, str));
                }

                return(new ElaValue(new ElaRecord(arr)));
            }

            case ElaNodeType.Primitive:
            {
                var n = (ElaPrimitive)exp;

                switch (n.Value.LiteralType)
                {
                case ElaTypeCode.Integer:
                    return(new ElaValue(n.Value.AsInteger()));

                case ElaTypeCode.Single:
                    return(new ElaValue(n.Value.AsReal()));

                case ElaTypeCode.Double:
                    return(new ElaValue(n.Value.AsDouble()));

                case ElaTypeCode.Long:
                    return(new ElaValue(n.Value.AsLong()));

                case ElaTypeCode.Char:
                    return(new ElaValue(n.Value.AsChar()));

                case ElaTypeCode.Boolean:
                    return(new ElaValue(n.Value.AsBoolean()));

                case ElaTypeCode.String:
                    return(new ElaValue(n.Value.AsString()));

                default:
                    throw Fail(str);
                }
            }

            case ElaNodeType.UnitLiteral:
                return(new ElaValue(ElaUnit.Instance));

            default:
                throw Fail(str);
            }
        }