コード例 #1
0
 public SemanticAtom Visit(ArrayAssignment n)
 {
     n.Destination.Accept(this);
     n.Index.Accept(this);
     n.Source.Accept(this);
     return(null);
 }
コード例 #2
0
 public void Visit(ArrayAssignment n)
 {
     n.Destination.Accept(this);
     Helpers.Write("[");
     n.Index.Accept(this);
     Helpers.Write("] = ");
     n.Source.Accept(this);
 }
コード例 #3
0
        public void Visit(ArrayAssignment n)
        {
            Globals.Builder.Start($"array assignment [{n.Location.StartLine}, {n.Location.StartColumn}]");

            n.Source.Accept(this);
            Globals.Builder.WriteUnaryOp("push", "rax");

            GetArrayAtIndex(n.Destination, n.Index);                   // Array is in rax, index is in r10.

            Globals.Builder.WriteBinaryOp("add", "r10", 1.ToString()); // shift index by 1 to account for length.
            Globals.Builder.WriteUnaryOp("pop", "r11");
            Globals.Builder.WriteBinaryOp("mov", "[rax + r10 * 8]", "r11");

            Globals.Builder.WriteBinaryOp("mov", "rax", "r11");

            Globals.Builder.End($"array assignment [{n.Location.EndLine}, {n.Location.EndColumn}]");
        }
コード例 #4
0
        public void Visit(ArrayAssignment n)
        {
            Helpers.WriteLine($"{_tab}{n.Text} [{n.Location.StartLine}, {n.Location.StartColumn}]");
            Tab();


            Helpers.WriteLine($"{_tab}Destination");
            Tab();
            n.Destination.Accept(this);
            Helpers.WriteLine($"{_tab}Index");
            Tab();
            n.Index.Accept(this);
            Untab();
            Helpers.WriteLine($"{_tab}Source");
            Tab();
            n.Source.Accept(this);
            Untab();

            Untab();
        }
コード例 #5
0
        public SemanticAtom Visit(ArrayAssignment n)
        {
            // TODO: Allow array of other types?

            var sourceType = n.Source.Accept(this);

            if (n.Destination.Accept(this) != Primitive.IntArray)
            {
                throw new Exception("This should never happen.");
            }

            if (sourceType != Primitive.Int)
            {
                Globals.Errors.Add($"[{n.Location.StartLine}, {n.Location.StartColumn}] Source expression ({sourceType.Name}) is not assignable to {Primitive.Int.Name}.");
            }

            if (n.Index.Accept(this) != Primitive.Int)
            {
                Globals.Errors.Add($"[{n.Index.Location.StartLine}, {n.Index.Location.StartColumn}] Index expression is not of type {Primitive.Int.Name}.");
            }

            return(Primitive.Int);
        }
コード例 #6
0
 public virtual TRes Visit(ArrayAssignment node, TArg arg) => Visit((Assignment)node, arg);
コード例 #7
0
        //Parses a variable or array assignment statement, else continues.
        private Node ParseAssignment()
        {
            //Statement can only be assignment if first token is a valid variable ID.
            if (CurrentToken.tokenType == TokenTypes.identity)
            {
                try
                {
                    //If next token is valid assignment operator or array element then assume assignment
                    if (Tokens[pos + 1].tokenType == TokenTypes.op && (Tokens[pos + 1].token.Equals("=") || Tokens[pos + 1].token.Equals(":=") || Tokens[pos + 1].token.Equals("<-") || Tokens[pos + 1].token.Equals("[")))
                    {
                        var id = ParseVarName();
                        if (CurrentToken.token.Equals("[")) //Array element assignment
                        {
                            var node = new ArrayAssignment(CurrentToken);
                            Consume(); //Consume [
                            Node value   = null;
                            Node element = null;
                            try
                            {
                                element = ParseExpression();
                            }
                            catch (Exception)
                            {
                                element = ParseVarOrFunction();
                            }

                            if (CurrentToken.token.Equals("]")) //Must match opening and closing []
                            {
                                Consume();
                            }
                            else
                            {
                                throw new ParserException("Expected ], found " + CurrentToken.token);
                            }

                            //Allow variety of assignment symbols
                            if (CurrentToken.token.Equals("=") || CurrentToken.token.Equals("<-") || CurrentToken.token.Equals(":="))
                            {
                                Consume();
                            }
                            else
                            {
                                throw new ParserException("Expected assignment operator, found " + CurrentToken.token);
                            }

                            //RHS of assignment statement, must be either expression of variable value
                            try
                            {
                                value = ParseExpression();
                            }
                            catch (Exception)
                            {
                                value = ParseVarOrFunction();
                            }

                            node.ID      = id;
                            node.Value   = value;
                            node.Element = element;
                            return(node);
                        }
                        else //Variable assignment
                        {
                            var node = new Assignment(CurrentToken);
                            //Allow variety of assignment symbols
                            if (CurrentToken.token.Equals("=") || CurrentToken.token.Equals("<-") || CurrentToken.token.Equals(":="))
                            {
                                Consume();
                            }
                            else
                            {
                                throw new ParserException("Expected assignment operator, found " + CurrentToken.token);
                            }
                            Node value = null;
                            try //RHS
                            {
                                value = ParseExpression();
                            }
                            catch (Exception)
                            {
                                value = ParseVarOrFunction();
                            }
                            node.ID    = id;
                            node.Value = value;
                            return(node);
                        }
                    }
                    else // Not assignment, assume expression or function
                    {
                        if (Tokens[pos + 1].token.Equals("(")) // Assume method
                        {
                            return(ParseVarOrFunction());
                        }
                        return(ParseExpression());
                    }
                }
                catch (Exception)
                {
                    if (Tokens[pos + 1].token.Equals("(")) // Assume method
                    {
                        return(ParseVarOrFunction());
                    }
                    return(ParseExpression());
                }
            }
            else //not assignment, try boolean
            {
                return(ParseEquality());
            }
        }