From 8b1d8698e9abe244aa8ce4c54b0ae0e18c6c7264 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Wed, 2 Feb 2022 23:55:43 -0500 Subject: Move all common utilities to `std/common.cup` In the future we can split this into multiple files if we need to, after we've added to ability to handle more complex input graphs without parsing the same file twice. --- std/common.cup | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ std/math.cup | 22 ------------------ 2 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 std/common.cup delete mode 100644 std/math.cup (limited to 'std') diff --git a/std/common.cup b/std/common.cup new file mode 100644 index 0000000..b5e0e9f --- /dev/null +++ b/std/common.cup @@ -0,0 +1,73 @@ +fn min(a: int, b: int): int { + return a < b ? a : b; +} + +fn max(a: int, b: int): int { + return a > b ? a : b; +} + +fn sign(a: int): int { + return a > 0 ? 1 : a == 0 ? 0 : -1; +} + +fn abs(a: int): int { + return a * sign(a); +} + +fn factorial(n: int): int { + let res: int = 1; + for (;n > 0; n = n - 1) + res = res * n; + return res; +} + + +fn strlen(s: char *): int { + let count: int = 0; + while (*s != 0) { + count = count + 1; + s = s + 1; + } + return count; +} + +fn strcpy(dst: char *, src: char *) { + while (*src != 0) { + *dst = *src; + dst = dst + 1; + src = src + 1; + } + *dst = '\0'; +} + +fn strcat(dst: char *, src: char *) { + while (*dst != 0) { + dst = dst + 1; + } + while (*src != 0) { + *dst = *src; + dst = dst + 1; + src = src + 1; + } + *dst = '\0'; +} + +fn streq(s1: char *, s2: char *): int { + while (*s1 != 0 && *s2 != 0) { + if (*s1 != *s2) + return 0; + s1 = s1 + 1; + s2 = s2 + 1; + } + return *s1 == *s2; +} + +// TODO: This should call fwrite() directly at some point. +fn puts(c: char *) { + let len: int = strlen(c); + let i: int = 0; + + for (i = 0; i < len; i = i + 1) { + putc(c[i]); + } +} \ No newline at end of file diff --git a/std/math.cup b/std/math.cup deleted file mode 100644 index d58aa86..0000000 --- a/std/math.cup +++ /dev/null @@ -1,22 +0,0 @@ -fn min(a: int, b: int): int { - return a < b ? a : b; -} - -fn max(a: int, b: int): int { - return a > b ? a : b; -} - -fn sign(a: int): int { - return a > 0 ? 1 : a == 0 ? 0 : -1; -} - -fn abs(a: int): int { - return a * sign(a); -} - -fn factorial(n: int): int { - let res: int = 1; - for (;n > 0; n = n - 1) - res = res * n; - return res; -} \ No newline at end of file -- cgit v1.2.3