Esempio n. 1
0
        public IndirectCallContext(StackContext originalSctx,
            Engine parentEngine,
            Application parentApplication,
            IEnumerable<string> importedNamespaces,
            IIndirectCall callable,
            PValue[] args)
        {
            if (parentEngine == null)
                throw new ArgumentNullException("parentEngine");
            if (parentApplication == null)
                throw new ArgumentNullException("parentApplication");
            if (importedNamespaces == null)
                throw new ArgumentNullException("importedNamespaces");
            if (callable == null)
                throw new ArgumentNullException("callable");
            if (args == null)
                throw new ArgumentNullException("args");

            _engine = parentEngine;
            _application = parentApplication;
            _importedNamespaces = (importedNamespaces as SymbolCollection) ??
                new SymbolCollection(importedNamespaces);
            _callable = callable;
            _arguments = args;
            _originalStackContext = originalSctx;
        }
Esempio n. 2
0
        public static CilFunctionContext New(StackContext caller,
            SymbolCollection importedNamespaces)
        {
            if (caller == null)
                throw new ArgumentNullException("caller");

            if (importedNamespaces == null)
                importedNamespaces = new SymbolCollection();

            return new CilFunctionContext(caller.ParentEngine, caller.ParentApplication,
                importedNamespaces);
        }
Esempio n. 3
0
        public CoroutineContext(StackContext sctx, IEnumerator<PValue> coroutine)
        {
            if (sctx == null)
                throw new ArgumentNullException("sctx");
            if (coroutine == null)
                throw new ArgumentNullException("coroutine");

            _coroutine = coroutine;

            _parentEngine = sctx.ParentEngine;
            _parentApplication = sctx.ParentApplication;
            _importedNamespaces = sctx.ImportedNamespaces;
        }
Esempio n. 4
0
        public CooperativeContext(StackContext sctx,
            Func<Action<PValue>, IEnumerable<bool>> methodCtor)
        {
            if (sctx == null)
                throw new ArgumentNullException("sctx");
            if (methodCtor == null)
                throw new ArgumentNullException("methodCtor");

            _methodCtor = methodCtor;
            _parentEngine = sctx.ParentEngine;
            _parentApplication = sctx.ParentApplication;
            _importedNamespaces = sctx.ImportedNamespaces;
        }
Esempio n. 5
0
 public virtual IEnumerable<string> OnTab(string attr, string pref, string root,
     StackContext sctx)
 {
     if (sctx == null)
         throw new ArgumentNullException("sctx",
             "OnTab must either be called from via the ReadLine method or with a valid stack context.");
     if (_onTab != null && !_onTab.IsNull)
     {
         var plst = _onTab.IndirectCall(_sctx, new PValue[] {pref, root});
         plst.ConvertTo(_sctx, PType.Object[typeof (IEnumerable)], true);
         foreach (var o in (IEnumerable) plst.Value)
         {
             yield return ((PValue) o).CallToString(_sctx);
         }
     }
 }
Esempio n. 6
0
        public bool TryDynamicCall(StackContext sctx, PValue[] args, PCall call, string id,
            out PValue result)
        {
            CallExpectation expectation;
            Assert.IsTrue(_expectations.TryGetValue(id, out expectation),
                String.Format("A call to member {0} on object {1} is not expected.", id, Name));

            Assert.AreEqual(expectation.ExpectedCall, call, "Call type (get/set)");
            Assert.AreEqual(expectation.ExpectedArguments.Length, args.Length,
                "Number of arguments do not match. Called with " + args.ToEnumerationString());
            for (var i = 0; i < expectation.ExpectedArguments.Length; i++)
                Assert.AreEqual(expectation.ExpectedArguments[i], args[i],
                    String.Format("Arguments at position {0} don't match", i));

            result = expectation.ReturnValue ?? PType.Null;
            expectation.WasCalled = true;
            return true;
        }
Esempio n. 7
0
 public override PValue Run(StackContext sctx, PValue[] args)
 {
     var n = args[0].CallToString(sctx);
     var virtualFile = _virtualFiles[n];
     using(var cr = new StringReader(virtualFile))
         _loaderReference.LoadFromReader(cr,n);
     return PType.Null;
 }
