Пример #1
0
 private void assignment(AST tree)
 {
     string variableName = tree.getChild(0).getTokenString();
     AST expression = tree.getChild(1);
     ReturnValue expressionValue = execute(expression);
     assignValue(variableName, expressionValue);
 }
Пример #2
0
        public void addChildFirst(AST childTree)
        {
            Debug.Assert(childTree != null);

            allocateListIfNecessary();
            m_children.Insert(0, childTree);
        }
Пример #3
0
        public void addChildAtPosition(AST childTree, int pos)
        {
            Debug.Assert(childTree != null);

            allocateListIfNecessary();
            m_children.Insert(pos, childTree);
        }
Пример #4
0
        private void evaluateIfScope(AST tree)
        {
            Scope subscope = new Scope(Scope.ScopeType.IF_SCOPE,"<IF-SUBSCOPE>", m_currentScope);

            #if WRITE_DEBUG_INFO
            Console.WriteLine("\nDefined IF-subscope");
            #endif

            m_currentScope = subscope;

            AST_IfNode ifNode = (tree as AST_IfNode);
            Debug.Assert(ifNode != null);

            ifNode.setScope(subscope); // save the new scope in the IF-token tree node

            // Evaluate expression
            evaluateScopeDeclarationsInAllChildren(tree.getChild(0));

            AST trueNode = ifNode.getChild(1);
            AST falseNode = null;
            if (ifNode.getChildren().Count == 3)
            {
                falseNode = ifNode.getChild(2);
            }

            evaluateScopeDeclarationsInAllChildren(trueNode);
            if (falseNode != null)
            {
                evaluateScopeDeclarationsInAllChildren(falseNode);
            }

            m_currentScope = m_currentScope.getEnclosingScope(); // pop scope
        }
Пример #5
0
        public MemorySpace(string name, AST root, Scope scope, MemorySpaceNodeListCache cache)
        {
            Debug.Assert(name != null);
            Debug.Assert(root != null);
            Debug.Assert(scope != null);
            Debug.Assert(cache != null);

            m_name = name;
            m_scope = scope;
            m_cache = cache;

            //Console.WriteLine("Creating list of nodes from tree: " + root.getTreeAsString());

            if(m_cache.hasCachedFunction(root)) {
                //Console.WriteLine("Found cached list for " + m_name);
                m_nodes = m_cache.getList(root).ToArray();
            }
            else {
                List<AST> list = new List<AST>();
                addToList(list, root);
                m_cache.addMemorySpaceList(list, root);
                m_nodes = list.ToArray();
                //Console.WriteLine("Created new list for " + m_name);
            }

            m_currentNode = -1;

            //Console.WriteLine("New memory space " + name + " has got " + list.Count + " AST nodes in its list.");
        }
Пример #6
0
        public ScopeBuilder(AST ast, ErrorHandler errorHandler)
        {
            Debug.Assert(ast != null);
            Debug.Assert(errorHandler != null);

            m_errorHandler = errorHandler;
            m_ast = ast;
        }
Пример #7
0
        public void addChild(AST childTree)
        {
            Debug.Assert(childTree != null);
            if(childTree == null) throw new Exception("Child tree was null!");

            allocateListIfNecessary();
            m_children.Add(childTree);
        }
Пример #8
0
 public Interpreter(AST ast, Scope globalScope, ErrorHandler errorHandler, ExternalFunctionCreator externalFunctionCreator)
 {
     m_ast = ast;
     m_errorHandler = errorHandler;
     m_globalScope = globalScope;
     m_currentScope = m_globalScope;
     m_externalFunctionCreator = externalFunctionCreator;
 }
Пример #9
0
        private string GetNodeString( AST pNode )
        {
			string result;
			result = /*pNode.getTokenType().ToString() + " : " + */ pNode.ToString();
			if(_printExecutions) {
				result += " : " + pNode.Executions;
			}
            return result;
        }
