WO2022222586A1 - 一种代码转换方法及装置 - Google Patents

一种代码转换方法及装置 Download PDF

Info

Publication number
WO2022222586A1
WO2022222586A1 PCT/CN2022/076476 CN2022076476W WO2022222586A1 WO 2022222586 A1 WO2022222586 A1 WO 2022222586A1 CN 2022076476 W CN2022076476 W CN 2022076476W WO 2022222586 A1 WO2022222586 A1 WO 2022222586A1
Authority
WO
WIPO (PCT)
Prior art keywords
variable
code
static
programming language
converted
Prior art date
Application number
PCT/CN2022/076476
Other languages
English (en)
French (fr)
Inventor
杨吉明
商楠
Original Assignee
北京字节跳动网络技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 北京字节跳动网络技术有限公司 filed Critical 北京字节跳动网络技术有限公司
Publication of WO2022222586A1 publication Critical patent/WO2022222586A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/51Source to source
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation

Definitions

  • the present disclosure relates to the field of computer technology, and in particular, to a code conversion method and device.
  • the dynamic programming language is easy for programmers to develop, but the code running efficiency is low, requiring a cold start, and it is not suitable for online applications.
  • the static programming language code has high running efficiency, but it is not easy to learn and write, and it is not suitable for development Therefore, applications can be developed using dynamic programming languages, and then compiled into static programming languages and pushed to online use, taking advantage of the advantages of the two programming languages.
  • the embodiments of the present disclosure provide at least one code conversion method and apparatus.
  • an embodiment of the present disclosure provides a code conversion method, including:
  • the code to be converted is the code to be converted into the static programming language
  • the target dynamic variable is a dynamic variable that is assigned multiple times in the code to be converted, and the assigned assignment type has multiple;
  • the determining of the target dynamic variable in the code to be converted includes:
  • a dynamic variable that is assigned multiple times and has multiple assigned assignment types is used as the target dynamic variable.
  • the static variables corresponding to each of the assignment types in the static programming language are created, including:
  • the static variable corresponding to the new assignment type under the static programming language is created, including:
  • the static variable corresponding to the new assignment type is created under the static programming language, further comprising:
  • the grammatical rules of the static programming language allow variables of different types to have the same name, create a static variable corresponding to the new assignment type in the static programming language, and add the type variable in the code that declares the static variable.
  • Variable identifier the name of the static variable is the same as the name of the target dynamic variable.
  • the method further includes:
  • the container variable is used as the static variable of the target dynamic variable after conversion, and the operation code corresponding to the container variable is converted into the operation function corresponding to the container variable; the operation function defines Operation code corresponding to each type in the container variable.
  • the method further includes:
  • the method further includes:
  • an embodiment of the present disclosure also provides a code conversion device, comprising:
  • an acquisition module used for acquiring the code to be converted written by the dynamic programming language;
  • the code to be converted is the code to be converted into the static programming language;
  • a determination module for determining the target dynamic variable in the code to be converted the target dynamic variable is a dynamic variable that is assigned multiple times in the code to be converted, and the assigned assignment type has multiple;
  • the conversion module is used to, in the conversion process, for the target dynamic variable, create a static variable corresponding to each of the assignment types in a static programming language, or create a container variable in the static programming language, in which the container variable is A type is defined for characterizing each of the assignment types in a dynamic programming language.
  • the determining module is specifically used for:
  • a dynamic variable that is assigned multiple times and has multiple assigned assignment types is used as the target dynamic variable.
  • the conversion module when the conversion module creates static variables corresponding to each of the assignment types in a static programming language, it is used for:
  • the conversion module when the conversion module creates a static variable corresponding to the new assignment type in a static programming language, it is used for:
  • the conversion module when the conversion module creates a static variable corresponding to the new assignment type in a static programming language, it is also used for:
  • the grammatical rules of the static programming language allow variables of different types to have the same name, create a static variable corresponding to the new assignment type in the static programming language, and add the type variable in the code that declares the static variable.
  • Variable identifier the name of the static variable is the same as the name of the target dynamic variable.
  • the conversion module is further used for:
  • the container variable is used as the static variable of the target dynamic variable after conversion, and the operation code corresponding to the container variable is converted into the operation function corresponding to the container variable; the operation function defines Operation code corresponding to each type in the container variable.
  • the apparatus further includes a compiling module for:
  • the apparatus further includes a compilation module modification module for:
  • embodiments of the present disclosure further provide an electronic device, including: a processor, a memory, and a bus, where the memory stores machine-readable instructions executable by the processor, and when the electronic device runs, the processing A bus communicates between the processor and the memory, and when the machine-readable instructions are executed by the processor, the first aspect or the steps in any possible implementation manner of the first aspect are performed.
  • embodiments of the present disclosure further provide a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and the computer program is executed by a processor to execute the first aspect, or any one of the first aspect. steps in one possible implementation.
  • an embodiment of the present disclosure further provides a computer program, comprising: instructions, when executed by a processor, the instructions cause the processor to execute the above-mentioned first aspect, or any possible implementation of the first aspect steps in the method.
  • embodiments of the present disclosure further provide a computer program product, including instructions, which, when executed by a processor, cause the processor to execute the above-mentioned first aspect, or any possible implementation of the first aspect steps in the method.
  • the code to be converted written in a dynamic programming language is obtained; the code to be converted is the code to be converted into a static programming language; the target dynamic variable in the code to be converted is determined;
  • the target dynamic variable is a dynamic variable that has been assigned multiple times in the code to be converted, and has multiple assigned assignment types; in the conversion process, for the target dynamic variable, create a static programming language with various values.
  • Static variables corresponding to the assignment types respectively, or a container variable in a static programming language is created, and a type used to represent each assignment type in the dynamic programming language is defined in the container variable.
  • FIG. 1 shows a flowchart of a code conversion method provided by an embodiment of the present disclosure
  • FIG. 2 shows a flowchart of another code conversion method provided by an embodiment of the present disclosure
  • FIG. 3 shows a schematic diagram of a code conversion device provided by an embodiment of the present disclosure
  • FIG. 4 shows a schematic diagram of an electronic device provided by an embodiment of the present disclosure.
  • the type of variables in dynamic programming languages is dynamic, and a variable may have multiple types.
  • static programming languages the type of variables is static, and a variable can only have one type.
  • the present disclosure provides a code conversion method and device.
  • creating static variables corresponding to each assignment type in a static programming language, or creating a container variable in a static programming language during the conversion process to meet the requirements of the static programming language
  • the following syntax requires variables so that the converted code can run correctly.
  • the execution body of the code conversion method provided by the embodiment of the present disclosure is generally a computer device with a certain computing capability, such as a terminal.
  • the device in some possible implementations, the transcoding method may be implemented by a processor invoking computer-readable instructions stored in the memory.
  • FIG. 1 is a flowchart of a code conversion method provided by an embodiment of the present disclosure, the method includes steps S101-S103, wherein:
  • the code to be converted is the code written in the dynamic programming language.
  • the user can write the required code in the dynamic programming language, it can be used as the code to be converted.
  • the terminal device can obtain the code to be converted and process it. , which translates to code written in a static programming language.
  • the dynamic programming language may include TypeScript language, JavaScript language, etc.
  • the static programming language may include Rust language, C++ language, and the like.
  • S102 Determine a target dynamic variable in the code to be converted; the target dynamic variable is a dynamic variable that is assigned multiple times in the code to be converted, and has multiple assigned assignment types.
  • the assignment type determines the actual type of the variable in the code. If a dynamic variable has multiple actual types in the code to be converted, it will be screened out as the target dynamic variable.
  • each dynamic variable included in the code to be converted can be determined based on the grammatical rules of the dynamic programming language, and then, based on the assignment code corresponding to each dynamic variable at the time of assignment, it is determined when each dynamic variable is assigned in the code to be converted For the corresponding assignment type, finally, among the dynamic variables, the dynamic variables that are assigned multiple times and that have multiple assigned assignment types are used as target dynamic variables.
  • the code to be converted may be converted according to the syntax difference between the dynamic programming language and the static programming language.
  • the code to be converted may be decomposed into a plurality of syntax units according to the syntax of the dynamic programming language, and the code to be converted may be decomposed into multiple syntax units according to the syntax of the dynamic programming language.
  • the diff transforms the source syntax unit of a dynamic programming language to the target syntax unit of a static programming language.
  • the conversion relationship between the source syntax unit and the target syntax unit can be determined.
  • the possible types of conversion relationships are as follows:
  • the obtained target syntax unit can be input into the syntax aggregation tool, and the target syntax unit can be aggregated into continuous code statements to obtain the converted target syntax unit. code.
  • variable-related syntactic units Due to the difference in variable types between dynamic programming languages and static programming languages, it is impossible to guarantee that the converted code can run normally based on semantic conversion, and the function of the converted code may also change. Therefore, during the conversion process, Special handling of variable-related syntactic units is required.
  • static variables corresponding to each assignment type in the static programming language can be created. These static variables can be independent of each other and have different types.
  • the target dynamic variables can be converted into corresponding static variables. , so that the converted code can run normally.
  • a static variable corresponding to the new assignment type in the static programming language may be created.
  • the actual type corresponding to the variable "d” includes numerical type and character type.
  • the assignment type has not changed, then no new static variable is created.
  • the grammar rules of the static programming language do not allow variables of different types to have the same name, such as the C++ language, then based on the preset variable name creation rules, create a static programming language corresponding to the new assignment type , so that no variable with the same name but of a different type exists in code converted to a static programming language.
  • the grammar rules of the static programming language allow variables of different types to have the same name, such as the Rust language, create a static variable corresponding to the new assignment type under the static programming language, and declare the static variable.
  • a variable type identifier is added to the code of the variable; the name of the static variable is the same as the name of the target dynamic variable.
  • a type used to represent each assignment type in a dynamic programming language may be defined in the container variable.
  • "String”, “Number”, and “Boolean” in the container variable are the assignment types in the dynamic programming language, that is, the variable types, and "(String)”, “ (f64)” and “(bool)” are the variable types in their corresponding static programming languages.
  • the type of the variable is defined as a container variable, and there is no calculation rule for container variables in the Rust language, supplementary code needs to be added to the converted code to define the calculation method of the container variable. , and then replace each calculation symbol in the dynamic programming language with the defined calculation method.
  • the container variable can be used as the static variable of the target dynamic variable after conversion, and the operation code corresponding to the container variable can be converted into the operation function corresponding to the container variable; the operation function defines the operation corresponding to each type in the container variable. code.
  • the defined calculation method can be as follows:
  • the target code obtained after the conversion can be compiled to obtain an executable product of the target code, and the executable product of the target code can be run by a computer device, and execute the corresponding target code. calculation logic.
  • the operation code corresponding to the assignment type of the container variable may be selected from the above-mentioned operation functions and executed. For example, when the code “Primitive(Number)Add'abc'” is executed, the ADD method in the above operation code is called. After judgment, the assignment type on the left side of the "ADD" character is Number, and the assignment type on the right side is the character String. In line with the situation of "(x,Primitive::String(y))" in the above ADD method, execute its corresponding code “format!(" ⁇ ",x,y).into()", put the variable Concatenated with the characters 'abc'.
  • the modified target code can be modified according to the modified instruction in response to the modified instruction for the converted target code to obtain the modified target code, and then the modified target code can be modified.
  • the code is compiled to obtain an executable product of the object code.
  • the modification instruction for the target code may be input by the user.
  • FIG. 2 it is a flowchart of another code conversion method provided by an embodiment of the present disclosure, wherein source code (code to be converted) A written in the TypeScript (TS) programming language or the JavaScript (JS) programming language is obtained , and then, convert A into an abstract syntax tree (abstract syntax code, AST) B.
  • the abstract syntax tree B can contain multiple syntax units, such as B1, B2, B3, B4 and BX in Figure 2, where BX It can be a set containing multiple syntax units.
  • the conversion relationship between the source syntax unit and the target syntax unit can be determined, and the TS Or the JS language syntax unit is converted into the Rust programming language syntax unit, for example, the TS or JS language syntax unit B1 is converted into the Rust programming language syntax unit R1, B2 and B3 are converted into R2 together, and B4 is converted into two Rust language syntaxes The units are R3 and R4 respectively.
  • Multiple TS or JS language syntax units BX can be converted into multiple Rust programming language syntax units RY.
  • the static variables corresponding to each assignment type in the above-mentioned static programming language are created, and the above-mentioned static variables are created.
  • the step of the container variable in the programming language can be used as a part of the step of converting the syntax unit to convert the syntax unit corresponding to the variable into the created static variable or container variable.
  • the code to be converted written in a dynamic programming language is obtained; the code to be converted is the code to be converted into a static programming language; the target dynamic variable in the code to be converted is determined;
  • the target dynamic variable is a dynamic variable that is assigned multiple times in the code to be converted, and has multiple assigned assignment types; in the conversion process, for the target dynamic variable, create a static programming language with each of the described Static variables corresponding to the assignment types respectively, or a container variable in a static programming language is created, in which the container variable defines a type used to represent each assignment type in the dynamic programming language.
  • static variables corresponding to each assignment type in the static programming language are created in the conversion process, or container variables in the static programming language are created, so as to meet the requirements of the grammar in the static language for variables, so that the conversion can be obtained after conversion.
  • the code runs correctly.
  • the writing order of each step does not mean a strict execution order but constitutes any limitation on the implementation process, and the specific execution order of each step should be based on its function and possible Internal logic is determined.
  • the embodiments of the present disclosure also provide a code conversion device corresponding to the code conversion method. The implementation of the method will not be repeated here.
  • FIG. 3 it is a schematic diagram of a code conversion apparatus provided by an embodiment of the present disclosure, and the apparatus includes:
  • the obtaining module 310 is used to obtain the code to be converted written in the dynamic programming language; the code to be converted is the code to be converted into the static programming language;
  • the determination module 320 is used to determine the target dynamic variable in the code to be converted; the target dynamic variable is a dynamic variable that has been assigned multiple times in the code to be converted, and has a plurality of assigned assignment types;
  • the conversion module 330 is configured to, in the conversion process, for the target dynamic variable, create a static variable corresponding to each of the assignment types in a static programming language, or create a container variable in a static programming language, the container variable Types used to characterize each of the assignment types in a dynamic programming language are defined in .
  • the determining module 320 is specifically configured to:
  • a dynamic variable that is assigned multiple times and has multiple assigned assignment types is used as the target dynamic variable.
  • the conversion module 330 when the conversion module 330 creates static variables corresponding to each of the assignment types in a static programming language, it is used to:
  • the conversion module 330 when the conversion module 330 creates a static variable corresponding to the new assignment type in a static programming language, it is used to:
  • the conversion module 330 when the conversion module 330 creates a static variable corresponding to the new assignment type in a static programming language, it is also used for:
  • the grammatical rules of the static programming language allow variables of different types to have the same name, create a static variable corresponding to the new assignment type in the static programming language, and add the type variable in the code that declares the static variable.
  • Variable identifier the name of the static variable is the same as the name of the target dynamic variable.
  • the conversion module 330 is further configured to:
  • the container variable is used as the static variable of the target dynamic variable after conversion, and the operation code corresponding to the container variable is converted into the operation function corresponding to the container variable; the operation function defines Operation code corresponding to each type in the container variable.
  • the apparatus further includes a compiling module for:
  • the apparatus further includes a compilation module modification module for:
  • an embodiment of the present disclosure further provides an electronic device 400.
  • the schematic structural diagram of the electronic device 400 provided by the embodiment of the present disclosure includes:
  • the data exchanged by the external memory 422 such as the hard disk the processor 41 exchanges data with the external memory 422 through the memory 421, and when the electronic device 400 is running, the processor 41 and the memory 42 communicate through the bus 43, so that The processor 41 executes the following instructions:
  • the code to be converted is the code to be converted into the static programming language
  • the target dynamic variable is a dynamic variable that is assigned multiple times in the code to be converted, and the assigned assignment type has multiple;
  • the determining the target dynamic variable in the code to be converted includes:
  • a dynamic variable that is assigned multiple times and has multiple assigned assignment types is used as the target dynamic variable.
  • the static variables corresponding to each of the assignment types in the static programming language are created, including:
  • the static variable corresponding to the new assignment type under the static programming language is created, including:
  • the static variable corresponding to the new assignment type under the static programming language is created, further comprising:
  • the grammatical rules of the static programming language allow variables of different types to have the same name, create a static variable corresponding to the new assignment type in the static programming language, and add the type variable in the code that declares the static variable.
  • Variable identifier the name of the static variable is the same as the name of the target dynamic variable.
  • the processor 41 is further configured to execute:
  • the container variable is used as the static variable of the target dynamic variable after conversion, and the operation code corresponding to the container variable is converted into the operation function corresponding to the container variable; the operation function defines Operation code corresponding to each type in the container variable.
  • the processor 41 is further configured to execute:
  • processor 41 is further configured to execute:
  • Embodiments of the present disclosure further provide a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is run by a processor, the steps of the code conversion method described in the foregoing method embodiments are executed.
  • the storage medium may be a volatile or non-volatile computer-readable storage medium.
  • Embodiments of the present disclosure further provide a computer program product, where the computer program product carries program codes, and the instructions included in the program codes can be used to execute the steps of the code conversion methods described in the foregoing method embodiments. For details, please refer to the foregoing methods. The embodiments are not repeated here.
  • the above-mentioned computer program product can be specifically implemented by means of hardware, software or a combination thereof.
  • the computer program product is embodied as a computer storage medium, and in another optional embodiment, the computer program product is embodied as a software product, such as a software development kit (Software Development Kit, SDK), etc. Wait.
  • the units described as separate components may or may not be physically separated, and components shown as units may or may not be physical units, that is, may be located in one place, or may be distributed to multiple network units. Some or all of the units may be selected according to actual needs to implement the solution of this embodiment.
  • each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically alone, or two or more units may be integrated into one unit.
  • the functions, if implemented in the form of software functional units and sold or used as stand-alone products, may be stored in a processor-executable non-volatile computer-readable storage medium.
  • the computer software products are stored in a storage medium, including Several instructions are used to cause a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present disclosure.
  • the aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), magnetic disk or optical disk and other media that can store program codes .

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

