(Somewhat adapted) code and solutions from the book "Build Your Own Lisp" http://www.buildyourownlisp.com
lisp
c

prompt.c 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* If we are on Windows compile these functions */
  4. #ifdef _WIN32
  5. #include <string.h>
  6. #include <assert.h>
  7. static char buffer[2048];
  8. /* Fake readline function */
  9. char* readline(char* prompt) {
  10. fputs(prompt, stdout);
  11. fgets(buffer, 2048, stdin);
  12. char* cpy = malloc(strlen(buffer)+1);
  13. assert(cpy != NULL)
  14. strcpy(cpy, buffer);
  15. cpy[strlen(cpy)-1] = '\0';
  16. return cpy;
  17. }
  18. /* Fake add_history function */
  19. void add_history(char* unused) {}
  20. /* Otherwise include the editline headers
  21. could use __APPLE__ for detection of OSX */
  22. #else
  23. #include <editline/readline.h>
  24. #endif
  25. int main(int argc, char const *argv[])
  26. {
  27. /* Print version and exit information */
  28. puts("Lispy version 0.0.0.0.1");
  29. puts("Press Ctrl+c to exit\n");
  30. /* In a never ending loop */
  31. while (1) {
  32. /* Output our prompt and get input */
  33. char* input = readline("lispy> ");
  34. /* Add input to history */
  35. add_history(input);
  36. /* Echo input back to user */
  37. printf("No you're a %s\n", input);
  38. /* Free retrieved input */
  39. free(input);
  40. }
  41. return 0;
  42. }