Пример #10
0
        public FunctionSymbol(Scope enclosingScope, string name, ReturnValueType type, AST functionDefinitionNode)
            : base(Scope.ScopeType.FUNCTION_SCOPE, name, enclosingScope)
        {
            Debug.Assert(enclosingScope != null);
            Debug.Assert(functionDefinitionNode != null);

            m_enclosingScope = enclosingScope;
            m_functionDefinitionNode = functionDefinitionNode;
            m_returnValueType = type;
        }
 public void BooleanAND()
 {
     AST root = new AST(new Token(Token.TokenType.OPERATOR, "&&"));
     AST lhs = new AST(new Token(Token.TokenType.NUMBER, "1"));
     AST rhs = new AST(new Token(Token.TokenType.NUMBER, "0"));
     root.addChild(lhs);
     root.addChild(rhs);
     ExpressionEvaluator e = new ExpressionEvaluator(root);
     Assert.AreEqual(0, e.getValue());
 }
Пример #12
0
        public void addChild(AST childTree)
        {
            if (childTree == null) {
                //throw new Exception ("Child tree is null");
                if(childTree == null) throw new Error("Failed to understand source code", Error.ErrorType.SYNTAX, -1, -1);
            }

            allocateListIfNecessary();
            m_children.Add(childTree);
        }
 public void BooleanGREATEROREQUALS2()
 {
     AST root = new AST(new Token(Token.TokenType.OPERATOR, ">="));
     AST lhs = new AST(new Token(Token.TokenType.NUMBER, "4"));
     AST rhs = new AST(new Token(Token.TokenType.NUMBER, "3"));
     root.addChild(lhs);
     root.addChild(rhs);
     ExpressionEvaluator e = new ExpressionEvaluator(root);
     Assert.AreEqual(1, e.getValue());
 }
Пример #14
0
        private AST_FunctionDefinitionNode createFunctionDefinitionNode(string returnTypeName, string functionName, AST parameterList)
        {
            AST_FunctionDefinitionNode functionNode =
                new AST_FunctionDefinitionNode(new Token(Token.TokenType.FUNC_DECLARATION, "<EXTERNAL_FUNC_DECLARATION>"));

            functionNode.addChild(new Token(Token.TokenType.BUILT_IN_TYPE_NAME, returnTypeName));
            functionNode.addChild(new Token(Token.TokenType.NAME, functionName));
            functionNode.addChild(parameterList);
            functionNode.addChild(new Token(Token.TokenType.STATEMENT_LIST, "<EMPTY_STATEMENT_LIST>"));

            return functionNode;
        }
Пример #15
0
        private AST createParameterDefinition(string typeName, string variableName)
        {
            AST parameter = new AST(new Token(Token.TokenType.PARAMETER, "<PARAMETER>"));

            AST declaration = new AST_VariableDeclaration(new Token(Token.TokenType.VAR_DECLARATION, "<PARAMETER_DECLARATION>"), ReturnValue.getReturnValueTypeFromString(typeName), variableName);
            AST assigment = new AST_Assignment(new Token(Token.TokenType.ASSIGNMENT, "="), variableName);

            parameter.addChild(declaration);
            parameter.addChild(assigment);

            return parameter;
        }
