コード例 #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
ファイル: AST.cs プロジェクト: bloomingbridges/hxSprak
        public void addChildFirst(AST childTree)
        {
            Debug.Assert(childTree != null);

            allocateListIfNecessary();
            m_children.Insert(0, childTree);
        }
コード例 #3
0
ファイル: AST.cs プロジェクト: bloomingbridges/hxSprak
        public void addChildAtPosition(AST childTree, int pos)
        {
            Debug.Assert(childTree != null);

            allocateListIfNecessary();
            m_children.Insert(pos, childTree);
        }
コード例 #4
0
ファイル: ScopeBuilder.cs プロジェクト: substans/Sprak
        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
ファイル: MemorySpace.cs プロジェクト: substans/Sprak
        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
ファイル: ScopeBuilder.cs プロジェクト: substans/Sprak
        public ScopeBuilder(AST ast, ErrorHandler errorHandler)
        {
            Debug.Assert(ast != null);
            Debug.Assert(errorHandler != null);

            m_errorHandler = errorHandler;
            m_ast = ast;
        }
コード例 #7
0
ファイル: AST.cs プロジェクト: substans/Sprak
        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
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
        private string GetNodeString( AST pNode )
        {
			string result;
			result = /*pNode.getTokenType().ToString() + " : " + */ pNode.ToString();
			if(_printExecutions) {
				result += " : " + pNode.Executions;
			}
            return result;
        }
コード例 #10
0
ファイル: FunctionSymbol.cs プロジェクト: substans/Sprak
        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;
        }
コード例 #11
0
 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
ファイル: AST.cs プロジェクト: bloomingbridges/hxSprak
        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);
        }
コード例 #13
0
 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
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
 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
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
        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
ファイル: Parser.cs プロジェクト: bloomingbridges/hxSprak
        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
ファイル: ScopeBuilder.cs プロジェクト: substans/Sprak
        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
ファイル: AST.cs プロジェクト: substans/Sprak
        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
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
 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);
        }
コード例 #26
0
 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
ファイル: AST.cs プロジェクト: bloomingbridges/hxSprak
 public int getIndexOfChild(AST child)
 {
     if(m_children != null) {
         return m_children.IndexOf(child);
     }
     else {
         return -1;
     }
 }
コード例 #29
0
ファイル: SprakRunner.cs プロジェクト: substans/Sprak
 private void PaintAST(AST ast)
 {
     ASTPainter painter = new ASTPainter();
     painter.PaintAST(ast);
 }
コード例 #30
0
ファイル: SprakRunner.cs プロジェクト: substans/Sprak
 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
ファイル: AST.cs プロジェクト: substans/Sprak
        public void addChild(Token token)
        {
            AST childTree = new AST(token);

            addChild(childTree);
        }
コード例 #32
0
 public bool hasCachedFunction(AST rootNode)
 {
     return(m_lists.ContainsKey(rootNode));
 }
コード例 #33
0
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
 private void BuildNodeList(AST pCurrentList, List<AST> pList)
 {
     pList.Add(pCurrentList);
     foreach(AST a in pCurrentList.getChildren())
     {
         BuildNodeList(a, pList);
     }
 }
コード例 #34
0
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
 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
ファイル: AST.cs プロジェクト: bloomingbridges/hxSprak
 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);
        }
コード例 #37
0
 public List <AST> getList(AST rootNode)
 {
     return(m_lists[rootNode]);
 }
コード例 #38
0
ファイル: ASTPainter.cs プロジェクト: substans/Sprak
        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);
        }