@@ -12,6 +12,17 @@ import {ContextType, TSHelper as tsHelper} from "./TSHelper";
1212type StatementVisitResult = tstl . Statement | tstl . Statement [ ] | undefined ;
1313type ExpressionVisitResult = tstl . Expression | undefined ;
1414
15+ export enum ScopeType {
16+ Function ,
17+ Switch ,
18+ Loop ,
19+ }
20+
21+ interface Scope {
22+ type : ScopeType ;
23+ id : number ;
24+ }
25+
1526export class LuaTransformer {
1627 public luaKeywords : Set < string > = new Set (
1728 [ "and" , "break" , "do" , "else" , "elseif" ,
@@ -29,6 +40,14 @@ export class LuaTransformer {
2940 private currentNamespace : ts . ModuleDeclaration ;
3041 private classStack : tstl . Identifier [ ] ;
3142
43+ private scopeStack : Scope [ ] ;
44+ private genVarCounter : number ;
45+
46+ public constructor ( ) {
47+ this . scopeStack = [ ] ;
48+ this . genVarCounter = 0 ;
49+ }
50+
3251 public transformSourceFile ( node : ts . SourceFile ) : tstl . Block {
3352 return tstl . createBlock ( this . transformStatements ( node . statements ) , undefined , node ) ;
3453 }
@@ -645,6 +664,8 @@ export class LuaTransformer {
645664 body : ts . Block ,
646665 spreadIdentifier ?: tstl . Identifier
647666 ) : tstl . Statement [ ] {
667+ this . pushScope ( ScopeType . Function ) ;
668+
648669 const headerStatements = [ ] ;
649670
650671 // Add default parameters
@@ -663,6 +684,8 @@ export class LuaTransformer {
663684
664685 const bodyStatements = this . transformStatements ( body . statements ) ;
665686
687+ this . popScope ( ) ;
688+
666689 return headerStatements . concat ( bodyStatements ) ;
667690 }
668691
@@ -891,4 +914,17 @@ export class LuaTransformer {
891914 return tstl . createVariableAssignmentStatement ( lhs , rhs , parent , tsOriginal ) ;
892915 }
893916 }
917+
918+ private peekScope ( ) : Scope {
919+ return this . scopeStack [ this . scopeStack . length - 1 ] ;
920+ }
921+
922+ private pushScope ( scopeType : ScopeType ) : void {
923+ this . scopeStack . push ( { type : scopeType , id : this . genVarCounter } ) ;
924+ this . genVarCounter ++ ;
925+ }
926+
927+ private popScope ( ) : Scope {
928+ return this . scopeStack . pop ( ) ;
929+ }
894930}
0 commit comments