Пример #16
0
 private float evaluate(AST tree)
 {
     float returnValue = 0;
     if(tree.getTokenType() == Token.TokenType.NUMBER) {
         returnValue = (float)System.Convert.ToDouble(tree.getTokenString());
     }
     else if(tree.getTokenType() == Token.TokenType.OPERATOR) {
         if(tree.getTokenString() == "+") {
             returnValue = evaluate(tree.getChild(0)) + evaluate(tree.getChild(1));
         }
         else if(tree.getTokenString() == "-") {
             returnValue = evaluate(tree.getChild(0)) - evaluate(tree.getChild(1));
         }
         else if(tree.getTokenString() == "*") {
             returnValue = evaluate(tree.getChild(0)) * evaluate(tree.getChild(1));
         }
         else if(tree.getTokenString() == "/") {
             returnValue = evaluate(tree.getChild(0)) / evaluate(tree.getChild(1));
         }
         else if(tree.getTokenString() == "<") {
             returnValue = (evaluate(tree.getChild(0)) < evaluate(tree.getChild(1))) ? 1 : 0;
         }
         else if(tree.getTokenString() == ">") {
             returnValue = (evaluate(tree.getChild(0)) > evaluate(tree.getChild(1))) ? 1 : 0;
         }
         else if(tree.getTokenString() == "<=") {
             returnValue = (evaluate(tree.getChild(0)) <= evaluate(tree.getChild(1))) ? 1 : 0;
         }
         else if(tree.getTokenString() == ">=") {
             returnValue = (evaluate(tree.getChild(0)) >= evaluate(tree.getChild(1))) ? 1 : 0;
         }
         else if(tree.getTokenString() == "&&") {
             returnValue = (evaluate(tree.getChild(0)) != 0 && evaluate(tree.getChild(1)) != 0) ? 1 : 0;
         }
         else if(tree.getTokenString() == "||") {
             returnValue = (evaluate(tree.getChild(0)) != 0 || evaluate(tree.getChild(1)) != 0) ? 1 : 0;
         }
         else if(tree.getTokenString() == "!=") {
             returnValue = (evaluate(tree.getChild(0)) != evaluate(tree.getChild(1))) ? 1 : 0;
         }
         else if(tree.getTokenString() == "==") {
             returnValue = (evaluate(tree.getChild(0)) == evaluate(tree.getChild(1))) ? 1 : 0;
         }
         else {
             throw new InvalidOperationException("ExpressionEvaluator can't handle operators with string " + tree.getTokenString());
         }
     }
     else {
         throw new InvalidOperationException("ExpressionEvaluator can't handle tokens of type " + tree.getTokenType());
     }
     return returnValue;
 }
Пример #17
0
 private void GetLeafNodes(AST pCurrentNode, List<AST> pList)
 {
     List<AST> tmpList = pCurrentNode.getChildren();
     if (tmpList == null || tmpList.Count <= 0)
     {
         pList.Add(pCurrentNode);
     }
     else
     {
         foreach (AST child in tmpList)
         {
             GetLeafNodes(child, pList);
         }
     }
 }
Пример #18
0
        public void PaintAST(AST pRoot)
        {
            _root = pRoot;
            //build a list of all nodes
            List<AST> tmpAllNodes = new List<AST>();
            BuildNodeList(pRoot, tmpAllNodes);
            allNodes = tmpAllNodes.ToArray();

            //collect all leaf nodes
            List<AST> leafNodes = new List<AST>();
            GetLeafNodes(pRoot, leafNodes);

            foreach (AST leaf in allNodes)
            {
                Console.WriteLine(BuildRow(leaf, pRoot));
            }
        }
Пример #19
0
        private void defineFunction(FunctionDefinition f)
        {
            if (externalFunctions.ContainsKey(f.functionName))
            {
                throw new Error("There is already a function called '" + f.functionName + "'", Error.ErrorType.UNDEFINED, 0, 0);
            }

            AST parameterList = new AST(new Token(Token.TokenType.NODE_GROUP, "<PARAMETER_LIST>"));
            for (int i = 0; i < f.parameterTypes.Length; ++i)
            {
                parameterList.addChild(createParameterDefinition(f.parameterTypes[i], f.parameterNames[i]));
            }

            AST functionNode = createFunctionDefinitionNode(f.returnType, f.functionName, parameterList);

            m_builtInFunctions.Add(functionNode);
            externalFunctions.Add(f.functionName, f.callback);
        }
Пример #20
0
        public Parser(List<Token> tokens, ErrorHandler errorHandler)
        {
            m_tokens = tokens;
            m_errorHandler = errorHandler;

            m_nextTokenIndex = 0;
            m_lookahead = new Token[k];
            m_lookaheadIndex = 0;

            //Fill lookahead buffer:
            for (int i = 0; i < k; i++) {
                consumeCurrentToken();
            }

            m_programAST = new AST(new Token(Token.TokenType.PROGRAM_ROOT, "<PROGRAM_ROOT>"));
            m_isInsideFunctionDefinition = false;
            m_processed = false;
        }
