summaryrefslogtreecommitdiff
path: root/main.c
blob: e6f6f9777da69cb238dce589e7346dd2ac1d61e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "lisp.h"
#include "lex.yy.h"
#include "lisp.tab.h"

env *global_env = NULL;

void eval_file(const char *filename) {
	FILE *f = fopen(filename, "r");
	if (!f) {
		fprintf(stderr, "could not open file: %s\n", filename);
		return;
	}

	int parens = 0;
	char line[1024];
	char buffer[4096] = "";
	while (fgets(line, sizeof(line), f)) {
		if (*line == '\n' || *line == ';') continue; // skip empty lines or comments
		for (char *c = line; *c != '\0'; c++) {
			if (*c == '(') {
				parens++;
			} else if (*c == ')') {
				parens--;
			}
		}
		//append line to buffer
		strcat(buffer, line);
		strcat(buffer, "\n");
		if (parens <= 0) {
			//if parens are balanced: parse
			yy_scan_string(buffer);
			yyparse();
			//reset buffer
			buffer[0] = '\0';
		}
	}
	fclose(f);
}

char *env_token_gen(const char *text, int state) {
	static int len;
	static env *nth_env;
	if (state == 0) {
		nth_env = global_env->next;
		len = strlen(text);
	}

	while (nth_env) {
		const char *cur = nth_env->symbol;
		nth_env = nth_env->next;

		if (strncmp(cur, text, len) == 0) {
			return strdup(cur);
		}
	}

	return NULL;
}

/* Dispatch function: called by readline to get all matches */
char **my_completion(const char *text, int start, int end) {
    /* Prevent default filename completion */
	rl_attempted_completion_over = 1;
	return rl_completion_matches(text, env_token_gen);
}

void init_readline() {
	rl_attempted_completion_function = my_completion;
}

int main(int argc, char **argv) {
	// init global env
	global_env = env_create(NULL);
	env_define(global_env, "cons", make_builtin(builtin_cons));
	env_define(global_env, "car", make_builtin(builtin_car));
	env_define(global_env, "cdr", make_builtin(builtin_cdr));
	env_define(global_env, "reverse", make_builtin(builtin_reverse));
	env_define(global_env, "null?", make_builtin(builtin_isnull));
	env_define(global_env, "equal?", make_builtin(builtin_equal));
	env_define(global_env, "+", make_builtin(builtin_add));
	env_define(global_env, "-", make_builtin(builtin_sub));
	env_define(global_env, "*", make_builtin(builtin_mul));
	env_define(global_env, "/", make_builtin(builtin_div));
	env_define(global_env, "<", make_builtin(builtin_lt));

	// evaluate files
	for(int i = 1; i < argc; i++) {
		eval_file(argv[i]);
	}

	// START REPL
	init_readline();

	int parens = 0;
	char *line;
	char buffer[4096] = "";
	char *prompt = "λ> ";
	while ((line = readline(prompt)) != NULL) {
		for (char *c = line; *c != '\0'; c++) {
			if (*c == '(') {
				parens++;
			} else if (*c == ')') {
				parens--;
			}
		}
		//append line to buffer
		strcat(buffer, line);
		if (parens <= 0) {
			//if parens are balanced parse
			yy_scan_string(buffer);
			yyparse();
			add_history(buffer);
			//reset buffer and prompt string
			buffer[0] = '\0';
			prompt = "λ> ";
		} else {
			strcat(buffer, "\n");
			prompt = "   ";
		}
		free(line);
	}
	return 0;
}