-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Adds inline if and unless keywords
Directly adds the inline versions of the `if` and `unless` keywords to the VM. It does this by adding a new `IfInline` opcode, which is combined with a new `Not` opcode that inverst the condition in the case of `unless`. This also allows us to remove the `Unless` opcode and rewrite it in terms of the `If` opcode in general. Since `unless` is generally much less common of an opcode, this shouldn't be an issue performance-wise, and we benefit from having a simpler set of wireformat/opcodes.
- Loading branch information
Chris Garrett
committed
Dec 17, 2020
1 parent
c355419
commit f26034b
Showing
17 changed files
with
591 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/@glimmer/compiler/lib/passes/1-normalization/keywords/utils/if-unless.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ASTv2, generateSyntaxError } from '@glimmer/syntax'; | ||
|
||
import { Err, Ok, Result } from '../../../../shared/result'; | ||
|
||
export function assertValidIfUnlessInlineUsage(type: string, inverted: boolean) { | ||
return ( | ||
originalNode: ASTv2.AppendContent | ASTv2.ExpressionNode | ||
): Result<{ | ||
condition: ASTv2.ExpressionNode; | ||
truthy: ASTv2.ExpressionNode; | ||
falsy: ASTv2.ExpressionNode | null; | ||
}> => { | ||
let node = originalNode.type === 'AppendContent' ? originalNode.value : originalNode; | ||
let named = node.type === 'Call' ? node.args.named : null; | ||
let positional = node.type === 'Call' ? node.args.positional : null; | ||
|
||
if (named && !named.isEmpty()) { | ||
return Err( | ||
generateSyntaxError( | ||
`${type} cannot receive named parameters, received ${named.entries | ||
.map((e) => e.name.chars) | ||
.join(', ')}`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
let condition = positional?.nth(0); | ||
|
||
if (!positional || !condition) { | ||
return Err( | ||
generateSyntaxError( | ||
`When used inline, ${type} requires at least two parameters 1. the condition that determines the state of the ${type}, and 2. the value to return if the condition is ${ | ||
inverted ? 'false' : 'true' | ||
}. Did not receive any parameters`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
let truthy = positional.nth(1); | ||
let falsy = positional.nth(2); | ||
|
||
if (truthy === null) { | ||
return Err( | ||
generateSyntaxError( | ||
`When used inline, ${type} requires at least two parameters 1. the condition that determines the state of the ${type}, and 2. the value to return if the condition is ${ | ||
inverted ? 'false' : 'true' | ||
}. Received only one parameter, the condition`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
if (positional.size > 3) { | ||
return Err( | ||
generateSyntaxError( | ||
`When used inline, ${type} can receive a maximum of three positional parameters 1. the condition that determines the state of the ${type}, 2. the value to return if the condition is ${ | ||
inverted ? 'false' : 'true' | ||
}, and 3. the value to return if the condition is ${ | ||
inverted ? 'true' : 'false' | ||
}. Received ${positional?.size ?? 0} parameters`, | ||
originalNode.loc | ||
) | ||
); | ||
} | ||
|
||
return Ok({ condition, truthy, falsy }); | ||
}; | ||
} |
Oops, something went wrong.