Пример #21
0
        private void evaluateFunctionScope(AST tree)
        {
            // Define function name
            ReturnValueType returnType = ReturnValue.getReturnValueTypeFromString(tree.getChild(0).getTokenString());
            string functionName = tree.getChild(1).getTokenString();

            Symbol functionScope = new FunctionSymbol(m_currentScope, functionName, returnType, tree);
            m_globalScope.define(functionScope); // all functions are saved in the global scope
            m_currentScope = (Scope)functionScope;
            AST_FunctionDefinitionNode functionCallNode = (AST_FunctionDefinitionNode)(tree);
            functionCallNode.setScope((Scope)functionScope);

            #if WRITE_DEBUG_INFO
            Console.WriteLine("\nDefined function with name " + functionName + " and return type " + returnType);
            #endif

            // Process the body of the function
            evaluateScopeDeclarations(tree.getChild(3));

            m_currentScope = m_currentScope.getEnclosingScope(); // pop scope
        }
Пример #22
0
        public AST findParent(AST ofThisChild)
        {
            Console.WriteLine("Going into " + getTokenString());

            if(ofThisChild == null) { return null; }
            if(m_children == null) { return null; }

            int i = m_children.IndexOf(ofThisChild);
            if(i >= 0) {
                Console.WriteLine("Found " + ofThisChild.getTokenString());
                return this;
            }
            else {
                foreach(AST child in m_children) {
                    AST parent = child.findParent(ofThisChild);
                    if(parent != null) {
                        return parent;
                    }
                }
            }
            return null;
        }
Пример #23
0
 private AST GetParent(AST pLeaf)
 {
     foreach (AST a in allNodes)
     {
         foreach (AST child in a.getChildren())
         {
             if (child == pLeaf)
                 return a;
         }
     }
     return null;
 }
Пример #24
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);
        }
Пример #25
0
        private ReturnValue quotedString(AST tree)
        {
            ReturnValue returnValue = new ReturnValue(tree.getTokenString());

            return(returnValue);
        }
 public void addMemorySpaceList(List <AST> list, AST rootNode)
 {
     m_lists.Add(rootNode, list);
 }
