Files
Toy/repl/main.c
Kayne Ruse 09e4cb7b03 Squashed: Added integration and standalone tests, read more
This wasn't an easy fix, as it was primarily the test pipelines that
were failing. I resorted to using forced pushes to run the CI, to try
and track down the problems.

The primary cause seems to be the differences in how each supported
platform handles file paths, specifically, slash vs. backslash.

I've also added gdb scripts to set up automated breakpoints, and to run
operations on them to check for issues - the 'gdb_init' files are mostly
empty for the time being.

commit a34b0ff5d407bbe7d70ff9504aa035ec6fbecb7c
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Fri Oct 18 11:45:40 2024 +1100

    Restored the workflows

commit eb3d94f30d4dc4150139517f44cc874f2901124f
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Fri Oct 18 11:35:39 2024 +1100

    I think the library path on macos is fixed

commit 964572b5e93c7cb464686f19ddbe3e9d315f391b
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Fri Oct 18 11:22:56 2024 +1100

    I think the file paths are fixed

commit 1721f3da7252b4063f4347926e800ef4f7c9bf4c
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Thu Oct 17 15:57:28 2024 +1100

    Added standalone tests

commit 90c783f4059d88f4a7bbaf18215a9b414f3ab66f
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Thu Oct 17 15:01:18 2024 +1100

    Trying to fix the integration test pipeline

commit fccced1396568a55c1385e2f1b04fedf7c2585a5
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Wed Oct 16 00:31:39 2024 +1100

    Workflow integration tests are not passing

commit 6b1e0d1e0f89291e89768bf6102f4f7ed4581496
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Tue Oct 15 20:07:20 2024 +1100

    Fixed file paths in workflow

commit c0f1ec78fe79a5abb34c3e05308236cb18c23b97
Author: Kayne Ruse <kayneruse@gmail.com>
Date:   Tue Oct 15 19:46:14 2024 +1100

    Moved example scripts into proper integration tests

    Also adjusted makefiles to allow easy invoking of the tests.

    Adjusted and updated CI to invoke tests correctly.

    Fixed #141
2024-10-18 12:04:29 +11:00

507 lines
12 KiB
C

