Programming language migration running method and apparatus

By importing the built-in function library 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 flexibility and scalability, and quickly covering M-language functions.

CN120950074BActive Publication Date: 2025-12-26CHANGSHA KELIANG TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511460657.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-10-14
Publication Date
2025-12-26
Estimated Expiration
2045-10-14

AI Technical Summary

Technical Problem

Existing technologies struggle to effectively run M-language code without relying on the Matlab core library, resulting in functional limitations and low maintainability. Furthermore, third-party libraries have limited applicability and flexibility.

Method used

By pre-importing the target built-in function library into the Python runtime environment, M language functions are identified and converted into Python functions. Based on the preset syntax form and correspondence table, M language script code is converted into Python functions, leveraging Python's flexibility and module functionality to implement the functions of the M language.

Benefits of technology

It reduces implementation difficulty, improves flexibility and scalability, enables quick and efficient migration of the M language to the Python runtime environment, covers its functionality, and supports independent modification of built-in functions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120950074B_ABST
    Figure CN120950074B_ABST
Patent Text Reader

Abstract

The application relates to a programming language migration running method and device, which can recognize M language functions from M language script codes, convert the M language functions into Python functions, and uniformly map and convert the remaining codes with complex M language syntax forms into Python functions, so that different functions or different processing logics for different types are realized through the context and other functions in the Python functions. The implementation difficulty is greatly reduced, complex functions can be quickly realized, and the M language can be effectively run without depending on the core library of Matlab.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of programming languages, and in particular to a programming language migration running method and device. BACKGROUND

[0002] There are few software supporting M language (MATLAB language, which is a programming language specially used for Matlab platform) in the current industry. Some software supporting M language mostly uses the core library of Matlab to support the running environment of M language. If the core library of Matlab cannot be used, the functions possessed by the code written by M language cannot be used, which has obvious limitations.

[0003] In order to reduce the above limitations, in some solutions, a corresponding interpreter is specially written by a developer for M language, which is used to parse M language. This way has high implementation difficulty, not only the syntax support of M language needs to be implemented, but also the execution environment needs to be provided. Moreover, the syntax of M language is complex and there is no official document, the effect of implementation is not necessarily good, and the maintainability is low and the coupling is too high, and modification often causes other problems.

[0004] In some other solutions, M language is parsed by relying on a third-party library to generate code of other programming languages, so that the generated code can run on other programming language platforms. However, the third-party library has limited applicable scenarios, and has low flexibility and scalability.

[0005] Therefore, it is urgent to propose a method that can effectively run M language without relying on the core library of Matlab. SUMMARY

[0006] Therefore, it is urgent to propose a method that can effectively run M language without relying on the core library of Matlab.

[0007] In a first aspect, the present application provides a programming language migration running method. The method comprises:

[0008] initializing a Python running environment; the Python running environment pre-imports a target built-in function library; the target built-in function library includes built-in functions of Python language and Python built-in functions of the same name which are self-defined for built-in functions used by M language;

[0009] In the conversion process of the current M language file to be converted under the current directory, at least one M language function is recognized 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 rule of Python;

[0010] For script code other than the M language function in the M language script code, based on a pre-established correspondence relationship table of a preset M language syntax form and a Python function, code written in the preset M language syntax form in the script code is converted into a corresponding Python function; wherein the same kind of M language syntax form has different functions or represents processing between different data types, and the corresponding converted Python function performs different functions according to the function internal context or performs type checking in the function, and performs different processing logic for different types;

[0011] Based on the converted Python function, a Python file converted from the M language file is generated, and the Python file is run in a Python running environment to realize the functions possessed by the M language script code.

[0012] In a second aspect, the present application also provides a programming language migration running device. The device comprises:

[0013] An environment initialization module is configured to initialize a Python running environment; the Python running environment pre-imports a target built-in function library; the target built-in function library includes built-in functions of Python language itself and Python built-in functions of the same name customized and written for built-in functions used by M language;

[0014] A code conversion module is configured to, in the conversion process of the current M language file to be converted under the current directory, recognize at least one M language function from the M language script code included in the M language file, and convert each M language function into a corresponding Python function according to the function syntax rule of Python;

[0015] The code conversion module is further configured to, for script code other than the M language function in the M language script code, based on a pre-established correspondence relationship table of a preset M language syntax form and a Python function, convert code written in the preset M language syntax form in the script code into a corresponding Python function; wherein the same kind of M language syntax form has different functions or represents processing between different data types, and the corresponding converted Python function performs different functions according to the function internal context or performs type checking in the function, and performs different processing logic for different types;

[0016] The running module is configured to generate a Python file converted from the M language file based on the converted Python function, and run the Python file in a Python running environment to realize the functions provided by the M language script code.

[0017] In a third aspect, the present application provides a computer device. The computer device comprises a memory and a processor. The memory stores a computer program. The processor implements the steps of the first aspect and the embodiments thereof when executing the computer program.

[0018] In a fourth aspect, the present application provides a computer readable storage medium. The computer readable storage medium stores a computer program. The processor implements the steps of the first aspect and the embodiments thereof when executing the computer program.

[0019] In a fifth aspect, the present application provides a computer program product. The computer program product comprises a computer program. The processor implements the steps of the first aspect and the embodiments thereof when executing the computer program.

[0020] The programming language migration running method, device, computer device, storage medium and computer program product described above identify the M language function from the M language script code and convert it into a Python function. In addition, the remaining code with complex M language syntax can be uniformly mapped and converted into a Python function. Through the context and other functions inside the Python function, different functions or different types of processing logic can be implemented. This greatly reduces the difficulty of implementation and enables quick implementation of complex functions. Moreover, the Python module function is open source and can be modified independently. For example, the built-in function used in the M language can be customized to write a Python built-in function with the same name, which greatly enhances flexibility and scalability. In this way, the functions provided by the M language can be more comprehensively covered. The generated and run Python file can quickly and effectively migrate the M language to the Python running environment. BRIEF DESCRIPTION OF DRAWINGS

[0021] Figure 1 A flowchart of the programming language migration running method in one embodiment;

[0022] Figure 2 A structural block diagram of the programming language migration running device in one embodiment;

[0023] Figure 3 A structural block diagram of the programming language migration running device in another embodiment;

[0024] Figure 4A structural block diagram of a programming language migration running device in another embodiment;

[0025] Figure 5 An internal structural diagram of a computer device in an embodiment. DETAILED DESCRIPTION

[0026] For the purpose, technical solutions and advantages of the present application to be more clear, the present application is further described in detail below in combination with the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and do not limit the present application.

