Skip to content

Commit 0b7ff57

Browse files
committed
antlr4: Moved the :: up the precedece
1 parent 1015d01 commit 0b7ff57

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

src/main/antlr4/PiccodeScript.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ literal_expr :
6060
expr_stmt: expr;
6161
// parser rules
6262
expr
63-
: expr LPAREN call_expr_list? RPAREN
63+
: expr CC expr
64+
| expr LPAREN call_expr_list? RPAREN
6465
| var_decl
6566
| closure_decl
66-
| expr CC expr
6767
| expr DOT expr
6868
| expr MUL expr
6969
| expr DIV expr

src/main/java/org/piccode/antlr4/PiccodeScript.interp

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

src/main/java/org/piccode/antlr4/PiccodeScriptParser.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,11 +1332,11 @@ private ExprContext expr(int _p) throws RecognitionException {
13321332
_localctx = new ExprContext(_parentctx, _parentState);
13331333
pushNewRecursionContext(_localctx, _startState, RULE_expr);
13341334
setState(183);
1335-
if (!(precpred(_ctx, 32))) throw new FailedPredicateException(this, "precpred(_ctx, 32)");
1335+
if (!(precpred(_ctx, 35))) throw new FailedPredicateException(this, "precpred(_ctx, 35)");
13361336
setState(184);
13371337
match(CC);
13381338
setState(185);
1339-
expr(33);
1339+
expr(36);
13401340
}
13411341
break;
13421342
case 2:
@@ -1596,7 +1596,7 @@ private ExprContext expr(int _p) throws RecognitionException {
15961596
_localctx = new ExprContext(_parentctx, _parentState);
15971597
pushNewRecursionContext(_localctx, _startState, RULE_expr);
15981598
setState(249);
1599-
if (!(precpred(_ctx, 35))) throw new FailedPredicateException(this, "precpred(_ctx, 35)");
1599+
if (!(precpred(_ctx, 34))) throw new FailedPredicateException(this, "precpred(_ctx, 34)");
16001600
setState(250);
16011601
match(LPAREN);
16021602
setState(252);
@@ -2752,7 +2752,7 @@ public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) {
27522752
private boolean expr_sempred(ExprContext _localctx, int predIndex) {
27532753
switch (predIndex) {
27542754
case 0:
2755-
return precpred(_ctx, 32);
2755+
return precpred(_ctx, 35);
27562756
case 1:
27572757
return precpred(_ctx, 31);
27582758
case 2:
@@ -2796,7 +2796,7 @@ private boolean expr_sempred(ExprContext _localctx, int predIndex) {
27962796
case 21:
27972797
return precpred(_ctx, 11);
27982798
case 22:
2799-
return precpred(_ctx, 35);
2799+
return precpred(_ctx, 34);
28002800
}
28012801
return true;
28022802
}
@@ -2872,8 +2872,8 @@ private boolean expr_sempred(ExprContext _localctx, int predIndex) {
28722872
",\2\2\u00b7\u00a6\3\2\2\2\u00b7\u00a8\3\2\2\2\u00b7\u00a9\3\2\2\2\u00b7"+
28732873
"\u00ae\3\2\2\2\u00b7\u00af\3\2\2\2\u00b7\u00b0\3\2\2\2\u00b7\u00b1\3\2"+
28742874
"\2\2\u00b7\u00b2\3\2\2\2\u00b7\u00b3\3\2\2\2\u00b7\u00b4\3\2\2\2\u00b7"+
2875-
"\u00b5\3\2\2\2\u00b7\u00b6\3\2\2\2\u00b8\u0103\3\2\2\2\u00b9\u00ba\f\""+
2876-
"\2\2\u00ba\u00bb\7\26\2\2\u00bb\u0102\5\"\22#\u00bc\u00bd\f!\2\2\u00bd"+
2875+
"\u00b5\3\2\2\2\u00b7\u00b6\3\2\2\2\u00b8\u0103\3\2\2\2\u00b9\u00ba\f%"+
2876+
"\2\2\u00ba\u00bb\7\26\2\2\u00bb\u0102\5\"\22&\u00bc\u00bd\f!\2\2\u00bd"+
28772877
"\u00be\7-\2\2\u00be\u0102\5\"\22\"\u00bf\u00c0\f \2\2\u00c0\u00c1\7\5"+
28782878
"\2\2\u00c1\u0102\5\"\22!\u00c2\u00c3\f\37\2\2\u00c3\u00c4\7\6\2\2\u00c4"+
28792879
"\u0102\5\"\22 \u00c5\u00c6\f\36\2\2\u00c6\u00c7\7\7\2\2\u00c7\u0102\5"+
@@ -2892,7 +2892,7 @@ private boolean expr_sempred(ExprContext _localctx, int predIndex) {
28922892
"\f\20\2\2\u00f0\u00f1\7\21\2\2\u00f1\u0102\5\"\22\21\u00f2\u00f3\f\17"+
28932893
"\2\2\u00f3\u00f4\7\23\2\2\u00f4\u0102\5\"\22\20\u00f5\u00f6\f\16\2\2\u00f6"+
28942894
"\u00f7\7\22\2\2\u00f7\u0102\5\"\22\17\u00f8\u00f9\f\r\2\2\u00f9\u00fa"+
2895-
"\7\35\2\2\u00fa\u0102\5\"\22\16\u00fb\u00fc\f%\2\2\u00fc\u00fe\7\31\2"+
2895+
"\7\35\2\2\u00fa\u0102\5\"\22\16\u00fb\u00fc\f$\2\2\u00fc\u00fe\7\31\2"+
28962896
"\2\u00fd\u00ff\5<\37\2\u00fe\u00fd\3\2\2\2\u00fe\u00ff\3\2\2\2\u00ff\u0100"+
28972897
"\3\2\2\2\u0100\u0102\7\32\2\2\u0101\u00b9\3\2\2\2\u0101\u00bc\3\2\2\2"+
28982898
"\u0101\u00bf\3\2\2\2\u0101\u00c2\3\2\2\2\u0101\u00c5\3\2\2\2\u0101\u00c8"+

src/main/java/org/piccode/ast/Ast.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public static Location getLocation(Ast node) {
7171
if (node instanceof DoExprAst dot) {
7272
return new Location(dot.line, dot.column);
7373
}
74+
75+
if (node instanceof CCOperationAst cc) {
76+
return new Location(cc.line, cc.column);
77+
}
7478

7579
if (node instanceof DotOperationAst dot) {
7680
return new Location(dot.line, dot.column);

src/main/java/org/piccode/ast/CallAst.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public String toString() {
4646
public PiccodeValue execute() {
4747
var expr_val = expr.execute();
4848
if (!(expr_val instanceof PiccodeClosure) && !(expr_val instanceof NativeFunction)) {
49-
throw new PiccodeException(file, line, column, "Attempt to call a non-callable expression");
49+
throw new PiccodeException(file, line, column, "Attempt to call a non-callable expression. Issue is: " + expr + " = " + expr_val);
5050
}
5151
lastCall = expr;
5252

src/main/java/org/piccode/rt/modules/PiccodeTypesModule.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.util.Arrays;
55
import java.util.List;
66
import org.piccode.rt.PiccodeArray;
7+
import org.piccode.rt.PiccodeBoolean;
78
import org.piccode.rt.PiccodeNumber;
9+
import org.piccode.rt.PiccodeObject;
10+
import org.piccode.rt.PiccodeString;
811
import org.piccode.rt.PiccodeTuple;
912
import org.piccode.rt.PiccodeValue;
1013

@@ -16,16 +19,29 @@
1619
public class PiccodeTypesModule {
1720
public static void addFunctions() {
1821

19-
NativeFunctionFactory.create("tuplesize", List.of("tuple"), (args, namedArgs) -> {
20-
var arr = ((PiccodeTuple) namedArgs.get("tuple")).array().length;
21-
return new PiccodeNumber("" + arr);
22+
NativeFunctionFactory.create("isnumber", List.of("value"), (args, namedArgs) -> {
23+
var value = namedArgs.get("value");
24+
return new PiccodeBoolean(String.valueOf(value instanceof PiccodeNumber));
2225
});
2326

24-
NativeFunctionFactory.create("tupletoarray", List.of("tuple"), (args, namedArgs) -> {
25-
var arr = ((PiccodeTuple) namedArgs.get("tuple")).array();
26-
var list = new ArrayList<PiccodeValue>();
27-
list.addAll(Arrays.asList(arr));
28-
return new PiccodeArray(list);
27+
NativeFunctionFactory.create("isstring", List.of("value"), (args, namedArgs) -> {
28+
var value = namedArgs.get("value");
29+
return new PiccodeBoolean(String.valueOf(value instanceof PiccodeString));
30+
});
31+
32+
NativeFunctionFactory.create("isobject", List.of("value"), (args, namedArgs) -> {
33+
var value = namedArgs.get("value");
34+
return new PiccodeBoolean(String.valueOf(value instanceof PiccodeObject));
35+
});
36+
37+
NativeFunctionFactory.create("isarr", List.of("value"), (args, namedArgs) -> {
38+
var value = namedArgs.get("value");
39+
return new PiccodeBoolean(String.valueOf(value instanceof PiccodeArray));
40+
});
41+
42+
NativeFunctionFactory.create("istuple", List.of("value"), (args, namedArgs) -> {
43+
var value = namedArgs.get("value");
44+
return new PiccodeBoolean(String.valueOf(value instanceof PiccodeTuple));
2945
});
3046

3147
}

0 commit comments

Comments
 (0)