Add desugaring of standalone assertions

This commit is contained in:
Stanisław Barzowski 2017-08-18 14:16:05 -04:00 committed by Dave Cunningham
parent 0d716ae56f
commit a4456d8ecf
7 changed files with 21 additions and 2 deletions

View File

@ -228,6 +228,13 @@ func desugarObjectComp(astComp *ast.ObjectComp, objLevel int) (ast.Node, error)
// TODO(sbarzowski) this
}
func buildLiteralString(value string) ast.Node {
return &ast.LiteralString{
Kind: ast.StringDouble,
Value: value,
}
}
func buildSimpleIndex(obj ast.Node, member ast.Identifier) ast.Node {
return &ast.Index{
Target: obj,
@ -301,8 +308,14 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
*astPtr = comp
case *ast.Assert:
// TODO(sbarzowski) this
*astPtr = &ast.LiteralNull{}
if node.Message == nil {
node.Message = buildLiteralString("Assertion failed")
}
*astPtr = &ast.Conditional{
Cond: node.Cond,
BranchTrue: node.Rest,
BranchFalse: &ast.Error{Expr: node.Message},
}
case *ast.Binary:
// some operators get replaced by stdlib functions

1
testdata/assert.golden vendored Normal file
View File

@ -0,0 +1 @@
true

1
testdata/assert.input vendored Normal file
View File

@ -0,0 +1 @@
assert true; true

1
testdata/assert_failed.golden vendored Normal file
View File

@ -0,0 +1 @@
RUNTIME ERROR: Assertion failed

1
testdata/assert_failed.input vendored Normal file
View File

@ -0,0 +1 @@
assert false; true

1
testdata/assert_failed_custom.golden vendored Normal file
View File

@ -0,0 +1 @@
RUNTIME ERROR: Custom Message

1
testdata/assert_failed_custom.input vendored Normal file
View File

@ -0,0 +1 @@
assert false : "Custom Message"; true