@@ -3,6 +3,8 @@ package top.mcfpp.antlr
33import top.mcfpp.Project
44import top.mcfpp.annotations.InsertCommand
55import top.mcfpp.lang.*
6+ import top.mcfpp.lang.type.MCFPPType
7+ import top.mcfpp.lang.value.MCFPPValue
68import top.mcfpp.model.*
79import top.mcfpp.model.function.Function
810import top.mcfpp.model.field.GlobalField
@@ -41,33 +43,12 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
4143 @Override
4244 override fun visitVarWithSelector (ctx : mcfppParser.VarWithSelectorContext ): Var <* > {
4345 Project .ctx = ctx
44- val namespaceID : Pair <String ?, String >
4546 if (ctx.primary() != null ){
4647 currSelector = visit(ctx.primary())
4748 }
4849 if (currSelector is UnknownVar ){
49- if (ctx.primary() != null || ctx.type().className() != null ){
50- namespaceID = if (ctx.primary() != null ){
51- null to ctx.primary().text
52- } else {
53- StringHelper .splitNamespaceID(ctx.type().text)
54- }
55- val o = GlobalField .getObject(namespaceID.first, namespaceID.second)
56- if (o != null ) {
57- currSelector = o.getType()
58- } else {
59- LogProcessor .error(" Undefined type: ${namespaceID.second} " )
60- currSelector = UnknownVar (" ${ctx.type().className().text} _type_" + UUID .randomUUID())
61- }
62- }else {
63- currSelector = CompoundDataCompanion (
64- // 基本类型
65- when (ctx.type().text){
66- " int" -> MCInt .data
67- else -> TODO ()
68- }
69- )
70- }
50+ val type = MCFPPType .parseFromIdentifier(ctx.type().text, Function .currFunction.field)
51+ currSelector = type
7152 }
7253 for (selector in ctx.selector()){
7354 visit(selector)
@@ -77,8 +58,9 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
7758
7859 @Override
7960 override fun visitSelector (ctx : mcfppParser.SelectorContext ? ): Var <* > {
80- currSelector = visit(ctx!! .`var `())!! .getTempVar()
81- return (currSelector as Var <* >)
61+ // 进入visitVar,currSelector作为成员选择的上下文
62+ currSelector = visit(ctx!! .`var `())
63+ return currSelector as Var <* >
8264 }
8365
8466 /* *
@@ -95,6 +77,31 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
9577 } else if (ctx.value() != null ) {
9678 // 数字
9779 return visit(ctx.value())
80+ } else if (ctx.range() != null ){
81+ // 是范围
82+ val left = ctx.range().num1?.let { visit(it) }
83+ val right = ctx.range().num2?.let { visit(it) }
84+ if (left is MCNumber <* >? && right is MCNumber <* >? ){
85+ if (left is MCFPPValue <* >? && right is MCFPPValue <* >? ){
86+ val leftValue = left?.value.toString().toFloatOrNull()
87+ val rightValue = right?.value.toString().toFloatOrNull()
88+ return RangeVarConcrete (leftValue to rightValue)
89+ }else {
90+ val range = RangeVar ()
91+ if (left is MCInt ){
92+ range.left = MCFloat (range.identifier + " _left" )
93+ }
94+ if (right is MCInt ){
95+ range.right = MCFloat (range.identifier + " _right" )
96+ }
97+ left?.let { range.left.assign(it) }
98+ right?.let { range.right.assign(it) }
99+ return range
100+ }
101+ }else {
102+ LogProcessor .error(" Range sides should be a number: ${left?.type} and ${right?.type} " )
103+ return UnknownVar (" range_" + UUID .randomUUID())
104+ }
98105 } else {
99106 // this或者super
100107 val s = if (ctx.SUPER () != null ){
@@ -120,7 +127,7 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
120127 @InsertCommand
121128 override fun visitVar (ctx : mcfppParser.VarContext ): Var <* > {
122129 Project .ctx = ctx
123- return if (ctx.Identifier () != null && ctx.arguments() == null ) {
130+ if (ctx.Identifier () != null && ctx.arguments() == null ) {
124131 // 变量
125132 // 没有数组选取
126133 val qwq: String = ctx.Identifier ().text
@@ -142,19 +149,38 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
142149 if (ctx.identifierSuffix() == null || ctx.identifierSuffix().size == 0 ) {
143150 return re
144151 } else {
145- if (re is Indexable <* >){
146- for (value in ctx.identifierSuffix()) {
152+ for (value in ctx.identifierSuffix()) {
153+ if (value.conditionalExpression() != null ){
154+ if (re !is Indexable <* >){
155+ LogProcessor .error(" Cannot index ${re.type} " )
156+ return UnknownVar (" ${re.identifier} _index_" + UUID .randomUUID())
157+ }
158+ // 索引
147159 val index = visit(value.conditionalExpression())!!
148160 re = (re as Indexable <* >).getByIndex(index)
161+ }else {
162+ if (! re.isTemp) re = re.getTempVar()
163+ // 初始化
164+ for (initializer in value.objectInitializer()){
165+ val id = initializer.Identifier ().text
166+ val v = visit(initializer.expression())
167+ val (m, b) = re.getMemberVar(id, re.getAccess(Function .currFunction))
168+ if (! b){
169+ LogProcessor .error(" Cannot access member $id " )
170+ }
171+ if (m == null ) {
172+ LogProcessor .error(" Member $id not found" )
173+ continue
174+ }
175+ m.replacedBy(m.assign(v))
176+ }
149177 }
150- }else {
151- throw IllegalArgumentException (" Cannot index ${re.type} " )
152178 }
153179 return re
154180 }
155181 } else if (ctx.expression() != null ) {
156182 // '(' expression ')'
157- visit(ctx.expression())
183+ return McfppLeftExprVisitor (). visit(ctx.expression())
158184 } else {
159185 // 函数的调用
160186 Function .addComment(ctx.text)
@@ -184,15 +210,28 @@ class McfppLeftExprVisitor : mcfppParserBaseVisitor<Var<*>>(){
184210 }
185211 // 调用函数
186212 return if (func is UnknownFunction ) {
187- // 可能是构造函数
188- var cls: Class ? = GlobalField .getClass(p.first, p.second)
189- if (cls == null ) {
190- LogProcessor .error(" Function " + ctx.text + " not defined" )
191- Function .addComment(" [Failed to Compile]${ctx.text} " )
192- func.invoke(normalArgs,currSelector)
193- return func.returnVar
194- }
195- if (cls is GenericClass ){
213+ var cls: Class ? = if (ctx.arguments().readOnlyArgs() != null ){
214+ GlobalField .getClass(p.first, p.second ,readOnlyArgs.map { it.type })
215+ }else {
216+ GlobalField .getClass(p.first, p.second)
217+ }
218+ // 可能是构造函数
219+ if (cls == null ) {
220+ val template: DataTemplate ? = GlobalField .getTemplate(p.first, p.second)
221+ if (template == null ){
222+ LogProcessor .error(" Function ${func.identifier} <${readOnlyArgs.joinToString(" ," ) { it.type.typeName }} >(${normalArgs.map { it.type.typeName }.joinToString(" ," )} ) not defined" )
223+ Function .addComment(" [Failed to Compile]${ctx.text} " )
224+ func.invoke(normalArgs,currSelector)
225+ return func.returnVar
226+ }
227+ // 模板默认构造函数
228+ if (readOnlyArgs.isNotEmpty() || normalArgs.isNotEmpty()){
229+ LogProcessor .error(" Template constructor ${template.identifier} cannot have arguments" )
230+ return UnknownVar (" ${template.identifier} _type_" + UUID .randomUUID())
231+ }
232+ return DataTemplateObject (template)
233+ }
234+ if (cls is GenericClass ){
196235 // 实例化泛型函数
197236 cls = cls.compile(readOnlyArgs)
198237 }
0 commit comments