Skip to content

Commit

Permalink
[Update] Add array pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Siubaak committed Jul 15, 2019
1 parent ac1bb52 commit 2d20f88
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src_/compile/pattern.ts
Expand Up @@ -41,22 +41,38 @@ export function ObjectPattern(node: estree.ObjectPattern, state: State) {

}
}
state.opCodes.push({ op: OP.POP })
}

export function ArrayPattern(node: estree.ArrayPattern, state: State) {
for (let i = 0; i < node.elements.length; i++) {
const element = node.elements[i]
if (!element) continue // for the case: let [ , x] = [1, 2]
state.opCodes.push({ op: OP.COPY })
state.opCodes.push({ op: OP.LOADK, val: i })
state.opCodes.push({ op: OP.MGET })
if (element.type === 'Identifier') {

if (state.symbols.type) {
state.opCodes.push({ op: OP.ALLOC, val: state.symbols.set(element.name).pointer })
} else {
state.opCodes.push({ op: OP.STORE, val: state.symbols.get(element.name).pointer })
}
} else if (element.type === 'MemberExpression') {

compile(element.object, state)
const property = element.property
if (property.type === 'Identifier') {
state.opCodes.push({ op: OP.LOADK, val: property.name })
} else { // node.computed === true
compile(property, state)
}
state.opCodes.push({ op: OP.MSET })
} else if (element.type === 'RestElement') {

} else {
compilePattern(element, state)
}
}
state.opCodes.push({ op: OP.POP })
}

export function AssignmentPattern(node: estree.AssignmentPattern, state: State) {
Expand Down
1 change: 1 addition & 0 deletions src_/jsvm/index.ts
Expand Up @@ -364,6 +364,7 @@ function step(state: State) {
case OP.YIELD: signal = SIGNAL.YIELD; break
case OP.AWAIT: signal = SIGNAL.AWAIT; break
case OP.COPY: stack.push(stack[stack.length - 1]); break
case OP.POP: stack.pop(); break
case OP.DBG: debugger; break
case OP.THROW: {
if (code.val) {
Expand Down
1 change: 1 addition & 0 deletions src_/share/const.ts
Expand Up @@ -40,6 +40,7 @@ export enum OP {
YIELD, // yield (val: delegate or not)
AWAIT, // await (no val)
COPY, // copy the top of stack and push into stack (no val)
POP, // pop the top of stack (no val)
DBG, // debug (no val)
THROW, // throw (val: { pc: catch statement pc })
}
Expand Down
4 changes: 2 additions & 2 deletions src_/test.ts
Expand Up @@ -2,6 +2,6 @@ import Sval from '.'

const interpreter = new Sval()
interpreter.run(`
let { a, b, c: { d } } = { a: 1, b: 2, c: { d: 3 } }
console.log(a, b, d)
let a,b,c
console.log([a,b,c] = [1,2,3,4])
`)

0 comments on commit 2d20f88

Please sign in to comment.