public override object VisitNumericAssignments([NotNull] ClepsParser.NumericAssignmentsContext context)
        {
            IValue val;
            string numericValue = context.numeric().NumericValue.Text;
            string numericType  = context.numeric().NumericType?.Text;

            if (numericType == "b")
            {
                byte byteVal;
                if (!byte.TryParse(numericValue, out byteVal))
                {
                    string errorMessage = String.Format("Value {0} is outside the range of values allowed for a byte. Allowed range is 0 to 255", numericValue);
                    Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                    //Use a different value to avoid stopping the compilation
                    byteVal = 0;
                }

                val = CodeGenerator.CreateByte(byteVal);
            }
            else
            {
                throw new NotImplementedException("Other numeric types not supported yet");
            }

            return(val);
        }
Example #2
0
        public override LLVMRegister VisitNumericAssignments([NotNull] ClepsParser.NumericAssignmentsContext context)
        {
            string valueString = context.numeric().NumericValue.Text;
            string numericType = context.numeric().NumericType != null?context.numeric().NumericType.Text : "";

            ulong value;

            if (!ulong.TryParse(valueString, out value))
            {
                throw new Exception(String.Format("Numeric value {0} not a valid int", valueString));
            }


            LLVMTypeRef llvmType = LLVM.Int32TypeInContext(Context);

            //hardcoded identifiers for llvm i.e. native types
            if (numericType == "ni")
            {
                LLVMValueRef valueToRet = LLVM.ConstInt(llvmType, value, false);
                LLVMValueRef valuePtr   = LLVM.BuildAlloca(Builder, LLVM.TypeOf(valueToRet), "nativeIntValuePtr");
                LLVM.BuildStore(Builder, valueToRet, valuePtr);
                LLVMRegister nativeRet = new LLVMRegister(ClepsLLVMTypeConvertorInst.GetClepsNativeLLVMType(llvmType), valuePtr);
                return(nativeRet);
            }

            if (!String.IsNullOrEmpty(numericType))
            {
                //for now other numeric types are not supported
                string errorMessage = String.Format("Numerics of type {0} was not found. Make sure the class that defined {0} was imported.", numericType);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //just assume this is numericType is "" to avoid stalling the compilation
            }

            LLVMRegister ret = GetConstantIntRegisterOfClepsType(context, llvmType, value, "int32" /* friendly type name */);

            return(ret);
        }