Skip to content

Commit

Permalink
chore: valid unsigned integer print format
Browse files Browse the repository at this point in the history
  • Loading branch information
saffage committed Jun 14, 2024
1 parent 4997f72 commit 1fe0121
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions cgen/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
)
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 1fe0121

Please sign in to comment.