Commit 44e82a1d authored by Chris Müller's avatar Chris Müller
Browse files

implement first version for cherry interpreter command

parent 57f7466b
......@@ -19,7 +19,6 @@
#pragma once
#include "cherry/runtime.h"
#include "cherry/standard.h"
#include <stdlib.h>
#include <stdint.h>
......
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <readline/readline.h>
#include "cherry.h"
#include <gc.h>
#include "cherry/array.h"
#include "cherry/read.h"
int main(int argc, char** argv)
static void
print_usage(FILE* out, const char* prog) {
fprintf(out, "Usage: %s [options] [file]\n");
fprintf(out,
" -h --help Display this usage information.\n"
" -I --loadpath=[PATH] Specify loadpath for interpreter.\n"
" -M --main=[method] Specify method that is executed automatically.\n");
}
static const cy_byte_t*
get_text(const char* filename, const char* mode) {
FILE* file = fopen(filename, mode);
size_t filesize;
if(!file)
return 0;
fseek(file, 0, SEEK_END);
filesize = ftell(file);
rewind(file);
cy_byte_t* data = GC_MALLOC(filesize * sizeof(cy_byte_t));
fread(data, sizeof(cy_byte_t), filesize, file);
fclose(file);
return data;
}
static void
org_cherry_process_file(const char* filename, const cy_byte_t* method, struct org_cherry_value* arguments)
{
printf("Cherry Interpreter 0.1\n\n");
const cy_byte_t* src = get_text(filename, "rb");
org_cherry_initialize(0);
if(src == 0) {
fprintf(stderr, "cherry: couldn't load %s\n", filename);
exit(EXIT_FAILURE);
}
struct org_cherry_symbollist* env = org_cherry_symbollist();
struct org_cherry_context* context = org_cherry_context(src, src, CY_SUPRESS_COMMENTS);
struct org_cherry_value* exp = org_cherry_read(context);
while(exp != 0) {
org_cherry_eval(env, exp);
exp = org_cherry_read(context);
}
if(method) {
struct org_cherry_value* main = org_cherry_list_cons(org_cherry_symbol(method), arguments);
org_cherry_eval(env, main);
}
exit(EXIT_SUCCESS);
}
static void
org_cherry_start_repl(void)
{
printf("Cherry Interpreter 0.1\n\n");
struct org_cherry_symbollist* env = org_cherry_symbollist();
......@@ -22,8 +84,59 @@ int main(int argc, char** argv)
free(line);
}
}
int
main(int argc, char** argv)
{
static struct option options[] = {
{"help", no_argument, 0, 'h'},
{"loadpath", optional_argument, 0, 'I'},
{"main", optional_argument, 0, 'M'}
};
org_cherry_initialize(0);
int ch;
const char* filename = 0;
const cy_byte_t* method = 0;
struct org_cherry_ptrarray* load_path = org_cherry_ptrarray_new(4);
struct org_cherry_value* arguments = org_cherry_emptylist;
while( (ch = getopt_long(argc, argv, "hI:M:", options, 0)) != -1) {
switch(ch) {
case 'h':
print_usage(stdout, argv[0]);
exit(EXIT_SUCCESS);
case 'I':
org_cherry_ptrarray_append(load_path, optarg);
break;
case 'M':
method = optarg;
printf("%s\n", optarg);
break;
case '?':
print_usage(stderr, argv[0]);
exit(EXIT_FAILURE);
default:
fprintf(stderr, "???");
exit(EXIT_FAILURE);
}
}
if(optind < argc)
filename = argv[optind++];
while(optind < argc) {
arguments = org_cherry_list_cons(org_cherry_string(argv[optind++]), arguments);
}
printf("Goodbye\n");
if(filename)
org_cherry_process_file(filename, method, arguments);
else
org_cherry_start_repl();
return EXIT_SUCCESS;
}
......@@ -730,5 +730,5 @@ org_cherry_read(struct org_cherry_context* context)
}
}
return org_cherry_false;
return 0;
}
......@@ -222,7 +222,7 @@ print_usage(FILE* stream, const char* programm, int exit_code)
fprintf(stream,
" -h --help Display this usage information for your testcase.\n"
" -c --colors Enable color support for test output.\n"
" -v --verbose Show more details about the testing progress.\n"
" -v --verbose Show more details about the testing progress.\n"
" -r --repeat=INT Set number of repeats for each testcase.\n"
" -a --allow Allow stdout output for test files.\n"
" -f --filter=PATTERN Set a regular expression filter for all testcases.\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment