Add endwhile syntax

This commit is contained in:
Nigel Tao committed 2020-02-22 13:11:02 +11:00
1 parent d1a4abe321
commit b1e3a15c08
9 files changed
+53 -29

No files matched your search

+1
View File
@@ -3,6 +3,7 @@
## Work In Progress ## Work In Progress
- Added `endwhile` syntax.
- Added `example/imageviewer`. - Added `example/imageviewer`.
- Added `example/jsonptr`. - Added `example/jsonptr`.
- Added `std/bmp`. - Added `std/bmp`.
+2 -1
View File
@@ -31,7 +31,8 @@ Minor features or restrictions include:
an `if` or `while` condition are optional, but the `{}` curly braces are an `if` or `while` condition are optional, but the `{}` curly braces are
mandatory. There is no 'dangling else' ambiguity. mandatory. There is no 'dangling else' ambiguity.
- Labeled jumps look like `break.loopname` and `continue.loopname`, for a - Labeled jumps look like `break.loopname` and `continue.loopname`, for a
matching `while.loopname`. matching `while.loopname`. The `while.loopname`'s closing curly brace must be
followed by `endwhile.loopname`.
Wuffs code is formatted by the Wuffs code is formatted by the
[`wuffsfmt`](https://godoc.org/github.com/google/wuffs/cmd/wuffsfmt) program. [`wuffsfmt`](https://godoc.org/github.com/google/wuffs/cmd/wuffsfmt) program.
+20
View File
@@ -644,6 +644,22 @@ func (p *parser) parseLabel() (t.ID, error) {
return 0, nil return 0, nil
} }
func (p *parser) parseEndwhile(label t.ID) bool {
if p.peek1() != t.IDEndwhile {
return false
}
p.src = p.src[1:]
if p.peek1() != t.IDDot {
return false
}
p.src = p.src[1:]
if p.peek1() != label {
return false
}
p.src = p.src[1:]
return true
}
func (p *parser) parseStatement1() (*a.Node, error) { func (p *parser) parseStatement1() (*a.Node, error) {
x := p.peek1() x := p.peek1()
if x == t.IDVar { if x == t.IDVar {
@@ -721,6 +737,10 @@ func (p *parser) parseStatement1() (*a.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if (label != 0) && !p.parseEndwhile(label) {
return nil, fmt.Errorf(`parse: expected endwhile.%s at %s:%d`,
label.Str(p.tm), p.filename, p.line())
}
return a.NewWhile(label, condition, asserts, body).AsNode(), nil return a.NewWhile(label, condition, asserts, body).AsNode(), nil
} }
return p.parseAssignNode() return p.parseAssignNode()
+20 -18
View File
@@ -366,24 +366,25 @@ const (
IDConst = ID(0xA2) IDConst = ID(0xA2)
IDContinue = ID(0xA3) IDContinue = ID(0xA3)
IDElse = ID(0xA4) IDElse = ID(0xA4)
IDFunc = ID(0xA5) IDEndwhile = ID(0xA5)
IDIOBind = ID(0xA6) IDFunc = ID(0xA6)
IDIOLimit = ID(0xA7) IDIOBind = ID(0xA7)
IDIf = ID(0xA8) IDIOLimit = ID(0xA8)
IDImplements = ID(0xA9) IDIf = ID(0xA9)
IDInv = ID(0xAA) IDImplements = ID(0xAA)
IDIterate = ID(0xAB) IDInv = ID(0xAB)
IDPost = ID(0xAC) IDIterate = ID(0xAC)
IDPre = ID(0xAD) IDPost = ID(0xAD)
IDPri = ID(0xAE) IDPre = ID(0xAE)
IDPub = ID(0xAF) IDPri = ID(0xAF)
IDReturn = ID(0xB0) IDPub = ID(0xB0)
IDStruct = ID(0xB1) IDReturn = ID(0xB1)
IDUse = ID(0xB2) IDStruct = ID(0xB2)
IDVar = ID(0xB3) IDUse = ID(0xB3)
IDVia = ID(0xB4) IDVar = ID(0xB4)
IDWhile = ID(0xB5) IDVia = ID(0xB5)
IDYield = ID(0xB6) IDWhile = ID(0xB6)
IDYield = ID(0xB7)
) )
const ( const (
@@ -723,6 +724,7 @@ var builtInsByID = [nBuiltInIDs]string{
IDConst: "const", IDConst: "const",
IDContinue: "continue", IDContinue: "continue",
IDElse: "else", IDElse: "else",
IDEndwhile: "endwhile",
IDFunc: "func", IDFunc: "func",
IDIOBind: "io_bind", IDIOBind: "io_bind",
IDIOLimit: "io_limit", IDIOLimit: "io_limit",
+1 -1
View File
@@ -257,7 +257,7 @@ pri func decoder.decode_blocks?(dst: base.io_writer, src: base.io_reader) {
continue.outer continue.outer
} }
} }
} } endwhile.outer
} }
// decode_uncompressed decodes an uncompresed block as per the RFC section // decode_uncompressed decodes an uncompresed block as per the RFC section
+1 -1
View File
@@ -362,7 +362,7 @@ pri func decoder.decode_huffman_fast!(dst: base.io_writer, src: base.io_reader)
args.dst.copy_n32_from_history_fast!(n: length, distance: (dist_minus_1 + 1)) args.dst.copy_n32_from_history_fast!(n: length, distance: (dist_minus_1 + 1))
break break
} }
} } endwhile.loop
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its // Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
// bytes into the bits variable. // bytes into the bits variable.
+1 -1
View File
@@ -272,7 +272,7 @@ pri func decoder.decode_huffman_slow?(dst: base.io_writer, src: base.io_reader)
length -= n_copied length -= n_copied
yield? base."$short write" yield? base."$short write"
} }
} } endwhile.loop
// TODO: "assert n_bits < 8"? What about (bits >> n_bits)? // TODO: "assert n_bits < 8"? What about (bits >> n_bits)?
+2 -2
View File
@@ -992,8 +992,8 @@ pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reade
continue.inner continue.inner
} }
return lzw_status return lzw_status
} } endwhile.inner
} } endwhile.outer
this.compressed_ri = 0 this.compressed_ri = 0
this.compressed_wi = 0 this.compressed_wi = 0
+5 -5
View File
@@ -119,7 +119,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
continue.outer continue.outer
} }
whitespace_length += 1 whitespace_length += 1
} // while.ws } endwhile.ws
// Emit whitespace. // Emit whitespace.
if whitespace_length > 0 { if whitespace_length > 0 {
@@ -286,7 +286,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
} }
string_length += 1 string_length += 1
} }
} // while.string_loop } endwhile.string_loop
// The trailing '"' is filler. // The trailing '"' is filler.
while true { while true {
@@ -523,7 +523,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
} }
return "#bad input" return "#bad input"
} // while.goto_parsed_a_leaf_value } endwhile.goto_parsed_a_leaf_value
// We've just parsed a leaf (non-container) value: literal (null, // We've just parsed a leaf (non-container) value: literal (null,
// false, true), number or string. // false, true), number or string.
@@ -531,7 +531,7 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
break.outer break.outer
} }
expect = expect_after_value expect = expect_after_value
} // while.outer } endwhile.outer
} }
pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] { pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] {
@@ -654,7 +654,7 @@ pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] {
n = this.decode_digits!(src: args.src, n: n) n = this.decode_digits!(src: args.src, n: n)
break.goto_done break.goto_done
} // while.goto_done } endwhile.goto_done
return n | floating_point return n | floating_point
} }