Exemplo n.º 1
0
        public bool Eval(bool measureTime)
        {
#if !DEBUG
            try
#endif
            {
                var dt  = DateTime.Now;
                var res = DyMachine.Execute(ExecutionContext);
                Printer.Output(res);

                if (measureTime)
                {
                    Printer.SupplementaryOutput($"Time taken: {DateTime.Now - dt}");
                }

                return(true);
            }
#if !DEBUG
            catch (DyCodeException ex)
            {
                Printer.Error(ex.ToString());
                return(false);
            }
            catch (DyRuntimeException ex)
            {
                Printer.Error(ex.Message);
                return(false);
            }
            catch (Exception ex)
            {
                Printer.Error($"Critical failure: {Environment.NewLine}{ex.ToString()}");
                return(false);
            }
#endif
        }
Exemplo n.º 2
0
        public bool EvalFile(string fileName, bool measureTime)
        {
            if (!Make(fileName, out var composition))
            {
                return(false);
            }

            if (ExecutionContext == null)
            {
                ExecutionContext = DyMachine.CreateExecutionContext(composition);
            }

            return(Eval(measureTime));
        }
Exemplo n.º 3
0
        public void Dump(object arg)
        {
            Printer.Output("Dump of globals:");

            if (ctx.ExecutionContext == null)
            {
                Printer.Output("...none");
                return;
            }

            foreach (var rv in DyMachine.DumpVariables(ctx.ExecutionContext))
            {
                Printer.Output($"{rv.Name} = {Printer.Format(rv.Value, ctx.ExecutionContext)}");
            }
        }
Exemplo n.º 4
0
    protected override DyObject BindOrRun(ExecutionContext ctx, DyObject arg)
    {
        if (Auto)
        {
            try
            {
                var size   = GetMemoryCells(ctx);
                var locals = size == 0 ? Array.Empty <DyObject>() : new DyObject[size];
                ctx.CallStack.Push(Caller.External);
                return(DyMachine.ExecuteWithData((DyNativeFunction)BindToInstance(ctx, arg), locals, ctx));
            }
            catch (DyCodeException ex)
            {
                ctx.Error = ex.Error;
                return(Nil);
            }
        }

        return(BindToInstance(ctx, arg));
    }
Exemplo n.º 5
0
        public bool Eval(string source)
        {
            var made = Linker.Make(SourceBuffer.FromString(source, "<stdio>"));

            if (made.Messages.Any())
            {
                Printer.PrintErrors(made.Messages);
            }

            if (!made.Success)
            {
                return(false);
            }

            if (ExecutionContext == null)
            {
                ExecutionContext = DyMachine.CreateExecutionContext(made.Value);
            }

            return(Eval(measureTime: false));
        }
Exemplo n.º 6
0
    public void Dump(object _)
    {
        Printer.LineFeed();
        Printer.Output("Dump of globals:");

        if (ctx.RuntimeContext is null)
        {
            Printer.Output("<none>");
            return;
        }

        var xs    = DyMachine.DumpVariables(ctx.RuntimeContext).ToList();
        var vals  = new string[xs.Count];
        var types = new string[xs.Count];

        var(keyLen, valLen) = (0, 0);
        var etx = DyMachine.CreateExecutionContext(ctx.RuntimeContext);

        for (var i = 0; i < xs.Count; i++)
        {
            var rv = xs[i];
            vals[i]  = Printer.Format(rv.Value, etx, notype: true, maxLen: 32);
            types[i] = rv.Value.TypeName;

            if (keyLen < rv.Name.Length)
            {
                keyLen = rv.Name.Length;
            }
            if (valLen < vals[i].Length)
            {
                valLen = vals[i].Length;
            }
        }

        for (var i = 0; i < xs.Count; i++)
        {
            var rv = xs[i];
            Printer.Output($"{rv.Name}{new string(' ', keyLen - rv.Name.Length)} | {vals[i]}{new string(' ', valLen - vals[i].Length)} | {types[i]}");
        }
    }