[0027] In one embodiment, as shown in Figure 1 A programming language migration running method is provided, which is described by taking a computer device as an example. The computer device can be a server or a terminal. The method can be executed by the terminal or the server, or can be realized by interaction between the terminal and the server. The method comprises the following steps:

[0028] S11, initializing a Python running environment; a target built-in function library is pre-imported in the Python running environment; the target built-in function library comprises built-in functions required by Python language and Python built-in functions of the same name which are self-defined for built-in functions used by M language.

[0029] Python language is an open source object-oriented programming language, which has the characteristics of simple syntax and easy coding. Python language is also a dynamic type language, which is consistent with M language and simplifies the translated form. In addition, Python language has a large number of module libraries, including scientific computing, data processing and visualization libraries, which can perfectly cover most of the functions of M language. The object properties of Python can be changed at will, which is particularly flexible, and this feature helps to realize some special M language functions.

[0030] Therefore, using Python as the execution environment of M language helps to reduce the development difficulty and improve the expansibility. Moreover, a specific and detailed implementation scheme of migrating and running M language into Python running environment is proposed. The scheme is not easily thought of directly, because the specific scheme of converting M language into Python language running determines the effect of M language migration and running, and also determines the efficiency and cost of realizing migration and running. The specific scheme of the present application is obtained after creative labor.

[0031] Specifically, for the running environment of Python, the RERL interactive Python environment is used in the present application, and the built-in functions required by the Python language and the target built-in function library of the same name of the Python built-in functions customized for the M language are imported in the initialization stage. Based on the language characteristics of Python, the same name of the Python built-in functions can be very convenient and simple to write.

[0032] S12, in the conversion process of the M language file to be converted currently under the current directory, at least one M language function is recognized 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 rule of Python.

[0033] The M language script code involved under the current directory needs to be converted into Python code. There can be multiple M language files under the current target, so steps S12 to S14 can be executed one by one for each M language file to obtain the corresponding Python file of each M language file.

[0034] It should be understood that in some cases in the M language, the variables and functions are not obviously distinguished, for example, the undefined variable A(1, 2) and the function A(1, 2) are the same in form in the M language script code, but the forms of the variable and the function are different in the Python code converted by the present application, for example, the variable A(1, 2) is converted into the code arraygetvalue('A', 0, '(', 1, 2, None), and the function A(1, 2) is converted into the code A(1, 2), therefore, it is necessary to distinguish the variable and the function.

[0035] Specifically, in the conversion process, the M language functions are recognized from the M language script code included in the M language file, and the number of M language functions is one or more. Each M language function is converted into a corresponding Python function form according to the function syntax rule of Python.

[0036] In the conversion process, the variables in the M language script code can also be recognized and the code about the variables can be converted into the code in the form of the function, for example, in combination with the above example. How to distinguish and recognize the function and the variable, and the specific processing of the variable conversion will be described below, which is not described here.

[0037] As step S11, the built-in function has been pre-written to generate a built-in function library, and the M language function identified from the M language script code is a custom function. Because the position of the function definition in an M language file can be at any position, but the definition of the function in Python must be before the call, therefore, after converting the corresponding Python function, the Python function code is placed above the code segment to realize the function definition. Exemplarily, the function definition position processing manner of the custom function is as follows:

[0038] Specifically, when converting each M language function (i.e., custom function) into a Python function, the function code stack is maintained through Python code, and the code of the Python function obtained by step-by-step conversion is stored in the function code stack. After the function conversion is completed, the code of the converted Python function is taken out from the function code stack, and the taken-out code is placed in front of the top code segment of the function code stack to realize the definition of the converted Python function, thereby ensuring that the function definition is before the call code of the function, and thus ensuring the compliance of the Python syntax.

[0039] In some embodiments, nested functions are often used in custom functions of M language, which belong to a common custom function. The nested function is multi-layered, and the inner function can use the variables in the outer function. For example, a function FuncA internally defines another function FuncB, and FuncB can directly use the existing variables in FuncA.

[0040] The present application finds that the differences between nested functions in M language and Python language are as follows:

[0041] (1) The inner function in M language can directly use the variables of the outer function, and Python must increase nonlocal to declare the variables in the outer function;

[0042] (2) The call of the code in the outer function to the inner function in Python must be after the definition of the inner function, and M language does not need it.

[0043] In view of the above differences, the present application embodiment proposes a scheme. Specifically, when converting the M language nested function into a Python nested function, a global scope stack is set in Python; the scope of each function in the Python nested function has a corresponding scope stack frame in the global scope stack, and the scope stack frame is used to store the variables in the layer of the scope of the function. A layer identifier is added in the call form of the function for the scope of the function; the layer identifier is used to represent the layer of the scope.

[0044] When each function in the Python nested function is called, a hierarchical identifier of the function's scope is used to determine a corresponding scope stack frame from the global scope stack, a corresponding variable is searched from the determined scope stack frame, and if the variable is not found, the variable is searched step by step from the bottom of the stack based on the hierarchical identifier. The data type of the hierarchical identifier can be an integer parameter. For example, a hierarchical identifier of 0 indicates that the current scope is the topmost; a hierarchical identifier of 1 indicates that the current scope is one level of nesting, and if there is no variable in the current scope, the variable can be searched one level from the bottom of the stack; a hierarchical identifier of 2 indicates that the variable search range can be two levels from the bottom of the stack; and so on.

[0045] That is, when finally converted into a Python file and run, if any function in the Python nested function is called, the variable of the outer function can be used based on the hierarchical identifier of the scope of the function and the global scope stack. In this way, without adding a nonlocal declaration, the inner function of the converted Python nested function can use the variable of the outer function in a completely new way.

[0046] Each memory function in the nested function belongs to a custom function, and for the above-mentioned difference (2), the function definition position processing method of the custom function described above can be used. By maintaining a function code stack, the memory function is defined before being called by the outer function, which will not be described here. In some embodiments, the identified M language function can include a function supporting multiple return values (for convenience of description, it is referred to as a target function). The relevant processing of the target function will not be described here, and will be described in detail below.

[0047] S13, for script code other than M language functions in the M language script code, based on a pre-established correspondence relationship 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 a corresponding Python function; wherein, the same M language syntax form has different functions or represents processing between different data types, and the corresponding converted Python function performs different functions according to the function internal context or performs type checking in the function, and performs different processing logic for different types.

[0048] The present application finds that an M language syntax form has different functions. For example, an assignment symbol = can have an assignment function (for example: A(1, 2) = 1), can have an expansion dimension function (for example: A(2, 2) = 1), and can have a dimension deletion function (for example: A(2, :) = []), etc. For example, the syntax form of the square brackets [] can represent constructing an array (for example: [1, 2; 1, 2]), can represent horizontally concatenating arrays (for example: [A, A]), and can represent vertically concatenating arrays (for example: [A; A]).

