Ejemplo n.º 1
0
        public ArithmaticUnifier()
        {
            ProgramBlockParser parser = new ProgramBlockParser();
            var progIn = parser.Parse(solver);

            interpreter = new Interpreter.Execution.Interpreter(((AstProgramBlock)progIn.AstResult).Convert(),
                                                                new[] { new ArithmaticFunctions() }.Cast <ITrsNativeFunction>().ToList());
        }
Ejemplo n.º 2
0
 public ArithmaticUnifier()
 {
     ProgramBlockParser parser = new ProgramBlockParser();
       var progIn = parser.Parse(solver);
       interpreter = new Interpreter.Execution.Interpreter(((AstProgramBlock)progIn.AstResult).Convert(),
     new[] { new ArithmaticFunctions() }.Cast<ITrsNativeFunction>().ToList());
 }
Ejemplo n.º 3
0
        public void ExecuteRewriteStep()
        {
            // Evaluate
            string        prevInput = ParseRegexes.LookAheadSingleLineComment.Replace(code.ProgramBlock, Environment.NewLine).Trim();
            StringBuilder messages  = new StringBuilder();
            // Execute
            Tokenizer          tokenizer = new Tokenizer();
            ProgramBlockParser parser    = new ProgramBlockParser();
            var tokenResult = tokenizer.Tokenize(code.ProgramBlock);

            if (tokenResult.Succeed && tokenResult.Tokens.Count == 0)
            {
                messages.AppendLine("// No input statements given.");
                messages.AppendLine();
                messages.AppendLine(prevInput);
            }
            else if (tokenResult.Succeed)
            {
                var ast = parser.Parse(tokenResult.Tokens, 0);
                if (ast.Succeed)
                {
                    var nativeFunctions = new [] { new ArithmaticFunctions() }.Cast <ITrsNativeFunction>().ToList();
                    var externalUnifiers = new [] { new ArithmaticUnifier() /* new TestUnifier() */ }.Cast <ITrsUnifierCalculation>().ToList();
                    var programBlock = ((AstProgramBlock)ast.AstResult).Convert();
                    var interpreter  = new Interpreter.Execution.Interpreter(programBlock, nativeFunctions, externalUnifiers.ToList());
                    messages.AppendLine("// Interpreter validation messages: " + (interpreter.ValidationMessages.Count == 0 ? "None" : string.Empty));
                    foreach (var msg in interpreter.ValidationMessages)
                    {
                        messages.AppendLine("// " + msg.Message);
                    }
                    if (interpreter.ValidationMessages.Where(msg => msg.MessageType == InterpreterMessageType.Error).FirstOrDefault() != null)
                    {
                        messages.AppendLine("// Errors found, exiting ...");
                        messages.AppendLine();
                        messages.AppendLine(prevInput);
                    }
                    else
                    {
                        if (!interpreter.ExecuteRewriteStep())
                        {
                            messages.AppendLine("// No further rewriting took place");
                        }
                        messages.AppendLine();
                        messages.AppendLine(interpreter.GetCurrentRewriteResult().ProgramOut.ToSourceCode());
                    }
                }
                else
                {
                    messages.AppendLine("// Parsing failed:\n// " + ast.ErrorMessage);
                    var errPos = ast.StartPosition >= ast.SourceTokens.Count ? ast.SourceTokens.Count - 1 : ast.StartPosition;
                    messages.AppendLine("// Input is: " + GetErrorLine(tokenResult.Tokens[errPos].From).Trim());
                    messages.AppendLine();
                    messages.AppendLine(prevInput);
                }
            }
            else
            {
                messages.AppendLine("// Tokenization failed:\n// " + tokenResult.ErrorMessage);
                messages.AppendLine();
                messages.AppendLine(prevInput);
            }
            code.ProgramBlock = messages.ToString();
        }