static public MethodDeclarationSyntax CreateStaticMethod(DreamGrammarParser.FunctionDeclarationContext context, DreamRoslynVisitor visitor) { String methodName = context.funcName().GetText(); //Since C# main methods musbe be named Main this converts to C# style. if (methodName.Equals(Constants.JuiliarMain)) { methodName = Constants.RoslynMain; } SyntaxToken name = SyntaxFactory.Identifier(methodName); SyntaxToken returnType = SyntaxFactory.Token(SyntaxKind.VoidKeyword); var statementList = new List <StatementSyntax>(); var tlist = new List <SyntaxNodeOrToken>(); foreach (IParseTree child in context.children) { CSharpSyntaxNode node = child.Accept(visitor); if (node != null) { if (node is ParameterSyntax) { tlist.Add((ParameterSyntax)node); } else if (node is StatementSyntax) { statementList.Add(node as StatementSyntax); } } } var block = SyntaxFactory.Block().AddStatements(statementList.ToArray()); if (tlist.Count > 0) { return(SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(returnType), name).WithBody(block) .WithModifiers(SyntaxFactory.TokenList(new[] { SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PublicKeyword) })) .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(tlist)))); } return(SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(returnType), name) .WithBody( block) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.PublicKeyword) }))); }
public override CSharpSyntaxNode VisitFunctionDeclaration([NotNull] DreamGrammarParser.FunctionDeclarationContext context) { MethodDeclarationSyntax method = GenerateStaticMethod.CreateStaticMethod(context, this); return(method); }
public override Expression VisitFunctionDeclaration(DreamGrammarParser.FunctionDeclarationContext context) { return(base.VisitFunctionDeclaration(context)); }
public override Expression VisitFunctionDeclaration(DreamGrammarParser.FunctionDeclarationContext context) { var children = context.children; var i = children.Count; var currentChild = 0; var functionKeyWord = RemoveLeadingAndTrailingQuotes(children[currentChild].Accept(this).ToString()); if (!functionKeyWord.Equals("function")) { throw new Exception("invalid function declaration"); } var functionName = children[++currentChild].Accept(this); var parameters = RemoveLeadingAndTrailingQuotes(children[++currentChild].Accept(this).ToString()); var expressionList = new List <Expression>(); if (parameters.Equals("()")) { // no input parameters read equal sign and brackets var equalSign = RemoveLeadingAndTrailingQuotes(children[++currentChild].Accept(this).ToString()); if (equalSign.Equals("=")) { var leftBracket = RemoveLeadingAndTrailingQuotes(children[++currentChild].Accept(this).ToString()); if (!leftBracket.Equals("{")) { throw new Exception("No left bracket"); } while (currentChild <= i) { var currentStatementExpression = children[++currentChild].Accept(this); if (RemoveLeadingAndTrailingQuotes(currentStatementExpression.ToString()).Equals("}")) { break; } else { expressionList.Add(currentStatementExpression); } } } } // Test expression to add to the bottom of the list of expressions. var localParameters = new List <ParameterExpression>(); var expressionsToAdd = new List <Expression>(); LabelTarget label = Expression.Label(); expressionsToAdd.Add(Expression.Label(label)); highLevelFunctions.Add(RemoveLeadingAndTrailingQuotes(functionName.ToString()), label); // THIS LOOP NEEDS to // BE REFACTORED // UGLY CODE AHEAD foreach (var parm in expressionList) { if (parm is ParameterExpression item) { localParameters.Add(item); continue; } if (parm is DreamMulitipleVariableExpressions itemExpression) { foreach (var exp in itemExpression.GetExpressions()) { if (exp is ParameterExpression parameterExpression) { localParameters.Add(parameterExpression); } else { expressionsToAdd.Add(exp); } } continue; } if (parm is DreamMethodCall dreamMethodCall) { expressionsToAdd.Add(dreamMethodCall); expressionsToAdd.Add(Expression.Label(dreamMethodCall.ReturnTarget)); expressionsToAdd.Add(Expression.Goto(dreamMethodCall.ReturnTarget)); continue; } if (parm is PrintExpression p) { expressionsToAdd.Add(p); continue; } } expressionList.RemoveRange(0, localParameters.Count); expressionList.AddRange(expressionsToAdd); /* * expressionList.Add(Expression.Call(null, * typeof(Trace).GetMethod("WriteLine", new Type[] {typeof(String)}) ?? * throw new InvalidOperationException(), * Expression.Constant("0s"))); */ return(Expression.Block(localParameters, expressionsToAdd)); }