Beispiel #1
0
        private SyntaxNode Bind(FunctionNode node, Dictionary <string, string> env)
        {
            Dictionary <string, string> localEnvironment = new Dictionary <string, string>();
            string name = "f" + GetFunctionCount();

            env.Add(node.functionName.identifier, name);
            List <ParameterNode> boundParams     = BindParameters(node.parameters, localEnvironment);
            List <StatementNode> boundStatements = new List <StatementNode>();

            foreach (StatementNode statement in node.body)
            {
                StatementNode boundStatement = Bind(statement, localEnvironment);
                boundStatements.Add(boundStatement);
            }
            ReturnNode boundReturn = null;

            if (node.returnNode != null)
            {
                boundReturn = BindReturn(node.returnNode, localEnvironment);
            }
            return(new FunctionNode(new IdentifierNode(name), boundParams, boundStatements, boundReturn));
        }
Beispiel #2
0
        private void Generate(FunctionNode node, List <Instruction> instructions)
        {
            string name = node.functionName.identifier;
            List <ParameterInstruction> parameters = new List <ParameterInstruction>();

            foreach (ParameterNode param in node.parameters)
            {
                parameters.Add(new ParameterInstruction(param.identifier));
            }
            List <Instruction> funcInstructions = new List <Instruction>();

            foreach (StatementNode statement in node.body)
            {
                Generate(statement, funcInstructions);
            }
            ReturnInstruction returnInstruction = null;

            if (node.returnNode != null)
            {
                List <Instruction> returnInstructions = new List <Instruction>();
                returnInstruction = GenerateReturn(node.returnNode);
            }
            instructions.Add(new FunctionInstruction(name, parameters, funcInstructions, returnInstruction));
        }