Esempio n. 8
0
 public PValue IndirectCall(StackContext sctx, PValue[] args)
 {
     return Run(sctx.ParentEngine, args);
 }
Esempio n. 9
0
 /// <summary>
 ///     Indicates whether the context still has code/work to do.
 /// </summary>
 /// <returns>True if the context has additional work to perform in the next cycle, False if it has finished it's work and can be removed from the stack</returns>
 protected override bool PerformNextCycle(StackContext lastContext)
 {
     return false;
 }
Esempio n. 10
0
 /// <summary>
 ///     Creates a new <see cref = "PType" /> instance given its CLR <see cref = "Type" />.
 /// </summary>
 /// <param name = "sctx">The stack context in which to create the <see cref = "PType" /></param>
 /// <param name = "ptypeClrType">A CLR <see cref = "Type" /> that inherits from <see cref = "PType" />.</param>
 /// <param name = "args">An array of type arguments.</param>
 /// <returns>The created <see cref = "PType" /> instance.</returns>
 /// <exception cref = "ArgumentException"><paramref name = "ptypeClrType" /> does not inherit from <see cref = "PType" />.</exception>
 public PType CreatePType(StackContext sctx, Type ptypeClrType, PValue[] args)
 {
     return CreatePType(sctx, PType.Object[ptypeClrType], args);
 }
Esempio n. 11
0
 /// <summary>
 ///     Creates a stack context, that might later be pushed onto the stack.
 /// </summary>
 /// <param name = "sctx">The engine for which the context is to be created.</param>
 /// <param name = "args">The arguments passed to this instantiation.</param>
 /// <returns>The created <see cref = "StackContext" /></returns>
 public StackContext CreateStackContext(StackContext sctx, PValue[] args)
 {
     return _function.CreateFunctionContext(sctx.ParentEngine, args, _sharedVariables);
 }
Esempio n. 12
0
 /// <summary>
 ///     Creates a new instance of <see cref = "PrexoniteRuntimeException" /> with a stack trace based on <paramref
 ///      name = "sctx" />.
 /// </summary>
 /// <param name = "sctx">The stack context that caused the exception.</param>
 /// <param name = "message">The custom message to be displayed.</param>
 /// <returns>An instance of <see cref = "PrexoniteRuntimeException" /> with a stack trace.</returns>
 public static PrexoniteRuntimeException CreateRuntimeException(
     StackContext sctx, string message)
 {
     return CreateRuntimeException(sctx, message, null);
 }
Esempio n. 13
0
 /// <summary>
 ///     Indicates whether the context still has code/work to do.
 /// </summary>
 /// <returns>True if the context has additional work to perform in the next cycle, False if it has finished it's work and can be removed from the stack</returns>
 protected override bool PerformNextCycle(StackContext lastContext)
 {
     return _method.MoveNext() && _method.Current;
 }
Esempio n. 14
0
 /// <summary>
 ///     "Calling" a variable means "getting" or "setting" the variable's value.
 /// </summary>
 /// <param name = "sctx">The stack context in which to perform the indirect call.</param>
 /// <param name = "args">The list of arguments to be passed to this indirect call. <br />
 ///     Only the first element of the array is ever used in by this special implementation of <see
 ///      cref = "IIndirectCall.IndirectCall" />.</param>
 /// <remarks>
 ///     If <paramref name = "args" /> is empty, just the variable's current value is returned.<br />
 ///     Otherwise, the first element of <paramref name = "args" /> is assigned to the variable.
 /// </remarks>
 /// <returns>Always the variable's (new) value.</returns>
 public PValue IndirectCall(StackContext sctx, PValue[] args)
 {
     if (args.Length != 0)
         _value = args[args.Length - 1];
     return _value;
 }
Esempio n. 15
0
 public Benchmark(StackContext sctx)
     : this(sctx.ParentEngine)
 {
 }
Esempio n. 16
0
 public Benchmark(StackContext sctx, int iterations)
     : this(sctx.ParentEngine, iterations)
 {
 }
Esempio n. 17
0
        public bool TryDynamicCall(
            StackContext sctx, PValue[] args, PCall call, string id, out PValue result)
        {
            if (sctx == null)
                throw new ArgumentNullException("sctx");
            if (args == null)
                args = new PValue[0];
            if (id == null)
                id = "";

            result = null;
            switch (id.ToLower(CultureInfo.InvariantCulture))
            {
                case "include":
                    foreach (var arg in args)
                        Include(arg.ConvertTo<PFunction>(sctx));
                    result = PType.Null.CreatePValue();
                    break;
                case "includerange":
                    foreach (var arg in args)
                    {
                        if (arg.Type.ToBuiltIn() != PType.BuiltIn.List)
                            continue;
                        foreach (var func in (List<PValue>) arg.Value)
                            Include(func.ConvertTo<PFunction>(sctx));
                    }
                    result = PType.Null.CreatePValue();
                    break;
                case "includeall":
                    Application tapp;
                    if (!(args.Length > 0 && args[0].TryConvertTo(sctx, out tapp)))
                        tapp = sctx.ParentApplication;
                    IncludeAll(tapp);
                    result = PType.Null.CreatePValue();
                    break;
                case "measureall":
                    bool verbose;
                    if (!(args.Length > 0 && args[0].TryConvertTo(sctx, out verbose)))
                        verbose = true;
                    var plst = MeasureAll(verbose).Select(sctx.CreateNativePValue).ToList();
                    result = (PValue) plst;
                    break;
                case "warmup":
                    WarmUp();
                    result = PType.Null.CreatePValue();
                    break;
            }
            return result != null;
        }
Esempio n. 18
0
File: VM.cs Progetto: SealedSun/prx
        /// <summary>
        ///     Executes a give stack context.
        /// </summary>
        /// <param name = "sctx">Any stack context.</param>
        /// <exception cref = "ArgumentNullException"><paramref name = "sctx" /> is null.</exception>
        public PValue Process(StackContext sctx)
        {
            if (sctx == null)
                throw new ArgumentNullException("sctx");
            Stack.AddLast(sctx);
#if Verbose
                    Console.WriteLine("\n#PSH: " + sctx + "(?)");
#endif
            sctx.ReturnMode = ReturnMode.Exit;
            return Process();
        }
Esempio n. 19
0
        /// <summary>
        ///     Creates a new instance of <see cref = "PrexoniteRuntimeException" /> with a stack trace based on <paramref
        ///      name = "esctx" />.
        /// </summary>
        /// <param name = "esctx">The stack context that caused the exception.</param>
        /// <param name = "message">The custom message to be displayed.</param>
        /// <param name = "innerException">The inner exception.</param>
        /// <returns>An instance of <see cref = "PrexoniteRuntimeException" /> with a stack trace.</returns>
        public static PrexoniteRuntimeException CreateRuntimeException(
            StackContext esctx, string message, Exception innerException)
        {
            if (esctx == null)
                throw new ArgumentNullException("esctx");
            if (message == null)
                message = "An error occured at runtime.";

            var builder = new StringBuilder();
            var stack = new List<StackContext>(esctx.ParentEngine.Stack);
            for (var i = stack.Count - 1; i >= 0; i--)
            {
                var sctx = stack[i];
                var fctx = sctx as FunctionContext;
                builder.Append("   at ");
                if (fctx == null)
                {
                    builder.Append(sctx.ToString());
                }
                else
                {
                    var func = fctx.Implementation;
                    var code = func.Code;
                    var pointer = fctx.Pointer - 1;

                    builder.Append("function ");

                    builder.Append(func.Meta.GetDefault(PFunction.LogicalIdKey, func.Id).Text);

                    if (pointer < code.Count)
                    {
                        builder.Append(" around instruction ");

                        builder.Append(pointer);
                        builder.Append(": ");
                        builder.Append(code[pointer]);

                        var sm = SourceMapping.Load(fctx.Implementation);
                        ISourcePosition pos;
                        if (sm.TryGetValue(pointer, out pos))
                        {
                            builder.AppendFormat(" (in {0}, on line {1}, col {2})", pos.File,
                                pos.Line, pos.Column);
                        }
                    }
                }
                builder.Append("\n");
            }

            return new PrexoniteRuntimeException(message, innerException, builder.ToString());
        }
