Skip to content

Commit 501b4f9

Browse files
committed
Optimizations + Refactoring
## Optimizations ### Processors - Updated `Processors.CX_PCM_LGC`. Changed the `z-index` of the wrapper tree. With that, the wrapper tree will not be a child of the operator when the wrapper tree contain only the operator tree. With that, now the correct error is thrown when a misplaced operator. - Updated `Processors.OZ_CLEAN`. Limited the cleaning to be done only on `CX_CMD` and `CX_INJ`. With that, now it is possible to inject inside two double-quotes or two quotes in the text. ### Others - added method `Jamplate.execute(Environment, Supplier<Memory>, Compilation[])`. Now the user can supply the default memory. - allow whitespace between the command and the start of the line. ## Refactorings ### Instructions - created instruction `Idle`. ### Compilers - renamed `KindCompiler` to `FilterByKindCompiler`. - renamed `OrderCompiler` to `FirstCompileCompiler`. - renamed `EmptyCompiler` to `ToIdleCompiler`. - renamed `WhitespaceCompiler` to `ToIdleWhitespaceCompiler`. - renamed `PushConstCompiler` to `ToPushConstCompiler`. - renamed `PushUnescapeConstCompiler` to `ToPushConstUnescapeCompiler`. - renamed `PushEvalAddrCompiler` to `ToPushEvalAddrCompiler`. - renamed `ReprntConstCompiler` to `ToReprntConstCompiler`. ### Parsers - renamed `KindParser` to `FilterByKindParser`. - renamed `HierarchyKindParser` to `FilterHierarchyByKindParser`. - renamed `MergeParser` to `MergeByOrderParser`. - renamed `OrderParser` to `MergeByWeightParser`. - renamed `FlatMergeParser` to `MergeFlatByOrderParser`. - renamed `FlatOrderParser` to `MergeFlatByWeightParser`. - renamed `GroupParser` to `PatternGroupParser`. - renamed `DoublePatternParser` to `PatternRangeParser`. - deleted `EmptyParser`. - deleted `RandomMergeParser`. ### Other - removed `Instruction.empty()` ~ replaced by `Idle`. - removed `Instruction.empty(Tree)` ~ replaced by `Idle`. ## TODO - replace the processors in `org.jamplate.impl.processor` with analyzers.
1 parent 9a7d4f4 commit 501b4f9

26 files changed

Lines changed: 347 additions & 450 deletions

