Skip to content

Commit

Permalink
minor improvements in env viz (#1489)
Browse files Browse the repository at this point in the history
* minor improvements in env viz

* linting
  • Loading branch information
martin-henz authored Sep 17, 2023
1 parent c9c9e88 commit 5ee81dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
5 changes: 0 additions & 5 deletions src/ec-evaluator/instrCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ export const envInstr = (env: Environment, srcNode: es.Node): EnvInstr => ({
srcNode
})

export const pushUndefIfNeededInstr = (srcNode: es.Node): Instr => ({
instrType: InstrType.PUSH_UNDEFINED_IF_NEEDED,
srcNode
})

export const arrLitInstr = (arity: number, srcNode: es.Node): ArrLitInstr => ({
instrType: InstrType.ARRAY_LITERAL,
arity,
Expand Down
29 changes: 14 additions & 15 deletions src/ec-evaluator/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
// The no declarations case is handled by Agenda :: simplifyBlocksWithoutDeclarations, so no blockStatement node
// without declarations should end up here.
const next = agenda.peek()
if (!(next && isInstr(next) && next.instrType === InstrType.ENVIRONMENT)) {
// Push ENVIRONMENT instruction if needed
if (next && !(isInstr(next) && next.instrType === InstrType.ENVIRONMENT)) {
agenda.push(instr.envInstr(currentEnvironment(context), command))
}

Expand Down Expand Up @@ -680,7 +681,10 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
if (hasContinueStatement(command.body as es.BlockStatement)) {
agenda.push(instr.contMarkerInstr(command.srcNode))
}
agenda.push(instr.pushUndefIfNeededInstr(command.srcNode)) // The loop returns undefined if the stash is empty
if (!valueProducing(command.body)) {
// if loop body is not value-producing, insert undefined expression statement
agenda.push(ast.identifier('undefined', command.body.loc))
}
agenda.push(command.body)
agenda.push(instr.popInstr(command.srcNode)) // Pop previous body value
}
Expand All @@ -703,7 +707,10 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
if (hasContinueStatement(command.body as es.BlockStatement)) {
agenda.push(instr.contMarkerInstr(command.srcNode))
}
agenda.push(instr.pushUndefIfNeededInstr(command.srcNode)) // The loop returns undefined if the stash is empty
if (!valueProducing(command.body)) {
// if loop body is not value-producing, insert undefined expression statement
agenda.push(ast.identifier('undefined', command.body.loc))
}
agenda.push(command.body)
agenda.push(instr.popInstr(command.srcNode)) // Pop previous body value
}
Expand Down Expand Up @@ -830,7 +837,10 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
const returnStatement = block.body[0] as es.ReturnStatement
agenda.push(returnStatement.argument ?? ast.identifier('undefined', returnStatement.loc))
} else {
agenda.push(instr.markerInstr(command.srcNode))
if (agenda.peek()) {
// push marker if agenda not empty
agenda.push(instr.markerInstr(command.srcNode))
}
agenda.push(func.node.body)
}
} else if (typeof func === 'function') {
Expand Down Expand Up @@ -890,17 +900,6 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
}
},

[InstrType.PUSH_UNDEFINED_IF_NEEDED]: function (
command: Instr,
context: Context,
agenda: Agenda,
stash: Stash
) {
if (stash.size() === 0) {
stash.push(undefined)
}
},

[InstrType.ARRAY_LITERAL]: function (
command: ArrLitInstr,
context: Context,
Expand Down
1 change: 0 additions & 1 deletion src/ec-evaluator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export enum InstrType {
APPLICATION = 'Application',
BRANCH = 'Branch',
ENVIRONMENT = 'Environment',
PUSH_UNDEFINED_IF_NEEDED = 'PushUndefinedIfNeeded',
ARRAY_LITERAL = 'ArrayLiteral',
ARRAY_ACCESS = 'ArrayAccess',
ARRAY_ASSIGNMENT = 'ArrayAssignment',
Expand Down

0 comments on commit 5ee81dc

Please sign in to comment.