Пример #27
0
        private ReturnValue operation(AST tree)
        {
            ReturnValue returnValue = null;

            float lhs = execute(tree.getChild(0)).FloatValue;
            float rhs = execute(tree.getChild(1)).FloatValue;

            if (tree.getTokenString() == "+")
            {
                returnValue = new ReturnValue(lhs + rhs);
            }
            else if (tree.getTokenString() == "-")
            {
                returnValue = new ReturnValue(lhs - rhs);
            }
            else if (tree.getTokenString() == "*")
            {
                returnValue = new ReturnValue(lhs * rhs);
            }
            else if (tree.getTokenString() == "/")
            {
                returnValue = new ReturnValue(lhs / rhs);
            }
            else if (tree.getTokenString() == "<")
            {
                float v = lhs < rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == ">")
            {
                float v = lhs > rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == "<=")
            {
                float v = lhs <= rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == ">=")
            {
                float v = lhs >= rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == "==")
            {
                float v = lhs == rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == "!=")
            {
                float v = lhs != rhs ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == "&&")
            {
                float v = ((lhs != 0 ? true : false) && (rhs != 0 ? true : false)) ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else if (tree.getTokenString() == "||")
            {
                float v = ((lhs != 0 ? true : false) || (rhs != 0 ? true : false)) ? 1 : 0;
                returnValue = new ReturnValue(v);
            }
            else
            {
                throw new NotImplementedException("Operator " + tree.getTokenString() + " isn't implemented yet!");
            }

            return(returnValue);
        }
Пример #28
0
 public int getIndexOfChild(AST child)
 {
     if(m_children != null) {
         return m_children.IndexOf(child);
     }
     else {
         return -1;
     }
 }
Пример #29
0
 private void PaintAST(AST ast)
 {
     ASTPainter painter = new ASTPainter();
     painter.PaintAST(ast);
 }
Пример #30
0
 private Scope CreateScopeTree(AST ast)
 {
     ScopeBuilder scopeBuilder = new ScopeBuilder(ast, m_compileTimeErrorHandler);
     scopeBuilder.process();
     Scope globalScope = scopeBuilder.getGlobalScope();
     //Console.WriteLine("\nScopes: \n" + globalScope + "\n\n");
     return globalScope;
 }
Пример #31
0
        public void addChild(Token token)
        {
            AST childTree = new AST(token);

            addChild(childTree);
        }
 public bool hasCachedFunction(AST rootNode)
 {
     return(m_lists.ContainsKey(rootNode));
 }
Пример #33
0
 private void BuildNodeList(AST pCurrentList, List<AST> pList)
 {
     pList.Add(pCurrentList);
     foreach(AST a in pCurrentList.getChildren())
     {
         BuildNodeList(a, pList);
     }
 }
Пример #34
0
 private bool IsLastChild(AST pNode, AST pParent)
 {
     if (pParent == null)
         return true;
     if (pParent.getChildren().IndexOf(pNode) == pParent.getChildren().Count - 1)
         return true;
     return false;
 }
Пример #35
0
 public void addChild(Token token)
 {
     AST childTree = new AST(token);
     addChild(childTree);
 }
Пример #36
0
        private ReturnValue number(AST tree)
        {
            ReturnValue returnValue = new ReturnValue((float)Convert.ToDouble(tree.getTokenString()));

            return(returnValue);
        }
 public List <AST> getList(AST rootNode)
 {
     return(m_lists[rootNode]);
 }
Пример #38
0
        private string BuildRow(AST pLeaf, AST pRoot)
        {
            currentNode = pLeaf;
            StringBuilder resultString = new StringBuilder();
            if (currentNode == _root)
            {
                PrependString(resultString, GetNodeString(currentNode) );
            }
            else if (currentNode.getChildren().Count <= 0) {
#if CANT_HANDLE_FANCY_CHARS
				PrependString(resultString, "-------- " + GetNodeString(currentNode) );
#else
                PrependString(resultString, "──────── " + GetNodeString(currentNode) );
#endif
			}
            else
#if CANT_HANDLE_FANCY_CHARS
                PrependString(resultString, "-------- " + GetNodeString(currentNode) );
#else
				PrependString(resultString, "──────┬─ " + GetNodeString(currentNode) );
#endif
         
            if (currentNodeIsLastChild)
            {
#if CANT_HANDLE_FANCY_CHARS
                PrependString(resultString, "      |");
#else
				PrependString(resultString, "      └");
#endif				
            }
            else
            {
#if CANT_HANDLE_FANCY_CHARS
                PrependString(resultString, "      |");
#else 
				PrependString(resultString, "      ├");
#endif
            }
            currentNode = currentParentNode;
            while (currentParentNode != null)
            {
                if (currentNodeIsLastChild)
                {
					PrependString(resultString, "       ");
                }
                else
                {
#if CANT_HANDLE_FANCY_CHARS
                    PrependString(resultString, "      |");
#else
					PrependString(resultString, "      │");
#endif
                }
                currentNode = currentParentNode;
            }

            return resultString.ToString();
        }
Пример #39
0
        private AST_FunctionDefinitionNode createFunctionDefinitionNode(string returnTypeName, string functionName, AST parameterList)
        {
            AST_FunctionDefinitionNode functionNode =
                new AST_FunctionDefinitionNode(new Token(Token.TokenType.FUNC_DECLARATION, "<EXTERNAL_FUNC_DECLARATION>"));

            functionNode.addChild(new Token(Token.TokenType.BUILT_IN_TYPE_NAME, returnTypeName));
            functionNode.addChild(new Token(Token.TokenType.NAME, functionName));
            functionNode.addChild(parameterList);
            functionNode.addChild(new Token(Token.TokenType.STATEMENT_LIST, "<EMPTY_STATEMENT_LIST>"));

            return(functionNode);
        }