Пример #1
0
        private CFlatType TypeCheckIncrementDecrement(ASTExpression expr, string operatorName, LexLocation loc)
        {
            //totally cheating here. the grammar should not even allow this to happen.
            if (expr is ASTIdentifier)
            {
                ASTIdentifier identifier = (ASTIdentifier)expr;
                //need to set a flag so that the code generator can store back the result
                identifier.IsLeftHandSide = true;

                CFlatType t = CheckSubTree(identifier);
                if (t.IsNumeric)
                {
                    return(t);
                }
                else
                {
                    ReportError(loc, "The {0} operator requires an instance of a numeric datatype", operatorName);
                }
            }
            else
            {
                ReportError(loc, "The {0} operator requires an instance of a numeric datatype", operatorName);
            }

            /* note: the ReportError method will always throw, so this part of the code should not be reached,
             * unless of course we change the ReportError method to not throw or try to implement some error recovery
             * strategy...
             * */
            throw new InternalCompilerException("This part of the code should not be reachable.");
        }
Пример #2
0
        public override void VisitIdentifier(ASTIdentifier n)
        {
            _lastWalkedIdentifier = n.ID;
            //yeah this gets a bit messy, but I kinda do need to check all 3 possibilities where an identifier could
            //be declared (we're not doing globals in the language).
            if (n.IsLeftHandSide)
            {
                if (IsIdentifierLocal(n.ID))
                {
                    LocalBuilderInfo localInfo = _currentMethodBuilder.Locals[n.ID];
                    _assignmentCallback = gen => gen.Emit(OpCodes.Stloc, localInfo.Index);
                }
                else if (IsIdentifierField(n.ID))
                {
                    FieldBuilder field = _currentTypeBuilder.FieldMap[n.ID];
                    //push the "this" argument
                    _gen.Emit(OpCodes.Ldarg_0);
                    _assignmentCallback = gen => gen.Emit(OpCodes.Stfld, field);
                }
                else if (IsIdentifierArgument(n.ID))
                {
                    _assignmentCallback = gen => gen.Emit(OpCodes.Starg, _currentMethodBuilder.Arguments[n.ID].Index);
                }
                else
                {
                    throw new Exception(String.Format("Identifier '{0}' is not a local, argument, or member variable of the current class.", n.ID));
                }
            }
            else
            {
                if (IsIdentifierLocal(n.ID))
                {
                    var info = _currentMethodBuilder.Locals[n.ID];
                    _gen.Emit(OpCodes.Ldloc, info.Index);

                    _lastWalkedType = info.Builder.LocalType;
                }
                else if (IsIdentifierField(n.ID))
                {
                    FieldBuilder field = _currentTypeBuilder.FieldMap[n.ID];
                    _gen.Emit(OpCodes.Ldarg_0);
                    _gen.Emit(OpCodes.Ldfld, field);

                    _lastWalkedType = field.FieldType;
                }
                else if (IsIdentifierArgument(n.ID))
                {
                    ArgumentInfo info  = _currentMethodBuilder.Arguments[n.ID];
                    int          index = info.Index;
                    if (!_currentMethodBuilder.Method.IsStatic)
                    {
                        index++;
                    }
                    _gen.Emit(OpCodes.Ldarg, index);

                    _lastWalkedType = info.CilType;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Makes sure the identifier has been declared and that is is actually a type of some kind (i.e. local, member, not a method).
        /// Sets the AST node's type and descriptor if it is declared.
        /// </summary>
        /// <param name="n"></param>
        public override void VisitIdentifier(ASTIdentifier n)
        {
            if (_scopeMgr.HasSymbol(n.ID))
            {
                Descriptor d = _scopeMgr.GetType(n.ID);
                if (d != null)
                {
                    n.Descriptor = d;
                    n.CFlatType  = d.Type;

                    _lastSeenType = d.Type;
                }
                else
                {
                    ReportError(n.Location, "Identifier '{0}' is not a type (it's probably a method or something).", n.ID);
                }
            }
            else
            {
                ReportError(n.Location, "Identifier '{0}' has not been declared.", n.ID);
            }
        }
Пример #4
0
		/// <summary> This method corresponds to variable
		/// references in Velocity templates.
		/// The following are examples of variable
		/// references that may be found in a
		/// template:
		/// *
		/// $foo
		/// $bar
		/// *
		/// </summary>
		public void Identifier()
		{
			/*@bgen(jjtree) Identifier */
			ASTIdentifier jjtn000 = new ASTIdentifier(this, ParserTreeConstants.IDENTIFIER);
			nodeTree.OpenNodeScope(jjtn000);
			try
			{
				ConsumeToken(ParserConstants.IDENTIFIER);
			}
			finally
			{
				nodeTree.CloseNodeScope(jjtn000, true);
			}
		}
Пример #5
0
 public virtual Object Visit(ASTIdentifier node, Object data)
 {
     data = node.ChildrenAccept(this, data);
     return(data);
 }
Пример #6
0
 /// <summary>Display an ASTIdentifier node
 /// </summary>
 public override Object Visit(ASTIdentifier node, Object data)
 {
     return(ShowNode(node, data));
 }
Пример #7
0
        private static ASTNode ParseSingleStatement()
        {
            ASTNode ret = null;

            if (currentTokens[currentToken] is StringToken)
            {
                ret = new ASTString { String = currentTokens[currentToken].Token };
                currentToken++;
            }
            else if (currentTokens[currentToken] is NumberToken)
            {
                double dResult;
                if (double.TryParse(currentTokens[currentToken].Token, NumberStyles.Any, CultureInfo.GetCultureInfo("en"), out dResult))
                {
                    ret = new ASTNumber { Number = dResult };
                }
                else
                {
                    throw new ParserException(string.Format("Number {0} is not valid.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
                }
                currentToken++;
            }
            else if (currentTokens[currentToken] is ReservedWordToken)
            {
                if (currentTokens[currentToken].Token == "this" || currentTokens[currentToken].Token == "VM")
                {
                    string scope = currentTokens[currentToken].Token;
                    currentToken++;
                    if (currentTokens[currentToken] is SpecialToken && currentTokens[currentToken].Token == ".")
                    {
                        currentToken++;
                        ret = ParseFunctionCallOrParameterAccess(scope);
                    }
                    else
                    {
                        throw new ParserException(string.Format("Expected '.' instead of '{0}'.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
                    }
                }
                else if (currentTokens[currentToken].Token == "object" ||
                    currentTokens[currentToken].Token == "string" ||
                    currentTokens[currentToken].Token == "number")
                {
                    VMType type = currentTokens[currentToken].Token == "number" ? VMType.Number : (currentTokens[currentToken].Token == "string" ? VMType.String : VMType.Object);
                    currentToken++;
                    if (currentTokens[currentToken] is IdentifierToken)
                    {
                        ret = new ASTLocalVariable()
                        {
                            Type = type,
                            Name = currentTokens[currentToken].Token
                        };
                    }
                    else
                    {
                        throw new ParserException("Expected Identifier.", currentTokens[currentToken].LineNumber);
                    }
                    currentToken++;
                }
                else if (currentTokens[currentToken].Token == "new")
                {
                    currentToken++;
                    ASTNode createClass = ParseFunctionCallOrParameterAccess("constructor");
                    if (createClass is ASTFunctionCall)
                    {
                        ASTFunctionCall fc = (ASTFunctionCall) createClass;
                        ret = new ASTCreateClass()
                        {
                            Scope = fc.FunctionName,
                            FunctionName = fc.FunctionName,
                            Parameter = fc.Parameter
                        };
                    }
                    else
                    {
                        throw new ParserException("Error with new.", currentTokens[currentToken].LineNumber);
                    }
                }
                else if (currentTokens[currentToken].Token == "return")
                {
                    currentToken++;
                    ret = new ASTReturn()
                    {
                        Return = ParseStatement()
                    };

                    // Point to ';' again!!!
                    --currentToken;
                }
                else
                {
                    throw new ParserException(string.Format("Reserved Word {0} is not legal in Statement.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
                }
            }
            else if (currentTokens[currentToken] is IdentifierToken)
            {
                string identifierOrScope = currentTokens[currentToken].Token;
                currentToken++;
                if (currentTokens[currentToken] is SpecialToken)
                {
                    if (currentTokens[currentToken].Token == ".")
                    {
                        currentToken++;
                        TokenBase isFunction = currentTokens[currentToken + 1];
                        ret = ParseFunctionCallOrParameterAccess(identifierOrScope);
                    }
                    else if (currentTokens[currentToken].Token == "(")
                    {
                        // Set focus again to the function name
                        --currentToken;
                        ret = ParseFunctionCallOrParameterAccess("this");
                    }
                    else if (currentTokens[currentToken].Token == "[")
                    {
                        currentToken++;
                        ASTStatement statement = ParseStatement();
                        if (statement == null)
                            throw new ParserException("Expected Statement for Parameter Access.", currentTokens[currentToken].LineNumber);
                        ret = new ASTClassParameter()
                        {
                            Scope = identifierOrScope,
                            Parameter = statement
                        };
                    }
                    else
                    {
                        ret = new ASTIdentifier() { Identifier = identifierOrScope };
                    }
                }
                else
                {
                    ret = new ASTIdentifier() { Identifier = identifierOrScope };
                }
            }

            if (ret == null)
                throw new ParserException(String.Format("Problems parsing the Identifier/Function Call/String/Number {0}.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
            return ret;
        }
Пример #8
0
 public ASTVariableDeclarator(ASTIdentifier declarator) : base("VariableDeclarator")
 {
     Identifier = declarator;
 }
Пример #9
0
 /// <summary>Display an ASTIdentifier node
 /// </summary>
 public override System.Object visit(ASTIdentifier node, System.Object data)
 {
     return(showNode(node, data));
 }
Пример #10
0
 public virtual System.Object visit(ASTIdentifier node, System.Object data)
 {
     data = node.childrenAccept(this, data);
     return(data);
 }
Пример #11
0
 public override object Visit(ASTIdentifier node, object data)
 {
     return(this.ShowNode(node, data));
 }
Пример #12
0
        private static ASTNode ParseSingleStatement()
        {
            ASTNode ret = null;

            if (currentTokens[currentToken] is StringToken)
            {
                ret = new ASTString {
                    String = currentTokens[currentToken].Token
                };
                currentToken++;
            }
            else if (currentTokens[currentToken] is NumberToken)
            {
                double dResult;
                if (double.TryParse(currentTokens[currentToken].Token, NumberStyles.Any, CultureInfo.GetCultureInfo("en"), out dResult))
                {
                    ret = new ASTNumber {
                        Number = dResult
                    };
                }
                else
                {
                    throw new ParserException(string.Format("Number {0} is not valid.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
                }
                currentToken++;
            }
            else if (currentTokens[currentToken] is ReservedWordToken)
            {
                if (currentTokens[currentToken].Token == "this" || currentTokens[currentToken].Token == "VM")
                {
                    string scope = currentTokens[currentToken].Token;
                    currentToken++;
                    if (currentTokens[currentToken] is SpecialToken && currentTokens[currentToken].Token == ".")
                    {
                        currentToken++;
                        ret = ParseFunctionCallOrParameterAccess(scope);
                    }
                    else
                    {
                        throw new ParserException(string.Format("Expected '.' instead of '{0}'.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
                    }
                }
                else if (currentTokens[currentToken].Token == "object" ||
                         currentTokens[currentToken].Token == "string" ||
                         currentTokens[currentToken].Token == "number")
                {
                    VMType type = currentTokens[currentToken].Token == "number" ? VMType.Number : (currentTokens[currentToken].Token == "string" ? VMType.String : VMType.Object);
                    currentToken++;
                    if (currentTokens[currentToken] is IdentifierToken)
                    {
                        ret = new ASTLocalVariable()
                        {
                            Type = type,
                            Name = currentTokens[currentToken].Token
                        };
                    }
                    else
                    {
                        throw new ParserException("Expected Identifier.", currentTokens[currentToken].LineNumber);
                    }
                    currentToken++;
                }
                else if (currentTokens[currentToken].Token == "new")
                {
                    currentToken++;
                    ASTNode createClass = ParseFunctionCallOrParameterAccess("constructor");
                    if (createClass is ASTFunctionCall)
                    {
                        ASTFunctionCall fc = (ASTFunctionCall)createClass;
                        ret = new ASTCreateClass()
                        {
                            Scope        = fc.FunctionName,
                            FunctionName = fc.FunctionName,
                            Parameter    = fc.Parameter
                        };
                    }
                    else
                    {
                        throw new ParserException("Error with new.", currentTokens[currentToken].LineNumber);
                    }
                }
                else if (currentTokens[currentToken].Token == "return")
                {
                    currentToken++;
                    ret = new ASTReturn()
                    {
                        Return = ParseStatement()
                    };

                    // Point to ';' again!!!
                    --currentToken;
                }
                else
                {
                    throw new ParserException(string.Format("Reserved Word {0} is not legal in Statement.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
                }
            }
            else if (currentTokens[currentToken] is IdentifierToken)
            {
                string identifierOrScope = currentTokens[currentToken].Token;
                currentToken++;
                if (currentTokens[currentToken] is SpecialToken)
                {
                    if (currentTokens[currentToken].Token == ".")
                    {
                        currentToken++;
                        TokenBase isFunction = currentTokens[currentToken + 1];
                        ret = ParseFunctionCallOrParameterAccess(identifierOrScope);
                    }
                    else if (currentTokens[currentToken].Token == "(")
                    {
                        // Set focus again to the function name
                        --currentToken;
                        ret = ParseFunctionCallOrParameterAccess("this");
                    }
                    else if (currentTokens[currentToken].Token == "[")
                    {
                        currentToken++;
                        ASTStatement statement = ParseStatement();
                        if (statement == null)
                        {
                            throw new ParserException("Expected Statement for Parameter Access.", currentTokens[currentToken].LineNumber);
                        }
                        ret = new ASTClassParameter()
                        {
                            Scope     = identifierOrScope,
                            Parameter = statement
                        };
                    }
                    else
                    {
                        ret = new ASTIdentifier()
                        {
                            Identifier = identifierOrScope
                        };
                    }
                }
                else
                {
                    ret = new ASTIdentifier()
                    {
                        Identifier = identifierOrScope
                    };
                }
            }

            if (ret == null)
            {
                throw new ParserException(String.Format("Problems parsing the Identifier/Function Call/String/Number {0}.", currentTokens[currentToken].Token), currentTokens[currentToken].LineNumber);
            }
            return(ret);
        }