git » jacl.git » commit a1cd176

progn optimization: dont wrap with IIFE in expr ctx if theres only a node.ret

author Alan Dipert
2020-01-30 01:06:14 UTC
committer Alan Dipert
2020-01-30 01:06:14 UTC
parent 3466be97b6514b87c3c5e59eba92314c5a376c9d

progn optimization: dont wrap with IIFE in expr ctx if theres only a node.ret

jacl.js +10 -5

diff --git a/jacl.js b/jacl.js
index 80576a0..96828d8 100644
--- a/jacl.js
+++ b/jacl.js
@@ -1939,11 +1939,16 @@ const emitNode = (print, node) => {
       if (context !== 'expr') print(';\n');
       break;
     } case 'progn': {
-      if (context === 'expr') print('(function()');
-      print('{');
-      emitBlock(print, node.statements, node.ret);
-      print('}');
-      if (context === 'expr') print(')()');
+      if (context === 'expr' && !node.statements.length) {
+        // If there's only a return expression, just emit that.
+        emitNode(print, merge(node.ret, { env: node.ret.env.withContext('expr') }));
+      } else {
+        if (context === 'expr') print('(function()');
+        print('{');
+        emitBlock(print, node.statements, node.ret);
+        print('}');
+        if (context === 'expr') print(')()');
+      }
       break;
     } default:
       throw new Error(`Unknown op: ${op}`);