public override object VisitFunctionVariableDeclarationStatement([NotNull] ClepsParser.FunctionVariableDeclarationStatementContext context)
        {
            ClepsVariable variable = Visit(context.variableDeclaration()) as ClepsVariable;

            VariableManager variableManager = VariableManagers.Last();

            if (!variableManager.IsVariableNameAvailable(variable.VariableName))
            {
                string errorMessage = String.Format("Variable {0} is already defined", variable.VariableName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Use a different variable name to avoid stopping the compilation
                string newVariableName = variableManager.GetAvailableVariableName(variable.VariableName);
                variable = new ClepsVariable(newVariableName, variable.VariableType, variable.IsConstant);
            }

            IValue value = null;

            if (context.rightHandExpression() != null)
            {
                value = Visit(context.rightHandExpression()) as IValue;
                if (variable.VariableType != value.ExpressionType)
                {
                    throw new NotImplementedException("Assignment for non identical types not supported yet");
                }
            }

            IValueRegister variableRegister = CurrMethodGenerator.CreateNewVariable(variable, value);

            variableManager.AddLocalVariable(variable, variableRegister);

            return(variable);
        }
        public override IMethodValue VisitFunctionAssignment_Ex([NotNull] ClepsParser.FunctionAssignmentContext context)
        {
            var             oldCurrMethodRegister = CurrMethodGenerator;
            VariableManager variableManager       = new VariableManager();

            VariableManagers.Add(variableManager);

            ClepsType returnType = VoidClepsType.GetVoidType();

            if (context.FunctionReturnType != null)
            {
                returnType = Visit(context.FunctionReturnType) as ClepsType;
            }

            List <ClepsVariable> functionParameters = context._FunctionParameters.Select(p => Visit(p) as ClepsVariable).ToList();

            FunctionClepsType functionType = new FunctionClepsType(functionParameters.Select(p => p.VariableType).ToList(), returnType);

            var newMethod = CodeGenerator.CreateNewMethod(functionType);

            CurrMethodGenerator = newMethod;

            CurrMethodGenerator.SetFormalParameterNames(functionParameters.Select(p => p.VariableName).ToList());

            functionParameters.ForEach(variable => {
                variableManager.AddLocalVariable(variable, CurrMethodGenerator.GetFormalParameterRegister(variable.VariableName));
            });

            Visit(context.statementBlock());

            VariableManagers.RemoveAt(VariableManagers.Count - 1);
            CurrMethodGenerator = oldCurrMethodRegister;
            return(newMethod);
        }
Exemple #3
0
        public override object VisitNativeFunctionStatement([NotNull] ClepsParser.NativeFunctionStatementContext context)
        {
            var code = Visit(context.nativeStatement()) as string;

            if (code != null)
            {
                CurrMethodGenerator.AddNativeCode(code);
            }

            return(true);
        }
Exemple #4
0
        public override object VisitDoWhileStatement([NotNull] ClepsParser.DoWhileStatementContext context)
        {
            IValue whileConditionValue = Visit(context.TerminalCondition) as IValue;

            CurrMethodGenerator.CreateLoop(null, whileConditionValue);

            Visit(context.statementBlock());

            CurrMethodGenerator.CloseBlock();

            return(whileConditionValue);
        }
Exemple #5
0
        public override object VisitIfStatement([NotNull] ClepsParser.IfStatementContext context)
        {
            IValue conditionValue = Visit(context.rightHandExpression()) as IValue;

            CurrMethodGenerator.CreateIfStatementBlock(conditionValue);

            Visit(context.statementBlock());

            CurrMethodGenerator.CloseBlock();

            return(conditionValue);
        }
        private void CreateAssignment(IValueRegister register, ClepsParser.RightHandExpressionContext rightHandExpression)
        {
            IValue value = Visit(rightHandExpression) as IValue;

            if (register.ExpressionType == value.ExpressionType && CompilerConstants.SystemSupportedTypes.Contains(register.ExpressionType))
            {
                CurrMethodGenerator.CreateAssignment(register, value);
            }
            else
            {
                throw new NotImplementedException("assignment for non byte types not supported yet");
            }
        }
        public override object VisitFunctionCallStatement([NotNull] ClepsParser.FunctionCallStatementContext context)
        {
            IValue target;

            if (context.rightHandExpression() == null)
            {
                //if no target is specified, pretend this is called on 'this'
                target = CodeGenerator.GetThisInstanceValue(new BasicClepsType(FullyQualifiedClassName));
            }
            else
            {
                target = Visit(context.rightHandExpression()) as IValue;
            }

            IValue functionCall = doFunctionCall(context.functionCall(), target, target.ExpressionType, true /* allowVoidReturn */);

            CurrMethodGenerator.CreateFunctionCallStatement(functionCall);

            return(true);
        }
Exemple #8
0
        public override bool VisitFunctionReturnStatement_Ex([NotNull] ClepsParser.FunctionReturnStatementContext context)
        {
            IValue returnValue = null;

            if (context.rightHandExpression() != null)
            {
                returnValue = Visit(context.rightHandExpression()) as IValue;
            }

            var currFunctionType = CurrMethodGenerator.ExpressionType as FunctionClepsType;

            if (currFunctionType.ReturnType != returnValue.ExpressionType)
            {
                string errorMessage = String.Format("Expected return of {0}. Returning type {1} instead.", currFunctionType.ReturnType.GetClepsTypeString(), returnValue.ExpressionType.GetClepsTypeString());
                Status.AddError(new CompilerError(FileName, context.rightHandExpression().Start.Line, context.rightHandExpression().Start.Column, errorMessage));
            }

            CurrMethodGenerator.CreateReturnStatement(returnValue);

            return(true);
        }