Skip to content

Commit ecdc04b

Browse files
committed
feat: rewrite with builder pattern
1 parent 119ca57 commit ecdc04b

42 files changed

Lines changed: 10531 additions & 8303 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,61 @@
1-
namespace MySpace;
2-
3-
InBadlands := SurfaceCondition {
4-
Biome [
5-
minecraft:badlands
6-
minecraft:eroded_badlands
7-
minecraft:wooded_badlands
8-
]
9-
}
10-
11-
SkyTerracotta := SurfaceRule {
12-
If (AboveSurface) Block stone
13-
}
14-
15-
NonHoleOrangeTerracotta := SurfaceRule { If (Not(Hole)) Block orange_terracotta }
16-
17-
TerracottaBands := SurfaceRule {
18-
If (StoneDepth Floor 0 Sub 0)
19-
Sequence [
20-
If (YAbove 74 1 Add)
21-
Sequence [
22-
If (Or (
23-
Noise minecraft:surface [-0.909 -0.5454]
24-
Noise minecraft:surface [-0.1818 0.1818]
25-
Noise minecraft:surface [0.5454 0.909]
26-
))
27-
Block minecraft:terracotta
28-
29-
Bandlands
1+
namespace MySpace {
2+
Surface {
3+
InBadlands = Biome(
4+
minecraft:badlands,
5+
minecraft:eroded_badlands,
6+
minecraft:wooded_badlands
7+
)
8+
SkyTerracotta = If (AboveSurface()) Block(minecraft:stone)
9+
NonHoleOrangeTerracotta = If(!Hole()) Block(minecraft:orange_terracotta)
10+
TerracottaBands = If(StoneDepth(Floor)) [
11+
If(
12+
Or(
13+
Noise(minecraft:surface).Min(-0.909).Max(-0.5454)
14+
Noise(minecraft:surface).Min(-0.1818).Max(0.1818)
15+
Noise(minecraft:surface).Min(0.5454).Max(0.909)
16+
)
17+
) Block(minecraft:terracotta)
18+
Bandlands()
3019
]
31-
]
32-
}
20+
WhiteTerracotta = If (AboveWater().Offset(-6).Mul(-1).Add())
21+
Block(minecraft:white_terracotta)
3322

34-
WhiteTerracotta := SurfaceRule { If (AboveWater -6 -1 Add) Block white_terracotta }
35-
OrangeTerracotta := SurfaceRule { If (YAbove 63 0 Sub ) Block orange_terracotta }
36-
StoneAndGravel := SurfaceRule {
37-
Sequence [
38-
If (StoneDepth Ceiling 0 Sub 0) Block stone
39-
Block Gravel
40-
]
41-
}
42-
43-
SurfaceSands := SurfaceRule {
44-
If (AboveWater -1 0 Sub) Sequence [
45-
If (StoneDepth Ceiling 0 Sub 0) Block minecraft:red_sandstone
46-
Block minecraft:red_sand
47-
]
48-
}
23+
OrangeTerracotta = If (YAbove(63)) Block(minecraft:orange_terracotta)
24+
StoneAndGravel = [
25+
If (StoneDepth(Ceiling)) Block(minecraft:stone)
26+
Block(minecraft:gravel)
27+
]
28+
SurfaceSands = If (AboveWater().Offset(-1)) [
29+
If (StoneDepth(Ceiling)) Block(minecraft:red_sandstone)
30+
Block(minecraft:red_sand)
31+
]
4932

50-
OrangeTerracottaEdge := SurfaceRule {
51-
If ( And (
52-
YAbove 63 0 Sub
53-
Not(YAbove 74 1 Add)
54-
) ) Block minecraft:orange_terracotta
55-
}
33+
OrangeTerracottaEdge = If(
34+
And(
35+
YAbove(63)
36+
!YAbove(74).Mul(1).Add()
37+
)
38+
) Block(minecraft:orange_terracotta)
39+
40+
Badlands = If(InBadlands) [
41+
If(YAbove(63)) [
42+
SkyTerracotta
43+
TerracottaBands
44+
SurfaceSands
45+
NonHoleOrangeTerracotta
46+
WhiteTerracotta
47+
StoneAndGravel
48+
]
49+
50+
If(YAbove(63).Mul(-1).Add()) [
51+
OrangeTerracottaEdge
52+
Bandlands()
53+
]
54+
55+
If (StoneDepth(Floor)) [
56+
WhiteTerracotta
57+
]
58+
]
59+
}
5660

57-
Badlands := SurfaceRule {
58-
If (InBadlands) Sequence [
59-
If (YAbove 63 0 Sub) Sequence [
60-
SkyTerracotta
61-
TerracottaBands
62-
SurfaceSands
63-
NonHoleOrangeTerracotta
64-
WhiteTerracotta
65-
StoneAndGravel
66-
]
67-
If (YAbove 63 -1 Add) Sequence [
68-
OrangeTerracottaEdge
69-
Bandlands
70-
]
71-
If (StoneDepth Floor 0 Add 0) WhiteTerracotta
72-
]
7361
}

flake.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
set -e
2424
src="./grammar"
2525
dst="./lang"
26-
${pkgs.antlr4}/bin/antlr4 -Dlanguage=Go $src/Main_Lexer.g4 -o $dst -package grammar;
27-
${pkgs.antlr4}/bin/antlr4 -Dlanguage=Go $src/Main_Parser.g4 -lib $dst/grammar -o $dst -package grammar;
26+
${pkgs.antlr4}/bin/antlr4 -Dlanguage=Go $src/MinecraftMetascript.g4 -o $dst -package grammar;
2827
'';
2928
};
3029
in

grammar/Core_Lang.g4

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
grammar Core_Lang;
2+
3+
resourceReference: (Identifier ':')? Identifier;
4+
5+
6+
Int: '-'? [0-9]+;
7+
Float: ('-'? [0-9]+ '.' [0-9]+);
8+
String: '"' ~[\r\n]* '"';
9+
number: Int | Float;
10+
11+
NL: [\n];
12+
WS: [ \t]+ -> skip;
13+
Identifier: [a-zA-Z][a-zA-Z0-9_]*;
14+
15+
BlockComment: '/*' .*? '*/' -> channel(HIDDEN);
16+
LineComment: '//' ~[\r\n]* -> channel(HIDDEN);

grammar/Lang_Lexer.g4

Lines changed: 0 additions & 34 deletions
This file was deleted.

grammar/Lang_Parser.g4

Lines changed: 0 additions & 10 deletions
This file was deleted.

grammar/Lexer.tokens

Lines changed: 0 additions & 8 deletions
This file was deleted.

grammar/Main_Lexer.g4

Lines changed: 0 additions & 39 deletions
This file was deleted.

grammar/Main_Parser.g4

Lines changed: 0 additions & 14 deletions
This file was deleted.

grammar/MinecraftMetascript.g4

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
grammar MinecraftMetascript;
2+
3+
import Surface, Core_Lang;
4+
5+
script: (namespace NL*)*;
6+
7+
namespaceDeclaration: 'namespace' Identifier;
8+
namespace: namespaceDeclaration NL* '{' NL* (contentBlocks NL*)* NL* '}';
9+
10+
contentBlocks: surface;

grammar/Surface.g4

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
grammar Surface;
2+
import Core_Lang;
3+
4+
surface: 'Surface' NL* '{' NL* (surfaceStatement NL*)* NL* '}';
5+
surfaceStatement: verticalAnchorDeclaration | surfaceConditionDeclaration | surfaceRuleDeclaration;
6+
7+
verticalAnchor: ('~'? Int) | Identifier;
8+
verticalAnchorDeclaration: Identifier '=' verticalAnchor;
9+
10+
sharedBuilder_Offset: '.Offset(' Int ')';
11+
sharedBuilder_Add:'.Add()';
12+
sharedBuilder_Mul: '.Mul(' number ')';
13+
sharedBuilder_MulInt: '.Mul(' Int ')';
14+
15+
surfaceCondition:
16+
(
17+
surfaceCondition_Not
18+
| surfaceCondition_AboveSurface
19+
| surfaceCondition_Biome
20+
| surfaceCondition_Hole
21+
| surfaceCondition_Steep
22+
| surfaceCondition_Freezing
23+
| surfaceCondition_Noise
24+
| surfaceCondition_StoneDepth
25+
| surfaceCondition_AboveWater
26+
| surfaceCondition_YAbove
27+
| surfaceCondition_Reference
28+
| surfaceCondition_And
29+
| surfaceCondition_Or
30+
| surfaceCondition_VerticalGradient
31+
)
32+
;
33+
surfaceConditionDeclaration: Identifier '=' surfaceCondition;
34+
35+
surfaceCondition_Not: '!' surfaceCondition;
36+
surfaceCondition_And: 'And' NL* '(' NL* (surfaceCondition NL*)* surfaceCondition NL* ')';
37+
surfaceCondition_Or: 'Or' NL* '(' NL* (surfaceCondition NL*)* surfaceCondition NL* ')';
38+
39+
40+
surfaceCondition_Reference: resourceReference;
41+
surfaceCondition_AboveSurface: 'AboveSurface()';
42+
surfaceCondition_Biome: 'Biome' '(' NL* (resourceReference ',' NL*)* NL* resourceReference NL* ')';
43+
44+
surfaceCondition_Hole: 'Hole' '(' ')';
45+
surfaceCondition_Steep: 'Steep' '(' ')';
46+
surfaceCondition_Freezing: 'Freezing' '(' ')';
47+
48+
49+
// These are split out to make them easier to differentiate in the go code
50+
surfaceCondition_NoiseBuilder_Min: '.Min(' number ')';
51+
surfaceCondition_NoiseBuilder_Max: '.Max(' number ')';
52+
surfaceCondition_NoiseBuilder: surfaceCondition_NoiseBuilder_Max | surfaceCondition_NoiseBuilder_Min;
53+
surfaceCondition_Noise: 'Noise' '(' resourceReference ')' NL* (surfaceCondition_NoiseBuilder NL*)*;
54+
55+
StoneDepthMode: 'Floor' | 'Ceiling';
56+
surfaceCondition_StoneDepth:
57+
'StoneDepth(' StoneDepthMode ')' NL*
58+
(surfaceCondition_StoneDepthBuilder NL*)*;
59+
60+
surfaceCondition_StoneDepthBuilder:
61+
sharedBuilder_Offset
62+
| sharedBuilder_Add
63+
| surfaceCondition_StoneDepthBuilder_SecondaryDepthRange
64+
;
65+
66+
surfaceCondition_StoneDepthBuilder_SecondaryDepthRange:'.secondaryDepthRange(' Int ')';
67+
68+
surfaceCondition_VerticalGradient: 'VerticalGradient' '(' String ')' NL* (surfaceCondition_VerticalGradientBuilder NL*)*;
69+
surfaceCondition_VerticalGradientBuilder:
70+
surfaceCondition_VerticalGradientBuilder_Top
71+
| surfaceCondition_VerticalGradientBuilder_Bottom
72+
;
73+
74+
surfaceCondition_VerticalGradientBuilder_Top: '.Top' '(' verticalAnchor ')';
75+
surfaceCondition_VerticalGradientBuilder_Bottom: '.Bottom' '(' verticalAnchor ')';
76+
77+
78+
surfaceCondition_AboveWater: 'AboveWater' '(' ')' NL* (surfaceCondition_AboveWaterBuilder NL*)*;
79+
surfaceCondition_AboveWaterBuilder:
80+
sharedBuilder_Offset
81+
| sharedBuilder_Add
82+
| sharedBuilder_Mul;
83+
84+
surfaceCondition_YAbove: 'YAbove' '(' verticalAnchor ')' NL* (surfaceCondition_YAboveBuilder NL*)* ;
85+
surfaceCondition_YAboveBuilder: sharedBuilder_MulInt | sharedBuilder_Add;
86+
87+
surfaceRuleDeclaration: Identifier '=' surfaceRule;
88+
surfaceRule: surfaceRule_Block
89+
| surfaceRule_Sequence
90+
| surfaceRule_Reference
91+
| surfaceRule_If
92+
| surfaceRule_Bandlands;
93+
94+
surfaceRule_Reference: resourceReference;
95+
surfaceRule_Block: 'Block' '(' resourceReference ')';
96+
surfaceRule_Sequence: '[' NL* (surfaceRule NL*)* NL* ']';
97+
surfaceRule_Bandlands: 'Bandlands' '(' ')';
98+
surfaceRule_If: 'If' NL* '(' NL* surfaceCondition NL* ')' NL* surfaceRule;

0 commit comments

Comments
 (0)