mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Add a Checker.Funcs method.
This commit is contained in:
1 parent
1d07846295
commit
98352c2f07
3 files changed
+128
-11
No files matched your search
+51
-11
@@ -16,12 +16,45 @@ var (
|
|||||||
// DummyTypeFoo are dummy values for the a.Node.Type field:
|
// DummyTypeFoo are dummy values for the a.Node.Type field:
|
||||||
// - DummyTypeType means that n is itself a type.
|
// - DummyTypeType means that n is itself a type.
|
||||||
// - DummyTypeNumber means that n is an ideal numeric constant.
|
// - DummyTypeNumber means that n is an ideal numeric constant.
|
||||||
DummyTypeType = &a.Node{}
|
DummyTypeType = &a.Node{Kind: a.KType}
|
||||||
DummyTypeIdealNumber = &a.Node{}
|
DummyTypeIdealNumber = &a.Node{Kind: a.KType}
|
||||||
)
|
)
|
||||||
|
|
||||||
// scope maps from names to types.
|
// TypeString returns a string form of the type n.
|
||||||
type scope map[t.ID]*a.Node
|
func TypeString(n *a.Node, m *t.IDMap) string {
|
||||||
|
if n == nil {
|
||||||
|
return "!nil!"
|
||||||
|
}
|
||||||
|
if n.Kind != a.KType {
|
||||||
|
return "!not_a_type!"
|
||||||
|
}
|
||||||
|
switch n {
|
||||||
|
case DummyTypeType:
|
||||||
|
return "!invalid_type!"
|
||||||
|
case DummyTypeIdealNumber:
|
||||||
|
return "!ideal!"
|
||||||
|
}
|
||||||
|
switch n.ID0 {
|
||||||
|
case 0:
|
||||||
|
return m.ByID(n.ID1)
|
||||||
|
case t.IDPtr:
|
||||||
|
return "ptr " + TypeString(n.RHS, m)
|
||||||
|
case t.IDOpenBracket:
|
||||||
|
// TODO.
|
||||||
|
default:
|
||||||
|
return m.ByID(n.ID0) + "." + m.ByID(n.ID1)
|
||||||
|
}
|
||||||
|
return "!invalid_type!"
|
||||||
|
}
|
||||||
|
|
||||||
|
// TypeMap maps from variable names (as token IDs) to types.
|
||||||
|
type TypeMap map[t.ID]*a.Node
|
||||||
|
|
||||||
|
type Func struct {
|
||||||
|
QID t.QID
|
||||||
|
Node *a.Node
|
||||||
|
LocalVars TypeMap
|
||||||
|
}
|
||||||
|
|
||||||
func Check(m *t.IDMap, files ...*a.Node) (*Checker, error) {
|
func Check(m *t.IDMap, files ...*a.Node) (*Checker, error) {
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
@@ -44,8 +77,8 @@ func Check(m *t.IDMap, files ...*a.Node) (*Checker, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c := &Checker{
|
c := &Checker{
|
||||||
idMap: m,
|
idMap: m,
|
||||||
funcScopes: map[*a.Node]scope{},
|
funcs: map[t.QID]Func{},
|
||||||
}
|
}
|
||||||
for _, phase := range phases {
|
for _, phase := range phases {
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
@@ -67,10 +100,12 @@ var phases = [...]func(*Checker, *a.Node) error{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Checker struct {
|
type Checker struct {
|
||||||
idMap *t.IDMap
|
idMap *t.IDMap
|
||||||
funcScopes map[*a.Node]scope
|
funcs map[t.QID]Func
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
|
||||||
|
|
||||||
func (c *Checker) checkUse(n *a.Node) error {
|
func (c *Checker) checkUse(n *a.Node) error {
|
||||||
if n.Kind != a.KUse {
|
if n.Kind != a.KUse {
|
||||||
return nil
|
return nil
|
||||||
@@ -99,17 +134,22 @@ func (c *Checker) checkFuncBody(n *a.Node) error {
|
|||||||
if n.Kind != a.KFunc {
|
if n.Kind != a.KFunc {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
p := scope{}
|
p := TypeMap{}
|
||||||
for _, m := range n.List2 {
|
for _, m := range n.List2 {
|
||||||
if err := c.checkStatement(m, p); err != nil {
|
if err := c.checkStatement(m, p); err != nil {
|
||||||
return fmt.Errorf("%v at %s:%d", err, m.Filename, m.Line)
|
return fmt.Errorf("%v at %s:%d", err, m.Filename, m.Line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.funcScopes[n] = p
|
qid := t.QID{n.ID0, n.ID1}
|
||||||
|
c.funcs[qid] = Func{
|
||||||
|
QID: qid,
|
||||||
|
Node: n,
|
||||||
|
LocalVars: p,
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) checkStatement(n *a.Node, p scope) error {
|
func (c *Checker) checkStatement(n *a.Node, p TypeMap) error {
|
||||||
if n == nil {
|
if n == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
// Use of this source code is governed by a BSD-style license that can be found
|
||||||
|
// in the LICENSE file.
|
||||||
|
|
||||||
|
package check
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/puffs/lang/parse"
|
||||||
|
"github.com/google/puffs/lang/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheck(t *testing.T) {
|
||||||
|
const filename = "foo.puffs"
|
||||||
|
const src = `
|
||||||
|
func bar()() {
|
||||||
|
x = 1 as u8
|
||||||
|
y = 2 as i32
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
idMap := &token.IDMap{}
|
||||||
|
|
||||||
|
tokens, _, err := token.Tokenize([]byte(src), idMap, filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Tokenize: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
node, err := parse.ParseFile(tokens, idMap, filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ParseFile: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := Check(idMap, node)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Check: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
funcs := c.Funcs()
|
||||||
|
if len(funcs) != 1 {
|
||||||
|
t.Fatalf("Funcs: got %d elements, want 1", len(funcs))
|
||||||
|
}
|
||||||
|
bar := Func{}
|
||||||
|
for _, f := range funcs {
|
||||||
|
bar = f
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, want := idMap.ByID(bar.QID[1]), "bar"; got != want {
|
||||||
|
t.Fatalf("Funcs[0] name: got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := [][2]string(nil)
|
||||||
|
for id, typ := range bar.LocalVars {
|
||||||
|
got = append(got, [2]string{
|
||||||
|
idMap.ByID(id),
|
||||||
|
TypeString(typ, idMap),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
sort.Slice(got, func(i, j int) bool {
|
||||||
|
return got[i][0] < got[j][0]
|
||||||
|
})
|
||||||
|
|
||||||
|
want := [][2]string{
|
||||||
|
{"x", "u8"},
|
||||||
|
{"y", "i32"},
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Fatalf("\ngot %v\nwant %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -70,6 +70,10 @@ func (x ID) IsAssign() bool { return Flags(x)&FlagsAssign != 0 }
|
|||||||
func (x ID) IsImplicitSemicolon() bool { return Flags(x)&FlagsImplicitSemicolon != 0 }
|
func (x ID) IsImplicitSemicolon() bool { return Flags(x)&FlagsImplicitSemicolon != 0 }
|
||||||
func (x ID) IsNumType() bool { return Flags(x)&FlagsNumType != 0 }
|
func (x ID) IsNumType() bool { return Flags(x)&FlagsNumType != 0 }
|
||||||
|
|
||||||
|
// QID is a qualified ID, such as "foo.bar". QID[0] is "foo"'s ID and QID[1] is
|
||||||
|
// "bar"'s. QID[0] may be 0 for a plain "bar".
|
||||||
|
type QID [2]ID
|
||||||
|
|
||||||
// Token combines an ID and the line number it was seen.
|
// Token combines an ID and the line number it was seen.
|
||||||
type Token struct {
|
type Token struct {
|
||||||
ID ID
|
ID ID
|
||||||
|
|||||||
Reference in new issue
Block a user