Skip to content

Commit 0fc7d69

Browse files
committed
added support for consts in stdlib gen
1 parent 2a189a1 commit 0fc7d69

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

gen/stdlib-generator.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ import (
2323
var nameMap = map[string]map[string]string{
2424
"DE": {
2525
"type": "Typ",
26+
"value": "Wert",
2627
"var": "Variablen",
28+
"const": "Konstante",
2729
"func": "Funktionen",
2830
"comb": "Kombinationen",
2931
"moduleEmpty": "Dieses Modul ist Leer",
3032
},
3133
"EN": {
3234
"type": "Type",
35+
"value": "Value",
3336
"var": "variables",
37+
"const": "constants",
3438
"func": "functions",
3539
"comb": "combinations",
3640
"moduleEmpty": "This module is empty",
@@ -134,11 +138,9 @@ func makeMdFiles(publicDecls []ast.Declaration, inputFile []byte, outputFilePath
134138
}
135139

136140
func writeMD(inputFile string, outputFile *os.File, publicDecls []ast.Declaration, lang string) {
137-
funcBldr := &bytes.Buffer{}
138-
varBldr := &bytes.Buffer{}
139-
structBldr := &bytes.Buffer{}
141+
funcBldr, varBldr, constBldr, structBldr := &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}
140142

141-
hasStructs, hasVars, hasFuncs := false, false, false
143+
hasStructs, hasVars, hasConsts, hasFuncs := false, false, false, false
142144

143145
for _, decl := range publicDecls {
144146
switch decl := decl.(type) {
@@ -161,7 +163,7 @@ func writeMD(inputFile string, outputFile *os.File, publicDecls []ast.Declaratio
161163
types := make([]string, 0, len(decl.Parameters))
162164
for _, param := range decl.Parameters {
163165
names = append(names, "<code>"+param.Name.String()+"</code>")
164-
types = append(types, "<code>"+param.Type.String()+"</code>")
166+
types = append(types, "<code>"+param.Type.Type.String()+"</code>")
165167
}
166168

167169
params = strings.Join(names, ", ")
@@ -193,13 +195,20 @@ func writeMD(inputFile string, outputFile *os.File, publicDecls []ast.Declaratio
193195
}
194196

195197
fmt.Fprintf(funcBldr, "{{< duden-function name=\"%s\" desc=\"%s\" params=\"%s\" paramTypes=\"%s\" ret=\"%s\" impl=\"%s\" extern=\"%t\" aliases=\"%s\" >}}\n\n", // }}"
196-
decl.Name(), descr, params, paramTypes, decl.Type.String(), impl, isExtern, aliases)
198+
decl.Name(), descr, params, paramTypes, decl.ReturnType.String(), impl, isExtern, aliases)
197199
case *ast.VarDecl:
198200
hasVars = true
199201

200202
fmt.Fprintf(varBldr, "## %s\n", decl.Name())
201203
fmt.Fprintf(varBldr, "* %s: <code>%s</code>\n", nameMap[lang]["type"], decl.Type)
202204
fmt.Fprintln(varBldr, "")
205+
case *ast.ConstDecl:
206+
hasConsts = true
207+
208+
fmt.Fprintf(constBldr, "## %s\n", decl.Name())
209+
fmt.Fprintf(constBldr, "* %s: <code>%s</code>\n", nameMap[lang]["type"], decl.Type)
210+
fmt.Fprintf(constBldr, "* %s: <code>%s</code>\n", nameMap[lang]["value"], decl.Val.Token().Literal)
211+
fmt.Fprintln(constBldr, "")
203212
case *ast.StructDecl:
204213
hasStructs = true
205214

@@ -250,11 +259,15 @@ func writeMD(inputFile string, outputFile *os.File, publicDecls []ast.Declaratio
250259
fmt.Fprintf(outputFile, "# Duden/%s %s\n", fileName, nameMap[lang]["var"])
251260
fmt.Fprintln(outputFile, varBldr)
252261
}
262+
if hasConsts {
263+
fmt.Fprintf(outputFile, "# Duden/%s %s\n", fileName, nameMap[lang]["const"])
264+
fmt.Fprintln(outputFile, constBldr)
265+
}
253266
if hasFuncs {
254267
fmt.Fprintf(outputFile, "# Duden/%s %s\n", fileName, nameMap[lang]["func"])
255268
fmt.Fprintln(outputFile, funcBldr)
256269
}
257-
if !hasFuncs && !hasVars {
258-
fmt.Fprintf(outputFile, nameMap[lang]["moduleEmpty"])
270+
if !hasFuncs && !hasVars && hasConsts && !hasStructs {
271+
fmt.Fprint(outputFile, nameMap[lang]["moduleEmpty"])
259272
}
260273
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module github.com/DDP-Projekt/Bedienungsanleitung/gen
22

33
go 1.22.2
44

5-
require github.com/DDP-Projekt/Kompilierer v0.4.0-alpha
5+
require github.com/DDP-Projekt/Kompilierer v0.6.0

go.sum

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha h1:zQadWm8wLjOTN3/pWDLDLfNBM/I4Apx8GaxWGNRZfvE=
2-
github.com/DDP-Projekt/Kompilierer v0.4.0-alpha/go.mod h1:wIQjgAVockUlLS3bRd2WSuJlPUFC+ZTZizIQ1rsEeP4=
1+
github.com/DDP-Projekt/Kompilierer v0.6.0 h1:EkNnWt8v47ma+Nda5lZaT23GUh4iGQY+Kn/IWDjRg2A=
2+
github.com/DDP-Projekt/Kompilierer v0.6.0/go.mod h1:FL5eYp6AuDeKh15doNtcOyESwUg7JmVOhOi4J/EepFY=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
8+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)