public void Analyze_Keyword_ReturnsIdentifierNode()
        {
            SyntaxNode root = new IdentifierNode("e");

            root = SemanticAnalyzer.Analyze(root, new Environment());

            Assert.AreEqual("e", root.ToString());
        }
        public void Analyze_VariableSymbol_ReturnsIdentifierNode()
        {
            SyntaxNode  node        = new IdentifierNode("x");
            Environment environment = new Environment();

            environment.AddSymbol("x");

            node = SemanticAnalyzer.Analyze(node, environment);

            Assert.AreEqual("x", node.ToString());
        }
        public void Analyze_RecognizeShorthandMultiplicationInsideIdentiferWithOneVariable_ReturnsOperatorNode()
        {
            SyntaxNode  root        = new IdentifierNode("xx");
            Environment environment = new Environment();

            environment.AddSymbol("x");

            root = SemanticAnalyzer.Analyze(root, environment);

            Assert.AreEqual("x * x", root.ToString());
        }
        public void Analyze_RecognizeShorthandMultiplicationInsideIdentifierWithPredefinedSymbols_ReturnsIdentifierNode()
        {
            SyntaxNode  root        = new IdentifierNode("xe");
            Environment environment = new Environment();

            environment.AddSymbol("x");

            root = SemanticAnalyzer.Analyze(root, environment);

            Assert.AreEqual("x * e", root.ToString());
        }
Beispiel #5
0
        public Variable(IdentifierNode identNode)
        {
            if (identNode == null)
            {
                throw new ArgumentNullException();
            }

            Name        = identNode.ToString();
            Row         = identNode.line;
            StartColumn = identNode.col;
        }
Beispiel #6
0
        public static string GenerateIdentListNameString(AssociativeNode node)
        {
            IdentifierListNode iNode;
            AssociativeNode    leftNode   = node;
            List <string>      stringList = new List <string>();

            while (leftNode is IdentifierListNode)
            {
                iNode    = leftNode as IdentifierListNode;
                leftNode = iNode.LeftNode;
                if (iNode.RightNode is IdentifierNode)
                {
                    IdentifierNode currentNode = (iNode.RightNode as IdentifierNode);
                    stringList.Add(currentNode.ToString());
                }
                else if (iNode.RightNode is FunctionCallNode)
                {
                    FunctionCallNode fCall = iNode.RightNode as FunctionCallNode;
                    stringList.Add(fCall.Function.Name);
                }
                else
                {
                    return(string.Empty);
                }
            }
            stringList.Add(leftNode.ToString());

            stringList.Reverse();

            string retString = string.Empty;

            foreach (string s in stringList)
            {
                retString += s;
                retString += '.';
            }

            // Remove the last dot
            retString = retString.Remove(retString.Length - 1);

            return(retString);
        }
        public override string ToString()
        {
            string str = "";

            if (IsReference)
            {
                str += "&";
            }

            if (ModuleIdentifier != null)
            {
                str += $"{ModuleIdentifier}.{TypeIdentifier}";
            }
            else
            {
                str += TypeIdentifier.ToString();
            }

            for (int i = 0; i < PointerLevel; i++)
            {
                str += "*";
            }

            if (IsArray)
            {
                if (ArraySizeExpression != null)
                {
                    str += $"[{ArraySizeExpression}]";
                }
                else
                {
                    str += "[]";
                }
            }

            return(str);
        }
        public override AssociativeNode VisitIdentifierNode(IdentifierNode node)
        {
            var name = node.ToString();

            string[] strIdentList = name.Split('.');

            // return IdentifierNode if identifier string is not separated by '.'
            if (strIdentList.Length == 1)
            {
                return(new IdentifierNode(strIdentList[0]));
            }

            // create IdentifierListNode from string such that
            // RightNode is IdentifierNode representing classname
            // and LeftNode is IdentifierNode representing one or more namespaces.
            var           rightNode = new IdentifierNode(strIdentList.Last());
            StringBuilder bld       = new StringBuilder();

            for (int i = 0; i < strIdentList.Length - 1; i++)
            {
                if (!string.IsNullOrEmpty(bld.ToString()))
                {
                    bld.Append(".");
                }
                bld.Append(strIdentList[i]);
            }
            var leftNode = new IdentifierNode(bld.ToString());

            var identListNode = new IdentifierListNode
            {
                LeftNode  = leftNode,
                RightNode = rightNode,
                Optr      = Operator.dot
            };

            return(identListNode.Accept(this));
        }
Beispiel #9
0
        private void GetNamespaces(ref List <NamespaceNode> lista, NamespaceNode tree, IdentifierNode parent_identifiers)
        {
            var current_namespace = new NamespaceNode();

            current_namespace.identifier              = tree.identifier;
            current_namespace.using_array             = tree.using_array;
            current_namespace.types_declaration_array = tree.types_declaration_array;

            int parent_pos  = -1;
            var parent_name = parent_identifiers.ToString();
            var i           = 0;

            foreach (var nsp in lista)
            {
                if (nsp.ToString() == parent_name)
                {
                    parent_pos = i;
                }
                i++;
            }

            current_namespace.parent_position = parent_pos;
            if (parent_pos >= 0)
            {
                current_namespace.using_array.Add(new UsingNode(lista[parent_pos].identifier));
            }
            current_namespace.identifier.identifiers.InsertRange(0, parent_identifiers.identifiers);

            lista.Add(current_namespace);


            foreach (var nspname in tree.namespace_array)
            {
                GetNamespaces(ref lista, nspname, current_namespace.identifier);
            }
        }