@@ -2180,10 +2180,19 @@ private static void ParseFunction(Scope scope, ILuaLexer code, bool isLocal)
2180
2180
if ( isLocal ) // Local function, only one identifier is allowed
2181
2181
{
2182
2182
var t = FetchToken ( LuaToken . Identifier , code ) ;
2183
- ParameterExpression funcVar = null ;
2184
- var exprFunction = ParseLamdaDefinition ( scope , code , t . Value , false ,
2185
- typeDelegate => funcVar = scope . RegisterVariable ( typeDelegate , t . Value )
2186
- ) ;
2183
+
2184
+ ParameterExpression funcVar = scope . LookupExpression ( t . Value ) as ParameterExpression ;
2185
+ Expression exprFunction ;
2186
+ if ( funcVar == null )
2187
+ {
2188
+ exprFunction = ParseLamdaDefinition ( scope , code , t . Value , false ,
2189
+ typeDelegate => funcVar = scope . RegisterVariable ( typeDelegate , t . Value ) ) ;
2190
+ }
2191
+ else
2192
+ {
2193
+ exprFunction = ParseLamdaDefinition ( scope , code , t . Value , false , null ) ;
2194
+ }
2195
+
2187
2196
scope . AddExpression ( Expression . Assign ( funcVar , exprFunction ) ) ;
2188
2197
}
2189
2198
else // Function that is assigned to a table. A chain of identifiers is allowed.
@@ -2202,7 +2211,7 @@ private static void ParseFunction(Scope scope, ILuaLexer code, bool isLocal)
2202
2211
memberName = FetchToken ( LuaToken . Identifier , code ) . Value ;
2203
2212
}
2204
2213
// add a method to the table. methods get a hidden parameter and will bo marked
2205
- bool lMethodMember ;
2214
+ bool lMethodMember = false ;
2206
2215
if ( code . Current . Typ == LuaToken . Colon )
2207
2216
{
2208
2217
code . Next ( ) ;
@@ -2216,8 +2225,17 @@ private static void ParseFunction(Scope scope, ILuaLexer code, bool isLocal)
2216
2225
else
2217
2226
{
2218
2227
if ( assignee == null )
2228
+ {
2229
+ // there was no member access, so try to find a local to assign to
2230
+ var local = scope . LookupExpression ( memberName ) ;
2231
+ if ( local is ParameterExpression )
2232
+ {
2233
+ scope . AddExpression ( Expression . Assign ( local , ParseLamdaDefinition ( scope , code , memberName , false , null ) ) ) ;
2234
+ return ;
2235
+ }
2236
+
2219
2237
assignee = scope . LookupExpression ( csEnv ) ; // create a global function
2220
- lMethodMember = false ;
2238
+ }
2221
2239
}
2222
2240
2223
2241
// generate lambda
@@ -2295,7 +2313,7 @@ private static Expression ParseLamdaDefinition(Scope parent, ILuaLexer code, str
2295
2313
// register the delegate
2296
2314
if ( functionTypeCollected != null )
2297
2315
{
2298
- var functionType = scope . ReturnType == typeof ( void )
2316
+ var functionType = scope . ReturnType == typeof ( void )
2299
2317
? Expression . GetActionType ( ( from p in parameters select p . Type ) . ToArray ( ) )
2300
2318
: Expression . GetFuncType ( ( from p in parameters select p . Type ) . Concat ( new Type [ ] { scope . ReturnType } ) . ToArray ( ) ) ;
2301
2319
functionTypeCollected ( functionType ) ;
@@ -2428,7 +2446,7 @@ private static void ParseTableField(ParameterExpression tableVar, Scope scope, I
2428
2446
2429
2447
private static Expression CreateEmptyTableExpression ( )
2430
2448
=> Expression . New ( typeof ( LuaTable ) ) ;
2431
-
2449
+
2432
2450
#endregion
2433
2451
2434
2452
#region -- FetchToken, ParseError ---------------------------------------------
@@ -2449,7 +2467,7 @@ public static Token FetchToken(LuaToken typ, ILuaLexer code, bool isOptional = f
2449
2467
2450
2468
public static LuaParseException ParseError ( Token start , string message )
2451
2469
=> new LuaParseException ( start . Start , message , null ) ;
2452
-
2470
+
2453
2471
private static Exception ParseError ( ILuaLexer code , string message = null )
2454
2472
{
2455
2473
switch ( code . Current . Typ )
0 commit comments