Skip to content

Commit 602abed

Browse files
committed
kernel: catch some invalid uses of '~'; add tests
Also, print a (more) helpful error message. Before: gap> ~; Error, '~' does not have a value here not in any function at *stdin*:1 gap> x -> ~; function( x ) ... end gap> x -> (1,~); function( x ) ... end gap> (1,~); Error, '~' does not have a value here not in any function at *stdin*:4 After: gap> ~; Syntax error: '~' not allowed here ~; ^ gap> x -> ~; Syntax error: '~' not allowed here x -> ~; ^ gap> x -> (1,~); Syntax error: '~' not allowed here x -> (1,~); ^ gap> (1,~); Syntax error: '~' not allowed here (1,~); ^
1 parent 94242b8 commit 602abed

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

src/read.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ static void ReadFuncExprBody(
12351235
// push the new local variables list
12361236
PushPlist(STATE(StackNams), args.nams);
12371237

1238-
// begin interpreting the function expression (with 1 argument)
1238+
// begin interpreting the function expression
12391239
TRY_IF_NO_ERROR {
12401240
IntrFuncExprBegin(args.isvarg ? -args.narg : args.narg, nloc,
12411241
args.nams, startLine);
@@ -1480,6 +1480,9 @@ static void ReadLiteral (
14801480

14811481
/* '~' */
14821482
case S_TILDE:
1483+
if (ReaderState()->ReadTop == 0) {
1484+
SyntaxError("'~' not allowed here");
1485+
}
14831486
ReaderState()->ReadTilde = 1;
14841487
TRY_IF_NO_ERROR { IntrTildeExpr(); }
14851488
Match( S_TILDE, "~", follow );

tst/testinstall/interpreter.tst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ gap> QUIT;
3939
gap> ?qwert_asdf
4040
Help: no matching entry found
4141

42-
#
43-
# tilde
44-
#
45-
gap> ~;
46-
Error, '~' does not have a value here
47-
4842
#
4943
# function call with options
5044
#

tst/testinstall/tilde.tst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
gap> START_TEST("tilde.tst");
2+
3+
#
24
gap> aqq~ := 1;
35
Error, Variable: 'aqq' must have a value
46
Syntax error: ; expected in stream:1
57
aqq~ := 1;
68
^
9+
10+
#
711
gap> l := [2, ~];
812
[ 2, ~ ]
913
gap> l = l[2];
@@ -18,6 +22,71 @@ gap> r.x;
1822
rec( x := ~, y := [ 1, 2, ~ ] )
1923
gap> r.y[3];
2024
rec( x := ~, y := [ 1, 2, ~ ] )
25+
26+
#
27+
gap> [ ~ ];
28+
[ ~ ]
29+
gap> [ 1, 2, [0 .. Length(~)] ];
30+
[ 1, 2, [ 0 .. 2 ] ]
31+
gap> [0, 1 + ~, 2 ];
32+
[ 0, [ 1 ], 2 ]
33+
34+
#
35+
gap> [ (x->~)(1) ];
36+
[ ~ ]
37+
gap> l := [ x->~ ]; # this function escapes with an invalid tilde reference
38+
[ function( x ) ... end ]
39+
gap> f := l[1];;
40+
gap> f(1);
41+
Error, '~' does not have a value here
42+
gap> [ f(1) ];
43+
[ ~ ]
44+
45+
#
46+
gap> ~;
47+
Syntax error: '~' not allowed here in stream:1
48+
~;
49+
^
50+
gap> (1,~);
51+
Syntax error: '~' not allowed here in stream:1
52+
(1,~);
53+
^
54+
gap> x->~;
55+
Syntax error: '~' not allowed here in stream:1
56+
x->~;
57+
^
58+
gap> x -> (1,~);
59+
Syntax error: '~' not allowed here in stream:1
60+
x -> (1,~);
61+
^
62+
63+
#
64+
gap> [1..~];
65+
Syntax error: Sorry, '~' not allowed in range in stream:1
66+
[1..~];
67+
^
68+
gap> [~..1];
69+
Syntax error: Sorry, '~' not allowed in range in stream:1
70+
[~..1];
71+
^
72+
gap> [1,~..5];
73+
Syntax error: Sorry, '~' not allowed in range in stream:1
74+
[1,~..5];
75+
^
76+
gap> x->[1..~];
77+
Syntax error: Sorry, '~' not allowed in range in stream:1
78+
x->[1..~];
79+
^
80+
gap> x->[~..1];
81+
Syntax error: Sorry, '~' not allowed in range in stream:1
82+
x->[~..1];
83+
^
84+
gap> x->[1,~..5];
85+
Syntax error: Sorry, '~' not allowed in range in stream:1
86+
x->[1,~..5];
87+
^
88+
89+
#
2190
gap> f := function(~) local a; end;
2291
Syntax error: identifier expected in stream:1
2392
f := function(~) local a; end;
@@ -42,6 +111,8 @@ gap> {~,~} -> 2;
42111
Syntax error: identifier expected in stream:1
43112
{~,~} -> 2;
44113
^
114+
115+
#
45116
gap> list1 := [1,~];
46117
[ 1, ~ ]
47118
gap> list2 := [1,[1,[1,[1,0]]]];
@@ -96,4 +167,6 @@ gap> [2,rem(~),3,4,rem(~),5,6,rem(~)];
96167
[ , 1, 3,, 1, 5,, 1 ]
97168
gap> (function() return [2,rem(~),3,4,rem(~),5,6,rem(~)]; end)();
98169
[ , 1, 3,, 1, 5,, 1 ]
170+
171+
#
99172
gap> STOP_TEST( "tilde.tst", 1);

0 commit comments

Comments
 (0)