Exemple #1
0
        private void AnalysePrimaryExpression(PrimaryExpression expression, string type)
        {
            if (expression is IdentifierPE)
            {
                IdentifierPE identifierPe = (IdentifierPE)expression;
                variableExists(identifierPe);
                CheckType(type, identifierPe);
                return;
            }

            if (expression is BracketsPE)
            {
                BracketsPE bracketsPe = (BracketsPE)expression;
                AnalysePrimaryExpression(bracketsPe.GetExpression.getP1, type);
                AnalysePrimaryExpression(bracketsPe.GetExpression.getP2, type);
                AnalyzeOperator(bracketsPe.GetExpression.GetOperator(), type);
                return;
            }

            if (expression is ConditionPE)
            {
                ConditionPE conditionPe = (ConditionPE)expression;
                variableExists(conditionPe);
                if (type != "boolean")
                {
                    logCError(expression, "Boolean illegal in: " + type + "");
                }
            }
        }
Exemple #2
0
        private void AnalyzeCondition(ConditionExpression conditionExpression)
        {
            AST       ast1 = conditionExpression.AST1;
            AST       ast2 = conditionExpression.AST2;
            Condition c    = conditionExpression.C1;

            IdentifierPE identifierPe = (IdentifierPE)getFirstPrimaryExpression(ast1);

            if (!variableExists(identifierPe))
            {
                return;
            }
            string type = table[identifierPe.getName()];

            if (type == "boolean")
            {
                switch (c.Name)
                {
                case "<":
                case ">":
                case ">=":
                case "<=":
                    logCError(conditionExpression, "Incorrect condition statement: \"" + c.Name + "\" for: \"" + type + "\"");
                    return;
                }
            }
            AnalyzeIfAST(ast1, type);
            AnalyzeIfAST(ast2, type);
        }
Exemple #3
0
        private bool CheckType(string type, PrimaryExpression primaryExpression)
        {
            if (primaryExpression is IdentifierPE)
            {
                IdentifierPE identifierPe = (IdentifierPE)primaryExpression;
                if (!table.ContainsKey(identifierPe.getName()))
                {
                    return(false);
                }
                if (table[identifierPe.getName()] == type)
                {
                    return(true);
                }

                string[] msg = { "expected: " + type, "given: " + table[identifierPe.getName()] };
                logCError(primaryExpression, msg);
                return(false);
            }

            if (primaryExpression is ConditionPE)
            {
                if (_compilerUtils.Types[type] == _boolean)
                {
                    return(true);
                }
                string[] msg = { "expected: " + type, "given: " + _compilerUtils.Syntax[_boolean] };
                logCError(primaryExpression, msg);
                return(false);
            }

            if (primaryExpression is LiteralString)
            {
                if (_compilerUtils.Types[type] == _string)
                {
                    return(true);
                }
                string[] msg = { "expected: " + type, "given: " + _compilerUtils.Syntax[_string] };
                logCError(primaryExpression, msg);
                return(false);
            }

            if (primaryExpression is LiteralInt)
            {
                if (_compilerUtils.Types[type] == _integer)
                {
                    return(true);
                }
                string[] msg = { "expected: " + type, "given: " + _compilerUtils.Syntax[_integer] };
                logCError(primaryExpression, msg);
                return(false);
            }
            logCError(primaryExpression, "Unknown error in CheckType");
            return(false);
        }
Exemple #4
0
        private PrimaryExpression parsePrimary()
        {
            PrimaryExpression PE;

            if (CurrentToken == null)
            {
                return(null);
            }
            switch (CurrentToken.getType())
            {
//                case _condition:
            case _identifier:
                var Id = parseIdentifier();
                PE = new IdentifierPE(prevLine, Id);
                break;

            case _lPar:
                nextToken();
                PE = new BracketsPE(prevLine, parseExpression());
                accept(_rPar);
                break;

            case _condition:
                var C = parseCondition();
                PE = new ConditionPE(prevLine, C);
                break;

            case _literalString:
                var S = parseLiteral();
                PE = new LiteralString(prevLine, S);
                break;

            case _literalInt:
                var I = parseLiteral();
                PE = new LiteralString(prevLine, I);
                break;

            default:
                logError(CurrentToken, "Syntax Error in Primary");
                PE = null;
                break;
            }
            return(PE);
        }