123 lines
3.3 KiB
C
123 lines
3.3 KiB
C
/*
|
|
* This code is part of the programming language flup
|
|
* flup comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
|
|
* Copyright (C) 2024 VegOwOtenks
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include "../submodules/utilitiec/src/argumentc/argumentc.h"
|
|
#include "interpreter.h"
|
|
#include "tokenizer.h"
|
|
|
|
int tokenize_all(StringView source, DynamicArray* a)
|
|
{
|
|
Token t;
|
|
while ((t = Tokenizer_NextToken(&source)).type != TOKENTYPE_NONE) {
|
|
int append_code = DynamicArray_Append(a, &t);
|
|
if (append_code) return append_code;
|
|
if (t.type == TOKENTYPE_ERROR) break;
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
char* load_file_string(StringView path)
|
|
{
|
|
FILE* stream = fopen(path.source, "rb");
|
|
if (stream == NULL) {
|
|
fprintf(stderr, "Fatal Error: Failed to open file at %*s\n", (int) path.length, path.source);
|
|
return NULL;
|
|
}
|
|
|
|
if (fseek(stream, 0, SEEK_END)) {
|
|
perror("fseek");
|
|
return NULL;
|
|
}
|
|
|
|
long length = ftell(stream);
|
|
if (length == -1) {
|
|
perror("ftell");
|
|
return NULL;
|
|
}
|
|
|
|
if (fseek(stream, 0, SEEK_SET)) {
|
|
perror("fseek");
|
|
return NULL;
|
|
}
|
|
|
|
char* buffer = malloc(length + 1);
|
|
if (buffer == NULL) {
|
|
fprintf(stderr, "Fatal Error: Failed to allocate %li bytes\n", length);
|
|
return NULL;
|
|
}
|
|
|
|
size_t objects_read = fread(buffer, 1, length, stream);
|
|
if (objects_read != (size_t) length) {
|
|
fprintf(stderr, "Fatal Error: Failed read %li bytes from script file, got only %li\n", length, objects_read);
|
|
free(buffer);
|
|
return NULL;
|
|
}
|
|
|
|
fclose(stream);
|
|
|
|
buffer[length] = '\0';
|
|
|
|
return buffer;
|
|
}
|
|
|
|
int main(int argc, const char* argv [])
|
|
{
|
|
Argumentc arguments;
|
|
Argumentc_Create(&arguments, argc, argv);
|
|
|
|
Option script_file = Argumentc_PopLongArgument(&arguments, StringView_FromString("file")).argument;
|
|
if (script_file.type == OPTIONTYPE_NONE) {
|
|
fprintf(stderr, "Usage: [program] --file path/to/script_file\n");
|
|
return 1;
|
|
}
|
|
Argumentc_Destroy(&arguments);
|
|
|
|
char* script_string = load_file_string(script_file.content);
|
|
if (script_string == NULL) return 1;
|
|
|
|
StringView source = StringView_FromString(script_string);
|
|
DynamicArray tokens;
|
|
if (DynamicArray_Create(&tokens, sizeof(Token), 128, NULL)) {
|
|
fprintf(stderr, "Fatal Error: Failed to create dynamicarray\n");
|
|
return 1;
|
|
}
|
|
|
|
if (tokenize_all(source, &tokens)) {
|
|
fprintf(stderr, "Fatal Error: Out of Memory in tokenizing\n");
|
|
return 1;
|
|
}
|
|
|
|
Interpreter interpreter;
|
|
Interpreter_Create(&interpreter, &tokens);
|
|
|
|
|
|
int interpret_code = Interpreter_Interpret(&interpreter);
|
|
|
|
Interpreter_Destroy(&interpreter);
|
|
DynamicArray_Destroy(&tokens);
|
|
|
|
free(script_string);
|
|
|
|
return interpret_code;
|
|
}
|