Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel: catch some invalid uses of '~'; add tests #2750

Merged
merged 2 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ CVar CompListTildeExpr (

/* remember the old value of '~' */
tilde = CVAR_TEMP( NewTemp( "tilde" ) );
Emit( "%c = STATE( Tilde );\n", tilde );
Emit( "%c = STATE(Tilde);\n", tilde );

/* create the list value */
list = CompListExpr1( expr );
Expand Down Expand Up @@ -2718,19 +2718,19 @@ CVar CompRecTildeExpr (

/* remember the old value of '~' */
tilde = CVAR_TEMP( NewTemp( "tilde" ) );
Emit( "%c = STATE( Tilde );\n", tilde );
Emit( "%c = STATE(Tilde);\n", tilde );

/* create the record value */
rec = CompRecExpr1( expr );

/* assign the record value to the variable '~' */
Emit( "STATE( Tilde ) = %c;\n", rec );
Emit( "STATE(Tilde) = %c;\n", rec );

/* evaluate the subexpressions into the record value */
CompRecExpr2( rec, expr );

/* restore the old value of '~' */
Emit( "STATE( Tilde ) = %c;\n", tilde );
Emit( "STATE(Tilde) = %c;\n", tilde );
if ( IS_TEMP_CVAR( tilde ) ) FreeTemp( TEMP_CVAR( tilde ) );

/* return the record value */
Expand Down
4 changes: 1 addition & 3 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,12 @@ Obj FuncCALL_WITH_CATCH(Obj self, Obj func, volatile Obj args)
SWITCH_TO_OLD_LVARS(currLVars);
STATE(CurrStat) = currStat;
SetRecursionDepth(recursionDepth);
#ifdef HPCGAP
STATE(Tilde) = tilde;
#ifdef HPCGAP
PopRegionLocks(lockSP);
TLS(currentRegion) = savedRegion;
if (TLS(CurrentHashLock))
HashUnlock(TLS(CurrentHashLock));
#else
STATE(Tilde) = tilde;
#endif
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/exprs.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ Obj EvalListTildeExpr (
Obj tilde; /* old value of tilde */

/* remember the old value of '~' */
tilde = STATE( Tilde );
tilde = STATE(Tilde);

/* create the list value */
list = ListExpr1(expr, 1);
Expand Down Expand Up @@ -1309,7 +1309,7 @@ Obj EvalRecTildeExpr (
Obj tilde; /* old value of tilde */

/* remember the old value of '~' */
tilde = STATE( Tilde );
tilde = STATE(Tilde);

/* create the record value */
rec = RecExpr1( expr );
Expand Down
6 changes: 3 additions & 3 deletions src/intrprtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -2056,7 +2056,7 @@ void IntrListExprBegin (
/* if this is an outmost list, save it for reference in '~' */
/* (and save the old value of '~' on the values stack) */
if ( top ) {
old = STATE( Tilde );
old = STATE(Tilde);
if ( old != 0 ) { PushObj( old ); }
else { PushVoidObj(); }
STATE(Tilde) = list;
Expand Down Expand Up @@ -2270,10 +2270,10 @@ void IntrRecExprBegin (
/* if this is an outmost record, save it for reference in '~' */
/* (and save the old value of '~' on the values stack) */
if ( top ) {
old = STATE( Tilde );
old = STATE(Tilde);
if ( old != 0 ) { PushObj( old ); }
else { PushVoidObj(); }
STATE( Tilde ) = record;
STATE(Tilde) = record;
}

/* push the record */
Expand Down
8 changes: 5 additions & 3 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,7 @@ static void ReadRecExpr (
nr++;
}

}
while ( STATE(Symbol) == S_COMMA );
} while (STATE(Symbol) == S_COMMA);

/* ')' */
Match( S_RPAREN, ")", follow );
Expand Down Expand Up @@ -1236,7 +1235,7 @@ static void ReadFuncExprBody(
// push the new local variables list
PushPlist(STATE(StackNams), args.nams);

// begin interpreting the function expression (with 1 argument)
// begin interpreting the function expression
TRY_IF_NO_ERROR {
IntrFuncExprBegin(args.isvarg ? -args.narg : args.narg, nloc,
args.nams, startLine);
Expand Down Expand Up @@ -1481,6 +1480,9 @@ static void ReadLiteral (

/* '~' */
case S_TILDE:
if (ReaderState()->ReadTop == 0) {
SyntaxError("'~' not allowed here");
}
ReaderState()->ReadTilde = 1;
TRY_IF_NO_ERROR { IntrTildeExpr(); }
Match( S_TILDE, "~", follow );
Expand Down
6 changes: 0 additions & 6 deletions tst/testinstall/interpreter.tst
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ gap> QUIT;
gap> ?qwert_asdf
Help: no matching entry found

#
# tilde
#
gap> ~;
Error, '~' does not have a value here

#
# function call with options
#
Expand Down
73 changes: 73 additions & 0 deletions tst/testinstall/tilde.tst
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
gap> START_TEST("tilde.tst");

#
gap> aqq~ := 1;
Error, Variable: 'aqq' must have a value
Syntax error: ; expected in stream:1
aqq~ := 1;
^

#
gap> l := [2, ~];
[ 2, ~ ]
gap> l = l[2];
Expand All @@ -18,6 +22,71 @@ gap> r.x;
rec( x := ~, y := [ 1, 2, ~ ] )
gap> r.y[3];
rec( x := ~, y := [ 1, 2, ~ ] )

#
gap> [ ~ ];
[ ~ ]
gap> [ 1, 2, [0 .. Length(~)] ];
[ 1, 2, [ 0 .. 2 ] ]
gap> [0, 1 + ~, 2 ];
[ 0, [ 1 ], 2 ]

#
gap> [ (x->~)(1) ];
[ ~ ]
gap> l := [ x->~ ]; # this function escapes with an invalid tilde reference
[ function( x ) ... end ]
gap> f := l[1];;
gap> f(1);
Error, '~' does not have a value here
gap> [ f(1) ];
[ ~ ]

#
gap> ~;
Syntax error: '~' not allowed here in stream:1
~;
^
gap> (1,~);
Syntax error: '~' not allowed here in stream:1
(1,~);
^
gap> x->~;
Syntax error: '~' not allowed here in stream:1
x->~;
^
gap> x -> (1,~);
Syntax error: '~' not allowed here in stream:1
x -> (1,~);
^

#
gap> [1..~];
Syntax error: Sorry, '~' not allowed in range in stream:1
[1..~];
^
gap> [~..1];
Syntax error: Sorry, '~' not allowed in range in stream:1
[~..1];
^
gap> [1,~..5];
Syntax error: Sorry, '~' not allowed in range in stream:1
[1,~..5];
^
gap> x->[1..~];
Syntax error: Sorry, '~' not allowed in range in stream:1
x->[1..~];
^
gap> x->[~..1];
Syntax error: Sorry, '~' not allowed in range in stream:1
x->[~..1];
^
gap> x->[1,~..5];
Syntax error: Sorry, '~' not allowed in range in stream:1
x->[1,~..5];
^

#
gap> f := function(~) local a; end;
Syntax error: identifier expected in stream:1
f := function(~) local a; end;
Expand All @@ -42,6 +111,8 @@ gap> {~,~} -> 2;
Syntax error: identifier expected in stream:1
{~,~} -> 2;
^

#
gap> list1 := [1,~];
[ 1, ~ ]
gap> list2 := [1,[1,[1,[1,0]]]];
Expand Down Expand Up @@ -96,4 +167,6 @@ gap> [2,rem(~),3,4,rem(~),5,6,rem(~)];
[ , 1, 3,, 1, 5,, 1 ]
gap> (function() return [2,rem(~),3,4,rem(~),5,6,rem(~)]; end)();
[ , 1, 3,, 1, 5,, 1 ]

#
gap> STOP_TEST( "tilde.tst", 1);