[0049] In addition, the same M language syntax form represents processing between different data types. For example, a + b can represent adding two scalars, adding two vectors, adding two matrices, adding a scalar and a matrix, and adding strings, etc. in matlab.

[0050] Obviously, the syntax form of the M language is very complex. In addition to the M language function, the script code in the script code has such a complex syntax form. In view of this situation, in the present embodiment, a correspondence table of the preset M language syntax form and the Python function is established in advance, which can be seen in Table 1. It should be understood that Table 1 only shows part of the correspondence, and the values in the M language syntax form and the Python form in Table 1 are only used for example and illustration, and the function name in the Python form field is also only used for simple illustration, and can be other naming, which is not limited.

[0051] Table 1

[0052]

[0053] Based on the correspondence table, at least part of the script code other than the M language function can be converted into the corresponding Python function. Specifically, in the script code other than the M language function, if there is code written using the preset M language syntax form in the correspondence table, it can be converted into the corresponding Python function in the correspondence table. For example, if there is code using the syntax form a(1, 2,...) in the script code other than the M language function, it can be converted into the Python function form arraygetvalue('a', 0, '(', 1, 2,... None). For example, the script code other than the M language function can include code representing variables, and based on the correspondence table, the code segment representing variables written in the preset M language syntax form is converted into the corresponding Python function.

[0054] Thus, the same M language syntax form is translated into a unified Python function, and the translated Python function name is the same as long as the form is consistent. The specific function is executed by the Python function inside according to the context to distinguish different function codes. The translation and execution are decoupled, the function inside is modified, the flexibility is increased, etc.

[0055] In addition to being able to implement different functions, the M language syntax form is translated into a Python function, which can also distinguish data types through the internal Python function, thereby achieving targeted data processing between different data types. For example, for "a+b" in the M language, it is converted into the corresponding Python function sum(a,b). The dynamic type language feature of Python can be used to perform data type checking inside the sum function, and then different processing logic is executed according to the checked data type.

[0056] S14, based on the converted Python function, generates a Python file converted from the M language file, and runs the Python file in the Python running environment to realize the functions possessed by the M language script code.

[0057] Exemplarily, the converted Python file includes at least the following parts of code:

[0058] (1) The first part of the code includes: the Python code needs to include "from __main__ import*" at the beginning and the code for importing the (converted) Python function in the current directory.

[0059] Among them, from __main__ import* is a module import statement, which is used to import all public variables, functions, classes and other objects from the main module (__main__ module) of the current program.

[0060] It should be understood that the M language file in the current directory will be converted into a Python file, and each Python file needs to use the functions including the self-defined functions and the functions defined in other files in the current directory. Therefore, the converted Python functions in other Python files can be used through the code for importing the Python functions in the current directory.

[0061] (2) The second part of the code includes: the definition of the self-defined function in the current converted Python file.

[0062] As shown above, the functions that need to be used in the current Python file include the custom Python functions, which need to be defined before being called, and the second part of the code is the code for defining the custom functions.

[0063] It should be understood that the custom Python functions in the current Python file and the Python functions in the other Python files in the current directory that need to be imported are all converted by the method in each embodiment of the present application, such as the Python functions converted from the M language functions originally possessed by the M language file, and the Python functions converted from the non-function M language syntax form.

[0064] (3) The third part of the code: the converted script main body code.

[0065] It should be understood that the Python file has other script main body codes in addition to the functions, which are the above-mentioned third part of the code.

[0066] (4) The fourth part of the code: the code for updating the variables of the current scope dictionary to the main module (__main__ module) to ensure availability next time.

[0067] In the above method, the M language functions are recognized from the M language script code and converted into Python functions. In addition, the rest of the code with complex M language syntax forms can be uniformly mapped and converted into Python functions, and through the context and other functions inside the Python functions, different functions or different types of processing logic can be implemented. This greatly reduces the difficulty of implementation and can quickly implement complex functions. Moreover, the module functions of Python are open source and can be modified independently, such as custom writing Python built-in functions with the same name as the built-in functions used in M language, which greatly enhances flexibility and scalability. In this way, the functions possessed by M language can be more comprehensively covered, and the generated and run Python file can quickly and effectively migrate M language to the Python running environment.

[0068] In addition, based on the free coding characteristics of Python functions, the translation conversion and function functionality can be decoupled.

[0069] Moreover, compared with converting M language into other scientific computing languages, such as Julia language, the extensibility and flexibility of the scheme of the present application are better. Because the extensibility of Julia language is not good, the flexibility is insufficient, and it also cannot support many original functions of M language, especially the related functions of global variables and persistent variables.

[0070] Similarly, compared with relying on a third-party library to parse the M language, the scheme of the present application has better flexibility and scalability. Because, once the third-party library (such as ANTLR4) is not applicable to the scene, it cannot be processed and run, and the flexibility is not strong. Moreover, it can only rely on the update support of ANTLR4, and cannot fully support the functions of the M language, and the scalability is not strong. The scheme of the present application can use the simple coding of Python and the numerous module libraries to increase specific function functions, and has very strong flexibility and scalability, and can fully and quickly cover the original functions of the M language.

[0071] From the foregoing, if you want to accurately convert the M language script code into Python code, it is essential to distinguish between functions and variables in the M language script code. How to distinguish and identify functions and variables will be introduced below.

[0072] In some embodiments, the target built-in function library pre-imported in the Python running environment corresponds to the M language built-in function library. 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 built-in functions. Therefore, 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 built-in functions are obtained.

[0073] The target built-in function library includes a custom built-in function library (including Python built-in functions of the same name that are custom written for the built-in functions used by the M language) and a Python built-in function library (that is, the builtins library, including built-in functions that come with the Python language). The built-in function names can be obtained from the custom built-in function library through the main module (__main__ module), and the built-in function names can be obtained through the builtins library.

[0074] Moreover, the custom function names in the current directory and the custom function names in the current M language file to be converted (for example, according to the search rules of the M language, the custom function names of the function scripts in the search directory set and the custom function names of the function scripts in the current directory are found) can also be obtained. Further, a function name list is generated according to the obtained built-in function names and custom function names. The function name list covers the system built-in functions and user-level custom functions, and is relatively comprehensive. Therefore, it can be used as an important basis for subsequent identification of M language functions, and can accurately distinguish and identify functions and variables, thereby improving the conversion accuracy. It should be understood that if the current directory is changed, the function name list needs to be updated accordingly.

[0075] Further, a variable list and a function parameter list are created in advance. The variable list and the function parameter list are updated as the conversion process proceeds. For example, the identification and conversion of the M language function can be processed in parallel, and the function parameter list can be updated based on the function parameters in the identified M language function.