Esempio n. 20
0
 public PValue IndirectCall(StackContext sctx, PValue[] args)
 {
     return _impl(sctx, args);
 }
Esempio n. 21
0
 /// <summary>
 ///     Creates a new instance of <see cref = "PrexoniteRuntimeException" /> with a stack trace based on <paramref
 ///      name = "sctx" />.
 /// </summary>
 /// <param name = "sctx">The stack context that caused the exception.</param>
 /// <param name = "innerException">The inner exception.</param>
 /// <returns>An instance of <see cref = "PrexoniteRuntimeException" /> with a stack trace.</returns>
 public static PrexoniteRuntimeException CreateRuntimeException(
     StackContext sctx, Exception innerException)
 {
     return CreateRuntimeException(sctx, innerException.Message, innerException);
 }
Esempio n. 22
0
 /// <summary>
 ///     Creates a new <see cref = "PType" /> instance based on it's type name.
 /// </summary>
 /// <param name = "sctx">The stack context in which to create the <see cref = "PType" />.</param>
 /// <param name = "typeName">The type's name.</param>
 /// <param name = "args">An array of type arguments.</param>
 /// <returns>The created <see cref = "PType" /> instance.</returns>
 /// <exception cref = "SymbolNotFoundException"><paramref name = "typeName" /> cannot be found in the 
 ///     <see cref = "PTypeRegistry" />.</exception>
 public PType CreatePType(StackContext sctx, string typeName, PValue[] args)
 {
     if (!PTypeRegistry.Contains(typeName))
         throw new SymbolNotFoundException(
             "PTypeRegistry does not hold a record for \"" + typeName + "\".");
     return CreatePType(sctx, PTypeRegistry[typeName], args);
 }
Esempio n. 23
0
 /// <summary>
 ///     Invokes the function with the shared variables.
 /// </summary>
 /// <param name = "sctx">The stack context in which to invoke the function.</param>
 /// <param name = "args">A list of arguments to pass to the function.</param>
 /// <returns>The value returned by the function.</returns>
 public PValue IndirectCall(StackContext sctx, PValue[] args)
 {
     if (!_function.HasCilImplementation)
         throw new PrexoniteException("CilClosure cannot handle " + _function +
             " because it has no cil implementation");
     PValue result;
     ReturnMode returnMode;
     _function.CilImplementation(_function, sctx, args, _sharedVariables, out result,
         out returnMode);
     return result;
 }
Esempio n. 24
0
        public override FunctionContext CreateFunctionContext(StackContext sctx, PValue[] args)
        {
            PValue returnValue;
            if (args.Length < 1)
                returnValue = PType.Null.CreatePValue();
            else
                returnValue = args[0];

            var fctx = base.CreateFunctionContext(sctx, args);

            //restore state
            fctx.Pointer = _entryOffset;

            _populateStack(fctx);

            foreach (var variable in _state)
                fctx.LocalVariables[variable.Key].Value = variable.Value;

            //insert the value returned by the called function
            fctx.Push(returnValue);

            return fctx;
        }
