diff --git a/README.md b/README.md index 4e7102e9..bc5a7ba3 100644 --- a/README.md +++ b/README.md @@ -288,11 +288,12 @@ be enclosed by curly `{}`s. There is no 'dangling else' ambiguity. ## Keywords -Puffs is a simple language, with only 22 keywords. 5 of these introduce +Puffs is a simple language, with only 23 keywords. 6 of these introduce top-level concepts: - `error` - `func` +- `packageid` - `status` - `struct` - `use` diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index b778b161..64bce886 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -370,11 +370,11 @@ func (g *gen) cName(name string) string { } func (g *gen) gatherStatuses(n *a.Status) error { - msg := n.Message().String(g.tm) - if len(msg) < 2 || msg[0] != '"' || msg[len(msg)-1] != '"' { - return fmt.Errorf("bad status message %q", msg) + raw := n.Message().String(g.tm) + msg, ok := t.Unescape(raw) + if !ok { + return fmt.Errorf("bad status message %q", raw) } - msg = msg[1 : len(msg)-1] prefix := "status " isError := n.Keyword().Key() == t.KeyError if isError { diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 88c8be08..82b05b0e 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -30,6 +30,7 @@ const ( KFunc KIf KJump + KPackageID KReturn KStatus KStruct @@ -49,22 +50,23 @@ func (k Kind) String() string { var kindStrings = [...]string{ KInvalid: "KInvalid", - KArg: "KArg", - KAssert: "KAssert", - KAssign: "KAssign", - KExpr: "KExpr", - KField: "KField", - KFile: "KFile", - KFunc: "KFunc", - KIf: "KIf", - KJump: "KJump", - KReturn: "KReturn", - KStatus: "KStatus", - KStruct: "KStruct", - KTypeExpr: "KTypeExpr", - KUse: "KUse", - KVar: "KVar", - KWhile: "KWhile", + KArg: "KArg", + KAssert: "KAssert", + KAssign: "KAssign", + KExpr: "KExpr", + KField: "KField", + KFile: "KFile", + KFunc: "KFunc", + KIf: "KIf", + KJump: "KJump", + KPackageID: "KPackageID", + KReturn: "KReturn", + KStatus: "KStatus", + KStruct: "KStruct", + KTypeExpr: "KTypeExpr", + KUse: "KUse", + KVar: "KVar", + KWhile: "KWhile", } type Flags uint32 @@ -104,23 +106,24 @@ func (n *Node) Kind() Kind { return n.kind } func (n *Node) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 } func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked } -func (n *Node) Arg() *Arg { return (*Arg)(n) } -func (n *Node) Assert() *Assert { return (*Assert)(n) } -func (n *Node) Assign() *Assign { return (*Assign)(n) } -func (n *Node) Expr() *Expr { return (*Expr)(n) } -func (n *Node) Field() *Field { return (*Field)(n) } -func (n *Node) File() *File { return (*File)(n) } -func (n *Node) Func() *Func { return (*Func)(n) } -func (n *Node) If() *If { return (*If)(n) } -func (n *Node) Jump() *Jump { return (*Jump)(n) } -func (n *Node) Raw() *Raw { return (*Raw)(n) } -func (n *Node) Return() *Return { return (*Return)(n) } -func (n *Node) Status() *Status { return (*Status)(n) } -func (n *Node) Struct() *Struct { return (*Struct)(n) } -func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) } -func (n *Node) Use() *Use { return (*Use)(n) } -func (n *Node) Var() *Var { return (*Var)(n) } -func (n *Node) While() *While { return (*While)(n) } +func (n *Node) Arg() *Arg { return (*Arg)(n) } +func (n *Node) Assert() *Assert { return (*Assert)(n) } +func (n *Node) Assign() *Assign { return (*Assign)(n) } +func (n *Node) Expr() *Expr { return (*Expr)(n) } +func (n *Node) Field() *Field { return (*Field)(n) } +func (n *Node) File() *File { return (*File)(n) } +func (n *Node) Func() *Func { return (*Func)(n) } +func (n *Node) If() *If { return (*If)(n) } +func (n *Node) Jump() *Jump { return (*Jump)(n) } +func (n *Node) PackageID() *PackageID { return (*PackageID)(n) } +func (n *Node) Raw() *Raw { return (*Raw)(n) } +func (n *Node) Return() *Return { return (*Return)(n) } +func (n *Node) Status() *Status { return (*Status)(n) } +func (n *Node) Struct() *Struct { return (*Struct)(n) } +func (n *Node) TypeExpr() *TypeExpr { return (*TypeExpr)(n) } +func (n *Node) Use() *Use { return (*Use)(n) } +func (n *Node) Var() *Var { return (*Var)(n) } +func (n *Node) While() *While { return (*While)(n) } func (n *Node) Walk(f func(*Node) error) error { if n != nil { @@ -624,6 +627,24 @@ func NewStruct(flags Flags, filename string, line uint32, name t.ID, fields []*N } } +// PackageID is "packageid ID1": +// - ID1: package ID +type PackageID Node + +func (n *PackageID) Node() *Node { return (*Node)(n) } +func (n *PackageID) Filename() string { return n.filename } +func (n *PackageID) Line() uint32 { return n.line } +func (n *PackageID) ID() t.ID { return n.id1 } + +func NewPackageID(filename string, line uint32, id t.ID) *PackageID { + return &PackageID{ + kind: KPackageID, + filename: filename, + line: line, + id1: id, + } +} + // Use is "use ID1": // - ID1: package path type Use Node @@ -643,7 +664,7 @@ func NewUse(filename string, line uint32, path t.ID) *Use { } // File is a file of source code: -// - List0: top-level declarations +// - List0: top-level declarations type File Node func (n *File) Node() *Node { return (*Node)(n) } diff --git a/lang/check/check.go b/lang/check/check.go index c4fa27cf..0ae237ee 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -7,6 +7,8 @@ import ( "errors" "fmt" + "github.com/google/puffs/lang/base38" + a "github.com/google/puffs/lang/ast" t "github.com/google/puffs/lang/token" ) @@ -104,6 +106,7 @@ func Check(tm *t.Map, files ...*a.File) (*Checker, error) { c := &Checker{ tm: tm, reasonMap: rMap, + packageID: base38.Max + 1, funcs: map[t.QID]Func{}, statuses: map[t.ID]Status{}, structs: map[t.ID]Struct{}, @@ -135,6 +138,7 @@ var phases = [...]struct { kind a.Kind check func(*Checker, *a.Node) error }{ + {a.KPackageID, (*Checker).checkPackageID}, {a.KUse, (*Checker).checkUse}, {a.KStatus, (*Checker).checkStatus}, {a.KStruct, (*Checker).checkStructDecl}, @@ -150,6 +154,9 @@ type Checker struct { tm *t.Map reasonMap reasonMap + packageID uint32 + otherPackageID *a.PackageID + funcs map[t.QID]Func statuses map[t.ID]Status structs map[t.ID]Struct @@ -157,10 +164,44 @@ type Checker struct { unsortedStructs []*a.Struct } +func (c *Checker) PackageID() uint32 { return c.packageID } func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } func (c *Checker) Statuses() map[t.ID]Status { return c.statuses } func (c *Checker) Structs() map[t.ID]Struct { return c.structs } +func (c *Checker) checkPackageID(node *a.Node) error { + n := node.PackageID() + if c.otherPackageID != nil { + return &Error{ + Err: fmt.Errorf("check: multiple packageid declarations"), + Filename: n.Filename(), + Line: n.Line(), + OtherFilename: c.otherPackageID.Filename(), + OtherLine: c.otherPackageID.Line(), + } + } + raw := n.ID().String(c.tm) + s, ok := t.Unescape(raw) + if !ok { + return &Error{ + Err: fmt.Errorf("check: %q is not a valid packageid", raw), + Filename: n.Filename(), + Line: n.Line(), + } + } + u, ok := base38.Encode(s) + if !ok || u == 0 { + return &Error{ + Err: fmt.Errorf("check: %q is not a valid packageid", s), + Filename: n.Filename(), + Line: n.Line(), + } + } + c.packageID = u + c.otherPackageID = n + return nil +} + func (c *Checker) checkUse(node *a.Node) error { // TODO. return nil diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 7fbf1ea3..31ce9c4a 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -8,6 +8,8 @@ package parse import ( "fmt" + "github.com/google/puffs/lang/base38" + a "github.com/google/puffs/lang/ast" t "github.com/google/puffs/lang/token" ) @@ -72,8 +74,8 @@ func (p *parser) parseFile() (*a.File, error) { func (p *parser) parseTopLevelDecl() (*a.Node, error) { flags := a.Flags(0) line := p.src[0].Line - switch p.peek1().Key() { - case t.KeyUse: + switch k := p.peek1().Key(); k { + case t.KeyPackageID, t.KeyUse: p.src = p.src[1:] path := p.peek1() if !path.IsStrLiteral() { @@ -86,7 +88,19 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line()) } p.src = p.src[1:] - return a.NewUse(p.filename, line, path).Node(), nil + if k == t.KeyPackageID { + raw := path.String(p.tm) + s, ok := t.Unescape(raw) + if !ok { + return nil, fmt.Errorf(`parse: %q is not a valid packageid`, raw) + } + if u, ok := base38.Encode(s); !ok || u == 0 { + return nil, fmt.Errorf(`parse: %q is not a valid packageid`, s) + } + return a.NewPackageID(p.filename, line, path).Node(), nil + } else { + return a.NewUse(p.filename, line, path).Node(), nil + } case t.KeyPub: flags |= a.FlagsPublic diff --git a/lang/token/list.go b/lang/token/list.go index 65a8fd6c..4cedd5f6 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -188,28 +188,29 @@ const ( KeyAs = Key(IDAs >> KeyShift) // TODO: sort these by name, when the list has stabilized. - KeyFunc = Key(IDFunc >> KeyShift) - KeyPtr = Key(IDPtr >> KeyShift) - KeyAssert = Key(IDAssert >> KeyShift) - KeyWhile = Key(IDWhile >> KeyShift) - KeyIf = Key(IDIf >> KeyShift) - KeyElse = Key(IDElse >> KeyShift) - KeyReturn = Key(IDReturn >> KeyShift) - KeyBreak = Key(IDBreak >> KeyShift) - KeyContinue = Key(IDContinue >> KeyShift) - KeyStruct = Key(IDStruct >> KeyShift) - KeyUse = Key(IDUse >> KeyShift) - KeyVar = Key(IDVar >> KeyShift) - KeyNptr = Key(IDNptr >> KeyShift) - KeyPre = Key(IDPre >> KeyShift) - KeyInv = Key(IDInv >> KeyShift) - KeyPost = Key(IDPost >> KeyShift) - KeyVia = Key(IDVia >> KeyShift) - KeyPub = Key(IDPub >> KeyShift) - KeyPri = Key(IDPri >> KeyShift) - KeyError = Key(IDError >> KeyShift) - KeyStatus = Key(IDStatus >> KeyShift) - KeyLimit = Key(IDLimit >> KeyShift) + KeyFunc = Key(IDFunc >> KeyShift) + KeyPtr = Key(IDPtr >> KeyShift) + KeyAssert = Key(IDAssert >> KeyShift) + KeyWhile = Key(IDWhile >> KeyShift) + KeyIf = Key(IDIf >> KeyShift) + KeyElse = Key(IDElse >> KeyShift) + KeyReturn = Key(IDReturn >> KeyShift) + KeyBreak = Key(IDBreak >> KeyShift) + KeyContinue = Key(IDContinue >> KeyShift) + KeyStruct = Key(IDStruct >> KeyShift) + KeyUse = Key(IDUse >> KeyShift) + KeyVar = Key(IDVar >> KeyShift) + KeyNptr = Key(IDNptr >> KeyShift) + KeyPre = Key(IDPre >> KeyShift) + KeyInv = Key(IDInv >> KeyShift) + KeyPost = Key(IDPost >> KeyShift) + KeyVia = Key(IDVia >> KeyShift) + KeyPub = Key(IDPub >> KeyShift) + KeyPri = Key(IDPri >> KeyShift) + KeyError = Key(IDError >> KeyShift) + KeyStatus = Key(IDStatus >> KeyShift) + KeyLimit = Key(IDLimit >> KeyShift) + KeyPackageID = Key(IDPackageID >> KeyShift) KeyFalse = Key(IDFalse >> KeyShift) KeyTrue = Key(IDTrue >> KeyShift) @@ -346,28 +347,29 @@ const ( IDAs = ID(0x5B<