CN114489683B - JVM-based functional programming grammar implementation method for java-like language - Google Patents

JVM-based functional programming grammar implementation method for java-like language Download PDF

Info

Publication number
CN114489683B
CN114489683B CN202210394797.2A CN202210394797A CN114489683B CN 114489683 B CN114489683 B CN 114489683B CN 202210394797 A CN202210394797 A CN 202210394797A CN 114489683 B CN114489683 B CN 114489683B
Authority
CN
China
Prior art keywords
expression
instruction
type
null
grammar
Prior art date
Legal status (The legal status 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 status listed.)
Active
Application number
CN202210394797.2A
Other languages
Chinese (zh)
Other versions
CN114489683A (en
Inventor
王三明
王聪明
王圆博
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Anyuan Technology Co.,Ltd.
Qiye Cloud Big Data Nanjing Co ltd
Original Assignee
Qiye Cloud Big Data Nanjing Co ltd
Nanjing Anyuan Technology Co ltd
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 Qiye Cloud Big Data Nanjing Co ltd, Nanjing Anyuan Technology Co ltd filed Critical Qiye Cloud Big Data Nanjing Co ltd
Priority to CN202210394797.2A priority Critical patent/CN114489683B/en
Publication of CN114489683A publication Critical patent/CN114489683A/en
Application granted granted Critical
Publication of CN114489683B publication Critical patent/CN114489683B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis
    • G06F8/433Dependency analysis; Data or control flow analysis
    • G06F8/434Pointers; Aliasing

Abstract

The invention discloses a method for realizing a java-like language JVM-based functional programming grammar, which comprises the following specific steps: s1: parsing the grammar, converting the grammar into a grammar intermediate representation layer object, S2: in the parsing process, the code is decomposed into three parts, i.e., a left expression, a symbol and a right expression, S3: and resolving the symbols. According to the method, through syntax analysis, the formula is divided into a left expression part, a symbol part and a right expression part, the left expression part is processed, the type is deduced through the right expression part, a default value is given, then the right expression part is processed, null pointer errors are avoided, non-professional personnel using the platform can configure logic of high-quality null pointers, and convenience in platform operation is improved.

Description

JVM-based functional programming grammar implementation method for java-like language
Technical Field
The invention relates to the technical field of programming language compilation, in particular to a method for realizing Java-like language based JVM (Java virtual machine) -based functional programming grammar.
Background
The java language realizes the cross-platform characteristic through the JVM virtual machine technology, and is translated into the machine code of the target platform to be executed during the operation through a set of byte codes as an intermediate representation layer. On the basis of the above, many front-end languages based on JVM, such as kotlin, scala, groovy, have been introduced in the industry. The JVM executes bytecode by interpreting it mainly through a stack, and accesses modified data through a local variable table and an operand stack.
The class file is a file format that defines a set of binary specifications for carrying bytecodes as the standard execution input of the JVM.
Antlr is a language tool that can automatically generate a corresponding lexical grammar parser for the grammar conforming to the BNF paradigm.
The ASM is a general java bytecode operation framework, is commonly used in dynamic systems, and can also be used in compilers, such as implementation of lambda expressions in OpenJDK, Groovy compilers, and Kotlin compilers.
The IR Node intermediate representation layer is a machine-independent abstraction common in compilation, and machine-independent optimization of code can be performed through information retained by the intermediate representation layer.
qLang is used as a bifurcation language of java and used for graphical low code programming of a start-up cloud platform, and graphical configuration generated by a user can be compiled into qLang language to finally generate a target class or other target languages.
Null pointer errors are one of the most common errors in jvm-based virtual machine language programming, and because the low-code platform is general, the target user of the platform is not a professional technician, null pointer exceptions are more easily generated, and therefore a method is needed to enable the user to directly configure logic without high-quality null pointers.
Disclosure of Invention
The invention aims to provide a method for realizing a java-like language JVM-based functional programming grammar, so as to solve the problems in the background art.
In order to achieve the purpose, the invention provides the following technical scheme: a method for realizing a Java-like language JVM-based functional programming grammar comprises the following specific steps:
s1: and (5) parsing the grammar, and converting the grammar into a grammar intermediate representation layer object.
S2: in the process of resolving, the code is decomposed into three parts of a left expression, a symbol and a right expression.
S3: and resolving the symbols, wherein the processing steps are as follows:
s31: and processing the left expression, calculating the position x of the left expression in locals, and deducing a type adding load instruction according to the left expression.
S32: and analyzing the symbol, and executing a jump instruction for judging whether the left side of the symbol is null.
S33: at this time, a jump flag label1 and a jump flag label2 are generated, and a non-null jump instruction ifnunull label1 is written.
S34: and when the expression on the left side is null, the sequential execution is carried out, and at the moment, the byte length occupied by the type is deduced according to the expression on the left side, and the byte length is written into pop and pop2, so that the value of load _ x in the stack is popped up.
S35: the type inferred from the expression on the right is assigned a default value.
S36: at this time, the case that the expression on the left side is null is executed, and the unconditional jump instruction goto label2 needs to be written, and label1 is the current position.
S37: and processing the right expression, and writing a normal calling instruction according to the method call and the object reference.
S38: after the whole expression is executed, label2 is the current position, and this position represents that the execution of the whole expression is finished.
Preferably, in step S31, each time a set { } is resolved, the qLang compiler generates a corresponding scope object, and the IR Node in the current scope holds a reference of the scope, and records the defined type and variable name in the current scope when resolving to the defined Node.
Preferably, in step S31, it is determined whether the left-side expression is a method call or not, if the left-side expression is a method call, the corresponding method is found by a jdk method built in method finder.
Preferably, in the step S32, a copy of the left expression needs to be provided for the jump determination instruction, and a copy does not need to be provided in a normal case.
Preferably, in the step S32, the byte length occupied by the type deduced from the expression on the left is written into the dup or dup2 instruction:
if there are 8 bytes for long or double, the dup2 instruction is written.
If int or object occupies four bytes, the dup instruction is written.
Preferably, in the step S35, the default value of 0 can be divided into four types:
the int type writes to the iconst _0 instruction.
The bootean type defaults to false, which is represented as 0 in the byte code, written to the iconst _0 instruction.
Other object types default to null, write command aconst null.
The void type has no return value, does not need to push a value into the stack, and has no instruction to write.
Preferably, in the step S36: the label1 is located at the non-null code execution start position.
The invention has the technical effects and advantages that:
according to the method, through syntax analysis, the code is divided into a left expression, a symbol and a right expression, the left expression is processed, the type is deduced through the right expression, a default value is given, then the right expression is processed, null pointer errors are avoided, non-professionals using a platform can configure logic of high-quality null pointers, convenience of platform operation is improved, and compiling of bifurcated languages on java syntax is enhanced.
Drawings
FIG. 1 is a schematic diagram illustrating an embodiment of the present invention.
Fig. 2 is a schematic diagram of calculating a position in a stack corresponding to a left-side expression in the embodiment of the present invention.
FIG. 3 is a code diagram of an embodiment of the present invention.
FIG. 4 is a diagram illustrating a call scenario of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The invention is further illustrated by the following specific examples, which are not intended to be limiting.
Example one
For convenience of explaining the present invention, the present embodiment gives the following example codes:
Figure DEST_PATH_IMAGE002
as shown in fig. 1-4, the method comprises the following specific steps:
s1: and (4) parsing the grammar, and converting the grammar into a grammar middle representation layer object and a set of middle IR Node abstracted by qLang.
S2: during the analysis, the
Figure DEST_PATH_IMAGE004
Decomposed into left expression o, symbol
Figure DEST_PATH_IMAGE006
And the right expression getField () is shown in figure 1, and the direct call case is shown in figure 4.
S3: and resolving the symbols, wherein the processing steps are as follows:
s31: processing the left expression, calculating the position x of the left expression in locals, as shown in fig. 2, when a group of { } is analyzed, the qLang compiler generates a corresponding scope object, meanwhile, the IR Node in the current scope holds the reference of the scope, when the definition Node is analyzed, the defined type and the variable name are recorded in the current scope, and deducing the type adding load instruction according to the left expression, firstly judging whether the left expression is a method call according to the left expression, if the left expression is the method call, finding the corresponding method through the built-in method FindMethod of jd by the left expression and the parameter type of the method call, if the method is the method of qLang analysis in the current compiling context, obtaining the context stored during compiling, deducing the type of the expression according to the method return value, if the type is the reference type, the corresponding type of the homonymous variable is found upwards through the scope.
S32: analyzing the symbol, different from the situation that the common direct call needs to execute a jump instruction for judging whether the left side of the symbol is null or not, at the moment, a copy of a left side expression needs to be provided for the jump judgment instruction (the copy does not need to be provided under the normal condition), execute the jump instruction for judging whether the left side of the symbol is null or not, need to provide a copy of the left side expression for the jump judgment instruction, write a dup or dup2 instruction according to the byte length occupied by the type deduced from the left side expression, write a dup2 instruction at the moment if the left side expression occupies 8 bytes, write the dup instruction at the moment if the left side expression occupies four bytes.
S33: at this time, a jump flag label1 and a jump flag label2 are generated, and a non-null jump instruction ifnunull label1 is written.
S34: the left expression is null, and in order to prevent the null pointer from calling the right expression any more, the byte length occupied by the type inferred (logically the same as the inferred type) from the left expression is written into pop, pop2, and the value of load _ x in the stack is popped up.
S35: the type deduced according to the expression on the right side is given a default value, the numerical default value of 0 can be divided into four types, int type is written into an iconst _0 instruction, boilean type is default into false, the false is represented as 0 in byte codes, the iconst _0 instruction is written, other object types are default into null, the aconst _ null is written, the void type has no return value, no value needs to be pressed into a stack, and no instruction is written.
S36: at this time, the case that the left expression is null is already executed, the unconditional jump instruction goto label2 needs to be written, label1 is the current position, and label1 is the non-null code execution start position.
S37: and processing the right expression, and writing a normal calling instruction according to method calling, object reference and the like.
S38: after the whole expression is executed, the label2 is the current position, and this position represents that the whole execution of the expression is finished, and the byte code generation is shown in fig. 2.
Finally, it should be noted that: although the present invention has been described in detail with reference to the foregoing embodiments, it will be apparent to those skilled in the art that modifications may be made to the embodiments or portions thereof without departing from the spirit and scope of the invention.

Claims (7)

1. A Java language-like JVM-based functional programming grammar implementation method is characterized by comprising the following specific steps:
s1: parsing grammar, and converting into a grammar intermediate presentation layer object;
s2: in the analysis process, decomposing the code into three parts of a left expression, a symbol and a right expression;
s3: and resolving the symbols, wherein the processing steps are as follows:
s31: processing the left expression, calculating the position x of the left expression in locals, and deducing a type adding load instruction according to the left expression;
s32: analyzing the symbol, and executing a jump instruction for judging whether the left side of the symbol is null or not;
s33: at this time, a jump mark label1 and a jump mark label2 are generated, and a non-null jump instruction ifnunull label1 is written;
s34: sequentially executing when the expression on the left side is null, deducing the byte length occupied by the type according to the expression on the left side, writing the byte length into pop and pop2, and popping up the value of load _ x in the stack;
s35: assigning default values to the types inferred from the expression on the right;
s36: at this time, if the left expression is null, the execution is finished, and an unconditional jump instruction goto label2 needs to be written, and label1 is the current position;
s37: processing the right expression, and writing a normal calling instruction according to method calling and object reference;
s38: after the whole expression is executed, label2 is the current position, and this position represents that the execution of the whole expression is finished.
2. The method as claimed in claim 1, wherein in step S31, each time a set of { } is parsed, the qLang compiler generates a corresponding scope object, and the IR Node in the current scope holds a reference of the scope, and records the defined type and variable name into the current scope when parsing to the defined Node.
3. The method as claimed in claim 1, wherein in step S31, it is determined whether the expression is a method call according to the left-side expression, if the expression is a method call, the corresponding method is found by using a method finder findmethod built-in jdk through the left-side expression and parameter type of the method call, if the method is a qLang parsing method in the current compilation context, the type of the expression expressed as an external whole is deduced according to the method return value, and if the method is a reference type, the corresponding type of the same-name variable is found upwards through the scope.
4. The method as claimed in claim 1, wherein in step S32, a copy of the left expression is required to be provided for the jump judging instruction, and no copy is required in normal case.
5. The method as claimed in claim 1, wherein in step S32, the byte length occupied by the type deduced from the expression on the left side is written into dup or dup2 instruction:
if long or double occupies 8 bytes, at this time, a dup2 instruction is written;
if int or object occupies four bytes, the dup instruction is written.
6. The method as claimed in claim 1, wherein the number default value 0 is classified into four types in the step S35:
int type write iconst _0 instruction;
the toolean type defaults to false, which is represented as 0 in the byte code, writes to the iconst _0 instruction;
defaulting other object types to null, and writing an instruction aconst _ null;
the void type has no return value, does not need to push a value into the stack, and has no instruction to write.
7. The method as claimed in claim 1, wherein in the step S36: the label1 is located at the non-null code execution start position.
CN202210394797.2A 2022-04-15 2022-04-15 JVM-based functional programming grammar implementation method for java-like language Active CN114489683B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210394797.2A CN114489683B (en) 2022-04-15 2022-04-15 JVM-based functional programming grammar implementation method for java-like language

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210394797.2A CN114489683B (en) 2022-04-15 2022-04-15 JVM-based functional programming grammar implementation method for java-like language

Publications (2)

Publication Number Publication Date
CN114489683A CN114489683A (en) 2022-05-13
CN114489683B true CN114489683B (en) 2022-06-21

Family

ID=81489474

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210394797.2A Active CN114489683B (en) 2022-04-15 2022-04-15 JVM-based functional programming grammar implementation method for java-like language

Country Status (1)

Country Link
CN (1) CN114489683B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090313613A1 (en) * 2008-06-16 2009-12-17 Real Dice Inc. Methods and Apparatus for Automatic Translation of a Computer Program Language Code
CN101901268A (en) * 2010-08-02 2010-12-01 华为技术有限公司 Rule matching method and device
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
CN111381828A (en) * 2018-12-29 2020-07-07 北京奇虎科技有限公司 Method and device for generating syntax tree of code file and electronic equipment

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090313613A1 (en) * 2008-06-16 2009-12-17 Real Dice Inc. Methods and Apparatus for Automatic Translation of a Computer Program Language Code
CN101901268A (en) * 2010-08-02 2010-12-01 华为技术有限公司 Rule matching method and device
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
CN111381828A (en) * 2018-12-29 2020-07-07 北京奇虎科技有限公司 Method and device for generating syntax tree of code file and electronic equipment

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
Switch竟然会报空指针异常,学到了!;里奥ii;<https://blog.csdn.net/qq36846776/article/details/116701932>;20210512;第1-8页 *

Also Published As

Publication number Publication date
CN114489683A (en) 2022-05-13

Similar Documents

Publication Publication Date Title
AU774467B2 (en) Automatic stub/adapter generator
US5586328A (en) Module dependency based incremental compiler and method
US7162709B2 (en) System and method for common code generation
US7380242B2 (en) Compiler and software product for compiling intermediate language bytecodes into Java bytecodes
EP0806725B1 (en) Method and apparatus for early insertion of assembler code for optimization
US5381547A (en) Method for dynamically linking definable program elements of an interactive data processing system
EP1280056B1 (en) Generation of debugging information
US7171649B1 (en) Optimizing safe downcasting in an object-oriented programming language
US20040216094A1 (en) Unified data type system and method
US20040268301A1 (en) Adding new compiler methods to an integrated development environment
JPH08339304A (en) Method and apparatus for generation of platform standard object file containing machine independence code
EP2378413B1 (en) Methods and systems to implement non-ABI conforming features across unseen interfaces
US8464232B2 (en) Compiler compiler system with syntax-controlled runtime and binary application programming interfaces
JPH02272627A (en) Digital computer system and method of invocation of procedure of the same
WO2022033229A1 (en) Software code compiling method and system
JP2005141380A (en) Template compile method
US6799320B1 (en) Providing binding options for component interfaces
CN113703779A (en) Cross-platform multi-language compiling method and ultra-light Internet of things virtual machine
CA2167306C (en) Multiple entry point method dispatch
JP2009169864A (en) Compile method and compile program
CN114489683B (en) JVM-based functional programming grammar implementation method for java-like language
CN112416313B (en) Compiling method supporting large integer data type and operator
US5826087A (en) Method and apparatus for cross calling programs of different lexical scoping methodology
EP3534252A1 (en) Method and system for providing a processing device with executable machine code
CN113835688B (en) Object packaging method of scientific computing language interpreter

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant
CP01 Change in the name or title of a patent holder
CP01 Change in the name or title of a patent holder

Address after: 11-14 / F, tower a, Tengfei building, 88 Jiangmiao Road, yanchuangyuan, Jiangbei new district, Nanjing, Jiangsu Province 210000

Patentee after: Anyuan Technology Co.,Ltd.

Patentee after: Qiye cloud big data (Nanjing) Co.,Ltd.

Address before: 11-14 / F, tower a, Tengfei building, 88 Jiangmiao Road, yanchuangyuan, Jiangbei new district, Nanjing, Jiangsu Province 210000

Patentee before: NANJING ANYUAN TECHNOLOGY Co.,Ltd.

Patentee before: Qiye cloud big data (Nanjing) Co.,Ltd.