[0076] In this embodiment, the specific processing of identifying at least one M language function from the M language script code included in the M language file in the conversion process of the M language file currently to be converted under the current directory in step S12 is as follows:

[0077] The M language file is converted in sequence. In the conversion process of the M language file, if an identifier (the identifier represents a name in the code) is identified, it is further determined whether a preset character is present after the identifier (the next adjacent character). If not, (1) is performed, and if yes, (2) to (6) are performed. The preset character is a character with information content that can reflect the identity of a variable and a function. For example, in the M language, both the variable A(1, 2) and the function A(1, 2) have a left parenthesis after the identifier. Therefore, the preset character can be the left parenthesis '('.

[0078] (1) If the identifier is identified and the preset character and a special character are not present after the identifier, the identifier is identified as a variable name.

[0079] Specifically, step (1) includes: if the identifier is identified and the preset character is not present after the identifier, it is determined whether the identifier is present in the current variable list or the function parameter list. If yes, the identifier is identified as a variable name, and if not, it is determined whether the identifier is a special character (for example, the imaginary unit 'i' or 'j'). If yes, the identifier is identified as a special character, and if not, the identifier is identified as a variable name.

[0080] It should be understood that if the identifier is identified as a variable name, the code including the identifier is code about the variable, and the code about the variable can be converted into a corresponding Python function.

[0081] (2) If the identifier is identified and the preset character is present after the identifier, it is determined whether the identifier is present in the current variable list or the function parameter list. If yes, (3) is performed, and if not, (4) is performed.

[0082] (3) If the identifier is present in the current variable list or the function parameter list, the identifier is identified as a variable name.

[0083] (4) If the identifier is not present in the current variable list or the function parameter list, it is further determined whether the identifier is present in the function name list. If yes, (5) is performed, and if not, (6) is performed.

[0084] (5) If the identifier exists in the function name list, the identifier is identified as a function name of the M language function.

[0085] It should be understood that after the M language function is identified, the function parameters inside the M language function can be added to the function parameter list to update the function parameter list in the conversion process, facilitating subsequent variable and function differentiation identification. The parameters in the function are essentially also variables, so when the variable and function differentiation identification is performed, the function parameters inside the function can be considered and added to the function parameter list.

[0086] (6) If the identifier does not exist in the function name list, the identifier is identified as a variable name.

[0087] Exemplarily, in the conversion process, the variable name that is assigned can be added to the variable list as a variable to update the variable list, facilitating subsequent variable and function differentiation identification.

[0088] In some embodiments, in addition to the M language function, the script code includes code about variables; the variables include global variables and persistent variables.

[0089] (1) The code about the global variable can be converted into a corresponding Python global variable function. For example, the statement of global A is converted into the function arraysetglobal('A'), and the statement of deleting the global variable clear global A is converted into the function arrayclearglobal('A').

[0090] The present application finds that there are the following differences between the M language and Python for the storage of the global variable after research:

[0091] (1) The M language has a special global variable storage area; the global variable of Python belongs to a single module, and other modules need to be imported into this module first;

[0092] (2) The life cycle of the global variable in the M language is the life cycle of the Matlab process; the life cycle of the global variable of Python follows the life cycle of the module, and if the module is unloaded, the life cycle of the global variable inside the module ends;

[0093] (3) The creation of the global variable of the M language can be performed at any place where the global declaration is used; Python can only be defined in the module and cannot be defined in the function.

[0094] Therefore, for the statement of global variable, in addition to being translated into Python global variable function formally, in order to realize the function of global variable, there is a global dictionary and a global variable name list stack in the main module in Python; wherein, the global dictionary is a global storage area, and the global variable name list stack is used to store the global variable name declared in the current scope. Setting the global dictionary and the global variable name list stack in the main module does not need to import a separate module, in addition, the global variable name is stored in the module and the global variable name list stack, and is not defined in the function, which meets the syntax compliance of Python. Since the main module (__main__ module) exists following the RERL mode of Python, as long as the Python process exists, the main module also exists, which solves the problem of life cycle.

[0095] It should be understood that after the Python global variable function is called, the global variable related processing is executed based on the global dictionary and the global variable name list stack.

[0096] For example, the function of arraysetglobal ('A') is to add the global variable A to the global dictionary if A does not exist in the global dictionary globalDict, and otherwise do nothing; then write the global variable A into the globalnames list (global variable name list) of the current stack; the function of arrayclearglobal ('A') is to delete the global variable 'A' in the globalnames list of the current stack, and delete the global variable A in the global dictionary globalDict. That is, if the Python global variable function-arraysetglobal ('A') is called, the global variable can be added and stored based on the global dictionary and the global variable name list stack. If the function arrayclearglobal ('A') is called, the global variable is deleted based on the global dictionary and the global variable name list stack.

[0097] (2) The code about persistent variable can be converted into the corresponding Python persistent variable function.

[0098] The persistent variable is the local variable of the function declared in the M language. There is no concept of persistent variable in Python. In the embodiment of the present application, the following method is used to realize the function of persistent variable in Python.

[0099] The code declared in the M language function about the persistent variable can be converted into the corresponding Python persistent variable function. For example, the persistent variable persistent A in the function FuncA is converted into the Python persistent variable function arraysetpersistents(FuncA, 'A'). In addition to being formally converted into the Python persistent variable function, a persistent variable dictionary is set inside the Python persistent variable function; the persistent variable dictionary is used to store the persistent variable in the Python persistent variable function.

[0100] The function arraysetpersistents(func, 'A') is used to add the persistent variable dictionary in the attribute of the function func if the persistent variable dictionary (i.e., the persistent variable dictionary) __persistent__ does not exist in the attribute of the function func, and then, it is judged whether A exists in the persistent variable dictionary. If A does not exist in the persistent variable dictionary, the key of the persistent variable A is added in the persistent variable dictionary, and the default value is an empty list. Thus, the persistent variable in the Python persistent variable function is stored in the persistent variable dictionary to realize the function of the persistent variable.

[0101] In some embodiments, the present application finds that the assignment in the M language has multiple forms, such as a = 1, a.b = 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], and the like. These forms can be superimposed on each other, and thus the form is particularly complex. In order to reduce the complexity of the translation conversion into Python and increase more flexibility, in the present embodiment, the default expression feature of the function is used to complete the unification of the assignment. Specifically, the preset M language syntax form in the above correspondence table (such as Table 1) includes a syntax form representing assignment. The syntax form representing assignment can be converted into the corresponding Python assignment function based on the correspondence table. For example, the specific form of the Python assignment function is:

[0102] arraysetvalue(varName, level, *args).

