public IMethodRegister CreateMethod(string className, bool isStatic, ClepsType functionType, string functionName)
        {
            if (!classesLoaded.ContainsKey(className))
            {
                throw new ArgumentException(String.Format("Class {0} not loaded", className));
            }

            if (!functionType.IsFunctionType)
            {
                throw new ArgumentException("Expected function type. Got " + functionType.GetClepsTypeString());
            }

            var classNameToUse = isStatic ? className + ".static" : className;
            var functionNameAndType = new FunctionNameAndType(classNameToUse, functionName, functionType);
            var methodList = isStatic ? staticMethods[classNameToUse] : memberMethods[classNameToUse];

            if (methodList.Contains(functionNameAndType))
            {
                throw new ArgumentException(String.Format("Function {0} {1} {2} for class {3} already exists", isStatic ? "static" : "", functionType, functionName, className));
            }

            methodList.Add(functionNameAndType);
            var methodRegister = new JavaScriptMethod(functionType as FunctionClepsType);
            methodBodies.Add(functionNameAndType, methodRegister);

            return methodRegister;
        }
        public IMethodRegister CreateMethod(string className, bool isStatic, ClepsType functionType, string functionName)
        {
            if (!classesLoaded.ContainsKey(className))
            {
                throw new ArgumentException(String.Format("Class {0} not loaded", className));
            }

            if (!functionType.IsFunctionType)
            {
                throw new ArgumentException("Expected function type. Got " + functionType.GetClepsTypeString());
            }

            var classNameToUse      = isStatic ? className + ".static" : className;
            var functionNameAndType = new FunctionNameAndType(classNameToUse, functionName, functionType);
            var methodList          = isStatic ? staticMethods[classNameToUse] : memberMethods[classNameToUse];

            if (methodList.Contains(functionNameAndType))
            {
                throw new ArgumentException(String.Format("Function {0} {1} {2} for class {3} already exists", isStatic ? "static" : "", functionType, functionName, className));
            }

            methodList.Add(functionNameAndType);
            var methodRegister = new JavaScriptMethod(functionType as FunctionClepsType);

            methodBodies.Add(functionNameAndType, methodRegister);

            return(methodRegister);
        }
        private IValue doFunctionCall(ParserRuleContext context, string targetFunctionName, List<IValue> parameters, IValue target, ClepsType targetType, bool allowVoidReturn)
        {
            IValue dereferencedTarget = target == null? null : GetDereferencedRegisterOrNull(target);
            BasicClepsType dereferencedType = target == null? targetType as BasicClepsType : dereferencedTarget.ExpressionType as BasicClepsType;

            if (dereferencedType == null)
            {
                string errorMessage = String.Format("Could not dereference expression on type {0}", targetType.GetClepsTypeString());
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //just return something to avoid stalling
                return CodeGenerator.CreateByte(0);
            }

            ClepsClass targetClepsClass = ClassManager.GetClass(dereferencedType.GetClepsTypeString());


            List<ClepsVariable> functionOverloads;
            bool isStatic;
            if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName))
            {
                isStatic = true;
                functionOverloads = targetClepsClass.StaticMemberMethods[targetFunctionName];
            }
            else if (target != null && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName))
            {
                isStatic = false;
                functionOverloads = targetClepsClass.MemberMethods[targetFunctionName];
            }
            else
            {
                string errorMessage = String.Format("Class {0} does not contain a {1}static function called {2}.", targetClepsClass.FullyQualifiedName, target == null? "" : "member or ",targetFunctionName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return CodeGenerator.CreateByte(0);
            }

            int matchedPosition;
            string fnMatchErrorMessage;

            if (!FunctionOverloadManager.FindMatchingFunctionType(TypeManager, functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage))
            {
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, fnMatchErrorMessage));
                //Just return something to avoid stalling compilation
                return CodeGenerator.CreateByte(0);
            }

            FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition].VariableType as FunctionClepsType;

            if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidClepsType.GetVoidType())
            {
                string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return CodeGenerator.CreateByte(0);
            }

            IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(isStatic? null : dereferencedTarget, dereferencedType, targetFunctionName, chosenFunctionType, parameters);
            return returnValue;
        }
