Exemplo n.º 1
0
        public InterpretingTask Process(InterpretingTask input)
        {
            var visitor = new InterpreterVisitor();

            input.InterpretedAst = visitor.EvaluateAst(input.InitialAst, input.Memory, input.Symbols);
            return(input);
        }
Exemplo n.º 2
0
        public Value Run(string code, SourceId sid, Options opts)
        {
            var lexer  = new Lexer.Lexer(code, sid);
            var tokens = lexer.Lex();

            if (opts.DebugLexer)
            {
                foreach (Lexer.Token token in tokens)
                {
                    Console.WriteLine(
                        $"{token.Span.S.ToString()}-{token.Span.E.ToString()}: {token.Ty.Display()}: `{token.Contents}`");
                }
            }

            _ops.SetStream(tokens);
            _ops.Extract();
            if (opts.DebugParser)
            {
                _ops.PrintEntries();
            }

            var parser = new Parser.Parser(_ops, tokens, new Span(0, 0, sid));
            var expr   = parser.ParseProgram();

            if (opts.DebugParser)
            {
                var prettyPrinter = new AstPrinter();
                Console.WriteLine(prettyPrinter.Print(expr));
            }

            return(InterpreterVisitor.Interpret(_env, expr));
        }
Exemplo n.º 3
0
 public void Setup()
 {
     _assertVisitor = new AssertVisitor();
     _valueFactory  = new ValueFactory();
     _interpreter   = new InterpreterVisitor(_valueFactory);
     _memory        = new MemorySystem();
 }
Exemplo n.º 4
0
 public void RunEvalLoop(){
    while(true){
      Console.Write("Interp> ");
      String line = Console.ReadLine();
      if(line == "reset")
        interp_visitor = new InterpreterVisitor();
      else
        VisitLine(line);        
    }
 }
 public void interpret()
 {
     try {
         if (buildProgram())
         {
             ProgramNode  program            = (ProgramNode)this.ast.getProgram();
             INodeVisitor interpreterVisitor = new InterpreterVisitor(this.symbolTable, this.io);
             program.accept(interpreterVisitor);
         }
     } catch (MiniPLException exception) {
         this.io.outputLine(exception.getMessage());
     } catch (SemanticException exception) {
         this.io.outputLine(exception.getMessage());
     }
 }
Exemplo n.º 6
0
        public LData CallFunction(string name, ICollection <LData> parameters)
        {
            //Console.WriteLine($"Calling: {name}");
            if (Intrinsics.LIntrinsics.ContainsKey(name))
            {
                return(Intrinsics.LIntrinsics[name].InterpImplementation(parameters));
            }

            current = globals.GoDown();
            current.PutAllInScope(Functions[name].args, parameters);
            var res = new InterpreterVisitor(this, current).Visit(Functions[name]);

            current = current.GoUp();
            return(res);
        }
Exemplo n.º 7
0
        public void GivenBoundAndUnboundVariableWithTheSameName()
        {
            var application = "(λx.(λy.xy))x";

            var lambdaExpression = _parser.Parse(application);

            var interpreter = new InterpreterVisitor();

            interpreter.Result.Accept(interpreter);

            var printVisitor = new NormalFormVisitor();

            lambdaExpression.Accept(printVisitor);

            Assert.Equal("(λt.yt)", printVisitor.Result);
        }
Exemplo n.º 8
0
        public void GivenTheIdentifyFunctionParseAndInterpretToTheIdentifyFunction()
        {
            var identityFunction = "λx.x";

            var lambdaExpression = _parser.Parse(identityFunction);

            var interpreter = new InterpreterVisitor();

            lambdaExpression.Accept(interpreter);

            var printVisitor = new NormalFormVisitor();

            interpreter.Result.Accept(printVisitor);

            Assert.Equal("(λx.x)", printVisitor.Result);
        }
Exemplo n.º 9
0
        private List <string> GetProgramOutput(string[] lines, string[] inputs)
        {
            FileReader.ClearInput();
            IOHandler io = new TestIO(inputs);

            foreach (string line in lines)
            {
                FileReader.AddInputLine(line);
            }
            string    input = ConcatLines(lines);
            BlockNode ast   = p.Parse(input);
            Visitor   v     = new InterpreterVisitor(io);

            v.VisitProgram(ast);
            FileReader.ClearInput();
            return(io.GetOutput());
        }
Exemplo n.º 10
0
        public void GivenAnApplicationEvaluateTheApplicationCorrectly()
        {
            var application = @"(λx.(λy.x)) (λx.(λy.x))";

            var lambdaExpression = _parser.Parse(application);

            var interpreter = new InterpreterVisitor();
            var events      = new ReductionEventCollector();

            ConnectEvents(interpreter, events);

            lambdaExpression.Accept(interpreter);

            var printVisitor = new NormalFormVisitor();

            interpreter.Result.Accept(printVisitor);

            Assert.Collection(events.Events,
                              e => Assert.Equal("α", e),
                              e => Assert.Equal("β", e));

            Assert.Equal("λy.(λx0.(λx1.x0))", printVisitor.Result);
        }
Exemplo n.º 11
0
 private static void ConnectEvents(InterpreterVisitor interpreter, ReductionEventCollector events)
 {
     interpreter.AlphaReductionEvent += events.OnAlphaReductionEvent;
     interpreter.BetaReductionEvent  += events.OnBetaReductionEvent;
     interpreter.EtaReductionEvent   += events.OnEtaReductionEvent;
 }
Exemplo n.º 12
0
 //----< constructor >------------------------------
 public consoleProgram()
 {
     interp_visitor = new InterpreterVisitor();
     print_visitor = new PrettyPrintVisitor();
 }