[0103] As can be seen from the above function, the multiple parameters of the Python assignment function are referred to as the first initial parameter group, including the variable name, the scope level of the variable, and the default parameter.

[0104] The variable name varName represents the name string of the variable, such as 'A'.

[0105] level: indicates the function scope level where the current variable is located, for example, level = 0 indicates not in a function, level = 1 indicates in a one-level function, and level = 2 indicates in a nested second-level function.

[0106] args: indicates the default parameters used to implement the assignment.

[0107] For example, the format of the default parameter args is as follows:

[0108] args=[type, dim1, dim2, …, None] * value

[0109] []: indicates that the content in [] can or can not be present

[0110] *: indicates that it can be repeated 0 times or more

[0111] type: indicates the type identifier, for example, '(' indicates array index reading, '{' indicates cell array index reading value, '.' indicates structure attribute reading, etc.

[0112] dimN: can be a number indicating the dimension (such as index dimension), can be an identifier string indicating the attribute (i.e. attribute name), and can be ':' indicating the index with ':' special meaning. dim1 and dim2 in the above format are examples of dimN.

[0113] None: the end of parameter identifier indicates the end of the parameter at this level;

[0114] value: indicates the data to be assigned.

[0115] It should be understood that based on the converted Python assignment function of the above data format, a variety of complex assignment forms in M language can be implemented through a function.

[0116] Specifically, during the running of the Python file, if the Python assignment function is called, the assignment can be implemented through the following steps:

[0117] (1-1) Based on the scope level where the variable is located, determine the first scope dictionary corresponding to the variable name in the first initial parameter group. For example, the dictionaries of global domain, persistent variable domain, nested function domain, and other scopes are determined to determine the assignment range.

[0118] (1-2) Split the parameters with vectors in the default parameters of the first initial parameter group into multiple groups of parameters without vectors to obtain the first parameter group.

[0119] For example, the parameter ('a', 0, '(', [1, 2, 3], 2, None, a) is preprocessed into ('a', 0, '(', 1, 2, None, a), ('a', 0, '(', 2, 2, None, a), ('a', 0, '(', 3, 2, None, a), and some empty value assignment processing.

[0120] (1-3) Secondary processing of the parameters in the first parameter group to obtain a parameter group without assignment data.

[0121] The main purpose is to process the vector parameters and some logical value parameters left over from the preprocessing to obtain a parameter group without assignment data.

[0122] (1-4) Matching and combining the parameter group without assignment data and the assignment data to generate a target parameter group.

[0123] It should be understood that if the assignment data is a vector or a matrix, the corresponding elements of the vector and the matrix also need to be extracted and matched with the parameter group without assignment data to form different target parameter groups. For example, the parameter group without assignment data is ('a', 0, '(', 1, 2, None), ('a', 0, '(', 2, 2, None), and the assignment data is [1, 2]. Then, 1 and 2 in the assignment data can be matched and combined with the parameter group without assignment data to generate the final target parameter group ('a', 0, '(', 1, 2, None, 1), ('a', 0, '(', 2, 2, None, 2).

[0124] (1-5) Assignment based on the target parameter group, and updating the assignment result to the first scope dictionary.

[0125] In the case of multiple target parameter groups, assignment is performed based on each target parameter value. Updating the assignment result to the first scope dictionary means that in the first scope dictionary, the variable name is bound and mapped to the assignment result, so that when the variable name is called, the assignment result can be accessed.

[0126] In some embodiments, the default parameter in the target parameter group includes a type identifier parameter. The assignment processing step (1-5) based on the target parameter group includes:

[0127] (1-5.1) Obtaining 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.

[0128] Specifically, the reference of the variable value is obtained, and if the variable value does not exist, an empty variable value is generated and its reference is obtained. The obtained initial reference is denoted as reference A.

[0129] (1-5.2) Perform parameter processing for the target parameter group. If the type identification parameter (i.e., type) represents a structure property reading, perform a dictionary property reading based on the remaining parameters in the default parameters, and update the initial reference based on the read property.

[0130] Specifically, if the type identification parameter represents a structure property reading (e.g., '.'), in step (1-5.2), the remaining dimN parameter in the default parameters is a string representing a property name, a property reading is performed from the dictionary based on the property name, and if the property name exists in the variable indicated by the reference A, the property value read based on the property name is updated to the reference A, i.e., the reference A is pointed to the property value corresponding to the property name. If the property name does not exist in the variable indicated by the reference A, the property corresponding to the property name is generated and its value is assigned to empty, and then its reference is updated to the reference A.

[0131] (1-5.3) If the type identification parameter represents dimension deletion '[]', perform dimension deletion processing on the variable value indicated by the initial reference based on the remaining parameters in the default parameters to update the initial reference.

[0132] In this case, the remaining parameters in the default parameters are indication parameters representing dimension deletion. Based on the indication parameters representing dimension deletion, it is determined whether to delete the entire column, if so, the entire column is deleted; or it is determined whether to delete the entire row, if so, the entire row is deleted; or it is determined whether to delete a single element of the matrix, if so, the remaining elements are combined into a vector. In this way, dimension deletion processing is achieved, and the data and structure of the variable value after dimension deletion processing have changed, so the initial reference A is pointed to the data after dimension deletion, and the update of the initial reference is achieved. After the variable is called, the actual data obtained based on the updated reference is the data after dimension deletion.

[0133] If the variable is deleted, the variable is assigned to empty, i.e., the entire variable no longer exists.

[0134] (1-5.4) If the type identification parameter represents array index reading (e.g., '(') or cell array index reading (e.g., '{'), perform index reading based on the remaining parameters in the default parameters to read the element at the index; update the initial reference based on the element at the index or the cell value within the element at the index.

[0135] Specifically, in this case, the remaining parameter dimN in the default parameter can be a number, indicating the index dimension, dimN can be one or more, that is, there can be one or more indexes. If it is a single index, if the index exceeds the number of elements, the vector is expanded, the expanded elements are filled with zeros, and if it does not exceed, it is not processed. For example, assuming that the variable value pointed to by the initial reference A is a vector with 5 elements, but the index indicates the 6th element, it exceeds the number of elements of the variable value, and therefore the vector can be expanded by zero filling to a vector with 6 elements.

[0136] If it is multiple indexes, a dimension judgment is performed, and if the index dimension exceeds the dimension of the variable value indicated by the initial reference A, a multi-dimensional expansion is performed, the expanded elements are filled with zeros, and if it does not exceed, it is not processed. For example, the variable value pointed to by the initial reference A is a 2x2 matrix, but there are 2 indexes dim2, dim3, respectively, then the variable value needs to be expanded to a 2x3 matrix by zero filling to locate the element at the index.

