pocketlang/cli/main.c

103 lines
2.6 KiB
C
Raw Normal View History

2021-02-07 15:40:00 +08:00
/*
* Copyright (c) 2021 Thakee Nathees
* Licensed under: MIT License
*/
2021-02-17 02:28:03 +08:00
#include <stdlib.h>
2021-02-07 15:40:00 +08:00
#include <stdio.h>
2021-05-09 18:28:00 +08:00
#include "pocketlang.h"
2021-02-08 02:30:29 +08:00
2021-05-09 18:28:00 +08:00
void errorPrint(PKVM* vm, PKErrorType type, const char* file, int line,
2021-04-26 17:34:30 +08:00
const char* message) {
if (type == PK_ERROR_COMPILE) {
fprintf(stderr, "Error: %s\n\tat %s:%i\n", message, file, line);
} else if (type == PK_ERROR_RUNTIME) {
fprintf(stderr, "Error: %s\n", message);
} else if (type == PK_ERROR_STACKTRACE) {
fprintf(stderr, " [%s:%i] %s()\n", file, line, message);
}
2021-02-08 02:30:29 +08:00
}
2021-05-09 18:28:00 +08:00
void writeFunction(PKVM* vm, const char* text) {
2021-02-12 01:35:43 +08:00
fprintf(stdout, "%s", text);
2021-02-08 02:30:29 +08:00
}
2021-05-07 15:21:34 +08:00
// FIXME:
2021-05-09 18:28:00 +08:00
void onResultDone(PKVM* vm, pkStringResult result) {
2021-05-07 15:21:34 +08:00
// // The result.string is the allocated buffer and it has to be freed
// // manually since it wasn't allocated by the VM.
// free((void*)result.string);
2021-02-08 02:30:29 +08:00
}
2021-05-07 15:21:34 +08:00
// FIXME:
2021-05-09 18:28:00 +08:00
pkStringResult resolvePath(PKVM* vm, const char* from, const char* name) {
2021-05-07 15:21:34 +08:00
if (from == NULL) {
// TODO: name is the complete path.
}
2021-05-09 18:28:00 +08:00
pkStringResult result;
2021-05-06 22:19:30 +08:00
result.success = true;
result.on_done = onResultDone;
2021-05-07 15:21:34 +08:00
result.string = name;
2021-05-06 22:19:30 +08:00
return result;
}
2021-05-09 18:28:00 +08:00
pkStringResult loadScript(PKVM* vm, const char* path) {
pkStringResult result;
2021-05-06 22:19:30 +08:00
result.success = true;
result.on_done = onResultDone;
2021-02-12 01:35:43 +08:00
// Open the file.
FILE* file = fopen(path, "r");
if (file == NULL) {
2021-05-07 17:41:19 +08:00
result.success = false;
2021-02-12 01:35:43 +08:00
return result;
}
// Get the source length.
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
// Read source to buffer.
char* buff = (char*)malloc(file_size + 1);
size_t read = fread(buff, sizeof(char), file_size, file);
// Using read instead of file_size is because "\r\n" is read as '\n' in
// windows the '\r'.
buff[read] = '\0';
fclose(file);
2021-05-06 22:19:30 +08:00
result.string = buff;
2021-02-12 01:35:43 +08:00
return result;
2021-02-08 02:30:29 +08:00
}
2021-02-12 01:35:43 +08:00
int main(int argc, char** argv) {
2021-02-15 20:49:19 +08:00
const char* notice =
2021-05-09 18:28:00 +08:00
"MiniScript " PK_VERSION_STRING " (https://github.com/ThakeeNathees/miniscript/)\n"
2021-02-15 20:49:19 +08:00
"Copyright(c) 2020 - 2021 ThakeeNathees.\n"
"Free and open source software under the terms of the MIT license.\n";
const char* help = "Usage: miniscript <source_path>\n";
if (argc < 2) {
printf("%s\n%s", notice, help);
return 0;
}
2021-05-07 17:41:19 +08:00
2021-02-15 20:49:19 +08:00
const char* source_path = argv[1];
2021-05-09 18:28:00 +08:00
pkConfiguration config;
pkInitConfiguration(&config);
2021-02-12 01:35:43 +08:00
config.error_fn = errorPrint;
config.write_fn = writeFunction;
config.load_script_fn = loadScript;
2021-05-07 17:41:19 +08:00
config.resolve_path_fn = resolvePath;
2021-02-12 01:35:43 +08:00
2021-05-09 18:28:00 +08:00
PKVM* vm = pkNewVM(&config);
PKInterpretResult result = pkInterpret(vm, source_path);
pkFreeVM(vm);
2021-02-15 20:49:19 +08:00
return result;
2021-02-07 15:40:00 +08:00
}