Exemplo n.º 1
0
        /// <summary>
        /// Compile function declaration argument
        /// </summary>
        protected virtual void CompileFunctionArgumentDeclaration()
        {
            if (MatchToken(Token.Type.TypeName))
            {
                string  typeName  = previous.text;
                TypeDef typedef   = _assembly.GetTypeDef(typeName);
                bool    isArray   = false;
                int     arraySize = 0;

                if (MatchToken(Token.Type.ArrayIndexBegin))
                {
                    isArray = true;
                    if (MatchToken(Token.Type.Numeric))
                    {
                        arraySize = Int32.Parse(previous.text);
                    }
                    Require(Token.Type.ClosingArrayIndex, "] Expected");
                }

                Token name = Require(Token.Type.Identifier, "identifier expected");

                int refIndex = -1;
                if (isArray)
                {
                    object arr = Array.CreateInstance(typedef.type, arraySize);
                    refIndex = _cdata.AddMemoryObject(arr);
                }
                else
                {
                    refIndex = _cdata.AddMemoryObject(typedef.DefaultConstructor()());
                }

                _cdata._scopeResolver.Add(name.text, refIndex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Compile a variable declaration
        /// </summary>
        /// <param name="allowFunction"></param>
        /// <param name="requireEOS"></param>
        /// <returns></returns>
        protected virtual object CompileDeclaration(bool allowFunction = true, bool requireEOS = true)
        {
            if (MatchToken(Token.Type.EOS))
            {
                return(null);
            }

            if (MatchToken(Token.Type.TypeName))
            {
                string  typeName  = previous.text;
                TypeDef typedef   = _assembly.GetTypeDef(typeName);
                bool    isArray   = false;
                int     arraySize = 0;

                if (MatchToken(Token.Type.ArrayIndexBegin))
                {
                    isArray = true;
                    if (MatchToken(Token.Type.Numeric))
                    {
                        arraySize = Int32.Parse(previous.text);
                    }
                    Require(Token.Type.ClosingArrayIndex, "] Expected");
                }

                if (Peek().type == Token.Type.Dot)
                {
                    BackStep();
                    return(CompileFunctionCall());
                }

                if (typedef.staticClass)
                {
                    throw new Exception("Cannot create an instance variable of a static class/struct");
                }

                Token name = Require(Token.Type.Identifier, "identifier expected");

                if (allowFunction && Peek().type == Token.Type.OpenParenthesis)
                {
                    return(CompileFunctionDeclaration(typedef, name.text, isArray));
                }

                int refIndex = -1;
                if (isArray)
                {
                    object arr = Array.CreateInstance(typedef.type, arraySize);
                    refIndex = _cdata.AddMemoryObject(arr);
                }
                else
                {
                    refIndex = _cdata.AddMemoryObject(typedef.DefaultConstructor()());
                }

                _cdata._scopeResolver.Add(name.text, refIndex);

                if (Peek().type == Token.Type.Assign)
                {
                    BackStep();
                    object result = CompileAssignment();
                    Require(Token.Type.EOS, "; Expected");
                    return(result);
                }
                Require(Token.Type.EOS, "; Expected");
                return(null);
            }
            return(CompileControlInstructionStatement());
        }