예제 #1
0
        public override object VisitBasicType([NotNull] ClepsParser.BasicTypeContext context)
        {
            string rawTypeName = context.RawTypeName.GetText();
            var    ret         = new BasicClepsType(rawTypeName);

            return(ret);
        }
        public override object VisitFunctionCallAssignment([NotNull] ClepsParser.FunctionCallAssignmentContext context)
        {
            BasicClepsType currentType = new BasicClepsType(FullyQualifiedClassName);
            IValue         target      = CurrMemberIsStatic? null : CodeGenerator.GetThisInstanceValue(currentType);

            return(doFunctionCall(context.functionCall(), target, currentType, true /* allowVoidReturn */));
        }
예제 #3
0
        public IValue GetFunctionCallReturnValue(IValue target, BasicClepsType targetType, string targetFunctionName, FunctionClepsType clepsType, List <IValue> parameters)
        {
            string code;

            if (CompilerConstants.SystemSupportedTypes.Contains(targetType) && target != null)
            {
                string fullFunctionName = String.Format("{0}.{1}.prototype.{2}", JavaScriptCodeParameters.TOPLEVELNAMESPACE, targetType.GetClepsTypeString(), JavaScriptCodeParameters.GetMangledFunctionName(targetFunctionName, clepsType));
                string functionTarget   = target != null ? (target as JavaScriptValue).Expression : String.Format("{0}.{1}", JavaScriptCodeParameters.TOPLEVELNAMESPACE, targetType.GetClepsTypeString());
                string parameterString  = String.Join("", parameters.Select(v => ", " + (v as JavaScriptValue).Expression).ToList());

                code = String.Format("{0}.call({1}{2})", fullFunctionName, functionTarget, parameterString);
            }
            else
            {
                string functionTarget   = target != null ? (target as JavaScriptValue).Expression : String.Format("{0}.{1}", JavaScriptCodeParameters.TOPLEVELNAMESPACE, targetType.GetClepsTypeString());
                string fullFunctionName = String.Format("{0}.{1}", functionTarget, JavaScriptCodeParameters.GetMangledFunctionName(targetFunctionName, clepsType));

                string parameterString = String.Join(", ", parameters.Select(v => (v as JavaScriptValue).Expression).ToList());
                code = String.Format("{0}({1})", fullFunctionName, parameterString);
            }

            JavaScriptValue ret = new JavaScriptValue(code, clepsType.ReturnType);

            return(ret);
        }
예제 #4
0
        public IValue CreateClassInstance(BasicClepsType instanceType, List <IValue> parameters)
        {
            string parameterString = String.Join(", ", parameters.Select(v => (v as JavaScriptValue).Expression).ToList());
            string code            = String.Format("new {0}({1})", instanceType.GetClepsTypeString(), parameterString);

            JavaScriptValue ret = new JavaScriptValue(code, instanceType);

            return(ret);
        }
        public override object VisitFunctionCallAssignment([NotNull] ClepsParser.FunctionCallAssignmentContext context)
        {
            if (!ClassManager.IsClassBodySet(FullyQualifiedClassName))
            {
                //This is probably due to some earlier error. Just return something to avoid stalling compilation
                return(CodeGenerator.CreateByte(0));
            }

            ClepsType currentType = new BasicClepsType(FullyQualifiedClassName);

            return(doFunctionCall(context.functionCall(), currentType, !CurrMemberIsStatic, false /* allowVoidReturn */));
        }
예제 #6
0
        public override object VisitThisAssignment([NotNull] ClepsParser.ThisAssignmentContext context)
        {
            if (CurrMemberIsStatic)
            {
                string errorMessage = "Cannot use the this keyword in a static member";
                Status.AddError(new CompilerError(FileName, context.Start.Line, context.Start.Column, errorMessage));
                //just return something to avoid stalling
                return(CodeGenerator.CreateByte(0));
            }

            BasicClepsType currentClassType = new BasicClepsType(FullyQualifiedClassName);
            IValue         thisValue        = CodeGenerator.GetThisInstanceValue(currentClassType);
            IValue         thisPtr          = CodeGenerator.GetPtrToValue(thisValue);

            return(thisPtr);
        }
예제 #7
0
 public IValue CreateClassInstance(BasicClepsType instanceType, List <IValue> parameters)
 {
     throw new NotImplementedException();
 }
        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);
        }
예제 #9
0
        public IValue GetThisInstanceValue(BasicClepsType thisInstanceType)
        {
            JavaScriptValue ret = new JavaScriptValue("this", thisInstanceType);

            return(ret);
        }