shader: Refactoring

This commit is contained in:
Hajime Hoshi 2020-05-09 17:32:10 +09:00
parent 195c51fe51
commit 1b2c29f6f3

View File

@ -100,32 +100,40 @@ func (s *Shader) addError(pos token.Pos, str string) {
s.errs = append(s.errs, fmt.Sprintf("%s: %s", p, str)) s.errs = append(s.errs, fmt.Sprintf("%s: %s", p, str))
} }
func (s *Shader) parse(f *ast.File) { func (sh *Shader) parse(f *ast.File) {
for name, obj := range f.Scope.Objects { for _, d := range f.Decls {
switch name { switch d := d.(type) {
case varyingStructName: case *ast.GenDecl:
s.parseVaryingStruct(obj) switch d.Tok {
default: case token.TYPE:
switch obj.Kind { // TODO: Parse regular structs or other types
case ast.Con: for _, s := range d.Specs {
s.parsePackageLevelConstant(name, obj) s := s.(*ast.TypeSpec)
case ast.Var: if s.Name.Name == varyingStructName {
s.parsePackageLevelVariable(name, obj) sh.parseVaryingStruct(s)
} }
} }
case token.CONST:
for _, s := range d.Specs {
s := s.(*ast.ValueSpec)
sh.parsePackageLevelConstant(s)
}
case token.VAR:
for _, s := range d.Specs {
s := s.(*ast.ValueSpec)
sh.parsePackageLevelVariable(s)
}
}
default:
// TODO: Parse functions
}
} }
} }
func (sh *Shader) parseVaryingStruct(obj *ast.Object) { func (sh *Shader) parseVaryingStruct(t *ast.TypeSpec) {
name := obj.Name s, ok := t.Type.(*ast.StructType)
if obj.Kind != ast.Typ {
sh.addError(obj.Pos(), fmt.Sprintf("%s must be a type but %s", name, obj.Kind))
return
}
t := obj.Decl.(*ast.TypeSpec).Type
s, ok := t.(*ast.StructType)
if !ok { if !ok {
sh.addError(t.Pos(), fmt.Sprintf("%s must be a struct but not", name)) sh.addError(t.Type.Pos(), fmt.Sprintf("%s must be a struct but not", t.Name))
return return
} }
@ -178,17 +186,14 @@ func (sh *Shader) parseVaryingStruct(obj *ast.Object) {
} }
} }
func (s *Shader) parsePackageLevelVariable(name string, obj *ast.Object) { func (s *Shader) parsePackageLevelVariable(vs *ast.ValueSpec) {
v, ok := obj.Decl.(*ast.ValueSpec) t, err := parseType(vs.Type)
if !ok {
s.addError(obj.Pos(), "value spec expected")
return
}
t, err := parseType(v.Type)
if err != nil { if err != nil {
s.addError(v.Type.Pos(), err.Error()) s.addError(vs.Type.Pos(), err.Error())
return return
} }
for _, n := range vs.Names {
name := n.Name
val := variable{ val := variable{
name: name, name: name,
typ: t, typ: t,
@ -199,24 +204,17 @@ func (s *Shader) parsePackageLevelVariable(name string, obj *ast.Object) {
} else { } else {
s.globals = append(s.globals, val) s.globals = append(s.globals, val)
} }
}
} }
func (s *Shader) parsePackageLevelConstant(name string, obj *ast.Object) { func (s *Shader) parsePackageLevelConstant(vs *ast.ValueSpec) {
vs, ok := obj.Decl.(*ast.ValueSpec)
if !ok {
s.addError(obj.Pos(), "value spec expected")
return
}
t, err := parseType(vs.Type) t, err := parseType(vs.Type)
if err != nil { if err != nil {
s.addError(vs.Pos(), err.Error()) s.addError(vs.Type.Pos(), err.Error())
return return
} }
for i, v := range vs.Values { for i, n := range vs.Names {
if vs.Names[i].Name != name { v := vs.Values[i]
continue
}
var init string var init string
switch v := v.(type) { switch v := v.(type) {
case *ast.BasicLit: case *ast.BasicLit:
@ -228,6 +226,7 @@ func (s *Shader) parsePackageLevelConstant(name string, obj *ast.Object) {
default: default:
// TODO: Parse the expression. // TODO: Parse the expression.
} }
name := n.Name
val := variable{ val := variable{
name: name, name: name,
typ: t, // TODO: Treat consts without types typ: t, // TODO: Treat consts without types