2024-11-18 09:54:27 +00:00
|
|
|
#include "../debug.h"
|
2024-11-02 11:18:38 +00:00
|
|
|
#include "cmd.h"
|
|
|
|
|
|
|
|
|
|
#include <blue/cmd.h>
|
2024-11-13 21:37:49 +00:00
|
|
|
#include <blue/term.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <ivy/file.h>
|
|
|
|
|
#include <ivy/lang/lex.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
ARG_SOURCE_FILE,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int compile_file(const char *path)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
FILE *fp = fopen(path, "r");
|
|
|
|
|
if (!fp) {
|
|
|
|
|
b_err("cannot open source file '%s'", path);
|
|
|
|
|
b_i("reason: %s", strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ivy_file *src = ivy_file_from_fp(fp);
|
2024-11-16 23:05:07 +00:00
|
|
|
struct ivy_lexer *lex;
|
|
|
|
|
if (ivy_lexer_create(&lex) != IVY_OK) {
|
2024-11-13 21:37:49 +00:00
|
|
|
b_err("failed to initialise Ivy lexer");
|
|
|
|
|
ivy_file_close(src);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 23:05:07 +00:00
|
|
|
ivy_lexer_set_source(lex, &src->f_base);
|
|
|
|
|
enum ivy_status status = IVY_OK;
|
2024-11-13 21:37:49 +00:00
|
|
|
|
|
|
|
|
while (true) {
|
2024-11-16 23:05:07 +00:00
|
|
|
struct ivy_token *tok = ivy_lexer_read(lex);
|
|
|
|
|
status = ivy_lexer_get_status(lex);
|
|
|
|
|
if (status == IVY_ERR_EOF) {
|
2024-11-13 21:37:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-16 23:05:07 +00:00
|
|
|
if (status != IVY_OK) {
|
2024-11-13 21:37:49 +00:00
|
|
|
b_err("failed to parse '%s'", path);
|
|
|
|
|
b_i("reason: lex error (%s)",
|
2024-11-16 23:05:07 +00:00
|
|
|
ivy_status_to_string(ivy_lexer_get_status(lex)));
|
2024-11-13 21:37:49 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 09:54:27 +00:00
|
|
|
print_lex_token(tok);
|
2024-11-13 21:37:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-16 23:05:07 +00:00
|
|
|
int r = (status == IVY_OK || status == IVY_ERR_EOF) ? 0 : -1;
|
2024-11-13 21:37:49 +00:00
|
|
|
ivy_file_close(src);
|
2024-11-16 23:05:07 +00:00
|
|
|
ivy_lexer_destroy(lex);
|
2024-11-13 21:37:49 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int compile(const b_command *cmd, const b_arglist *args, const b_array *_)
|
|
|
|
|
{
|
|
|
|
|
b_arglist_iterator it;
|
|
|
|
|
b_arglist_foreach_filtered(&it, args, B_COMMAND_INVALID_ID, ARG_SOURCE_FILE)
|
|
|
|
|
{
|
|
|
|
|
b_arglist_value *path = it.value;
|
|
|
|
|
if (path->val_type != B_COMMAND_ARG_STRING) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("%s\n", path->val_str);
|
|
|
|
|
int r = compile_file(path->val_str);
|
|
|
|
|
if (r != 0) {
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-11-02 11:18:38 +00:00
|
|
|
|
|
|
|
|
B_COMMAND(CMD_COMPILE, CMD_ROOT)
|
|
|
|
|
{
|
|
|
|
|
B_COMMAND_NAME("compile");
|
|
|
|
|
B_COMMAND_SHORT_NAME('C');
|
|
|
|
|
B_COMMAND_DESC(
|
|
|
|
|
"compile one or more Ivy source files into Ivy object files.");
|
|
|
|
|
B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT);
|
2024-11-13 21:37:49 +00:00
|
|
|
B_COMMAND_FUNCTION(compile);
|
|
|
|
|
|
|
|
|
|
B_COMMAND_ARG(ARG_SOURCE_FILE)
|
|
|
|
|
{
|
|
|
|
|
B_ARG_NAME("source file");
|
|
|
|
|
B_ARG_DESC("the .im source files to compile.");
|
|
|
|
|
B_ARG_NR_VALUES(B_ARG_1_OR_MORE_VALUES);
|
|
|
|
|
}
|
2024-11-02 11:18:38 +00:00
|
|
|
|
|
|
|
|
B_COMMAND_HELP_OPTION();
|
|
|
|
|
}
|