Esempio n. 25
0
        /// <summary>
        ///     Creates a new <see cref = "PType" /> instance given its CLR <see cref = "Type" />, encapsulated in an <see
        ///      cref = "ObjectPType" />.
        /// </summary>
        /// <param name = "sctx">The stack context in which to create the <see cref = "PType" /></param>
        /// <param name = "ptypeClrType">A CLR <see cref = "Type" /> that inherits from <see cref = "PType" />, encapsulated in a <see
        ///      cref = "ObjectPType" /> object.</param>
        /// <param name = "args">An array of type arguments.</param>
        /// <returns>The created <see cref = "PType" /> instance.</returns>
        /// <exception cref = "ArgumentException">The <see cref = "System.Type" /> provided by the <see
        ///      cref = "ObjectPType.ClrType" /> property of <paramref name = "ptypeClrType" /> does not inherit from <see
        ///      cref = "PType" />.</exception>
        /// <exception cref = "PrexoniteException">If a silent error occured during the creation of the <see cref = "PType" /> instance.</exception>
        public PType CreatePType(StackContext sctx, ObjectPType ptypeClrType, PValue[] args)
        {
            if (!PType.IsPType(ptypeClrType))
                throw new ArgumentException(
                    "Cannot construct PType. ClrType " + ptypeClrType.ClrType +
                        " is not a PType.");

            //Performance optimizations
            var clrType = ptypeClrType.ClrType;
            if (clrType == typeof (IntPType))
                return PType.Int;
            if (clrType == typeof (RealPType))
                return PType.Real;
            if (clrType == typeof (BoolPType))
                return PType.Bool;
            if (clrType == typeof (StringPType))
                return PType.String;
            if (clrType == typeof (NullPType))
                return PType.Null;
            if (clrType == typeof (ObjectPType) && args.Length > 0 && args[0].Type == PType.String)
                return PType.Object[sctx, (string) args[0].Value];
            if (clrType == typeof (ListPType))
                return PType.List;
            if (clrType == typeof (HashPType))
                return PType.Hash;
            if (clrType == typeof (CharPType))
                return PType.Char;
            if (clrType == typeof (StructurePType))
                return PType.Structure;

            var result =
                ptypeClrType.Construct(sctx, new[] {PType.Object.CreatePValue(args)});
            if (result == null || result.IsNull)
                throw new PrexoniteException(
                    "Could not construct PType (resulted in null reference)");
            if (!PType.IsPType(result))
                throw new PrexoniteException(
                    "Could not construct PType (" + result.ClrType +
                        " is not a PType).");
            return result.Value as PType;
        }
Esempio n. 26
0
 public NullContext(StackContext parentCtx)
     : this(parentCtx.ParentEngine, parentCtx.ParentApplication, parentCtx.ImportedNamespaces
         )
 {
 }
Esempio n. 27
0
 /// <summary>
 ///     Creates a new <see cref = "PType" /> instance based on a constant type expression.
 /// </summary>
 /// <param name = "sctx">The stack context in which to create the <see cref = "PType" />.</param>
 /// <param name = "expression">A constant type expression.</param>
 /// <returns>The created <see cref = "PType" /> instance.</returns>
 /// <remarks>
 ///     <para>
 ///         While it may seem convenient to use this overload, bear in mind this it 
 ///         requires a fully featured parser to translate the supplied string 
 ///         to a <see cref = "PType" /> instance.
 ///     </para>
 ///     <para>
 ///         You are advised to stick with the other overloads unless you get such 
 ///         an expression from a third source (e.g., a configuration file or an <see cref = "Instruction" />).
 ///     </para>
 /// </remarks>
 public PType CreatePType(StackContext sctx, string expression)
 {
     using (var lexer = TypeExpressionScanner.CreateFromString(expression))
     {
         var parser = new TypeExpressionParser(lexer, sctx);
         parser.Parse();
         if (parser.errors.count > 0)
             throw new PrexoniteException(
                 "Could not construct PType. (Errors in PType expression: " + expression +
                     ")");
         else
             return parser.LastType;
     }
 }
Esempio n. 28
0
 public CoroutineContext(StackContext sctx, IEnumerable<PValue> coroutine)
     : this(sctx, coroutine.GetEnumerator())
 {
 }
Esempio n. 29
0
        public override PValue IndirectCall(StackContext sctx, PValue[] args)
        {
            if (sctx == null)
                throw new ArgumentNullException("sctx");
            if (args == null)
                throw new ArgumentNullException("args");

            var fctx = CreateFunctionContext(sctx, args);

            //run the continuation
            return sctx.ParentEngine.Process(fctx);
        }
Esempio n. 30
0
 /// <summary>
 ///     Indicates whether the context still has code/work to do.
 /// </summary>
 /// <returns>True if the context has additional work to perform in the next cycle, False if it has finished it's work and can be removed from the stack</returns>
 protected override bool PerformNextCycle(StackContext lastContext)
 {
     var moved = _coroutine.MoveNext();
     if (moved)
     {
         if (_coroutine.Current != null)
             _returnValue = _coroutine.Current;
         ReturnMode = ReturnMode.Continue;
     }
     else
     {
         ReturnMode = ReturnMode.Break;
     }
     return false; //remove the context from the stack (for now)
 }