public override object VisitFunctionAssignment_Ex([NotNull] ClepsParser.FunctionAssignmentContext context)
        {
            VariableManager variableManager = new VariableManager();
            VariableManagers.Add(variableManager);

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

            List<ClepsType> parameterTypes = context._FunctionParameterTypes.Select(t => Visit(context.FunctionReturnType) as ClepsType).ToList();
            FunctionClepsType functionType = new FunctionClepsType(parameterTypes, returnType);

            IMethodRegister methodRegister = CodeGenerator.GetMethodRegister(FullyQualifiedClassName, CurrMemberIsStatic, CurrMemberType, CurrMemberName);
            var formalParameterNames = context._FormalParameters.Select(p => Visit(p) as string).ToList();
            methodRegister.SetFormalParameterNames(formalParameterNames);

            formalParameterNames.Zip(parameterTypes, (name, clepsType) => new ClepsVariable(name, clepsType))
                .ToList().ForEach(variable =>
                {
                    variableManager.AddLocalVariable(variable, methodRegister.GetFormalParameterRegister(variable.VariableName));
                });

            Visit(context.statementBlock());
            VariableManagers.RemoveAt(VariableManagers.Count - 1);
            return functionType;
        }
Exemplo n.º 2
0
        public void Output(string directoryName, string fileNameWithoutExtension, CompileStatus status)
        {
            StringBuilder output = new StringBuilder();
            InitializeOutput(output);

            foreach(var nativeCodeSnippet in GlobalNativeCodeSnippets)
            {
                output.AppendLine(nativeCodeSnippet);
            }

            foreach (var clepsClass in ClassesLoaded)
            {
                GenerateClass(output, clepsClass.Value);
            }

            output.AppendLine(GlobalInitializer.GetMethodBodyWithoutDeclaration());

            FunctionClepsType voidFuncType = new FunctionClepsType(new List<ClepsType>(), VoidClepsType.GetVoidType());
            foreach (var clepsType in CompilerConstants.SystemSupportedTypes)
            {
                output.AppendFormat("{0}.{1}.{2}();\n", JavaScriptCodeParameters.TOPLEVELNAMESPACE, clepsType.GetClepsTypeString(), JavaScriptCodeParameters.GetMangledFunctionName("classStaticInitializer", voidFuncType));
            }

            if (!String.IsNullOrWhiteSpace(EntryPointClass) && !String.IsNullOrWhiteSpace(EntryPointFunctionName))
            {
                output.AppendFormat("{0}.{1}.{2}();\n", JavaScriptCodeParameters.TOPLEVELNAMESPACE, EntryPointClass, JavaScriptCodeParameters.GetMangledFunctionName("classStaticInitializer", voidFuncType));
                output.AppendFormat("{0}.{1}.{2}();\n", JavaScriptCodeParameters.TOPLEVELNAMESPACE, EntryPointClass, JavaScriptCodeParameters.GetMangledFunctionName(EntryPointFunctionName, voidFuncType));
            }

            var outputFileName = Path.Combine(directoryName, fileNameWithoutExtension + ".js");
            File.WriteAllText(outputFileName, output.ToString());
        }
        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;
        }
Exemplo n.º 4
0
        public override bool NotNullObjectEquals(ClepsType obj)
        {
            if (obj.GetType() != typeof(FunctionClepsType))
            {
                return(false);
            }

            FunctionClepsType objToCompare = obj as FunctionClepsType;

            return(ReturnType == objToCompare.ReturnType && ParameterTypes.SequenceEqual(objToCompare.ParameterTypes));
        }
Exemplo n.º 5
0
        public static string GetMangledFunctionName(string currentFunctionName, FunctionClepsType functionType)
        {
            string ret = String.Format("{0} {1}", currentFunctionName, functionType.GetClepsTypeString());

            foreach(var kvp in CHARACTERSUBSTITUTIONS)
            {
                ret = ret.Replace(kvp.Key, kvp.Value);
            }

            return ret;
        }
