X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=templates%2Fprogram.c;h=ecabb1758c1c5971b78265890884d5992a7964ad;hp=d027c52508617e57acd7313ff21f4a481a95f193;hb=c40954b865b48ffa4993f735d56213fb91c3e90e;hpb=2b8b0fd7071b4a829253e7564289aa179b9a47d9 diff --git a/templates/program.c b/templates/program.c index d027c52..ecabb17 100644 --- a/templates/program.c +++ b/templates/program.c @@ -1,3 +1,6 @@ +#include +#include +#include #include #include @@ -7,6 +10,12 @@ struct String; typedef struct String String; +enum Type; +typedef enum Type Type; +union Instance; +typedef union Instance Instance; +struct Object; +typedef struct Object Object; struct Runtime; typedef struct Runtime Runtime; @@ -16,6 +25,24 @@ struct String char* characters; }; +enum Type +{ + INTEGER, + STRING +}; + +union Instance +{ + int32_t integer; + String* string; +}; + +struct Object +{ + Type type; + Instance instance; +}; + struct Runtime { size_t permanentStringsLength; @@ -64,21 +91,45 @@ void Runtime_addPermanentString(Runtime* self, String* string) self->permanentStringsLength++; } -String* stringLiteral(Runtime* runtime, const char* literal) +Object integerLiteral(int32_t literal) { - String* result = malloc(sizeof(String)); - result->length = strlen(literal); - result->characters = malloc(result->length); - memcpy(result->characters, literal, result->length); - Runtime_addPermanentString(runtime, result); + Object result; + result.type = INTEGER; + result.instance.integer = literal; + return result; +} + +Object stringLiteral(Runtime* runtime, const char* literal) +{ + String* resultString = malloc(sizeof(String)); + resultString->length = strlen(literal); + resultString->characters = malloc(resultString->length); + memcpy(resultString->characters, literal, resultString->length); + Runtime_addPermanentString(runtime, resultString); + + Object result; + result.type = STRING; + result.instance.string = resultString; return result; } {% if 'print' in builtins %} -void builtin$print(String* output) +void builtin$print(Object output) { - // Using fwrite instead of printf to handle size_t length - fwrite(output->characters, 1, output->length, stdout); + switch(output.type) + { + case INTEGER: + printf("%" PRId32, output.instance.integer); + break; + + case STRING: + // Using fwrite instead of printf to handle size_t length + fwrite(output.instance.string->characters, 1, output.instance.string->length, stdout); + break; + + default: + assert(false); + } } {% endif %}