Example #1
0
        private int Variable(ref VarType type, ref VarType subType)
        {
            if (Tokens[currentToken].Type != TokenType.Identifier && Tokens[currentToken].Type != TokenType.DoubleQuoteString && Tokens[currentToken].Type != TokenType.Number)
            {
                WriteError("Expected Identifier", Tokens[currentToken]);
                AdvanceToken();
                return(0);
            }
            int address = 0;

            if (symbolTable.Lookup(Tokens[currentToken].Value) == -1)
            {
                if (Tokens[currentToken].Type == TokenType.DoubleQuoteString)
                {
                    address = assembler.AllocateStaticMemory(Tokens[currentToken].Value);
                    type    = VarType.String;
                    AdvanceToken();
                    return(address);
                }
                if (Tokens[currentToken].Type == TokenType.Number)
                {
                    address = int.Parse(Tokens[currentToken].Value);
                    type    = VarType.Number;
                    AdvanceToken();
                    return(address);
                }
                //variable not found
                WriteErrorLine("Undeclared variable '" + Tokens[currentToken].Value + "'", Tokens[currentToken]);
                type = VarType.Invalid;
            }
            else
            {
                address = symbolTable.Lookup(Tokens[currentToken].Value);
                type    = symbolTable.GetType(Tokens[currentToken].Value);
            }

            AdvanceToken();

            //loading an array
            if (Tokens[currentToken].Type == TokenType.LeftBracket)
            {
                AdvanceToken();
                if (type != VarType.Invalid)
                {
                    if (type != VarType.Array)
                    {
                        WriteErrorLine("Variable treated like array but not declared as such", Tokens[currentToken]);
                    }
                    else
                    {
                        subType = symbolTable.GetArrayType(Tokens[currentToken - 2].Value);
                    }
                }
                //else just eat the brackets, no need to error check, already threw an error
                VarType ttype = VarType.Invalid;
                Expr(0, ref ttype);
                if (Tokens[currentToken].Type != TokenType.RightBracket)
                {
                    WriteError("Expected end of array", Tokens[currentToken]);
                }
                AdvanceToken();
                if (ttype != VarType.Int)
                {
                    WriteErrorLine("Expected int for array index", Tokens[currentToken]);
                }
                //emit some stuff idk
            }

            return(address);
        }