Interface conversion method between different languages, language interface system, and program running method
Patent Information
- Application Number
- CN202510563345.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-29
- Publication Date
- 2026-10-30
AI Technical Summary
每个C++函数均需重复上述参数校验、解析与注册流程;若函数参数变动,需同步修改相关代码非常繁琐
[0029]本发明实施例提供的不同语言之间的对接转换方法包括以下步骤:设定T C L语言为第一语言,对第一语言的不同部分进行定义,以将第一语言分成第一语言命令、第一语言选项和/或参数和第一语言返回值三个部分;设定C++语言为第二语言,对第二语言的不同部分进行定义,以将第二语言分成第二语言函数过程、第二语言变量赋值和第二语言运行结果三个部分;建立由第一语言命令至第二语言函数过程的第一映射关系;建立由第一语言选项和/或参数至第二语言变量赋值的第二映射关系;建立由第二语言运行结果至第一语言返回值的第三映射关系;将第一映射关系、第二映射关系、第三映射关系输入至语言接口系统进行编译,将新的T C L命令输入至编译后的语言接口系统进行T C L语言与C++语言的对接转换。本实施例通过将T C L命令拆解为命令、参数和/或选项、返回值三大模块,并与C++的函数过程、变量赋值、运行结果建立映射关系,实现了两种语言的结构化对接。开发者只需定义模块间的对应规则,系统便捷自动完成语义翻译和类型转换。
Smart Images