#include "toy.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//utilities
#define APPEND(dest, src) \
strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1);
#if defined(_WIN32) || defined(_WIN64)
#define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '/' ? '\\' : str[i];
#else
#define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '\\' ? '/' : str[i];
#endif
unsigned char* readFile(char* path, int* size) {
//BUGFIX: fix the path based on platform - it might be slower, but it's better than dealing with platform crap
int pathLength = strlen(path);
char realPath[pathLength + 1];
strncpy(realPath, path, pathLength);
realPath[pathLength] = '\0';
FLIPSLASH(realPath);
//open the file
FILE* file = fopen(path, "rb");
if (file == NULL) {
*size = -1; //missing file error
return NULL;
}
//determine the file's length
fseek(file, 0L, SEEK_END);
*size = ftell(file);
rewind(file);
//make some space
unsigned char* buffer = malloc(*size + 1);
if (buffer == NULL) {
fclose(file);
return NULL;
}
//read the file
if (fread(buffer, sizeof(unsigned char), *size, file) < *size) {
fclose(file);
*size = -2; //singal a read error
return NULL;
}
buffer[(*size)++] = '\0';
//clean up and return
fclose(file);
return buffer;
}
int getFilePath(char* dest, const char* src) {
char* p = NULL;
//find the last slash, regardless of platform
p = strrchr(src, '\\');
if (p == NULL) {
p = strrchr(src, '/');
}
if (p == NULL) {
int len = strlen(src);
strncpy(dest, src, len);
return len;
}
//determine length of the path
int len = p - src + 1;
//copy to the dest
strncpy(dest, src, len);
dest[len] = '\0';
return len;
}
int getFileName(char* dest, const char* src) {
char* p = NULL;
//find the last slash, regardless of platform
p = strrchr(src, '\\');
if (p == NULL) {
p = strrchr(src, '/');
}
if (p == NULL) {
int len = strlen(src);
strncpy(dest, src, len);
return len;
}
p++; //skip the slash
//determine length of the file name
int len = strlen(p);
//copy to the dest
strncpy(dest, p, len);
dest[len] = '\0';
return len;
}
//handle command line arguments
typedef struct CmdLine {
bool error;
bool help;
bool version;
char* infile;
int infileLength;
} CmdLine;
void usageCmdLine(int argc, const char* argv[]) {
printf("Usage: %s [ -h | -v | -f source.toy ]\n\n", argv[0]);
}
void helpCmdLine(int argc, const char* argv[]) {
usageCmdLine(argc, argv);
printf("The Toy Programming Language, leave arguments blank for an interactive REPL.\n\n");
printf(" -h, --help\t\t\tShow this help then exit.\n");
printf(" -v, --version\t\t\tShow version and copyright information then exit.\n");
printf(" -f, --file infile\t\tParse, compile and execute the source file then exit.\n");
}
void versionCmdLine(int argc, const char* argv[]) {
printf("The Toy Programming Language, Version %d.%d.%d %s\n\n", TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD);
//copy/pasted from the license file - there's a way to include it directly, but it's too finnicky to bother
const char* license = "\
Copyright (c) 2020-2024 Kayne Ruse, KR Game Studios\n\
\n\
This software is provided 'as-is', without any express or implied\n\
warranty. In no event will the authors be held liable for any damages\n\
arising from the use of this software.\n\
\n\
Permission is granted to anyone to use this software for any purpose,\n\
including commercial applications, and to alter it and redistribute it\n\
freely, subject to the following restrictions:\n\
\n\
1. The origin of this software must not be misrepresented; you must not\n\
claim that you wrote the original software. If you use this software\n\
in a product, an acknowledgment in the product documentation would be\n\
appreciated but is not required.\n\
2. Altered source versions must be plainly marked as such, and must not be\n\
misrepresented as being the original software.\n\
3. This notice may not be removed or altered from any source distribution.\n\n";
printf("%s",license);
}
CmdLine parseCmdLine(int argc, const char* argv[]) {
CmdLine cmd = { .error = false, .help = false, .version = false, .infile = NULL, .infileLength = 0 };
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
cmd.help = true;
}
else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
cmd.version = true;
}
else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")) {
if (argc < i + 1) {
cmd.error = true;
}
else {
if (cmd.infile != NULL) { //don't leak
free(cmd.infile);
}
i++;
//total space to reserve - it's actually longer than needed, due to the exe name being removed
cmd.infileLength = strlen(argv[0]) + strlen(argv[i]);
cmd.infile = malloc(cmd.infileLength + 1);
if (cmd.infile == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate space while parsing the command line, exiting\n" TOY_CC_RESET);
exit(-1);
}
getFilePath(cmd.infile, argv[0]);
APPEND(cmd.infile, argv[i]);
FLIPSLASH(cmd.infile);
}
}
else {
cmd.error = true;
}
}
return cmd;
}
//repl function
static void errorAndContinueCallback(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
int repl(const char* filepath) {
Toy_setErrorCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(errorAndContinueCallback);
//vars to use
char prompt[256];
getFileName(prompt, filepath);
unsigned int INPUT_BUFFER_SIZE = 4096;
char inputBuffer[INPUT_BUFFER_SIZE];
memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_VM vm;
Toy_initVM(&vm);
printf("%s> ", prompt); //shows the terminal prompt
//read from the terminal
while(fgets(inputBuffer, INPUT_BUFFER_SIZE, stdin)) {
//work around fgets() adding a newline
unsigned int length = strlen(inputBuffer);
if (inputBuffer[length - 1] == '\n') {
inputBuffer[--length] = '\0';
}
if (length == 0) {
printf("%s> ", prompt); //shows the terminal prompt
continue;
}
//end
if (strlen(inputBuffer) == 4 && (strncmp(inputBuffer, "exit", 4) == 0 || strncmp(inputBuffer, "quit", 4) == 0)) {
break;
}
//parse the input, prep the VM for run
Toy_Lexer lexer;
Toy_bindLexer(&lexer, inputBuffer);
Toy_Parser parser;
Toy_bindParser(&parser, &lexer);
Toy_Ast* ast = Toy_scanParser(&bucket, &parser); //Ast is in the bucket, so it doesn't need to be freed
//parsing error, retry
if (parser.error) {
printf("%s> ", prompt); //shows the terminal prompt
continue;
}
Toy_Bytecode bc = Toy_compileBytecode(ast);
Toy_bindVM(&vm, bc.ptr);
//run
Toy_runVM(&vm);
//free the bytecode, and leave the VM ready for the next loop
Toy_resetVM(&vm);
//count the bucket memory - hang on, this this garbage collection??
Toy_Bucket* iter = bucket;
int depth = 0;
while (iter->next) {
iter = iter->next;
if (++depth >= 7) {
Toy_freeBucket(&bucket);
bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
break;
}
}
printf("%s> ", prompt); //shows the terminal prompt
}
//cleanp all memory
Toy_freeVM(&vm);
Toy_freeBucket(&bucket);
return 0;
}
//debugging
static void debugStackPrint(Toy_Stack* stack) {
//DEBUG: if there's anything on the stack, print it
if (stack->count > 0) {
printf("Stack Dump\n\ntype\tvalue\n");
for (int i = 0; i < stack->count; i++) {
Toy_Value v = ((Toy_Value*)(stack + 1))[i];
printf("%d\t", v.type);
switch(v.type) {
case TOY_VALUE_NULL:
printf("null");
break;
case TOY_VALUE_BOOLEAN:
printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false");
break;
case TOY_VALUE_INTEGER:
printf("%d", TOY_VALUE_AS_INTEGER(v));
break;
case TOY_VALUE_FLOAT:
printf("%f", TOY_VALUE_AS_FLOAT(v));
break;
case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(v);
//print based on type
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
printf("%s", buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
printf("%s", str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
printf("%s", str->as.name.data);
}
break;
}
case TOY_VALUE_ARRAY:
case TOY_VALUE_DICTIONARY:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
printf("???");
break;
}
printf("\n");
}
}
}
static void debugScopePrint(Toy_Scope* scope, int depth) {
//DEBUG: if there's anything in the scope, print it
if (scope->table->count > 0) {
printf("Scope %d Dump\n\ntype\tname\tvalue\n", depth);
for (int i = 0; i < scope->table->capacity; i++) {
if ( (TOY_VALUE_IS_STRING(scope->table->data[i].key) && TOY_VALUE_AS_STRING(scope->table->data[i].key)->type == TOY_STRING_NAME) == false) {
continue;
}
Toy_Value k = scope->table->data[i].key;
Toy_Value v = scope->table->data[i].value;
printf("%d\t%s\t", v.type, TOY_VALUE_AS_STRING(k)->as.name.data);
switch(v.type) {
case TOY_VALUE_NULL:
printf("null");
break;
case TOY_VALUE_BOOLEAN:
printf("%s", TOY_VALUE_AS_BOOLEAN(v) ? "true" : "false");
break;
case TOY_VALUE_INTEGER:
printf("%d", TOY_VALUE_AS_INTEGER(v));
break;
case TOY_VALUE_FLOAT:
printf("%f", TOY_VALUE_AS_FLOAT(v));
break;
case TOY_VALUE_STRING: {
Toy_String* str = TOY_VALUE_AS_STRING(v);
//print based on type
if (str->type == TOY_STRING_NODE) {
char* buffer = Toy_getStringRawBuffer(str);
printf("%s", buffer);
free(buffer);
}
else if (str->type == TOY_STRING_LEAF) {
printf("%s", str->as.leaf.data);
}
else if (str->type == TOY_STRING_NAME) {
printf("%s\nWarning: The above value is a name string", str->as.name.data);
}
break;
}
case TOY_VALUE_ARRAY:
case TOY_VALUE_DICTIONARY:
case TOY_VALUE_FUNCTION:
case TOY_VALUE_OPAQUE:
printf("???");
break;
}
printf("\n");
}
}
if (scope->next != NULL) {
debugScopePrint(scope->next, depth + 1);
}
}
//callbacks
static void printCallback(const char* msg) {
fprintf(stdout, "%s\n", msg);
}
static void errorAndExitCallback(const char* msg) {
fprintf(stderr, "%s\n", msg);
exit(-1);
}
//main file
int main(int argc, const char* argv[]) {
Toy_setPrintCallback(printCallback);
Toy_setErrorCallback(errorAndExitCallback);
Toy_setAssertFailureCallback(errorAndExitCallback);
//repl
if (argc == 1) {
return repl(argv[0]);
}
//if there's args, process them
CmdLine cmd = parseCmdLine(argc, argv);
if (cmd.error) {
usageCmdLine(argc, argv);
}
else if (cmd.help) {
helpCmdLine(argc, argv);
}
else if (cmd.version) {
versionCmdLine(argc, argv);
}
else if (cmd.infile != NULL) {
//run the given file
int size;
unsigned char* source = readFile(cmd.infile, &size);
//check the file
if (source == NULL) {
if (size == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Could not parse an empty file '%s', exiting\n" TOY_CC_RESET, cmd.infile);
return -1;
}
else if (size == -1) {
fprintf(stderr, TOY_CC_ERROR "ERROR: File not found '%s', exiting\n" TOY_CC_RESET, cmd.infile);
return -1;
}
else {
fprintf(stderr, TOY_CC_ERROR "ERROR: Unknown error while reading file '%s', exiting\n" TOY_CC_RESET, cmd.infile);
return -1;
}
}
free(cmd.infile);
cmd.infile = NULL;
cmd.infileLength = 0;
Toy_Lexer lexer;
Toy_bindLexer(&lexer, (char*)source);
Toy_Parser parser;
Toy_bindParser(&parser, &lexer);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Ast* ast = Toy_scanParser(&bucket, &parser);
Toy_Bytecode bc = Toy_compileBytecode(ast);
//run the setup
Toy_VM vm;
Toy_initVM(&vm);
Toy_bindVM(&vm, bc.ptr);
//run
Toy_runVM(&vm);
//print the debug info
debugStackPrint(vm.stack);
debugScopePrint(vm.scope, 0);
//cleanup
Toy_freeVM(&vm);
Toy_freeBucket(&bucket);
free(source);
}
else {
usageCmdLine(argc, argv);
}
return 0;
}