예제 #1
0
파일: UnitTest1.cs 프로젝트: chargen/AGI-X0
        // tests if a functioncall to an defined function can be made
        //
        // creates providers and surrogates, then an callee and called function
        public void InterpreterFunctioninvocation1()
        {
            FunctionalInterpretationContext functionalContext = new FunctionalInterpretationContext();


            FunctionalInterpreter functionalInterpreter = new FunctionalInterpreter();

            functionalInterpreter.tracer = new NullFunctionalInterpreterTracer();

            FunctionalInterpreterSurrogate interpreterSurrogate = new FunctionalInterpreterSurrogate(functionalInterpreter, functionalContext);

            // dispatcher which dispatches hidden function calls to the interpreter
            SurrogateProvider surrogateProvider = new SurrogateProvider();

            // dispatcher which can shadow calls (to the surrogate provider)
            ShadowableHiddenDispatcher shadowableHiddenDispatcher = new ShadowableHiddenDispatcher(surrogateProvider);


            // dispatcher which calls another dispatcher and a number of observers,
            // which is in this case our instrumentation observer
            InstrumentationHiddenDispatcher instrHiddenDispatcher = new InstrumentationHiddenDispatcher(shadowableHiddenDispatcher);


            ArgumentBasedDispatcher publicDispatcherByArguments = new ArgumentBasedDispatcher(instrHiddenDispatcher);

            PublicCallDispatcher callDispatcher = new PublicCallDispatcher(publicDispatcherByArguments);

            // all calls the functional interpreter makes to an user defined function are dispatched with this
            functionalContext.publicFnRegistryAndDispatcher = new PublicFunctionRegistryAndDispatcher(callDispatcher);


            Functional.ParseTreeElement parseTree      = Functional.parseRecursive("3");
            NodeRefererEntry            rootnodeCalled = FunctionalToParseTreeTranslator.translateRecursive(parseTree);

            { // set descriptor to route all public function id's 0 to hidden function id 0
                ArgumentBasedDispatcher.FunctionDescriptor fnDescriptor = new ArgumentBasedDispatcher.FunctionDescriptor();
                fnDescriptor.wildcardHiddenFunctionId = HiddenFunctionId.make(0);
                publicDispatcherByArguments.setFunctionDescriptor(PublicFunctionId.make(0), fnDescriptor);
            }

            surrogateProvider.updateSurrogateByFunctionId(HiddenFunctionId.make(0), interpreterSurrogate);
            interpreterSurrogate.updateFunctionBody(HiddenFunctionId.make(0), rootnodeCalled.entry);
            interpreterSurrogate.updateParameterNames(HiddenFunctionId.make(0), new List <string>());

            callDispatcher.setFunctionId("a", PublicFunctionId.make(0));      // bind function name "a" to public function id 0
            functionalContext.publicFnRegistryAndDispatcher.addFunction("a"); // register so the dispatcher used by the interpreter knows that the function exists

            Functional.ParseTreeElement parseTree2     = Functional.parseRecursive("(a)");
            NodeRefererEntry            rootnodeCallee = FunctionalToParseTreeTranslator.translateRecursive(parseTree2);


            functionalInterpreter.interpret(functionalContext, rootnodeCallee.entry, new List <ImmutableNodeReferer>(), new List <string>());
            ImmutableNodeReferer result = rootnodeCallee.entry.interpretationResult;

            Assert.AreEqual(result.valueInt, 3);
        }
예제 #2
0
파일: UnitTest1.cs 프로젝트: chargen/AGI-X0
        NodeRefererEntry interpretAndReturnRootnode(string functionalProgram, IList <ImmutableNodeReferer> parameterValues = null, IList <String> parameterNames = null)
        {
            Functional.ParseTreeElement parseTree = Functional.parseRecursive(functionalProgram);
            NodeRefererEntry            rootnode  = FunctionalToParseTreeTranslator.translateRecursive(parseTree);

            FunctionalInterpreter functionalInterpreter = new FunctionalInterpreter();

            functionalInterpreter.tracer = new NullFunctionalInterpreterTracer();

            functionalInterpreter.interpret(new FunctionalInterpretationContext(), rootnode.entry, parameterValues == null ? new List <ImmutableNodeReferer>() : parameterValues, parameterNames == null ? new List <string>() : parameterNames);

            return(rootnode);
        }