protected static void WriteEnumRegisters(StreamWriter stream, Parser.Class cl) { WriteCommentLine(stream); stream.Write( "sulphur::foundation::Vector<ScriptableEnumRegister> " + cl.name + "::EnumRegister()\n" + "{\n" + "\treturn\n" + "\t{\n"); Parser.Enum e; int n = cl.enums.Count; for (int i = 0; i < n; ++i) { stream.Write("\t\t{\n"); e = cl.enums[i]; WriteEnum(stream, e); stream.Write("\t\t}" + (i != n - 1 ? "," : "") + "\n"); } stream.Write( "\t};\n" + "}\n"); }
protected static void WriteComponentRegister(StreamWriter stream, Parser.Class cl) { WriteCommentLine(stream); stream.Write( "sulphur::engine::ComponentIDBase* " + cl.name + "::ComponentRegister()\n" + "{\n" + "\treturn sulphur::foundation::Memory::Construct<ComponentID<" + cl.name + ">>();\n" + "}\n" ); }
protected static void WriteFunctionRegisters(StreamWriter stream, Parser.Class cl, List <FuncDecl> decl_member, List <FuncDecl> decl_static) { RegisterWriter WriteAll = (List <FuncDecl> decl) => { stream.Write( "\treturn\n" + "\t{"); if (decl.Count == 0) { stream.Write("};"); return; } stream.Write("\n"); FuncDecl d; for (int i = 0; i < decl.Count; ++i) { d = decl[i]; stream.Write("\t\t{ \"" + d.func_name + "\", " + d.static_name + " }"); if (i == decl.Count - 1) { stream.Write( "\n" + "\t};"); continue; } stream.Write(",\n"); } }; WriteCommentLine(stream); string register_decl = "sulphur::foundation::Vector<ScriptableFunctionRegister> " + cl.name + "::"; stream.Write( register_decl + "FunctionRegister()\n" + "{\n"); WriteAll(decl_static); stream.Write("\n}\n\n"); WriteCommentLine(stream); stream.Write( register_decl + "MetaRegister()\n" + "{\n"); WriteAll(decl_member); stream.Write("\n}\n\n"); }
protected static void WriteClass(StreamWriter stream, Parser.Class cl) { string[] namespaces = cl.namespaces.Split(new string[] { "::" }, StringSplitOptions.None); foreach (string ns in namespaces) { stream.Write("namespace " + ns + "\n{\n"); } List <FuncDecl> decl_member = new List <FuncDecl>(); List <FuncDecl> decl_static = new List <FuncDecl>(); FuncDecl decl; foreach (Parser.Function func in cl.funcs) { decl = WriteFunction(stream, cl, func); if (func.is_static == true) { decl_static.Add(decl); continue; } decl_member.Add(decl); } WriteFunctionRegisters(stream, cl, decl_member, decl_static); WriteEnumRegisters(stream, cl); if (cl.is_component == true) { WriteComponentRegister(stream, cl); } foreach (string ns in namespaces) { stream.Write("\n}"); } }
protected static FuncDecl WriteFunction(StreamWriter stream, Parser.Class cl, Parser.Function func) { WriteCommentLine(stream); FuncDecl decl; decl.static_name = "lua_" + cl.name + "_" + func.name; decl.func_name = func.name; stream.Write( "void " + decl.static_name + "(ScriptableArgs* args)\n" + "{\n"); string check = func.is_static == false ? "U" : ""; bool is_args = func.args.Count > 0 && func.args[0].type.type == Parser.SupportedTypes.kArgs; if (is_args == false) { foreach (Parser.Function.Arg arg in func.args) { check += TypeToWrapperChar(arg.type.type); } } if (is_args == false) { stream.Write("\targs->Check(\"" + check + "\");\n\n"); } if (func.is_static == false) { stream.Write("\t" + cl.name + "* self = ScriptableObject::CheckUserData<" + cl.name + ">(" + cl.name + "::Name(), args->GetArg(0));\n"); } Parser.Function.Arg a; string type_name; string get_arg; int aidx; int nargs = func.args.Count; for (int i = 0; i < nargs; ++i) { aidx = func.is_static == true ? i : i + 1; a = func.args[i]; type_name = a.type.unqualified_name; get_arg = "args->GetArg(" + aidx + ")"; if (a.type.type == Parser.SupportedTypes.kUserdata) { if (a.type.ref_type == Parser.RefType.kPointer) { stream.Write("\t" + type_name + "* arg_" + i + " = ScriptableObject::CheckUserData<" + type_name + ">(" + type_name + "::Name(), " + get_arg + ");\n"); continue; } } else if (a.type.type == Parser.SupportedTypes.kArgs) { if (a.type.ref_type == Parser.RefType.kPointer) { stream.Write("\tScriptableArgs* arg_" + i + " = args;"); continue; } } stream.Write("\t" + type_name + " arg_" + i + " = ScriptUtils::As<" + type_name + ">(" + get_arg + ");\n"); } stream.Write("\n\t"); if (func.return_type.type != Parser.SupportedTypes.kVoid) { stream.Write(func.return_type.name + " ret = "); } if (func.is_static == true) { stream.Write(cl.name + "::" + func.name + "("); } else { stream.Write("self->" + func.name + "("); } if (nargs == 0) { stream.Write(");\n"); } for (int i = 0; i < nargs; ++i) { stream.Write("arg_" + i); if (i == nargs - 1) { stream.Write(");\n"); continue; } stream.Write(", "); } if (func.return_type.type != Parser.SupportedTypes.kVoid) { if (func.return_type.type == Parser.SupportedTypes.kUserdata) { stream.Write("\targs->AddUserData(ret);\n"); } else { stream.Write("\targs->AddReturn(ret);\n"); } } stream.Write("}\n"); return(decl); }