internal Delegate GetDelegate(RubyScope /*!*/ declaringScope, RubyModule /*!*/ declaringModule) { if (_delegate == null) { lock (this) { if (_delegate == null) { // TODO: remove options AstGenerator gen = new AstGenerator(declaringScope.RubyContext, new RubyCompilerOptions(), _document, _encoding, false); MSA.LambdaExpression lambda = _ast.TransformBody(gen, declaringScope, declaringModule); _delegate = RubyScriptCode.CompileLambda(lambda, declaringScope.RubyContext); } } } return(_delegate); }
public void Can_parse_nested_rule_to_ast() { var css = @" .header { BackgroundColor: Green; Label { BackgroundColor: Red; } } "; var ast = new AstGenerator().GetAst(css).Root; var headerRuleNode = ast.GetRootStyleRuleNode(0); var labelRuleNode = headerRuleNode .GetSubStyleRuleNode(0); labelRuleNode .GetSelectorNode(0, 0, 0).Text.Should().Be("Label"); }
static void Main(string[] args) { #if debugenabled try { #endif string Code = File.ReadAllText("Tests\\Ackermann.cs"); CodeLexer lexer = new CodeLexer(Code); LexTokenList lexTokens = lexer.Analyze(); CPreprocessor preProcessor = new CPreprocessor(lexTokens); lexTokens = preProcessor.Process(); Console.WriteLine("LexTokens (post-processed):"); foreach (LexToken lexToken in lexTokens) { Console.WriteLine(lexToken.Kind.ToString() + " " + (lexToken.Value == null ? "" : lexToken.Value.ToString())); } Console.WriteLine("------\nGenerating AST.."); AstGenerator astGenerator = new AstGenerator(lexTokens); StatementList statements = astGenerator.Generate(); Console.WriteLine("Generated AST!"); Console.WriteLine(View.ASTView.GenerateStatements(statements)); #if debugenabled } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); } #endif Console.ReadLine(); }
public void ReadJsFile(string in_filename) { var context = Context.Create(); var astBuilder = new AstGenerator(); var etGenerator = new EtGenerator(); var astNodes = astBuilder.Build(in_filename, Encoding.UTF8); Action <Scope> compiled = etGenerator.Build(astNodes, context); var globals = Scope.CreateGlobal(context); context.SetupGlobals(globals); // set up 'puts' function Action <object> emit = (obj) => { Console.WriteLine(JsTypeConverter.ToString(obj)); }; globals.Global("puts", emit); // Forms the `net" namespace net netObj = new net(context); globals.Global("net", netObj); compiled(globals); }
public static ICode ToAst(Ctx ctx, bool verbose = false) { var ast = AstGenerator.CreateBlockedCilAst(ctx); //int step = 0; //Action<Stmt, string> print = (s, name) => { // if (verbose) { // if (name != null && name.StartsWith("Visitor")) { // name = name.Substring(7); // } // Console.WriteLine(" --- AST Transform Step {0}{1} ---", step++, name == null ? "" : (" '" + name + "'")); // Console.WriteLine(ShowVisitor.V(s)); // Console.WriteLine(); // } //}; //Func<Func<Stmt, Stmt>, Stmt, string, Stmt> doStep = (fn, s0, name) => { // var s1 = fn(s0); // if (s1 != s0) { // print(s1, name); // var dupStmts = VisitorFindDuplicateStmts.Find(s1); // if (dupStmts.Any()) { // Console.WriteLine("*** ERROR *** {0} DUPLICATE STMT(S) ***", dupStmts.Count()); // foreach (var dup in dupStmts) { // Console.WriteLine(); // Console.WriteLine(ShowVisitor.V(dup)); // } // throw new InvalidOperationException("Duplicate stmt(s) found"); // } // } // return s1; //}; //print(ast, null); Print(ast, null, verbose); ast = DoStep(s => (Stmt)VisitorConvertCilToSsa.V(s), ast, "VisitorConvertCilToSsa", verbose); // Reduce to AST with no continuations for (int i = 0; ; i++) { var astOrg = ast; ast = DoStep(s => (Stmt)VisitorSubstitute.V(s), ast, "VisitorSubstitute", verbose); ast = DoStep(s => (Stmt)VisitorTryCatchFinallySequencing.V(s), ast, "VisitorTryCatchFinallySequencing", verbose); ast = DoStep(s => (Stmt)VisitorIfDistribution.V(s), ast, "VisitorIfDistribution", verbose); ast = DoStep(s => (Stmt)VisitorDerecurse.V(s), ast, "VisitorDerecurse", verbose); ast = DoStep(s => (Stmt)VisitorBooleanSimplification.V(s), ast, "VisitorBooleanSimplification", verbose); ast = DoStep(s => (Stmt)VisitorIfSimplification.V(s), ast, "VisitorIfSimplification", verbose); ast = DoStep(s => (Stmt)VisitorIfReorder.V(s), ast, "VisitorIfReorder", verbose); ast = DoStep(s => (Stmt)VisitorEmptyBlockRemoval.V(s), ast, "VisitorEmptyBlockRemoval", verbose); ast = DoStep(s => (Stmt)VisitorSwitchSequencing.V(s, false), ast, "VisitorSwitchSequencing", verbose); if (ast == astOrg) { if (!VisitorFindContinuations.Any(ast)) { break; } else { ast = DoStep(s => (Stmt)VisitorSwitchSequencing.V(s, true), ast, "VisitorSwitchSequencing-LastChance", verbose); if (ast != astOrg) { continue; } ast = DoStep(s => (Stmt)VisitorSubstituteIrreducable.V(s), ast, "VisitorSubstituteIrreducable", verbose); if (ast != astOrg) { continue; } ast = DoStep(s => (Stmt)VisitorDuplicateCode.V(s), ast, "VisitorDuplicateCode", verbose); if (ast != astOrg) { continue; } throw new InvalidOperationException("Error: Cannot reduce IL to AST with no continuations"); } } if (i > 20) { // After 20 iterations even the most complex method should be sorted out throw new InvalidOperationException("Error: Stuck in loop trying to reduce AST"); } } // PhiSimplifier must be done before DefiniteAssignment ast = DoStep(s => (Stmt)VisitorPhiSimplifier.V(s), ast, "VisitorPhiSimplifier", verbose); // DefiniteAssignment must be done before SsaCopyPropagation ast = DoStep(s => (Stmt)VisitorDefiniteAssignment.V(s), ast, "VisitorDefiniteAssignment", verbose); // Simplify AST for (int i = 0; ; i++) { var astOrg = ast; // TODO: VisitorBooleanSimplification may re-order logic, which should not be done at this stage ast = DoStep(s => (Stmt)VisitorBooleanSimplification.V(s), ast, "VisitorBooleanSimplification", verbose); ast = DoStep(s => (Stmt)VisitorIfSimplification.V(s), ast, "VisitorIfSimplification", verbose); ast = DoStep(s => (Stmt)VisitorMoveOutOfLoop.V(s), ast, "VisitorMoveOutOfLoop", verbose); ast = DoStep(s => (Stmt)VisitorSsaCopyPropagation.V(s), ast, "VisitorSsaCopyPropagation", verbose); ast = DoStep(s => (Stmt)VisitorPhiSimplifier.V(s), ast, "VisitorPhiSimplifier", verbose); ast = DoStep(s => (Stmt)VisitorExpressionSimplifier.V(s), ast, "VisitorExpressionSimplifier", verbose); ast = DoStep(s => (Stmt)VisitorEmptyBlockRemoval.V(s), ast, "VisitorEmptyBlockRemoval", verbose); if (ast == astOrg) { break; } if (i > 20) { // After 20 iterations even the most complex method should be sorted out throw new InvalidOperationException("Error: Stuck in loop trying to simplify AST"); } } ast = DoStep(s => (Stmt)VisitorRemoveCasts.V(s), ast, "VisitorRemoveCasts", verbose); ast = DoStep(s => (Stmt)VisitorRemoveFinalReturn.V(s), ast, "VisitorRemoveFinalReturn", verbose); ast = DoStep(s => (Stmt)VisitorTypeCorrector.V(s), ast, "VisitorTypeCorrector", verbose); return(ast); }
/// <summary> /// Generates any preparation code for a new class def or function def scope. /// </summary> public virtual MSAst.Expression[] PrepareScope(AstGenerator /*!*/ gen) { return(AstGenerator.EmptyExpression); }
protected abstract MSAst.Expression /*!*/ GetGlobal(string /*!*/ name, AstGenerator /*!*/ ag, bool isLocal);
public static StyleSheet Parse(string document, string defaultCssNamespace = null, IDictionary <string, string> variables = null) { var result = new AstGenerator().GetAst(document); return(Parse(result, defaultCssNamespace, variables)); }
public void Can_generate_ast() { var doc = new AstGenerator().GetAst(css).Root; var mainClassSelector = doc.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleRule) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.Selectors) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.Selector) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.SimpleSelectorSequence) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.ClassSelector); mainClassSelector.Should().NotBeNull(); mainClassSelector.Text.Should().Be(".main"); var children = doc.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleRule) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.Selectors) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.Selector) ?.Children.ToList(); children.Should().NotBeNull(); children[0].Type.Should().Be(CssNodeType.SimpleSelectorSequence); children[0].Children.ToList()[0].Type.Should().Be(CssNodeType.ClassSelector); children[0].Children.ToList()[0].Text.Should().Be(".main"); children[1].Type.Should().Be(CssNodeType.GeneralDescendantCombinator); children[1].Children.Should().BeEmpty(); children[2].Type.Should().Be(CssNodeType.SimpleSelectorSequence); children[2].Children.ToList()[0].Type.Should().Be(CssNodeType.ClassSelector); children[2].Children.ToList()[0].Text.Should().Be(".sub"); children[3].Type.Should().Be(CssNodeType.DirectDescendantCombinator); children[3].Children.Should().BeEmpty(); children[4].Type.Should().Be(CssNodeType.SimpleSelectorSequence); children[4].Children.ToList()[0].Type.Should().Be(CssNodeType.TypeSelector); children[4].Children.ToList()[0].Text.Should().Be("div"); children[5].Type.Should().Be(CssNodeType.GeneralDescendantCombinator); children[5].Children.Should().BeEmpty(); children[6].Type.Should().Be(CssNodeType.SimpleSelectorSequence); children[6].Children.ToList()[0].Type.Should().Be(CssNodeType.TypeSelector); children[6].Children.ToList()[0].Text.Should().Be("xamlcss|Button"); children.Count.Should().Be(7); var node = doc.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleRule) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleDeclarationBlock) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleDeclaration) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.Value && x.Text == "red") ; var attachedPropertyNode = doc.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleRule) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleDeclarationBlock) ?.Children.FirstOrDefault(x => x.Type == CssNodeType.StyleDeclaration && x.Children.Any(y => y.Text == "Grid.Row")) ; var attachedPropertyNodeValue = attachedPropertyNode ?.Children.FirstOrDefault(x => x.Type == CssNodeType.Value && x.Text == "1") ; Assert.NotNull(node); Assert.NotNull(attachedPropertyNode); Assert.NotNull(attachedPropertyNodeValue); }
public static StyleSheet Parse(string document, string defaultCssNamespace = null) { var result = new AstGenerator().GetAst(document); return(Parse(result, defaultCssNamespace)); }