Skip to content

Commit a182b9a

Browse files
authored
fix indent of typescript and create C# (#11)
1 parent c113379 commit a182b9a

10 files changed

Lines changed: 605 additions & 73 deletions

File tree

cli/vstruct/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func PrintUsage() {
3535
fmt.Printf("\tpython\t\t\tPython (https://www.python.org/)\n")
3636
fmt.Printf("\trust\t\t\tRust (https://www.rust-lang.org/)\n")
3737
fmt.Printf("\tdart\t\t\tDart (https://dart.dev/)\n")
38-
fmt.Printf("\ttypescript\t\t\tTypescript (https://www.typescriptlang.org/)\n")
38+
fmt.Printf("\tts\t\t\tTypescript (https://www.typescriptlang.org/)\n")
39+
fmt.Printf("\tcs\t\t\tC# (https://docs.microsoft.com/en-us/dotnet/csharp/)\n")
3940

4041
fmt.Printf("\n")
4142
os.Exit(1)
@@ -73,7 +74,7 @@ func main() {
7374
continue
7475
}
7576

76-
langSet := map[string]bool{"go": true, "python": true, "rust": true, "dart": true, "typescript": true}
77+
langSet := map[string]bool{"go": true, "python": true, "rust": true, "dart": true, "ts": true, "cs": true}
7778
if lang == "" {
7879
lang = args[i]
7980
if !langSet[lang] {
@@ -103,11 +104,12 @@ func main() {
103104
}
104105

105106
var langExt map[string]string = map[string]string{
106-
"go": ".go",
107-
"python": ".py",
108-
"rust": ".rs",
109-
"dart": ".dart",
110-
"typescript": ".ts",
107+
"go": ".go",
108+
"python": ".py",
109+
"rust": ".rs",
110+
"dart": ".dart",
111+
"ts": ".ts",
112+
"cs": ".cs",
111113
}
112114

113115
var outputFileName string = inputfile + langExt[lang]

compile/backend/csharp/alias.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package csharp
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/lemon-mint/vstruct/ir"
8+
)
9+
10+
func writeAliases(w io.Writer, i *ir.IR) {
11+
for _, a := range i.Aliases {
12+
fmt.Fprintf(w, "\tusing %s = %s;\n", NameConv(a.Name), AliasConv(TypeConv(a.OriginalType)))
13+
}
14+
}

compile/backend/csharp/conv.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package csharp
2+
3+
import "strings"
4+
5+
func NameConv(in string) string {
6+
return strings.Title(in)
7+
}
8+
9+
func NumberConv(unsigned bool, bit int) string {
10+
switch unsigned {
11+
case true:
12+
switch bit {
13+
case 8:
14+
return "byte"
15+
case 16:
16+
return "ushort"
17+
case 32:
18+
return "uint"
19+
case 64:
20+
return "ulong"
21+
}
22+
case false:
23+
switch bit {
24+
case 8:
25+
return "sbyte"
26+
case 16:
27+
return "short"
28+
case 32:
29+
return "int"
30+
case 64:
31+
return "long"
32+
}
33+
}
34+
return "UInt64"
35+
}
36+
37+
func TypeConv(in string) string {
38+
switch in {
39+
case "uint8":
40+
return "byte"
41+
case "uint16":
42+
return "ushort"
43+
case "uint32":
44+
return "uint"
45+
case "uint64":
46+
return "ulong"
47+
case "int8":
48+
return "sbyte"
49+
case "int16":
50+
return "short"
51+
case "int32":
52+
return "int"
53+
case "int64":
54+
return "long"
55+
case "float32":
56+
return "float"
57+
case "float64":
58+
return "double"
59+
case "bool":
60+
return "bool"
61+
case "string":
62+
return "string"
63+
case "bytes":
64+
return "byte[]"
65+
default:
66+
return NameConv(in)
67+
}
68+
}
69+
70+
func AliasConv(primitive string) string {
71+
switch primitive {
72+
case "bool":
73+
return "System.Boolean"
74+
case "byte":
75+
return "System.Byte"
76+
case "sbyte":
77+
return "System.SByte"
78+
case "short":
79+
return "System.Int16"
80+
case "ushort":
81+
return "System.UInt16"
82+
case "int":
83+
return "System.Int32"
84+
case "uint":
85+
return "System.UInt32"
86+
case "long":
87+
return "System.Int64"
88+
case "ulong":
89+
return "System.UInt64"
90+
case "float":
91+
return "System.Single"
92+
case "double":
93+
return "System.Double"
94+
case "string":
95+
return "System.String"
96+
case "bytes":
97+
return "System.Byte[]"
98+
}
99+
return primitive
100+
}

compile/backend/csharp/cs.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package csharp
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"strings"
7+
8+
"github.com/lemon-mint/vstruct/ir"
9+
)
10+
11+
func Generate(w io.Writer, i *ir.IR, packageName string) error {
12+
var codedataBuf strings.Builder
13+
writeAliases(&codedataBuf, i)
14+
writeEnums(&codedataBuf, i)
15+
writeStructs(&codedataBuf, i)
16+
output := fmt.Sprintf(
17+
`namespace vstruct.%s
18+
{
19+
%s
20+
}
21+
`,
22+
packageName,
23+
codedataBuf.String(),
24+
)
25+
_, err := w.Write([]byte(output))
26+
return err
27+
}

compile/backend/csharp/enums.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package csharp
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/lemon-mint/vstruct/ir"
8+
)
9+
10+
func writeEnums(w io.Writer, i *ir.IR) {
11+
for _, e := range i.Enums {
12+
fmt.Fprintf(w, "\tpublic enum %s : %s\n", NameConv(e.Name), NumberConv(true, e.Size*8))
13+
fmt.Fprintf(w, "\t{\n")
14+
for _, o := range e.Options {
15+
fmt.Fprintf(w, "\t\t%s,\n", NameConv(o))
16+
}
17+
fmt.Fprintf(w, "\t}\n\n")
18+
}
19+
}

0 commit comments

Comments
 (0)