1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00
mal/c/step1_read_print.c
Joel Martin dd7a4f55f3 Test uncaught throw, catchless try* . Fix 46 impls.
Fixes made to: ada, c, chuck, clojure, coffee, common-lisp, cpp,
crystal, d, dart, elm, erlang, es6, factor, fsharp, gnu-smalltalk,
groovy, guile, haxe, hy, js, livescript, matlab, miniMAL, nasm, nim,
objc, objpascal, ocaml, perl, perl6, php, plsql, ps, python, r,
rpython, ruby, scheme, swift3, tcl, ts, vb, vimscript, wasm, yorick.

Catchless try* test is an optional test. Not all implementations
support catchless try* but a number were fixed so they at least don't
crash on catchless try*.
2018-12-12 14:18:26 -06:00

87 lines
1.7 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "types.h"
#include "readline.h"
#include "reader.h"
// read
MalVal *READ(char prompt[], char *str) {
char *line;
MalVal *ast;
if (str) {
line = str;
} else {
line = _readline(prompt);
if (!line) {
_error("EOF");
return NULL;
}
}
ast = read_str(line);
if (!str) { MAL_GC_FREE(line); }
return ast;
}
// eval
MalVal *EVAL(MalVal *ast, GHashTable *env) {
if (!ast || mal_error) return NULL;
return ast;
}
// print
char *PRINT(MalVal *exp) {
if (mal_error) {
return NULL;
}
return _pr_str(exp,1);
}
// repl
// read and eval
MalVal *RE(GHashTable *env, char *prompt, char *str) {
MalVal *ast, *exp;
ast = READ(prompt, str);
if (!ast || mal_error) return NULL;
exp = EVAL(ast, env);
if (ast != exp) {
malval_free(ast); // Free input structure
}
return exp;
}
int main()
{
MalVal *exp;
char *output;
char prompt[100];
MAL_GC_SETUP();
// Set the initial prompt
snprintf(prompt, sizeof(prompt), "user> ");
// repl loop
for(;;) {
exp = RE(NULL, prompt, NULL);
if (mal_error && strcmp("EOF", mal_error->val.string) == 0) {
return 0;
}
output = PRINT(exp);
if (mal_error) {
fprintf(stderr, "Error: %s\n", _pr_str(mal_error,1));
malval_free(mal_error);
mal_error = NULL;
} else if (output) {
puts(output);
MAL_GC_FREE(output); // Free output string
}
//malval_free(exp); // Free evaluated expression
}
}