private static LoweredFunction LowerFunction(BoundFunctionDeclaration declaration) { var loweredBody = Lowerer.Lower(declaration.Body); return(new LoweredFunction( declaration: declaration, name: declaration.Name, inputDescription: declaration.InputDescription, outputDescription: declaration.OutputDescription, body: loweredBody)); }
public LoweredFunction( BoundFunctionDeclaration declaration, string name, ImmutableArray <ParameterSymbol> inputDescription, ImmutableArray <ParameterSymbol> outputDescription, BoundBlockStatement body) { Declaration = declaration; Name = name; InputDescription = inputDescription; OutputDescription = outputDescription; Body = body; }
public static BoundProgram BindProgram(SyntaxTree syntaxTree) { var binder = new Binder(); var boundRoot = binder.BindRoot(syntaxTree.NullRoot); var statements = ((BoundBlockStatement)boundRoot.File.Body).Statements; var functionsBuilder = ImmutableDictionary.CreateBuilder <FunctionSymbol, LoweredFunction>(); var globalStatements = statements.Where(s => s.Kind != BoundNodeKind.FunctionDeclaration).ToArray(); var mainFunction = (FunctionSymbol?)null; var scriptFunction = (FunctionSymbol?)null; if (globalStatements.Length > 0) { // we have to gather all bound expression statements into a "script" function. scriptFunction = new FunctionSymbol("%script"); var body = Block(globalStatements[0].Syntax, globalStatements); var loweredBody = Lowerer.Lower(body); var declaration = new BoundFunctionDeclaration( syntax: globalStatements[0].Syntax, name: "%script", inputDescription: ImmutableArray <ParameterSymbol> .Empty, outputDescription: ImmutableArray <ParameterSymbol> .Empty, body: body); var loweredFunction = LowerFunction(declaration); functionsBuilder.Add(scriptFunction, loweredFunction); } var functions = statements.OfType <BoundFunctionDeclaration>().ToArray(); var first = true; foreach (var function in functions) { var functionSymbol = new FunctionSymbol( name: function.Name); var loweredFunction = LowerFunction(function); functionsBuilder.Add(functionSymbol, loweredFunction); if (first && globalStatements.Length == 0) { // the first function in a file will become "main". first = false; mainFunction = functionSymbol; } } return(new BoundProgram( binder._diagnostics.ToImmutableArray(), mainFunction, scriptFunction, functionsBuilder.ToImmutable())); }