Exemplo n.º 6
0
        public void Initiate()
        {
            FunctionClepsType voidFuncType = new FunctionClepsType(new List<ClepsType>(), VoidClepsType.GetVoidType());

            ClassesLoaded = new Dictionary<string, ClepsClass>();
            ClassInitializers = new Dictionary<string, JavaScriptMethod>();
            ClassStaticInitializers = new Dictionary<string, JavaScriptMethod>();
            GlobalInitializer = new JavaScriptMethod(voidFuncType);
            EntryPointClass = null;
            EntryPointFunctionName = null;
            GlobalNativeCodeSnippets = new List<string>();
        }
Exemplo n.º 7
0
        private void GenerateClass(StringBuilder output, ClepsClass clepsClass)
        {
            FunctionClepsType voidFuncType = new FunctionClepsType(new List<ClepsType>(), VoidClepsType.GetVoidType());

            EnsureNamespaceExists(output, clepsClass);
            output.AppendLine(JavaScriptCodeParameters.TOPLEVELNAMESPACE + "." + clepsClass.FullyQualifiedName + " = function() {");
            {
                clepsClass.MemberVariables.ToList().ForEach(kvp => output.AppendFormat("\tthis.{0} = undefined;\n", kvp.Key));

                output.AppendFormat("\tthis.{0}();\n", JavaScriptCodeParameters.GetMangledFunctionName("classInitializer", voidFuncType));
                output.AppendFormat("\t{0}.{1}.{2}();\n", JavaScriptCodeParameters.TOPLEVELNAMESPACE, clepsClass.FullyQualifiedName, JavaScriptCodeParameters.GetMangledFunctionName("classStaticInitializer", voidFuncType));
            }
            output.AppendLine("};");

            GenerateMethodWithBody(output, clepsClass.FullyQualifiedName, "classInitializer", voidFuncType, false, ClassInitializers[clepsClass.FullyQualifiedName]);
            GenerateMethodWithBody(output, clepsClass.FullyQualifiedName, "classStaticInitializer", voidFuncType, true, ClassStaticInitializers[clepsClass.FullyQualifiedName]);
        }
Exemplo n.º 8
0
 public JavaScriptMethod(FunctionClepsType methodType) : base("", methodType)
 {
 }
Exemplo n.º 9
0
 public IMethodValue CreateNewMethod(FunctionClepsType functionType)
 {
     var methodRegister = new JavaScriptMethod(functionType);
     return methodRegister;
 }
Exemplo n.º 10
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;
        }
Exemplo n.º 11
0
 public IValue GetFunctionCallReturnValue(string fullyQualifiedClassName, string targetFunctionName, FunctionClepsType clepsType, List<IValue> parameters)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 12
0
        private void GenerateMethodWithBody(StringBuilder output, string fullyQualifiedClassName, string methodName, FunctionClepsType methodType, bool isStatic, JavaScriptMethod method)
        {
            string fullFunctionName = String.Format("{0}.{1}.{2}{3}",
                JavaScriptCodeParameters.TOPLEVELNAMESPACE,
                fullyQualifiedClassName,
                isStatic ? "" : "prototype.",
                JavaScriptCodeParameters.GetMangledFunctionName(methodName, methodType)
            );

            output.AppendFormat("{0} = {1};\n",
                fullFunctionName,
                method.GetMethodText()
            );
        }
Exemplo n.º 13
0
 public JavaScriptMethod(FunctionClepsType methodType)
 {
     MethodType = methodType;
 }
Exemplo n.º 14
0
        public IValue GetFunctionCallReturnValue(string fullyQualifiedClassName, string targetFunctionName, FunctionClepsType clepsType, List<IValue> parameters)
        {
            string parameterString = String.Join(", ", parameters.Select(v => (v as JavaScriptValue).Expression).ToList());
            string code = String.Format("{0}.{1}.{2}({3})", TOPLEVELNAMESPACE, fullyQualifiedClassName, targetFunctionName, parameterString);

            JavaScriptValue ret = new JavaScriptValue(code, clepsType.ReturnType);
            return ret;
        }
Exemplo n.º 15
0
 public void SetMethodType(FunctionClepsType methodType)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 16
0
 public static bool MatchingFunctionTypeExists(List<FunctionClepsType> functionOverloads, FunctionClepsType typeToFind)
 {
     FunctionClepsType matchingTypeIfExists = functionOverloads.Where(o => o == typeToFind).FirstOrDefault();
     return matchingTypeIfExists != null;
 }