@@ -9,6 +9,7 @@ import { Var } from '../../vars/var.js';
99import { Vars } from '../../vars/vars.js' ;
1010
1111export class MacroCmdLet extends CmdLet implements InputInterceptLine {
12+ private static readonly PARAM_PREFIX : string = '$' ;
1213 name = 'm' ;
1314 category = 'misc' ;
1415 help = 'manage macros' ;
@@ -110,7 +111,7 @@ m name ${CmdLet.DELETE_CHAR} - delete a macro
110111 return Var . ZERO ;
111112 }
112113
113- public static runSync ( macro : Macro ) : Var {
114+ public static runSync ( macro : Macro , tokens : Token [ ] ) : Var {
114115 let ret = Var . ZERO ;
115116 for ( const [ idx , command ] of macro . commands . entries ( ) ) {
116117 if ( command . length === 0 ) continue ;
@@ -121,8 +122,9 @@ m name ${CmdLet.DELETE_CHAR} - delete a macro
121122 if ( command . trim ( ) . length === 0 ) continue ;
122123
123124 const parser = new Parser ( command . toString ( ) ) ;
124- const tokens = parser . tokenize ( ) ;
125- ret = Command . runSync ( tokens ) ;
125+ const commandTokens = parser . tokenize ( ) ;
126+ const substituted = MacroCmdLet . substituteTokens ( commandTokens , tokens ) ;
127+ ret = Command . runSync ( substituted ) ;
126128 Vars . setRet ( ret ) ;
127129
128130 /*
@@ -136,4 +138,37 @@ m name ${CmdLet.DELETE_CHAR} - delete a macro
136138 }
137139 return ret ;
138140 }
141+
142+ private static substituteTokens (
143+ input : Token [ ] ,
144+ substitutes : Token [ ] ,
145+ ) : Token [ ] {
146+ const tokens : Token [ ] = [ ] ;
147+ for ( const token of input ) {
148+ const literal = token . getLiteral ( ) ;
149+
150+ /* If it's not a parameter, don't substitute it */
151+ if ( ! literal . startsWith ( MacroCmdLet . PARAM_PREFIX ) ) {
152+ tokens . push ( token ) ;
153+ continue ;
154+ }
155+
156+ const indexString = literal . slice ( 1 ) ;
157+ const index = parseInt ( indexString ) ;
158+ /* Our token may be a register (which also starts with a '$' */
159+ if ( isNaN ( index ) ) {
160+ tokens . push ( token ) ;
161+ continue ;
162+ }
163+
164+ if ( index >= substitutes . length ) {
165+ throw new Error (
166+ `macro parameter index ${ index } out of range (max ${ substitutes . length - 1 } )` ,
167+ ) ;
168+ }
169+
170+ tokens . push ( substitutes [ index ] as Token ) ;
171+ }
172+ return tokens ;
173+ }
139174}
0 commit comments