From 1d4ca5d55c46f74e29dc55e5ab06a68c66f81f50 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 13 Jun 2024 23:14:35 -0400 Subject: [PATCH] broke runtime up into a static lib --- .gitignore | 2 + build.sh | 10 +- runtime.h | 224 +++------------------------------ runtime/Array.c | 10 ++ runtime/Hash.c | 10 ++ runtime/Object.c | 11 ++ runtime/OpAdd.c | 30 +++++ runtime/OpDiv.c | 14 +++ runtime/OpEq.c | 17 +++ runtime/OpGt.c | 13 ++ runtime/OpGtEq.c | 13 ++ runtime/OpLt.c | 13 ++ runtime/OpLtEq.c | 13 ++ runtime/OpMod.c | 12 ++ runtime/OpMul.c | 14 +++ runtime/OpNeq.c | 17 +++ runtime/OpSub.c | 14 +++ runtime/ToString.c | 23 ++++ runtime.c => runtime/runtime.c | 0 19 files changed, 254 insertions(+), 206 deletions(-) create mode 100644 runtime/Array.c create mode 100644 runtime/Hash.c create mode 100644 runtime/Object.c create mode 100644 runtime/OpAdd.c create mode 100644 runtime/OpDiv.c create mode 100644 runtime/OpEq.c create mode 100644 runtime/OpGt.c create mode 100644 runtime/OpGtEq.c create mode 100644 runtime/OpLt.c create mode 100644 runtime/OpLtEq.c create mode 100644 runtime/OpMod.c create mode 100644 runtime/OpMul.c create mode 100644 runtime/OpNeq.c create mode 100644 runtime/OpSub.c create mode 100644 runtime/ToString.c rename runtime.c => runtime/runtime.c (100%) diff --git a/.gitignore b/.gitignore index 38b2985..b11885c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ cerise-c cerise-c.c +*.o +*.a diff --git a/build.sh b/build.sh index 2ffc1e9..59796c2 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,14 @@ #!/bin/sh +for f in runtime/*.c; do + printf "CC %s\n" "$f" + gcc -c -I. -O2 -o "${f%.c}.o" "$f" +done +printf "LIB %s\n" libruntime.a +ar rcs libruntime.a runtime/*.o + +printf "\n" ./cerise-c.rb > cerise-c.c \ - && gcc -I. -O2 -o cerise-c cerise-c.c runtime.c \ + && gcc -I. -O2 -o cerise-c cerise-c.c libruntime.a \ && ./cerise-c diff --git a/runtime.h b/runtime.h index c512780..2a29bee 100644 --- a/runtime.h +++ b/runtime.h @@ -194,27 +194,8 @@ static inline Value MakeHash(int32_t nslots) { /* Value Type Conversions *************************************************/ -static Value ToString(Value val) { - Value retval; - if (IsString(val)) { - retval = val; - } else if (IsNil(val)) { - retval = MakeString("nil"); - } else if (IsBool(val)) { - retval = MakeString(IsTrue(val) ? "true" : "false"); - } else if (IsInt(val)) { - char str[64]; - snprintf(str, sizeof(str), "%d", ValueAsInt(val)); - retval = MakeString(str); - } else if (IsReal(val)) { - char str[64]; - snprintf(str, sizeof(str), "%f", ValueAsReal(val)); - retval = MakeString(str); - } else { - assert(!"could not convert string"); - } - return retval; -} +Value ToString(Value val); + /* Builtin Functions *************************************************/ @@ -232,192 +213,25 @@ static void Assert(char* file, int lineno, Value val) { /* Binary Operators *************************************************/ - -static inline Value OpAdd(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeInt(ValueAsInt(left) + ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeReal(ValueAsReal(left) + ValueAsReal(right)); - } else if (IsString(left)) { - right = ToString(right); - String* left_str = ValueAsString(left); - String* right_str = ValueAsString(right); - size_t sz = left_str->length + right_str->length; - String* str = calloc(sizeof(String) + sz + 1, 1); - str->length = sz; - memcpy( - str->bytes, - left_str->bytes, - left_str->length); - memcpy( - &str->bytes[left_str->length], - right_str->bytes, - right_str->length + 1); - result = (Value){ .as_uint64 = (NAN_TAG_STRING | (uint64_t)str) }; - } else { - fprintf(stderr, "fatal error: addition operator used on invalid type\n"); - exit(1); - } - return result; -} - -static inline Value OpSub(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeInt(ValueAsInt(left) - ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeReal(ValueAsReal(left) - ValueAsReal(right)); - } else { - fprintf(stderr, "fatal error: addition operator used on invalid type\n"); - exit(1); - } - return result; -} - -static inline Value OpMul(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeInt(ValueAsInt(left) * ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeReal(ValueAsReal(left) * ValueAsReal(right)); - } else { - fprintf(stderr, "fatal error: addition operator used on invalid type\n"); - exit(1); - } - return result; -} - -static inline Value OpDiv(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeInt(ValueAsInt(left) / ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeReal(ValueAsReal(left) / ValueAsReal(right)); - } else { - fprintf(stderr, "fatal error: addition operator used on invalid type\n"); - exit(1); - } - return result; -} - -static inline Value OpMod(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeInt(ValueAsInt(left) % ValueAsInt(right)); - } else { - fprintf(stderr, "fatal error: addition operator used on invalid type\n"); - exit(1); - } - return result; -} - -static inline Value OpLt(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeBool(ValueAsInt(left) < ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeBool(ValueAsReal(left) < ValueAsReal(right)); - } else { - result = MakeBool(false); - } - return result; -} - -static inline Value OpLtEq(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeBool(ValueAsInt(left) <= ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeBool(ValueAsReal(left) <= ValueAsReal(right)); - } else { - result = MakeBool(false); - } - return result; -} - -static inline Value OpGt(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeBool(ValueAsInt(left) > ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeBool(ValueAsReal(left) > ValueAsReal(right)); - } else { - result = MakeBool(false); - } - return result; -} - -static inline Value OpGtEq(Value left, Value right) { - Value result; - if (IsInt(left)) { - result = MakeBool(ValueAsInt(left) >= ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeBool(ValueAsReal(left) >= ValueAsReal(right)); - } else { - result = MakeBool(false); - } - return result; -} - -static inline Value OpEq(Value left, Value right) { - Value result; - if (IsBool(left)) { - result = MakeBool(ValueAsBool(left) == ValueAsBool(right)); - } else if (IsInt(left)) { - result = MakeBool(ValueAsInt(left) == ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeBool(ValueAsReal(left) == ValueAsReal(right)); - } else if (IsString(left)) { - result = MakeBool(!strcmp(ValueAsString(left)->bytes, ValueAsString(right)->bytes)); - } else { - result = MakeBool(false); - } - return result; -} - -static inline Value OpNeq(Value left, Value right) { - Value result; - if (IsBool(left)) { - result = MakeBool(ValueAsBool(left) != ValueAsBool(right)); - } else if (IsInt(left)) { - result = MakeBool(ValueAsInt(left) != ValueAsInt(right)); - } else if (IsReal(left)) { - result = MakeBool(ValueAsReal(left) != ValueAsReal(right)); - } else if (IsString(left)) { - result = MakeBool(0 != strcmp(ValueAsString(left)->bytes, ValueAsString(right)->bytes)); - } else { - result = MakeBool(false); - } - return result; -} +Value OpAdd(Value left, Value right); +Value OpSub(Value left, Value right); +Value OpMul(Value left, Value right); +Value OpDiv(Value left, Value right); +Value OpMod(Value left, Value right); +Value OpLt(Value left, Value right); +Value OpLtEq(Value left, Value right); +Value OpGt(Value left, Value right); +Value OpGtEq(Value left, Value right); +Value OpEq(Value left, Value right); +Value OpNeq(Value left, Value right); /* Arrays And Hashes *************************************************/ -static void Array_Set(Value array, int index, Value value) -{ -} +void Array_Set(Value array, int index, Value value); +Value Array_Get(Value array, int index); -static Value Array_Get(Value array, int index) -{ - return MakeNil(); -} +void Hash_Set(Value hash, Value key, Value value); +Value Hash_Get(Value hash, Value key); -static void Hash_Set(Value hash, Value key, Value value) -{ -} - -static Value Hash_Get(Value hash, Value key) -{ - return MakeNil(); -} - -static Value Object_Get(Value object, Value key) -{ - return MakeNil(); -} - -static Value Object_Set(Value object, Value key, Value value) -{ - return MakeNil(); -} +Value Object_Get(Value object, Value key); +Value Object_Set(Value object, Value key, Value value); diff --git a/runtime/Array.c b/runtime/Array.c new file mode 100644 index 0000000..7b92b95 --- /dev/null +++ b/runtime/Array.c @@ -0,0 +1,10 @@ +#include "runtime.h" + +void Array_Set(Value array, int index, Value value) +{ +} + +Value Array_Get(Value array, int index) +{ + return MakeNil(); +} diff --git a/runtime/Hash.c b/runtime/Hash.c new file mode 100644 index 0000000..b644ae5 --- /dev/null +++ b/runtime/Hash.c @@ -0,0 +1,10 @@ +#include "runtime.h" + +void Hash_Set(Value hash, Value key, Value value) +{ +} + +Value Hash_Get(Value hash, Value key) +{ + return MakeNil(); +} diff --git a/runtime/Object.c b/runtime/Object.c new file mode 100644 index 0000000..0e0318f --- /dev/null +++ b/runtime/Object.c @@ -0,0 +1,11 @@ +#include "runtime.h" + +Value Object_Get(Value object, Value key) +{ + return MakeNil(); +} + +Value Object_Set(Value object, Value key, Value value) +{ + return MakeNil(); +} diff --git a/runtime/OpAdd.c b/runtime/OpAdd.c new file mode 100644 index 0000000..aa126c6 --- /dev/null +++ b/runtime/OpAdd.c @@ -0,0 +1,30 @@ +#include "runtime.h" + +Value OpAdd(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeInt(ValueAsInt(left) + ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeReal(ValueAsReal(left) + ValueAsReal(right)); + } else if (IsString(left)) { + right = ToString(right); + String* left_str = ValueAsString(left); + String* right_str = ValueAsString(right); + size_t sz = left_str->length + right_str->length; + String* str = calloc(sizeof(String) + sz + 1, 1); + str->length = sz; + memcpy( + str->bytes, + left_str->bytes, + left_str->length); + memcpy( + &str->bytes[left_str->length], + right_str->bytes, + right_str->length + 1); + result = (Value){ .as_uint64 = (NAN_TAG_STRING | (uint64_t)str) }; + } else { + fprintf(stderr, "fatal error: addition operator used on invalid type\n"); + exit(1); + } + return result; +} diff --git a/runtime/OpDiv.c b/runtime/OpDiv.c new file mode 100644 index 0000000..d428d66 --- /dev/null +++ b/runtime/OpDiv.c @@ -0,0 +1,14 @@ +#include "runtime.h" + +Value OpDiv(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeInt(ValueAsInt(left) / ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeReal(ValueAsReal(left) / ValueAsReal(right)); + } else { + fprintf(stderr, "fatal error: addition operator used on invalid type\n"); + exit(1); + } + return result; +} diff --git a/runtime/OpEq.c b/runtime/OpEq.c new file mode 100644 index 0000000..158b7b1 --- /dev/null +++ b/runtime/OpEq.c @@ -0,0 +1,17 @@ +#include "runtime.h" + +Value OpEq(Value left, Value right) { + Value result; + if (IsBool(left)) { + result = MakeBool(ValueAsBool(left) == ValueAsBool(right)); + } else if (IsInt(left)) { + result = MakeBool(ValueAsInt(left) == ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeBool(ValueAsReal(left) == ValueAsReal(right)); + } else if (IsString(left)) { + result = MakeBool(!strcmp(ValueAsString(left)->bytes, ValueAsString(right)->bytes)); + } else { + result = MakeBool(false); + } + return result; +} diff --git a/runtime/OpGt.c b/runtime/OpGt.c new file mode 100644 index 0000000..5c26007 --- /dev/null +++ b/runtime/OpGt.c @@ -0,0 +1,13 @@ +#include "runtime.h" + +Value OpGt(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeBool(ValueAsInt(left) > ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeBool(ValueAsReal(left) > ValueAsReal(right)); + } else { + result = MakeBool(false); + } + return result; +} diff --git a/runtime/OpGtEq.c b/runtime/OpGtEq.c new file mode 100644 index 0000000..fb415f6 --- /dev/null +++ b/runtime/OpGtEq.c @@ -0,0 +1,13 @@ +#include "runtime.h" + +Value OpGtEq(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeBool(ValueAsInt(left) >= ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeBool(ValueAsReal(left) >= ValueAsReal(right)); + } else { + result = MakeBool(false); + } + return result; +} diff --git a/runtime/OpLt.c b/runtime/OpLt.c new file mode 100644 index 0000000..30c6bbc --- /dev/null +++ b/runtime/OpLt.c @@ -0,0 +1,13 @@ +#include "runtime.h" + +Value OpLt(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeBool(ValueAsInt(left) < ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeBool(ValueAsReal(left) < ValueAsReal(right)); + } else { + result = MakeBool(false); + } + return result; +} diff --git a/runtime/OpLtEq.c b/runtime/OpLtEq.c new file mode 100644 index 0000000..862f203 --- /dev/null +++ b/runtime/OpLtEq.c @@ -0,0 +1,13 @@ +#include "runtime.h" + +Value OpLtEq(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeBool(ValueAsInt(left) <= ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeBool(ValueAsReal(left) <= ValueAsReal(right)); + } else { + result = MakeBool(false); + } + return result; +} diff --git a/runtime/OpMod.c b/runtime/OpMod.c new file mode 100644 index 0000000..6a5e361 --- /dev/null +++ b/runtime/OpMod.c @@ -0,0 +1,12 @@ +#include "runtime.h" + +Value OpMod(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeInt(ValueAsInt(left) % ValueAsInt(right)); + } else { + fprintf(stderr, "fatal error: addition operator used on invalid type\n"); + exit(1); + } + return result; +} diff --git a/runtime/OpMul.c b/runtime/OpMul.c new file mode 100644 index 0000000..456e631 --- /dev/null +++ b/runtime/OpMul.c @@ -0,0 +1,14 @@ +#include "runtime.h" + +Value OpMul(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeInt(ValueAsInt(left) * ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeReal(ValueAsReal(left) * ValueAsReal(right)); + } else { + fprintf(stderr, "fatal error: addition operator used on invalid type\n"); + exit(1); + } + return result; +} diff --git a/runtime/OpNeq.c b/runtime/OpNeq.c new file mode 100644 index 0000000..d2e72be --- /dev/null +++ b/runtime/OpNeq.c @@ -0,0 +1,17 @@ +#include "runtime.h" + +Value OpNeq(Value left, Value right) { + Value result; + if (IsBool(left)) { + result = MakeBool(ValueAsBool(left) != ValueAsBool(right)); + } else if (IsInt(left)) { + result = MakeBool(ValueAsInt(left) != ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeBool(ValueAsReal(left) != ValueAsReal(right)); + } else if (IsString(left)) { + result = MakeBool(0 != strcmp(ValueAsString(left)->bytes, ValueAsString(right)->bytes)); + } else { + result = MakeBool(false); + } + return result; +} diff --git a/runtime/OpSub.c b/runtime/OpSub.c new file mode 100644 index 0000000..d21b07c --- /dev/null +++ b/runtime/OpSub.c @@ -0,0 +1,14 @@ +#include "runtime.h" + +Value OpSub(Value left, Value right) { + Value result; + if (IsInt(left)) { + result = MakeInt(ValueAsInt(left) - ValueAsInt(right)); + } else if (IsReal(left)) { + result = MakeReal(ValueAsReal(left) - ValueAsReal(right)); + } else { + fprintf(stderr, "fatal error: addition operator used on invalid type\n"); + exit(1); + } + return result; +} diff --git a/runtime/ToString.c b/runtime/ToString.c new file mode 100644 index 0000000..453d949 --- /dev/null +++ b/runtime/ToString.c @@ -0,0 +1,23 @@ +#include "runtime.h" + +Value ToString(Value val) { + Value retval; + if (IsString(val)) { + retval = val; + } else if (IsNil(val)) { + retval = MakeString("nil"); + } else if (IsBool(val)) { + retval = MakeString(IsTrue(val) ? "true" : "false"); + } else if (IsInt(val)) { + char str[64]; + snprintf(str, sizeof(str), "%d", ValueAsInt(val)); + retval = MakeString(str); + } else if (IsReal(val)) { + char str[64]; + snprintf(str, sizeof(str), "%f", ValueAsReal(val)); + retval = MakeString(str); + } else { + assert(!"could not convert string"); + } + return retval; +} diff --git a/runtime.c b/runtime/runtime.c similarity index 100% rename from runtime.c rename to runtime/runtime.c -- 2.52.0