示例#1
0
        public void run()
        {
            m_globalMemorySpace = new MemorySpace("globals");
            m_currentMemorySpace = m_globalMemorySpace;

            execute(m_ast);
        }
示例#2
0
        public void PushMemorySpace(MemorySpace pMemorySpace)
        {
            if(m_memorySpaces.Count > 0 && m_memorySpaces.Peek() == pMemorySpace) {
                //Console.WriteLine("Pushing duplicate memory space, will ignore");
                return;
            }

            //Console.WriteLine("Push " + pMemorySpace.getName() + " into scope " + this.getName());
            m_memorySpaces.Push(pMemorySpace);
        }
示例#3
0
文件: Scope.cs 项目: substans/Sprak
 public void PushMemorySpace(MemorySpace pMemorySpace)
 {
     //Console.WriteLine("Push " + pMemorySpace.getName() + " into scope " + this.getName());
     m_memorySpaces.Push(pMemorySpace);
 }
示例#4
0
        private ReturnValue function(AST tree, List<ReturnValue> parameterValues)
        {
            // Push scope
            Scope m_previousScope = m_currentScope;
            AST_FunctionDefinitionNode functionDefinitionNode = (AST_FunctionDefinitionNode)(tree);
            Assert.IsNotNull(functionDefinitionNode);
            m_currentScope = (Scope)functionDefinitionNode.getScope();
            Assert.IsNotNull(m_currentScope);

            // Push memory space
            MemorySpace m_previousMemorySpace = m_currentMemorySpace;
            MemorySpace functionMemorySpace =
                new MemorySpace("<FUNCTION_SPACE " + tree.getChild(1).getTokenString() + ">");
            m_memoryStack.Push(functionMemorySpace);
            m_currentMemorySpace = functionMemorySpace;

            // Add parameters to memory space
            List<AST> parameterDeclarations = tree.getChild(2).getChildren();
            if(parameterDeclarations != null) {

                if(parameterDeclarations.Count != parameterValues.Count) {
                    m_errorHandler.errorOccured(
                        "The number of arguments in function " +
                        tree.getChild(1).getTokenString() +
                        " does not match!",
                        Error.ErrorType.SYNTAX);
                }

                foreach(AST parameter in parameterDeclarations) {
                    varDeclaration(parameter);
                }
            }

            // Assign values to parameters
            if(parameterValues != null) {
                int i = 0;
                foreach(ReturnValue parameterValue in parameterValues) {

                    string parameterName = parameterDeclarations[i].getChild(1).getTokenString();
                    assignValue(parameterName, parameterValue);
                    i++;
                }
            }

            // Execute function
            ReturnValue returnValue = null;

            try {
                executeAllChildNodes(tree.getChild(3)); // child 3 is the function body
            }
            catch(ReturnValue functionReturnValue) {
                returnValue = functionReturnValue;
            }

            // Pop memory space
            m_memoryStack.Pop();
            m_currentMemorySpace = m_previousMemorySpace;

            // Pop scope
            m_currentScope = m_previousScope;
            Assert.IsNotNull(m_currentScope);

            return returnValue;
        }
示例#5
0
 public void PushMemorySpace(MemorySpace pMemorySpace)
 {
     //Console.WriteLine("Push " + pMemorySpace.getName() + " into scope " + this.getName());
     m_memorySpaces.Push(pMemorySpace);
 }
示例#6
0
        private ReturnValue function(AST tree, List <ReturnValue> parameterValues)
        {
            // Push scope
            Scope m_previousScope = m_currentScope;
            AST_FunctionDefinitionNode functionDefinitionNode = (AST_FunctionDefinitionNode)(tree);

            Assert.IsNotNull(functionDefinitionNode);
            m_currentScope = (Scope)functionDefinitionNode.getScope();
            Assert.IsNotNull(m_currentScope);

            // Push memory space
            MemorySpace m_previousMemorySpace = m_currentMemorySpace;
            MemorySpace functionMemorySpace   =
                new MemorySpace("<FUNCTION_SPACE " + tree.getChild(1).getTokenString() + ">");

            m_memoryStack.Push(functionMemorySpace);
            m_currentMemorySpace = functionMemorySpace;

            // Add parameters to memory space
            List <AST> parameterDeclarations = tree.getChild(2).getChildren();

            if (parameterDeclarations != null)
            {
                if (parameterDeclarations.Count != parameterValues.Count)
                {
                    m_errorHandler.errorOccured(
                        "The number of arguments in function " +
                        tree.getChild(1).getTokenString() +
                        " does not match!",
                        Error.ErrorType.SYNTAX);
                }

                foreach (AST parameter in parameterDeclarations)
                {
                    varDeclaration(parameter);
                }
            }

            // Assign values to parameters
            if (parameterValues != null)
            {
                int i = 0;
                foreach (ReturnValue parameterValue in parameterValues)
                {
                    string parameterName = parameterDeclarations[i].getChild(1).getTokenString();
                    assignValue(parameterName, parameterValue);
                    i++;
                }
            }

            // Execute function
            ReturnValue returnValue = null;

            try {
                executeAllChildNodes(tree.getChild(3));                 // child 3 is the function body
            }
            catch (ReturnValue functionReturnValue) {
                returnValue = functionReturnValue;
            }

            // Pop memory space
            m_memoryStack.Pop();
            m_currentMemorySpace = m_previousMemorySpace;

            // Pop scope
            m_currentScope = m_previousScope;
            Assert.IsNotNull(m_currentScope);

            return(returnValue);
        }