Skip to content

Commit 9975e68

Browse files
committed
Remove blocks
1 parent 2487bd2 commit 9975e68

5 files changed

Lines changed: 11 additions & 17 deletions

File tree

feature/block.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,4 @@ export const block = () =>
3535

3636
// body() - parse { body } or single statement
3737
export const body = () =>
38-
space() !== OBRACE ? expr(STATEMENT + .5) : (skip(), ['block', expr(STATEMENT - .5, CBRACE) || null]);
39-
40-
// Compile
41-
operator('block', body => body === undefined ? () => {} : (body = compile(body), ctx => body(ctx)));
38+
space() !== OBRACE ? expr(STATEMENT + .5) : (skip(), expr(STATEMENT - .5, CBRACE) || null);

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ <h1><a href="https://github.com/dy/subscript" class="logo-link" target="_blank"
394394
ternary: '?:', arrow: '=>', optional: '?.', spread: '...',
395395
unary: 'typeof void', identity: '=== !==', nullish: '??',
396396
// Literals
397-
literal: 'true/false/null', collection: '[], {}', template: '`${}`',
397+
literal: 'true/false/null', collection: '[1,2], {a:1}', template: '`${}`',
398398
regex: '/re/', unit: '5px', comment: '//',
399399
// Control
400-
block: '{ }', 'if': 'if/else', loop: 'for/while', 'switch': 'switch',
400+
block: '{...}', 'if': 'if/else', loop: 'for/while', 'switch': 'switch,
401401
'var': 'let/const', destruct: '{a,b}=', 'try': 'try/catch',
402402
// Functions
403403
'function': 'fn(){}', async: 'async/await', 'class': 'class X {}',
@@ -431,7 +431,7 @@ <h1><a href="https://github.com/dy/subscript" class="logo-link" target="_blank"
431431
regex: 'Regular expressions<br><code>/pattern/</code> <code>/\\d+/g</code>',
432432
unit: 'Unit suffixes<br><code>5px</code> <code>10rem</code> <code>2s</code>',
433433
comment: 'Comments (ignored)<br><code>// line</code> <code>/* block */</code>',
434-
block: 'Block statements<br><code>{ stmt1; stmt2 }</code>',
434+
block: 'Statement block<br><code>if (x) { a; b }</code>',
435435
'if': 'Conditionals<br><code>if (c) x</code> <code>if (c) x else y</code>',
436436
loop: 'Loops<br><code>while (c) body</code> <code>for (;;) body</code> <code>for (x of arr)</code>',
437437
'switch': 'Switch statement<br><code>switch (x) { case 1: ... }</code>',

test/feature/async-class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ test('async/class: private fields', async () => {
6969

7070
test('async/class: for await', async () => {
7171
const { parse } = await import('../../jessie.js');
72-
assert.deepEqual(parse('for await (x of y) {}'), ['for await', ['of', 'x', 'y'], ['block', null]]);
72+
assert.deepEqual(parse('for await (x of y) {}'), ['for await', ['of', 'x', 'y'], null]);
7373
});
7474

7575
test('operators: range', async () => {

test/feature/control.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ test('control: return', t => {
6767
})
6868

6969
test('control: block', t => {
70-
// blocks only work inside control structures, not standalone (to avoid conflict with object literal)
71-
is(parse('if (1) { a }'), ['if', [, 1], ['block', 'a']])
72-
is(parse('if (1) { a; b }'), ['if', [, 1], ['block', [';', 'a', 'b']]])
73-
is(parse('while (x) { y }'), ['while', 'x', ['block', 'y']])
70+
// blocks are just sequences, no wrapper node
71+
is(parse('if (1) { a }'), ['if', [, 1], 'a'])
72+
is(parse('if (1) { a; b }'), ['if', [, 1], [';', 'a', 'b']])
73+
is(parse('while (x) { y }'), ['while', 'x', 'y'])
7474
// blocks allow access to outer scope (like JS)
7575
let ctx = { x: 1 }
7676
run('if (1) { x = 2 }', ctx)

util/stringify.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ export const codegen = node => {
6767

6868
// --- Statement generators (need structure) ---
6969

70-
generator('block', body => body === undefined ? '{}' : '{ ' + codegen(body) + ' }');
71-
7270
// Variables: ['let', decl] or ['let', decl1, decl2, ...]
7371
const varGen = kw => (...args) => kw + ' ' + args.map(codegen).join(', ');
7472
generator('let', varGen('let'));
7573
generator('const', varGen('const'));
7674
generator('var', varGen('var'));
7775

7876
// Control flow
79-
const wrap = s => s?.[0] === 'block' ? codegen(s) : '{ ' + (s ? codegen(s) : '') + ' }';
77+
const wrap = s => '{ ' + (s ? codegen(s) : '') + ' }';
8078
generator('if', (cond, then, els) => 'if (' + codegen(cond) + ') ' + wrap(then) + (els ? ' else ' + wrap(els) : ''));
8179
generator('while', (cond, body) => 'while (' + codegen(cond) + ') ' + wrap(body));
8280
generator('do', (body, cond) => 'do ' + wrap(body) + ' while (' + codegen(cond) + ')');
@@ -101,8 +99,7 @@ generator('finally', (expr, body) => codegen(expr) + ' finally { ' + codegen(bod
10199
// Functions
102100
generator('function', (name, params, body) => {
103101
const args = !params ? '' : params[0] === ',' ? params.slice(1).map(codegen).join(', ') : codegen(params);
104-
const b = body?.[0] === 'block' ? codegen(body) : '{ ' + (body ? codegen(body) : '') + ' }';
105-
return 'function' + (name ? ' ' + name : '') + '(' + args + ') ' + b;
102+
return 'function' + (name ? ' ' + name : '') + '(' + args + ') ' + wrap(body);
106103
});
107104

108105
generator('=>', (params, body) => {

0 commit comments

Comments
 (0)