Beispiel #4
0
        private LLVMTypeRef GetLLVMType(ClepsType clepsType)
        {
            string      clepsName = clepsType.GetClepsTypeString();
            LLVMTypeRef llvmType;

            if (clepsName.StartsWith("System.RawTypes."))
            {
                if (clepsName == "System.RawTypes.Byte")
                {
                    llvmType = LLVM.Int1TypeInContext(Context);
                }
                else
                {
                    throw new NotImplementedException("Rawtype " + clepsName + " not supported");
                }
            }
            else
            {
                llvmType = ClassSkeletons[clepsName];
            }

            return(llvmType);
        }
        private string GetTypeInstance(ClepsType clepsType)
        {
            string clepsName = clepsType.GetClepsTypeString();
            string ret       = "";

            if (clepsName.StartsWith("System.RawTypes."))
            {
                if (clepsName == "System.RawTypes.Byte")
                {
                    ret = "0";
                }
                else
                {
                    throw new NotImplementedException("Rawtype " + clepsName + " not supported");
                }
            }
            else
            {
                ret = String.Format("new {0}.{1}()", TOPLEVELNAMESPACE, clepsName);
            }

            return(ret);
        }
Beispiel #6
0
        private LLVMTypeRef GetLLVMType(ClepsType clepsType)
        {
            string clepsName = clepsType.GetClepsTypeString();
            LLVMTypeRef llvmType;

            if (clepsName.StartsWith("System.RawTypes."))
            {
                if (clepsName == "System.RawTypes.Byte")
                {
                    llvmType = LLVM.Int1TypeInContext(Context);
                }
                else
                {
                    throw new NotImplementedException("Rawtype " + clepsName + " not supported");
                }
            }
            else
            {
                llvmType = ClassSkeletons[clepsName];
            }

            return llvmType;
        }
        private IValue doFunctionCall(ParserRuleContext context, string targetFunctionName, List <IValue> parameters, IValue target, ClepsType targetType, bool allowVoidReturn)
        {
            IValue         dereferencedTarget = target == null? null : GetDereferencedRegisterOrNull(target);
            BasicClepsType dereferencedType   = target == null? targetType as BasicClepsType : dereferencedTarget.ExpressionType as BasicClepsType;

            if (dereferencedType == null)
            {
                string errorMessage = String.Format("Could not dereference expression on type {0}", targetType.GetClepsTypeString());
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //just return something to avoid stalling
                return(CodeGenerator.CreateByte(0));
            }

            ClepsClass targetClepsClass = ClassManager.GetClass(dereferencedType.GetClepsTypeString());


            List <ClepsVariable> functionOverloads;
            bool isStatic;

            if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName))
            {
                isStatic          = true;
                functionOverloads = targetClepsClass.StaticMemberMethods[targetFunctionName];
            }
            else if (target != null && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName))
            {
                isStatic          = false;
                functionOverloads = targetClepsClass.MemberMethods[targetFunctionName];
            }
            else
            {
                string errorMessage = String.Format("Class {0} does not contain a {1}static function called {2}.", targetClepsClass.FullyQualifiedName, target == null? "" : "member or ", targetFunctionName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            int    matchedPosition;
            string fnMatchErrorMessage;

            if (!FunctionOverloadManager.FindMatchingFunctionType(TypeManager, functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage))
            {
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, fnMatchErrorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition].VariableType as FunctionClepsType;

            if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidClepsType.GetVoidType())
            {
                string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName);
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(isStatic? null : dereferencedTarget, dereferencedType, targetFunctionName, chosenFunctionType, parameters);

            return(returnValue);
        }
        private IValue doFunctionCall(ClepsParser.FunctionCallContext functionCall, ClepsType targetType, bool isMemberFunctionsAccessible, bool allowVoidReturn)
        {
            ClepsClass targetClepsClass;
            
            if(targetType is BasicClepsType)
            {
                targetClepsClass = ClassManager.GetClass(targetType.GetClepsTypeString());
            }
            else if(targetType is BasicStaticClepsType)
            {
                targetClepsClass = ClassManager.GetClass(targetType.GetClepsTypeString());
                isMemberFunctionsAccessible = false;
            }
            else
            {
                throw new NotImplementedException("Function calls on non basic types not supported");
            }

            string targetFunctionName = functionCall.FunctionName.GetText();
            List<IValue> parameters = functionCall._FunctionParameters.Select(p => Visit(p) as IValue).ToList();

            ClepsType functionType = null;
            if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName))
            {
                functionType = targetClepsClass.StaticMemberMethods[targetFunctionName];
            }
            else if (isMemberFunctionsAccessible && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName))
            {
                functionType = targetClepsClass.MemberMethods[targetFunctionName];
            }

            if (functionType == null)
            {
                string errorMessage = String.Format("Class {0} does not contain a function called {1}", targetClepsClass.FullyQualifiedName, targetFunctionName);
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return CodeGenerator.CreateByte(0);
            }

            List<ClepsType> functionOverloads = new List<ClepsType> { functionType };

            int matchedPosition;
            string fnMatchErrorMessage;

            if (!FunctionOverloadManager.FindMatchingFunctionType(functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage))
            {
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, fnMatchErrorMessage));
                //Just return something to avoid stalling compilation
                return CodeGenerator.CreateByte(0);
            }

            FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition] as FunctionClepsType;

            if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidType.GetVoidType())
            {
                string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName);
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return CodeGenerator.CreateByte(0);
            }

            IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(FullyQualifiedClassName, targetFunctionName, chosenFunctionType, parameters);
            return returnValue;
        }
        private IValue doFunctionCall(ClepsParser.FunctionCallContext functionCall, ClepsType targetType, bool isMemberFunctionsAccessible, bool allowVoidReturn)
        {
            ClepsClass targetClepsClass;

            if (targetType is BasicClepsType)
            {
                targetClepsClass = ClassManager.GetClass(targetType.GetClepsTypeString());
            }
            else if (targetType is BasicStaticClepsType)
            {
                targetClepsClass            = ClassManager.GetClass(targetType.GetClepsTypeString());
                isMemberFunctionsAccessible = false;
            }
            else
            {
                throw new NotImplementedException("Function calls on non basic types not supported");
            }

            string        targetFunctionName = functionCall.FunctionName.GetText();
            List <IValue> parameters         = functionCall._FunctionParameters.Select(p => Visit(p) as IValue).ToList();

            ClepsType functionType = null;

            if (targetClepsClass.StaticMemberMethods.ContainsKey(targetFunctionName))
            {
                functionType = targetClepsClass.StaticMemberMethods[targetFunctionName];
            }
            else if (isMemberFunctionsAccessible && targetClepsClass.MemberMethods.ContainsKey(targetFunctionName))
            {
                functionType = targetClepsClass.MemberMethods[targetFunctionName];
            }

            if (functionType == null)
            {
                string errorMessage = String.Format("Class {0} does not contain a function called {1}", targetClepsClass.FullyQualifiedName, targetFunctionName);
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            List <ClepsType> functionOverloads = new List <ClepsType> {
                functionType
            };

            int    matchedPosition;
            string fnMatchErrorMessage;

            if (!FunctionOverloadManager.FindMatchingFunctionType(functionOverloads, parameters, out matchedPosition, out fnMatchErrorMessage))
            {
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, fnMatchErrorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            FunctionClepsType chosenFunctionType = functionOverloads[matchedPosition] as FunctionClepsType;

            if (!allowVoidReturn && chosenFunctionType.ReturnType == VoidType.GetVoidType())
            {
                string errorMessage = String.Format("Function {0} does not return a value", targetFunctionName);
                Status.AddError(new CompilerError(FileName, functionCall.Start.Line, functionCall.Start.Column, errorMessage));
                //Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            IValue returnValue = CodeGenerator.GetFunctionCallReturnValue(FullyQualifiedClassName, targetFunctionName, chosenFunctionType, parameters);

            return(returnValue);
        }
Beispiel #10
0
 public string GetChosenEntryPointOrNull()
 {
     if (EntryPoints.Count == 0)
     {
         string errorMessage = String.Format("No program entry points found. Make sure you have a static function called static function called {0} with signature {1}", EntryPointName, EntryPointType.GetClepsTypeString());
         Status.AddError(new CompilerError("", 0, 0, errorMessage));
         return(null);
     }
     else
     {
         if (SpecifiedEntryPointClass == null)
         {
             if (EntryPoints.Count == 1)
             {
                 return(EntryPoints[0]);
             }
             else
             {
                 string errorMessage = String.Format("Multiple program entry points found. \n{0}.\n. Specify which one you want to use as a compiler parameter.", String.Join(", ", EntryPoints));
                 Status.AddError(new CompilerError("", 0, 0, errorMessage));
                 return(null);
             }
         }
         else
         {
             if (EntryPoints.Contains(SpecifiedEntryPointClass))
             {
                 return(SpecifiedEntryPointClass);
             }
             else
             {
                 string errorMessage = String.Format("Program entry point not found in {0}. Either add a program entry point to {0} or change the specified program entry point.", SpecifiedEntryPointClass);
                 Status.AddError(new CompilerError("", 0, 0, errorMessage));
                 return(null);
             }
         }
     }
 }
        private string GetTypeInstance(ClepsType clepsType)
        {
            string clepsName = clepsType.GetClepsTypeString();
            string ret = "";

            if (clepsName.StartsWith("System.RawTypes."))
            {
                if (clepsName == "System.RawTypes.Byte")
                {
                    ret = "0";
                }
                else
                {
                    throw new NotImplementedException("Rawtype " + clepsName + " not supported");
                }
            }
            else
            {
                ret = String.Format("new {0}.{1}()", TOPLEVELNAMESPACE, clepsName);
            }

            return ret;
        }