From 1fe012131ed25024762ba05fc2aea91f0ca286a4 Mon Sep 17 00:00:00 2001 From: Saffage <04coder@gmail.com> Date: Sat, 15 Jun 2024 00:44:36 +0300 Subject: [PATCH] chore: valid unsigned integer print format --- cgen/builtins.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/cgen/builtins.go b/cgen/builtins.go index 26ce727..d54c121 100644 --- a/cgen/builtins.go +++ b/cgen/builtins.go @@ -47,12 +47,13 @@ func (gen *generator) builtInPrint(call *ast.Call) string { case types.KindUntypedString: if value.Value != nil { return fmt.Sprintf( - "fwrite(%[1]s, 1, sizeof(%[1]s), stdout)", + `fwrite(%s, 1, %d, stdout)`, value.Value, + len(*constant.AsString(value.Value)), ) } else { return fmt.Sprintf( - "fwrite(%[1]s, 1, sizeof(%[1]s), stdout)", + `fwrite(%[1]s, 1, strlen(%[1]s), stdout)`, gen.exprString(call.Args.Nodes[0]), ) } @@ -61,16 +62,21 @@ func (gen *generator) builtInPrint(call *ast.Call) string { types.KindI16, types.KindI32, types.KindI64, - types.KindU8, - types.KindU16, - types.KindU32, - types.KindU64, types.KindUntypedInt: return fmt.Sprintf( `fprintf(stdout, "%%d", %s)`, gen.exprString(call.Args.Nodes[0]), ) + case types.KindU8, + types.KindU16, + types.KindU32, + types.KindU64: + return fmt.Sprintf( + `fprintf(stdout, "%%u", %s)`, + gen.exprString(call.Args.Nodes[0]), + ) + default: panic("not implemented") } @@ -109,16 +115,21 @@ func (gen *generator) builtInPrintln(call *ast.Call) string { types.KindI16, types.KindI32, types.KindI64, - types.KindU8, - types.KindU16, - types.KindU32, - types.KindU64, types.KindUntypedInt: return fmt.Sprintf( `fprintf(stdout, "%%d\n", %s)`, gen.exprString(call.Args.Nodes[0]), ) + case types.KindU8, + types.KindU16, + types.KindU32, + types.KindU64: + return fmt.Sprintf( + `fprintf(stdout, "%%u\n", %s)`, + gen.exprString(call.Args.Nodes[0]), + ) + default: panic("not implemented") }