Figure CN123011199A_ABST
Abstract
Description
[Technical Field]
[0001] This invention relates to the field of cross-language programming interface technology, and in particular to a method for interface conversion between different languages, a language interface system, computer equipment, and media. [Background Technology]
[0002] TCL (Tool Command Language) is a dynamically interpreted scripting language whose core runtime environment is a command-line interactive interface. Users can input text commands in a specific format through the interface, executing them line by line for parsing, function invocation, and automated control. C++ is a compiled high-level language, often used for core algorithm or low-level module development due to its high performance and system-level control capabilities. To achieve collaboration between the two, developers need to build an interface layer between C++ and TCL, allowing TCL scripts to directly call C++ functions or classes, while simultaneously allowing C++ code to dynamically execute TCL instructions.
[0003] Existing methods typically involve manually wrapping the TCL extension library. This requires developers to manually register each C++ function and handle type conversions and error handling. Each C++ function must repeat the parameter validation, parsing, and registration process; if function parameters change, the relevant code must be modified synchronously, making it extremely tedious. Another existing method uses the SWIG (Simplified Wrapper and Interface Generator) tool to automatically generate C++ to TCL binding code. However, SWIG cannot automatically associate strings with C++ enumeration values, requiring developers to write string-to-integer conversion logic. This presents two pain points: first, manual coding or complex configuration is time-consuming and hinders rapid iteration; second, it easily leads to compatibility issues during maintenance. In other words, existing methods cannot easily interface and convert between C++ and TCL languages. [Summary of the Invention]
[0004] In order to find a convenient method for interfacing and converting between C++ and TCL languages, this invention provides an interfacing and conversion method between different languages, a language interface system, a computer device, and a medium.
[0005] To solve the above-mentioned technical problems, this invention provides the following technical solution: a method for interfacing and converting between different languages, used to convert C++ language to TCL language, comprising the following steps:
[0006] Set TCL language as the first language, and define different parts of the first language to divide the first language into three parts: first language commands, first language options and / or parameters, and first language return values;
[0007] We define C++ as the second language and define different parts of the second language, dividing it into three parts: second language function procedures, second language variable assignments, and second language execution results.
[0008] Establish the first mapping relationship from first language commands to second language functions;
[0009] Establish a second mapping relationship from the assignment of first language options and / or parameters to second language variables;
[0010] Establish a third mapping relationship from the results of the second language execution to the return value of the first language;
[0011] The first, second, and third mapping relationships are input into the language interface system for compilation; the new TCL commands are input into the compiled language interface system for TCL language and C++ language interface conversion.
[0012] Preferably, the first language command is the name of the TCL command, and the second language function procedure is the entire procedure of the function in the C++ language. After the first mapping relationship is established, the corresponding TCL command is input, and the language interface system will automatically call the corresponding function in the C++ language.
[0013] Preferably, after the second mapping relationship is established, the options and / or parameters can be set to integers, floating-point numbers, or strings.
[0014] Preferably, after establishing the second mapping relationship, the process further includes:
[0015] Add a fourth part to set the first language; the fourth part is the first language string.
[0016] Add a fourth part to the second language settings; the fourth part is the second language enumeration.
[0017] Establish a fourth mapping relationship from the first language string to the second language enumeration;
[0018] Once the fourth mapping relationship is established, the options and / or parameters can be set to integers, floating-point numbers, strings, or enumerations.
[0019] Preferably, the first mapping relationship, the second mapping relationship, the third mapping relationship, and the fourth mapping relationship are four different program constructions, and the above conversion method can quickly define these four mapping relationships.
[0020] Preferably, after completing the interface conversion between C++ and TCL languages, the process also includes:
[0021] If the developer provides a new custom TCL command, the new custom TCL command is converted to C++ language analysis by the language interface system to obtain the C++ language execution result, and then the execution result is returned to TCL through the language interface, and the return value is obtained in the TCL language input interface.
[0022] To solve the above-mentioned technical problems, the present invention provides another technical solution as follows: a language interface system, applied to the above-mentioned method for interfacing and converting between different languages, wherein the language interface system includes a display module and a processing module connected to each other, and the processing module includes a TCL command interpreter, a command registration module, a command management module, and a parameter parsing module connected in sequence;
[0023] When the first mapping relationship, the second mapping relationship, and the third mapping relationship are connected to the language interface system using the solution invented in this patent, they together with the command registration module and the command management module generate a compiled executable program.
[0024] When the processing module and the display module are compiled together, a TCL language input interface can be provided in the display module. When a TCL command is entered on the input interface, the parameter parsing module parses the entered TCL command and converts it into C++ language for execution.
[0025] Preferably, the command management module includes an execution command management module, a registration naming option module, a registration location parameter module, and a setting command result module. The execution command management module is connected to the command registration module and is used to establish a first mapping relationship. The registration naming option module and the registration location parameter module are used to establish a second mapping relationship and a fourth mapping relationship. The setting command result module is used to establish a third mapping relationship.
[0026] To solve the above-mentioned technical problems, the present invention provides another technical solution as follows: a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, the computer instructions being used to cause the computer to execute the above-mentioned interface conversion method between different languages.
[0027] To solve the above-mentioned technical problems, the present invention provides another technical solution as follows: a computer device for use in the above-mentioned interfacing and conversion method between different languages, comprising a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the interfacing and conversion method between the different languages.
[0028] Compared with existing technologies, the present invention provides a method for interfacing and converting between different languages, a language interface system, a computer device, and a medium, which have the following beneficial effects:
[0029] The interface conversion method between different languages provided in this embodiment of the invention includes the following steps: Setting TCL language as the first language, defining different parts of the first language to divide it into three parts: first language commands, first language options and / or parameters, and first language return values; setting C++ language as the second language, defining different parts of the second language to divide it into three parts: second language function procedures, second language variable assignments, and second language execution results; establishing a first mapping relationship from first language commands to second language function procedures; establishing a second mapping relationship from first language options and / or parameters to second language variable assignments; establishing a third mapping relationship from second language execution results to first language return values; inputting the first, second, and third mapping relationships into a language interface system for compilation, and inputting new TCL commands into the compiled language interface system for interface conversion between TCL and C++ languages. This embodiment achieves structured interface between the two languages by decomposing TCL commands into three modules: commands, parameters and / or options, and return values, and establishing mapping relationships with C++ function procedures, variable assignments, and execution results. Developers only need to define the corresponding rules between modules, and the system can easily and automatically complete semantic translation and type conversion. [Attached Image Description]
[0030] Figure 1 This is a flowchart of the interface conversion method between different languages provided in the first embodiment of the present invention.
[0031] Figure 2 This is a flowchart illustrating the establishment of a fourth mapping relationship in the interoperability and conversion method between different languages provided in the first embodiment of the present invention.
[0032] Figure 3 This is a schematic diagram of the language interface system provided in the second embodiment of the present invention.
[0033] Figure 4 This is a schematic diagram of the structure of the computer device provided in the fourth embodiment of the present invention.
[0034] Explanation of reference numerals in the attached diagram:
[0035] 2. Computer equipment;
[0036] 21. Memory; 22. Processor; 23. Computer program.
Detailed Implementation Methods
[0037] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0038] In the embodiments provided by this invention, it should be understood that "B corresponding to A" means that B is associated with A, and B can be determined based on A. However, it should also be understood that determining B based on A does not mean determining B solely based on A; B can also be determined based on A and / or other information.
[0039] It should be understood that the phrase "one embodiment" or "an embodiment" throughout the specification means that a specific feature, structure, or characteristic related to the embodiment is included in at least one embodiment of the invention. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Those skilled in the art should also recognize that the embodiments described in the specification are optional embodiments, and the actions and modules involved are not necessarily essential to the invention.
[0040] In various embodiments of the present invention, it should be understood that the sequence number of each process does not necessarily imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0041] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, or they may sometimes be executed in reverse order, depending on the functions involved. It is particularly important to note that each block in a block diagram and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0042] TCL (ToolCommand Language) is a dynamically interpreted scripting language whose core runtime environment is a command-line interactive interface. Users can input text commands in a specific format through the interface, executing them line by line for parsing, function invocation, and automated control. C++ is a compiled high-level language, often used for core algorithm or low-level module development due to its high performance and system-level control capabilities. To achieve collaboration between the two, developers need to build an interface layer between C++ and TCL, allowing TCL scripts to directly call C++ functions or classes, while simultaneously allowing C++ code to dynamically execute TCL instructions.
[0043] Example 1: A simple TCL command is as follows (lines starting with # are comments, used to explain the next line; text after / / is a comment, used to explain the code):
[0044] 1#input
[0045] 2 subtract 5 3-format float at 3
[0047] 4#output 5 2.0
[0049] Here, 5 and 3 are parameters; the first parameter is the minuend, and the second parameter is the subtrahend. To obtain the result of the TCL command, the user needs to call a C++ function to perform the calculation. The C++ function that implements the above TCL command functionality is as follows:
[0050] 1enum class OutputFormat{Int,Flo at};
[0051] 2flo at subtract(int a,int b,OutputFormat fmt=OutputFormat::Int){
[0052] 3flo at result = ab;
[0053] 4return(fmt==OutputF ormat::Flo at)? result:st atic_cast <int>(result);
[0054] 5}
[0055] It is evident that for the same functionality, the coding style of C++ differs significantly from that of TCL. In relevant fields, each TCL command corresponds to a C++ module that implements the corresponding algorithm. Therefore, there is a need to implement an interface between TCL commands and C++ functions.
[0056] Existing methods typically involve manually wrapping the TCL extension library. This requires developers to manually register each C++ function and handle type conversions and error handling. Each C++ function must repeat the parameter validation, parsing, and registration process; if function parameters change, the relevant code must be modified synchronously, making it extremely tedious. Another existing method uses the SWIG (Simplified Wrapper and Interface Generator) tool to automatically generate C++ to TCL binding code. However, SWIG cannot automatically associate strings with C++ enumeration values, requiring developers to write string-to-integer conversion logic. This has two major drawbacks: first, manual coding or complex configuration is time-consuming and hinders rapid iteration; second, it easily leads to compatibility issues during maintenance. In other words, existing methods cannot easily interface and convert between C++ and TCL languages.
[0057] To resolve the above issues, please refer to Figure 1 The first embodiment of the present invention provides a method for interfacing and converting between different languages, used to convert C++ language to TCL language, including the following steps:
[0058] S1, set TCL language as the first language, define different parts of the first language, and divide the first language into three parts: first language commands, first language options and / or parameters, and first language return values;
[0059] S2 sets C++ as the second language and defines different parts of the second language to divide the second language into three parts: second language function procedures, second language variable assignment, and second language execution results.
[0060] S3, establish the first mapping relationship from the first language command to the second language function process;
[0061] S4, establish a second mapping relationship from the assignment of first language options and / or parameters to second language variables;
[0062] S5, establish a third mapping relationship from the second language execution result to the first language return value;
[0063] S6, input the first mapping relationship, the second mapping relationship, and the third mapping relationship into the language interface system for compilation;
[0064] S7 inputs the new TCL commands into the compiled language interface system to perform the interface conversion between TCL language and C++ language.
[0065] Understandably, since existing methods cannot easily interface TCL commands and the C++ language, this implementation provides a method to achieve rapid conversion between TCL and C++ languages using a language interface system that allows TCL commands to interface with the C++ language. Taking Example 1 as an example, firstly, TCL language is set as the first language, then the TCL language is divided into three parts, and each part is defined separately. In Example 1, firstly, "subtract" is the name of the TCL command. Each TCL command can perform its specific function, and this part is defined as a first language command. Secondly, "5 and 3" are the parameters of the TCL command. The TCL command identifies the parameter by its position. For example, in this TCL command, the first parameter is the minuend, and the second parameter is the subtrahend. "-format float" is the option of the TCL command, consisting of the option name "-format" and the option value "float". Therefore, the parameters and options in the TCL command are defined as first language options and / or parameters. Furthermore, "2.0" is the result of the TCL command. Outputting only the first four lines of data in Example 1 will not produce the fifth line of result. This result requires calculation using other function tools (i.e., the C++ function procedure corresponding to the TCL command). Therefore, "2.0" is defined as the first language return value. In other words, the TCL command is first divided into three parts based on the functions and attributes of different fields, and these three parts are defined separately.
[0066] Furthermore, to establish interoperability between different languages, the goal is to achieve convenient conversion between C++ and TCL languages. Existing technologies that rely on manual registration or writing additional interfaces undoubtedly suffer from efficiency drawbacks. In this embodiment, C++ is set as the second language, and is also divided into three parts, as shown in Example 1. First, the content of lines 2 to 4 in the C++ function: "flo at subtract……static_c ast <int>"(result)" represents the entire implementation process of the C++ function; this part is defined as a second-language function procedure. Secondly, "a", "b", and "fmt" are the variable values of the C++ function; therefore, this part is defined as a second-language variable assignment. Furthermore, "return(fmt==OutputFormat::Float)? result:static_cast" <int>"(result)" is the output after running the content of lines 2 to 4, so this part is defined as the second language running result.
[0067] It should be understood that this embodiment divides the TCL language into three parts, and the C++ language into three parts as well. To facilitate convenient interface conversion between the C++ and TCL languages, it is now necessary to establish a mapping relationship between the TCL and C++ languages. The first step is to establish a first mapping relationship from first language commands to second language functions. Specifically, establishing this first mapping relationship can be achieved through a preset first function, such as reg_cmd. <t>() Template functions. The first language command can be understood as the command name. When the user enters the name of the TCL command in the interface, based on the first mapping relationship, C++ can be called to run the entire function process and perform calculations. Furthermore, a second mapping relationship can be established through preset second functions, such as `reg_argument()` / `reg_option()`. When the user enters the parameters and options of the TCL command in the interface, based on the second mapping relationship, the parameters and options of the TCL command can be automatically converted into C++ variable values. Furthermore, a third mapping relationship can be established through preset third functions, such as `set_result()`. When the user enters the name, parameters, and options of the TCL command in sequence, based on the first and second mapping relationships, a C++ function is first called to calculate the C++ variable values, and finally the result of the C++ function is obtained. Based on the third mapping relationship, this result can be converted into the return value of the TCL command and output.
[0068] Specifically, the first, second, and third mapping relationships can be understood as user-defined correspondences. Inputting these correspondences into the language interface system for compilation establishes a conversion channel between C++ and TCL languages. When a user needs to use TCL to call the corresponding function of a C++ program provided by the developer, they only need to input the corresponding TCL command into the language interface system's input interface. This will call the C++ function to perform calculations on the TCL command, and the result can be returned in TCL as a return value. For example, as shown in Example 1, when "subtract 5 3-format float" is input into the language interface system's input interface, the system will call the C++ function to perform the calculation, and then return a value of "2.0". It should be understood that this embodiment achieves structured integration between the two languages by breaking down TCL commands into three main modules: command, parameters and / or options, and return value, and establishing mapping relationships with C++ function procedures, variable assignments, and execution results. Developers only need to define the correspondence rules between modules, and the system automatically completes semantic translation and type conversion. In industrial automation systems, this solution simplifies the process, which previously required manually writing hundreds of lines of interface code or adding redundant interfaces to each existing one—a process that was extremely tedious. This embodiment simplifies it to declarative configuration, greatly shortening the development cycle.
[0069] Specifically, the first language command is the name of the TCL command, and the second language function procedure is the entire procedure of a function in C++. Once the first mapping relationship is established, inputting the corresponding TCL command will automatically call the corresponding function in C++. It should be understood that this embodiment utilizes reg_cmd. <t>This template function () calls the corresponding C++ function when the user enters the name of a TCL command. When the user enters the TCL command name on the interface, the language interface system automatically triggers the execution of the corresponding C++ function by matching the command name. This process eliminates the tedious step of manually registering functions in traditional solutions.
[0070] Specifically, after establishing the second mapping relationship, options and / or parameters can be set to integers, floating-point numbers, or strings. It should be understood that integers are mathematical integer values, excluding decimal parts, and can be positive, negative, or zero. They are typically represented by the `int` type, such as 42, -5, or 0. They are stored in memory in precise binary form without loss of precision. Floating-point numbers are numerical values with decimal parts, or numbers represented in scientific notation, such as 3.14 or 1.5e6. They are typically represented by the `float` or `double` type in programming. Strings are sequences of text composed of characters, typically letters, numbers, symbols, spaces, etc. The parameters in this implementation are applicable to various types. The system automatically converts the numerical and string parameter types in TCL commands to their corresponding C++ variable types based on the variable types defined by the developer. In communication protocol testing, if the developer uses the `reg_option` function to establish a second mapping relationship between the "-timeout" option and an integer C++ variable, the user-inputted string parameter "-timeout 500" will be automatically converted to an integer value, eliminating the need for the developer to manually write type validation code and removing the risk of system crashes due to type errors.
[0071] Furthermore, please combine Figure 1 and Figure 2 After establishing the second mapping relationship, it also includes:
[0072] S41. Add a fourth part to set the first language, where the fourth part is the first language string;
[0073] S42. Add a fourth part to the setting of the second language, the fourth part being the second language enumeration;
[0074] S43. Establish a fourth mapping relationship from the first language string to the second language enumeration;
[0075] Once the fourth mapping relationship is established, the options and / or parameters can be set to integers, floating-point numbers, strings, or enumerations.
[0076] Understandably, after establishing the second mapping relationship, options and / or parameters can only be set to integers, floating-point numbers, and strings. Enumerations cannot be converted. Establishing the fourth mapping relationship adds conversion functionality for enumeration classes. It should be noted that an enumeration is a special programming data type used to define a set of named constant values. These constants typically represent discrete values within a fixed range, such as status, category, and options. Each constant corresponds to a unique integer value, but they are referenced by name to improve code readability and security. This embodiment further adds a fourth part to the first language, where a first language string is added. A fourth part to the second language is also added, where a second language enumeration is added. Then, a fourth mapping relationship between strings and enumerations is established. For example, the "flo at" string in the TCL command in Example 1 corresponds to "OutputFormat::Float" in the C++ function. Without establishing the fourth mapping relationship, the correspondence between enumerations cannot be converted. For example, for enumeration types in C++, the language interface system uses a developer-defined string-enumeration value mapping table to implement automatic conversion. When the "-mode fast" option appears in the TCL command, "fast" is automatically mapped to a predefined enumeration constant in C++, which reduces the amount of interface code for switching encoding modes, and when adding a new mode, only the mapping table needs to be extended without modifying the core logic.
[0077] Specifically, the first, second, third, and fourth mapping relationships represent four different program constructs. Program construct refers to the overall architecture and design patterns of software, describing how different components collaborate to achieve functionality. It focuses on code organization, module division, and the design of data and control flow. Code is the concrete implementation of a program construct, composed of syntax rules and data structures. Functions are the modular units of code, responsible for implementing specific tasks.
[0078] Specifically, the algorithm for establishing the first mapping is as follows:
[0079] S11. Register TCL command: By calling the preset first function (such as the reg_cmd function), passing in the custom command class and command name (cmd_name function), an instance object of the command class is created.
[0080] S12. Establish command mapping: In the preset second function (such as the constructor of cmd_base), call the TCL underlying interface (Tcl_CreateObj Command) to bind the TCL command name with a unified callback function (such as run_cmd) and the current command object (this pointer).
[0081] It should be understood that in this embodiment, when the user enters the command name in the TCL interface, the run_cmd function will be automatically triggered, and the associated command object will be passed as a parameter.
[0082] S13, Command Execution Logic: When a user invokes a TCL command, it performs the functions of parameter parsing, method invocation, and result return.
[0083] For example, the first mapping is jointly established by the reg_cmd function of tcl_mgr, the constructor of cmd_base, and the run_cmd function, as shown in the following code example:
[0084]
[0085] Here, `commands_` is a member variable of `tcl_mgr`, and its type is `std::vector`. <unique_ptr<cmd_base> >, used to store the registered command class.
[0086] The constructor for cmd_b ase is as follows:
[0087]
[0088] The core function of the constructor of cmd_b as e is Tcl_CreateO bj Command, which is a basic component provided by the Tcl language. It is used to register a Tcl command so that when the user enters the corresponding string (i.e., the command name, corresponding to the variable cmd_name) in the Tcl language interface, the C++ function cmd_b as e::run_cmd is automatically called, and the pointer (this, i.e., the object address of the command class T) entered during registration is passed to run_cmd.
[0089] The two functions above implement the tcl command's call to the run_cmd function. Example code for run_cmd is as follows:
[0090]
[0091]
[0092] The `run_cmd` function, after parsing the parameters and options of the Tcl command, uses the passed-in pointer (i.e., the pointer to the corresponding command class object, referred to as `this` above, and `cd` in the `run_cmd` function parameter) to call the overridden `run()` function of the custom command class. Through this process, the binding between the Tcl command and the corresponding command class's `run()` function is achieved, thus realizing the first mapping.
[0093] Specifically, the algorithm for constructing the second mapping is as follows:
[0094] S21. Register command options and parameters: When registering a TCL command, the default third function (such as the reg_option function) and the default fourth function (such as the reg_argument function) are called to define the options and parameters of the command.
[0095] The control language interface system encapsulates the definition information of options and parameters into objects of the parameter parsing module.
[0096] Establish a mapping relationship between the option name and the corresponding parameter parsing module object.
[0097] The definitions of options and parameters are stored in their respective containers. It should be noted that a container is a data structure used to store and manage multiple data objects of the same type or related to each other. Containers have both storage and management functions.
[0098] S22. Apply default value initialization: Before parsing user input, call the fifth preset function (such as the apply_default_value function) to assign predefined default values to all options and parameters.
[0099] It should be understood that this step ensures that the system automatically initializes with default values when the user does not enter certain options or parameters.
[0100] S23. Parse user input parameters:
[0101] When a user invokes a TCL command, the following parsing process is executed:
[0102] S231, Extract input parameters.
[0103] For example, this step can convert user-inputted TCL parameters into an editable list.
[0104] S232, Match and process options.
[0105] It should be understood that option names starting with special characters can be used to find the corresponding parameter parsing module object; it can also delete the identified option names from the list and extract their associated values; it can also parse the values into C++ types and store them in a preset memory address.
[0106] S233, Process the remaining parameters.
[0107] It should be understood that this step can either match the remaining parameters in the list that are not recognized as options in order with the definitions in the editable list, or parse the parameter values and store them in the corresponding memory address.
[0108] For example, during the process of establishing the first mapping, the reg_cmd function calls cmd->reg_options() to establish the second mapping.
[0109] The cmd_base class contains relevant member variables:
[0110] std::map<std::string,int> _opt_map = {};
[0111] std::vector<cmd_spec> _options={};
[0112] const std::vector<cmd_spec> _args = {};
[0113] The `_options` directory stores the `cmd_spec` objects corresponding to all options of the current command, and the `_args` directory stores the `cmd_spec` objects corresponding to all arguments of the current command. The `_opt_map` directory stores the relationship between the option names and their corresponding `cmd_spec` objects.
[0114] The reg_options() function calls a series of reg_option and reg_argument functions defined by the developer to create cmd_spec objects corresponding to the options and parameters, and stores them in the aforementioned member variables.
[0115] After creating the corresponding objects, during the actual command execution, the `run_cmd` function uses these objects to parse the strings entered when calling the tcl command, thereby assigning values to the corresponding variables. This process is implemented in the `cmd->parse_options(objc,objv)` function.
[0116] The run_cmd function receives tcl parameters passed in from the tcl language and stores them in two variables: objc and objv.
[0117] `objc` represents the number of TCL parameters, and `objv` is an array of length `objc`, where each element is a corresponding TCL parameter. It's important to note that the meaning of "TCL parameters" here differs slightly from "options and parameters" in the patent. For example, entering the example command...
[0118] In the patent, "parameters" in `subtract53-formatfloat` specifically refer to the numbers 5 and 3, which are defined by specific Tcl commands. These Tcl parameters, i.e., the contents of the objv object, consist of four Tcl objects: `{"5", "3", "-format", "float"}`, corresponding to the objc value of 4.
[0119] In the `parse_opitions` function, the contents of `objv` will first be used for matching options, as shown in the following example code:
[0120]
[0121]
[0122] This code first converts `obj v` to a `vector tcl_args` for easier editing. At this point, `objv = {"5", "3", "-format", "flo at"}` is converted to `tcl_args = {"5", "3", "-format", "flo at"}` because `vector` objects have the ability to add and delete.
[0123] Next, the string will be traversed, matching the name of the option. Here, "-format" is matched. After a successful match, the string "-format" is removed from tcl_args, and its corresponding cmd_spec object is found, namely _options.at(opt_it->s econd). The string "flo at" is extracted and used as the parameter of the save_data function. save_data will parse the string "flo at" and convert it into the corresponding C++ variable, and then store it in the address recorded by cmd_spec. The variable after parsing the string "float" is stored in the memory address corresponding to the option "-format", which is the second mapping option part implementation.
[0124] After the matching options are completed, the corresponding Tcl parameter list tcl_args = {"5", "3"}, which correspond to the first and second parameters respectively, will be saved sequentially to the corresponding variable addresses. This is the implementation of the parameter mapping part in the second mapping. The function related to "parsing the string into the value of a C++ variable" used is very simple and is not covered by this patent, so it will not be elaborated on.
[0125] At the beginning of parse_options, the apply_default_value() function initializes options and parameters with default values, implementing the function of assigning default values when the user does not enter the corresponding options or parameters.
[0126] Specifically, the algorithm for constructing the third mapping is as follows:
[0127] S 31. Define a unified result interface: Through the C++ function overloading mechanism, define result setting functions with the same name for different data types to form a unified call entry point.
[0128] S 32, Automatic Type Conversion and Wrapping:
[0129] S 321. Perform type identification.
[0130] It should be understood that type identification can automatically match the corresponding overloaded function based on the actual type of the input data.
[0131] S 322, Object encapsulation.
[0132] Specifically, it converts C++ data into a TCL interpreter-compatible result object.
[0133] S 323, Return the results to the TCL interface.
[0134] It should be understood that passing the encapsulated TCL result object to the TCL interpreter allows the TCL command caller to directly obtain the parsed data, and the data type is completely consistent with the C++ calculation result.
[0135] For example,
[0136]
[0137] It mainly uses C++'s overloading mechanism, which allows any supported variable type to be output using the same function call form, namely set_result.
[0138] Specifically, in the fourth mapping, when defining options or parameters of type enum, a corresponding enum_map needs to be specified, which is stored in cmd_spec. When the corresponding option or parameter is matched, the string will be converted using this enum_map in the save_data function to obtain the corresponding enum type value. This implements the fourth mapping.
[0139] It should be understood that the above code is only for functional illustration. The actual code can be adjusted according to the designer's actual needs, and will not be elaborated further. Four mapping relationships are established through four program structures to handle command mapping, parameter binding, result return, and enumeration conversion respectively, realizing the modular encapsulation of interface logic.
[0140] Furthermore, after completing the interface conversion between C++ and TCL languages, it also includes:
[0141] After being converted to C++ language by the language interface system, the execution result of the C++ language is obtained. Then, the execution result is returned to the TCL command through the language interface to obtain the return value, thus establishing a correspondence between the TCL command and the return value. It should be understood that in this embodiment, the relationship between the TCL command and the function in the C++ language can be either one-to-one or many-to-one. For example, multiple TCL commands can run the same C++ function.
[0142] Please combine Figure 1 and Figure 3 The second embodiment of the present invention also provides a language interface system applied to the above-mentioned method for interfacing and converting between different languages. The language interface system includes interconnected processing modules, and the processing modules include a TCL command interpreter, a command registration module (cmd_mgr), a command management module (cmd_b ase), and a parameter parsing module (cmd_spec) connected in sequence.
[0143] Furthermore, once the first, second, third, and fourth mapping relationships are defined by the developer as program code, the command registration module and the command management module jointly complete the compilation process to implement them. Specifically, the command management module includes an execution command management module (run_cmd), a registration naming option module (reg_option), a registration positional parameter module (reg_argument), and a command result setting module (set_result). The execution command management module is connected to the command registration module and is used to establish the first mapping relationship. The registration naming option module and the registration positional parameter module are used to establish the second and fourth mapping relationships, and the command result setting module is used to establish the third mapping relationship. By compiling and then executing the language interface system, convenient conversion between different languages can be achieved.
[0144] It should be understood that establishing and running a language interface system involves two processes. First, the existing language interface system needs to be compiled. Only after compilation can the three mapping relationships be compiled into the language interface system, requiring the use of the command registration module and the command management module. Once compilation is complete, the input TCL commands can then utilize the three mapping relationships to run the corresponding C++ functions. Specifically, when establishing the first mapping relationship, the command registration module (cmd_mgr) and the command management module (cmd_base) are mainly used. The command registration module (cmd_mgr) includes the static reg_cmd... <t>The function () refers to the class derived from cmd_base. During compilation, the TCL command will be based on static reg_cmd. <t>The parentheses function compiles the corresponding C++ code, thus completing the first mapping relationship between TCL commands and C++ function procedures.
[0145] Furthermore, the command management module (cmd_b as e) can establish a second mapping relationship. Specifically, the binding naming options (reg_option) and binding positional parameters (reg_argument) within the command management module (cmd_b ase) can establish a second mapping relationship between TCL command options and / or parameters and their assignment to C++ language variables. For example, when "5" and "3" are entered in the TCL command, based on the command management module (cmd_b ase), they will automatically correspond to "a" and "b" in the C++ language function. That is, cmd_b ase completes the mapping between options and / or parameters and variable values in both languages. Furthermore, the command management module (cmd_b as e) also provides the set_result() function, which can automatically convert C++ primitive types, strings, and STL containers into TCL objects. For example, after the C++ language function obtains its execution result, it will ultimately output the return value "2.0" in the TCL command, thus completing the establishment of the third mapping.
[0146] Furthermore, when developers create new custom TCL commands, that is, define the first, second, and third mapping relationships of the commands, the command registration module and the command management module jointly complete the compilation work. After the processing module and the display module are compiled together, a TCL language input interface can be provided in the display module. When a TCL command is entered on the input interface, the parameter parsing module parses the entered TCL command to convert it into C++ language for execution.
[0147] It should be understood that the parameter parsing module (cmd_spec) is responsible for the specific implementation of the second mapping. For example, when a user inputs a TCL command on the input interface, the parameter parsing module parses the input TCL command and converts it into C++ language. For instance, when the user inputs "5" and "3", based on the parameter parsing module (cmd_spec), it will be automatically converted into "a" and "b" in a C++ language function, and then used for calculation. That is, the command management module (cmd_base) writes the corresponding content and establishes the mapping relationship, while the parameter parsing module (cmd_spec) is the high-speed language interface system that implements the mapping relationship. The two modules have different functions, but together they achieve the language conversion function.
[0148] Specifically, the parameter parsing module (cmd_spec) has the following functions:
[0149] Variable address binding: Stores C++ variables registered by the developer, such as parameters "5" and "3" and variable values "a" and "b".
[0150] String parsing: For example, the process of parsing the value "5" from the TCL command input interface to the variable value "a" is the string parsing process. Specifically, the function s ave_data() converts the string input by TCL into the target type and writes it to the binding address.
[0151] Error interception: If the user enters incorrect characters, the error interception process will begin. If the conversion fails (e.g., an invalid string to integer conversion), an exception will be thrown in the language interface system and the parsing process will be terminated.
[0152] Enumeration type resolution: If the type set by the developer is not int but an enumeration type, the parameter resolution module will automatically enter the enumeration type resolution process, that is, use the fourth mapping relationship defined by the developer to convert the string input by TCL into the corresponding enumeration type.
[0153] Specifically, the command management module (cmd_b as e) serves as the base class for all TCL commands and defines the following core capabilities:
[0154] The static function run_cmd(): automates process control, executing default value initialization, parameter parsing, business logic calls (triggering the developer-implemented run()), and result encapsulation in sequence.
[0155] The default value initialization function means that when you don't input a certain parameter or option, the C++ variable value will acquire a default value, so you don't need to input all the options to run a command. Assuming ten options correspond to ten C++ variable values, you don't need to input all ten options; for example, you only need to input two. The other eight `run_cmd()` functions will use the default values to run the C++ function. In this embodiment, the language interface system will automatically call the C++ variable values corresponding to the options. The business logic call function means that developer-defined command classes are implemented by overriding the `run` function and `reg_options`. Finally, when using a developer-defined command class, objects are generated through `reg_option` (binding named options) and `reg_argument` (binding positional parameters). The developer-defined command class containing the object is further parsed using the parameter parsing module (`cmd_spec`).
[0156] The virtual function run() is overridden by the developer in the derived class corresponding to the command to implement the specific function logic and can directly access the bound member variables;
[0157] Parameter binding: Parameter mapping relationships are declared through reg_option (bind named options) and reg_argument (bind positional parameters), supporting default value settings and mapping to enumeration types, that is, establishing a second mapping relationship and a fourth mapping relationship.
[0158] Let's start with an example of code that creates a custom command for developers:
[0159]
[0160]
[0161] In the code examples above, the part after the / / symbol provides code explanations. The code provided in this embodiment is for illustrative purposes only. Specifically, the language interface provided in this embodiment first performs parameter parsing and execution, for example:
[0162] Step 1: The TCL interpreter calls my_cmd 100-opt"he llo"-enum"type 1", enters the C++ function run_cmd(), which controls the subsequent execution flow;
[0163] First, initialize the default values:
[0164] Call apply_dft_value(), setting param=10, opt="default", enum_var=MyEnum::DEFAULT;
[0165] Step 2: Parse the input parameters:
[0166] Match the -opt option, call the corresponding cmd_spec's save_data("hello"), and assign opt = "hello";
[0167] Matching the -enum option, calling the corresponding cmd_spec's save_data("type 1"), converting "type 1" to MyEnum m::TYPE 1 through enum_map, and assigning enum_var = MyEnum::TYPE 1;
[0168] The remaining position parameter 100 is assigned to param;
[0169] Step 3: Execute business logic:
[0170] Call `run()` and use `set_result()` to return the result; finally, the TCL input interface will output the characters "Result:100". This embodiment conveniently improves the language interface system's ability to easily interface and convert between C++ and TCL languages.
[0171] The third embodiment of the present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, the computer instructions being used to cause the computer to execute the above-described inter-language conversion method.
[0172] Please combine Figure 1 and Figure 4 The fourth embodiment of the present invention provides a computer device 2, which is applied to the above-mentioned interfacing and conversion method between different languages. It includes a memory 21, a processor 22, and a computer program 23 stored in the memory. The processor 22 executes the above-mentioned computer program to realize the interfacing and conversion method between different languages.
[0173] The foregoing has provided a detailed description of a method for interfacing and converting between different languages, a language interface system, a computer device, and a medium disclosed in the embodiments of the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the embodiments above are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention. Any modifications, equivalent substitutions, and improvements made within the principles of the present invention should be included within the protection scope of the present invention.< / t> < / t> < / t> < / t> < / int> < / int> < / int>
Claims
1. A method for interfacing and converting between different languages, used to convert C++ language to TCL language, characterized in that: The methods for interface conversion between different languages include the following steps: Set TCL language as the first language, and define different parts of the first language to divide the first language into three parts: first language commands, first language options and / or parameters, and first language return values; We define C++ as the second language and define different parts of the second language, dividing it into three parts: second language function procedures, second language variable assignments, and second language execution results. Establish the first mapping relationship from first language commands to second language functions; Establish a second mapping relationship from the assignment of first language options and / or parameters to second language variables; Establish a third mapping relationship from the results of the second language execution to the return value of the first language; The first, second, and third mapping relationships are input into the language interface system for compilation. The new TCL commands are input into the compiled language interface system to perform the interface conversion between TCL language and C++ language.
2. The method for interfacing and converting between different languages as described in claim 1, characterized in that: The first language command is the name of the TCL command, and the second language function procedure is the entire procedure of the function in the C++ language. After the first mapping relationship is established, the corresponding TCL command is entered, and the language interface system will automatically call the corresponding function in the C++ language.
3. The method for interfacing and converting between different languages as described in claim 1, characterized in that: Once the second mapping is established, options and / or parameters can be set to integers, floating-point numbers, or strings.
4. The method for interfacing and converting between different languages as described in claim 1, characterized in that: After establishing the second mapping relationship, it also includes: Add a fourth part to set the first language; the fourth part is the first language string. Add a fourth part to the second language settings; the fourth part is the second language enumeration. Establish a fourth mapping relationship from the first language string to the second language enumeration; Once the fourth mapping relationship is established, the options and / or parameters can be set to integers, floating-point numbers, strings, or enumerations.
5. The method for interfacing and converting between different languages as described in claim 1, characterized in that: The first mapping relationship, the second mapping relationship, the third mapping relationship, and the fourth mapping relationship are four different program constructions.
6. The method for interfacing and converting between different languages as described in claim 1, characterized in that: After completing the interface conversion between C++ and TCL, the following is also included: If the developer provides a new custom TCL command, the new custom TCL command is converted to C++ language analysis by the language interface system to obtain the C++ language execution result, and then the execution result is returned to TCL through the language interface, and the return value is obtained in the TCL language input interface.
7. A language interface system, applied to the interfacing and conversion method between different languages as described in any one of claims 1-6, characterized in that: The language interface system includes a display module and a processing module that are interconnected. The processing module includes a TCL command interpreter, a command registration module, a command management module, and a parameter parsing module that are connected in sequence. When developers create new custom TCL commands, that is, define the first, second, and third mapping relationships of the commands, the command registration module and the command management module work together to complete the compilation. When the processing module and the display module are compiled together, a TCL language input interface can be provided in the display module. When a TCL command is entered on the input interface, the parameter parsing module parses the entered TCL command and converts it into C++ language for execution.
8. The language interface system as described in claim 7, characterized in that: The command management module includes an execution command management module, a registration naming option module, a registration location parameter module, and a setting command result module. The execution command management module is connected to the command registration module and is used to establish a first mapping relationship. The registration naming option module and the registration location parameter module are used to establish a second and a fourth mapping relationship. The setting command result module is used to establish a third mapping relationship.
9. A computer-readable storage medium, wherein, The computer-readable storage medium stores computer instructions for causing the computer to execute the interface conversion method between different languages according to any one of claims 1 to 6.
10. A computer device, applied to the interface conversion method between different languages as described in any one of claims 1 to 6, characterized in that: It includes a memory, a processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the interface conversion method between the different languages.
