1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 18:18:51 +03:00
mal/impls/c.2/step1_read_print.c
2021-05-12 09:32:29 -05:00

64 lines
1010 B
C

#include <stdio.h>
#include <stdlib.h>
#include <editline/readline.h>
#include <editline/history.h>
#include "types.h"
#include "reader.h"
#include "printer.h"
#define PROMPT_STRING "user> "
MalType* READ(char* str) {
return read_str(str);
}
MalType* EVAL(MalType* val) {
return val;
}
void PRINT(MalType* val) {
char* output = pr_str(val, READABLY);
printf("%s\n", output);
}
void rep(char* str) {
PRINT(EVAL(READ(str)));
}
int main(int argc, char** argv) {
/* Greeting message */
puts("Make-a-lisp version 0.0.2\n");
puts("Press Ctrl+d to exit\n");
while (1) {
/* print prompt and get input*/
/* readline allocates memory for input */
char* input = readline(PROMPT_STRING);
/* Check for EOF (Ctrl-D) */
if (!input) {
printf("\n");
return 0;
}
/* add input to history */
add_history(input);
/* call Read-Eval-Print */
rep(input);
/* have to release the memory used by readline */
free(input);
}
return 0;
}