diff options
author | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-04-30 02:40:55 -0400 |
---|---|---|
committer | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-04-30 02:40:55 -0400 |
commit | 05d8ec6a16acf88c5ae7521d86131f5ea7f9b4e4 (patch) | |
tree | 028bfcc345c02afad9a6201e2af63ed638a3e8d9 /nexgb/xgbgen/field.go | |
parent | 3115c13e88badfd3b6b1762f2239edbf9d0b8951 (diff) | |
download | haven-05d8ec6a16acf88c5ae7521d86131f5ea7f9b4e4.tar.gz haven-05d8ec6a16acf88c5ae7521d86131f5ea7f9b4e4.tar.xz haven-05d8ec6a16acf88c5ae7521d86131f5ea7f9b4e4.zip |
complete and total overhaul like i promised. things are much easier to reason about. still not working yet though.
Diffstat (limited to 'nexgb/xgbgen/field.go')
-rw-r--r-- | nexgb/xgbgen/field.go | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/nexgb/xgbgen/field.go b/nexgb/xgbgen/field.go new file mode 100644 index 0000000..a659e6e --- /dev/null +++ b/nexgb/xgbgen/field.go @@ -0,0 +1,170 @@ +package main + +type Field interface { + Initialize(p *Protocol) + SrcName() string + XmlName() string + Size() Size + + Define(c *Context) + Read(c *Context) +} + +func (pad *PadField) Initialize(p *Protocol) {} + +type PadField struct { + Bytes uint +} + +func (p *PadField) SrcName() string { + panic("illegal to take source name of a pad field") +} + +func (p *PadField) XmlName() string { + panic("illegal to take XML name of a pad field") +} + +func (p *PadField) Size() Size { + return newFixedSize(p.Bytes) +} + +type SingleField struct { + srcName string + xmlName string + Type Type +} + +func (f *SingleField) Initialize(p *Protocol) { + f.srcName = SrcName(f.XmlName()) + f.Type = f.Type.(*Translation).RealType(p) +} + +func (f *SingleField) SrcName() string { + return f.srcName +} + +func (f *SingleField) XmlName() string { + return f.xmlName +} + +func (f *SingleField) Size() Size { + return f.Type.Size() +} + +type ListField struct { + srcName string + xmlName string + Type Type + LengthExpr Expression +} + +func (f *ListField) SrcName() string { + return f.srcName +} + +func (f *ListField) XmlName() string { + return f.xmlName +} + +func (f *ListField) Size() Size { + return newExpressionSize(f.LengthExpr).Multiply(f.Type.Size()) +} + +func (f *ListField) Initialize(p *Protocol) { + f.srcName = SrcName(f.XmlName()) + f.Type = f.Type.(*Translation).RealType(p) + if f.LengthExpr != nil { + f.LengthExpr.Initialize(p) + } +} + +type LocalField struct { + *SingleField +} + +type ExprField struct { + srcName string + xmlName string + Type Type + Expr Expression +} + +func (f *ExprField) SrcName() string { + return f.srcName +} + +func (f *ExprField) XmlName() string { + return f.xmlName +} + +func (f *ExprField) Size() Size { + return f.Type.Size() +} + +func (f *ExprField) Initialize(p *Protocol) { + f.srcName = SrcName(f.XmlName()) + f.Type = f.Type.(*Translation).RealType(p) + f.Expr.Initialize(p) +} + +type ValueField struct { + MaskType Type + MaskName string + ListName string +} + +func (f *ValueField) SrcName() string { + panic("it is illegal to call SrcName on a ValueField field") +} + +func (f *ValueField) XmlName() string { + panic("it is illegal to call XmlName on a ValueField field") +} + +func (f *ValueField) Size() Size { + return f.MaskType.Size() +} + +func (f *ValueField) Initialize(p *Protocol) { + f.MaskType = f.MaskType.(*Translation).RealType(p) + f.MaskName = SrcName(f.MaskName) + f.ListName = SrcName(f.ListName) +} + +type SwitchField struct { + Name string + Expr Expression + Bitcases []*Bitcase +} + +func (f *SwitchField) SrcName() string { + panic("it is illegal to call SrcName on a SwitchField field") +} + +func (f *SwitchField) XmlName() string { + panic("it is illegal to call XmlName on a SwitchField field") +} + +// XXX: This is a bit tricky. The size has to be represented as a non-concrete +// expression that finds *which* bitcase fields are included, and sums the +// sizes of those fields. +func (f *SwitchField) Size() Size { + return newFixedSize(0) +} + +func (f *SwitchField) Initialize(p *Protocol) { + f.Name = SrcName(f.Name) + f.Expr.Initialize(p) + for _, bitcase := range f.Bitcases { + bitcase.Expr.Initialize(p) + for _, field := range bitcase.Fields { + field.Initialize(p) + } + } +} + +type Bitcase struct { + Fields []Field + Expr Expression +} + |