Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _ to the REPL #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions compiler/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6340,6 +6340,52 @@ export default program => {

program.id = { name: 'main' };

if (Prefs.repl) {
let newBody = [{
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: { type: 'Identifier', name: '_' },
init: { type: 'Identifier', name: 'undefined' }
}
],
kind: 'let'
}];
for (const node of program.body) {
if (
// skip setting _ for:
// ;
node.type === 'EmptyStatement'
// print(...)
|| node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' && node.expression.callee.name === 'print'
) {
newBody.push(node);
} else if (node.type === 'ExpressionStatement') {
newBody.push({
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
left: { type: 'Identifier', name: '_' },
operator: '=',
right: node.expression
}
})
} else {
newBody.push(node, {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
left: { type: 'Identifier', name: '_' },
operator: '=',
right: { type: 'Identifier', name: 'undefined' }
}
})
}
}
program.body = newBody;
}

program.body = {
type: 'BlockStatement',
body: program.body
Expand Down
3 changes: 2 additions & 1 deletion runner/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import compile from '../compiler/wrap.js';

import util from 'node:util';

process.argv.push('--no-opt-unused');
process.argv.push('--no-opt-unused', '--repl');
globalThis.argvChanged();

let repl;
try {
Expand Down