Trying to build a forth runtime in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
forth/src/main.c

89 lines
2.1 KiB

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/limits.h>
#include "forth.h"
int main(int argc, char *argv[])
{
fh_globals.verbose = false;
fh_globals.interactive = isatty(STDIN_FILENO);
fh_globals.echo = 0;
FILE *infile = stdin;
char pwd[PATH_MAX+1];
getcwd(pwd, PATH_MAX);
// TODO use getopt
for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-' && argv[a][1] != '-') {
// opt
char *cc = argv[a] + 1;
char c;
while (0 != (c = *cc++)) {
switch (c) {
case 'v':
fh_globals.verbose = 1;
break;
case 'e':
fh_globals.echo = 1;
break;
case 'r':
fh_globals.rescue = 1;
break;
case 'h':
printf("forth runtime, written by Ondřej Hruška, Nov 2021\n");
printf("Arguments:\n");
printf("-v verbose logging\n");
printf("-e echo in batched mode\n");
printf("-r rescue on error in batched mode\n");
printf("-h this, duh\n");
printf("FILE file to interpret, stdin console if not given\n");
return 0;
default:
LOGE("Unknown flag: %c", c);
return 1;
}
}
} else {
infile = fopen(argv[a], "r");
fh_globals.interactive = false;
if (!infile) {
LOGE("Error opening infile: %s", argv[a]);
return 1;
}
realpath(argv[a], pwd);
char *end = strrchr(pwd, '/');
if (end) {
// add terminator
*end = 0;
}
}
}
enum fh_error rv;
struct fh_thread_s fh;
rv = fh_init(&fh);
if (rv != FH_OK) {
LOGE("Error in forth init: %s", fherr_name(rv));
return 1;
}
fh_runtime_start(&fh, fh_create_input_from_filestruct(infile, pwd));
// Show resource usage
LOG("\nResources used: DS %dW, RS %dW, memory %dB\n",
(int) fh.data_stack_hwm, (int) fh.return_stack_hwm,
(int) fh.here);
if (fh_globals.interactive) {
FHPRINT_SVC("Bye.\n");
}
return 0;
}