Programming language migration operation method and device
By importing built-in function libraries into the Python environment and converting M-language functions into Python functions, the problem of running M-language without relying on the Matlab core library is solved, achieving efficient migration from M-language to Python and improving flexibility and scalability.
Patent Information
- Application Number
- CN202511460657.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-14
- Publication Date
- 2025-11-14
- Estimated Expiration
- 2045-10-14
AI Technical Summary
Existing technologies struggle to effectively run M-language code without relying on the Matlab core library, and existing solutions are difficult to implement, have low maintainability, and lack flexibility and scalability.
By pre-importing the target built-in function library into the Python runtime environment, M-language functions are identified and converted into Python functions. By leveraging the syntax rules and dynamic typing features of Python functions, the functionality of M-language script code can be migrated.
It reduces the difficulty of implementation, improves flexibility and scalability, and enables the rapid and effective migration of the M language to the Python runtime environment, covering its functions. Furthermore, the Python module functions are open source and can be modified independently.
Smart Images

Figure CN120950074A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of programming language technology, and in particular to a programming language migration and execution method and apparatus. Background Technology
[0002] Currently, there is very little software in the industry that supports the M language (MATLAB, a programming language specific to the MATLAB platform). Most software that does support the M language utilizes MATLAB's core libraries to provide the M language runtime environment. If MATLAB's core libraries are unavailable, the functionality of code written in the M language will be unusable, resulting in significant limitations.
[0003] To mitigate these limitations, some solutions involve developers writing a dedicated interpreter for the M language to parse it. This approach is challenging, requiring not only syntax support for the M language but also an execution environment. Furthermore, the complex syntax of the M language lacks clear official documentation, resulting in inconsistent performance, low maintainability, and high coupling, often leading to additional problems during modifications.
[0004] Other solutions rely on third-party libraries to parse the M language and generate code in other programming languages, thus enabling the generated code to run on other programming language platforms. However, third-party libraries have limited applicability, flexibility, and scalability.
[0005] Therefore, there is an urgent need to propose a method that can effectively run the M language without relying on the Matlab core library. Summary of the Invention
[0006] Therefore, it is necessary to provide a programming language migration and execution method, apparatus, computer equipment, computer-readable storage medium, and computer program product that can effectively run the M language without relying on the core library of Matlab, in order to address the above-mentioned technical problems.
[0007] Firstly, this application provides a method for migrating and running a programming language. The method includes: Initialize the Python runtime environment; the target built-in function library is pre-imported into the Python runtime environment; the target built-in function library includes built-in functions that come with the Python language, as well as custom built-in functions with the same names written for built-in functions used by the M language; During the conversion of the M language file to be converted in the current directory, at least one M language function is identified from the M language script code included in the M language file, and each M language function is converted into the corresponding Python function according to the function syntax rules of Python. For script code other than M-language functions in M-language script code, based on a pre-established mapping table between preset M-language syntax forms and Python functions, the code written in the preset M-language syntax form in the script code is converted into the corresponding Python function. Among them, the same M-language syntax form has different functions or represents the processing between different data types. The corresponding converted Python function performs different functions according to the internal context of the function or performs type checking inside the function and performs different processing logic for different types. Based on the converted Python function, a Python file is generated from the M language file, and the Python file is run in the Python runtime environment to achieve the functionality of the M language script code.
[0008] Secondly, this application also provides a programming language migration and execution apparatus. The apparatus includes: The environment initialization module is used to initialize the Python runtime environment. The target built-in function library is pre-imported into the Python runtime environment. The target built-in function library includes built-in functions that come with the Python language, as well as custom built-in functions with the same names that are written for built-in functions used by the M language. The code conversion module is used to identify at least one M language function from the M language script code included in the M language file during the conversion process of the M language file to be converted in the current directory, and convert each M language function into the corresponding Python function according to the function syntax rules of Python; The code conversion module is also used to convert the script code written in the preset M language syntax form into the corresponding Python function based on a pre-established mapping table between preset M language syntax forms and Python functions. The same M language syntax form has different functions or represents the processing between different data types. The corresponding converted Python function performs different functions according to the internal context of the function or performs type checking inside the function and performs different processing logic for different types. The runtime module is used to generate a Python file from the converted M-language file based on the converted Python function, and then run the Python file in the Python runtime environment to achieve the functionality of the M-language script code.
[0009] Thirdly, this application also provides a computer device. The computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the various steps involved in the first aspect and its embodiments described above.
[0010] Fourthly, this application also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program thereon, which, when executed by a processor, implements the various steps involved in the first aspect and its embodiments described above.
[0011] Fifthly, this application also provides a computer program product. The computer program product includes a computer program that, when executed by a processor, implements the various steps involved in the first aspect and its embodiments described above.
[0012] The aforementioned programming language migration and execution methods, devices, computer equipment, storage media, and computer program products identify M-language functions from M-language script code and convert them into Python functions. Furthermore, for code with complex M-language syntax, a unified mapping and conversion to Python functions can be achieved. Through the internal context and other features of Python functions, different functionalities or different processing logics can be executed for different types. This greatly reduces the implementation difficulty, enabling rapid implementation of complex functions. Moreover, Python modules are open-source and can be modified independently; for example, custom built-in functions used in M-language can be written in Python with the same names, greatly enhancing flexibility and scalability. In this way, the functionality of M-language can be comprehensively covered, generating and running Python files, allowing for rapid and efficient migration of M-language code to the Python runtime environment. Attached Figure Description
[0013] Figure 1 This is a flowchart illustrating a programming language migration execution method in one embodiment; Figure 2 This is a structural block diagram of a programming language migration execution device in one embodiment; Figure 3 This is a structural block diagram of a programming language migration execution device in another embodiment; Figure 4 This is a structural block diagram of the programming language migration execution device in yet another embodiment; Figure 5 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0014] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0015] In one embodiment, such as Figure 1As shown, a method for migrating and running a programming language is provided. Taking the application of this method to a computer device as an example, the computer device can be a server or a terminal. This method can be executed by the terminal or the server, or it can be implemented through interaction between the terminal and the server. The method includes the following steps: S11 initializes the Python runtime environment; the target built-in function library is pre-imported into the Python runtime environment; the target built-in function library includes built-in functions required by the Python language, as well as custom built-in functions with the same names written for built-in functions used by the M language.
[0016] Python is an object-oriented, open-source programming language characterized by its simple syntax and ease of coding. Like M-language, it is also dynamically typed, simplifying its translated form. Furthermore, Python boasts numerous module libraries covering scientific computing, data processing, and visualization, perfectly encompassing most of the functionalities of M-language. Python's object attributes can be freely modified, offering exceptional flexibility, which facilitates the implementation of specialized M-language features.
[0017] Therefore, using Python as the execution environment for the M language in this application helps reduce development difficulty and improve scalability. Furthermore, it proposes a specific and detailed implementation scheme for migrating the M language to the Python runtime environment. This scheme is not easily conceived directly, because the specific method for converting the M language to Python determines the effectiveness of the migration, as well as the efficiency and cost of implementing the migration. The specific scheme in this application is the result of creative effort.
[0018] Specifically, for the Python runtime environment, this application uses the RERL interactive Python environment. During the initialization phase, the built-in functions required by the Python language and custom-written Python built-in functions with the same names for the built-in functions used in the M language are imported, i.e., the target built-in function library. Based on Python's language features, it is very convenient and simple to write Python built-in functions with the same names.
[0019] S12: During the conversion process of the M language file to be converted in the current directory, at least one M language function is identified from the M language script code included in the M language file, and each M language function is converted into the corresponding Python function according to the function syntax rules of Python.
[0020] All M-language scripts in the current directory need to be converted to Python code. Since there may be multiple M-language files in the current target directory, steps S12 to S14 can be performed on each M-language file individually to obtain the corresponding Python file for each M-language file.
[0021] It should be understood that in some cases in the M language, the distinction between variables and functions is not clear. For example, the undefined variable A(1,2) and the function A(1,2) have the same form in the M language script code, but in the Python code converted in this application, the forms of variables and functions are different. For example, the variable A(1,2) is converted to the code arraygetvalue('A',0,'(',1,2,None), while the function A(1,2) is converted to the code A(1,2). Therefore, it is essential to distinguish between variables and functions.
[0022] Specifically, during the conversion process, M-language functions are identified from the M-language script code included in the M-language file. There can be one or more M-language functions. Each M-language function is then converted into its corresponding Python function form according to Python's function syntax rules.
[0023] During the conversion process, variables in the M-language script code can also be identified, and corresponding conversions can be performed on the code related to these variables. For example, based on the above example, code related to variables can be converted into function-like code. Details on how to distinguish between functions and variables, and the specific processing of variable conversion, are described below and will not be discussed here.
[0024] As in step S11, the built-in functions have been pre-written to generate a built-in function library, and the M-language functions identified from the M-language script code are user-defined functions. Because the location of a function definition in an M-language file can be arbitrary, but in Python, the function definition must precede the call, after conversion to the corresponding Python function, the Python function code is placed above the code block to implement the function definition. For example, the handling of the function definition location for user-defined functions is as follows: Specifically, when converting each M-language function (i.e., a user-defined function) into a Python function, a function code stack is maintained using Python code, storing the code of the progressively converted Python function within the stack. After the function conversion is complete, the converted Python function code is retrieved from the stack and placed before the top code segment of the stack. This ensures the definition of the converted Python function, guaranteeing that the function definition precedes the function call code, thus ensuring the compliance of Python syntax.
[0025] In some embodiments, nested functions are frequently used in user-defined functions in the M language and are a common type of user-defined function. Nested functions are multi-level, and inner functions can use variables from outer functions. For example, if a function FuncA defines another function FuncB inside it, FuncB can directly use the variables that exist in FuncA.
[0026] This application, through research, has found the following differences between nested functions in the M language and the Python language: (1) In M language, inner functions can directly use variables of outer functions. In Python, nonlocal must be added to declare variables inside the outer function. (2) In Python, the outer function must call the inner function after the inner function is defined, while in M language this is not required.
[0027] To address the aforementioned differences, this application proposes a solution. Specifically, when converting nested functions in M language to nested functions in Python, a global scope stack is set up in Python. Each function within a Python nested function has a corresponding scope stack frame in the global scope stack, which stores variables within the scope hierarchy of the function. A hierarchy identifier is added to the function call format to indicate the scope hierarchy.
[0028] In Python nested functions, when each function is called, the corresponding scope stack frame is determined from the global scope stack based on the function's scope level identifier. The corresponding variable is then searched for within that scope stack frame. If the variable is not found, the search continues upwards from the bottom of the stack based on the level identifier. This level identifier can be an integer parameter. For example, level identifier 0 indicates the current scope is the top level; level identifier 1 indicates the current scope is nested one level, and if the variable is not found in the current scope, the search can continue upwards to the bottom of the stack; level identifier 2 indicates the search can continue two levels upwards from the bottom of the stack; and so on.
[0029] In other words, when the final Python file is converted and run, if any function within a nested Python function is called, it can access the variables of the outer function based on the function's scope hierarchy and the global scope stack. Thus, without adding a `nonlocal` declaration, a completely new way can be used to allow inner functions of the converted Python nested function to access the variables of the outer function.
[0030] Each memory function in a nested function is a user-defined function. Regarding the difference in point (2) above, the function definition location handling method for user-defined functions described above can be adopted. By maintaining the function code stack, the memory function is defined before being called by the outer function. This will not be elaborated further here. In some embodiments, the identified M-language functions may include functions that support multiple return values (for ease of description, these are referred to as target functions). The related processing of target functions will not be discussed here but will be described in detail below.
[0031] S13, for script code other than M language functions in M language script code, based on a pre-established mapping table of preset M language syntax forms and Python functions, the code written in the preset M language syntax form in the script code is converted into the corresponding Python function; wherein, the same M language syntax form has different functions or represents the processing between different data types, and the corresponding converted Python function performs different functions according to the internal context of the function or performs type checking inside the function and performs different processing logic for different types.
[0032] This application, through research, discovers that a syntax form in the M language can have different functions. For example, an assignment operator = can perform assignment (e.g., A(1,2) = 1), dimension expansion (e.g., A(2,2) = 1), and dimension deletion (e.g., A(2,:) = []). Similarly, the syntax form of square brackets [] can represent array construction (e.g., [1,2;1,2]), horizontally concatenated arrays (e.g., [A,A]), and vertically concatenated arrays (e.g., [A;A]).
[0033] Furthermore, the same M-language syntax can represent the processing of different data types. For example, a+b in MATLAB can represent the addition of two scalars, two vectors, two matrices, a scalar and a matrix, and even the addition of strings.
[0034] Clearly, the syntax of the M language is complex. This complex syntax exists in the script code outside of M language functions. To address this, this embodiment pre-establishes a mapping table between preset M language syntax forms and Python functions, as shown in Table 1. It should be understood that Table 1 only illustrates a partial mapping, and the values in the M language syntax forms and Python forms in Table 1 are for illustrative purposes only. The function names under the Python form columns are also for simple illustration only and can be other names; no limitation is placed on this.
[0035] Table 1
[0036] Based on this mapping table, at least a portion of the script code, excluding M-language functions, can be converted into corresponding Python functions. Specifically, if the script code, excluding M-language functions, contains code written using the preset M-language syntax form in the mapping table, it can be converted into the corresponding Python function in the mapping table. For example, if the script code, excluding M-language functions, contains code using the syntax form a(1,2,...), it can be converted into the Python function form arraygetvalue('a',0,'(',1,2,...None). For instance, the script code, excluding M-language functions, may include code representing variables; based on the mapping table, the code segment representing variables written using the preset M-language syntax form is converted into the corresponding Python function.
[0037] In this way, the same M-language syntax is translated into a unified Python function. As long as the form is consistent, the translated Python function name will be the same. The specific functionality is determined by the Python function itself, which executes different code based on the context. This achieves decoupling between translation and execution; modifying the internal functionality of the function changes the functionality itself, increasing flexibility.
[0038] Translating M-language syntax into Python functions not only enables different functionalities but also allows for data type differentiation within Python functions, enabling targeted data processing for different data types. For example, "a+b" in M-language can be converted into the corresponding Python function sum(a,b). Leveraging Python's dynamically typed nature, the sum function can perform data type checking internally and then execute different processing logic based on the checked data type.
[0039] S14 generates a Python file converted from the M language file based on the converted Python function, and runs the Python file in the Python runtime environment to achieve the functionality of the M language script code.
[0040] For example, the modified Python file should include at least the following code sections: (1) The first part of the code includes: The beginning of the Python code needs to contain "from __main__ import*" and code for importing the (converted) Python functions in the current directory.
[0041] Among them, `from __main__ import*` is a module import statement used to import all public variables, functions, classes, and other objects from the current program's main module (the `__main__` module).
[0042] It should be understood that all M-language files in the current directory will be converted to Python files. Each Python file needs to use functions including custom functions and functions defined in other files in the current directory. Therefore, you can use the converted Python functions in other Python files through the code used to import Python functions in the current directory.
[0043] (2) The second part of the code includes the definition of custom functions in the current converted Python file.
[0044] As shown above, the functions required in the current Python file include custom Python functions. These functions need to be defined before they can be called. The second part of the code is the code for defining the custom functions.
[0045] It should be understood that the custom Python functions in the current Python file and the Python functions in other Python files in the current directory that need to be imported are all obtained by the methods in the various embodiments of this application. For example, they include Python functions obtained by converting M language functions that originally exist in the M language file, and Python functions obtained by converting non-function M language syntax forms.
[0046] (3) The third part of the code: the main code of the converted script.
[0047] It should be understood that in addition to functions, Python files also contain other script body code, which is the third part of the code mentioned above.
[0048] (4) The fourth part of the code: the code used to update the variables in the current scope dictionary to the main module (__main__ module) to ensure that they are available next time.
[0049] The above method identifies M-language functions from M-language script code and converts them into Python functions. Furthermore, for code with complex M-language syntax, it can be uniformly mapped and converted into Python functions. Through the context and other features within Python functions, different functionalities or different processing logics can be implemented for different types. This greatly reduces the implementation difficulty and enables the rapid implementation of complex functions. Moreover, Python modules are open-source and can be modified independently. For example, custom built-in functions used in M-language can be written in Python with the same names, greatly enhancing flexibility and extensibility. In this way, the functionality of M-language can be comprehensively covered, and Python files can be generated and run, allowing for quick and efficient migration of M-language code to the Python runtime environment.
[0050] Furthermore, Python's free-coding feature of functions enables the decoupling of translation and conversion from function functionality.
[0051] Moreover, compared to converting M language into other scientific computing languages, such as Julia, the solution proposed in this application offers better scalability and flexibility. This is because Julia suffers from poor scalability and flexibility, and cannot support many of the original features of M language, especially those related to global variables and persistent variables.
[0052] Similarly, compared to relying on third-party libraries to parse the M language, the solution proposed in this application offers greater flexibility and scalability. This is because if a third-party library (such as ANTLR4) is unsuitable, the solution cannot handle or run, resulting in limited flexibility. Furthermore, it relies solely on ANTLR4 updates and cannot fully support the functionality of the M language, further limiting its scalability. The solution in this application leverages Python's simple coding style and numerous module libraries to add specific functionalities, offering exceptional flexibility and scalability, and fully and quickly covering the original functionality of the M language.
[0053] As mentioned earlier, to accurately convert M-language script code into Python code, it is essential to distinguish between functions and variables in the M-language script code. The following will explain how to differentiate between functions and variables.
[0054] In some embodiments, the target built-in function library pre-imported in the Python runtime environment corresponds to the built-in function library of the M language. Therefore, the built-in function names in the target built-in function library are the same as and correspond to the built-in function names of the M language's built-in functions. Thus, before converting the M language file, the built-in function names in the target built-in function library can be obtained, that is, all the built-in function names of the M language's built-in functions can be obtained.
[0055] The target built-in function library includes custom built-in function libraries (including custom Python built-in functions with the same names written for built-in functions used in the M language) and Python's built-in function libraries (i.e., the builtins library, which includes built-in functions that come with the Python language). Built-in function names can be obtained from custom built-in function libraries through the main module (the __main__ module), and also from the builtins library.
[0056] Furthermore, it can retrieve the names of custom functions in the current directory and the custom function names in the M-language file to be converted (for example, searching for custom function names in function scripts in the set search directory and the current directory according to the M-language search rules). Further, it generates a function name list based on the retrieved built-in and custom function names. This function name list comprehensively covers both system-built-in functions and user-defined functions. This serves as a crucial basis for subsequent identification of M-language functions, accurately distinguishing between functions and variables, thereby improving conversion accuracy. It should be understood that if the current directory changes, the function name list needs to be updated accordingly.
[0057] In addition, variable lists and function parameter lists are pre-created. These lists are continuously updated as the conversion process progresses. For example, the identification and conversion of M-language functions can be processed in parallel, and the function parameter lists can be updated based on the function parameters of the identified M-language functions.
[0058] In this embodiment, the specific process of identifying at least one M language function from the M language script code included in the M language file during the conversion of the M language file currently to be converted in the current directory in step S12 is as follows: M-language files are converted sequentially. During the conversion process, if an identifier (which represents a name in the code) is detected, it will continue to determine whether the character following the identifier (the next adjacent character) is a preset character. If not, (1) is executed; if so, (2) to (6) are executed. Among them, the preset character is a characteristic character that reflects the identity of variables and functions and has information content. For example, in M-language, both variable A(1,2) and function A(1,2) have a left parenthesis after the identifier. Therefore, the preset character can be the left parenthesis '('.
[0059] (1) If an identifier is detected and the identifier is not followed by a preset character or a special character, then the identifier is recognized as a variable name.
[0060] Specifically, step (1) includes: if an identifier is detected and the identifier is not followed by a preset character, then determine whether the identifier exists in the current variable list or function parameter list. If it exists, the identifier is recognized as a variable name. If it does not exist, then determine whether the identifier is a special character (e.g., the imaginary unit 'i' or 'j'). If it is, the identifier is recognized as a special character. If it is not a special character, the identifier is recognized as a variable name.
[0061] It should be understood that if an identifier is recognized as a variable name, then the code including the identifier is the code about the variable, and the code about the variable can be converted into the corresponding Python function.
[0062] (2) If an identifier is detected and the identifier is followed by a preset character, determine whether the identifier is in the current variable list or function parameter list. If yes, execute (3); otherwise, execute (4).
[0063] (3) If the identifier is in the current variable list or function parameter list, the identifier will be recognized as a variable name.
[0064] (4) If the identifier is not in the current variable list or function parameter list, continue to check if the identifier exists in the function name list. If it exists, execute (5); if it does not exist, execute (6).
[0065] (5) If the identifier exists in the function name list, it is identified as the function name of the M language function.
[0066] It should be understood that after identifying the M-language function, the function parameters inside the M-language function can be added to the function parameter list. This allows the function parameter list to be updated during the conversion process, facilitating subsequent differentiation between variables and functions. Since parameters within a function are essentially variables themselves, the function parameters inside the function can be considered and added to the function parameter list when differentiating between variables and functions.
[0067] (6) If the identifier does not exist in the function name list, the identifier will be recognized as a variable name.
[0068] For example, during the conversion process, the variable name to be assigned can be added to the variable list as a variable to update the variable list, making it easier to distinguish and identify variables and functions in the future.
[0069] In some embodiments, the script code, in addition to M-language functions, includes code about variables; variables include global variables and persistent variables.
[0070] (i) Code involving global variables can be converted into corresponding Python global variable functions. For example, the statement `global A` is converted into the function `arraysetglobal('A')`, and the statement to delete the global variable `clear global A` is converted into the function `arrayclearglobal('A')`.
[0071] This application's research has revealed the following differences between M and Python regarding the storage of global variables: (1) M language has a dedicated global variable storage area; Python's global variables belong to a single module, and other modules must import the module before they can use them; (2) In M language, the lifetime of global variables is the lifetime of Matlab process; in Python, the lifetime of global variables follows the lifetime of module. If a module is unloaded, the lifetime of global variables inside that module ends. (3) Global variables in M language can be created anywhere that uses the global declaration, while in Python they can only be defined in modules and not in functions.
[0072] Therefore, statements concerning global variables, besides being formally translated into Python global variable functions, utilize a global dictionary and a global variable name list stack within the main module of Python to achieve the functionality of global variables. The global dictionary serves as the global storage area, while the global variable name list stack stores the names of global variables declared in the current scope. Setting up the global dictionary and global variable name list stack within the main module eliminates the need for separate module imports. Furthermore, storing global variable names within the module and the global variable name list stack, rather than defining them within functions, satisfies Python's syntax compliance. Since the main module (the `__main__` module) exists along with Python's RERL mode, it exists as long as the Python process exists, thus resolving the lifecycle issue.
[0073] It should be understood that when a Python global variable function is called, global variable-related processing is performed based on the global dictionary and the stack of global variable name lists.
[0074] For example, the `arraysetglobal('A')` function adds the global variable 'A' to the global dictionary `globalDict` if 'A' does not exist there, otherwise it does nothing; then it writes 'A' to the `globalnames` list (the list of global variable names) on the current stack. The `arrayclearglobal('A')` function removes the global variable 'A' from the `globalnames` list on the current stack and also removes 'A' from the global dictionary `globalDict`. In other words, if the Python global variable function `arraysetglobal('A')` is called, it adds and stores global variables based on the global dictionary and the stack of global variable names. If the function `arrayclearglobal('A')` is called, it removes global variables based on the global dictionary and the stack of global variable names.
[0075] (ii) The code for persistent variables can be converted into the corresponding Python persistent variable function.
[0076] In the M language, persistent variables are local variables of the function in which they are declared. Python does not have the concept of persistent variables. In this embodiment, the following method is used to implement the functionality of persistent variables in Python.
[0077] Code declaring persistent variables in M-language functions can be converted into corresponding Python persistent variable functions. For example, the persistent variable `persistent A` in the function `FuncA` is converted into the Python function `arraysetpersistents(FuncA, 'A')`, which is a Python persistent variable function. Besides the formal conversion to a Python persistent variable function, the Python persistent variable function internally sets up a persistent variable dictionary; the persistent variable dictionary is used to store the persistent variables in the Python persistent variable function.
[0078] The function `arraysetpersistents(func, 'A')` stores persistent variables from Python functions in a persistent variable dictionary. If the dictionary `__persistent__` is not present in the function `func`'s attributes, it adds it. Then, it checks if `A` exists in the persistent variable dictionary; if not, it adds `A` as the key to the persistent variable dictionary, with the default value being an empty list. Thus, it stores persistent variables from Python functions in the persistent variable dictionary, achieving the functionality of persistent variables.
[0079] In some embodiments, this application finds that assignment in the M language has multiple forms, such as a = 1, ab = 1, a(1) = 1, a(1,1) = 1, a{1} = 1, a{1,1} = 1, a(1,:) = [1,2], a(1,1:2) = [1,2], etc. These forms can also be superimposed on each other, making the forms particularly complex. To reduce the complexity of translation into Python and increase flexibility, this embodiment uses the default expression feature of functions to unify assignment. Specifically, the preset M language syntax forms in the above correspondence table (as shown in Table 1) include the syntax forms representing assignment. The syntax forms representing assignment can be converted into corresponding Python assignment functions based on the correspondence table. For example, the specific form of the Python assignment function is: arraysetvalue(varName,level,*args).
[0080] As can be seen from the above function, the multiple parameters corresponding to the Python assignment function are denoted as the first initial parameter group, which includes the variable name, the scope level of the variable, and the default parameters.
[0081] Variable name varName: Represents the name string of the variable, such as 'A'.
[0082] The scope level: indicates the function scope level in which the current variable is located. For example, level = 0 means it is not in a function, level = 1 means it is in a function at the first level, and level = 2 means it is in a nested function at the second level.
[0083] Default parameter args: Represents the default parameters used to perform the assignment.
[0084] For example, the default parameter args has the following format: args=[type,dim1,dim2,...,None]* value []: indicates that the content within [] may or may not be present. *: Indicates that it can be repeated 0 or more times. type: Indicates the type identifier, such as '(' for array index reading, '{' for cell array index reading value, '.' for struct attribute reading, etc.; dimN: can be a number representing the dimension (such as the index dimension), an identifier string representing the attribute (i.e., the attribute name), or a colon (':') indicating a special meaning for the index. The dim1 and dim2 in the above format are examples illustrating dimN.
[0085] None: The parameter end marker indicates the end of the parameters at this level; value: Represents the data to be assigned.
[0086] It should be understood that the Python assignment function, based on the above data format conversion, can implement a variety of complex assignment forms in the M language through a single function.
[0087] Specifically, during the execution of a Python file, if the Python assignment function is called, assignment can be achieved through the following steps: (1-1) Based on the scope hierarchy of the variables, determine the first scope dictionary corresponding to the variable names in the first initial parameter group. For example, dictionaries of scopes such as global scope, persistent variable scope, and nested function scope, to determine the assignment range.
[0088] (1-2) The parameters with vectors in the default parameters of the first initial parameter group are split into multiple groups of parameters without vectors to obtain the first parameter group.
[0089] For example, the parameter ('a',0,'(',[1,2,3],2,None,a) becomes ('a',0,'(',1,2,None,a), ('a',0,'(',2,2,None,a), and ('a',0,'(',3,2,None,a)) after preprocessing. In addition, there are some other processing such as assigning empty values.
[0090] (1-3) Perform secondary processing on the parameters in the first parameter group to obtain a parameter group that does not include the assigned data.
[0091] The main task is to process the vector parameters and some logical value parameters left over from the preprocessing to obtain a parameter set that does not contain assigned data.
[0092] (1-4) Match and combine the parameter group without assignment data and the assignment data to generate the target parameter group.
[0093] It should be understood that if the assigned data is a vector or matrix, the corresponding elements of the vector and matrix need to be extracted and matched with the parameter sets without assigned data to form different target parameter sets. For example, if the parameter sets without assigned data are ('a',0,'(',1,2,None) and ('a',0,'(',2,2,None), and the assigned data is [1,2], then the 1 and 2 in the assigned data can be matched and combined with the parameter sets without assigned data to generate the final target parameter sets as ('a',0,'(',1,2,None,1) and ('a',0,'(',2,2,None,2).
[0094] (1-5) Assign values based on the target parameter set and update the first scope dictionary with the assignment results.
[0095] When there are multiple target parameters, assignments are made based on the values of each target parameter. Updating the assignment results to the first scope dictionary means binding and mapping variable names with assignment results in the first scope dictionary, so that the assignment results can be accessed when the variable name is called.
[0096] In some embodiments, the default parameters in the target parameter group include a type identifier parameter. The assignment processing steps (1-5) described above, which assign values based on the target parameter group, include: (1-5.1) Obtain the initial reference corresponding to the variable name in the target parameter group; the initial reference indicates the variable value corresponding to the variable name.
[0097] Specifically, obtain a reference to the variable value. If the variable value does not exist, generate an empty variable value and obtain its reference. This initial reference is denoted as reference A.
[0098] (1-5.2) Perform parameter processing on the target parameter group in a loop. If the type identifier parameter (i.e., type) indicates that the structure property is read, then the dictionary property is read based on the remaining parameters in the default parameters, and the initial reference is updated based on the read property.
[0099] Specifically, if the type identifier parameter indicates the reading of a structure attribute (e.g., '.'), in step (1-5.2), the remaining dimN parameter in the default parameters is a string representing the attribute name. The attribute is read from the dictionary based on this attribute name. If the attribute name exists in the variable pointed to by reference A, the attribute value read based on the attribute name is updated to reference A; that is, reference A is set to point to the attribute value corresponding to the attribute name. If the attribute name does not exist in the variable pointed to by reference A, the attribute corresponding to that attribute name is created, its value is assigned to null, and then its reference is updated to reference A.
[0100] (1-5.3) If the type identifier parameter indicates dimension deletion '[]', then the variable value indicated by the initial reference is processed by dimension deletion based on the remaining parameters in the default parameters in order to update the initial reference.
[0101] In this case, the remaining parameters in the default parameters are the indicator parameters representing dimension deletion. Based on the indicator parameters representing dimension deletion, it is determined whether an entire column is being deleted; if so, the entire column is deleted. Alternatively, it is determined whether an entire row is being deleted; if so, the entire row is deleted. Or, if it is determined whether a single element of the matrix is being deleted, the remaining elements are combined into a vector. This achieves dimension deletion processing. The data and structure of the variable values after dimension deletion have changed, so the initial reference A is updated to point to the data after dimension deletion. When the variable is accessed, the data actually retrieved based on this updated reference is the data after dimension deletion.
[0102] If a variable is deleted, the variable will be assigned an empty value, meaning the entire variable will no longer exist.
[0103] (1-5.4) If the type identifier parameter indicates an array index read (e.g., '(')) or a cell array index read (e.g., '{'), then the index read is performed based on the remaining parameters in the default parameter to read the element at the index; the initial reference is updated based on the element at the index or the cell value within the element at the index.
[0104] Specifically, in this case, the remaining parameter `dimN` in the default parameters can be a number representing the index dimension. `dimN` can be one or more, meaning there can be one or more indices. If it's a single index, and the index exceeds the number of elements, the vector is padded with zeros; otherwise, no action is taken. For example, suppose the initial reference `A` points to a variable with 5 elements, but the index indicates the 6th element. This exceeds the number of elements the variable can have, so the vector is padded with zeros to expand it to a vector with 6 elements.
[0105] If there are multiple indices, a dimension check is performed. If the index dimension exceeds the dimension of the variable value pointed to by the initial reference A, multi-dimensional expansion is performed, padding the expanded elements with zeros. If the dimension does not exceed the dimension, no action is taken. For example, if the initial reference A points to a variable value that is a 2x2 matrix, but there are two indices, dim2 and dim3, then the variable value needs to be expanded to a 2x3 matrix by padding with zeros to locate the element at the index.
[0106] After retrieving the element at the specified index, if the type identifier parameter indicates array index reading ('('), then the initial reference is updated based on the element at the index, making the updated reference point to the element at that index. If the type identifier parameter indicates cell array index reading ('{'), then the initial reference is updated based on the cell value within the element at the index, making the updated reference point to the cell value within the element at that index.
[0107] (1-5.5) Based on the assigned data in the target parameter group, perform the assignment processing on the updated reference.
[0108] The updated reference is used to point to the location where the final value needs to be assigned. Therefore, the updated reference can be assigned a value based on the assignment data in the target parameter group.
[0109] (1-5.6) If the default parameter in the target parameter group does not have a type identifier, it means that there is no nesting level. In this case, the initial reference can be directly assigned based on the assignment data in the target parameter group.
[0110] In some embodiments, this application has found that value retrieval in the M language also exists in multiple forms. Compared to the various assignment forms exemplified above, the value retrieval forms simply omit the equals sign and the data on the right-hand side, and are obviously still quite complex. In this embodiment, the default expression feature of functions is also used to unify the value retrieval. Specifically, the preset M language syntax forms in the above correspondence table (as shown in Table 1) include syntax forms representing value retrieval. Based on the correspondence table, the syntax forms representing value retrieval are converted into corresponding Python value retrieval functions. For example, the specific form of the Python value retrieval function is: arraygetvalue(varName,level,*args).
[0111] As seen from the functions above, Python's value-determining functions also correspond to multiple parameters, denoted as the second initial parameter group. This includes the variable name, the variable's scope level, and default parameters. For a detailed explanation of each parameter, please refer to the introduction of assignment functions; it will not be repeated here. It should be noted that, compared to the first initial parameter group of Python's assignment functions, the default parameter `args` in the second initial parameter group does not have a `value` parameter. This is because the `value` parameter only exists in the scenario where assignment is required, serving as the assigned data.
[0112] If a Python fetch function is called during the execution of a Python file, the value can be retrieved using the following steps: (2-1) Based on the scope level, determine the second scope dictionary corresponding to the variable names in the second initial parameter group.
[0113] The principle is explained in step (1-1) of the assignment description section, and will not be repeated here.
[0114] (2-2) The parameters with vectors in the default parameters of the second initial parameter group are split into multiple groups of parameters without vectors to obtain the second parameter group.
[0115] (2-3) Determine the dimension of the result value based on the parameters in the second parameter group, and pre-allocate the corresponding null value based on the dimension of the result value.
[0116] This is equivalent to allocating memory space as an empty value before retrieving the value, and then directly storing the value into the corresponding memory space after retrieval.
[0117] (2-4) Perform value retrieval based on the type identifier and scope dictionary in the second parameter group, and update the pre-allocated corresponding null value based on the value retrieval result.
[0118] Specifically, the initial reference to the variable value is obtained, denoted as reference 'a'. The parameters are processed in a loop. If the type identifier is '.', the dictionary's properties are read. If the read property exists in the variable value pointed to by reference 'a', reference 'a' is updated based on the read property value. The value is then retrieved from the scope dictionary based on the updated reference, and the pre-allocated null value is updated based on the retrieved value to store the result in the pre-allocated memory space. If the property does not exist in the variable value pointed to by reference 'a', the value cannot be retrieved, and the process exits with an error.
[0119] If the type is identified as '(' or '{', then the value is read by index. If the index is out of bounds, an error is raised and the process exits. If the index is not out of bounds, then the reference 'a' is updated based on the value at the index. If the loop ends, the value pointed to by the updated reference is retrieved and returned, which is the result.
[0120] In some embodiments, M-language functions include target M-language functions with multiple return values. Furthermore, the number of return values for the same target M-language function can be different. For example, `[a,b] = FuncA()` and `[a,b,c] = FuncA()` are both supported in M-language. However, Python does not support such multi-return-value functions, nor does it support the functionality of returning different numbers of return values based on different return values. This application, through research, proposes a strategy for dynamically modifying Python function data, enabling the conversion of target M-language functions with multiple return values into Python functions with this functionality.
[0121] Specifically, during the conversion of the target Python function, a first attribute `mulOut` and a second attribute `mulNum` are added to the target Python function. Values for these attributes are assigned based on the return value of the target Python function. The first attribute `mulOut` indicates multiple return values, and the second attribute `mulNum` indicates the number of return values. For example, in the M-language file `[a,b] = funcA()`, after conversion, `funcA.mulOut=True; funcA.outNum=2`. That is, by adding the first and second attributes to the converted function, multiple return values can be dynamically obtained, and the number of return values can be dynamically determined.
[0122] In some cases, it may not be necessary to use the entire return value of a function; only a portion of the return value is needed. The function return value refers to the entire value returned by the function. In such cases, the function return value can be stored in a temporary variable, and the temporary variable can be assigned a value to ignore the unused return value and only use the required return value.
[0123] For example: In the M language, [a,~,b]=funcA() means that the function funcA() will return 3 return values, but only 2 of them need to be used, namely "a" and "b". The middle return value does not need to be used, so it is represented by "~". Then, using the method in this embodiment, after conversion, we get funcA.mulOut=True; funcA.outNum=3; _1 =funcA(); a=_1[0]; b=_1[2] ; where _1 represents a temporary variable, which is equivalent to storing the 3 return values of funcA() in the temporary variable _1 after conversion. Then, the temporary variable is assigned a value, that is, "a=_1[0]; b=_1[2]" means that among the 3 return values stored in the temporary variable, the 0th and 2nd return values are used, and the middle 1st return value is not used, so as to ignore the return value that does not need to be used.
[0124] In this embodiment, the method of adding first and second attributes and temporary variables to functions is only applicable to the conversion processing of target M language functions that support multiple return values. For M language functions that do not support multiple return values, it is not necessary to add these attributes and temporary variables, and it is only necessary to convert them according to the regular Python function syntax rules.
[0125] The methods in the various embodiments of this application allow users to quickly transfer their previous work in Matlab (i.e., M-language script code previously developed based on Matlab) to an open-source Python environment that does not rely on Matlab. Using the methods in this application, a small number of general or unified functions can be quickly implemented in Python to rapidly cover the use cases of the M-language, such as support for common data types (scalars, vectors, matrices, strings, multidimensional arrays, structures, cell arrays, etc.), matrix expressions, scientific computing, matrix operations, visualization functions, user-defined functions, mutual calls between function files, support for nested function scope, global variables, persistent variables, etc. Thus, based on the basic idea of this application, by flexibly utilizing code conversion and the dynamic attributes of Python objects, all M-language scenarios can be quickly and normally run in the Python environment. By combining the methods in the embodiments of this application with model file parsing, the direct conversion and execution of Matlab platform model files in power simulation modeling software (which includes a Python environment) has been successfully achieved, while maintaining consistency with Matlab simulation results.
[0126] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0127] Based on the same inventive concept, this application also provides a programming language migration and execution apparatus for implementing the programming language migration and execution method described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more programming language migration and execution apparatus embodiments provided below can be found in the limitations of the programming language migration and execution method described above, and will not be repeated here.
[0128] In one embodiment, such as Figure 2 As shown, a programming language migration and execution device is provided, the device comprising: The environment initialization module 202 is used to initialize the Python runtime environment. The target built-in function library is pre-imported into the Python runtime environment. The target built-in function library includes built-in functions that come with the Python language, as well as custom built-in functions with the same names that are written for built-in functions used by the M language. The code conversion module 204 is used to identify at least one M language function from the M language script code included in the M language file during the conversion process of the M language file to be converted in the current directory, and convert each M language function into the corresponding Python function according to the function syntax rules of Python. The code conversion module 204 is also used to convert the script code written in the preset M language syntax form into the corresponding Python function based on a pre-established correspondence table between preset M language syntax forms and Python functions. The same M language syntax form has different functions or represents the processing between different data types. The corresponding converted Python function performs different functions according to the internal context of the function or performs type checking inside the function and performs different processing logic for different types. Module 206 is used to generate a Python file converted from the M language file based on the converted Python function, and to run the Python file in the Python runtime environment to achieve the functionality of the M language script code.
[0129] In some embodiments, the M-language function is a user-defined function. The code conversion module 204 is further configured to maintain a function code stack using Python code when converting each M-language function to a Python function, storing the converted Python function code in the function code stack; after the function conversion is completed, the converted Python function code is retrieved from the function code stack and placed in front of the top code segment of the function code stack to define the converted Python function.
[0130] In some embodiments, at least one M-language function includes nested M-language functions; the code conversion module 204 is also used to set up a global scope stack in Python; the scope of each function in the Python nested functions has a corresponding scope stack frame in the global scope stack, and the scope stack frame is used to store variables in the scope level of the function; a level identifier is added to the scope of the function in the function call form; the level identifier is used to indicate the level of the scope; when each function in the Python nested functions is called, the corresponding scope stack frame is determined from the global scope stack based on the level identifier of the function's scope, and the corresponding variable is searched from the determined scope stack frame. If it is not found, the variable is searched step by step from the bottom of the stack based on the level identifier.
[0131] In some embodiments, the code conversion module 204 is further configured to: obtain the built-in function names in the target built-in function library before converting the M language file; obtain the custom function names in the current directory and the custom function names in the M language file; generate a function name list based on the obtained built-in function names and custom function names; during the conversion of the M language file, if an identifier is detected and the identifier is followed by a preset character, determine whether the identifier is in the current variable list or function parameter list; if not, determine whether the identifier exists in the function name list; if it exists, identify it as the function name of the M language function and add the function parameters inside the M language function to the function parameter list; if it does not exist, identify it as a variable name and add the assigned variable name as a variable to the variable list; if so, identify the identifier as a variable name; if an identifier is detected and the identifier is not followed by a preset character or a special character, identify the identifier as a variable name.
[0132] In some embodiments, the script code, in addition to M-language functions, includes code about variables; variables include global variables and persistent variables; code about global variables is converted into corresponding Python global variable functions, and code about persistent variables is converted into corresponding Python persistent variable functions. Specifically, for Python global variable functions, the main module in Python sets up a global dictionary and a global variable name list stack. The global dictionary is the global storage area, and the global variable name list stack is used to store the names of global variables declared in the current scope. When a Python global variable function is called, global variable-related processing is performed based on the global dictionary and the global variable name list stack. For Python persistent variable functions, a persistent variable dictionary is set up inside the Python persistent variable function; the persistent variable dictionary is used to store the persistent variables in the Python persistent variable function.
[0133] In some embodiments, the preset M-language syntax includes a syntax for representing assignment; the Python function corresponding to the syntax for representing assignment is a Python assignment function; the first initial parameter group corresponding to the Python assignment function includes the variable name, the scope level, and the default parameters used to implement the assignment.
[0134] like Figure 3 As shown, the device also includes: Assignment module 208 is used during the execution of a Python file. If a Python assignment function is called, it determines the first scope dictionary corresponding to the variable names in the first initial parameter group based on the scope hierarchy. It then splits the default parameters of the first initial parameter group that contain vectors into multiple groups of parameters without vectors, obtaining the first parameter group. It performs secondary processing on the parameters in the first parameter group to obtain a parameter group that does not include the assignment data. It matches and combines the parameter group without assignment data with the assignment data to generate the target parameter group. It then assigns values based on the target parameter group and updates the first scope dictionary with the assignment results.
[0135] In some embodiments, the default parameters in the target parameter group include a type identifier parameter; the assignment module is also used to obtain the initial reference corresponding to the variable name in the target parameter group; the initial reference indicates the variable value corresponding to the variable name; if the type identifier parameter indicates array index reading or cell array index reading, then the index is read based on the remaining parameters in the default parameters to read the element at the index; the initial reference is updated based on the element at the index or the cell value within the element at the index; if the type identifier parameter indicates structure attribute reading, then the dictionary attribute is read based on the remaining parameters in the default parameters, and the initial reference is updated based on the read attribute; if the type identifier parameter indicates dimension deletion, then the variable value indicated by the initial reference is processed for dimension deletion based on the remaining parameters in the default parameters to update the initial reference; for the updated initial reference, the updated reference is assigned a value based on the assignment data in the target parameter group.
[0136] In some embodiments, the preset M-language syntax includes a syntax for representing values; the Python function corresponding to the syntax for representing values is a Python value-determining function; the second initial parameter set corresponding to the Python value-determining function includes the variable name, the scope level, and the default parameters used to implement the value determination.
[0137] like Figure 4 As shown, the device also includes: The value retrieval module 210 is used during the execution of a Python file to determine the second scope dictionary corresponding to the variable names in the second initial parameter group based on the scope hierarchy when the Python value retrieval function is called; to split the parameters with vectors in the default parameters of the second initial parameter group into multiple groups of parameters without vectors, thus obtaining the second parameter group; to determine the dimension of the result value based on the parameters in the second parameter group, and to pre-allocate the corresponding null value based on the dimension of the result value; to perform value retrieval processing based on the type identifier and scope dictionary in the second parameter group, and to update the pre-allocated corresponding null value based on the value retrieval result.
[0138] In some embodiments, an M-language function includes a target M-language function with multiple return values; The code conversion module 204 is also used to add a first attribute and a second attribute to the target Python function during the conversion process of the target M language function, and to assign values to the first attribute and the second attribute based on the function return value of the target Python function; the first attribute represents multiple return values, and the second attribute represents the number of return values; when only a part of the function return value is needed, the function return value is stored in a temporary variable, and the temporary variable is assigned a value to ignore the unused return value in the function return value.
[0139] Each module in the aforementioned programming language migration and execution device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device in hardware form, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.
[0140] In one embodiment, a computer device is provided, which may be a server or a terminal, and its internal structure diagram may be as follows: Figure 5 As shown, this computer device includes a processor, memory, input / output (I / O) interfaces, and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides the environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communication with external terminals via a network connection. When the computer program is executed by the processor, it implements a programming language migration and execution method.
[0141] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0142] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the embodiments of the present application.
[0143] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the steps of the embodiments of this application.
[0144] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps of the embodiments of this application.
[0145] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of the relevant countries and regions.
[0146] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments described above. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited to these.
[0147] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0148] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A method for migrating and running a programming language, characterized in that, The method includes: Initialize the Python runtime environment; the target built-in function library is pre-imported into the Python runtime environment; the target built-in function library includes built-in functions that come with the Python language, as well as custom built-in functions with the same name written for built-in functions used by the M language; During the conversion process of the M language file to be converted in the current directory, at least one M language function is identified from the M language script code included in the M language file, and each M language function is converted into a corresponding Python function according to the function syntax rules of Python. For the script code in the M language script code other than the M language functions, based on a pre-established correspondence table between preset M language syntax forms and Python functions, the code written in the script code using the preset M language syntax forms is converted into corresponding Python functions; wherein, the same M language syntax form has different functions or represents the processing between different data types, and the corresponding converted Python functions execute the different functions differently according to the function's internal context or perform type checking inside the function, executing different processing logic for different types; Based on the converted Python function, a Python file converted from the M language file is generated, and the Python file is run in the Python runtime environment to achieve the functionality of the M language script code.
2. The method according to claim 1, characterized in that, The M-language function is a user-defined function; the method further includes: When converting each of the M language functions into Python functions, a function code stack is maintained through Python code, and the code of the converted Python function is stored in the function code stack; After the function conversion is completed, the code of the converted Python function is retrieved from the function code stack and placed in front of the top code segment of the function code stack to define the converted Python function.
3. The method according to claim 2, characterized in that, The at least one M-language function includes nested M-language functions; When converting the nested M-language function into a nested Python function, the method further includes: In Python, a global scope stack is set up; the scope of each function in the nested Python function has a corresponding scope stack frame in the global scope stack, and the scope stack frame is used to store variables in the hierarchy where the function's scope is located. In the function call format, a hierarchy identifier is added to the function's scope; the hierarchy identifier is used to indicate the level of the scope; when each function in the Python nested function is called, the corresponding scope stack frame is determined from the global scope stack based on the hierarchy identifier of the function's scope, and the corresponding variable is searched from the determined scope stack frame. If it is not found, the variable is searched step by step towards the bottom of the stack based on the hierarchy identifier.
4. The method according to claim 1, characterized in that, The method further includes: Before converting the M language file, obtain the built-in function names in the M language built-in function library corresponding to the target built-in function library; obtain the custom function names in the current directory and the custom function names in the M language file; generate a function name list based on the obtained built-in function names and custom function names; During the conversion process of the M-language file to be converted in the current directory, at least one M-language function is identified from the M-language script code included in the M-language file, including: During the conversion of the M language file, if an identifier is detected and the identifier is followed by a preset character, it is determined whether the identifier is located in the current variable list or function parameter list. If not, determine whether the identifier exists in the function name list. If it exists, identify it as the function name of the M language function and add the function parameters inside the M language function to the function parameter list. If it does not exist, identify it as a variable name and add the assigned variable name as a variable to the variable list. If yes, identify the identifier as a variable name. If an identifier is detected, and the identifier is not followed by a preset character or a special character, then the identifier is recognized as a variable name.
5. The method according to claim 1, characterized in that, The script code, excluding the M-language functions, includes code related to variables; these variables include global variables and persistent variables. Code related to global variables is converted into corresponding Python global variable functions, and code related to persistent variables is converted into corresponding Python persistent variable functions; Specifically, for the Python global variable function, the main module in Python has a global dictionary and a global variable name list stack; the global dictionary is a global storage area, and the global variable name list stack is used to store the global variable names declared in the current scope; after the Python global variable function is called, global variable-related processing is performed based on the global dictionary and the global variable name list stack. For the Python persistent variable function, a persistent variable dictionary is set up inside the Python persistent variable function; the persistent variable dictionary is used to store the persistent variables in the Python persistent variable function.
6. The method according to claim 1, characterized in that, The preset M-language syntax includes a syntax for representing assignment; the Python function corresponding to the syntax for representing assignment is a Python assignment function; the first initial parameter group corresponding to the Python assignment function includes the variable name, the scope level, and the default parameters used to implement the assignment. The method further includes: During the execution of the Python file, if the Python assignment function is called, the first scope dictionary corresponding to the variable names in the first initial parameter group is determined based on the scope hierarchy. The parameters with vectors in the default parameters of the first initial parameter group are split into multiple groups of parameters without vectors to obtain the first parameter group; The parameters in the first parameter group are processed a second time to obtain a parameter group that does not include the assigned data. The parameter group without assigned data is matched and combined with the assigned data to generate the target parameter group; The target parameter set is assigned a value, and the assignment result is updated to the first scope dictionary.
7. The method according to claim 6, characterized in that, The default parameters in the target parameter group include a type identifier parameter; The assignment based on the target parameter set includes: Obtain the initial reference corresponding to the variable name in the target parameter group; the initial reference indicates the variable value corresponding to the variable name. If the type identifier parameter indicates array index reading or cell array index reading, then the index reading is performed based on the remaining parameters in the default parameters to read the element at the index; the initial reference is updated based on the element at the index or the cell value within the element at the index. If the type identifier parameter indicates struct attribute reading, then dictionary attribute reading is performed based on the remaining parameters in the default parameters, and the initial reference is updated based on the read attributes; If the type identifier parameter indicates dimension deletion, then the variable value indicated by the initial reference is processed by dimension deletion based on the remaining parameters in the default parameters, so as to update the initial reference; For the updated initial reference, the updated reference is assigned a value based on the assignment data in the target parameter group.
8. The method according to claim 1, characterized in that, The preset M-language syntax includes a syntax for representing value retrieval; the Python function corresponding to the syntax for representing value retrieval is a Python value retrieval function; the second initial parameter group corresponding to the Python value retrieval function includes the variable name, the scope level, and the default parameters used to implement value retrieval. The method further includes: During the execution of the Python file, if the Python value retrieval function is called, the second scope dictionary corresponding to the variable name in the second initial parameter group is determined based on the scope hierarchy. The parameters with vectors in the default parameters of the second initial parameter group are split into multiple groups of parameters without vectors to obtain the second parameter group; The result value dimension is determined based on the parameters in the second parameter group, and corresponding null values are pre-assigned based on the result value dimension. The value is processed based on the type identifier in the second parameter group and the scope dictionary, and the pre-assigned corresponding null value is updated based on the value result.
9. The method according to claim 1, characterized in that, The M-language functions include target M-language functions with multiple return values; The step of converting each of the M-language functions into corresponding Python functions according to Python's function syntax rules includes: During the conversion of the target M language function, a first attribute and a second attribute are added to the target Python function. The first attribute and the second attribute are assigned values based on the function return value of the target Python function. The first attribute represents multiple return values, and the second attribute represents the number of return values. If only a portion of the function's return value is needed, the function's return value is stored in a temporary variable, and the temporary variable is assigned a value to ignore the unused return value from the function's return value.
10. A programming language migration and execution device, characterized in that, The device includes: The environment initialization module is used to initialize the Python runtime environment; the target built-in function library is pre-imported in the Python runtime environment; the target built-in function library includes built-in functions that come with the Python language, as well as custom built-in functions with the same names written for built-in functions used by the M language; The code conversion module is used to identify at least one M language function from the M language script code included in the M language file during the conversion process of the M language file to be converted in the current directory, and convert each M language function into a corresponding Python function according to the function syntax rules of Python. The code conversion module is further configured to convert the script code written in the preset M language syntax form into corresponding Python functions based on a pre-established mapping table between preset M language syntax forms and Python functions. The same M language syntax form may have different functions or represent processing between different data types. The corresponding converted Python functions may execute different functions based on the function's internal context or perform type checking within the function, executing different processing logic for different types. The execution module is used to generate a Python file converted from the M language file based on the converted Python function, and to run the Python file in the Python runtime environment to realize the functions of the M language script code.
Citation Information
Patent Citations
Object packaging method for scientific computing language interpreter
CN113835688A
C + +-based FPGA (Field Programmable Gate Array) hardware construction language implementation system
CN114756219A
Method and device for grammar mapping from SIMSCRIPT language to C language
CN117193782A
Simulation modeling method and device of custom language and computer equipment
CN117992056A
Application program code conversion method and device and computer equipment
CN118733046A
Cited By
JS script automatic generation method and system suitable for swan mongolian NEXT simulator
CN121255156A