public void LexicalTokenMatching_Usage() { var compiler = new RoslynCompiler(); var lexical = compiler.Lexical(); lexical .match() .any('(', '=', ',') .token("function", named: "fn") .enclosed('(', ')') .token('{', named: "brace") .then(compiler.Lexical().transform() .remove("fn") .insert("=>", before: "brace")) .match() .any(new[] { '(', '=', ',' }, named: "start") .enclosed('[', ']', start: "open", end: "close") .then(compiler.Lexical().transform() .insert("new []", after: "start") .replace("open", "{") .replace("close", "}")); ExpressionSyntax exprFunction = compiler.CompileExpression("call(10, function(x, y) {})"); Assert.IsTrue(exprFunction.DescendantNodes() .OfType <ParenthesizedLambdaExpressionSyntax>() .Any()); ExpressionSyntax exprArray = compiler.CompileExpression("call([1, 2, 3], 4, [5, 6, 7])"); Assert.IsTrue(exprArray.DescendantNodes() .OfType <ImplicitArrayCreationExpressionSyntax>() .Count() == 2); }
public void LexicalTokenMatching() { RoslynCompiler compiler = new RoslynCompiler(); var lexical = compiler.Lexical(); lexical .match() .any('(', '=', ',') .token("function", named: "fn") .enclosed('(', ')') .token('{', named: "brace") .then(compiler.Lexical().transform() .remove("fn") .insert("=>", before: "brace")) .match() .any(new[] { '(', '=', ',' }, named: "start") .enclosed('[', ']', start: "open", end: "close") .then(compiler.Lexical().transform() .insert("new []", after: "start") .replace("open", "{") .replace("close", "}")); ExpressionSyntax exprFunction = compiler.CompileExpression("call(10, function(x, y) {})"); Assert.IsTrue(exprFunction.DescendantNodes() .OfType<ParenthesizedLambdaExpressionSyntax>() .Any()); ExpressionSyntax exprArray = compiler.CompileExpression("call([1, 2, 3], 4, [5, 6, 7])"); Assert.IsTrue(exprArray.DescendantNodes() .OfType<ImplicitArrayCreationExpressionSyntax>() .Count() == 2); }
public void LexicalExtension() { RoslynCompiler compiler = new RoslynCompiler(); var lexical = compiler.Lexical(); lexical .extension("my_ext", ExtensionKind.Code, myExtLexical); string lResult = compiler.ApplyLexicalPass("my_ext(int i) { code(); }"); Assert.IsTrue(lResult == "my_ext_replaced (int i) = { code(); }"); lexical .extension("my_ext_s", ExtensionKind.Member, myExtSyntactical); SyntaxNode sResult = compiler.ApplyLexicalPass("my_ext_s(int i) { code(); }", out lResult); Assert.IsTrue(lResult == "void __extension() {}"); var method = sResult .DescendantNodes() .OfType <MethodDeclarationSyntax>() .FirstOrDefault(); Assert.IsNotNull(method); Assert.IsTrue(method .ParameterList .Parameters .Count == 1); Assert.IsTrue(method .Body .Statements .Count == 1); }
public void LexicalExtension() { RoslynCompiler compiler = new RoslynCompiler(); var lexical = compiler.Lexical(); lexical .extension("my_ext", ExtensionKind.Code, myExtLexical); string lResult = compiler.ApplyLexicalPass("my_ext(int i) { code(); }"); Assert.IsTrue(lResult == "my_ext_replaced (int i) = { code(); }"); lexical .extension("my_ext_s", ExtensionKind.Member, myExtSyntactical); SyntaxNode sResult = compiler.ApplyLexicalPass("my_ext_s(int i) { code(); }", out lResult); Assert.IsTrue(lResult == "void __extension() {}"); var method = sResult .DescendantNodes() .OfType<MethodDeclarationSyntax>() .FirstOrDefault(); Assert.IsNotNull(method); Assert.IsTrue(method .ParameterList .Parameters .Count == 1); Assert.IsTrue(method .Body .Statements .Count == 1); }
public static void Apply(RoslynCompiler compiler, Options options = null, Scope scope = null) { scope?.AddKeywords("concurrent", "spawn", "await"); scope?.set <Options>(options); if (options == null) { options = new Options(); } var lexical = compiler.Lexical(); lexical .match() .token("concurrent", named: "keyword") .token("class", named: "ref") .then(lexical.transform() .remove("keyword") .then(CompileClass(options))) .match() .token("concurrent", named: "keyword") .token("object", named: "ref") .then(lexical.transform() .remove("keyword") .replace("ref", "class ") .then(CompileObject(options))) .match() .token("concurrent", named: "keyword") .token("app", named: "ref") .token("{") .then(lexical.transform() .replace("keyword", "class ") .replace("ref", "__app") .then(CompileApp(options))); compiler.Syntax() .match <MethodDeclarationSyntax>(IsConcurrentFunction, "after-syntax") .then(CompileFunction); compiler.Environment() .dependency(new[] { "System.Threading", "System.Threading.Tasks", }) .dependency <ConcurrentObject>("Excess.Concurrent.Runtime"); }
public static void Apply(RoslynCompiler compiler, Options options = null, Scope scope = null) { if (scope != null) { var keywords = scope.get("keywords") as List <string>; if (keywords != null) { keywords.AddRange(GetKeywords()); } } if (options == null) { options = new Options(); } var lexical = compiler.Lexical(); lexical .match() .token("concurrent", named: "keyword") .token("class", named: "ref") .then(lexical.transform() .remove("keyword") .then(CompileClass(options))) .match() .token("concurrent", named: "keyword") .token("object", named: "ref") .then(lexical.transform() .remove("keyword") .replace("ref", "class ") .then(CompileObject(options))) .match() .token("concurrent", named: "keyword") .token("app", named: "ref") .token("{") .then(lexical.transform() .replace("keyword", "class ") .replace("ref", "__app") .then(CompileApp(options))); compiler.Environment() .dependency(new[] { "System.Threading", "System.Threading.Tasks", }) .dependency <ConcurrentObject>("Excess.Concurrent.Runtime"); if (options.GenerateAppProgram /*&& compilation != null */) { throw new NotImplementedException(); //var compilation = compiler.Compilation(); ////app support //var Programs = new List<ClassDeclarationSyntax>(); //var Singletons = new List<ClassDeclarationSyntax>(); //compilation // .match<ClassDeclarationSyntax>((@class, model, scpe) => // @class.Identifier.ToString() == "Program" // && @class // .Members // .OfType<MethodDeclarationSyntax>() // .Any(method => method.Identifier.ToString() == "Main")) // .then((node, model, scpe) => Programs.Add((ClassDeclarationSyntax)node)) // .match<ClassDeclarationSyntax>((@class, model, scpe) => isSingleton(@class)) // .then((node, model, scpe) => Singletons.Add((ClassDeclarationSyntax)node)) // .after(AddAppProgram(Programs, Singletons)); } }