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"
|
|
|
|
"go/token"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type block struct {
|
2020-05-09 17:59:18 +02:00
|
|
|
vars []variable
|
|
|
|
consts []constant
|
|
|
|
funcs []function
|
|
|
|
stmts []stmt
|
|
|
|
pos token.Pos
|
2020-05-09 16:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *block) dump(indent int) []string {
|
|
|
|
idt := strings.Repeat("\t", indent)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
|
|
|
|
for _, v := range b.vars {
|
2020-05-09 17:59:18 +02:00
|
|
|
init := ""
|
|
|
|
if v.init != "" {
|
|
|
|
init = " = " + v.init
|
|
|
|
}
|
|
|
|
lines = append(lines, fmt.Sprintf("%svar %s %s%s", idt, v.name, v.typ, init))
|
|
|
|
}
|
|
|
|
for _, c := range b.consts {
|
|
|
|
lines = append(lines, fmt.Sprintf("%sconst %s %s = %s", idt, c.name, c.typ, c.init))
|
|
|
|
}
|
|
|
|
for _, f := range b.funcs {
|
|
|
|
var args []string
|
|
|
|
for _, a := range f.args {
|
|
|
|
args = append(args, fmt.Sprintf("%s %s", a.name, a.typ))
|
|
|
|
}
|
|
|
|
var rets []string
|
|
|
|
for _, r := range f.rets {
|
|
|
|
name := r.name
|
|
|
|
if name == "" {
|
|
|
|
name = "_"
|
|
|
|
}
|
|
|
|
rets = append(rets, fmt.Sprintf("%s %s", name, r.typ))
|
|
|
|
}
|
|
|
|
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
|
|
|
|
stmtReturn
|
|
|
|
)
|
|
|
|
|
2020-05-09 16:38:52 +02:00
|
|
|
type stmt struct {
|
2020-05-09 16:58:22 +02:00
|
|
|
stmtType stmtType
|
|
|
|
exprs []expr
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
case stmtReturn:
|
|
|
|
var expr string
|
|
|
|
if len(s.exprs) > 0 {
|
|
|
|
var strs []string
|
|
|
|
for _, e := range s.exprs {
|
|
|
|
strs = append(strs, e.dump())
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type exprType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
exprNone exprType = iota
|
|
|
|
exprIdent
|
|
|
|
)
|
|
|
|
|
|
|
|
type expr struct {
|
|
|
|
exprType exprType
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *expr) dump() string {
|
|
|
|
switch e.exprType {
|
|
|
|
case exprNone:
|
|
|
|
return "(none)"
|
|
|
|
case exprIdent:
|
|
|
|
return e.value
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("(unkown expr: %d)", e.exprType)
|
|
|
|
}
|
2020-05-09 16:38:52 +02:00
|
|
|
}
|