[0137] After obtaining the element at the index, if the type identifier parameter represents an array index read ('('), the initial reference is updated based on the element at the index, so that the updated reference points to the element at the index. If the type identifier parameter represents a cell array index read ('{'), the initial reference is updated based on the cell value within the element at the index, so that the updated reference points to the cell value within the element at the index.

[0138] (1-5.5) Based on the assignment data in the target parameter group, the updated reference is assigned.

[0139] The updated reference is used to point to the location that needs to be assigned finally, and therefore the updated reference can be assigned based on the assignment data in the target parameter group.

[0140] (1-5.6) If the type identifier is not present in the default parameter in the target parameter group, it means that there is no nesting level, and the initial reference can be directly assigned based on the assignment data in the target parameter group.

[0141] In some embodiments, the present application finds that there are also various forms of value taking in the M language. Compared with the above-mentioned various assignment forms, the value taking form does not have an equal sign and data on the right, and obviously the value taking form is also very complex. In this embodiment, the default expression feature of the function is also used to complete the unification of value taking. Specifically, the preset M language syntax form in the above-mentioned correspondence table (such as Table 1) includes a syntax form representing value taking, and the syntax form representing value taking is converted into a corresponding Python value taking function based on the correspondence table. For example, the specific form of the Python value taking function is:

[0142] arraygetvalue(varName, level, *args).

[0143] From the above function, the Python value getting function also corresponds to multiple parameters, denoted as the second initial parameter group, including the variable name, the variable scope level, and the default parameter. The description of each parameter is detailed in the introduction of the assignment function, which is not repeated here. It needs to be explained that compared with the first initial parameter group of the Python assignment function, the default parameter args in the second initial parameter group does not have the value parameter, because the value parameter only exists in the scenario where the value needs to be assigned, as the assignment data.

[0144] In the process of running the Python file, if the Python value getting function is called, the value can be obtained through the following steps:

[0145] (2-1) Based on the scope level, determine the second scope dictionary corresponding to the variable name in the second initial parameter group.

[0146] The principle is referred to step (1-1) in the assignment description part, which is not repeated here.

[0147] (2-2) Split the parameters with vectors in the default parameters of the second initial parameter group into multiple groups of parameters without vectors to obtain the second parameter group.

[0148] (2-3) Determine the result value dimension based on the parameters in the second parameter group, and pre-allocate the corresponding null value based on the result value dimension.

[0149] In other words, allocate the memory space to null value before taking the value, and store the value directly into the corresponding memory space after taking the value.

[0150] (2-4) Based on the type identifier and the scope dictionary in the second parameter group, perform value getting processing, and update the pre-allocated corresponding null value based on the value getting result.

[0151] Specifically, the initial reference of the variable value is obtained, denoted as reference a, and the parameter processing is performed in a loop. If the type identifier is '.', attribute reading of the dictionary is performed. If the read attribute exists in the variable value indicated by reference a, the reference a is updated based on the reference of the attribute value read, so as to take the value from the scope dictionary based on the updated reference, and update the pre-allocated corresponding null value based on the value getting result, so as to store the value getting result to the pre-allocated memory space. If the attribute does not exist in the variable value indicated by reference a, the value cannot be taken, so an error is reported and the program exits.

[0152] If the type is identified as '(' or '{', the processing of index reading value is performed, if the index is out of bound, an error is reported and the program is exited, if there is no out of bound, the reference a is updated based on the reference of the value at the index, if the loop is ended, the value pointed by the updated reference is obtained and returned, which is the result of the value taking.

[0153] In some embodiments, the M language function includes a target M language function with multiple return values. And the number of return values of the same target M language function can be different. For example, [a, b] = FuncA() and [a, b, c] = FuncA() are both supported in the M language. But Python does not support such multiple return value functions, and does not support the function of returning different numbers of return values according to different return values. After research, the present application proposes a dynamic change strategy for the function data of Python, which can convert the target M language function with multiple return values into a Python function with this function.

[0154] Specifically, in the conversion process for the target M language function, a first attribute mulOut and a second attribute mulNum are added to the converted target Python function, and the first attribute and the second attribute are assigned based on the function return value of the target Python function; the first attribute mulOut represents multiple return values, and the second attribute mulNum represents 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 attribute and the second attribute to the converted function, multiple return values can be obtained dynamically, and the number of return values can be determined dynamically.

[0155] In some cases, all function return values may not be used, but only part of the function return values are needed. The function return value refers to all function return values. In this case, the function return value can be stored in a temporary variable, and the temporary variable is assigned to ignore the unused return values in the function return value, and only the needed return values are used.

[0156] For example, in the M language, [a,~,b]=funcA() indicates that the function funcA() returns 3 return values, but only 2 return values, i.e., "a" and "b", are needed, and the middle return value is not needed, so the "~" is used. Then, using the method in the embodiment, the conversion is obtained as funcA.mulOut=True; funcA.outNum=3; _1 =funcA(); a=_1[0]; b=_1[2], wherein _1 represents a temporary variable, which is equivalent to storing the 3 return values of funcA() in the temporary variable _1, and then, the temporary variable is assigned, i.e., "a=_1[0]; b=_1[2]" indicates that the 0th return value and the 2nd return value are used from the 3 return values stored in the temporary variable, and the middle 1st return value is not used, thereby achieving the purpose of ignoring the return value that is not needed.

[0157] The way of adding the first attribute and the second attribute to the function and the temporary variable in the embodiment is only applicable to the conversion processing of the target M language function supporting multiple return values, and for the M language function not supporting multiple return values, these attributes and temporary variables do not need to be added, and only the conversion according to the conventional Python function syntax rule is needed.

[0158] The method in the embodiments of the present application can support the user to quickly transfer the previous achievements (i.e., the M language script code previously developed based on Matlab) in Matlab to the open source Python environment not dependent on Matlab. Using the method in the embodiments of the present application, a small amount of general functions or unified functions are quickly implemented by using Python, and the use scenarios of the M language can be quickly covered, such as the support of common data types (scalar, vector, matrix, string, multi-dimensional array, structure, cell array, etc.), matrix expression, scientific calculation, matrix operation, visualization function, custom function, function file mutual call, support of nested function scope, global variable, persistent variable, and the like. In this way, based on the basic idea of the present application, the dynamic properties of the code conversion and the Python object are flexibly used, and all M language scenarios can be quickly implemented to normally run in the Python environment. By using the method in the embodiments of the present application combined with the analysis of the model file, the model file of the Matlab platform is successfully implemented to directly convert and run in the power simulation modeling software (including the Python environment), and the consistency with the simulation result of Matlab is maintained.

