diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-12 13:05:55 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-12 13:05:55 +0200 |
commit | c26a76a39a93656b547886acbc96147fe779cffc (patch) | |
tree | 557b48546ad02a4705cf1c035be8cf8c384c2bab /samples | |
parent | acb633cc836a3068b597c89d4f160c5ab6e22fa7 (diff) | |
download | golem-c26a76a39a93656b547886acbc96147fe779cffc.tar.gz |
🗿
Diffstat (limited to 'samples')
-rw-r--r-- | samples/helloworld.golem | 13 | ||||
-rw-r--r-- | samples/malloc.golem | 65 |
2 files changed, 71 insertions, 7 deletions
diff --git a/samples/helloworld.golem b/samples/helloworld.golem index 473331f..2e0d82c 100644 --- a/samples/helloworld.golem +++ b/samples/helloworld.golem @@ -19,14 +19,13 @@ main() { } strcpy(dst, src) { - local rv; - rv = dst; + local i; + i = 0; loop { - [dst] = [src]; - src = src + 1; - if ([src] == 0) - return rv; - dst = dst + 1; + [dst + i] = [src + i]; + i = i + 1; + if ([src + i] == 0) + return dst; } } diff --git a/samples/malloc.golem b/samples/malloc.golem new file mode 100644 index 0000000..762f78f --- /dev/null +++ b/samples/malloc.golem @@ -0,0 +1,65 @@ +main() { + local s; + s = strdup("coucou le monde"); + puts(s); + stoupper(s); + puts(s); +} + +puts(s) { + loop { + if ([s] == 0) { + wrt '\n'; + return 0; + } + wrt [s]; + s = s + 1; + } +} + +strlen(s) { + local len; + len = 0; + loop { + if ([s + len] == 0) { + return len; + } + len = len + 1; + } +} + +strdup(s) { + return strcpy(malloc(strlen(s) + 1), s); +} + +strcpy(dst, src) { + local i; + i = 0; + loop { + [dst + i] = [src + i]; + i = i + 1; + if ([src + i] == 0) + return dst; + } +} + +stoupper(s) { + loop { + if ([s] == 0) return 0; + [s] = [s] - ([s] >= 'a' & [s] <= 'z') * 32; + s = s + 1; + } +} + +global heap[4096] = 0x69; +global heap_size = 0; +malloc(n) { + local p; + p = heap + heap_size; + heap_size = heap_size + n; + if (heap_size > 4096) { + puts("malloc error: heap is full"); + return 0; + } + return p; +} |