mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 03:27:48 +00:00
Reorganize packages
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
|
||||
package main
|
||||
|
||||
const SIX = 6
|
||||
|
||||
func main() {
|
||||
println("Hello world from Go!")
|
||||
println("The answer is:", calculateAnswer())
|
||||
println("5 ** 2 =", square(5))
|
||||
println("3 + 12 =", add(3, 12))
|
||||
println("fib(11) =", fib(11))
|
||||
println("sumrange(100) =", sumrange(100))
|
||||
}
|
||||
|
||||
func calculateAnswer() int {
|
||||
seven := 7
|
||||
return SIX * seven
|
||||
}
|
||||
|
||||
func square(n int) int {
|
||||
return n * n
|
||||
}
|
||||
|
||||
func add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
func fib(n int) int {
|
||||
if n <= 2 {
|
||||
return 1
|
||||
}
|
||||
return fib(n - 1) + fib(n - 2)
|
||||
}
|
||||
|
||||
func sumrange(n int) int {
|
||||
sum := 0
|
||||
for i := 1; i <= n; i++ {
|
||||
sum += i
|
||||
}
|
||||
return sum
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "runtime.h"
|
||||
|
||||
#define print(buf, len) write(STDOUT_FILENO, buf, len)
|
||||
|
||||
void __go_printstring(string_t str) {
|
||||
write(STDOUT_FILENO, str.buf, str.len);
|
||||
}
|
||||
|
||||
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) {
|
||||
print("-", 1);
|
||||
n = -n;
|
||||
}
|
||||
intgo_t prevdigits = n / 10;
|
||||
if (prevdigits != 0) {
|
||||
__go_printint(prevdigits);
|
||||
}
|
||||
char buf[1] = {(n % 10) + '0'};
|
||||
print(buf, 1);
|
||||
}
|
||||
|
||||
void __go_printspace() {
|
||||
print(" ", 1);
|
||||
}
|
||||
|
||||
void __go_printnl() {
|
||||
print("\n", 1);
|
||||
}
|
||||
|
||||
void go_main() __asm__("main.main");
|
||||
|
||||
void __go_runtime_main() {
|
||||
go_main();
|
||||
}
|
||||
|
||||
__attribute__((weak))
|
||||
int main() {
|
||||
__go_runtime_main();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t len; // TODO: size_t (or, let max string size depend on target/flag)
|
||||
uint8_t *buf; // points to string buffer itself
|
||||
} string_t;
|
||||
|
||||
typedef int32_t intgo_t; // may be 64-bit
|
||||
Reference in New Issue
Block a user