mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Generate lang/check/reason.go.
This commit is contained in:
1 parent
61b771a418
commit
dec234779e
4 files changed
+363
-37
No files matched your search
@@ -311,7 +311,10 @@ func errFailedOrNil(ok bool) error {
|
||||
|
||||
var errFailed = errors.New("failed")
|
||||
|
||||
func binOpReasonError(tm *t.Map, op t.ID, lhs *a.Expr, rhs *a.Expr, err error) error {
|
||||
n := a.NewExpr(a.FlagsTypeChecked, op, 0, lhs.Node(), nil, rhs.Node(), nil)
|
||||
return fmt.Errorf("cannot prove %q: %v", n.String(tm), err)
|
||||
func proveReasonRequirement(q *checker, op t.ID, lhs *a.Expr, rhs *a.Expr) error {
|
||||
if err := q.proveBinaryOp(op.Key(), lhs, rhs); err != nil {
|
||||
n := a.NewExpr(a.FlagsTypeChecked, op, 0, lhs.Node(), nil, rhs.Node(), nil)
|
||||
return fmt.Errorf("cannot prove %q: %v", n.String(q.tm), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
//go:generate go run gen.go
|
||||
|
||||
package check
|
||||
|
||||
import (
|
||||
|
||||
@@ -0,0 +1,307 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
// gen.go generates reason.go.
|
||||
//
|
||||
// Invoke it via "go generate".
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var reasons = [...]string{
|
||||
"a < (b + c): a < c; 0 <= b",
|
||||
"(a + b) <= c: a <= (c - b)",
|
||||
"a < b: a < c; c <= b",
|
||||
"a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c",
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
out.WriteString(header)
|
||||
for _, reason := range reasons {
|
||||
nextTmp = 0
|
||||
names = map[string]bool{}
|
||||
if err := gen(reason); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
out.WriteString(footer)
|
||||
|
||||
formatted, err := format.Source(out.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile("reason.go", formatted, 0644)
|
||||
}
|
||||
|
||||
var (
|
||||
nextTmp int
|
||||
names map[string]bool
|
||||
out bytes.Buffer
|
||||
)
|
||||
|
||||
func gen(reason string) error {
|
||||
fmt.Fprintf(&out, "\n{`%q`, func(q *checker, n *a.Assert) error {\n", reason)
|
||||
|
||||
for i := 0; i < len(reason); i++ {
|
||||
b := reason[i]
|
||||
if (b < 0x20) || (0x7F <= b) {
|
||||
return fmt.Errorf("bad reason %q", reason)
|
||||
}
|
||||
}
|
||||
i := strings.IndexByte(reason, ':')
|
||||
if i < 0 {
|
||||
return fmt.Errorf("bad reason %q", reason)
|
||||
}
|
||||
claim := strings.TrimSpace(reason[:i])
|
||||
requirements := strings.Split(reason[i+1:], ";")
|
||||
|
||||
if err := genClaim(claim); err != nil {
|
||||
return fmt.Errorf("bad claim %q: %v", claim, err)
|
||||
}
|
||||
for _, req := range requirements {
|
||||
req = strings.TrimSpace(req)
|
||||
if err := genReq(req); err != nil {
|
||||
return fmt.Errorf("bad req %q: %v", req, err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(&out, "return nil }},\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
func genClaim(claim string) error {
|
||||
n, err := parse(claim)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := genParseBinaryOps(n, "n.Condition()"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func genParseBinaryOps(n *node, arg string) error {
|
||||
l := genName(n.lhs)
|
||||
r := genName(n.rhs)
|
||||
key := keys[n.op]
|
||||
if key == "" {
|
||||
return fmt.Errorf("bad op %q", n.op)
|
||||
}
|
||||
fmt.Fprintf(&out, "op, %s, %s := parseBinaryOp(%s)\n", l, r, arg)
|
||||
fmt.Fprintf(&out, "if op.Key() != t.Key%s { return errFailed }\n", key)
|
||||
if l[0] == 't' {
|
||||
if err := genParseBinaryOps(n.lhs, l); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if r[0] == 't' {
|
||||
if err := genParseBinaryOps(n.rhs, r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func genName(n *node) string {
|
||||
if isConstant(n.op) {
|
||||
if n.op == "0" {
|
||||
return "zeroExpr"
|
||||
}
|
||||
return "$BAD_CONSTANT"
|
||||
}
|
||||
if isVariable(n.op) {
|
||||
names[n.op] = true
|
||||
return "x" + n.op
|
||||
}
|
||||
nextTmp++
|
||||
return fmt.Sprintf("t%d", nextTmp-1)
|
||||
}
|
||||
|
||||
func genReq(req string) error {
|
||||
n, err := parse(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(&out, "// %s\n", req)
|
||||
genArgValues(n)
|
||||
return genProveReasonRequirement(n, "")
|
||||
}
|
||||
|
||||
func genArgValues(n *node) {
|
||||
switch {
|
||||
case isVariable(n.op):
|
||||
if names[n.op] {
|
||||
break
|
||||
}
|
||||
names[n.op] = true
|
||||
fmt.Fprintf(&out, "x%s := argValue(q.tm, n.Args(), %q)\n", n.op, n.op)
|
||||
fmt.Fprintf(&out, "if x%s == nil { return errFailed }\n", n.op)
|
||||
case isOp(n.op):
|
||||
genArgValues(n.lhs)
|
||||
genArgValues(n.rhs)
|
||||
}
|
||||
}
|
||||
|
||||
func genProveReasonRequirement(n *node, name string) error {
|
||||
l := genName(n.lhs)
|
||||
r := genName(n.rhs)
|
||||
key := keys[n.op]
|
||||
if key == "" {
|
||||
return fmt.Errorf("bad op %q", n.op)
|
||||
}
|
||||
if l[0] == 't' {
|
||||
if err := genProveReasonRequirement(n.lhs, l); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if r[0] == 't' {
|
||||
if err := genProveReasonRequirement(n.rhs, r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if name == "" {
|
||||
fmt.Fprintf(&out, "if err := proveReasonRequirement(q, t.ID%s, %s, %s); err != nil { return err }\n",
|
||||
key, l, r)
|
||||
} else {
|
||||
fmt.Fprintf(&out, "%s := a.NewExpr(a.FlagsTypeChecked, t.ID%s, 0, %s.Node(), nil, %s.Node(), nil)\n",
|
||||
name, key, l, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parse(s string) (*node, error) {
|
||||
n, s, err := parseExpr(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s != "" {
|
||||
return nil, fmt.Errorf("parse error")
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func parseExpr(s string) (*node, string, error) {
|
||||
lhs, s, err := parseOperand(s)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
s = trim(s)
|
||||
if !isOp(s) {
|
||||
return nil, "", fmt.Errorf("parseExpr error")
|
||||
}
|
||||
i := 1
|
||||
for ; i < len(s) && isOpByte(s[i]); i++ {
|
||||
}
|
||||
op, s := s[:i], s[i:]
|
||||
s = trim(s)
|
||||
rhs, s, err := parseOperand(s)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
s = trim(s)
|
||||
return &node{
|
||||
op: op,
|
||||
lhs: lhs,
|
||||
rhs: rhs,
|
||||
}, s, nil
|
||||
}
|
||||
|
||||
func parseOperand(s string) (*node, string, error) {
|
||||
switch {
|
||||
case isConstant(s), isVariable(s):
|
||||
i := 1
|
||||
for ; i < len(s) && isAlphaNumByte(s[i]); i++ {
|
||||
}
|
||||
return &node{op: s[:i]}, s[i:], nil
|
||||
case isParen(s):
|
||||
n, s, err := parseExpr(s[1:])
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
if len(s) > 0 && s[0] == ')' {
|
||||
return n, s[1:], nil
|
||||
}
|
||||
}
|
||||
return nil, "", fmt.Errorf("parseOperand error")
|
||||
}
|
||||
|
||||
type node struct {
|
||||
op string
|
||||
lhs *node
|
||||
rhs *node
|
||||
}
|
||||
|
||||
func (n *node) String() string {
|
||||
if n == nil {
|
||||
return "$NIL_NODE"
|
||||
}
|
||||
switch {
|
||||
case isConstant(n.op), isVariable(n.op):
|
||||
return n.op
|
||||
case isOp(n.op):
|
||||
return fmt.Sprintf("(%s %v %v)", n.op, n.lhs, n.rhs)
|
||||
}
|
||||
return "$BAD_NODE"
|
||||
}
|
||||
|
||||
func isConstant(s string) bool { return s != "" && '0' <= s[0] && s[0] <= '9' }
|
||||
func isOp(s string) bool { return s != "" && isOpByte(s[0]) }
|
||||
func isParen(s string) bool { return s != "" && '(' == s[0] }
|
||||
func isVariable(s string) bool { return s != "" && 'a' <= s[0] && s[0] <= 'z' }
|
||||
|
||||
func isOpByte(b byte) bool { return ('<' <= b && b <= '>') || '+' == b || '-' == b || '!' == b }
|
||||
func isAlphaNumByte(b byte) bool { return ('0' <= b && b <= '9') || ('a' <= b && b <= 'z') }
|
||||
|
||||
func trim(s string) string {
|
||||
for ; len(s) > 0 && s[0] == ' '; s = s[1:] {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
var keys = map[string]string{
|
||||
"+": "XBinaryPlus",
|
||||
"-": "XBinaryMinus",
|
||||
"!=": "XBinaryNotEq",
|
||||
"<": "XBinaryLessThan",
|
||||
"<=": "XBinaryLessEq",
|
||||
"==": "XBinaryEqEq",
|
||||
">=": "XBinaryGreaterEq",
|
||||
">": "XBinaryGreaterThan",
|
||||
}
|
||||
|
||||
const header = `// Code generated by running "go generate". DO NOT EDIT.
|
||||
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
package check
|
||||
|
||||
import (
|
||||
a "github.com/google/puffs/lang/ast"
|
||||
t "github.com/google/puffs/lang/token"
|
||||
)
|
||||
|
||||
var reasons = [...]struct {
|
||||
s string
|
||||
r reason
|
||||
}{
|
||||
`
|
||||
|
||||
const footer = "}\n"
|
||||
+48
-34
@@ -1,3 +1,5 @@
|
||||
// Code generated by running "go generate". DO NOT EDIT.
|
||||
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
@@ -12,56 +14,74 @@ var reasons = [...]struct {
|
||||
s string
|
||||
r reason
|
||||
}{
|
||||
|
||||
{`"a < (b + c): a < c; 0 <= b"`, func(q *checker, n *a.Assert) error {
|
||||
op, xa, xbc := parseBinaryOp(n.Condition())
|
||||
op, xa, t0 := parseBinaryOp(n.Condition())
|
||||
if op.Key() != t.KeyXBinaryLessThan {
|
||||
return errFailed
|
||||
}
|
||||
op, xb, xc := parseBinaryOp(xbc)
|
||||
op, xb, xc := parseBinaryOp(t0)
|
||||
if op.Key() != t.KeyXBinaryPlus {
|
||||
return errFailed
|
||||
}
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessThan, xa, xc); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessThan, xa, xc, err)
|
||||
// a < c
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xa, xc); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessEq, zeroExpr, xb); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessEq, zeroExpr, xb, err)
|
||||
// 0 <= b
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, zeroExpr, xb); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
|
||||
{`"(a + b) <= c: a <= (c - b)"`, func(q *checker, n *a.Assert) error {
|
||||
op, xab, xc := parseBinaryOp(n.Condition())
|
||||
op, t0, xc := parseBinaryOp(n.Condition())
|
||||
if op.Key() != t.KeyXBinaryLessEq {
|
||||
return errFailed
|
||||
}
|
||||
op, xa, xb := parseBinaryOp(xab)
|
||||
op, xa, xb := parseBinaryOp(t0)
|
||||
if op.Key() != t.KeyXBinaryPlus {
|
||||
return errFailed
|
||||
}
|
||||
sub := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryMinus, 0, xc.Node(), nil, xb.Node(), nil)
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessEq, xa, sub); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessEq, xa, sub, err)
|
||||
// a <= (c - b)
|
||||
t1 := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryMinus, 0, xc.Node(), nil, xb.Node(), nil)
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xa, t1); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
|
||||
{`"a < b: a < c; c <= b"`, func(q *checker, n *a.Assert) error {
|
||||
xc := argValue(q.tm, n.Args(), "c")
|
||||
if xc == nil {
|
||||
return errFailed
|
||||
}
|
||||
op, xa, xb := parseBinaryOp(n.Condition())
|
||||
if op.Key() != t.KeyXBinaryLessThan {
|
||||
return errFailed
|
||||
}
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessThan, xa, xc); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessThan, xa, xc, err)
|
||||
// a < c
|
||||
xc := argValue(q.tm, n.Args(), "c")
|
||||
if xc == nil {
|
||||
return errFailed
|
||||
}
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessEq, xc, xb); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessEq, xc, xb, err)
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xa, xc); err != nil {
|
||||
return err
|
||||
}
|
||||
// c <= b
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xc, xb); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
|
||||
{`"a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"`, func(q *checker, n *a.Assert) error {
|
||||
op, xa, t0 := parseBinaryOp(n.Condition())
|
||||
if op.Key() != t.KeyXBinaryLessThan {
|
||||
return errFailed
|
||||
}
|
||||
op, xb, xc := parseBinaryOp(t0)
|
||||
if op.Key() != t.KeyXBinaryPlus {
|
||||
return errFailed
|
||||
}
|
||||
// a < (b0 + c0)
|
||||
xb0 := argValue(q.tm, n.Args(), "b0")
|
||||
if xb0 == nil {
|
||||
return errFailed
|
||||
@@ -70,23 +90,17 @@ var reasons = [...]struct {
|
||||
if xc0 == nil {
|
||||
return errFailed
|
||||
}
|
||||
op, xa, xbc := parseBinaryOp(n.Condition())
|
||||
if op.Key() != t.KeyXBinaryLessThan {
|
||||
return errFailed
|
||||
t1 := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryPlus, 0, xb0.Node(), nil, xc0.Node(), nil)
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessThan, xa, t1); err != nil {
|
||||
return err
|
||||
}
|
||||
op, xb, xc := parseBinaryOp(xbc)
|
||||
if op.Key() != t.KeyXBinaryPlus {
|
||||
return errFailed
|
||||
// b0 <= b
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xb0, xb); err != nil {
|
||||
return err
|
||||
}
|
||||
plus := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryPlus, 0, xb0.Node(), nil, xc0.Node(), nil)
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessThan, xa, plus); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessThan, xa, plus, err)
|
||||
}
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessEq, xb0, xb); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessEq, xb0, xb, err)
|
||||
}
|
||||
if err := q.proveBinaryOp(t.KeyXBinaryLessEq, xc0, xc); err != nil {
|
||||
return binOpReasonError(q.tm, t.IDXBinaryLessEq, xc0, xc, err)
|
||||
// c0 <= c
|
||||
if err := proveReasonRequirement(q, t.IDXBinaryLessEq, xc0, xc); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}},
|
||||
|
||||
Reference in new issue
Block a user