2020-05-09 16:38:52 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package shader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-09 19:01:28 +02:00
|
|
|
"go/ast"
|
2020-05-09 16:38:52 +02:00
|
|
|
"go/token"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type block struct {
|
2020-05-10 17:13:08 +02:00
|
|
|
types []typ
|
2020-05-09 17:59:18 +02:00
|
|
|
vars []variable
|
|
|
|
consts []constant
|
|
|
|
funcs []function
|
|
|
|
stmts []stmt
|
|
|
|
pos token.Pos
|
2020-05-10 12:32:40 +02:00
|
|
|
outer *block
|
2020-05-09 16:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *block) dump(indent int) []string {
|
|
|
|
idt := strings.Repeat("\t", indent)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
|
2020-05-10 17:13:08 +02:00
|
|
|
for _, t := range b.types {
|
|
|
|
ls := t.dump(indent)
|
|
|
|
ls[0] = fmt.Sprintf("type %s %s", t.name, ls[0])
|
|
|
|
lines = append(lines, ls...)
|
|
|
|
}
|
2020-05-09 16:38:52 +02:00
|
|
|
for _, v := range b.vars {
|
2020-05-09 17:59:18 +02:00
|
|
|
init := ""
|
2020-05-09 19:01:28 +02:00
|
|
|
if v.init != nil {
|
|
|
|
init = " = " + dumpExpr(v.init)
|
2020-05-09 17:59:18 +02:00
|
|
|
}
|
2020-05-10 17:13:08 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%svar %s %s%s", idt, v.name, v.typ, init))
|
2020-05-09 17:59:18 +02:00
|
|
|
}
|
|
|
|
for _, c := range b.consts {
|
2020-05-10 17:13:08 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("%sconst %s %s = %s", idt, c.name, c.typ, dumpExpr(c.init)))
|
2020-05-09 17:59:18 +02:00
|
|
|
}
|
|
|
|
for _, f := range b.funcs {
|
|
|
|
var args []string
|
|
|
|
for _, a := range f.args {
|
2020-05-10 17:13:08 +02:00
|
|
|
args = append(args, fmt.Sprintf("%s %s", a.name, a.typ))
|
2020-05-09 17:59:18 +02:00
|
|
|
}
|
|
|
|
var rets []string
|
|
|
|
for _, r := range f.rets {
|
|
|
|
name := r.name
|
|
|
|
if name == "" {
|
|
|
|
name = "_"
|
|
|
|
}
|
2020-05-10 17:13:08 +02:00
|
|
|
rets = append(rets, fmt.Sprintf("%s %s", name, r.typ))
|
2020-05-09 17:59:18 +02:00
|
|
|
}
|
|
|
|
l := fmt.Sprintf("func %s(%s)", f.name, strings.Join(args, ", "))
|
|
|
|
if len(rets) > 0 {
|
|
|
|
l += " (" + strings.Join(rets, ", ") + ")"
|
|
|
|
}
|
|
|
|
l += " {"
|
|
|
|
lines = append(lines, l)
|
|
|
|
lines = append(lines, f.body.dump(indent+1)...)
|
|
|
|
lines = append(lines, "}")
|
2020-05-09 16:38:52 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 16:58:22 +02:00
|
|
|
for _, s := range b.stmts {
|
|
|
|
lines = append(lines, s.dump(indent)...)
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:38:52 +02:00
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:58:22 +02:00
|
|
|
type stmtType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
stmtNone stmtType = iota
|
2020-05-09 19:01:28 +02:00
|
|
|
stmtAssign
|
2020-05-10 14:37:59 +02:00
|
|
|
stmtBlock
|
2020-05-09 16:58:22 +02:00
|
|
|
stmtReturn
|
|
|
|
)
|
|
|
|
|
2020-05-09 16:38:52 +02:00
|
|
|
type stmt struct {
|
2020-05-09 16:58:22 +02:00
|
|
|
stmtType stmtType
|
2020-05-09 19:01:28 +02:00
|
|
|
exprs []ast.Expr
|
2020-05-10 14:37:59 +02:00
|
|
|
block *block
|
2020-05-09 16:58:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stmt) dump(indent int) []string {
|
|
|
|
idt := strings.Repeat("\t", indent)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
switch s.stmtType {
|
|
|
|
case stmtNone:
|
|
|
|
lines = append(lines, "%s(none)", idt)
|
2020-05-09 19:01:28 +02:00
|
|
|
case stmtAssign:
|
|
|
|
lines = append(lines, fmt.Sprintf("%s%s = %s", idt, dumpExpr(s.exprs[0]), dumpExpr(s.exprs[1])))
|
2020-05-10 14:37:59 +02:00
|
|
|
case stmtBlock:
|
|
|
|
lines = append(lines, fmt.Sprintf("%s{", idt))
|
|
|
|
lines = append(lines, s.block.dump(indent+1)...)
|
|
|
|
lines = append(lines, fmt.Sprintf("%s}", idt))
|
2020-05-09 16:58:22 +02:00
|
|
|
case stmtReturn:
|
|
|
|
var expr string
|
|
|
|
if len(s.exprs) > 0 {
|
|
|
|
var strs []string
|
|
|
|
for _, e := range s.exprs {
|
2020-05-09 19:01:28 +02:00
|
|
|
strs = append(strs, dumpExpr(e))
|
2020-05-09 16:58:22 +02:00
|
|
|
}
|
|
|
|
expr = " " + strings.Join(strs, ", ")
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%sreturn%s", idt, expr))
|
|
|
|
default:
|
|
|
|
lines = append(lines, fmt.Sprintf("%s(unknown stmt: %d)", idt, s.stmtType))
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2020-05-09 19:01:28 +02:00
|
|
|
func dumpExpr(e ast.Expr) string {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *ast.BasicLit:
|
|
|
|
return e.Value
|
|
|
|
case *ast.CompositeLit:
|
|
|
|
var vals []string
|
|
|
|
for _, e := range e.Elts {
|
|
|
|
vals = append(vals, dumpExpr(e))
|
|
|
|
}
|
2020-05-10 16:56:56 +02:00
|
|
|
return fmt.Sprintf("%s{%s}", e.Type, strings.Join(vals, ", "))
|
2020-05-09 19:01:28 +02:00
|
|
|
case *ast.Ident:
|
|
|
|
return e.Name
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
return fmt.Sprintf("%s.%s", dumpExpr(e.X), dumpExpr(e.Sel))
|
2020-05-09 16:58:22 +02:00
|
|
|
default:
|
2020-05-09 19:01:28 +02:00
|
|
|
return fmt.Sprintf("(unkown expr: %#v)", e)
|
2020-05-09 16:58:22 +02:00
|
|
|
}
|
2020-05-09 16:38:52 +02:00
|
|
|
}
|