本公开提供了一种代码转换方法及装置,该方法首先获取由动态编程语言编写的待转换代码;然后确定所述待转换代码中的目标动态变量;之后,在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量。本公开实施例通过在转换过程中创建静态编程语言下与各个赋值类型对应的静态变量,或创建静态编程语言下的容器变量,以满足在静态语言下的语法对变量的需求,使转换后得到的代码能够正确运行。

Description

一种代码转换方法及装置
本申请要求于2021年4月19日递交、申请号为202110420845.6、名称为“一种代码转换方法及装置”的中国专利申请的优先权,其全部内容通过引用并入本文。
技术领域
本公开涉及计算机技术领域,具体而言,涉及一种代码转换方法及装置。
背景技术
在代码开发场景中,动态编程语言易于程序员进行开发,但代码运行效率较低、需要冷启动,不适于线上应用,静态编程语言代码的运行效率高,但不易于学习编写,不适于开发场景,因此,可以利用动态编程语言开发应用,再将其编译为静态编程语言推至线上使用,发挥两种编程语言的优势。
发明内容
本公开实施例至少提供一种代码转换方法及装置。
第一方面,本公开实施例提供了一种代码转换方法,包括:
获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;
确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;
在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
一种可选的实施方式中,所述确定所述待转换代码中的目标动态变量,包括:
基于所述动态编程语言的语法规则,确定所述待转换代码中包含的各个动态变量;
基于所述各个动态变量在被赋值时对应的赋值代码,确定所述各个动态变量在所述待转换代码中被赋值时对应的赋值类型;
将所述各个动态变量中,被多次赋值、且被赋值的赋值类型存在多个的动态变量作为所述目标动态变量。
一种可选的实施方式中,所述创建静态编程语言下与各个所述赋值类型分别对应的静态变量,包括:
在检测到当前转换的目标动态变量被赋值的赋值类型变化时,创建静态编程语言下与新的赋值类型对应的静态变量。
一种可选的实施方式中,所述创建静态编程语言下与所述新的赋值类型对应的静态变量,包括:
若所述静态编程语言的语法规则不允许类型不同的变量具有相同的名称,则基于预设的变量名称创建规则,创建静态编程语言下与所述新的赋值类型对应的静态变量,以使转换为静态编程语言的代码中不存在具有相同名称且类型不同的变量。
一种可选的实施方式中,所述创建静态编程语言下与所述新的赋值类型对应的静态变量,还包括:
若所述静态编程语言的语法规则允许类型不同的变量具有相同的名称,则创建静态编程语言下与所述新的赋值类型对应的静态变量,并在声明所述静态变量的代码中加入类型可变标识;所述静态变量的名称与所述目标动态变量的名称相同。
一种可选的实施方式中,在创建静态编程语言下的容器变量之后,所 述方法还包括:
在转换过程中,将所述容器变量作为所述目标动态变量在转换后的静态变量,并将所述容器变量对应的运算代码转换为所述容器变量对应的运算函数;所述运算函数定义了所述容器变量中每个类型对应的运算代码。
一种可选的实施方式中,在所述待转换代码转换为静态编程语言之后,所述方法还包括:
对转换后得到的目标代码进行编译,得到所述目标代码的可执行产物;
在执行包含所述容器变量的可执行产物时,根据所述容器变量对应的赋值类型,从所述运算函数中选取并执行与所述容器变量的赋值类型对应的运算代码。
一种可选的实施方式中,所述方法还包括:
响应针对转换后得到的目标代码的修改指令,对转换后得到的目标代码进行修改,得到修改后的目标代码;
对修改后的目标代码进行编译,得到所述目标代码的可执行产物。
第二方面,本公开实施例还提供一种代码转换装置,包括:
获取模块,用于获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;
确定模块,用于确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;
转换模块,用于在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
一种可选的实施方式中,所述确定模块具体用于:
基于所述动态编程语言的语法规则,确定所述待转换代码中包含的各个动态变量;
基于所述各个动态变量在被赋值时对应的赋值代码,确定所述各个动态变量在所述待转换代码中被赋值时对应的赋值类型;
将所述各个动态变量中,被多次赋值、且被赋值的赋值类型存在多个的动态变量作为所述目标动态变量。
一种可选的实施方式中,所述转换模块在创建静态编程语言下与各个所述赋值类型分别对应的静态变量时,用于:
在检测到当前转换的目标动态变量被赋值的赋值类型变化时,创建静态编程语言下与新的赋值类型对应的静态变量。
一种可选的实施方式中,所述转换模块在创建静态编程语言下与所述新的赋值类型对应的静态变量时,用于:
若所述静态编程语言的语法规则不允许类型不同的变量具有相同的名称,则基于预设的变量名称创建规则,创建静态编程语言下与所述新的赋值类型对应的静态变量,以使转换为静态编程语言的代码中不存在具有相同名称且类型不同的变量。
一种可选的实施方式中,所述转换模块在创建静态编程语言下与所述新的赋值类型对应的静态变量时,还用于:
若所述静态编程语言的语法规则允许类型不同的变量具有相同的名称,则创建静态编程语言下与所述新的赋值类型对应的静态变量,并在声明所述静态变量的代码中加入类型可变标识;所述静态变量的名称与所述目标动态变量的名称相同。
一种可选的实施方式中,在创建静态编程语言下的容器变量之后,所述转换模块还用于:
在转换过程中,将所述容器变量作为所述目标动态变量在转换后的静 态变量,并将所述容器变量对应的运算代码转换为所述容器变量对应的运算函数;所述运算函数定义了所述容器变量中每个类型对应的运算代码。
一种可选的实施方式中,所述装置还包括编译模块,用于:
对转换后得到的目标代码进行编译,得到所述目标代码的可执行产物;
在执行包含所述容器变量的可执行产物时,根据所述容器变量对应的赋值类型,从所述运算函数中选取并执行与所述容器变量的赋值类型对应的运算代码。
一种可选的实施方式中,装置还包括编译模块修改模块,用于:
响应针对转换后得到的目标代码的修改指令,对转换后得到的目标代码进行修改,得到修改后的目标代码;
对修改后的目标代码进行编译,得到所述目标代码的可执行产物。
第三方面,本公开实施例还提供一种电子设备,包括:处理器、存储器和总线,所述存储器存储有所述处理器可执行的机器可读指令,当电子设备运行时,所述处理器与所述存储器之间通过总线通信,所述机器可读指令被所述处理器执行时执行上述第一方面,或第一方面中任一种可能的实施方式中的步骤。
第四方面,本公开实施例还提供一种计算机可读存储介质,该计算机可读存储介质上存储有计算机程序,该计算机程序被处理器运行时执行上述第一方面,或第一方面中任一种可能的实施方式中的步骤。
第五方面,本公开实施例还提供一种计算机程序,包括:指令,所述指令当由处理器执行时使所述处理器执行上述第一方面,或第一方面中任一种可能的实施方式中的步骤。
第六方面,本公开实施例还提供一种计算机程序产品,包括指令,所述指令当由处理器执行时使所述处理器执行上述第一方面,或第一方面中任一种可能的实施方式中的步骤。
本公开实施例提供的代码转换方法及装置,获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
为使本公开的特征和优点能更明显易懂,下文特举较佳实施例,并配合所附附图,作详细说明如下。
附图说明
为了更清楚地说明本公开实施例的技术方案,下面将对实施例中所需要使用的附图作简单地介绍,此处的附图被并入说明书中并构成本说明书中的一部分,这些附图示出了符合本公开的实施例,并与说明书一起用于说明本公开的技术方案。应当理解,以下附图仅示出了本公开的某些实施例,因此不应被看作是对范围的限定,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他相关的附图。
图1示出了本公开实施例所提供的一种代码转换方法的流程图;
图2示出了本公开实施例所提供的另一种代码转换方法的流程图;
图3示出了本公开实施例所提供的一种代码转换装置的示意图;
图4示出了本公开实施例所提供的一种电子设备的示意图。
具体实施方式
为使本公开实施例的技术方案和优点更加清楚,下面将结合本公开实施例中附图,对本公开实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本公开一部分实施例,而不是全部的实施例。通常 在此处附图中描述和示出的本公开实施例的组件可以以各种不同的配置来布置和设计。因此,以下对在附图中提供的本公开的实施例的详细描述并非旨在限制要求保护的本公开的范围,而是仅仅表示本公开的选定实施例。基于本公开的实施例,本领域技术人员在没有做出创造性劳动的前提下所获得的所有其他实施例,都属于本公开保护的范围。
应注意到:相似的标号和字母在下面的附图中表示类似项,因此,一旦某一项在一个附图中被定义,则在随后的附图中不需要对其进行进一步定义和解释。
本文中术语“和/或”,仅仅是描述一种关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。另外,本文中术语“至少一种”表示多种中的任意一种或多种中的至少两种的任意组合,例如,包括A、B、C中的至少一种,可以表示包括从A、B和C构成的集合中选择的任意一个或多个元素。
动态编程语言中的变量的类型是动态的,一个变量可能存在多种类型,静态编程语言中变量的类型是静态的,一个变量只能存在一种类型。在将由动态编程语言编写的代码转换为由静态编程语言编程的代码时,难以对变量进行合适的处理以精确的将动态的变量转换为正确的静态变量,容易导致转换得到的代码无法正常运行,因此,转换过程通常由人工进行,要求业务人员对两种编程语言具有较高的了解程度。
基于上述研究,本公开提供了一种代码转换方法及装置,通过在转换过程中创建静态编程语言下与各个赋值类型对应的静态变量,或创建静态编程语言下的容器变量,以满足在静态语言下的语法对变量的需求,使转换后得到的代码能够正确运行。
为便于对本实施例进行理解,首先对本公开实施例所公开的一种代码转换方法进行详细介绍,本公开实施例所提供的代码转换方法的执行主体一般为具有一定计算能力的计算机设备,如终端设备,在一些可能的实现 方式中,该代码转换方法可以通过处理器调用存储器中存储的计算机可读指令的方式来实现。
参见图1所示,为本公开实施例提供的代码转换方法的流程图,所述方法包括步骤S101~S103,其中:
S101、获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码。
该步骤中,待转换代码为由动态编程语言编写的代码,用户可以利用动态编程语言编写好需要的代码后,将其作为待转换代码,终端设备可以获取这些待转换代码,并对其进行处理,使其转换为由静态编程语言编写的代码。
其中,动态编程语言可以包括TypeScript语言、JavaScript语言等,静态编程语言可以包括Rust语言、C++语言等。
S102、确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量。
由于待转换代码中的动态变量可能存在多种类型,而静态编程语言下的同名变量不能出现不同的类型,因此,需要对存在多种类型的变量进行特殊处理,需要从待转换代码中筛选出存在多种类型的目标动态变量。
示例性的,在获取到待转换代码后,确定待转换代码中的各个赋值代码,比如,“d=10”即为一段赋值代码,在确定赋值代码后,可以根据赋值代码中变量被赋值的赋值类型,确定变量在代码中的实际类型,若一动态变量在待转换代码中存在多个实际类型,则将其筛选出来作为目标动态变量。
具体的,可以基于动态编程语言的语法规则,确定出待转换代码中包含的各个动态变量,然后,基于各个动态变量在赋值时对应的赋值代码,确定各个动态变量在待转换代码中被赋值时对应的赋值类型,最后,将各 个动态变量中,被多次赋值、且被赋值的赋值类型存在多个的动态变量作为目标动态变量。
示例性的,在待转换代码中,若依次出现了“d=10”、“d=d+1”及“d=‘abc’”三段赋值代码,在“d=10”中,赋值的“10”为数值类型,则可以确定变量d此时的实际类型为数值类型,在“d=d+1”中,赋值的“d+1”中“1”为数值类型,“d”为变量自身,此时变量d的实际类型为数值类型,在“d=‘abc’”中,被赋值的“‘abc’”为字符类型,则变量d此时的实际类型为字符类型,在上述代码中,变量d先后被赋值多次,且被赋值的赋值类型为两种,则可以将变量d作为目标动态变量。
S103、在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
该步骤中,可以按照动态编程语言及静态编程语言之间的语法差异,对待转换代码进行转换,转换过程中,可以按照动态编程语言的语法将待转换代码分解为多个语法单元,并按照语法差异将动态编程语言的源语法单元转换为静态编程语言的目标语法单元。
这里,根据动态编程语言及静态编程语言之间的语法差异,按照语义等价标准,可以确定源语法单元与目标语法单元之间的转换关系,转换关系可能包含的种类如下:
(1)一对一关系,一个源语法单元转换为一个目标语法单元。
(2)多对一关系,多个源语法单元转换为一个目标语法单元。
(3)一对多关系,一个源语法单元转换为多个目标语法单元。
(4)多对多关系,多个源语法单元转换为多个目标语法单元。
在将待转换代码的源语法单元转换为静态编程语言的目标语法单元 后,可以将得到的目标语法单元输入至语法汇总工具中,将目标语法单元汇总为连续的代码语句,得到转换后的目标代码。
这里,由于动态编程语言与静态编程语言在变量类型上的不同,单纯的根据语义转换无法保证转换后的代码能够正常运行,其转换后代码的功能也有可能会变化,因此,在转换过程中,需要对变量相关的语法单元进行特殊处理。
在转换过程中,可以创建静态编程语言下与各个赋值类型分别对应的静态变量,这些静态变量可以是相互独立的,具有不同的类型,在转换过程中可以将目标动态变量转换为对应的静态变量,使转换后得到的代码能够正常运行。
具体的,在转换过程中,可以在检测到当前转换的目标动态变量被赋值的赋值类型变化时,创建静态编程语言下与新的赋值类型对应的静态变量。
比如,在JS代码片段“d=10;d=d+1;d=‘abc’”中,变量“d”对应的实际类型包括数值类型和字符类型,在转换过程中,在转换“d=d+1”时,赋值类型并未变化,则不创建新的静态变量,在转换“d=‘abc’”时变量d对应的赋值类型从数值类型变为字符类型,因此可以生成静态变量e,转换为C++代码后可以为“d=10;d=d+1;e=‘abc’”其中,“d”与“e”即为生成的静态变量。
在静态编程语言中,有允许类型不同的变量具有相同名称的语言,也存在不允许类型不同的变量具有相同名称的语言,因此,可以检测转化的静态语言是否允许类型不同的变量具有相同的名称,再根据检测结果采取不同的处理方式。
具体的,若所述静态编程语言的语法规则不允许类型不同的变量具有相同的名称,如C++语言,则基于预设的变量名称创建规则,创建静态编程语言下与所述新的赋值类型对应的静态变量,以使转换为静态编程语言 的代码中不存在具有相同名称且类型不同的变量。
进一步的,若所述静态编程语言的语法规则允许类型不同的变量具有相同的名称,如Rust语言,则创建静态编程语言下与所述新的赋值类型对应的静态变量,并在声明所述静态变量的代码中加入类型可变标识;所述静态变量的名称与所述目标动态变量的名称相同。
该步骤中,类型可变标识可以添加在变量首次声明的位置,比如,若待转换的JS代码为“var d=10;d=d+1;d=‘abc’”,则转换后的Rust代码可以为“let mut d=10;d=d+1;let d=‘abc’”,其中,“let”为声明字段,由“var”转换而来,在“d=d+1”字段中,变量“d”的类型没有变化,因此该变量“d”与“d=10”中的变量“d”是同一静态变量,不生成新的静态变量,而在“d=‘abc’”中,变量“d”的类型变为了字符类型,因此,需要生成新的静态变量,在转换的代码中加入声明新的变量的“let”字段,由于上述代码中存在多个时代的变量“d”,因此,在首次声明变量“d”时需要加入“mut”字段,表示变量“d”是可变的“mutable”。
进一步的,在创建静态编程语言下的容器变量的方式中,上述容器变量中可以定义了用于表征动态编程语言下的每个赋值类型的类型。
示例性的,若待转换代码为“var d=10;d+10;d-2;d=d+‘abc’”其对应的Rust代码可以为:“let d:Primitive=10;Primitive(Number)Add 10;Primitive(Number)Minus 2;Primitive(Number)Add‘abc’”。
其中,“Primitive”即为生成的容器变量,定义容器变量的代码可以为:
Figure PCTCN2022076476-appb-000001
Figure PCTCN2022076476-appb-000002
其中,容器变量中“String”、“Number”、“Boolean”为动态编程语言下的各个赋值类型,也即变量类型,而与上述动态编程语言下的赋值类型对应的“(String)”、“(f64)”、“(bool)”则为其对应的静态编程语言下的变量类型。
在上述转化后的Rust代码中,由于将变量的类型定义为了容器变量,而Rust语言中并没有关于容器变量的计算规则,需要向转换的代码中添加补充代码,用于定义容器变量的计算方法,再用定义的计算方法替换动态编程语言下的各个计算符号。可以在转换过程中,将容器变量作为目标动态变量在转换后的静态变量,并将容器变量对应的运算代码转换为容器变量对应的运算函数;运算函数定义了容器变量中每个类型对应的运算代码。
示例性的,定义的计算方法可以如下:
Figure PCTCN2022076476-appb-000003
上述定义的计算方法中定义了“ADD”方法在不同类型下对应的计算 规则,将运算符“+”转换为“ADD”方法,可以实现变量“b”在不同类型下的计算。
进一步的,在待转换代码转换为静态编程语言后,可以对转换后得到的目标代码进行编译,得到目标代码的可执行产物,目标代码的可执行产物能够被计算机设备运行,并执行目标代码对应的计算逻辑。
在执行包含容器变量的可执行产物时,可以根据容器变量对应的赋值类型,从上述运算函数中选取并执行与所述容器变量的赋值类型对应的运算代码。比如,在执行代码“Primitive(Number)Add‘abc’”时,调用上述运算代码中的ADD方法,经过判断,“ADD”字符左侧的赋值类型为Number,右侧的赋值类型为字符String,符合上述ADD方法中“(x,Primitive::String(y))”的情况,则执行其对应的代码“format!("{}{}",x,y).into()”,将变量与字符‘abc’拼接。
进一步的,在得到转换后的目标代码后,可以响应针对转换后的目标代码的修改指令,根据修改指令对转换后得到的目标代码进行修改,得到修改后的目标代码,再对修改后的目标代码进行编译,得到目标代码的可执行产物。
其中,针对目标代码的修改指令可以是用户输入的。
参见图2所示,为本公开实施例提供的另一种代码转换方法的流程图,其中,获取由TypeScript(TS)编程语言或JavaScript(JS)编程语言编写的源代码(待转换代码)A,然后,将A转换为抽象语法树(abstract syntax code,AST)B,抽象语法树B中可以包含多个语法单元,如图2中的B1、B2、B3、B4及BX等,其中,BX可以为包含多个语法单元的集合,之后,可以按照动态编程语言及静态编程语言之间的语法差异,按照语义等价标准,可以确定源语法单元与目标语法单元之间的转换关系,将TS或JS语言语法单元转换为Rust编程语言的语法单元,比如,TS或JS语言语法单元B1转换为了Rust编程语言的语法单元R1,B2与B3共同转为了R2, B4则转换为了两个Rust语言语法单元,分别为R3及R4,多个TS或JS语言语法单元BX可以转换为多个Rust编程语言的语法单元RY,上述创建静态编程语言下与各个赋值类型分别对应的静态变量,以及上述创建静态编程语言下的容器变量的步骤,可以作为转换语法单元步骤的一部分,将变量对应的语法单元转换为创建的静态变量或容器变量。
本公开实施例提供的代码转换方法,获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。本公开实施例通过在转换过程中创建静态编程语言下与各个赋值类型对应的静态变量,或创建静态编程语言下的容器变量,以满足在静态语言下的语法对变量的需求,使转换后得到的代码能够正确运行。
本领域技术人员可以理解,在具体实施方式的上述方法中,各步骤的撰写顺序并不意味着严格的执行顺序而对实施过程构成任何限定,各步骤的具体执行顺序应当以其功能和可能的内在逻辑确定。
基于同一发明构思,本公开实施例中还提供了与代码转换方法对应的代码转换装置,由于本公开实施例中的装置的原理与本公开实施例上述代码转换方法相似,因此装置的实施可以参见方法的实施,重复之处不再赘述。
参照图3所示,为本公开实施例提供的一种代码转换装置的示意图,所述装置包括:
获取模块310,用于获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;
确定模块320,用于确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;
转换模块330,用于在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
一种可选的实施方式中,所述确定模块320具体用于:
基于所述动态编程语言的语法规则,确定所述待转换代码中包含的各个动态变量;
基于所述各个动态变量在被赋值时对应的赋值代码,确定所述各个动态变量在所述待转换代码中被赋值时对应的赋值类型;
将所述各个动态变量中,被多次赋值、且被赋值的赋值类型存在多个的动态变量作为所述目标动态变量。
一种可选的实施方式中,所述转换模块330在创建静态编程语言下与各个所述赋值类型分别对应的静态变量时,用于:
在检测到当前转换的目标动态变量被赋值的赋值类型变化时,创建静态编程语言下与新的赋值类型对应的静态变量。
一种可选的实施方式中,所述转换模块330在创建静态编程语言下与所述新的赋值类型对应的静态变量时,用于:
若所述静态编程语言的语法规则不允许类型不同的变量具有相同的名称,则基于预设的变量名称创建规则,创建静态编程语言下与所述新的赋值类型对应的静态变量,以使转换为静态编程语言的代码中不存在具有相同名称且类型不同的变量。
一种可选的实施方式中,所述转换模块330在创建静态编程语言下与所述新的赋值类型对应的静态变量时,还用于:
若所述静态编程语言的语法规则允许类型不同的变量具有相同的名称,则创建静态编程语言下与所述新的赋值类型对应的静态变量,并在声明所述静态变量的代码中加入类型可变标识;所述静态变量的名称与所述目标动态变量的名称相同。
一种可选的实施方式中,在创建静态编程语言下的容器变量之后,所述转换模块330还用于:
在转换过程中,将所述容器变量作为所述目标动态变量在转换后的静态变量,并将所述容器变量对应的运算代码转换为所述容器变量对应的运算函数;所述运算函数定义了所述容器变量中每个类型对应的运算代码。
一种可选的实施方式中,所述装置还包括编译模块,用于:
对转换后得到的目标代码进行编译,得到所述目标代码的可执行产物;
在执行包含所述容器变量的可执行产物时,根据所述容器变量对应的赋值类型,从所述运算函数中选取并执行与所述容器变量的赋值类型对应的运算代码。
一种可选的实施方式中,装置还包括编译模块修改模块,用于:
响应针对转换后得到的目标代码的修改指令,对转换后得到的目标代码进行修改,得到修改后的目标代码;
对修改后的目标代码进行编译,得到所述目标代码的可执行产物。
关于装置中的各模块的处理流程、以及各模块之间的交互流程的描述可以参照上述方法实施例中的相关说明,这里不再详述。
对应于图1中的代码转换方法,本公开实施例还提供了一种电子设备400,如图4所示,为本公开实施例提供的电子设备400结构示意图,包括:
处理器41、存储器42、和总线43;存储器42用于存储执行指令,包括内存421和外部存储器422;这里的内存421也称内存储器,用于暂时存放处理器41中的运算数据,以及与硬盘等外部存储器422交换的数据,处理器41通过内存421与外部存储器422进行数据交换,当所述电子设备400运行时,所述处理器41与所述存储器42之间通过总线43通信,使得所述处理器41执行以下指令:
获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;
确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;
在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
一种可选的实施方式中,所述处理器41执行的指令中,所述确定所述待转换代码中的目标动态变量,包括:
基于所述动态编程语言的语法规则,确定所述待转换代码中包含的各个动态变量;
基于所述各个动态变量在被赋值时对应的赋值代码,确定所述各个动态变量在所述待转换代码中被赋值时对应的赋值类型;
将所述各个动态变量中,被多次赋值、且被赋值的赋值类型存在多个的动态变量作为所述目标动态变量。
一种可选的实施方式中,所述处理器41执行的指令中,所述创建静态编程语言下与各个所述赋值类型分别对应的静态变量,包括:
在检测到当前转换的目标动态变量被赋值的赋值类型变化时,创建静态编程语言下与新的赋值类型对应的静态变量。
一种可选的实施方式中,所述处理器41执行的指令中,所述创建静态编程语言下与所述新的赋值类型对应的静态变量,包括:
若所述静态编程语言的语法规则不允许类型不同的变量具有相同的名称,则基于预设的变量名称创建规则,创建静态编程语言下与所述新的赋值类型对应的静态变量,以使转换为静态编程语言的代码中不存在具有相同名称且类型不同的变量。
一种可选的实施方式中,所述处理器41执行的指令中,所述创建静态编程语言下与所述新的赋值类型对应的静态变量,还包括:
若所述静态编程语言的语法规则允许类型不同的变量具有相同的名称,则创建静态编程语言下与所述新的赋值类型对应的静态变量,并在声明所述静态变量的代码中加入类型可变标识;所述静态变量的名称与所述目标动态变量的名称相同。
一种可选的实施方式中,在执行创建静态编程语言下的容器变量之后,所述处理器41还用于执行:
在转换过程中,将所述容器变量作为所述目标动态变量在转换后的静态变量,并将所述容器变量对应的运算代码转换为所述容器变量对应的运算函数;所述运算函数定义了所述容器变量中每个类型对应的运算代码。
一种可选的实施方式中,在所述待转换代码转换为静态编程语言之后,所述处理器41还用于执行:
对转换后得到的目标代码进行编译,得到所述目标代码的可执行产物;
在执行包含所述容器变量的可执行产物时,根据所述容器变量对应的赋值类型,从所述运算函数中选取并执行与所述容器变量的赋值类型对应的运算代码。
一种可选的实施方式中,所述处理器41还用于执行:
响应针对转换后得到的目标代码的修改指令,对转换后得到的目标代码进行修改,得到修改后的目标代码;
对修改后的目标代码进行编译,得到所述目标代码的可执行产物。
本公开实施例还提供一种计算机可读存储介质,该计算机可读存储介质上存储有计算机程序,该计算机程序被处理器运行时执行上述方法实施例中所述的代码转换方法的步骤。其中,该存储介质可以是易失性或非易失的计算机可读取存储介质。
本公开实施例还提供一种计算机程序产品,该计算机程序产品承载有程序代码,所述程序代码包括的指令可用于执行上述方法实施例中所述的代码转换方法的步骤,具体可参见上述方法实施例,在此不再赘述。
其中,上述计算机程序产品可以具体通过硬件、软件或其结合的方式实现。在一个可选实施例中,所述计算机程序产品具体体现为计算机存储介质,在另一个可选实施例中,计算机程序产品具体体现为软件产品,例如软件开发包(Software Development Kit,SDK)等等。
所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,上述描述的系统和装置的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。在本公开所提供的几个实施例中,应该理解到,所揭露的系统、装置和方法,可以通过其它的方式实现。以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式,又例如,多个单元或组件可以结合或者可以集成到另一个系统,或一些特征可以忽略,或不执行。另一点,所显示或讨论的相互之间的耦合或直接耦合或通信连接可以是通过一些通信接口,装置或单元的间接耦合或通信连接,可以是电性,机械或其它的形式。
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的, 作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案。
另外,在本公开各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。
所述功能如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个处理器可执行的非易失的计算机可读取存储介质中。基于这样的理解,本公开的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本公开各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:U盘、移动硬盘、只读存储器(Read-Only Memory,ROM)、随机存取存储器(Random Access Memory,RAM)、磁碟或者光盘等各种可以存储程序代码的介质。
最后应说明的是:以上所述实施例,仅为本公开的具体实施方式,用以说明本公开的技术方案,而非对其限制,本公开的保护范围并不局限于此,尽管参照前述实施例对本公开进行了详细的说明,本领域的普通技术人员应当理解:任何熟悉本技术领域的技术人员在本公开揭露的技术范围内,其依然可以对前述实施例所记载的技术方案进行修改或可轻易想到变化,或者对其中部分技术特征进行等同替换;而这些修改、变化或者替换,并不使相应技术方案的本质脱离本公开实施例技术方案的精神和范围,都应涵盖在本公开的保护范围之内。因此,本公开的保护范围应所述以权利要求的保护范围为准。

