Esempio n. 1
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);
    }
Esempio n. 2
0
 public DyCompiler(BuilderOptions options, DyLinker linker)
 {
     this.options = options ?? BuilderOptions.Default();
     this.linker  = linker;
     builder      = new(this.options, linker);
 }
Esempio n. 3
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()));
        }