Skip to content

Commit 12757c3

Browse files
committed
命令支持内联表达式
1 parent 824040b commit 12757c3

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
![](https://user-images.githubusercontent.com/90548686/236462051-b901f99c-bdef-435c-8ca2-0dda37b25285.png)
2+
[![Stargazers over time](https://starchart.cc/MinecraftFunctionPlusPlus/MCFPP.svg?variant=adaptive)](https://starchart.cc/MinecraftFunctionPlusPlus/MCFPP)
23
[English](./README.md)
34
------------
45

src/main/antlr/mcfppLexer.g4

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LCURL: '{' -> pushMode(DEFAULT_MODE);
1414
RCURL: '}' -> popMode;
1515
MULT: '*' ;
1616
MOD: '%' ;
17-
DIV: '/' ;
17+
DIV: '/' -> pushMode(OrgCommand);
1818
ADD: '+' ;
1919
SUB: '-' ;
2020
INCR: '++' ;
@@ -213,10 +213,6 @@ BooleanConstant
213213

214214
LineString: ('"' .*? '"' )|( '\'' .*? '\'' );
215215

216-
OrgCommand
217-
: '/' [a-z]* ([ ][a-z:._{}\\[0-9A-Z\]]*)+
218-
;
219-
220216
WS : [ \t\r\n\u000C]+ -> skip
221217
;
222218

@@ -232,8 +228,19 @@ LINE_COMMENT
232228
: '#' ~[\r\n]* -> skip
233229
;
234230

231+
mode OrgCommand ;
232+
233+
OrgCommandText
234+
: ~('$'|[\r\n])+ | '$'
235+
;
235236

237+
OrgCommandExprStart
238+
: '${' -> pushMode(DEFAULT_MODE)
239+
;
236240

241+
OrgCommandEnd
242+
: ('\r\n' | '\n') -> popMode
243+
;
237244

238245
mode MultiLineString ;
239246

src/main/antlr/mcfppParser.g4

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,16 @@ executeExpression
477477
;
478478

479479
orgCommand
480-
: OrgCommand
480+
: DIV orgCommandContent+ OrgCommandEnd
481+
;
482+
483+
orgCommandContent
484+
: orgCommandExpression
485+
| OrgCommandText
486+
;
487+
488+
orgCommandExpression
489+
: OrgCommandExprStart expression '}'
481490
;
482491

483492
controlStatement

src/main/kotlin/top/mcfpp/antlr/MCFPPImVisitor.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,26 @@ open class MCFPPImVisitor: mcfppParserBaseVisitor<Any?>() {
914914

915915
//endregion
916916

917-
917+
/**
918+
* 使用原版命令
919+
*/
918920
@InsertCommand
919921
override fun visitOrgCommand(ctx: mcfppParser.OrgCommandContext):Any? {
920922
Project.ctx = ctx
921-
Function.addCommand(ctx.text.substring(1))
923+
val sb = StringBuilder()
924+
for (content in ctx.orgCommandContent()){
925+
if(content.OrgCommandText() != null){
926+
sb.append(content.OrgCommandText().text)
927+
}else{
928+
val exp = MCFPPExprVisitor().visit(content.orgCommandExpression().expression())
929+
if(exp is MCFPPValue<*>){
930+
sb.append(exp.value)
931+
}else{
932+
sb.append(exp)
933+
}
934+
}
935+
}
936+
Function.addCommand(sb.toString())
922937
return null
923938
}
924939

src/test/kotlin/top/mcfpp/test/MNITest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class MNITest {
1313
int qwq = 1;
1414
print(qwq::jvm.identifier);
1515
print(qwq::jvm.sbObject.toString());
16+
/say ${'$'}{qwq}
17+
/say ${'$'}{qwq::jvm.identifier}
18+
/say ${'$'}{qwq::jvm.sbObject}
1619
}
1720
""".trimIndent()
1821
MCFPPStringTest.readFromString(test)

0 commit comments

Comments
 (0)