Exemplo n.º 7
0
        private static IList <FunSet> Compile(IEnumerable <string> files, BuilderOptions buildOptions, out List <BuildMessage> warns)
        {
            var funColl = new List <FunSet>();

            warns = new List <BuildMessage>();

            foreach (var file in files)
            {
                var linker = new DyLinker(FileLookup.Create(Path.GetDirectoryName(file)), buildOptions);
                var cres   = linker.Make(SourceBuffer.FromFile(file));
                var funs   = new FunSet();
                funs.Funs     = new Dictionary <string, DyFunction>(StringComparer.OrdinalIgnoreCase);
                funs.FileName = file;

                if (!cres.Success)
                {
                    throw new DyBuildException(cres.Messages);
                }

                warns.AddRange(cres.Messages.Where(m => m.Type == BuildMessageType.Warning));
                var ctx = DyMachine.CreateExecutionContext(cres.Value);
                funs.Context = ctx;
                DyMachine.Execute(ctx);

                foreach (var v in DyMachine.DumpVariables(ctx))
                {
                    if (v.Value is DyFunction fn)
                    {
                        funs.Funs.Remove(v.Name);
                        funs.Funs.Add(v.Name, fn);
                    }
                }

                funColl.Add(funs);
            }

            return(funColl);
        }
Exemplo n.º 8
0
    public static DyObject?Eval(SourceBuffer buffer, BuilderOptions options, FileLookup lookup, object?args = null)
    {
        DyTuple?tup = null;

        if (args is not null)
        {
            var arr = args.GetType().GetProperties().Select(pi =>
                                                            new DyLabel(pi.Name, TypeConverter.ConvertFrom(pi.GetValue(args)))).ToArray();
            tup = new DyTuple(arr);
        }

        var linker = new DyLinker(lookup ?? FileLookup.Default, options ?? BuilderOptions.Default(), tup);
        var result = linker.Make(buffer);

        if (!result.Success)
        {
            throw new DyBuildException(result.Messages);
        }

        var ctx     = DyMachine.CreateExecutionContext(result.Value !);
        var result2 = DyMachine.Execute(ctx);

        return(result2.Value);
    }
Exemplo n.º 9
0
        public DyObject Eval(ExecutionContext ctx, DyObject source, DyObject args)
        {
            if (!(source is DyString strObj))
            {
                return(ctx.InvalidType(source));
            }

            var tup  = args as DyTuple;
            var code = strObj.Value;

            var sb = new StringBuilder();

            sb.Append("func __x12(");

            if (tup != null)
            {
                for (var i = 0; i < tup.Count; i++)
                {
                    var o = tup.Values[i];

                    if (o is DyLabel lab)
                    {
                        if (i > 0)
                        {
                            sb.Append(',');
                        }

                        sb.Append(lab.Label);
                    }
                }
            }

            sb.Append("){");
            sb.Append(code);
            sb.Append('}');
            sb.Append("__x12");

            var linker = new DyLinker(null, BuilderOptions.Default());
            var result = linker.Make(SourceBuffer.FromString(sb.ToString()));

            if (!result.Success)
            {
                throw new DyBuildException(result.Messages);
            }

            var newctx   = DyMachine.CreateExecutionContext(result.Value);
            var result2  = DyMachine.Execute(newctx);
            var func     = result2.Value as DyFunction;
            var argsList = new List <DyObject>();

            if (tup != null)
            {
                for (var i = 0; i < tup.Count; i++)
                {
                    var o = tup.Values[i];

                    if (o is DyLabel lab)
                    {
                        argsList.Add(lab.Value);
                    }
                }
            }

            return(func.Call(newctx, argsList.ToArray()));
        }
Exemplo n.º 10
0
 protected override DyObject CallWithMemoryLayout(ExecutionContext ctx, DyObject[] locals)
 {
     ctx.CallStack.Push(Caller.External);
     return(DyMachine.ExecuteWithData(this, locals, ctx));
 }