Skip to content

Commit d04a6e6

Browse files
committed
enums as field ref
1 parent 173ba91 commit d04a6e6

12 files changed

Lines changed: 395 additions & 487 deletions

File tree

src/main/antlr/nts.g4

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ rval : type_literal
5555
| '(' rval ')'
5656

5757
| function_call
58-
| type_enum
5958
| type_bool
6059
| variable_reference
6160
| type_comparison
@@ -68,8 +67,6 @@ iterable :
6867
| variable_reference
6968
;
7069

71-
type_enum : type=IDENTIFIER '.' value=IDENTIFIER ;
72-
7370
function_call : function_name=IDENTIFIER arguments='('argument? (',' argument)* ')' ('as' type_cast= IDENTIFIER)?;
7471

7572
if_statement : IF condition_expression terminator statement_list END ;
@@ -132,6 +129,7 @@ IN: I N;
132129
FUNCTION: ( F N | F U N C T I O N );
133130

134131
IDENTIFIER : [a-zA-Z_][a-zA-Z0-9_]*;
132+
ENUM_IDENTIFIER : [A-Z_]*;
135133
fragment DIGIT : ('0'..'9');
136134
INT : [0-9]+;
137135
DOT : '.';

src/main/java/cz/neumimto/nts/bytecode/VisitorImpl.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,27 @@ public ScriptContext visitAssignment_statement(ntsParser.Assignment_statementCon
6363
@Override
6464
public ScriptContext visitGetField_statement(ntsParser.GetField_statementContext ctx) {
6565
String fieldName = ctx.field.getText();
66-
Variable variable = scriptContext.getVariable(ctx.fieldOwner.getText()).orElseThrow(() -> new RuntimeException("Unknown variable " + ctx.fieldOwner.getText()));
66+
String fieldOwnerName = ctx.fieldOwner.getText();
67+
68+
Optional<Variable> variableOptional = scriptContext.getVariable(ctx.fieldOwner.getText());
69+
70+
if (variableOptional.isEmpty()) {
71+
Enum enumValue = scriptContext.getEnumValue(fieldOwnerName, fieldName);
72+
addInsn(FieldAccess.forEnumeration(new EnumerationDescription.ForLoadedEnumeration(enumValue)));
73+
return scriptContext;
74+
}
75+
76+
77+
Variable variable = variableOptional.get();
6778
addInsn(variable.load());
68-
String variableName = ctx.fieldOwner.getText();
6979

7080
Variable c = scriptContext.currentScope().lastVariableOnStack;
7181

7282
//when we accessing function param
7383
if (c == null) {
7484
c = scriptContext.currentScope().findVariable(ctx.fieldOwner.getText());
7585
}
76-
Variable fieldOwner = scriptContext.getVariable(variableName).get();
86+
Variable fieldOwner = scriptContext.getVariable(fieldOwnerName).get();
7787

7888
try {
7989
Field field = fieldOwner.getRuntimeType().getField(fieldName);
@@ -222,8 +232,9 @@ public ScriptContext visitReturn_statement(ntsParser.Return_statementContext ctx
222232
var rtype = ctx.rval();
223233

224234

225-
if (rtype.type_enum() != null || rtype.type_literal() != null) {
235+
if (rtype.getField_statement() != null || rtype.type_literal() != null) {
226236
visitChildren(rtype);
237+
//todo enum or field rimirive from field
227238
addInsn(MethodReturn.REFERENCE);
228239

229240
} else if (rtype.type_bool() != null) {
@@ -239,15 +250,6 @@ public ScriptContext visitReturn_statement(ntsParser.Return_statementContext ctx
239250
return scriptContext;
240251
}
241252

242-
@Override
243-
public ScriptContext visitType_enum(ntsParser.Type_enumContext ctx) {
244-
String type = ctx.type.getText();
245-
String value = ctx.value.getText();
246-
Enum enumValue = scriptContext.getEnumValue(type, value);
247-
addInsn(FieldAccess.forEnumeration(new EnumerationDescription.ForLoadedEnumeration(enumValue)));
248-
return scriptContext;
249-
}
250-
251253
@Override
252254
public ScriptContext visitFunction_call(ntsParser.Function_callContext ctx) {
253255
String line = ctx.getText();

src/main/java/cz/neumimto/nts/nts.interp

Lines changed: 1 addition & 2 deletions
Large diffs are not rendered by default.

src/main/java/cz/neumimto/nts/nts.tokens

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ FOREACH=29
3030
IN=30
3131
FUNCTION=31
3232
IDENTIFIER=32
33-
INT=33
34-
DOT=34
33+
ENUM_IDENTIFIER=33
34+
INT=34
35+
DOT=35
3536
'as'=1
3637
'('=2
3738
')'=3
@@ -48,4 +49,4 @@ DOT=34
4849
'!='=21
4950
'>'=24
5051
'<'=25
51-
'.'=34
52+
'.'=35

src/main/java/cz/neumimto/nts/ntsBaseListener.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,6 @@ public class ntsBaseListener implements ntsListener {
191191
* <p>The default implementation does nothing.</p>
192192
*/
193193
@Override public void exitIterable(ntsParser.IterableContext ctx) { }
194-
/**
195-
* {@inheritDoc}
196-
*
197-
* <p>The default implementation does nothing.</p>
198-
*/
199-
@Override public void enterType_enum(ntsParser.Type_enumContext ctx) { }
200-
/**
201-
* {@inheritDoc}
202-
*
203-
* <p>The default implementation does nothing.</p>
204-
*/
205-
@Override public void exitType_enum(ntsParser.Type_enumContext ctx) { }
206194
/**
207195
* {@inheritDoc}
208196
*

src/main/java/cz/neumimto/nts/ntsBaseVisitor.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ public class ntsBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements nt
116116
* {@link #visitChildren} on {@code ctx}.</p>
117117
*/
118118
@Override public T visitIterable(ntsParser.IterableContext ctx) { return visitChildren(ctx); }
119-
/**
120-
* {@inheritDoc}
121-
*
122-
* <p>The default implementation returns the result of calling
123-
* {@link #visitChildren} on {@code ctx}.</p>
124-
*/
125-
@Override public T visitType_enum(ntsParser.Type_enumContext ctx) { return visitChildren(ctx); }
126119
/**
127120
* {@inheritDoc}
128121
*

src/main/java/cz/neumimto/nts/ntsLexer.interp

Lines changed: 4 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)