Rename token.IDMap to token.Map.

This commit is contained in:
Nigel Tao committed 2017-06-08 13:10:01 +10:00
1 parent f6cc7950ff
commit 6e7e566232
15 files changed
+230 -230

No files matched your search

+24 -24
View File
@@ -34,10 +34,10 @@ const (
)
func main() {
generate.Main(func(pkgName string, idMap *t.IDMap, files []*a.File) ([]byte, error) {
generate.Main(func(pkgName string, tm *t.Map, files []*a.File) ([]byte, error) {
g := &gen{
pkgName: pkgName,
idMap: idMap,
tm: tm,
files: files,
}
if err := g.generate(); err != nil {
@@ -99,7 +99,7 @@ type perFunc struct {
type gen struct {
buffer bytes.Buffer
pkgName string
idMap *t.IDMap
tm *t.Map
files []*a.File
perFunc perFunc
}
@@ -270,7 +270,7 @@ func (g *gen) writeStruct(n *a.Struct) error {
// etc_error_bad_version;" regardless of the sizeof(*this) struct reserved
// by the caller and even if the caller and callee were built with
// different versions.
structName := n.Name().String(g.idMap)
structName := n.Name().String(g.tm)
g.printf("typedef struct {\n")
if n.Suspendible() {
g.printf("puffs_%s_status status;\n", g.pkgName)
@@ -287,7 +287,7 @@ func (g *gen) writeStruct(n *a.Struct) error {
}
func (g *gen) writeCtorSignature(n *a.Struct, public bool, ctor bool) {
structName := n.Name().String(g.idMap)
structName := n.Name().String(g.tm)
ctorName := "destructor"
if ctor {
ctorName = "constructor"
@@ -349,7 +349,7 @@ func (g *gen) writeCtorImpls(n *a.Struct) error {
f := f.Field()
if dv := f.DefaultValue(); dv != nil {
// TODO: set default values for array types.
g.printf("self->%s%s = %d;\n", fPrefix, f.Name().String(g.idMap), dv.ConstValue())
g.printf("self->%s%s = %d;\n", fPrefix, f.Name().String(g.tm), dv.ConstValue())
}
}
}
@@ -369,13 +369,13 @@ func (g *gen) writeFuncSignature(n *a.Func) error {
}
g.printf(" puffs_%s", g.pkgName)
if r := n.Receiver(); r != 0 {
g.printf("_%s", r.String(g.idMap))
g.printf("_%s", r.String(g.tm))
}
g.printf("_%s(", n.Name().String(g.idMap))
g.printf("_%s(", n.Name().String(g.tm))
comma := false
if r := n.Receiver(); r != 0 {
g.printf("puffs_%s_%s *self", g.pkgName, r.String(g.idMap))
g.printf("puffs_%s_%s *self", g.pkgName, r.String(g.tm))
comma = true
}
for _, o := range n.In().Fields() {
@@ -431,7 +431,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
} else if r := n.Receiver(); r != 0 {
// TODO: fix this.
return fmt.Errorf(`cannot convert Puffs function "%s.%s" to C`,
r.String(g.idMap), n.Name().String(g.idMap))
r.String(g.tm), n.Name().String(g.tm))
}
if n.Public() {
badArg := false
@@ -447,7 +447,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
} else {
g.writes("if (")
}
g.printf("!%s%s", aPrefix, o.Name().String(g.idMap))
g.printf("!%s%s", aPrefix, o.Name().String(g.tm))
badArg = true
}
if badArg {
@@ -494,7 +494,7 @@ func (g *gen) writeField(n *a.Field, namePrefix string) error {
for x := n.XType(); x != nil; x = x.Inner() {
if p := x.PackageOrDecorator().Key(); p == t.KeyPtr {
if nPtr == maxNPtr {
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.XType().String(g.idMap))
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.XType().String(g.tm))
}
nPtr++
continue
@@ -516,14 +516,14 @@ func (g *gen) writeField(n *a.Field, namePrefix string) error {
}
if !convertible {
// TODO: fix this.
return fmt.Errorf("cannot convert Puffs type %q to C", n.XType().String(g.idMap))
return fmt.Errorf("cannot convert Puffs type %q to C", n.XType().String(g.tm))
}
for i := 0; i < nPtr; i++ {
g.writeb('*')
}
g.writes(namePrefix)
g.writes(n.Name().String(g.idMap))
g.writes(n.Name().String(g.tm))
for x := n.XType(); x != nil; x = x.Inner() {
if x.PackageOrDecorator() == t.IDOpenBracket {
@@ -547,12 +547,12 @@ func (g *gen) writeVars(n *a.Node, depth uint32) error {
// TODO: use g.writeCTypeName.
if k := x.Name().Key(); k < t.Key(len(cTypeNames)) {
if s := cTypeNames[k]; s != "" {
g.printf("%s %s%s;\n", s, vPrefix, n.Var().Name().String(g.idMap))
g.printf("%s %s%s;\n", s, vPrefix, n.Var().Name().String(g.tm))
return nil
}
}
// TODO: fix this.
return fmt.Errorf("cannot convert Puffs type %q to C", x.String(g.idMap))
return fmt.Errorf("cannot convert Puffs type %q to C", x.String(g.tm))
}
for _, l := range n.Raw().SubLists() {
@@ -643,7 +643,7 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
case a.KVar:
n := n.Var()
// TODO: consider suspendible calls.
g.printf("%s%s = ", vPrefix, n.Name().String(g.idMap))
g.printf("%s%s = ", vPrefix, n.Name().String(g.tm))
if v := n.Value(); v != nil {
if err := g.writeExpr(v, replaceCallSuspendibles, 0); err != nil {
return err
@@ -724,7 +724,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error {
g.perFunc.tempW++
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(g.idMap, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(g.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
// TODO: suspend coroutine state.
//
// TODO: choose error_unexpected_eof versus status_short_read based on
@@ -745,7 +745,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error {
// TODO: fix this.
//
// This might involve calling g.writeExpr with replaceNothing??
return fmt.Errorf("cannot convert Puffs call %q to C", n.String(g.idMap))
return fmt.Errorf("cannot convert Puffs call %q to C", n.String(g.tm))
}
return nil
}
@@ -808,7 +808,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
} else {
// TODO: don't assume that the vPrefix is necessary.
g.writes(vPrefix)
g.writes(id1.String(g.idMap))
g.writes(id1.String(g.tm))
}
return nil
@@ -833,18 +833,18 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
// TODO: don't assume that the fPrefix is necessary.
g.writes("->")
g.writes(fPrefix)
g.writes(n.ID1().String(g.idMap))
g.writes(n.ID1().String(g.tm))
return nil
}
return fmt.Errorf("unrecognized token.Key (0x%X) for writeExprOther", n.ID0().Key())
}
func isInSrcReadU8(m *t.IDMap, n *a.Expr) bool {
func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1() != m.ByName("src") {
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("src") {
return false
}
n = n.LHS().Expr()
@@ -902,7 +902,7 @@ func (g *gen) writeCTypeName(n *a.TypeExpr) error {
}
}
// TODO: fix this.
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.idMap))
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
}
var cTypeNames = [...]string{
+5 -5
View File
@@ -72,7 +72,7 @@ func genDir(puffsRoot string, dirname string, filenames []string, langs []string
// TODO: skip the generation if the output file already exists and its
// mtime is newer than all inputs and the puffs-gen-foo command.
idMap := &token.IDMap{}
tm := &token.Map{}
files := []*ast.File(nil)
buf := &bytes.Buffer{}
for _, filename := range filenames {
@@ -81,20 +81,20 @@ func genDir(puffsRoot string, dirname string, filenames []string, langs []string
if err != nil {
return err
}
tokens, _, err := token.Tokenize(idMap, filename, src)
tokens, _, err := token.Tokenize(tm, filename, src)
if err != nil {
return err
}
f, err := parse.Parse(idMap, filename, tokens)
f, err := parse.Parse(tm, filename, tokens)
if err != nil {
return err
}
files = append(files, f)
if err := render.Render(buf, idMap, tokens, nil); err != nil {
if err := render.Render(buf, tm, tokens, nil); err != nil {
return err
}
}
if _, err := check.Check(idMap, 0, files...); err != nil {
if _, err := check.Check(tm, 0, files...); err != nil {
return err
}
combinedSrc := buf.Bytes()
+4 -4
View File
@@ -107,18 +107,18 @@ func do(r io.Reader, filename string) error {
return err
}
idMap := &token.IDMap{}
tokens, comments, err := token.Tokenize(idMap, filename, src)
tm := &token.Map{}
tokens, comments, err := token.Tokenize(tm, filename, src)
if err != nil {
return err
}
// We don't need the AST node to pretty-print, but it's worth puffsfmt
// rejecting syntax errors. This is just a parse, not a full type check.
if _, err := parse.Parse(idMap, filename, tokens); err != nil {
if _, err := parse.Parse(tm, filename, tokens); err != nil {
return err
}
buf := &bytes.Buffer{}
if err := render.Render(buf, idMap, tokens, comments); err != nil {
if err := render.Render(buf, tm, tokens, comments); err != nil {
return err
}
dst := buf.Bytes()
+32 -32
View File
@@ -8,17 +8,17 @@ import (
)
// String returns a string form of n.
func (n *Expr) String(m *t.IDMap) string {
func (n *Expr) String(tm *t.Map) string {
if n == nil {
return ""
}
if n.id0 == 0 {
return m.ByID(n.id1)
return tm.ByID(n.id1)
}
return string(n.appendString(nil, m, false, 0))
return string(n.appendString(nil, tm, false, 0))
}
func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uint32) []byte {
func (n *Expr) appendString(buf []byte, tm *t.Map, parenthesize bool, depth uint32) []byte {
if depth > MaxExprDepth {
return append(buf, "!expr_recursion_depth_too_large!"...)
}
@@ -29,10 +29,10 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uin
case 0:
switch n.id0.Key() {
case 0:
buf = append(buf, m.ByID(n.id1)...)
buf = append(buf, tm.ByID(n.id1)...)
case t.KeyOpenParen:
buf = n.lhs.Expr().appendString(buf, m, true, depth)
buf = n.lhs.Expr().appendString(buf, tm, true, depth)
if n.flags&FlagsSuspendible != 0 {
buf = append(buf, '?')
}
@@ -41,46 +41,46 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uin
if i != 0 {
buf = append(buf, ", "...)
}
buf = append(buf, m.ByID(o.Arg().Name())...)
buf = append(buf, tm.ByID(o.Arg().Name())...)
buf = append(buf, ':')
buf = o.Arg().Value().appendString(buf, m, false, depth)
buf = o.Arg().Value().appendString(buf, tm, false, depth)
}
buf = append(buf, ')')
case t.KeyOpenBracket:
buf = n.lhs.Expr().appendString(buf, m, true, depth)
buf = n.lhs.Expr().appendString(buf, tm, true, depth)
buf = append(buf, '[')
buf = n.rhs.Expr().appendString(buf, m, false, depth)
buf = n.rhs.Expr().appendString(buf, tm, false, depth)
buf = append(buf, ']')
case t.KeyColon:
buf = n.lhs.Expr().appendString(buf, m, true, depth)
buf = n.lhs.Expr().appendString(buf, tm, true, depth)
buf = append(buf, '[')
buf = n.mhs.Expr().appendString(buf, m, false, depth)
buf = n.mhs.Expr().appendString(buf, tm, false, depth)
buf = append(buf, ':')
buf = n.rhs.Expr().appendString(buf, m, false, depth)
buf = n.rhs.Expr().appendString(buf, tm, false, depth)
buf = append(buf, ']')
case t.KeyDot:
buf = n.lhs.Expr().appendString(buf, m, true, depth)
buf = n.lhs.Expr().appendString(buf, tm, true, depth)
buf = append(buf, '.')
buf = append(buf, m.ByID(n.id1)...)
buf = append(buf, tm.ByID(n.id1)...)
}
case t.FlagsUnaryOp:
buf = append(buf, opStrings[0xFF&n.id0.Key()]...)
buf = n.rhs.Expr().appendString(buf, m, true, depth)
buf = n.rhs.Expr().appendString(buf, tm, true, depth)
case t.FlagsBinaryOp:
if parenthesize {
buf = append(buf, '(')
}
buf = n.lhs.Expr().appendString(buf, m, true, depth)
buf = n.lhs.Expr().appendString(buf, tm, true, depth)
buf = append(buf, opStrings[0xFF&n.id0.Key()]...)
if n.id0.Key() == t.KeyXBinaryAs {
buf = append(buf, n.rhs.TypeExpr().String(m)...)
buf = append(buf, n.rhs.TypeExpr().String(tm)...)
} else {
buf = n.rhs.Expr().appendString(buf, m, true, depth)
buf = n.rhs.Expr().appendString(buf, tm, true, depth)
}
if parenthesize {
buf = append(buf, ')')
@@ -95,7 +95,7 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uin
if i != 0 {
buf = append(buf, op...)
}
buf = o.Expr().appendString(buf, m, true, depth)
buf = o.Expr().appendString(buf, tm, true, depth)
}
if parenthesize {
buf = append(buf, ')')
@@ -141,17 +141,17 @@ var opStrings = [256]string{
}
// String returns a string form of n.
func (n *TypeExpr) String(m *t.IDMap) string {
func (n *TypeExpr) String(tm *t.Map) string {
if n == nil {
return ""
}
if n.PackageOrDecorator() == 0 && n.Min() == nil && n.Max() == nil {
return m.ByID(n.Name())
return tm.ByID(n.Name())
}
return string(n.appendString(nil, m, 0))
return string(n.appendString(nil, tm, 0))
}
func (n *TypeExpr) appendString(buf []byte, m *t.IDMap, depth uint32) []byte {
func (n *TypeExpr) appendString(buf []byte, tm *t.Map, depth uint32) []byte {
if depth > MaxTypeExprDepth {
return append(buf, "!type_expr_recursion_depth_too_large!"...)
}
@@ -162,25 +162,25 @@ func (n *TypeExpr) appendString(buf []byte, m *t.IDMap, depth uint32) []byte {
switch n.PackageOrDecorator().Key() {
case 0:
buf = append(buf, m.ByID(n.Name())...)
buf = append(buf, tm.ByID(n.Name())...)
case t.KeyPtr:
buf = append(buf, "ptr "...)
return n.Inner().appendString(buf, m, depth)
return n.Inner().appendString(buf, tm, depth)
case t.KeyOpenBracket:
buf = append(buf, '[')
buf = n.ArrayLength().appendString(buf, m, false, 0)
buf = n.ArrayLength().appendString(buf, tm, false, 0)
buf = append(buf, "] "...)
return n.Inner().appendString(buf, m, depth)
return n.Inner().appendString(buf, tm, depth)
default:
buf = append(buf, m.ByID(n.PackageOrDecorator())...)
buf = append(buf, tm.ByID(n.PackageOrDecorator())...)
buf = append(buf, '.')
buf = append(buf, m.ByID(n.Name())...)
buf = append(buf, tm.ByID(n.Name())...)
}
if n.Min() != nil || n.Max() != nil {
buf = append(buf, '[')
buf = n.Min().appendString(buf, m, false, 0)
buf = n.Min().appendString(buf, tm, false, 0)
buf = append(buf, ".."...)
buf = n.Max().appendString(buf, m, false, 0)
buf = n.Max().appendString(buf, tm, false, 0)
buf = append(buf, ']')
}
return buf
+4 -4
View File
@@ -59,19 +59,19 @@ func TestString(t *testing.T) {
"x as [8 + (2 * N)] ptr [4] ptr pkg.T[i..j]",
}
idMap := &token.IDMap{}
tm := &token.Map{}
for _, tc := range testCases {
tokens, _, err := token.Tokenize(idMap, filename, []byte(tc))
tokens, _, err := token.Tokenize(tm, filename, []byte(tc))
if err != nil {
t.Errorf("Tokenize(%q): %v", tc, err)
continue
}
expr, err := parse.ParseExpr(idMap, filename, tokens)
expr, err := parse.ParseExpr(tm, filename, tokens)
if err != nil {
t.Errorf("ParseExpr(%q): %v", tc, err)
continue
}
got := expr.String(idMap)
got := expr.String(tm)
if got != tc {
t.Errorf("got %q, want %q", got, tc)
continue
+7 -7
View File
@@ -135,8 +135,8 @@ func simplify(n *a.Expr) *a.Expr {
return n
}
func argValue(m *t.IDMap, args []*a.Node, name string) *a.Expr {
if x := m.ByName(name); x != 0 {
func argValue(tm *t.Map, args []*a.Node, name string) *a.Expr {
if x := tm.ByName(name); x != 0 {
for _, a := range args {
if a.Arg().Name() == x {
return a.Arg().Value()
@@ -251,15 +251,15 @@ var reasons = [...]struct {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.idMap), c.String(q.idMap))
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.tm), c.String(q.tm))
}
if !q.proveBinaryOp(t.KeyXBinaryLessEq, zeroExpr, b) {
return fmt.Errorf("cannot prove \"%s <= %s\"", zeroExpr.String(q.idMap), b.String(q.idMap))
return fmt.Errorf("cannot prove \"%s <= %s\"", zeroExpr.String(q.tm), b.String(q.tm))
}
return nil
}},
{`"a < b: a < c; c <= b"`, func(q *checker, n *a.Assert) error {
c := argValue(q.idMap, n.Args(), "c")
c := argValue(q.tm, n.Args(), "c")
if c == nil {
return errFailed
}
@@ -268,10 +268,10 @@ var reasons = [...]struct {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.idMap), c.String(q.idMap))
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.tm), c.String(q.tm))
}
if !q.proveBinaryOp(t.KeyXBinaryLessEq, c, b) {
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.idMap), b.String(q.idMap))
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.tm), b.String(q.tm))
}
return nil
}},
+23 -23
View File
@@ -92,12 +92,12 @@ func bitMask(nBits int) *big.Int {
return z.Sub(z, one)
}
func invert(m *t.IDMap, n *a.Expr) (*a.Expr, error) {
func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
if !n.MType().IsBool() {
return nil, fmt.Errorf("check: invert(%q) called on non-bool-typed expression", n.String(m))
return nil, fmt.Errorf("check: invert(%q) called on non-bool-typed expression", n.String(tm))
}
if cv := n.ConstValue(); cv != nil {
return nil, fmt.Errorf("check: invert(%q) called on constant expression", n.String(m))
return nil, fmt.Errorf("check: invert(%q) called on constant expression", n.String(tm))
}
id0, lhs, rhs := n.ID0(), n.LHS().Expr(), n.RHS().Expr()
switch id0.Key() {
@@ -124,7 +124,7 @@ func invert(m *t.IDMap, n *a.Expr) (*a.Expr, error) {
return o, nil
}
func bcheckField(idMap *t.IDMap, n *a.Field) error {
func bcheckField(tm *t.Map, n *a.Field) error {
x := n.XType()
for ; x.Inner() != nil; x = x.Inner() {
}
@@ -140,7 +140,7 @@ func bcheckField(idMap *t.IDMap, n *a.Field) error {
}
if dv.Cmp(nMin) < 0 || dv.Cmp(nMax) > 0 {
return fmt.Errorf("check: default value %v is not within bounds [%v..%v] for field %q",
dv, nMin, nMax, n.Name().String(idMap))
dv, nMin, nMax, n.Name().String(tm))
}
return nil
}
@@ -225,7 +225,7 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
if reasonFunc := q.reasonMap[reasonID.Key()]; reasonFunc != nil {
err = reasonFunc(q, n)
} else {
err = fmt.Errorf("no such reason %s", reasonID.String(q.idMap))
err = fmt.Errorf("no such reason %s", reasonID.String(q.tm))
}
} else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs {
if q.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) {
@@ -234,9 +234,9 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
}
if err != nil {
if err == errFailed {
return fmt.Errorf("check: cannot prove %q", condition.String(q.idMap))
return fmt.Errorf("check: cannot prove %q", condition.String(q.tm))
}
return fmt.Errorf("check: cannot prove %q: %v", condition.String(q.idMap), err)
return fmt.Errorf("check: cannot prove %q: %v", condition.String(q.tm), err)
}
q.facts.appendFact(condition)
return nil
@@ -322,10 +322,10 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
if rMin.Cmp(lMin) < 0 || rMax.Cmp(lMax) > 0 {
if op == t.IDEq {
return fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
rhs.String(q.idMap), rMin, rMax, lMin, lMax)
rhs.String(q.tm), rMin, rMax, lMin, lMax)
} else {
return fmt.Errorf("check: assignment %q bounds [%v..%v] is not within bounds [%v..%v]",
lhs.String(q.idMap)+" "+op.String(q.idMap)+" "+rhs.String(q.idMap),
lhs.String(q.tm)+" "+op.String(q.tm)+" "+rhs.String(q.tm),
rMin, rMax, lMin, lMax)
}
}
@@ -406,7 +406,7 @@ func (q *checker) bcheckIf(n *a.If) error {
// Check the if-false branch, assuming the inverted if condition.
q.facts = append(q.facts[:0], snap...)
if inverse, err := invert(q.idMap, n.Condition()); err != nil {
if inverse, err := invert(q.tm, n.Condition()); err != nil {
return err
} else {
q.facts.appendFact(inverse)
@@ -471,7 +471,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
}
q.facts.appendFact(o.Assert().Condition())
}
if inverse, err := invert(q.idMap, n.Condition()); err != nil {
if inverse, err := invert(q.tm, n.Condition()); err != nil {
return err
} else {
q.facts.appendFact(inverse)
@@ -577,7 +577,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
case t.KeyOpenParen:
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(q.idMap, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
break
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
@@ -672,22 +672,22 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
case t.KeyXBinaryShiftL:
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.tm))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.tm))
}
if rMax.Cmp(ffff) > 0 {
return nil, nil, fmt.Errorf("check: shift %q out of range", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift %q out of range", rhs.String(q.tm))
}
return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil
case t.KeyXBinaryShiftR:
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.tm))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.tm))
}
if rMin.Cmp(maxIntBits) >= 0 {
return zero, zero, nil
@@ -702,16 +702,16 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
// TODO: should type-checking ensure that bitwise ops only apply to
// *unsigned* integer types?
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.String(q.tm))
}
if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.String(q.tm))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.String(q.tm))
}
if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.String(q.tm))
}
z := (*big.Int)(nil)
if op == t.KeyXBinaryAmp {
@@ -750,7 +750,7 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
b = numTypeBounds[key]
}
if b[0] == nil || b[1] == nil {
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.idMap))
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.tm))
}
if n.IsRefined() {
if x := n.Min(); x != nil {
+29 -29
View File
@@ -24,7 +24,7 @@ type Error struct {
OtherFilename string
OtherLine uint32
IDMap *t.IDMap
TMap *t.Map
Facts []*a.Expr
}
@@ -36,13 +36,13 @@ func (e *Error) Error() string {
} else {
s = fmt.Sprintf("%s at %s:%d", e.Err, e.Filename, e.Line)
}
if e.IDMap == nil {
if e.TMap == nil {
return s
}
b := append([]byte(s), ". Facts:\n"...)
for _, f := range e.Facts {
b = append(b, '\t')
b = append(b, f.String(e.IDMap)...)
b = append(b, f.String(e.TMap)...)
b = append(b, '\n')
}
return string(b)
@@ -71,7 +71,7 @@ type Func struct {
LocalVars TypeMap
}
func Check(m *t.IDMap, flags Flags, files ...*a.File) (*Checker, error) {
func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) {
for _, f := range files {
if f == nil {
return nil, errors.New("check: Check given a nil *ast.File")
@@ -90,13 +90,13 @@ func Check(m *t.IDMap, flags Flags, files ...*a.File) (*Checker, error) {
rMap := reasonMap{}
for _, r := range reasons {
if id := m.ByName(r.s); id != 0 {
if id := tm.ByName(r.s); id != 0 {
rMap[id.Key()] = r.r
}
}
c := &Checker{
flags: flags,
idMap: m,
tm: tm,
reasonMap: rMap,
structs: map[t.ID]Struct{},
funcs: map[t.QID]Func{},
@@ -131,7 +131,7 @@ var phases = [...]struct {
type Checker struct {
flags Flags
idMap *t.IDMap
tm *t.Map
reasonMap reasonMap
structs map[t.ID]Struct
funcs map[t.QID]Func
@@ -150,38 +150,38 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool) error {
}
q := &checker{
c: c,
idMap: c.idMap,
c: c,
tm: c.tm,
}
fieldNames := map[t.ID]bool{}
for _, n := range fields {
f := n.Field()
if _, ok := fieldNames[f.Name()]; ok {
return fmt.Errorf("check: duplicate field %q", f.Name().String(c.idMap))
return fmt.Errorf("check: duplicate field %q", f.Name().String(c.tm))
}
if err := q.tcheckTypeExpr(f.XType(), 0); err != nil {
return fmt.Errorf("%v in field %q", err, f.Name().String(c.idMap))
return fmt.Errorf("%v in field %q", err, f.Name().String(c.tm))
}
if banPtrTypes {
for x := f.XType(); x.Inner() != nil; x = x.Inner() {
if x.PackageOrDecorator().Key() == t.KeyPtr {
// TODO: implement nptr (nullable pointer) types.
return fmt.Errorf("check: ptr type %q not allowed for field %q; use nptr instead",
x.String(c.idMap), f.Name().String(c.idMap))
x.String(c.tm), f.Name().String(c.tm))
}
}
}
if dv := f.DefaultValue(); dv != nil {
if f.XType().PackageOrDecorator() != 0 {
return fmt.Errorf("check: cannot set default value for qualified type %q for field %q",
f.XType().String(c.idMap), f.Name().String(c.idMap))
f.XType().String(c.tm), f.Name().String(c.tm))
}
if err := q.tcheckExpr(dv, 0); err != nil {
return err
}
}
if c.flags&FlagsOnlyTypeCheck == 0 {
if err := bcheckField(c.idMap, f); err != nil {
if err := bcheckField(c.tm, f); err != nil {
return err
}
}
@@ -196,7 +196,7 @@ func (c *Checker) checkStruct(node *a.Node) error {
n := node.Struct()
if err := c.checkFields(n.Fields(), true); err != nil {
return &Error{
Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.idMap)),
Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -204,7 +204,7 @@ func (c *Checker) checkStruct(node *a.Node) error {
id := n.Name()
if other, ok := c.structs[id]; ok {
return &Error{
Err: fmt.Errorf("check: duplicate struct %q", id.String(c.idMap)),
Err: fmt.Errorf("check: duplicate struct %q", id.String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
OtherFilename: other.Struct.Filename(),
@@ -223,7 +223,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
n := node.Func()
if err := c.checkFields(n.In().Fields(), false); err != nil {
return &Error{
Err: fmt.Errorf("%v in in-params for func %q", err, n.Name().String(c.idMap)),
Err: fmt.Errorf("%v in in-params for func %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -231,7 +231,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
n.In().Node().SetTypeChecked()
if err := c.checkFields(n.Out().Fields(), false); err != nil {
return &Error{
Err: fmt.Errorf("%v in out-params for func %q", err, n.Name().String(c.idMap)),
Err: fmt.Errorf("%v in out-params for func %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -241,7 +241,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
qid := n.QID()
if other, ok := c.funcs[qid]; ok {
return &Error{
Err: fmt.Errorf("check: duplicate function %q", qid.String(c.idMap)),
Err: fmt.Errorf("check: duplicate function %q", qid.String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
OtherFilename: other.Func.Filename(),
@@ -260,7 +260,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
if qid[0] != 0 {
if _, ok := c.structs[qid[0]]; !ok {
return &Error{
Err: fmt.Errorf("check: no receiver struct defined for function %q", qid.String(c.idMap)),
Err: fmt.Errorf("check: no receiver struct defined for function %q", qid.String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -285,8 +285,8 @@ func (c *Checker) checkFuncContract(node *a.Node) error {
return nil
}
q := &checker{
c: c,
idMap: c.idMap,
c: c,
tm: c.tm,
}
for _, o := range n.Asserts() {
if err := q.tcheckAssert(o.Assert()); err != nil {
@@ -300,7 +300,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
n := node.Func()
q := &checker{
c: c,
idMap: c.idMap,
tm: c.tm,
reasonMap: c.reasonMap,
f: c.funcs[n.QID()],
}
@@ -338,7 +338,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
Err: err,
Filename: q.errFilename,
Line: q.errLine,
IDMap: c.idMap,
TMap: c.tm,
Facts: q.facts,
}
}
@@ -350,10 +350,10 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
switch o.Kind() {
case a.KExpr:
return fmt.Errorf("check: internal error: unchecked %s node %q",
o.Kind(), o.Expr().String(q.idMap))
o.Kind(), o.Expr().String(q.tm))
case a.KTypeExpr:
return fmt.Errorf("check: internal error: unchecked %s node %q",
o.Kind(), o.TypeExpr().String(q.idMap))
o.Kind(), o.TypeExpr().String(q.tm))
}
return fmt.Errorf("check: internal error: unchecked %s node", o.Kind())
}
@@ -361,10 +361,10 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
o := o.Expr()
if typ := o.MType(); typ == nil {
return fmt.Errorf("check: internal error: expression %q has no (implicit) type",
o.String(q.idMap))
o.String(q.tm))
} else if typ == TypeExprIdeal && o.ConstValue() == nil {
return fmt.Errorf("check: internal error: expression %q has ideal number type "+
"but no const value", o.String(q.idMap))
"but no const value", o.String(q.tm))
}
}
return nil
@@ -377,7 +377,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
type checker struct {
c *Checker
idMap *t.IDMap
tm *t.Map
reasonMap reasonMap
f Func
+14 -14
View File
@@ -18,9 +18,9 @@ import (
"github.com/google/puffs/lang/token"
)
func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, comments []string, src string) error {
func compareToPuffsfmt(tm *token.Map, tokens []token.Token, comments []string, src string) error {
buf := &bytes.Buffer{}
if err := render.Render(buf, idMap, tokens, comments); err != nil {
if err := render.Render(buf, tm, tokens, comments); err != nil {
return err
}
got := strings.Split(buf.String(), "\n")
@@ -81,23 +81,23 @@ func TestCheck(t *testing.T) {
}
`) + "\n"
idMap := &token.IDMap{}
tm := &token.Map{}
tokens, comments, err := token.Tokenize(idMap, filename, []byte(src))
tokens, comments, err := token.Tokenize(tm, filename, []byte(src))
if err != nil {
t.Fatalf("Tokenize: %v", err)
}
file, err := parse.Parse(idMap, filename, tokens)
file, err := parse.Parse(tm, filename, tokens)
if err != nil {
t.Fatalf("Parse: %v", err)
}
if err := compareToPuffsfmt(idMap, tokens, comments, src); err != nil {
if err := compareToPuffsfmt(tm, tokens, comments, src); err != nil {
t.Fatalf("compareToPuffsfmt: %v", err)
}
c, err := Check(idMap, 0, file)
c, err := Check(tm, 0, file)
if err != nil {
t.Fatalf("Check: %v", err)
}
@@ -112,15 +112,15 @@ func TestCheck(t *testing.T) {
break
}
if got, want := fooBar.QID.String(idMap), "foo.bar"; got != want {
if got, want := fooBar.QID.String(tm), "foo.bar"; got != want {
t.Fatalf("Funcs[0] name: got %q, want %q", got, want)
}
got := [][2]string(nil)
for id, typ := range fooBar.LocalVars {
got = append(got, [2]string{
id.String(idMap),
typ.String(idMap),
id.String(tm),
typ.String(tm),
})
}
sort.Slice(got, func(i, j int) bool {
@@ -178,23 +178,23 @@ func TestConstValues(t *testing.T) {
"var b bool = false or true": 1,
}
idMap := &token.IDMap{}
tm := &token.Map{}
for s, wantInt64 := range testCases {
src := "pri func foo()() {\n\t" + s + "\n}\n"
tokens, _, err := token.Tokenize(idMap, filename, []byte(src))
tokens, _, err := token.Tokenize(tm, filename, []byte(src))
if err != nil {
t.Errorf("%q: Tokenize: %v", s, err)
continue
}
file, err := parse.Parse(idMap, filename, tokens)
file, err := parse.Parse(tm, filename, tokens)
if err != nil {
t.Errorf("%q: Parse: %v", s, err)
continue
}
c, err := Check(idMap, 0, file)
c, err := Check(tm, 0, file)
if err != nil {
t.Errorf("%q: Check: %v", s, err)
continue
+39 -39
View File
@@ -18,7 +18,7 @@ func (q *checker) tcheckVars(n *a.Node) error {
n := n.Var()
name := n.Name()
if _, ok := q.f.LocalVars[name]; ok {
return fmt.Errorf("check: duplicate var %q", name.String(q.idMap))
return fmt.Errorf("check: duplicate var %q", name.String(q.tm))
}
if err := q.tcheckTypeExpr(n.XType(), 0); err != nil {
return err
@@ -58,7 +58,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
if !cond.MType().Eq(TypeExprBool) {
return fmt.Errorf("check: if condition %q, of type %q, does not have a boolean type",
cond.String(q.idMap), cond.MType().String(q.idMap))
cond.String(q.tm), cond.MType().String(q.tm))
}
for _, o := range n.BodyIfTrue() {
if err := q.tcheckStatement(o); err != nil {
@@ -88,10 +88,10 @@ func (q *checker) tcheckStatement(n *a.Node) error {
if jumpTarget == nil {
sepStr, labelStr := "", ""
if id := n.Label(); id != 0 {
sepStr, labelStr = ":", id.String(q.idMap)
sepStr, labelStr = ":", id.String(q.tm)
}
return fmt.Errorf("no matching while statement for %s%s%s",
n.Keyword().String(q.idMap), sepStr, labelStr)
n.Keyword().String(q.tm), sepStr, labelStr)
}
if n.Keyword().Key() == t.KeyBreak {
jumpTarget.SetHasBreak()
@@ -113,7 +113,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
case a.KVar:
n := n.Var()
if !n.XType().Node().TypeChecked() {
return fmt.Errorf("check: internal error: unchecked type expression %q", n.XType().String(q.idMap))
return fmt.Errorf("check: internal error: unchecked type expression %q", n.XType().String(q.tm))
}
if value := n.Value(); value != nil {
if err := q.tcheckExpr(value, 0); err != nil {
@@ -130,7 +130,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
if !cond.MType().Eq(TypeExprBool) {
return fmt.Errorf("check: for-loop condition %q, of type %q, does not have a boolean type",
cond.String(q.idMap), cond.MType().String(q.idMap))
cond.String(q.tm), cond.MType().String(q.tm))
}
for _, o := range n.Asserts() {
if err := q.tcheckAssert(o.Assert()); err != nil {
@@ -163,7 +163,7 @@ func (q *checker) tcheckAssert(n *a.Assert) error {
}
if !cond.MType().Eq(TypeExprBool) {
return fmt.Errorf("check: assert condition %q, of type %q, does not have a boolean type",
cond.String(q.idMap), cond.MType().String(q.idMap))
cond.String(q.tm), cond.MType().String(q.tm))
}
for _, o := range n.Args() {
if err := q.tcheckExpr(o.Arg().Value(), 0); err != nil {
@@ -192,12 +192,12 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
return nil
}
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
rhs.String(q.idMap), rTyp.String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
rhs.String(q.tm), rTyp.String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
if !lTyp.IsNumType() {
return fmt.Errorf("check: assignment %q: assignee %q, of type %q, does not have numeric type",
n.Operator().String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
n.Operator().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
switch n.Operator().Key() {
@@ -206,16 +206,16 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
return nil
}
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
n.Operator().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
if rTyp == TypeExprIdeal || lTyp.EqIgnoringRefinements(rTyp) {
return nil
}
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
n.Operator().String(q.idMap),
lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(q.idMap), rTyp.String(q.idMap),
n.Operator().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
@@ -255,7 +255,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
id1 := n.ID1()
if id1.IsNumLiteral() {
z := big.NewInt(0)
s := id1.String(q.idMap)
s := id1.String(q.tm)
if _, ok := z.SetString(s, 0); !ok {
return fmt.Errorf("check: invalid numeric literal %q", s)
}
@@ -271,7 +271,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
}
}
// TODO: look for (global) names (constants, funcs, structs).
return fmt.Errorf("check: unrecognized identifier %q", id1.String(q.idMap))
return fmt.Errorf("check: unrecognized identifier %q", id1.String(q.tm))
}
switch id1.Key() {
case t.KeyFalse:
@@ -294,7 +294,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
case t.KeyOpenParen:
// n is a function call.
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(q.idMap, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
return err
}
@@ -316,12 +316,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprOther", n.ID0().Key())
}
func isInSrcReadU8(m *t.IDMap, n *a.Expr) bool {
func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1() != m.ByName("src") {
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("src") {
return false
}
n = n.LHS().Expr()
@@ -363,7 +363,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
if s == nil {
return fmt.Errorf("check: no struct type %q found for expression %q",
lTyp.Name().String(q.idMap), lhs.String(q.idMap))
lTyp.Name().String(q.tm), lhs.String(q.tm))
}
for _, field := range s.Fields() {
@@ -374,7 +374,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
}
return fmt.Errorf("check: no field named %q found in struct type %q for expression %q",
n.ID1().String(q.idMap), lTyp.Name().String(q.idMap), n.String(q.idMap))
n.ID1().String(q.tm), lTyp.Name().String(q.tm), n.String(q.tm))
}
func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
@@ -388,7 +388,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
case t.KeyXUnaryPlus, t.KeyXUnaryMinus:
if !numeric(rTyp) {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a numeric type",
n.ID0().AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
if cv := rhs.ConstValue(); cv != nil {
if n.ID0().Key() == t.KeyXUnaryMinus {
@@ -402,7 +402,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
case t.KeyXUnaryNot:
if !rTyp.Eq(TypeExprBool) {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a boolean type",
n.ID0().AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
if cv := rhs.ConstValue(); cv != nil {
n.SetConstValue(btoi(cv.Cmp(zero) == 0))
@@ -430,7 +430,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return nil
}
return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q",
lhs.String(q.idMap), lTyp.String(q.idMap), rhs.String(q.idMap))
lhs.String(q.tm), lTyp.String(q.tm), rhs.String(q.tm))
}
rhs := n.RHS().Expr()
if err := q.tcheckExpr(rhs, depth); err != nil {
@@ -442,22 +442,22 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
default:
if !numeric(lTyp) {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
if !numeric(rTyp) {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
case t.KeyXBinaryNotEq, t.KeyXBinaryEqEq:
// No-op.
case t.KeyXBinaryAnd, t.KeyXBinaryOr:
if lTyp != TypeExprBool {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
if rTyp != TypeExprBool {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
}
@@ -465,18 +465,18 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
default:
if !lTyp.EqIgnoringRefinements(rTyp) && lTyp != TypeExprIdeal && rTyp != TypeExprIdeal {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types",
op.AmbiguousForm().String(q.idMap),
lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(q.idMap), rTyp.String(q.idMap),
op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR:
if (lTyp == TypeExprIdeal) && (rTyp != TypeExprIdeal) {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+
"cannot shift an ideal number by a non-ideal number",
op.AmbiguousForm().String(q.idMap),
lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(q.idMap), rTyp.String(q.idMap),
op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
}
@@ -508,7 +508,7 @@ func (q *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
n.SetConstValue(big.NewInt(0).Mul(l, r))
case t.KeyXBinarySlash:
if r.Cmp(zero) == 0 {
return fmt.Errorf("check: division by zero in const expression %q", n.String(q.idMap))
return fmt.Errorf("check: division by zero in const expression %q", n.String(q.tm))
}
// TODO: decide on Euclidean division vs other definitions. See "go doc
// math/big int.divmod" for details.
@@ -516,13 +516,13 @@ func (q *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
case t.KeyXBinaryShiftL:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(q.idMap), n.String(q.idMap))
n.RHS().Expr().String(q.tm), n.String(q.tm))
}
n.SetConstValue(big.NewInt(0).Lsh(l, uint(r.Uint64())))
case t.KeyXBinaryShiftR:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(q.idMap), n.String(q.idMap))
n.RHS().Expr().String(q.tm), n.String(q.tm))
}
n.SetConstValue(big.NewInt(0).Rsh(l, uint(r.Uint64())))
case t.KeyXBinaryAmp:
@@ -575,7 +575,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
return err
}
if b.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", b.String(q.idMap))
return fmt.Errorf("check: %q is not constant", b.String(q.tm))
}
}
break
@@ -587,7 +587,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
break
}
// TODO: see if name refers to a struct type.
return fmt.Errorf("check: %q is not a type", n.Name().String(q.idMap))
return fmt.Errorf("check: %q is not a type", n.Name().String(q.tm))
case t.KeyPtr:
if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil {
@@ -600,7 +600,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
return err
}
if aLen.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", aLen.String(q.idMap))
return fmt.Errorf("check: %q is not constant", aLen.String(q.tm))
}
if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil {
return err
+10 -10
View File
@@ -22,7 +22,7 @@ var (
packageName = flag.String("package_name", "", "the package name of the Puffs input code")
)
type Generator func(packageName string, m *token.IDMap, files []*ast.File) ([]byte, error)
type Generator func(packageName string, tm *token.Map, files []*ast.File) ([]byte, error)
func Main(g Generator) {
if err := main1(g); err != nil {
@@ -35,8 +35,8 @@ func Main(g Generator) {
func main1(g Generator) error {
flag.Parse()
idMap := &token.IDMap{}
files, err := parseFiles(idMap)
tm := &token.Map{}
files, err := parseFiles(tm)
if err != nil {
return err
}
@@ -47,11 +47,11 @@ func main1(g Generator) error {
if *assumeAlreadyChecked {
checkFlags |= check.FlagsOnlyTypeCheck
}
if _, err := check.Check(idMap, checkFlags, files...); err != nil {
if _, err := check.Check(tm, checkFlags, files...); err != nil {
return err
}
out, err := g(*packageName, idMap, files)
out, err := g(*packageName, tm, files)
if err != nil {
return err
}
@@ -62,18 +62,18 @@ func main1(g Generator) error {
return nil
}
func parseFiles(idMap *token.IDMap) (files []*ast.File, err error) {
func parseFiles(tm *token.Map) (files []*ast.File, err error) {
if len(flag.Args()) == 0 {
const filename = "stdin"
src, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
tokens, _, err := token.Tokenize(idMap, filename, src)
tokens, _, err := token.Tokenize(tm, filename, src)
if err != nil {
return nil, err
}
f, err := parse.Parse(idMap, filename, tokens)
f, err := parse.Parse(tm, filename, tokens)
if err != nil {
return nil, err
}
@@ -85,11 +85,11 @@ func parseFiles(idMap *token.IDMap) (files []*ast.File, err error) {
if err != nil {
return nil, err
}
tokens, _, err := token.Tokenize(idMap, filename, src)
tokens, _, err := token.Tokenize(tm, filename, src)
if err != nil {
return nil, err
}
f, err := parse.Parse(idMap, filename, tokens)
f, err := parse.Parse(tm, filename, tokens)
if err != nil {
return nil, err
}
+27 -27
View File
@@ -12,10 +12,10 @@ import (
t "github.com/google/puffs/lang/token"
)
func Parse(m *t.IDMap, filename string, src []t.Token) (*a.File, error) {
func Parse(tm *t.Map, filename string, src []t.Token) (*a.File, error) {
p := &parser{
src: src,
m: m,
tm: tm,
filename: filename,
}
if len(src) > 0 {
@@ -24,10 +24,10 @@ func Parse(m *t.IDMap, filename string, src []t.Token) (*a.File, error) {
return p.parseFile()
}
func ParseExpr(m *t.IDMap, filename string, src []t.Token) (*a.Expr, error) {
func ParseExpr(tm *t.Map, filename string, src []t.Token) (*a.Expr, error) {
p := &parser{
src: src,
m: m,
tm: tm,
filename: filename,
}
if len(src) > 0 {
@@ -38,7 +38,7 @@ func ParseExpr(m *t.IDMap, filename string, src []t.Token) (*a.Expr, error) {
type parser struct {
src []t.Token
m *t.IDMap
tm *t.Map
filename string
lastLine uint32
}
@@ -77,12 +77,12 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
p.src = p.src[1:]
path := p.peek1()
if !path.IsStrLiteral() {
got := p.m.ByID(path)
got := p.tm.ByID(path)
return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
if x := p.peek1().Key(); x != t.KeySemicolon {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -102,11 +102,11 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
}
if id0 != 0 && id0.IsBuiltIn() {
return nil, fmt.Errorf(`parse: built-in %q used for func receiver at %s:%d`,
p.m.ByID(id0), p.filename, p.line())
p.tm.ByID(id0), p.filename, p.line())
}
if id1.IsBuiltIn() {
return nil, fmt.Errorf(`parse: built-in %q used for func name at %s:%d`,
p.m.ByID(id1), p.filename, p.line())
p.tm.ByID(id1), p.filename, p.line())
}
switch p.peek1().Key() {
case t.KeyExclam:
@@ -140,7 +140,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
return nil, err
}
if x := p.peek1().Key(); x != t.KeySemicolon {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -156,7 +156,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
}
if name.IsBuiltIn() {
return nil, fmt.Errorf(`parse: built-in %q used for struct name at %s:%d`,
p.m.ByID(name), p.filename, p.line())
p.tm.ByID(name), p.filename, p.line())
}
if p.peek1().Key() == t.KeyQuestion {
flags |= a.FlagsSuspendible
@@ -167,7 +167,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
return nil, err
}
if x := p.peek1().Key(); x != t.KeySemicolon {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -202,7 +202,7 @@ func (p *parser) parseIdent() (t.ID, error) {
}
x := p.src[0]
if !x.IsIdent() {
got := p.m.ByToken(x)
got := p.tm.ByToken(x)
return 0, fmt.Errorf(`parse: expected identifier, got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -213,7 +213,7 @@ func (p *parser) parseList(stop t.Key, parseElem func(*parser) (*a.Node, error))
if stop == t.KeyCloseParen {
if x := p.peek1().Key(); x != t.KeyOpenParen {
return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`,
p.m.ByKey(x), p.filename, p.line())
p.tm.ByKey(x), p.filename, p.line())
}
p.src = p.src[1:]
}
@@ -243,10 +243,10 @@ func (p *parser) parseList(stop t.Key, parseElem func(*parser) (*a.Node, error))
p.src = p.src[1:]
default:
return nil, fmt.Errorf(`parse: expected %q, got %q at %s:%d`,
p.m.ByKey(stop), p.m.ByKey(x), p.filename, p.line())
p.tm.ByKey(stop), p.tm.ByKey(x), p.filename, p.line())
}
}
return nil, fmt.Errorf(`parse: expected %q at %s:%d`, p.m.ByKey(stop), p.filename, p.line())
return nil, fmt.Errorf(`parse: expected %q at %s:%d`, p.tm.ByKey(stop), p.filename, p.line())
}
func (p *parser) parseFieldNode() (*a.Node, error) {
@@ -286,7 +286,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
return nil, err
}
if x := p.peek1().Key(); x != t.KeyCloseBracket {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -319,7 +319,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
// t.IDOpenBracket for an index.
func (p *parser) parseBracket(sep t.ID) (op t.ID, ei *a.Expr, ej *a.Expr, err error) {
if x := p.peek1().Key(); x != t.KeyOpenBracket {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return 0, nil, nil, fmt.Errorf(`parse: expected "[", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -344,9 +344,9 @@ func (p *parser) parseBracket(sep t.ID) (op t.ID, ei *a.Expr, ej *a.Expr, err er
if sep.Key() == t.KeyColon {
extra = ` or "]"`
}
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return 0, nil, nil, fmt.Errorf(`parse: expected %q%s, got %q at %s:%d`,
p.m.ByID(sep), extra, got, p.filename, p.line())
p.tm.ByID(sep), extra, got, p.filename, p.line())
}
if p.peek1().Key() != t.KeyCloseBracket {
@@ -357,7 +357,7 @@ func (p *parser) parseBracket(sep t.ID) (op t.ID, ei *a.Expr, ej *a.Expr, err er
}
if x := p.peek1().Key(); x != t.KeyCloseBracket {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return 0, nil, nil, fmt.Errorf(`parse: expected "]", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -367,7 +367,7 @@ func (p *parser) parseBracket(sep t.ID) (op t.ID, ei *a.Expr, ej *a.Expr, err er
func (p *parser) parseBlock() ([]*a.Node, error) {
if x := p.peek1().Key(); x != t.KeyOpenCurly {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "{", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -386,7 +386,7 @@ func (p *parser) parseBlock() ([]*a.Node, error) {
block = append(block, s)
if x := p.peek1().Key(); x != t.KeySemicolon {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -435,7 +435,7 @@ func (p *parser) parseAssertNode() (*a.Node, error) {
p.src = p.src[1:]
reason = p.peek1()
if !reason.IsStrLiteral() {
got := p.m.ByID(reason)
got := p.tm.ByID(reason)
return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -564,7 +564,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
func (p *parser) parseIf() (*a.If, error) {
if x := p.peek1().Key(); x != t.KeyIf {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected "if", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -600,7 +600,7 @@ func (p *parser) parseArgNode() (*a.Node, error) {
return nil, err
}
if x := p.peek1().Key(); x != t.KeyColon {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected ":", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
@@ -668,7 +668,7 @@ func (p *parser) parseOperand() (*a.Expr, error) {
return nil, err
}
if x := p.peek1().Key(); x != t.KeyCloseParen {
got := p.m.ByKey(x)
got := p.tm.ByKey(x)
return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line())
}
p.src = p.src[1:]
+2 -2
View File
@@ -16,7 +16,7 @@ import (
var newLine = []byte{'\n'}
func Render(w io.Writer, m *token.IDMap, src []token.Token, comments []string) (err error) {
func Render(w io.Writer, tm *token.Map, src []token.Token, comments []string) (err error) {
if len(src) == 0 {
return nil
}
@@ -109,7 +109,7 @@ func Render(w io.Writer, m *token.IDMap, src []token.Token, comments []string) (
}
}
buf = append(buf, m.ByToken(t)...)
buf = append(buf, tm.ByToken(t)...)
if t.ID.Key() == token.KeyOpenCurly {
if indent == maxIndent {
+3 -3
View File
@@ -36,7 +36,7 @@ const (
flagsUnused = Flags(0x8000)
)
// Key is the high 16 bits of an ID. It is the map key for an IDMap.
// Key is the high 16 bits of an ID. It is the map key for a Map.
type Key uint32
const (
@@ -51,7 +51,7 @@ const (
type ID uint32
// String returns a string form of x.
func (x ID) String(m *IDMap) string { return m.ByID(x) }
func (x ID) String(m *Map) string { return m.ByID(x) }
func (x ID) AmbiguousForm() ID { return ambiguousForms[0xFF&(x>>KeyShift)] }
func (x ID) UnaryForm() ID { return unaryForms[0xFF&(x>>KeyShift)] }
@@ -83,7 +83,7 @@ func (x ID) IsNumType() bool { return Flags(x)&FlagsNumType != 0 }
type QID [2]ID
// String returns a string form of x.
func (x QID) String(m *IDMap) string {
func (x QID) String(m *Map) string {
if x[0] == 0 {
return m.ByID(x[1])
}
+7 -7
View File
@@ -13,12 +13,12 @@ const (
maxTokenSize = 1023
)
type IDMap struct {
type Map struct {
byName map[string]ID
byKey []string
}
func (m *IDMap) insert(name string) (ID, error) {
func (m *Map) insert(name string) (ID, error) {
if name == "" {
return 0, nil
}
@@ -50,7 +50,7 @@ func (m *IDMap) insert(name string) (ID, error) {
return id, nil
}
func (m *IDMap) ByName(name string) ID {
func (m *Map) ByName(name string) ID {
if id, ok := builtInsByName[name]; ok {
return id
}
@@ -60,10 +60,10 @@ func (m *IDMap) ByName(name string) ID {
return 0
}
func (m *IDMap) ByID(x ID) string { return m.ByKey(Key(x >> KeyShift)) }
func (m *IDMap) ByToken(t Token) string { return m.ByKey(Key(t.ID >> KeyShift)) }
func (m *Map) ByID(x ID) string { return m.ByKey(Key(x >> KeyShift)) }
func (m *Map) ByToken(t Token) string { return m.ByKey(Key(t.ID >> KeyShift)) }
func (m *IDMap) ByKey(k Key) string {
func (m *Map) ByKey(k Key) string {
if k < nBuiltInKeys {
return builtInsByKey[k].name
}
@@ -102,7 +102,7 @@ func hasPrefix(a []byte, s string) bool {
return true
}
func Tokenize(m *IDMap, filename string, src []byte) (tokens []Token, comments []string, retErr error) {
func Tokenize(m *Map, filename string, src []byte) (tokens []Token, comments []string, retErr error) {
line := uint32(1)
loop:
for i := 0; i < len(src); {