Generate C break's and continue's.

This commit is contained in:
Nigel Tao committed 2017-06-01 15:25:01 +10:00
1 parent 2ed019b670
commit bcf6a7ee1d
6 files changed
+115 -24

No files matched your search

+21 -8
View File
@@ -72,6 +72,8 @@ const (
FlagsSuspendible = Flags(0x00000002)
FlagsPublic = Flags(0x00000004)
FlagsTypeChecked = Flags(0x00000008)
FlagsHasBreak = Flags(0x00000010)
FlagsHasContinue = Flags(0x00000020)
)
type Node struct {
@@ -80,6 +82,7 @@ type Node struct {
constValue *big.Int
mType *TypeExpr
jumpTarget *While
filename string
line uint32
@@ -320,17 +323,24 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
}
// While is "while:ID1 LHS, List0 { List1 }":
// - FlagsHasBreak is the while has an explicit break
// - FlagsHasContinue is the while has an explicit continue
// - ID1: <0|label>
// - LHS: <Expr>
// - List0: <Assert> asserts
// - List1: <Statement> loop body
type While Node
func (n *While) Node() *Node { return (*Node)(n) }
func (n *While) Label() t.ID { return n.id1 }
func (n *While) Condition() *Expr { return n.lhs.Expr() }
func (n *While) Asserts() []*Node { return n.list0 }
func (n *While) Body() []*Node { return n.list1 }
func (n *While) Node() *Node { return (*Node)(n) }
func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
func (n *While) Label() t.ID { return n.id1 }
func (n *While) Condition() *Expr { return n.lhs.Expr() }
func (n *While) Asserts() []*Node { return n.list0 }
func (n *While) Body() []*Node { return n.list1 }
func (n *While) SetHasBreak() { n.flags |= FlagsHasBreak }
func (n *While) SetHasContinue() { n.flags |= FlagsHasContinue }
func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While {
return &While{
@@ -384,9 +394,12 @@ func NewReturn(value *Expr) *Return {
// - ID1: <0|label>
type Jump Node
func (n *Jump) Node() *Node { return (*Node)(n) }
func (n *Jump) Keyword() t.ID { return n.id0 }
func (n *Jump) Label() t.ID { return n.id1 }
func (n *Jump) Node() *Node { return (*Node)(n) }
func (n *Jump) JumpTarget() *While { return n.jumpTarget }
func (n *Jump) Keyword() t.ID { return n.id0 }
func (n *Jump) Label() t.ID { return n.id1 }
func (n *Jump) SetJumpTarget(o *While) { n.jumpTarget = o }
func NewJump(keyword t.ID, label t.ID) *Jump {
return &Jump{
+2
View File
@@ -378,5 +378,7 @@ type checker struct {
errFilename string
errLine uint32
jumpTargets []*a.While
facts facts
}
+30 -1
View File
@@ -73,7 +73,32 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
case a.KJump:
// TODO: check that we're in a for loop.
n := n.Jump()
jumpTarget := (*a.While)(nil)
if id := n.Label(); id != 0 {
for i := len(q.jumpTargets) - 1; i >= 0; i-- {
if w := q.jumpTargets[i]; w.Label() == id {
jumpTarget = w
break
}
}
} else if nj := len(q.jumpTargets); nj > 0 {
jumpTarget = q.jumpTargets[nj-1]
}
if jumpTarget == nil {
sepStr, labelStr := "", ""
if id := n.Label(); id != 0 {
sepStr, labelStr = ":", id.String(q.idMap)
}
return fmt.Errorf("no matching while statement for %s%s%s",
n.Keyword().String(q.idMap), sepStr, labelStr)
}
if n.Keyword().Key() == t.KeyBreak {
jumpTarget.SetHasBreak()
} else {
jumpTarget.SetHasContinue()
}
n.SetJumpTarget(jumpTarget)
case a.KReturn:
n := n.Return()
@@ -113,6 +138,10 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
o.SetTypeChecked()
}
q.jumpTargets = append(q.jumpTargets, n)
defer func() {
q.jumpTargets = q.jumpTargets[:len(q.jumpTargets)-1]
}()
for _, o := range n.Body() {
if err := q.tcheckStatement(o); err != nil {
return err