[0159] It should be understood that although each step in the flowchart involved in the above embodiments is shown in sequence according to the arrow, these steps are not necessarily executed in the order indicated by the arrow. Unless otherwise specified herein, the execution of these steps is not strictly limited in sequence, and these steps can be executed in other orders. Moreover, at least some of the steps in the flowchart involved in the above embodiments can include multiple steps or multiple stages, which are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily sequential, but can be alternately executed with at least some of the other steps or steps or stages in the other steps.

[0160] Based on the same inventive concept, the embodiments of the present application also provide a programming language migration running device for implementing the above-mentioned programming language migration running method. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme described in the above method, so the specific limitations in one or more programming language migration running device embodiments provided below can refer to the limitations of the programming language migration running method in the foregoing, which will not be repeated here.

[0161] In one embodiment, as shown in Figure 2 a programming language migration running device is provided, which comprises:

[0162] an environment initialization module 202, configured to initialize a Python running environment; the Python running environment has pre-imported a target built-in function library; the target built-in function library includes built-in functions of the Python language itself and Python built-in functions of the same name customized and written for built-in functions used by the M language;

[0163] a code conversion module 204, configured to, in the conversion process of an M language file to be converted currently in a current directory, identify at least one M language function from M language script code included in the M language file, and convert each M language function into a corresponding Python function according to a function syntax rule of the Python;

[0164] The code conversion module 204 is further configured to, for script code other than the M language function in the M language script code, convert code written in a preset M language syntax form in the script code into a corresponding Python function based on a pre-established correspondence table of the preset M language syntax form and the Python function; wherein the same M language syntax form has different functions or represents processing between different data types, and the corresponding converted Python function performs different functions according to the function internal context or performs type checking in the function and executes different processing logic for different types.

[0165] The running module 206 is configured to generate a Python file converted from the M language file based on the converted Python function, and run the Python file in a Python running environment to realize the function provided by the M language script code.

[0166] In some embodiments, the M language function is a custom function. The code conversion module 204 is further configured to maintain a function code stack by Python code when converting each M language function into a Python function, store the code of the converted Python function in the function code stack, and take the code of the converted Python function from the function code stack after the conversion of the function ends, and place the taken code of the Python function in front of the top code segment of the function code stack to realize the definition of the converted Python function.

[0167] In some embodiments, the at least one M language function includes an M language nested function; the code conversion module 204 is further configured to set a global scope stack in Python; each function in the Python nested function has a corresponding scope stack frame in the global scope stack, and the scope stack frame is used to store variables in the scope of the function; a level identifier is added to the call form of the function for the scope of the function; the level 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 level identifier of the scope of the function, the corresponding variable is searched from the determined scope stack frame, and if the variable is not found, the variable is searched step by step from the bottom of the stack based on the level identifier.

[0168] 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 M language file; generate a function name list according to the obtained built-in function names and custom function names; in the conversion process of the M language file, if an identifier is identified, and the identifier is followed by a preset character, it is determined whether the identifier is located in the current variable list or the function parameter list; if not, it is determined whether the identifier exists in the function name list, if it exists, the identifier is identified as the function name of the M language function, and the function parameters in the M language function are added to the function parameter list, if it does not exist, the identifier is identified as a variable name, and the variable name to be assigned is added to the variable list as a variable; if yes, the identifier is identified as a variable name; if an identifier is identified, and the identifier is not followed by a preset character and a special character, the identifier is identified as a variable name.

[0169] 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.

[0170] 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.

[0171] 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.

[0172] 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.

[0173] like Figure 3 As shown, the device also includes:

[0174] 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.

[0175] 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.

[0176] 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.

[0177] like Figure 4 As shown, the device also includes:

[0178] 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.

[0179] In some embodiments, an M-language function includes a target M-language function with multiple return values;

[0180] 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.

[0181] The various modules in the programming language migration running apparatus described above can be implemented wholly or partially by software, hardware and combinations thereof. The various modules described above can be embedded in a processor in the computer device in hardware form or independent of the processor in the computer device, or stored in a memory in the computer device in software form to be invoked by the processor to perform the operations corresponding to the various modules.

[0182] In one embodiment, a computer device is provided, which can be a server or a terminal, and an internal structure diagram thereof can be as shown in Figure 5 The computer device includes a processor, a memory, an input / output interface (I / O) and a communication interface. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operating system and the computer program in the non-volatile storage medium to run. The input / output interface of the computer device is configured to exchange information between the processor and external devices. The communication interface of the computer device is configured to communicate with external terminals through network connection. The computer program is executed by the processor to implement a programming language migration running method.

[0183] Those skilled in the art can understand that Figure 5 The structure shown in the figure is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied. The specific computer device can include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0184] In one embodiment, a computer device is provided, which includes a memory and a processor, and the memory stores a computer program. The processor executes the computer program to implement the steps in the embodiments of the present application.

[0185] In one embodiment, a computer readable storage medium is provided, which stores a computer program. The computer program is executed by a processor to implement the steps in the embodiments of the present application.

[0186] In one embodiment, a computer program product is provided, which includes a computer program. The computer program is executed by a processor to implement the steps in the embodiments of the present application.

[0187] It should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of the country and region.

[0188] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing related hardware through a computer program. The computer program can be stored in a non-volatile computer readable storage medium, and when the computer program is executed, the processes of the above-mentioned embodiments of each method can be included. In the embodiments provided in the present application, any reference to memory, database or other medium 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 storage, high-density embedded non-volatile memory, resistive memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration but not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The database involved in the embodiments provided in the present application can include at least one of a relational database and a non-relational database. The non-relational database can include a distributed database based on a block chain, etc., without being limited thereto. The processor involved in the embodiments provided in the present application can be a general-purpose processor, a central processing unit, a graphics processing unit, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., without being limited thereto.

[0189] The technical features of the above embodiments can be combined in any way. To make the description concise, not all possible combinations of the technical features in the above embodiments are described, but as long as the combinations of the technical features do not exist contradictions, they should be considered as the scope of the present application.

[0190] The above-described embodiments are merely illustrative of several embodiments of the present application, and the description is relatively specific and detailed, but should not be understood as a limitation on the scope of the patent. It should be noted that for those skilled in the art, without departing from the concept of the present application, a number of modifications and improvements can be made, which are all within the scope of the present application. Therefore, the scope of protection of the present application should be subject to the appended claims.

Claims

1. A method of running a programming language migration, characterized by, The method comprises: initializing a Python running environment; the Python running environment has a target built-in function library pre-imported; the target built-in function library comprises built-in functions of Python language and Python built-in functions of the same name customized for built-in functions used by M language; in a conversion process of a current M language file to be converted in a current directory, at least one M language function is identified from M language script code included in the M language file, and each M language function is converted into a corresponding Python function according to a function syntax rule of Python; for script code other than the M language function in the M language script code, based on a preset M language syntax form and a corresponding relationship table of Python functions, code written in the preset M language syntax form in the script code is converted into a corresponding Python function; wherein the same M language syntax form has different functions or represents processing between different data types, and the converted Python function is executed according to the function internal context to differentiate the different functions or perform type checking in the function, and different processing logic is executed 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 running environment to realize the functions possessed by the M language script code.