src/main/java/org/jamplate/impl/Compilers.java

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ public final class Compilers {
4545
*/
4646
@NotNull
4747
public static final Compiler CX_CMD_CONSOLE =
48-
new KindCompiler(Kind.CX_CMD_CONSOLE, (compiler, compilation, tree) -> {
48+
new FilterByKindCompiler(Kind.CX_CMD_CONSOLE, (compiler, compilation, tree) -> {
4949
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
5050
Instruction instruction = compiler.compile(compiler, compilation, paramT);
5151

5252
return new ConsoleExecInstr(
5353
tree,
5454
instruction == null ?
55-
Instruction.empty(paramT) :
55+
new Idle(paramT) :
5656
instruction
5757
);
5858
});
@@ -65,7 +65,7 @@ public final class Compilers {
6565
@SuppressWarnings("OverlyLongLambda")
6666
@NotNull
6767
public static final Compiler CX_CMD_DECLARE =
68-
new KindCompiler(Kind.CX_CMD_DECLARE, (compiler, compilation, tree) -> {
68+
new FilterByKindCompiler(Kind.CX_CMD_DECLARE, (compiler, compilation, tree) -> {
6969
Tree keyT = tree.getSketch().get(Component.KEY).getTree();
7070
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
7171
Tree accessorT = tree.getSketch().get(Component.ACCESSOR).getTree();
@@ -111,7 +111,7 @@ public final class Compilers {
111111
*/
112112
@NotNull
113113
public static final Compiler CX_CMD_DEFINE =
114-
new KindCompiler(Kind.CX_CMD_DEFINE, (compiler, compilation, tree) -> {
114+
new FilterByKindCompiler(Kind.CX_CMD_DEFINE, (compiler, compilation, tree) -> {
115115
Tree keyT = tree.getSketch().get(Component.KEY).getTree();
116116
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
117117

@@ -138,14 +138,14 @@ public final class Compilers {
138138
*/
139139
@NotNull
140140
public static final Compiler CX_CMD_ERROR =
141-
new KindCompiler(Kind.CX_CMD_ERROR, (compiler, compilation, tree) -> {
141+
new FilterByKindCompiler(Kind.CX_CMD_ERROR, (compiler, compilation, tree) -> {
142142
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
143143
Instruction instruction = compiler.compile(compiler, compilation, paramT);
144144

145145
return new SerrExecInstr(
146146
tree,
147147
instruction == null ?
148-
Instruction.empty(paramT) :
148+
new Idle(paramT) :
149149
instruction
150150
);
151151
});
@@ -157,7 +157,7 @@ public final class Compilers {
157157
*/
158158
@NotNull
159159
public static final Compiler CX_CMD_INCLUDE =
160-
new KindCompiler(Kind.CX_CMD_INCLUDE, (compiler, compilation, tree) -> {
160+
new FilterByKindCompiler(Kind.CX_CMD_INCLUDE, (compiler, compilation, tree) -> {
161161
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
162162

163163
Instruction instruction = compiler.compile(compiler, compilation, paramT);
@@ -181,14 +181,14 @@ public final class Compilers {
181181
*/
182182
@NotNull
183183
public static final Compiler CX_CMD_MESSAGE =
184-
new KindCompiler(Kind.CX_CMD_MESSAGE, (compiler, compilation, tree) -> {
184+
new FilterByKindCompiler(Kind.CX_CMD_MESSAGE, (compiler, compilation, tree) -> {
185185
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
186186
Instruction instruction = compiler.compile(compiler, compilation, paramT);
187187

188188
return new SoutExecInstr(
189189
tree,
190190
instruction == null ?
191-
Instruction.empty(paramT) :
191+
new Idle(paramT) :
192192
instruction
193193
);
194194
});
@@ -200,7 +200,7 @@ public final class Compilers {
200200
*/
201201
@NotNull
202202
public static final Compiler CX_CMD_SPREAD =
203-
new KindCompiler(Kind.CX_CMD_SPREAD, (compiler, compilation, tree) -> {
203+
new FilterByKindCompiler(Kind.CX_CMD_SPREAD, (compiler, compilation, tree) -> {
204204
Tree paramT = tree.getSketch().get(Component.PARAMETER).getTree();
205205

206206
Instruction instruction = compiler.compile(compiler, compilation, paramT);
@@ -224,7 +224,7 @@ public final class Compilers {
224224
*/
225225
@NotNull
226226
public static final Compiler CX_CMD_UNDEC =
227-
new KindCompiler(Kind.CX_CMD_UNDEC, (compiler, compilation, tree) -> {
227+
new FilterByKindCompiler(Kind.CX_CMD_UNDEC, (compiler, compilation, tree) -> {
228228
Tree keyT = tree.getSketch().get(Component.KEY).getTree();
229229

230230
String address = Trees.read(keyT).toString();
@@ -242,7 +242,7 @@ public final class Compilers {
242242
*/
243243
@NotNull
244244
public static final Compiler CX_CMD_UNDEF =
245-
new KindCompiler(Kind.CX_CMD_UNDEF, (compiler, compilation, tree) -> {
245+
new FilterByKindCompiler(Kind.CX_CMD_UNDEF, (compiler, compilation, tree) -> {
246246
Tree keyT = tree.getSketch().get(Component.KEY).getTree();
247247

248248
String address = Trees.read(keyT).toString();
@@ -264,7 +264,7 @@ public final class Compilers {
264264
@SuppressWarnings("OverlyLongLambda")
265265
@NotNull
266266
public static final Compiler CX_FLW_CAPTURE =
267-
new KindCompiler(Kind.CX_FLW_CAPTURE, (compiler, compilation, tree) -> {
267+
new FilterByKindCompiler(Kind.CX_FLW_CAPTURE, (compiler, compilation, tree) -> {
268268
String address = null;
269269
List<Instruction> instructions = new ArrayList<>();
270270

@@ -309,7 +309,7 @@ public final class Compilers {
309309
@SuppressWarnings("OverlyLongLambda")
310310
@NotNull
311311
public static final Compiler CX_FLW_FOR =
312-
new KindCompiler(Kind.CX_FLW_FOR, (compiler, compilation, tree) -> {
312+
new FilterByKindCompiler(Kind.CX_FLW_FOR, (compiler, compilation, tree) -> {
313313
String address = null;
314314
Instruction instruction = null;
315315
List<Instruction> instructions = new ArrayList<>();
@@ -359,7 +359,7 @@ public final class Compilers {
359359
@SuppressWarnings("OverlyLongLambda")
360360
@NotNull
361361
public static final Compiler CX_FLW_IF =
362-
new KindCompiler(Kind.CX_FLW_IF, (compiler, compilation, tree) -> {
362+
new FilterByKindCompiler(Kind.CX_FLW_IF, (compiler, compilation, tree) -> {
363363
Map<Instruction, List<Instruction>> instructions = new LinkedHashMap<>();
364364
List<Instruction> current = null;
365365

@@ -443,7 +443,7 @@ else if (instruction == null)
443443
@SuppressWarnings("OverlyLongLambda")
444444
@NotNull
445445
public static final Compiler CX_FLW_WHILE =
446-
new KindCompiler(Kind.CX_FLW_WHILE, (compiler, compilation, tree) -> {
446+
new FilterByKindCompiler(Kind.CX_FLW_WHILE, (compiler, compilation, tree) -> {
447447
Instruction instruction = null;
448448
List<Instruction> instructions = new ArrayList<>();
449449

@@ -489,7 +489,7 @@ else if (instruction == null)
489489
*/
490490
@NotNull
491491
public static final Compiler CX_INJ =
492-
new KindCompiler(Kind.CX_INJ, (compiler, compilation, tree) -> {
492+
new FilterByKindCompiler(Kind.CX_INJ, (compiler, compilation, tree) -> {
493493
Tree parameterT = tree.getSketch().get(Component.PARAMETER).getTree();
494494

495495
Instruction instruction = compiler.compile(compiler, compilation, parameterT);
@@ -514,7 +514,7 @@ else if (instruction == null)
514514
@SuppressWarnings("OverlyLongLambda")
515515
@NotNull
516516
public static final Compiler CX_PCM_LGC =
517-
new KindCompiler(Kind.CX_PCM_LGC, (compiler, compilation, tree) -> {
517+
new FilterByKindCompiler(Kind.CX_PCM_LGC, (compiler, compilation, tree) -> {
518518
Tree leftT = tree.getSketch().get(Component.LEFT).getTree();
519519
Tree rightT = tree.getSketch().get(Component.RIGHT).getTree();
520520
Tree operatorT = tree.getSketch().get(Component.KEY).getTree();
@@ -702,7 +702,7 @@ else if (instruction == null)
702702
*/
703703
@NotNull
704704
public static final Compiler CX_PCM_REF =
705-
new KindCompiler(Kind.CX_PCM_REF, (compiler, compilation, tree) -> {
705+
new FilterByKindCompiler(Kind.CX_PCM_REF, (compiler, compilation, tree) -> {
706706
Tree keyT = tree.getSketch().get(Component.KEY).getTree();
707707
Tree accessorT = tree.getSketch().get(Component.ACCESSOR).getTree();
708708

@@ -733,7 +733,7 @@ else if (instruction == null)
733733
public static final Compiler CX_TXT =
734734
new FlattenCompiler(
735735
FallbackCompiler.INSTANCE,
736-
ReprntConstCompiler.INSTANCE
736+
ToReprntConstCompiler.INSTANCE
737737
);
738738

739739
//DC
@@ -745,7 +745,7 @@ else if (instruction == null)
745745
*/
746746
@NotNull
747747
public static final Compiler DC_EOL =
748-
new KindCompiler(Kind.DC_EOL, (compiler, compilation, tree) ->
748+
new FilterByKindCompiler(Kind.DC_EOL, (compiler, compilation, tree) ->
749749
new IpedXinstr(
750750
tree,
751751
new AllocAddrConst(
@@ -764,7 +764,7 @@ else if (instruction == null)
764764
*/
765765
@NotNull
766766
public static final Compiler DC_EOL_SUPPRESSED =
767-
new KindCompiler(Kind.DC_EOL_SUPPRESSED, (compiler, compilation, tree) ->
767+
new FilterByKindCompiler(Kind.DC_EOL_SUPPRESSED, (compiler, compilation, tree) ->
768768
new AllocAddrConst(
769769
tree,
770770
Address.LINE,
@@ -779,7 +779,7 @@ else if (instruction == null)
779779
*/
780780
@NotNull
781781
public static final Compiler DC_ROT =
782-
new KindCompiler(Kind.DC_ROT, (compiler, compilation, tree) -> {
782+
new FilterByKindCompiler(Kind.DC_ROT, (compiler, compilation, tree) -> {
783783
String line = String.valueOf(Trees.line(tree) + 1);
784784
String file = tree.document().toString();
785785
String dir = new File(file).getParent();
@@ -816,14 +816,14 @@ else if (instruction == null)
816816
*/
817817
@NotNull
818818
public static final Compiler SX_CUR =
819-
new KindCompiler(Kind.SX_CUR, new FlattenCompiler(
819+
new FilterByKindCompiler(Kind.SX_CUR, new FlattenCompiler(
820820
FallbackCompiler.INSTANCE,
821-
new MandatoryCompiler(new OrderCompiler(
822-
new KindCompiler(Kind.CX_ANC_OPEN, PushConstCompiler.INSTANCE),
823-
new KindCompiler(Kind.CX_ANC_CLOSE, PushConstCompiler.INSTANCE),
824-
new KindCompiler(Kind.SX_CMA, PushConstCompiler.INSTANCE),
825-
new KindCompiler(Kind.SX_CLN, PushConstCompiler.INSTANCE),
826-
WhitespaceCompiler.INSTANCE
821+
new MandatoryCompiler(new FirstCompileCompiler(
822+
new FilterByKindCompiler(Kind.CX_ANC_OPEN, ToPushConstCompiler.INSTANCE),
823+
new FilterByKindCompiler(Kind.CX_ANC_CLOSE, ToPushConstCompiler.INSTANCE),
824+
new FilterByKindCompiler(Kind.SX_CMA, ToPushConstCompiler.INSTANCE),
825+
new FilterByKindCompiler(Kind.SX_CLN, ToPushConstCompiler.INSTANCE),
826+
ToIdleWhitespaceCompiler.INSTANCE
827827
))
828828
));
829829

@@ -834,7 +834,7 @@ else if (instruction == null)
834834
*/
835835
@NotNull
836836
public static final Compiler SX_DQT =
837-
new KindCompiler(Kind.SX_DQT, PushUnescapeConstCompiler.INSTANCE);
837+
new FilterByKindCompiler(Kind.SX_DQT, ToPushConstUnescapeCompiler.INSTANCE);
838838

839839
/**
840840
* A compiler that compiles name instructions.
@@ -843,7 +843,7 @@ else if (instruction == null)
843843
*/
844844
@NotNull
845845
public static final Compiler SX_NME =
846-
new KindCompiler(Kind.SX_NME, PushEvalAddrCompiler.INSTANCE);
846+
new FilterByKindCompiler(Kind.SX_NME, ToPushEvalAddrCompiler.INSTANCE);
847847

848848
/**
849849
* A compiler that compiles numbers.
@@ -852,7 +852,7 @@ else if (instruction == null)
852852
*/
853853
@NotNull
854854
public static final Compiler SX_NUM =
855-
new KindCompiler(Kind.SX_NUM, PushConstCompiler.INSTANCE);
855+
new FilterByKindCompiler(Kind.SX_NUM, ToPushConstCompiler.INSTANCE);
856856

857857
/**
858858
* A compiler that compiles quotes.
@@ -861,12 +861,12 @@ else if (instruction == null)
861861
*/
862862
@NotNull
863863
public static final Compiler SX_QTE =
864-
new KindCompiler(Kind.SX_QTE, new FlattenCompiler(
865-
new OrderCompiler(
866-
new KindCompiler(Kind.CX_ANC_OPEN, EmptyCompiler.INSTANCE),
867-
new KindCompiler(Kind.CX_ANC_CLOSE, EmptyCompiler.INSTANCE)
864+
new FilterByKindCompiler(Kind.SX_QTE, new FlattenCompiler(
865+
new FirstCompileCompiler(
866+
new FilterByKindCompiler(Kind.CX_ANC_OPEN, ToIdleCompiler.INSTANCE),
867+
new FilterByKindCompiler(Kind.CX_ANC_CLOSE, ToIdleCompiler.INSTANCE)
868868
),
869-
PushConstCompiler.INSTANCE
869+
ToPushConstCompiler.INSTANCE
870870
));
871871

872872
/**
@@ -876,12 +876,12 @@ else if (instruction == null)
876876
*/
877877
@NotNull
878878
public static final Compiler SX_RND =
879-
new KindCompiler(Kind.SX_RND, new FlattenCompiler(
879+
new FilterByKindCompiler(Kind.SX_RND, new FlattenCompiler(
880880
FallbackCompiler.INSTANCE,
881-
new MandatoryCompiler(new OrderCompiler(
882-
new KindCompiler(Kind.CX_ANC_OPEN, EmptyCompiler.INSTANCE),
883-
new KindCompiler(Kind.CX_ANC_CLOSE, EmptyCompiler.INSTANCE),
884-
WhitespaceCompiler.INSTANCE
881+
new MandatoryCompiler(new FirstCompileCompiler(
882+
new FilterByKindCompiler(Kind.CX_ANC_OPEN, ToIdleCompiler.INSTANCE),
883+
new FilterByKindCompiler(Kind.CX_ANC_CLOSE, ToIdleCompiler.INSTANCE),
884+
ToIdleWhitespaceCompiler.INSTANCE
885885
))
886886
));
887887

@@ -892,13 +892,13 @@ else if (instruction == null)
892892
*/
893893
@NotNull
894894
public static final Compiler SX_SQR =
895-
new KindCompiler(Kind.SX_SQR, new FlattenCompiler(
895+
new FilterByKindCompiler(Kind.SX_SQR, new FlattenCompiler(
896896
FallbackCompiler.INSTANCE,
897-
new MandatoryCompiler(new OrderCompiler(
898-
new KindCompiler(Kind.CX_ANC_OPEN, PushConstCompiler.INSTANCE),
899-
new KindCompiler(Kind.CX_ANC_CLOSE, PushConstCompiler.INSTANCE),
900-
new KindCompiler(Kind.SX_CMA, PushConstCompiler.INSTANCE),
901-
WhitespaceCompiler.INSTANCE
897+
new MandatoryCompiler(new FirstCompileCompiler(
898+
new FilterByKindCompiler(Kind.CX_ANC_OPEN, ToPushConstCompiler.INSTANCE),
899+
new FilterByKindCompiler(Kind.CX_ANC_CLOSE, ToPushConstCompiler.INSTANCE),
900+
new FilterByKindCompiler(Kind.SX_CMA, ToPushConstCompiler.INSTANCE),
901+
ToIdleWhitespaceCompiler.INSTANCE
902902
))
903903
));
904904

0 commit comments

Comments
 (0)