diff --git a/lang/check/check.go b/lang/check/check.go index b34bfacc..362d5561 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -16,12 +16,45 @@ var ( // DummyTypeFoo are dummy values for the a.Node.Type field: // - DummyTypeType means that n is itself a type. // - DummyTypeNumber means that n is an ideal numeric constant. - DummyTypeType = &a.Node{} - DummyTypeIdealNumber = &a.Node{} + DummyTypeType = &a.Node{Kind: a.KType} + DummyTypeIdealNumber = &a.Node{Kind: a.KType} ) -// scope maps from names to types. -type scope map[t.ID]*a.Node +// TypeString returns a string form of the type n. +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) { for _, f := range files { @@ -44,8 +77,8 @@ func Check(m *t.IDMap, files ...*a.Node) (*Checker, error) { } c := &Checker{ - idMap: m, - funcScopes: map[*a.Node]scope{}, + idMap: m, + funcs: map[t.QID]Func{}, } for _, phase := range phases { for _, f := range files { @@ -67,10 +100,12 @@ var phases = [...]func(*Checker, *a.Node) error{ } type Checker struct { - idMap *t.IDMap - funcScopes map[*a.Node]scope + idMap *t.IDMap + funcs map[t.QID]Func } +func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } + func (c *Checker) checkUse(n *a.Node) error { if n.Kind != a.KUse { return nil @@ -99,17 +134,22 @@ func (c *Checker) checkFuncBody(n *a.Node) error { if n.Kind != a.KFunc { return nil } - p := scope{} + p := TypeMap{} for _, m := range n.List2 { if err := c.checkStatement(m, p); err != nil { 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 } -func (c *Checker) checkStatement(n *a.Node, p scope) error { +func (c *Checker) checkStatement(n *a.Node, p TypeMap) error { if n == nil { return nil } diff --git a/lang/check/check_test.go b/lang/check/check_test.go new file mode 100644 index 00000000..0fb4f278 --- /dev/null +++ b/lang/check/check_test.go @@ -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) + } +} diff --git a/lang/token/list.go b/lang/token/list.go index 193d6550..dc20e093 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -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) 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. type Token struct { ID ID