main: rearrange code and setup prototypes

This commit is contained in:
2026-02-11 08:07:06 +00:00
committed by oreodave
parent eca46069f8
commit e772b06ae5

View File

@@ -11,14 +11,58 @@
#include <alisp/alisp.h> #include <alisp/alisp.h>
void usage(FILE *fp) void usage(FILE *fp);
int init_stream_on_args(int argc, char *argv[], FILE **pipe, stream_t *stream);
int main(int argc, char *argv[])
{ {
fprintf(fp, "Usage: alisp [OPTIONS...] FILE\n" int ret = 0;
"Options:\n" FILE *pipe = NULL;
"\t--help Print this usage and exit.\n" stream_t stream = {0};
"File:\n" vec_t ast = {0};
"\t<filename> Read and interpret this file from filesystem.\n" sys_t sys = {0};
"\t-- Read and interpret from stdin using an EOF.\n");
ret = init_stream_on_args(argc, argv, &pipe, &stream);
if (ret)
goto end;
LOG("[INFO]: Initialised stream for `%s`\n", stream.name);
{
read_err_t err = read_all(&sys, &stream, &ast);
if (err)
{
u64 line = 0, col = 0;
stream_line_col(&stream, &line, &col);
fprintf(stderr, "%s:%lu:%lu: ERROR: %s\n", stream.name, line, col,
read_err_to_cstr(err));
ret = 1;
goto end;
}
}
LOG("[INFO]: Utilised %lu bytes in parsing\n", sys_cost(&sys));
LOG("[INFO]: Parsed %lu %s\n", VEC_SIZE(&ast, lisp_t *),
VEC_SIZE(&ast, lisp_t *) == 1 ? "expr" : "exprs");
{
for (u64 i = 0; i < VEC_SIZE(&ast, lisp_t *); ++i)
{
lisp_t *expr = VEC_GET(&ast, i, lisp_t *);
#if VERBOSE_LOGS
printf("\t[%lu]: ", i);
lisp_print(stdout, expr);
printf("\n");
#endif
}
}
end:
sys_free(&sys);
vec_free(&ast);
stream_free(&stream);
if (pipe)
fclose(pipe);
return ret;
} }
int init_stream_on_args(int argc, char *argv[], FILE **pipe, stream_t *stream) int init_stream_on_args(int argc, char *argv[], FILE **pipe, stream_t *stream)
@@ -63,53 +107,14 @@ int init_stream_on_args(int argc, char *argv[], FILE **pipe, stream_t *stream)
return 0; return 0;
} }
int main(int argc, char *argv[]) void usage(FILE *fp)
{ {
int ret = 0; fprintf(fp, "Usage: alisp [OPTIONS...] FILE\n"
FILE *pipe = NULL; "Options:\n"
stream_t stream = {0}; "\t--help Print this usage and exit.\n"
vec_t ast = {0}; "File:\n"
sys_t sys = {0}; "\t<filename> Read and interpret this file from filesystem.\n"
"\t-- Read and interpret from stdin using an EOF.\n");
ret = init_stream_on_args(argc, argv, &pipe, &stream);
if (ret)
goto end;
LOG("[INFO]: Initialised stream for `%s`\n", stream.name);
{
read_err_t err = read_all(&sys, &stream, &ast);
if (err)
{
u64 line = 0, col = 0;
stream_line_col(&stream, &line, &col);
fprintf(stderr, "%s:%lu:%lu: ERROR: %s\n", stream.name, line, col,
read_err_to_cstr(err));
ret = 1;
goto end;
}
}
LOG("[INFO]: Utilised %lu bytes in parsing\n", sys_cost(&sys));
LOG("[INFO]: Parsed %lu %s\n", VEC_SIZE(&ast, lisp_t *),
VEC_SIZE(&ast, lisp_t *) == 1 ? "expr" : "exprs");
{
for (u64 i = 0; i < VEC_SIZE(&ast, lisp_t *); ++i)
{
lisp_t *expr = VEC_GET(&ast, i, lisp_t *);
LOG("\t[%lu]: ", i);
lisp_print(stdout, expr);
printf("\n");
}
}
end:
sys_free(&sys);
vec_free(&ast);
stream_free(&stream);
if (pipe)
fclose(pipe);
return ret;
} }
/* Copyright (C) 2025, 2026 Aryadev Chavali /* Copyright (C) 2025, 2026 Aryadev Chavali