Fix non-ascii identifiers

This commit is contained in:
MrLetsplay 2024-03-21 21:00:31 +01:00
parent 56c36f01dd
commit 8433736096
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
2 changed files with 18 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"errors"
"log"
"strconv"
"unicode"
)
func getPrimitiveWATType(primitive PrimitiveType) string {
@ -34,6 +35,20 @@ func getWATType(t Type) string {
return getPrimitiveWATType(primitive)
}
func safeASCIIIdentifier(identifier string) string {
ascii := ""
for _, rune := range identifier {
if rune < unicode.MaxASCII && (unicode.IsLetter(rune) || unicode.IsDigit(rune)) {
ascii += string(rune)
continue
}
ascii += "$" + strconv.Itoa(int(rune))
}
return ascii
}
func getTypeCast(primitive PrimitiveType) string {
switch primitive {
case Primitive_I8:
@ -323,7 +338,7 @@ func compileBlockWAT(block Block) (string, error) {
}
func compileFunctionWAT(function ParsedFunction) (string, error) {
funcWAT := "(func $" + function.Name + "\n"
funcWAT := "(func $" + safeASCIIIdentifier(function.Name) + "\n"
for _, local := range function.Locals {
if !local.IsParameter {
@ -359,7 +374,7 @@ func compileFunctionWAT(function ParsedFunction) (string, error) {
funcWAT += wat
return funcWAT + ") (export \"" + function.Name + "\" (func $" + function.Name + "))\n", nil
return funcWAT + ") (export \"" + function.Name + "\" (func $" + safeASCIIIdentifier(function.Name) + "))\n", nil
}
func backendWAT(file ParsedFile) (string, error) {

View File

@ -2,7 +2,7 @@ u64 add(u8 a, u8 b) {
return add(a - 1u8, a);
}
u64 add2(u64 a, u64 b) {
u64 add(u64 a, u64 b) {
return a + b;
}