示例#1
0
        public static Assign CreateAssign(CodeElement elem, Scope scope)
        {
            // assign      = identifier '=' exp ';';
            var         cmd    = new Assign();
            CodeElement idElem = elem.Codes(WordIdentifier).First();

            cmd.VariableName = idElem.Value;
            cmd.VariableType = scope.ExistsVariable(cmd.VariableName, idElem);

            CodeElement expCode = elem.Codes().Last();

            cmd.Expression = ExpressionBuilder.CreateExpression(expCode, scope);

            if (cmd.VariableType == DefType.Bool && !ExpBase.IsBool(cmd.Expression))
            {
                throw new Exception(string.Format("The expression must be a bool'. {0}", expCode.GetLineAndColumn()));
            }
            if (cmd.VariableType == DefType.Int && !ExpBase.IsInt(cmd.Expression))
            {
                throw new Exception(string.Format("The expression must be an integer'. {0}", expCode.GetLineAndColumn()));
            }
            if (cmd.VariableType == DefType.Float && !ExpBase.IsNumber(cmd.Expression))
            {
                throw new Exception(string.Format("The expression must be a number'. {0}", expCode.GetLineAndColumn()));
            }

            return(cmd);
        }
示例#2
0
        public static Return CreateReturn(CodeElement elem, Scope scope, DefType resultType)
        {
            // return      = 'return' [exp] ';';

            var cmd = new Return();

            CodeElement expCode = elem.Codes().FirstOrDefault();

            if (expCode != null)
            {
                cmd.Expression = ExpressionBuilder.CreateExpression(expCode, scope);

                if (resultType == DefType.Bool && !ExpBase.IsBool(cmd.Expression))
                {
                    throw new Exception(string.Format("The expression must be a bool'. {0}", expCode.GetLineAndColumn()));
                }
                if (resultType == DefType.Int && !ExpBase.IsInt(cmd.Expression))
                {
                    throw new Exception(string.Format("The expression must be an integer'. {0}", expCode.GetLineAndColumn()));
                }
                if (resultType == DefType.Float && !ExpBase.IsNumber(cmd.Expression))
                {
                    throw new Exception(string.Format("The expression must be a number'. {0}", expCode.GetLineAndColumn()));
                }
            }
            else
            if (resultType != DefType.Void)
            {
                throw new Exception(string.Format("This function must return a value. {0}", elem.GetLineAndColumn()));
            }

            return(cmd);
        }
示例#3
0
        public static VariableDef CreateVariableDef(CodeElement elem, Scope scope)
        {
            // variableDef = typeAndId '=' exp ';';
            var def = CreateDeclare(elem.Codes(WordDeclare).First(), true);
            var cmd = new VariableDef()
            {
                Name = def.TheName, VariableType = def.TheType
            };

            if (elem.Codes().Count() == 2)
            {
                CodeElement expCode = elem.Codes().Last();
                cmd.Expression = ExpressionBuilder.CreateExpression(expCode, scope);

                if (cmd.VariableType == DefType.Bool && !ExpBase.IsBool(cmd.Expression))
                {
                    throw new Exception(string.Format("The expression must be a bool'. {0}", expCode.GetLineAndColumn()));
                }
                if (cmd.VariableType == DefType.Int && !ExpBase.IsInt(cmd.Expression))
                {
                    throw new Exception(string.Format("The expression must be an integer'. {0}", expCode.GetLineAndColumn()));
                }
                if (cmd.VariableType == DefType.Float && !ExpBase.IsNumber(cmd.Expression))
                {
                    throw new Exception(string.Format("The expression must be a number'. {0}", expCode.GetLineAndColumn()));
                }
            }
            if (scope.ExistsFunction(cmd.Name))
            {
                throw new Exception(string.Format("A Function called '{0}', {1}, is declared", cmd.Name, elem.GetLineAndColumn()));
            }

            scope.Vars.BuildVariable(cmd.VariableType, cmd.Name, elem);
            return(cmd);
        }