~emersion/gyosu

393dc2745c484808e4c16451e96778f0b1372994 — Simon Ser 3 months ago abd3997 master
Add support for macros

Closes: https://todo.sr.ht/~emersion/gyosu/7
2 files changed, 41 insertions(+), 1 deletions(-)

M main.go
M template.go
M main.go => main.go +34 -1
@@ 516,6 516,39 @@ func main() {
		}
	}

	for name, macro := range ast.Macros {
		if !macro.IsConst || len(macro.ReplacementList()) == 0 {
			continue
		}

		origFilename, ok := targetMap[macro.Position().Filename]
		if !ok {
			continue
		}

		var proto prototype
		proto.Keyword("#define").Raw(" ").Raw(name)
		if macro.IsFnLike {
			proto.Raw("(")
			for i, param := range macro.Params {
				if i > 0 {
					proto.Raw(", ")
				}
				proto.Raw(param.SrcStr())
			}
			proto.Raw(")")
		}
		proto.Raw(" …")

		decls = append(decls, declData{
			Kind:      declMacro,
			Name:      name,
			Prototype: proto,
			// TODO: Description
			Filename: origFilename,
		})
	}

	declMap := make(map[declKey]declData)
	for _, decl := range decls {
		if prev, ok := declMap[decl.key()]; ok {


@@ 543,7 576,7 @@ func main() {
	}

	sort.Slice(decls, func(i, j int) bool {
		return decls[i].Name < decls[j].Name
		return strings.ToLower(decls[i].Name) < strings.ToLower(decls[j].Name)
	})

	tpl := template.New("gyosu")

M template.go => template.go +7 -0
@@ 24,6 24,7 @@ const (
	declStruct declKind = "struct"
	declUnion  declKind = "union"
	declEnum   declKind = "enum"
	declMacro  declKind = "macro"
)

type inlineType int


@@ 92,6 93,12 @@ func (decl *declData) Title() string {
		return decl.Name + "()"
	case declVar:
		return decl.Name
	case declMacro:
		if strings.Contains(prototype(decl.Prototype).String(), "(") {
			return decl.Name + "()"
		} else {
			return decl.Name
		}
	default:
		return string(decl.Kind) + " " + decl.Name
	}