aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-02 23:55:43 -0500
committerMustafa Quraish <[email protected]>2022-02-03 03:05:19 -0500
commit8b1d8698e9abe244aa8ce4c54b0ae0e18c6c7264 (patch)
tree01e4d24a4bbb0546ddb927e6b03f98852cee8fd6
parentAvoid pointer-arithmetic multiplications if we have `char*` (diff)
downloadcup-8b1d8698e9abe244aa8ce4c54b0ae0e18c6c7264.tar.xz
cup-8b1d8698e9abe244aa8ce4c54b0ae0e18c6c7264.zip
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.
-rw-r--r--std/common.cup73
-rw-r--r--std/math.cup22
2 files changed, 73 insertions, 22 deletions
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