Claims (13)

  1. 一种代码转换方法,包括:
    获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;
    确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;
    在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
  2. 根据权利要求1所述的方法,其中,所述确定所述待转换代码中的目标动态变量,包括:
    基于所述动态编程语言的语法规则,确定所述待转换代码中包含的各个动态变量;
    基于所述各个动态变量在被赋值时对应的赋值代码,确定所述各个动态变量在所述待转换代码中被赋值时对应的赋值类型;
    将所述各个动态变量中,被多次赋值、且被赋值的赋值类型存在多个的动态变量作为所述目标动态变量。
  3. 根据权利要求1所述的方法,其中,所述创建静态编程语言下与各个所述赋值类型分别对应的静态变量,包括:
    在检测到当前转换的目标动态变量被赋值的赋值类型变化时,创建静态编程语言下与所述新的赋值类型对应的静态变量。
  4. 根据权利要求3所述的方法,其中,所述创建静态编程语言下与所述新的赋值类型对应的静态变量,包括:
    若所述静态编程语言的语法规则不允许类型不同的变量具有相同的名 称,则基于预设的变量名称创建规则,创建静态编程语言下与所述新的赋值类型对应的静态变量,以使转换为静态编程语言的代码中不存在具有相同名称且类型不同的变量。
  5. 根据权利要求4所述的方法,其中,所述创建静态编程语言下与所述新的赋值类型对应的静态变量,还包括:
    若所述静态编程语言的语法规则允许类型不同的变量具有相同的名称,则创建静态编程语言下与所述新的赋值类型对应的静态变量,并在声明所述静态变量的代码中加入类型可变标识;所述静态变量的名称与所述目标动态变量的名称相同。
  6. 根据权利要求1所述的方法,其中,在创建静态编程语言下的容器变量之后,所述方法还包括:
    在转换过程中,将所述容器变量作为所述目标动态变量在转换后的静态变量,并将所述容器变量对应的运算代码转换为所述容器变量对应的运算函数,所述运算函数定义了所述容器变量中每个类型对应的运算代码。
  7. 根据权利要求6所述的方法,其中,在所述待转换代码转换为静态编程语言之后,所述方法还包括:
    对转换后得到的目标代码进行编译,得到所述目标代码的可执行产物;
    在执行包含所述容器变量的可执行产物时,根据所述容器变量对应的赋值类型,从所述运算函数中选取并执行与所述容器变量的赋值类型对应的运算代码。
  8. 根据权利要求1所述的方法,还包括:
    响应针对转换后得到的目标代码的修改指令,对转换后得到的目标代码进行修改,得到修改后的目标代码;
    对修改后的目标代码进行编译,得到所述目标代码的可执行产物。
  9. 一种代码转换装置,包括:
    获取模块,用于获取由动态编程语言编写的待转换代码;所述待转换代码为待转换为静态编程语言的代码;
    确定模块,用于确定所述待转换代码中的目标动态变量;所述目标动态变量为在所述待转换代码中被多次赋值、且被赋值的赋值类型存在多个的动态变量;
    转换模块,用于在转换过程中,针对所述目标动态变量,创建静态编程语言下与各个所述赋值类型分别对应的静态变量,或者,创建静态编程语言下的容器变量,所述容器变量中定义了用于表征动态编程语言下的每个所述赋值类型的类型。
  10. 一种电子设备,包括:处理器、存储器和总线,所述存储器存储有所述处理器可执行的机器可读指令,当电子设备运行时,所述处理器与所述存储器之间通过总线通信,所述机器可读指令被所述处理器执行时执行如权利要求1至8任一所述的代码转换方法的步骤。
  11. 一种计算机可读存储介质,该计算机可读存储介质上存储有计算机程序,该计算机程序被处理器运行时执行如权利要求1至8任意一项所述的代码转换方法的步骤。
  12. 一种计算机程序,包括:
    指令,所述指令当由处理器执行时使所述处理器执行根据权利要求1至8中任一项所述的代码转换方法的步骤。
  13. 一种计算机程序产品,包括指令,所述指令当由处理器执行时使所述处理器执行根据权利要求1至8中任一项所述的代码转换方法的步骤。
