Skip to content
Merged
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
30 changes: 30 additions & 0 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ each let v, arr{ // Syntax Error
}
```

### while
条件がtrueの間ループを続けます。
条件が最初からfalseの場合はループは実行されません。
```js
var count = 0
while count < 42 {
count += 1
}
<: count // 42
// 条件が最初からfalseの場合
while false {
<: 'hoge'
} // no output
```

### do-while
条件がtrueの間ループを続けます。
条件が最初からfalseであってもループは一度実行されます。
```js
var count = 0
do {
count += 1
} while count < 42
<: count // 42
// 条件が最初からfalseの場合
do {
<: 'hoge'
} while false // hoge
```

### loop
`break`されるまで無制限にループを行います。
```js
Expand Down
2 changes: 0 additions & 2 deletions src/parser/plugins/validate-keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const reservedWord = [
'constructor',
// 'def',
'dictionary',
'do',
'enum',
'export',
'finally',
Expand All @@ -44,7 +43,6 @@ const reservedWord = [
'use',
'using',
'when',
'while',
'yield',
'import',
'is',
Expand Down
6 changes: 6 additions & 0 deletions src/parser/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ export class Scanner implements ITokenStream {
case 'loop': {
return TOKEN(TokenKind.LoopKeyword, loc, { hasLeftSpacing });
}
case 'do': {
return TOKEN(TokenKind.DoKeyword, loc, { hasLeftSpacing });
}
case 'while': {
return TOKEN(TokenKind.WhileKeyword, loc, { hasLeftSpacing });
}
case 'break': {
return TOKEN(TokenKind.BreakKeyword, loc, { hasLeftSpacing });
}
Expand Down
54 changes: 54 additions & 0 deletions src/parser/syntaxes/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export function parseStatement(s: ITokenStream): Ast.Node {
case TokenKind.LoopKeyword: {
return parseLoop(s);
}
case TokenKind.DoKeyword: {
return parseDoWhile(s);
}
case TokenKind.WhileKeyword: {
return parseWhile(s);
}
case TokenKind.BreakKeyword: {
s.next();
return NODE('break', {}, loc);
Expand Down Expand Up @@ -376,6 +382,54 @@ function parseLoop(s: ITokenStream): Ast.Node {
return NODE('loop', { statements }, loc);
}

/**
* ```abnf
* Loop = "do" BlockOrStatement "while" Expr
* ```
*/
function parseDoWhile(s: ITokenStream): Ast.Node {
const doLoc = s.token.loc;
s.nextWith(TokenKind.DoKeyword);
const body = parseBlockOrStatement(s);
const whileLoc = s.token.loc;
s.nextWith(TokenKind.WhileKeyword);
const cond = parseExpr(s, false);

return NODE('loop', {
statements: [
body,
NODE('if', {
cond: NODE('not', { expr: cond }, whileLoc),
then: NODE('break', {}, whileLoc),
elseif: [],
}, whileLoc),
],
}, doLoc);
}

/**
* ```abnf
* Loop = "while" Expr BlockOrStatement
* ```
*/
function parseWhile(s: ITokenStream): Ast.Node {
const loc = s.token.loc;
s.nextWith(TokenKind.WhileKeyword);
const cond = parseExpr(s, false);
const body = parseBlockOrStatement(s);

return NODE('loop', {
statements: [
NODE('if', {
cond: NODE('not', { expr: cond }, loc),
then: NODE('break', {}, loc),
elseif: [],
}, loc),
body,
],
}, loc);
}

/**
* ```abnf
* Assign = Expr ("=" / "+=" / "-=") Expr
Expand Down
2 changes: 2 additions & 0 deletions src/parser/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export enum TokenKind {
EachKeyword,
ForKeyword,
LoopKeyword,
DoKeyword,
WhileKeyword,
BreakKeyword,
ContinueKeyword,
MatchKeyword,
Expand Down
2 changes: 2 additions & 0 deletions test/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const reservedWords = [
'false',
'each',
'for',
'do',
'while',
'loop',
'break',
'continue',
Expand Down
44 changes: 44 additions & 0 deletions test/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,50 @@ describe('each', () => {
});
});

describe('while', () => {
test.concurrent('Basic', async () => {
const res = await exe(`
var count = 0
while count < 42 {
count += 1
}
<: count
`);
eq(res, NUM(42));
});

test.concurrent('start false', async () => {
const res = await exe(`
while false {
<: 'hoge'
}
`);
eq(res, NULL);
});
});

describe('do-while', () => {
test.concurrent('Basic', async () => {
const res = await exe(`
var count = 0
do {
count += 1
} while count < 42
<: count
`);
eq(res, NUM(42));
});

test.concurrent('start false', async () => {
const res = await exe(`
do {
<: 'hoge'
} while false
`);
eq(res, STR('hoge'));
});
});

describe('loop', () => {
test.concurrent('Basic', async () => {
const res = await exe(`
Expand Down
1 change: 1 addition & 0 deletions unreleased/while.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- while文とdo-while文を追加