Move files into separate folders for ease of reading, include source directory so we can use angle bracket includes, adjust build system to make directories for objects
103 lines
2.4 KiB
C
103 lines
2.4 KiB
C
/* Copyright (C) 2025 Aryadev Chavali
|
|
|
|
* 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 Version 2 for
|
|
* details.
|
|
|
|
* You may distribute and modify this code under the terms of the GNU General
|
|
* Public License Version 2, which you should have received a copy of along with
|
|
* this program. If not, please go to <https://www.gnu.org/licenses/>.
|
|
|
|
* Created: 2025-04-05
|
|
* Description: Entrypoint
|
|
*/
|
|
|
|
#include <lib/sv.h>
|
|
#include <lib/vec.h>
|
|
#include <lisp/lisp.h>
|
|
#include <lisp/reader.h>
|
|
|
|
#include <assert.h>
|
|
#include <ctype.h>
|
|
#include <malloc.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
void usage(char *prog_name, FILE *fp)
|
|
{
|
|
fprintf(fp,
|
|
"Usage: %s [FILE]\n"
|
|
"Interpret FILE as scheme in the OATS interpreter.\n"
|
|
"\t[FILE]: name of file\n",
|
|
prog_name);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int exit = 0;
|
|
context_t ctx = {0};
|
|
const char *filename = NULL;
|
|
#if 1
|
|
if (argc > 1)
|
|
filename = argv[1];
|
|
else
|
|
{
|
|
usage(argv[0], stderr);
|
|
return 1;
|
|
}
|
|
|
|
FILE *fp = fopen(filename, "r");
|
|
if (!fp)
|
|
{
|
|
fprintf(stderr, "[ERROR]: File `%s` does not exist\n", filename);
|
|
exit = 1;
|
|
goto end;
|
|
}
|
|
input_t inp = {0};
|
|
input_from_fp(&ctx, &inp, filename, fp);
|
|
fclose(fp);
|
|
#else
|
|
filename = "<example>";
|
|
char inp_data[] = "(print (+ 34 35))";
|
|
input_t inp = {0};
|
|
sv_t inp_sv = SV(inp_data, sizeof(inp_data));
|
|
input_from_sv(&ctx, &inp, filename, inp_sv);
|
|
#endif
|
|
|
|
if (inp.str.size == 0)
|
|
{
|
|
info("[WARNING] `%s` is empty.\n", filename);
|
|
goto end;
|
|
}
|
|
debug("[file read]: %luB read from `%s`\n", inp.str.size, filename);
|
|
|
|
// Setup a vector to hold all the lisps
|
|
vec_t results = {0};
|
|
perr_t perr = parse_all(&ctx, &inp, &results);
|
|
if (perr)
|
|
{
|
|
exit = perr;
|
|
print_perror(stderr, &inp, perr);
|
|
goto end;
|
|
}
|
|
context_reset_read(&ctx);
|
|
|
|
lisp_t **lisps = (lisp_t **)results.data;
|
|
u64 size = results.size / sizeof(*lisps);
|
|
for (u64 i = 0; i < size; ++i)
|
|
{
|
|
lisp_t *lisp = lisps[i];
|
|
// printf("tag=%x\n", tag_get(lisp));
|
|
sv_t serialised = serialise(&ctx, lisp);
|
|
info("lisp[%lu]: %p => " PR_SV "\n", i, lisp, SV_FMT(serialised));
|
|
}
|
|
context_report(&ctx);
|
|
|
|
end:
|
|
|
|
context_cleanup(&ctx);
|
|
return exit;
|
|
}
|