Commit c4af3e7c authored by Rob Pike's avatar Rob Pike

use embedded types to save boilerplate - almost 300 lines' worth

R=rsc
DELTA=427  (53 added, 302 deleted, 72 changed)
OCL=17857
CL=17868
parent db25e787
......@@ -47,28 +47,33 @@ export type Type interface {
Size() uint64;
}
// -- Basic
type BasicType struct{
// Fields and methods common to all types
type Common struct {
kind int;
name string;
size uint64;
}
func NewBasicType(name string, kind int, size uint64) Type {
return &BasicType{kind, name, size}
func (c *Common) Name() string {
return c.name
}
func (t *BasicType) Name() string {
return t.name
func (c *Common) Kind() int {
return c.kind
}
func (t *BasicType) Kind() int {
return t.kind
func (c *Common) Size() uint64 {
return c.size
}
// -- Basic
type BasicType struct {
Common
}
func (t *BasicType) Size() uint64 {
return t.size
func NewBasicType(name string, kind int, size uint64) Type {
return &BasicType{ Common{kind, name, size} }
}
// Prebuilt basic types
......@@ -114,24 +119,12 @@ export type PtrType interface {
}
type PtrTypeStruct struct {
name string;
Common;
sub *StubType;
}
func NewPtrTypeStruct(name string, sub *StubType) *PtrTypeStruct {
return &PtrTypeStruct{name, sub}
}
func (t *PtrTypeStruct) Kind() int {
return PtrKind
}
func (t *PtrTypeStruct) Name() string {
return t.name
}
func (t *PtrTypeStruct) Size() uint64 {
return ptrsize
return &PtrTypeStruct{ Common{PtrKind, name, ptrsize}, sub}
}
func (t *PtrTypeStruct) Sub() Type {
......@@ -147,22 +140,14 @@ export type ArrayType interface {
}
type ArrayTypeStruct struct {
name string;
Common;
elem *StubType;
open bool; // otherwise fixed size
len uint64;
}
func NewArrayTypeStruct(name string, open bool, len uint64, elem *StubType) *ArrayTypeStruct {
return &ArrayTypeStruct{name, elem, open, len}
}
func (t *ArrayTypeStruct) Kind() int {
return ArrayKind
}
func (t *ArrayTypeStruct) Name() string {
return t.name
return &ArrayTypeStruct{ Common{ArrayKind, name, 0}, elem, open, len}
}
func (t *ArrayTypeStruct) Size() uint64 {
......@@ -193,21 +178,13 @@ export type MapType interface {
}
type MapTypeStruct struct {
name string;
Common;
key *StubType;
elem *StubType;
}
func NewMapTypeStruct(name string, key, elem *StubType) *MapTypeStruct {
return &MapTypeStruct{name, key, elem}
}
func (t *MapTypeStruct) Kind() int {
return MapKind
}
func (t *MapTypeStruct) Name() string {
return t.name
return &MapTypeStruct{ Common{MapKind, name, 0}, key, elem}
}
func (t *MapTypeStruct) Size() uint64 {
......@@ -237,21 +214,13 @@ export const ( // channel direction
)
type ChanTypeStruct struct {
name string;
Common;
elem *StubType;
dir int;
}
func NewChanTypeStruct(name string, dir int, elem *StubType) *ChanTypeStruct {
return &ChanTypeStruct{name, elem, dir}
}
func (t *ChanTypeStruct) Kind() int {
return ChanKind
}
func (t *ChanTypeStruct) Name() string {
return t.name
return &ChanTypeStruct{ Common{ChanKind, name, 0}, elem, dir}
}
func (t *ChanTypeStruct) Size() uint64 {
......@@ -260,7 +229,6 @@ func (t *ChanTypeStruct) Size() uint64 {
}
func (t *ChanTypeStruct) Dir() int {
// -1 is open array? TODO
return t.dir
}
......@@ -283,24 +251,19 @@ type Field struct {
}
type StructTypeStruct struct {
name string;
Common;
field *[]Field;
}
func NewStructTypeStruct(name string, field *[]Field) *StructTypeStruct {
return &StructTypeStruct{name, field}
}
func (t *StructTypeStruct) Kind() int {
return StructKind
}
func (t *StructTypeStruct) Name() string {
return t.name
return &StructTypeStruct{ Common{StructKind, name, 0}, field}
}
// TODO: not portable; depends on 6g
func (t *StructTypeStruct) Size() uint64 {
if t.size > 0 {
return t.size
}
size := uint64(0);
for i := 0; i < len(t.field); i++ {
elemsize := t.field[i].typ.Get().Size();
......@@ -316,6 +279,7 @@ func (t *StructTypeStruct) Size() uint64 {
size += elemsize;
}
size = (size + 7) & ((1<<64 - 1) & ^7);
t.size = size;
return size;
}
......@@ -338,12 +302,12 @@ export type InterfaceType interface {
}
type InterfaceTypeStruct struct {
name string;
Common;
field *[]Field;
}
func NewInterfaceTypeStruct(name string, field *[]Field) *InterfaceTypeStruct {
return &InterfaceTypeStruct{name, field}
return &InterfaceTypeStruct{ Common{InterfaceKind, name, interfacesize}, field }
}
func (t *InterfaceTypeStruct) Field(i int) (name string, typ Type, offset uint64) {
......@@ -354,18 +318,6 @@ func (t *InterfaceTypeStruct) Len() int {
return len(t.field)
}
func (t *InterfaceTypeStruct) Kind() int {
return InterfaceKind
}
func (t *InterfaceTypeStruct) Name() string {
return t.name
}
func (t *InterfaceTypeStruct) Size() uint64 {
return interfacesize
}
// -- Func
export type FuncType interface {
......@@ -374,21 +326,13 @@ export type FuncType interface {
}
type FuncTypeStruct struct {
name string;
Common;
in *StructTypeStruct;
out *StructTypeStruct;
}
func NewFuncTypeStruct(name string, in, out *StructTypeStruct) *FuncTypeStruct {
return &FuncTypeStruct{name, in, out}
}
func (t *FuncTypeStruct) Kind() int {
return FuncKind
}
func (t *FuncTypeStruct) Name() string {
return t.name
return &FuncTypeStruct{ Common{FuncKind, name, 0}, in, out }
}
func (t *FuncTypeStruct) Size() uint64 {
......
This diff is collapsed.
......@@ -35,6 +35,17 @@ type Inst interface {
Print();
}
// Fields and methods common to all instructions
type Common struct {
next Inst;
index int;
}
func (c *Common) Next() Inst { return c.next }
func (c *Common) SetNext(i Inst) { c.next = i }
func (c *Common) Index() int { return c.index }
func (c *Common) SetIndex(i int) { c.index = i }
type RE struct {
expr string; // the original expression
ch *chan<- *RE; // reply channel when we're done
......@@ -61,68 +72,43 @@ const (
// --- START start of program
type Start struct {
next Inst;
index int;
Common
}
func (start *Start) Type() int { return START }
func (start *Start) Next() Inst { return start.next }
func (start *Start) SetNext(i Inst) { start.next = i }
func (start *Start) Index() int { return start.index }
func (start *Start) SetIndex(i int) { start.index = i }
func (start *Start) Print() { print("start") }
// --- END end of program
type End struct {
next Inst;
index int;
Common
}
func (end *End) Type() int { return END }
func (end *End) Next() Inst { return end.next }
func (end *End) SetNext(i Inst) { end.next = i }
func (end *End) Index() int { return end.index }
func (end *End) SetIndex(i int) { end.index = i }
func (end *End) Print() { print("end") }
// --- BOT beginning of text
type Bot struct {
next Inst;
index int;
Common
}
func (bot *Bot) Type() int { return BOT }
func (bot *Bot) Next() Inst { return bot.next }
func (bot *Bot) SetNext(i Inst) { bot.next = i }
func (bot *Bot) Index() int { return bot.index }
func (bot *Bot) SetIndex(i int) { bot.index = i }
func (bot *Bot) Print() { print("bot") }
// --- EOT end of text
type Eot struct {
next Inst;
index int;
Common
}
func (eot *Eot) Type() int { return EOT }
func (eot *Eot) Next() Inst { return eot.next }
func (eot *Eot) SetNext(i Inst) { eot.next = i }
func (eot *Eot) Index() int { return eot.index }
func (eot *Eot) SetIndex(i int) { eot.index = i }
func (eot *Eot) Print() { print("eot") }
// --- CHAR a regular character
type Char struct {
next Inst;
index int;
Common;
char int;
}
func (char *Char) Type() int { return CHAR }
func (char *Char) Next() Inst { return char.next }
func (char *Char) SetNext(i Inst) { char.next = i }
func (char *Char) Index() int { return char.index }
func (char *Char) SetIndex(i int) { char.index = i }
func (char *Char) Print() { print("char ", string(char.char)) }
func NewChar(char int) *Char {
......@@ -134,8 +120,7 @@ func NewChar(char int) *Char {
// --- CHARCLASS [a-z]
type CharClass struct {
next Inst;
index int;
Common;
char int;
negate bool; // is character class negated? ([^a-z])
// Vector of int, stored pairwise: [a-z] is (a,z); x is (x,x):
......@@ -143,10 +128,7 @@ type CharClass struct {
}
func (cclass *CharClass) Type() int { return CHARCLASS }
func (cclass *CharClass) Next() Inst { return cclass.next }
func (cclass *CharClass) SetNext(i Inst) { cclass.next = i }
func (cclass *CharClass) Index() int { return cclass.index }
func (cclass *CharClass) SetIndex(i int) { cclass.index = i }
func (cclass *CharClass) Print() {
print("charclass");
if cclass.negate {
......@@ -188,70 +170,45 @@ func NewCharClass() *CharClass {
// --- ANY any character
type Any struct {
next Inst;
index int;
Common
}
func (any *Any) Type() int { return ANY }
func (any *Any) Next() Inst { return any.next }
func (any *Any) SetNext(i Inst) { any.next = i }
func (any *Any) Index() int { return any.index }
func (any *Any) SetIndex(i int) { any.index = i }
func (any *Any) Print() { print("any") }
// --- BRA parenthesized expression
type Bra struct {
next Inst;
index int;
Common;
n int; // subexpression number
}
func (bra *Bra) Type() int { return BRA }
func (bra *Bra) Next() Inst { return bra.next }
func (bra *Bra) SetNext(i Inst) { bra.next = i }
func (bra *Bra) Index() int { return bra.index }
func (bra *Bra) SetIndex(i int) { bra.index = i }
func (bra *Bra) Print() { print("bra"); }
func (bra *Bra) Print() { print("bra", bra.n); }
// --- EBRA end of parenthesized expression
type Ebra struct {
next Inst;
index int;
Common;
n int; // subexpression number
}
func (ebra *Ebra) Type() int { return EBRA }
func (ebra *Ebra) Next() Inst { return ebra.next }
func (ebra *Ebra) SetNext(i Inst) { ebra.next = i }
func (ebra *Ebra) Index() int { return ebra.index }
func (ebra *Ebra) SetIndex(i int) { ebra.index = i }
func (ebra *Ebra) Print() { print("ebra ", ebra.n); }
// --- ALT alternation
type Alt struct {
next Inst;
index int;
Common;
left Inst; // other branch
}
func (alt *Alt) Type() int { return ALT }
func (alt *Alt) Next() Inst { return alt.next }
func (alt *Alt) SetNext(i Inst) { alt.next = i }
func (alt *Alt) Index() int { return alt.index }
func (alt *Alt) SetIndex(i int) { alt.index = i }
func (alt *Alt) Print() { print("alt(", alt.left.Index(), ")"); }
// --- NOP no operation
type Nop struct {
next Inst;
index int;
Common
}
func (nop *Nop) Type() int { return NOP }
func (nop *Nop) Next() Inst { return nop.next }
func (nop *Nop) SetNext(i Inst) { nop.next = i }
func (nop *Nop) Index() int { return nop.index }
func (nop *Nop) SetIndex(i int) { nop.index = i }
func (nop *Nop) Print() { print("nop") }
// report error and exit compiling/executing goroutine
......@@ -312,7 +269,7 @@ Grammar:
'$'
'.'
character
'[' character-ranges ']'
'[' [ '^' ] character-ranges ']'
'(' regexp ')'
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment