Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit e10ba02

Browse files
committed
[[ ReturnValue ]] Change syntax to avoid ambiguity
This patch changes the syntax of the new return forms so they don't conflict with functions called 'value' or 'error'. The syntax is now: return <expr> for error return <expr> for value
1 parent 72bee99 commit e10ba02

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

docs/dictionary/control_st/return.lcdoc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Name: return
22

33
Type: control structure
44

5-
Syntax: return [ { value | error } ] <value>
5+
Syntax: return <value> [ for { value | error } ]
66

77
Summary: Stops the current <handler> and <return|returns> a <value> to the
88
<handler> that <call|called> the current <handler>.
@@ -27,7 +27,7 @@ Example:
2727

2828
Example:
2929
function simpleFunction
30-
return value 1
30+
return 1 for value
3131
end simpleFunction
3232
on testSimpleFunction
3333
local tVar
@@ -37,7 +37,7 @@ Example:
3737

3838
Example:
3939
function simpleFunction
40-
return error 1
40+
return 1 for error
4141
end simpleFunction
4242
on testSimpleFunction
4343
local tVar
@@ -56,7 +56,7 @@ Example:
5656

5757
Example:
5858
command simpleCommand
59-
return value 1
59+
return 1 for value
6060
end simpleCommand
6161
on testSimpleCommand
6262
simpleCommand
@@ -65,7 +65,7 @@ Example:
6565

6666
Example:
6767
command simpleCommand
68-
return error 1
68+
return 1 for error
6969
end simpleCommand
7070
on testSimpleCommand
7171
simpleCommand
@@ -116,12 +116,12 @@ halt the current <message handler> and <pass> the <message> on through the
116116
<handler> without returning a value, use the <exit> <control structure>
117117
instead.
118118

119-
The <return error> and <return value> forms of the <return> command allow much
120-
easier scripting of the distinction between a return value of a handler call,
121-
and an error return value of a handler call. If a command or function <handler>
122-
succeeds, then the 'return value' form should be used to return the result of
123-
the execution to the calling <handler>. If a command or function <handler> fails,
124-
then the 'return error' form should be used to return an error status to the
119+
The value and error forms of the <return> command allow much easier scripting of
120+
the distinction between a return value of a handler call, and an error return
121+
value of a handler call. If a command or function <handler> succeeds, then the
122+
'return for value' form should be used to return the result of the execution to
123+
the calling <handler>. If a command or function <handler> fails, then the
124+
'return for error' form should be used to return an error status to the
125125
calling <handler>.
126126

127127
Any callers of handlers using the new error and value forms of the <return>

engine/src/cmds.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,28 +1281,40 @@ MCReturn::~MCReturn()
12811281
Parse_stat MCReturn::parse(MCScriptPoint &sp)
12821282
{
12831283
initpoint(sp);
1284-
if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_VALUE) == PS_NORMAL)
1285-
{
1286-
kind = kReturnValue;
1287-
}
1288-
else if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_ERROR) == PS_NORMAL)
1289-
{
1290-
kind = kReturnError;
1291-
}
1292-
else
1293-
{
1294-
kind = kReturn;
1295-
}
12961284

12971285
if (sp.parseexp(False, True, &source) != PS_NORMAL)
12981286
{
12991287
MCperror->add
13001288
(PE_RETURN_BADEXP, sp);
13011289
return PS_ERROR;
13021290
}
1303-
1304-
if (kind == kReturn &&
1305-
sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_WITH) == PS_NORMAL)
1291+
1292+
if (sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_FOR) == PS_NORMAL)
1293+
{
1294+
if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_VALUE) == PS_NORMAL)
1295+
{
1296+
kind = kReturnValue;
1297+
}
1298+
else if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_ERROR) == PS_NORMAL)
1299+
{
1300+
kind = kReturnError;
1301+
}
1302+
else
1303+
{
1304+
MCperror->add(PE_RETURN_BADFOR, sp);
1305+
return PS_ERROR;
1306+
}
1307+
1308+
Handler_type t_handler_type;
1309+
t_handler_type = sp.gethandler()->gettype();
1310+
if (t_handler_type != HT_MESSAGE &&
1311+
t_handler_type != HT_FUNCTION)
1312+
{
1313+
MCperror->add(PE_RETURN_BADFORMINCONTEXT, sp);
1314+
return PS_ERROR;
1315+
}
1316+
}
1317+
else if (sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_WITH) == PS_NORMAL)
13061318
{
13071319
kind = kReturnWithUrlResult;
13081320
if (sp.skip_token(SP_SUGAR, TT_UNDEFINED, SG_URL_RESULT) == PS_NORMAL)
@@ -1313,16 +1325,6 @@ Parse_stat MCReturn::parse(MCScriptPoint &sp)
13131325
return PS_ERROR;
13141326
}
13151327
}
1316-
1317-
Handler_type t_handler_type;
1318-
t_handler_type = sp.gethandler()->gettype();
1319-
if (kind != kReturn &&
1320-
t_handler_type != HT_MESSAGE &&
1321-
t_handler_type != HT_FUNCTION)
1322-
{
1323-
MCperror->add(PE_RETURN_BADFORMINCONTEXT, sp);
1324-
return PS_ERROR;
1325-
}
13261328

13271329
return PS_NORMAL;
13281330
}

engine/src/parseerrors.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,9 @@ enum Parse_errors
17651765

17661766
// {PE-0572} return: form not allowed in handler type
17671767
PE_RETURN_BADFORMINCONTEXT,
1768+
1769+
// {PE-0573} return: form not allowed in handler type
1770+
PE_RETURN_BADFOR,
17681771
};
17691772

17701773
extern const char *MCparsingerrors;

tests/lcs/core/engine/return.livecodescript

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ on TestReturn
4646
end TestReturn
4747

4848
private command DoReturnValueInCommand pValue
49-
return value pValue
49+
return pValue for value
5050
end DoReturnValueInCommand
5151

5252
private function DoReturnValueInFunction pValue
53-
return value pValue
53+
return pValue for value
5454
end DoReturnValueInFunction
5555

5656
on TestReturnValue
@@ -70,11 +70,11 @@ on TestReturnValue
7070
end TestReturnValue
7171

7272
private command DoReturnErrorInCommand pValue
73-
return error pValue
73+
return pValue for error
7474
end DoReturnErrorInCommand
7575

7676
private function DoReturnErrorInFunction pValue
77-
return error pValue
77+
return pValue for error
7878
end DoReturnErrorInFunction
7979

8080
on TestReturnError

0 commit comments

Comments
 (0)