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

R=rsc
DELTA=427  (53 added, 302 deleted, 72 changed)
OCL=17857
CL=17868
This commit is contained in:
Rob Pike 2008-10-26 08:28:33 -07:00
parent db25e787fe
commit c4af3e7c2a
3 changed files with 122 additions and 372 deletions

View file

@ -47,28 +47,33 @@ export type Type interface {
Size() uint64; Size() uint64;
} }
// -- Basic // Fields and methods common to all types
type Common struct {
type BasicType struct{
kind int; kind int;
name string; name string;
size uint64; size uint64;
} }
func (c *Common) Name() string {
return c.name
}
func (c *Common) Kind() int {
return c.kind
}
func (c *Common) Size() uint64 {
return c.size
}
// -- Basic
type BasicType struct {
Common
}
func NewBasicType(name string, kind int, size uint64) Type { func NewBasicType(name string, kind int, size uint64) Type {
return &BasicType{kind, name, size} return &BasicType{ Common{kind, name, size} }
}
func (t *BasicType) Name() string {
return t.name
}
func (t *BasicType) Kind() int {
return t.kind
}
func (t *BasicType) Size() uint64 {
return t.size
} }
// Prebuilt basic types // Prebuilt basic types
@ -114,24 +119,12 @@ export type PtrType interface {
} }
type PtrTypeStruct struct { type PtrTypeStruct struct {
name string; Common;
sub *StubType; sub *StubType;
} }
func NewPtrTypeStruct(name string, sub *StubType) *PtrTypeStruct { func NewPtrTypeStruct(name string, sub *StubType) *PtrTypeStruct {
return &PtrTypeStruct{name, sub} return &PtrTypeStruct{ Common{PtrKind, name, ptrsize}, sub}
}
func (t *PtrTypeStruct) Kind() int {
return PtrKind
}
func (t *PtrTypeStruct) Name() string {
return t.name
}
func (t *PtrTypeStruct) Size() uint64 {
return ptrsize
} }
func (t *PtrTypeStruct) Sub() Type { func (t *PtrTypeStruct) Sub() Type {
@ -147,22 +140,14 @@ export type ArrayType interface {
} }
type ArrayTypeStruct struct { type ArrayTypeStruct struct {
name string; Common;
elem *StubType; elem *StubType;
open bool; // otherwise fixed size open bool; // otherwise fixed size
len uint64; len uint64;
} }
func NewArrayTypeStruct(name string, open bool, len uint64, elem *StubType) *ArrayTypeStruct { func NewArrayTypeStruct(name string, open bool, len uint64, elem *StubType) *ArrayTypeStruct {
return &ArrayTypeStruct{name, elem, open, len} return &ArrayTypeStruct{ Common{ArrayKind, name, 0}, elem, open, len}
}
func (t *ArrayTypeStruct) Kind() int {
return ArrayKind
}
func (t *ArrayTypeStruct) Name() string {
return t.name
} }
func (t *ArrayTypeStruct) Size() uint64 { func (t *ArrayTypeStruct) Size() uint64 {
@ -193,21 +178,13 @@ export type MapType interface {
} }
type MapTypeStruct struct { type MapTypeStruct struct {
name string; Common;
key *StubType; key *StubType;
elem *StubType; elem *StubType;
} }
func NewMapTypeStruct(name string, key, elem *StubType) *MapTypeStruct { func NewMapTypeStruct(name string, key, elem *StubType) *MapTypeStruct {
return &MapTypeStruct{name, key, elem} return &MapTypeStruct{ Common{MapKind, name, 0}, key, elem}
}
func (t *MapTypeStruct) Kind() int {
return MapKind
}
func (t *MapTypeStruct) Name() string {
return t.name
} }
func (t *MapTypeStruct) Size() uint64 { func (t *MapTypeStruct) Size() uint64 {
@ -237,21 +214,13 @@ export const ( // channel direction
) )
type ChanTypeStruct struct { type ChanTypeStruct struct {
name string; Common;
elem *StubType; elem *StubType;
dir int; dir int;
} }
func NewChanTypeStruct(name string, dir int, elem *StubType) *ChanTypeStruct { func NewChanTypeStruct(name string, dir int, elem *StubType) *ChanTypeStruct {
return &ChanTypeStruct{name, elem, dir} return &ChanTypeStruct{ Common{ChanKind, name, 0}, elem, dir}
}
func (t *ChanTypeStruct) Kind() int {
return ChanKind
}
func (t *ChanTypeStruct) Name() string {
return t.name
} }
func (t *ChanTypeStruct) Size() uint64 { func (t *ChanTypeStruct) Size() uint64 {
@ -260,7 +229,6 @@ func (t *ChanTypeStruct) Size() uint64 {
} }
func (t *ChanTypeStruct) Dir() int { func (t *ChanTypeStruct) Dir() int {
// -1 is open array? TODO
return t.dir return t.dir
} }
@ -283,24 +251,19 @@ type Field struct {
} }
type StructTypeStruct struct { type StructTypeStruct struct {
name string; Common;
field *[]Field; field *[]Field;
} }
func NewStructTypeStruct(name string, field *[]Field) *StructTypeStruct { func NewStructTypeStruct(name string, field *[]Field) *StructTypeStruct {
return &StructTypeStruct{name, field} return &StructTypeStruct{ Common{StructKind, name, 0}, field}
}
func (t *StructTypeStruct) Kind() int {
return StructKind
}
func (t *StructTypeStruct) Name() string {
return t.name
} }
// TODO: not portable; depends on 6g // TODO: not portable; depends on 6g
func (t *StructTypeStruct) Size() uint64 { func (t *StructTypeStruct) Size() uint64 {
if t.size > 0 {
return t.size
}
size := uint64(0); size := uint64(0);
for i := 0; i < len(t.field); i++ { for i := 0; i < len(t.field); i++ {
elemsize := t.field[i].typ.Get().Size(); elemsize := t.field[i].typ.Get().Size();
@ -316,6 +279,7 @@ func (t *StructTypeStruct) Size() uint64 {
size += elemsize; size += elemsize;
} }
size = (size + 7) & ((1<<64 - 1) & ^7); size = (size + 7) & ((1<<64 - 1) & ^7);
t.size = size;
return size; return size;
} }
@ -338,12 +302,12 @@ export type InterfaceType interface {
} }
type InterfaceTypeStruct struct { type InterfaceTypeStruct struct {
name string; Common;
field *[]Field; field *[]Field;
} }
func NewInterfaceTypeStruct(name string, field *[]Field) *InterfaceTypeStruct { 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) { 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) 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 // -- Func
export type FuncType interface { export type FuncType interface {
@ -374,21 +326,13 @@ export type FuncType interface {
} }
type FuncTypeStruct struct { type FuncTypeStruct struct {
name string; Common;
in *StructTypeStruct; in *StructTypeStruct;
out *StructTypeStruct; out *StructTypeStruct;
} }
func NewFuncTypeStruct(name string, in, out *StructTypeStruct) *FuncTypeStruct { func NewFuncTypeStruct(name string, in, out *StructTypeStruct) *FuncTypeStruct {
return &FuncTypeStruct{name, in, out} return &FuncTypeStruct{ Common{FuncKind, name, 0}, in, out }
}
func (t *FuncTypeStruct) Kind() int {
return FuncKind
}
func (t *FuncTypeStruct) Name() string {
return t.name
} }
func (t *FuncTypeStruct) Size() uint64 { func (t *FuncTypeStruct) Size() uint64 {

View file

@ -19,6 +19,22 @@ export type Value interface {
Type() Type; Type() Type;
} }
// Common fields and functionality for all values
type CommonV struct { // BUG: want to call this Common but 6g does not hide the name
kind int;
typ Type;
addr Addr;
}
func (c *CommonV) Kind() int {
return c.kind
}
func (c *CommonV) Type() Type {
return c.typ
}
func NewValueAddr(typ Type, addr Addr) Value func NewValueAddr(typ Type, addr Addr) Value
type Creator *(typ Type, addr Addr) Value type Creator *(typ Type, addr Addr) Value
@ -50,19 +66,11 @@ export type Int8Value interface {
} }
type Int8ValueStruct struct { type Int8ValueStruct struct {
addr Addr CommonV
} }
func Int8Creator(typ Type, addr Addr) Value { func Int8Creator(typ Type, addr Addr) Value {
return &Int8ValueStruct{addr} return &Int8ValueStruct{ CommonV{Int8Kind, typ, addr} }
}
func (v *Int8ValueStruct) Kind() int {
return Int8Kind
}
func (v *Int8ValueStruct) Type() Type {
return Int8
} }
func (v *Int8ValueStruct) Get() int8 { func (v *Int8ValueStruct) Get() int8 {
@ -83,19 +91,11 @@ export type Int16Value interface {
} }
type Int16ValueStruct struct { type Int16ValueStruct struct {
addr Addr CommonV
} }
func Int16Creator(typ Type, addr Addr) Value { func Int16Creator(typ Type, addr Addr) Value {
return &Int16ValueStruct{addr} return &Int16ValueStruct{ CommonV{Int16Kind, typ, addr} }
}
func (v *Int16ValueStruct) Kind() int {
return Int16Kind
}
func (v *Int16ValueStruct) Type() Type {
return Int16
} }
func (v *Int16ValueStruct) Get() int16 { func (v *Int16ValueStruct) Get() int16 {
@ -116,19 +116,11 @@ export type Int32Value interface {
} }
type Int32ValueStruct struct { type Int32ValueStruct struct {
addr Addr CommonV
} }
func Int32Creator(typ Type, addr Addr) Value { func Int32Creator(typ Type, addr Addr) Value {
return &Int32ValueStruct{addr} return &Int32ValueStruct{ CommonV{Int32Kind, typ, addr} }
}
func (v *Int32ValueStruct) Type() Type {
return Int32
}
func (v *Int32ValueStruct) Kind() int {
return Int32Kind
} }
func (v *Int32ValueStruct) Get() int32 { func (v *Int32ValueStruct) Get() int32 {
@ -149,19 +141,11 @@ export type Int64Value interface {
} }
type Int64ValueStruct struct { type Int64ValueStruct struct {
addr Addr CommonV
} }
func Int64Creator(typ Type, addr Addr) Value { func Int64Creator(typ Type, addr Addr) Value {
return &Int64ValueStruct{addr} return &Int64ValueStruct{ CommonV{Int64Kind, typ, addr} }
}
func (v *Int64ValueStruct) Kind() int {
return Int64Kind
}
func (v *Int64ValueStruct) Type() Type {
return Int64
} }
func (v *Int64ValueStruct) Get() int64 { func (v *Int64ValueStruct) Get() int64 {
@ -182,19 +166,11 @@ export type Uint8Value interface {
} }
type Uint8ValueStruct struct { type Uint8ValueStruct struct {
addr Addr CommonV
} }
func Uint8Creator(typ Type, addr Addr) Value { func Uint8Creator(typ Type, addr Addr) Value {
return &Uint8ValueStruct{addr} return &Uint8ValueStruct{ CommonV{Uint8Kind, typ, addr} }
}
func (v *Uint8ValueStruct) Kind() int {
return Uint8Kind
}
func (v *Uint8ValueStruct) Type() Type {
return Uint8
} }
func (v *Uint8ValueStruct) Get() uint8 { func (v *Uint8ValueStruct) Get() uint8 {
@ -215,19 +191,11 @@ export type Uint16Value interface {
} }
type Uint16ValueStruct struct { type Uint16ValueStruct struct {
addr Addr CommonV
} }
func Uint16Creator(typ Type, addr Addr) Value { func Uint16Creator(typ Type, addr Addr) Value {
return &Uint16ValueStruct{addr} return &Uint16ValueStruct{ CommonV{Uint16Kind, typ, addr} }
}
func (v *Uint16ValueStruct) Kind() int {
return Uint16Kind
}
func (v *Uint16ValueStruct) Type() Type {
return Uint16
} }
func (v *Uint16ValueStruct) Get() uint16 { func (v *Uint16ValueStruct) Get() uint16 {
@ -248,19 +216,11 @@ export type Uint32Value interface {
} }
type Uint32ValueStruct struct { type Uint32ValueStruct struct {
addr Addr CommonV
} }
func Uint32Creator(typ Type, addr Addr) Value { func Uint32Creator(typ Type, addr Addr) Value {
return &Uint32ValueStruct{addr} return &Uint32ValueStruct{ CommonV{Uint32Kind, typ, addr} }
}
func (v *Uint32ValueStruct) Kind() int {
return Uint32Kind
}
func (v *Uint32ValueStruct) Type() Type {
return Uint32
} }
func (v *Uint32ValueStruct) Get() uint32 { func (v *Uint32ValueStruct) Get() uint32 {
@ -281,19 +241,11 @@ export type Uint64Value interface {
} }
type Uint64ValueStruct struct { type Uint64ValueStruct struct {
addr Addr CommonV
} }
func Uint64Creator(typ Type, addr Addr) Value { func Uint64Creator(typ Type, addr Addr) Value {
return &Uint64ValueStruct{addr} return &Uint64ValueStruct{ CommonV{Uint64Kind, typ, addr} }
}
func (v *Uint64ValueStruct) Kind() int {
return Uint64Kind
}
func (v *Uint64ValueStruct) Type() Type {
return Uint64
} }
func (v *Uint64ValueStruct) Get() uint64 { func (v *Uint64ValueStruct) Get() uint64 {
@ -314,19 +266,11 @@ export type Float32Value interface {
} }
type Float32ValueStruct struct { type Float32ValueStruct struct {
addr Addr CommonV
} }
func Float32Creator(typ Type, addr Addr) Value { func Float32Creator(typ Type, addr Addr) Value {
return &Float32ValueStruct{addr} return &Float32ValueStruct{ CommonV{Float32Kind, typ, addr} }
}
func (v *Float32ValueStruct) Kind() int {
return Float32Kind
}
func (v *Float32ValueStruct) Type() Type {
return Float32
} }
func (v *Float32ValueStruct) Get() float32 { func (v *Float32ValueStruct) Get() float32 {
@ -347,19 +291,11 @@ export type Float64Value interface {
} }
type Float64ValueStruct struct { type Float64ValueStruct struct {
addr Addr CommonV
} }
func Float64Creator(typ Type, addr Addr) Value { func Float64Creator(typ Type, addr Addr) Value {
return &Float64ValueStruct{addr} return &Float64ValueStruct{ CommonV{Float64Kind, typ, addr} }
}
func (v *Float64ValueStruct) Kind() int {
return Float64Kind
}
func (v *Float64ValueStruct) Type() Type {
return Float64
} }
func (v *Float64ValueStruct) Get() float64 { func (v *Float64ValueStruct) Get() float64 {
@ -380,19 +316,11 @@ export type Float80Value interface {
} }
type Float80ValueStruct struct { type Float80ValueStruct struct {
addr Addr CommonV
} }
func Float80Creator(typ Type, addr Addr) Value { func Float80Creator(typ Type, addr Addr) Value {
return &Float80ValueStruct{addr} return &Float80ValueStruct{ CommonV{Float80Kind, typ, addr} }
}
func (v *Float80ValueStruct) Kind() int {
return Float80Kind
}
func (v *Float80ValueStruct) Type() Type {
return Float80
} }
/* /*
@ -417,19 +345,11 @@ export type StringValue interface {
} }
type StringValueStruct struct { type StringValueStruct struct {
addr Addr CommonV
} }
func StringCreator(typ Type, addr Addr) Value { func StringCreator(typ Type, addr Addr) Value {
return &StringValueStruct{addr} return &StringValueStruct{ CommonV{StringKind, typ, addr} }
}
func (v *StringValueStruct) Kind() int {
return StringKind
}
func (v *StringValueStruct) Type() Type {
return String
} }
func (v *StringValueStruct) Get() string { func (v *StringValueStruct) Get() string {
@ -450,16 +370,7 @@ export type PtrValue interface {
} }
type PtrValueStruct struct { type PtrValueStruct struct {
addr Addr; CommonV
typ Type;
}
func (v *PtrValueStruct) Kind() int {
return PtrKind
}
func (v *PtrValueStruct) Type() Type {
return v.typ
} }
func (v *PtrValueStruct) Get() Addr { func (v *PtrValueStruct) Get() Addr {
@ -471,10 +382,10 @@ func (v *PtrValueStruct) Sub() Value {
} }
func PtrCreator(typ Type, addr Addr) Value { func PtrCreator(typ Type, addr Addr) Value {
return &PtrValueStruct{addr, typ}; return &PtrValueStruct{ CommonV{PtrKind, typ, addr} };
} }
// -- Array TODO: finish and test // -- Array
export type ArrayValue interface { export type ArrayValue interface {
Kind() int; Kind() int;
@ -485,11 +396,11 @@ export type ArrayValue interface {
} }
type OpenArrayValueStruct struct { type OpenArrayValueStruct struct {
addr Addr; CommonV;
typ Type;
elemtype Type; elemtype Type;
elemsize uint64; elemsize uint64;
} }
/* /*
Run-time representation of open arrays looks like this: Run-time representation of open arrays looks like this:
struct Array { struct Array {
@ -498,14 +409,6 @@ type OpenArrayValueStruct struct {
}; };
*/ */
func (v *OpenArrayValueStruct) Kind() int {
return ArrayKind
}
func (v *OpenArrayValueStruct) Type() Type {
return v.typ
}
func (v *OpenArrayValueStruct) Open() bool { func (v *OpenArrayValueStruct) Open() bool {
return true return true
} }
@ -520,21 +423,12 @@ func (v *OpenArrayValueStruct) Elem(i uint64) Value {
} }
type FixedArrayValueStruct struct { type FixedArrayValueStruct struct {
addr Addr; CommonV;
typ Type;
elemtype Type; elemtype Type;
elemsize uint64; elemsize uint64;
len uint64; len uint64;
} }
func (v *FixedArrayValueStruct) Kind() int {
return ArrayKind
}
func (v *FixedArrayValueStruct) Type() Type {
return v.typ
}
func (v *FixedArrayValueStruct) Open() bool { func (v *FixedArrayValueStruct) Open() bool {
return false return false
} }
@ -552,6 +446,7 @@ func ArrayCreator(typ Type, addr Addr) Value {
arraytype := typ.(ArrayType); arraytype := typ.(ArrayType);
if arraytype.Open() { if arraytype.Open() {
v := new(OpenArrayValueStruct); v := new(OpenArrayValueStruct);
v.kind = ArrayKind;
v.addr = addr; v.addr = addr;
v.typ = typ; v.typ = typ;
v.elemtype = arraytype.Elem(); v.elemtype = arraytype.Elem();
@ -559,6 +454,7 @@ func ArrayCreator(typ Type, addr Addr) Value {
return v; return v;
} }
v := new(FixedArrayValueStruct); v := new(FixedArrayValueStruct);
v.kind = ArrayKind;
v.addr = addr; v.addr = addr;
v.typ = typ; v.typ = typ;
v.elemtype = arraytype.Elem(); v.elemtype = arraytype.Elem();
@ -577,20 +473,11 @@ export type MapValue interface {
} }
type MapValueStruct struct { type MapValueStruct struct {
addr Addr; CommonV
typ Type;
} }
func MapCreator(typ Type, addr Addr) Value { func MapCreator(typ Type, addr Addr) Value {
return &MapValueStruct{addr, typ} return &MapValueStruct{ CommonV{MapKind, typ, addr} }
}
func (v *MapValueStruct) Kind() int {
return MapKind
}
func (v *MapValueStruct) Type() Type {
return v.typ
} }
func (v *MapValueStruct) Len() int { func (v *MapValueStruct) Len() int {
@ -610,20 +497,11 @@ export type ChanValue interface {
} }
type ChanValueStruct struct { type ChanValueStruct struct {
addr Addr; CommonV
typ Type;
} }
func ChanCreator(typ Type, addr Addr) Value { func ChanCreator(typ Type, addr Addr) Value {
return &ChanValueStruct{addr, typ} return &ChanValueStruct{ CommonV{ChanKind, typ, addr} }
}
func (v *ChanValueStruct) Kind() int {
return ChanKind
}
func (v *ChanValueStruct) Type() Type {
return v.typ
} }
// -- Struct // -- Struct
@ -636,19 +514,10 @@ export type StructValue interface {
} }
type StructValueStruct struct { type StructValueStruct struct {
addr Addr; CommonV;
typ Type;
field *[]Value; field *[]Value;
} }
func (v *StructValueStruct) Kind() int {
return StructKind
}
func (v *StructValueStruct) Type() Type {
return v.typ
}
func (v *StructValueStruct) Len() int { func (v *StructValueStruct) Len() int {
return len(v.field) return len(v.field)
} }
@ -659,10 +528,8 @@ func (v *StructValueStruct) Field(i int) Value {
func StructCreator(typ Type, addr Addr) Value { func StructCreator(typ Type, addr Addr) Value {
t := typ.(StructType); t := typ.(StructType);
v := new(StructValueStruct);
v.addr = addr;
nfield := t.Len(); nfield := t.Len();
v.field = new([]Value, nfield); v := &StructValueStruct{ CommonV{StructKind, typ, addr}, new([]Value, nfield) };
for i := 0; i < nfield; i++ { for i := 0; i < nfield; i++ {
name, ftype, offset := t.Field(i); name, ftype, offset := t.Field(i);
v.field[i] = NewValueAddr(ftype, addr + offset); v.field[i] = NewValueAddr(ftype, addr + offset);
@ -679,20 +546,11 @@ export type InterfaceValue interface {
} }
type InterfaceValueStruct struct { type InterfaceValueStruct struct {
addr Addr; CommonV
typ Type;
} }
func InterfaceCreator(typ Type, addr Addr) Value { func InterfaceCreator(typ Type, addr Addr) Value {
return &InterfaceValueStruct{addr, typ} return &InterfaceValueStruct{ CommonV{InterfaceKind, typ, addr} }
}
func (v *InterfaceValueStruct) Kind() int {
return InterfaceKind
}
func (v *InterfaceValueStruct) Type() Type {
return v.typ
} }
// -- Func // -- Func
@ -703,20 +561,11 @@ export type FuncValue interface {
} }
type FuncValueStruct struct { type FuncValueStruct struct {
addr Addr; CommonV
typ Type;
} }
func FuncCreator(typ Type, addr Addr) Value { func FuncCreator(typ Type, addr Addr) Value {
return &FuncValueStruct{addr, typ} return &FuncValueStruct{ CommonV{FuncKind, typ, addr} }
}
func (v *FuncValueStruct) Kind() int {
return FuncKind
}
func (v *FuncValueStruct) Type() Type {
return v.typ
} }
var creator *map[int] Creator var creator *map[int] Creator

View file

@ -35,6 +35,17 @@ type Inst interface {
Print(); 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 { type RE struct {
expr string; // the original expression expr string; // the original expression
ch *chan<- *RE; // reply channel when we're done ch *chan<- *RE; // reply channel when we're done
@ -61,68 +72,43 @@ const (
// --- START start of program // --- START start of program
type Start struct { type Start struct {
next Inst; Common
index int;
} }
func (start *Start) Type() int { return START } 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") } func (start *Start) Print() { print("start") }
// --- END end of program // --- END end of program
type End struct { type End struct {
next Inst; Common
index int;
} }
func (end *End) Type() int { return END } 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") } func (end *End) Print() { print("end") }
// --- BOT beginning of text // --- BOT beginning of text
type Bot struct { type Bot struct {
next Inst; Common
index int;
} }
func (bot *Bot) Type() int { return BOT } 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") } func (bot *Bot) Print() { print("bot") }
// --- EOT end of text // --- EOT end of text
type Eot struct { type Eot struct {
next Inst; Common
index int;
} }
func (eot *Eot) Type() int { return EOT } 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") } func (eot *Eot) Print() { print("eot") }
// --- CHAR a regular character // --- CHAR a regular character
type Char struct { type Char struct {
next Inst; Common;
index int;
char int; char int;
} }
func (char *Char) Type() int { return CHAR } 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 (char *Char) Print() { print("char ", string(char.char)) }
func NewChar(char int) *Char { func NewChar(char int) *Char {
@ -134,8 +120,7 @@ func NewChar(char int) *Char {
// --- CHARCLASS [a-z] // --- CHARCLASS [a-z]
type CharClass struct { type CharClass struct {
next Inst; Common;
index int;
char int; char int;
negate bool; // is character class negated? ([^a-z]) negate bool; // is character class negated? ([^a-z])
// Vector of int, stored pairwise: [a-z] is (a,z); x is (x,x): // 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) 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() { func (cclass *CharClass) Print() {
print("charclass"); print("charclass");
if cclass.negate { if cclass.negate {
@ -188,70 +170,45 @@ func NewCharClass() *CharClass {
// --- ANY any character // --- ANY any character
type Any struct { type Any struct {
next Inst; Common
index int;
} }
func (any *Any) Type() int { return ANY } 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") } func (any *Any) Print() { print("any") }
// --- BRA parenthesized expression // --- BRA parenthesized expression
type Bra struct { type Bra struct {
next Inst; Common;
index int;
n int; // subexpression number n int; // subexpression number
} }
func (bra *Bra) Type() int { return BRA } func (bra *Bra) Type() int { return BRA }
func (bra *Bra) Next() Inst { return bra.next } func (bra *Bra) Print() { print("bra", bra.n); }
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"); }
// --- EBRA end of parenthesized expression // --- EBRA end of parenthesized expression
type Ebra struct { type Ebra struct {
next Inst; Common;
index int;
n int; // subexpression number n int; // subexpression number
} }
func (ebra *Ebra) Type() int { return EBRA } 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); } func (ebra *Ebra) Print() { print("ebra ", ebra.n); }
// --- ALT alternation // --- ALT alternation
type Alt struct { type Alt struct {
next Inst; Common;
index int;
left Inst; // other branch left Inst; // other branch
} }
func (alt *Alt) Type() int { return ALT } 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(), ")"); } func (alt *Alt) Print() { print("alt(", alt.left.Index(), ")"); }
// --- NOP no operation // --- NOP no operation
type Nop struct { type Nop struct {
next Inst; Common
index int;
} }
func (nop *Nop) Type() int { return NOP } 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") } func (nop *Nop) Print() { print("nop") }
// report error and exit compiling/executing goroutine // report error and exit compiling/executing goroutine
@ -312,7 +269,7 @@ Grammar:
'$' '$'
'.' '.'
character character
'[' character-ranges ']' '[' [ '^' ] character-ranges ']'
'(' regexp ')' '(' regexp ')'
*/ */