mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 03:53:42 +00:00
Implement print() and println() in Go
This commit is contained in:
@@ -1,43 +1,6 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "runtime.h"
|
||||
|
||||
void __go_printstring(string_t str) {
|
||||
for (int i = 0; i < str.len; i++) {
|
||||
putchar(str.buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void __go_printint(intgo_t n) {
|
||||
// Print integer in signed big-endian base-10 notation, for humans to
|
||||
// read.
|
||||
// TODO: don't recurse, but still be compact (and don't divide/mod
|
||||
// more than necessary).
|
||||
if (n < 0) {
|
||||
putchar('-');
|
||||
n = -n;
|
||||
}
|
||||
intgo_t prevdigits = n / 10;
|
||||
if (prevdigits != 0) {
|
||||
__go_printint(prevdigits);
|
||||
}
|
||||
putchar((n % 10) + '0');
|
||||
}
|
||||
|
||||
void __go_printbyte(uint8_t c) {
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
void __go_printspace() {
|
||||
putchar(' ');
|
||||
}
|
||||
|
||||
void __go_printnl() {
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void go_main() __asm__("main.main");
|
||||
|
||||
void __go_runtime_main() {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
package runtime
|
||||
|
||||
// #include <stdio.h>
|
||||
import "C"
|
||||
|
||||
const Compiler = "tgo"
|
||||
|
||||
func printstring(s string) {
|
||||
for i := 0; i < len(s); i++ {
|
||||
C.putchar(C.int(s[i]))
|
||||
}
|
||||
}
|
||||
|
||||
func printint(n int) {
|
||||
// Print integer in signed big-endian base-10 notation, for humans to
|
||||
// read.
|
||||
// TODO: don't recurse, but still be compact (and don't divide/mod
|
||||
// more than necessary).
|
||||
if n < 0 {
|
||||
C.putchar('-')
|
||||
n = -n
|
||||
}
|
||||
prevdigits := n / 10
|
||||
if prevdigits != 0 {
|
||||
printint(prevdigits)
|
||||
}
|
||||
C.putchar(C.int((n % 10) + '0'))
|
||||
}
|
||||
|
||||
func printbyte(c uint8) {
|
||||
C.putchar(C.int(c))
|
||||
}
|
||||
|
||||
func printspace() {
|
||||
C.putchar(' ')
|
||||
}
|
||||
|
||||
func printnl() {
|
||||
C.putchar('\n')
|
||||
}
|
||||
Reference in New Issue
Block a user