@@ -21,6 +21,7 @@ import { evaluateBinaryExpression, evaluateUnaryExpression } from '../utils/oper
2121import * as rttc from '../utils/rttc'
2222import * as seq from '../utils/statementSeqTransform'
2323import { checkProgramForUndefinedVariables } from '../validator/validator'
24+ import { isSchemeLanguage } from '../alt-langs/mapper'
2425import Closure from './closure'
2526import {
2627 Continuation ,
@@ -43,7 +44,8 @@ import {
4344 Instr ,
4445 InstrType ,
4546 UnOpInstr ,
46- WhileInstr
47+ WhileInstr ,
48+ SpreadInstr
4749} from './types'
4850import {
4951 checkNumberOfArguments ,
@@ -81,7 +83,6 @@ import {
8183} from './utils'
8284import { isApply , isEval , schemeEval } from './scheme-macros'
8385import { Transformer } from './patterns'
84- import { isSchemeLanguage } from '../alt-langs/mapper'
8586import { flattenList , isList } from './macro-utils'
8687
8788type CmdEvaluator = (
@@ -758,6 +759,12 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
758759 }
759760 } ,
760761
762+ SpreadElement : function ( command : es . SpreadElement , context : Context , control : Control ) {
763+ const arr = command . argument as es . ArrayExpression
764+ control . push ( instr . spreadInstr ( arr ) )
765+ control . push ( arr )
766+ } ,
767+
761768 ArrayExpression : function ( command : es . ArrayExpression , context : Context , control : Control ) {
762769 const elems = command . elements as ContiguousArrayElements
763770 reverse ( elems )
@@ -1310,5 +1317,30 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
13101317 }
13111318 } ,
13121319
1313- [ InstrType . BREAK_MARKER ] : function ( ) { }
1320+ [ InstrType . BREAK_MARKER ] : function ( ) { } ,
1321+
1322+ [ InstrType . SPREAD ] : function (
1323+ command : SpreadInstr ,
1324+ context : Context ,
1325+ control : Control ,
1326+ stash : Stash
1327+ ) {
1328+ const array = stash . pop ( )
1329+
1330+ // spread array
1331+ for ( let i = 0 ; i < array . length ; i ++ ) {
1332+ stash . push ( array [ i ] )
1333+ }
1334+
1335+ // update call instr above
1336+ const cont = control . getStack ( )
1337+ const size = control . size ( )
1338+ for ( let i = size - 1 ; i >= 0 ; i -- ) {
1339+ // guaranteed at least one call instr above, because spread is not allowed inside arrays
1340+ if ( ( cont [ i ] as AppInstr ) . instrType === InstrType . APPLICATION ) {
1341+ ; ( cont [ i ] as AppInstr ) . numOfArgs += array . length - 1
1342+ break // only the nearest call instruction above
1343+ }
1344+ }
1345+ }
13141346}
0 commit comments