PCT/CN2022/076476 2021-04-19 2022-02-16 一种代码转换方法及装置 WO2022222586A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202110420845.6 2021-04-19
CN202110420845.6A CN113031967B (zh) 2021-04-19 2021-04-19 一种代码转换方法及装置

Publications (1)

Publication Number Publication Date
WO2022222586A1 true WO2022222586A1 (zh) 2022-10-27

Family

ID=76457937

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/076476 WO2022222586A1 (zh) 2021-04-19 2022-02-16 一种代码转换方法及装置

Country Status (2)

Country Link
CN (1) CN113031967B (zh)
WO (1) WO2022222586A1 (zh)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117278648A (zh) * 2023-09-21 2023-12-22 广东保伦电子股份有限公司 协议动态适配方法、装置、电子设备及存储介质

Families Citing this family (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113031967B (zh) * 2021-04-19 2024-05-10 抖音视界有限公司 一种代码转换方法及装置
CN113608746B (zh) * 2021-08-16 2023-10-27 北京字节跳动网络技术有限公司 一种代码转换处理方法、装置、计算机设备和存储介质
CN118466959A (zh) * 2023-02-07 2024-08-09 华为技术有限公司 程序变量类型处理方法和装置

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7895584B1 (en) * 2003-03-14 2011-02-22 Xilinx, Inc. Translation of a program in a dynamically-typed language to a program in a hardware description language
CN107179933A (zh) * 2017-06-08 2017-09-19 成都未来之门科技有限公司 动态语言变量识别方法、装置、翻译方法及软件移植方法
CN108920149A (zh) * 2017-03-29 2018-11-30 华为技术有限公司 编译方法和编译装置
CN110471666A (zh) * 2019-07-18 2019-11-19 五八有限公司 代码自动转换方法和装置、代码转换器及介质
CN113031967A (zh) * 2021-04-19 2021-06-25 北京字节跳动网络技术有限公司 一种代码转换方法及装置

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7895584B1 (en) * 2003-03-14 2011-02-22 Xilinx, Inc. Translation of a program in a dynamically-typed language to a program in a hardware description language
CN108920149A (zh) * 2017-03-29 2018-11-30 华为技术有限公司 编译方法和编译装置
CN107179933A (zh) * 2017-06-08 2017-09-19 成都未来之门科技有限公司 动态语言变量识别方法、装置、翻译方法及软件移植方法
CN110471666A (zh) * 2019-07-18 2019-11-19 五八有限公司 代码自动转换方法和装置、代码转换器及介质
CN113031967A (zh) * 2021-04-19 2021-06-25 北京字节跳动网络技术有限公司 一种代码转换方法及装置

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117278648A (zh) * 2023-09-21 2023-12-22 广东保伦电子股份有限公司 协议动态适配方法、装置、电子设备及存储介质

Also Published As

Publication number Publication date
CN113031967A (zh) 2021-06-25
CN113031967B (zh) 2024-05-10

Similar Documents

Publication Publication Date Title
WO2022222586A1 (zh) 一种代码转换方法及装置
Flanagan JavaScript: The definitive guide: Activate your web pages
Jensen et al. PASCAL user manual and report: ISO PASCAL standard
US8434062B2 (en) Enhancing source code debugging and readability using visual symbols
US7610548B1 (en) Method and apparatus for applying locale behaviors to regions of a form
CN108595171B (zh) 对象的模型生成方法、装置、设备及存储介质
US9043757B2 (en) Identifying differences between source codes of different versions of a software when each source code is organized using incorporated files
US11816456B2 (en) Notebook for navigating code using machine learning and flow analysis
TWI713015B (zh) 語言識別方法及裝置
CN111522558B (zh) 基于Java的动态配置规则的方法、装置、系统和可读介质
CN106874163A (zh) 一种显示软件错误码信息的方法及系统
CN109522013A (zh) 一种业务功能的代码生成方法及装置
Srinivasa Ragavan et al. Gridbook: Natural language formulas for the spreadsheet grid
CN110737431A (zh) 软件开发方法、开发平台、终端设备及存储介质
CN111930364B (zh) 通过动态配置规则装置实现流程节点按条件流转的方法
WO2020248784A1 (zh) 一种在计算机上实现母语编程的方法
US9383972B2 (en) Methods and arrangements for processing and presentation of information
Savic et al. Use case specification at different levels of abstraction
CN114816246B (zh) 一种用于Proto文件的处理方法及装置
CN115758973A (zh) 芯片寄存器设计文件的生成方法及装置、设备、存储介质
CN112748915B (zh) 一种基于StimulSoft的动态扩展业务函数的方法及设备
CN110968591A (zh) 查询语句的生成方法、装置、存储介质及处理器
CN114089980A (zh) 编程处理方法、装置、解释器及非易失性存储介质
US11144287B2 (en) Compile time validation of programming code
Diaconescu From universal logic to computer science, and back

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22790696

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 150224)

122 Ep: pct application non-entry in european phase

Ref document number: 22790696

Country of ref document: EP

Kind code of ref document: A1