2. The method of claim 1, wherein, The M language function is a customized function; the method further comprises: when each M language function is converted into a Python function, 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 taken out from the function code stack, and the taken-out code of the Python function is placed in front of a top code segment of the function code stack to realize the definition of the converted Python function.

3. The method of claim 2, wherein, The at least one M language function comprises an M language nested function; when the M language nested function is converted into a Python nested function, the method further comprises: a global scope stack is set in Python; each function in the Python nested function has a corresponding scope stack frame in the global scope stack, and the scope stack frame is used to store variables in a level of a scope of the function; a level identifier is added to the function in a call form of the function; the level identifier is used to represent a 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 level identifier of the scope of the function, the corresponding variable is searched from the determined scope stack frame, and if the variable is not found, the variable is searched step by step from the bottom of the stack based on the level identifier.

4. The method of claim 1, wherein, The method further comprises: Before the conversion of the M language file, built-in function names in an M language built-in function library corresponding to the target built-in function library are acquired; custom function names in the current directory and in the M language file are acquired; and a function name list is generated according to the acquired built-in function names and custom function names; In the conversion process of the current M language file to be converted in the current directory, at least one M language function is identified from M language script code included in the M language file, including: In the conversion process of the M language file, if an identifier is identified and a preset character is behind the identifier, it is determined whether the identifier is located in a current variable list or a function parameter list; If not, it is determined whether the identifier exists in the function name list; if yes, the identifier is identified as a function name of the M language function, and function parameters inside the M language function are added to the function parameter list; if not, the identifier is identified as a variable name, and a variable to be assigned is added to the variable list as a variable; if yes, the identifier is identified as a variable name; If an identifier is identified and a preset character and a special character are not behind the identifier, the identifier is identified as a variable name.

5. The method of claim 1, wherein, The script code other than the M language function includes code about variables; the variables include global variables and persistent variables; The code about the global variables is converted into a corresponding Python global variable function, and the code about the persistent variables is converted into a corresponding Python persistent variable function; For the Python global variable function, a global dictionary and a global variable name list stack are set in a main module in Python; the global dictionary is a global storage area, and the global variable name list stack is used to store global variable names declared in a 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 inside the Python persistent variable function; the persistent variable dictionary is used to store persistent variables in the Python persistent variable function.

6. The method of claim 1, wherein, The preset M language syntax form includes a syntax form representing assignment; a corresponding converted Python function of the syntax form representing assignment is a Python assignment function; a first initial parameter group of the Python assignment function includes a variable name, a scope level, and a default parameter used to implement assignment; The method further includes: In a process in which the Python file runs, if the Python assignment function is called, a first scope dictionary corresponding to the variable name in the first initial parameter group is determined based on the scope level; A parameter with a vector in the default parameter of the first initial parameter group is split into multiple groups of parameters without vectors, to obtain a first parameter group; Parameters in the first parameter group are processed a second time, to obtain a parameter group not including assignment data. The parameter group without assignment data is matched and combined with the assignment data to generate a target parameter group; Based on the target parameter group, assignment is performed, and the assignment result is updated to the first scope dictionary.

7. The method of claim 6, wherein, The target parameter group includes a type identifier parameter in the default parameters; The assignment based on the target parameter group includes: An initial reference corresponding to the variable name in the target parameter group is obtained; the initial reference indicates a variable value corresponding to the variable name; If the type identifier parameter represents array index reading or cell array index reading, index reading is performed based on the remaining parameters in the default parameters to read elements at the index; the initial reference is updated based on the elements at the index or cell values in the elements at the index; If the type identifier parameter represents structure attribute reading, dictionary attribute reading is performed 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 represents dimension deletion, dimension deletion processing is performed on the variable value indicated by the initial reference based on the remaining parameters in the default parameters to update the initial reference; For the updated initial reference, the updated reference is assigned based on the assignment data in the target parameter group.

8. The method of claim 1, wherein, The preset M language syntax form includes a syntax form representing a value; the converted Python function corresponding to the syntax form representing the value is a Python value function; a second initial parameter group corresponding to the Python value function includes a variable name, a scope level, and default parameters for implementing value reading; The method further includes: During the running of the Python file, if the Python value function is called, a second scope dictionary corresponding to the variable name in the second initial parameter group is determined based on the scope level; Parameters with vectors in the default parameters of the second initial parameter group are split into multiple groups of parameters without vectors to obtain a second parameter group; Based on the parameter determination result value dimension in the second parameter group, a corresponding empty value is pre-allocated based on the result value dimension; Based on the type identifier in the second parameter group and the scope dictionary, value reading is performed, and the pre-allocated corresponding empty value is updated based on the value reading result.

9. The method of claim 1, wherein, The M language function includes a target M language function with multiple return values; The conversion of each M language function into a corresponding Python function according to the Python function syntax rules includes: During the conversion of the target M language function, a first attribute and a second attribute are added to the converted target Python function, and the first attribute and the second attribute are assigned 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; In the case of only using part of the function return values, the function return values are stored in a temporary variable, and the temporary variable is assigned to ignore the return values not used in the function return values.

10. A programming language migration running apparatus characterized by comprising: The device includes: An environment initialization module is configured to initialize a Python running environment, wherein a target built-in function library is pre-imported into the Python running environment, and the target built-in function library includes built-in functions of the Python language and Python built-in functions of the same name which are customized for built-in functions used by the M language; A code conversion module is configured to identify at least one M language function from M language script code included in a current M language file to be converted in a conversion process of the current M language file, and convert each M language function into a corresponding Python function according to a function syntax rule of the Python language. The code conversion module is further configured to, for script code other than the M language function in the M language script code, convert code written in a preset M language syntax form in the script code into a corresponding Python function based on a preset corresponding relationship table between the preset M language syntax form and Python functions. The same M language syntax form has different functions or represents processing between different data types, and the corresponding converted Python function is executed according to the function internal context to differentiate the different functions or perform type checking in the function, and different processing logics are executed for different types. A running module is configured to generate a Python file converted from the M language file based on the converted Python function, and run the Python file in the Python running environment to implement functions possessed by the M language script code.

Citation Information

Patent Citations

  • Object packaging method for scientific computing language interpreter

    CN113835688A

  • Method and device for grammar mapping from SIMSCRIPT language to C language

    CN117193782A