public List<AphidExpression> Optimize(string script) { var tokens = new AphidLexer(script).GetTokens(); var ast = new AphidParser(tokens).Parse(); return Optimize(ast); }
public static List <AphidExpression> Parse( List <AphidToken> tokens, string code, string filename, bool useImplicitReturns = true) { if (tokens.Count == 0) { return(new List <AphidExpression>()); } var parser = new AphidParser(tokens) { UseImplicitReturns = useImplicitReturns }; List <AphidExpression> ast; try { ast = parser.Parse(); } catch (AphidParserException e) when(!string.IsNullOrEmpty(filename) && string.IsNullOrEmpty(e.Filename)) { e.Filename = filename; throw; } if (ast.Count != 0) { if (code != null) { ast[0].Code = code; } if (filename != null) { ast[0].Filename = filename; } } new AphidCodeVisitor().Visit(ast); return(ast); }
public AphidObject Deserialize(string obj) { var lexer = new AphidObjectLexer(obj); var tokens = lexer.GetTokens(); tokens.Add(new AphidToken(AphidTokenType.EndOfStatement, null, 0)); var ast = new AphidParser(tokens).Parse(); if (ast.Count != 1) { throw new AphidRuntimeException("Invalid Aphid object string: {0}", obj); } ast[0] = new UnaryOperatorExpression(AphidTokenType.retKeyword, ast[0]); var objInterpreter = new AphidInterpreter(); objInterpreter.Interpret(ast); return objInterpreter.GetReturnValue(); }
private void CheckParseRequest(ParseRequest req) { try { var lexer = new AphidLexer(req.Text); var parser = new AphidParser(lexer.GetTokens()); parser.Parse(); } catch (AphidParserException e) { var lineCol = TokenHelper.GetLineCol(req.Text, e.UnexpectedToken.Index); var span = new TextSpan() { iStartLine = lineCol.Item1, iEndLine = lineCol.Item1, iStartIndex = lineCol.Item2, iEndIndex = lineCol.Item2 + (e.UnexpectedToken.Lexeme != null ? e.UnexpectedToken.Lexeme.Length : 0) }; var msg = string.Format( "Unexpected {0}: {1}, expected {2}", e.UnexpectedToken.TokenType.ToString(), e.UnexpectedToken.Lexeme, e.ExpectedToken); req.Sink.AddError(req.FileName, msg, span, Severity.Error); } }
protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged) { LoadScriptExpression loadExp; AphidExpression scriptExp; if (expression.Type != AphidExpressionType.LoadScriptExpression) { hasChanged = false; return(null); } else if ((loadExp = (LoadScriptExpression)expression).FileExpression.Type != AphidExpressionType.StringExpression) { var constantFolder = new ConstantFoldingMutator(); var mutated = constantFolder.Mutate(new List <AphidExpression> { loadExp.FileExpression }); if (mutated.Count != 1 || (scriptExp = mutated[0]).Type != AphidExpressionType.StringExpression) { hasChanged = false; return(null); //throw new AphidParserException("Invalid load script operand", loadExp); } } else { scriptExp = loadExp.FileExpression; } var scriptStr = StringParser.Parse(((StringExpression)scriptExp).Value); var script = Loader.FindScriptFile(_applicationDirectory, scriptStr); if (!File.Exists(script)) { throw new AphidParserException( string.Format( "Could not find script {0}", scriptStr), scriptExp); } Included.Add(script); List <AphidExpression> ast; if (AphidConfig.Current.ScriptCaching && !DisableCaching) { AphidByteCodeCache cache; if (UseImplicitReturns) { cache = new AphidByteCodeCache(Loader.SearchPaths.ToArray()); } else { cache = new AphidByteCodeCache(Loader.SearchPaths.ToArray(), 0x1); } ast = cache.Read(script, out var cacheSources); for (var i = 0; i < cacheSources.Length; i++) { Included.Add(cacheSources[i].Name); } } else { var code = AphidScript.Read(script); ast = AphidParser.Parse(code, script, useImplicitReturns: UseImplicitReturns); } if (PerformCommonTransformations) { var mutatedAst = new PartialOperatorMutator().Mutate(ast); mutatedAst = new AphidMacroMutator().Mutate(mutatedAst); mutatedAst = new AphidPreprocessorDirectiveMutator().Mutate(mutatedAst); mutatedAst = new ConstantFoldingMutator().Mutate(mutatedAst); hasChanged = true; return(mutatedAst); } hasChanged = true; return(ast); }
protected override List <AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged) { LoadScriptExpression loadExp; AphidExpression scriptExp; if (expression.Type != AphidExpressionType.LoadScriptExpression) { hasChanged = false; return(null); } else if ((loadExp = (LoadScriptExpression)expression).FileExpression.Type != AphidExpressionType.StringExpression) { var mutated = new ConstantFoldingMutator() .MutateSingle(loadExp.FileExpression); if ((scriptExp = mutated).Type != AphidExpressionType.StringExpression) { hasChanged = false; return(null); //throw new AphidParserException("Invalid load script operand", loadExp); } } else { scriptExp = loadExp.FileExpression; } var scriptStr = StringParser.Parse(((StringExpression)scriptExp).Value); if (!EnvironmentHelper.IsWindows) { scriptStr = scriptStr.ToLower(); } var script = Loader.FindScriptFile(_applicationDirectory, scriptStr); if (script == null && scriptExp.Filename != null) { script = Loader.FindScriptFile(Path.GetDirectoryName(scriptExp.Filename), scriptStr); } if (!File.Exists(script)) { throw new AphidParserException( string.Format( "Could not find script {0}", scriptStr), scriptExp); } AddIncluded(script); List <AphidExpression> ast; if (AphidConfig.Current.ScriptCaching && !DisableCaching) { AphidByteCodeCache cache = UseImplicitReturns ? new AphidByteCodeCache(Loader.SearchPaths.ToArray()) { DisableConstantFolding = false, InlineScripts = true, } : new AphidByteCodeCache(Loader.SearchPaths.ToArray(), 0x1); ast = cache.Read(script, out var cacheSources); for (var i = 0; i < cacheSources.Length; i++) { AddIncluded(cacheSources[i].Name); } } else { var code = AphidScript.Read(script); ast = AphidParser.Parse(code, script, useImplicitReturns: UseImplicitReturns); } if (PerformCommonTransformations) { var mut = MutatorGroups.GetStandard().Mutate(ast); hasChanged = true; return(mut); } hasChanged = true; return(ast); }
private void ExecuteStatements() { ResetInterpreter(); InvokeDispatcher(() => { _codeViewer.Document.Blocks.Clear(); _codeViewer.AppendOutput("Running script..."); }); if (string.IsNullOrEmpty(Code)) { return; } var tokens = new AphidLexer(Code).GetTokens(); List<Components.Aphid.Parser.AphidExpression> ast = null; try { ast = new AphidParser(tokens).Parse(); } catch (AphidParserException ex) { InvokeDispatcher(() => _codeViewer.AppendParserException(Code, ex)); return; } try { _interpreter.Interpret(ast); } catch (AphidRuntimeException ex) { InvokeDispatcher(() => _codeViewer.AppendException(Code, "Runtime error", ex)); return; } catch (AphidParserException ex) { InvokeDispatcher(() => _codeViewer.AppendParserException(Code, ex)); return; } catch (Exception ex) { InvokeDispatcher(() => _codeViewer.AppendException(Code, "Internal error", ex)); } UpdateVariables(); ExecuteWatchExpressions(); InvokeDispatcher(() => { _codeViewer.AppendOutput("Done"); _codeViewer.ScrollToEnd(); }); }
private void ExecuteExpression() { if (string.IsNullOrEmpty(Code)) { return; } AddHistoricalCode(); var tokens = new AphidLexer(Code + " ").GetTokens(); if (!tokens.Any(x => x.TokenType == AphidTokenType.EndOfStatement)) { tokens.Add(new AphidToken(AphidTokenType.EndOfStatement, "", 0)); } List<Components.Aphid.Parser.AphidExpression> ast = null; try { ast = new AphidParser(tokens).Parse(); } catch (AphidParserException ex) { InvokeDispatcher(() => _codeViewer.AppendParserException(Code, ex)); return; } if (ast.Count != 1) { throw new InvalidOperationException(); } var exp = ast.First(); var unary = exp as UnaryOperatorExpression; if (unary == null || unary.Operator != AphidTokenType.retKeyword) { ast.Clear(); ast.Add(new UnaryOperatorExpression( AphidTokenType.retKeyword, exp)); } try { _interpreter.Interpret(ast); } catch (AphidRuntimeException ex) { InvokeDispatcher(() => _codeViewer.AppendException(Code, "Runtime error", ex)); return; } catch (AphidParserException ex) { InvokeDispatcher(() => _codeViewer.AppendParserException(Code, ex)); return; } var retVal = _interpreter.GetReturnValue(); var serializer = new AphidSerializer() { IgnoreFunctions = false }; InvokeDispatcher(() => _codeViewer.AppendOutput(Code, serializer.Serialize(retVal))); UpdateVariables(); ExecuteWatchExpressions(); }
public void Interpret(string code) { var lexer = new AphidLexer(code); var parser = new AphidParser(lexer.GetTokens()); var ast = parser.Parse(); Interpret(ast); }
static void RunTest(PerfTest test, int iterations) { var interpreter = new AphidInterpreter(); if (test.Prologue != null) { interpreter.Interpret(test.Prologue); } var ast = new AphidParser(new AphidLexer(test.Body).GetTokens()).Parse(); for (int i = 0; i < iterations; i++) { interpreter.Interpret(ast); } }