Beispiel #1
0
 /// <summary>
 /// Determines if the specified variables ID refers to a read-only variable.
 /// </summary>
 /// <param name="varId">ID of the variable to check.</param>
 /// <returns>If true, the variable is read-only.</returns>
 internal bool IsReadOnly(int varId)
 {
     if (ByteCodes.IsGlobalVariable(varId))
     {
         return(Variables[ByteCodes.GetVariableIndex(varId)].CompilerFlags.HasFlag(CompilerFlag.ReadOnly));
     }
     return(false);
 }
Beispiel #2
0
        private void ParseVar(Token token)
        {
            // Get variable name
            token = Lexer.GetNext();
            if (token.Type != TokenType.Symbol)
            {
                Error(ErrorCode.ExpectedSymbol, token);
                return;
            }

            // Check if variable already exists
            int varId = GetVariableId(token.Value, false);

            if (varId != -1)
            {
                Error(ErrorCode.VariableAlreadyDefined, token);
                NextLine();
                return;
            }

            if (InHeader)
            {
                // Create global variable
                varId = GetVariableId(token.Value);
                Debug.Assert(((ByteCodeVariableFlag)varId).HasFlag(ByteCodeVariableFlag.Global));
                // Process any assignment
                if (Lexer.PeekNext().Type == TokenType.Equal)
                {
                    // Consume equal sign
                    Lexer.GetNext();
                    // Parse value (only literal value allowed here)
                    if (!ParseLiteral(out Variable variable))
                    {
                        return;
                    }
                    // Assign value to variable
                    Variables[ByteCodes.GetVariableIndex(varId)] = variable;
                }
            }
            else
            {
                // Create local variable
                varId = GetVariableId(token.Value);
                // Process any assignment
                if (Lexer.PeekNext().Type == TokenType.Equal)
                {
                    // Consume equal sign
                    Lexer.GetNext();
                    Writer.Write(ByteCode.Assign, varId);
                    if (!ParseExpression())
                    {
                        return;
                    }
                }
            }
        }
Beispiel #3
0
        private Variable GetVariable(int varId)
        {
            if (ByteCodes.IsGlobalVariable(varId))
            {
                return(Variables[ByteCodes.GetVariableIndex(varId)]);
            }
            var context = GetFunctionContext();

            if (ByteCodes.IsLocalVariable(varId))
            {
                return(context.Variables[ByteCodes.GetVariableIndex(varId)]);
            }
            // Function parameter
            return(context.Parameters[ByteCodes.GetVariableIndex(varId)]);
        }