Skip to content

Commit

Permalink
增加JavaToXLangTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Aug 16, 2024
1 parent fe62807 commit b306fd2
Show file tree
Hide file tree
Showing 15 changed files with 882 additions and 244 deletions.
2 changes: 1 addition & 1 deletion nop-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

<!-- <graalvm.version>21.3.5</graalvm.version>-->
<!-- <jackson.version>2.10.2</jackson.version>-->
<janino.version>3.1.9</janino.version>
<janino.version>3.1.12</janino.version>
<!-- <junit-jupiter.version>5.7.0</junit-jupiter.version>-->

<pulsar.version>2.8.0</pulsar.version>
Expand Down
9 changes: 9 additions & 0 deletions nop-xlang/model/ast/io/nop/xlang/ast/XLangAST.xjava
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public class XLangAST {

}

class CompilationUnit{
@PropMeta(mandatory=true)
String packageName;

List<Statement> statements;
}

class Program extends Expression {
String sourceType; //: "script" | "module";

Expand Down Expand Up @@ -574,6 +581,8 @@ public class XLangAST {
XLangASTNode source;

Identifier local;

boolean staticImport;
}

//import {foo as bar} from "mod"
Expand Down
8 changes: 8 additions & 0 deletions nop-xlang/src/main/java/io/nop/xlang/ast/CompilationUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

package io.nop.xlang.ast;

import io.nop.xlang.ast._gen._CompilationUnit;

public class CompilationUnit extends _CompilationUnit{

}
210 changes: 106 additions & 104 deletions nop-xlang/src/main/java/io/nop/xlang/ast/XLangASTKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,212 +3,214 @@

public enum XLangASTKind{

Program, // ordinal: 0
CompilationUnit, // ordinal: 0

Identifier, // ordinal: 1
Program, // ordinal: 1

Literal, // ordinal: 2
Identifier, // ordinal: 2

TemplateStringLiteral, // ordinal: 3
Literal, // ordinal: 3

RegExpLiteral, // ordinal: 4
TemplateStringLiteral, // ordinal: 4

BlockStatement, // ordinal: 5
RegExpLiteral, // ordinal: 5

EmptyStatement, // ordinal: 6
BlockStatement, // ordinal: 6

ReturnStatement, // ordinal: 7
EmptyStatement, // ordinal: 7

BreakStatement, // ordinal: 8
ReturnStatement, // ordinal: 8

ContinueStatement, // ordinal: 9
BreakStatement, // ordinal: 9

IfStatement, // ordinal: 10
ContinueStatement, // ordinal: 10

SwitchStatement, // ordinal: 11
IfStatement, // ordinal: 11

SwitchCase, // ordinal: 12
SwitchStatement, // ordinal: 12

ThrowStatement, // ordinal: 13
SwitchCase, // ordinal: 13

TryStatement, // ordinal: 14
ThrowStatement, // ordinal: 14

CatchClause, // ordinal: 15
TryStatement, // ordinal: 15

WhileStatement, // ordinal: 16
CatchClause, // ordinal: 16

DoWhileStatement, // ordinal: 17
WhileStatement, // ordinal: 17

VariableDeclarator, // ordinal: 18
DoWhileStatement, // ordinal: 18

VariableDeclaration, // ordinal: 19
VariableDeclarator, // ordinal: 19

ForStatement, // ordinal: 20
VariableDeclaration, // ordinal: 20

ForOfStatement, // ordinal: 21
ForStatement, // ordinal: 21

ForRangeStatement, // ordinal: 22
ForOfStatement, // ordinal: 22

ForInStatement, // ordinal: 23
ForRangeStatement, // ordinal: 23

DeleteStatement, // ordinal: 24
ForInStatement, // ordinal: 24

ChainExpression, // ordinal: 25
DeleteStatement, // ordinal: 25

ThisExpression, // ordinal: 26
ChainExpression, // ordinal: 26

SuperExpression, // ordinal: 27
ThisExpression, // ordinal: 27

TemplateStringExpression, // ordinal: 28
SuperExpression, // ordinal: 28

ArrayExpression, // ordinal: 29
TemplateStringExpression, // ordinal: 29

ObjectExpression, // ordinal: 30
ArrayExpression, // ordinal: 30

PropertyAssignment, // ordinal: 31
ObjectExpression, // ordinal: 31

ParameterDeclaration, // ordinal: 32
PropertyAssignment, // ordinal: 32

FunctionDeclaration, // ordinal: 33
ParameterDeclaration, // ordinal: 33

ArrowFunctionExpression, // ordinal: 34
FunctionDeclaration, // ordinal: 34

UnaryExpression, // ordinal: 35
ArrowFunctionExpression, // ordinal: 35

UpdateExpression, // ordinal: 36
UnaryExpression, // ordinal: 36

BinaryExpression, // ordinal: 37
UpdateExpression, // ordinal: 37

InExpression, // ordinal: 38
BinaryExpression, // ordinal: 38

ExpressionStatement, // ordinal: 39
InExpression, // ordinal: 39

AssignmentExpression, // ordinal: 40
ExpressionStatement, // ordinal: 40

LogicalExpression, // ordinal: 41
AssignmentExpression, // ordinal: 41

MemberExpression, // ordinal: 42
LogicalExpression, // ordinal: 42

EvalExpression, // ordinal: 43
MemberExpression, // ordinal: 43

CallExpression, // ordinal: 44
EvalExpression, // ordinal: 44

NewExpression, // ordinal: 45
CallExpression, // ordinal: 45

SpreadElement, // ordinal: 46
NewExpression, // ordinal: 46

SequenceExpression, // ordinal: 47
SpreadElement, // ordinal: 47

ConcatExpression, // ordinal: 48
SequenceExpression, // ordinal: 48

TemplateExpression, // ordinal: 49
ConcatExpression, // ordinal: 49

BraceExpression, // ordinal: 50
TemplateExpression, // ordinal: 50

ObjectBinding, // ordinal: 51
BraceExpression, // ordinal: 51

PropertyBinding, // ordinal: 52
ObjectBinding, // ordinal: 52

RestBinding, // ordinal: 53
PropertyBinding, // ordinal: 53

ArrayBinding, // ordinal: 54
RestBinding, // ordinal: 54

ArrayElementBinding, // ordinal: 55
ArrayBinding, // ordinal: 55

ExportDeclaration, // ordinal: 56
ArrayElementBinding, // ordinal: 56

ExportNamedDeclaration, // ordinal: 57
ExportDeclaration, // ordinal: 57

ExportAllDeclaration, // ordinal: 58
ExportNamedDeclaration, // ordinal: 58

ExportSpecifier, // ordinal: 59
ExportAllDeclaration, // ordinal: 59

ImportDeclaration, // ordinal: 60
ExportSpecifier, // ordinal: 60

ImportAsDeclaration, // ordinal: 61
ImportDeclaration, // ordinal: 61

ImportSpecifier, // ordinal: 62
ImportAsDeclaration, // ordinal: 62

ImportDefaultSpecifier, // ordinal: 63
ImportSpecifier, // ordinal: 63

ImportNamespaceSpecifier, // ordinal: 64
ImportDefaultSpecifier, // ordinal: 64

AwaitExpression, // ordinal: 65
ImportNamespaceSpecifier, // ordinal: 65

Decorators, // ordinal: 66
AwaitExpression, // ordinal: 66

QualifiedName, // ordinal: 67
Decorators, // ordinal: 67

Decorator, // ordinal: 68
QualifiedName, // ordinal: 68

MetaObject, // ordinal: 69
Decorator, // ordinal: 69

MetaProperty, // ordinal: 70
MetaObject, // ordinal: 70

MetaArray, // ordinal: 71
MetaProperty, // ordinal: 71

UsingStatement, // ordinal: 72
MetaArray, // ordinal: 72

MacroExpression, // ordinal: 73
UsingStatement, // ordinal: 73

TextOutputExpression, // ordinal: 74
MacroExpression, // ordinal: 74

EscapeOutputExpression, // ordinal: 75
TextOutputExpression, // ordinal: 75

CollectOutputExpression, // ordinal: 76
EscapeOutputExpression, // ordinal: 76

CompareOpExpression, // ordinal: 77
CollectOutputExpression, // ordinal: 77

AssertOpExpression, // ordinal: 78
CompareOpExpression, // ordinal: 78

BetweenOpExpression, // ordinal: 79
AssertOpExpression, // ordinal: 79

GenNodeExpression, // ordinal: 80
BetweenOpExpression, // ordinal: 80

GenNodeAttrExpression, // ordinal: 81
GenNodeExpression, // ordinal: 81

OutputXmlAttrExpression, // ordinal: 82
GenNodeAttrExpression, // ordinal: 82

OutputXmlExtAttrsExpression, // ordinal: 83
OutputXmlAttrExpression, // ordinal: 83

TypeOfExpression, // ordinal: 84
OutputXmlExtAttrsExpression, // ordinal: 84

InstanceOfExpression, // ordinal: 85
TypeOfExpression, // ordinal: 85

CastExpression, // ordinal: 86
InstanceOfExpression, // ordinal: 86

ArrayTypeNode, // ordinal: 87
CastExpression, // ordinal: 87

ParameterizedTypeNode, // ordinal: 88
ArrayTypeNode, // ordinal: 88

TypeNameNode, // ordinal: 89
ParameterizedTypeNode, // ordinal: 89

UnionTypeDef, // ordinal: 90
TypeNameNode, // ordinal: 90

IntersectionTypeDef, // ordinal: 91
UnionTypeDef, // ordinal: 91

ObjectTypeDef, // ordinal: 92
IntersectionTypeDef, // ordinal: 92

PropertyTypeDef, // ordinal: 93
ObjectTypeDef, // ordinal: 93

TupleTypeDef, // ordinal: 94
PropertyTypeDef, // ordinal: 94

TypeParameterNode, // ordinal: 95
TupleTypeDef, // ordinal: 95

TypeAliasDeclaration, // ordinal: 96
TypeParameterNode, // ordinal: 96

FunctionTypeDef, // ordinal: 97
TypeAliasDeclaration, // ordinal: 97

FunctionArgTypeDef, // ordinal: 98
FunctionTypeDef, // ordinal: 98

EnumDeclaration, // ordinal: 99
FunctionArgTypeDef, // ordinal: 99

EnumMember, // ordinal: 100
EnumDeclaration, // ordinal: 100

ClassDefinition, // ordinal: 101
EnumMember, // ordinal: 101

FieldDeclaration, // ordinal: 102
ClassDefinition, // ordinal: 102

CustomExpression, // ordinal: 103
FieldDeclaration, // ordinal: 103

CustomExpression, // ordinal: 104

}
21 changes: 21 additions & 0 deletions nop-xlang/src/main/java/io/nop/xlang/ast/XLangASTOptimizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class XLangASTOptimizer<C> extends AbstractOptimizer<XLangASTNode,C>{
public XLangASTNode optimize(XLangASTNode node,C context){
switch(node.getASTKind()){

case CompilationUnit:
return optimizeCompilationUnit((CompilationUnit)node,context);

case Program:
return optimizeProgram((Program)node,context);

Expand Down Expand Up @@ -328,6 +331,24 @@ public XLangASTNode optimize(XLangASTNode node,C context){
}


public XLangASTNode optimizeCompilationUnit(CompilationUnit node, C context){
CompilationUnit ret = node;


if(node.getStatements() != null){

java.util.List<io.nop.xlang.ast.Statement> statementsOpt = optimizeList(node.getStatements(),true, context);
if(statementsOpt != node.getStatements()){
incChangeCount();
if(shouldClone(ret,node)) { clearParent(statementsOpt); ret = node.deepClone();}
ret.setStatements(statementsOpt);
}

}

return ret;
}

public XLangASTNode optimizeProgram(Program node, C context){
Program ret = node;

Expand Down
Loading

0 comments on commit b306fd2

Please sign in to comment.