~bouncepaw/agidel

b669d0d51fb192d6ab4743bc9aa911a72dd39054 — Timur Ismagilov 5 years ago 31d53da
Add enquote syntrans
1 files changed, 64 insertions(+), 0 deletions(-)

A enquote.c
A enquote.c => enquote.c +64 -0
@@ 0,0 1,64 @@
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>

enum State { LIST, STRING, ESCAPING, SYMBOL} state = LIST;
int nestlvl = 0;
char ch;

int main() {
  while(read(STDIN_FILENO, &ch, 1) > 0) {
    switch (state) {
    case ESCAPING:
      putchar(ch);
      state = STRING;
      break;
    case STRING:
      switch (ch) {
      case '\\':
        state = ESCAPING;
        putchar(ch);
        break;
      case '\"':
        state = LIST;
        putchar(ch);
        break;
      default:
        putchar(ch);
        break;
      }
      break;

    case SYMBOL:
      if (isspace(ch)) state = LIST;
      putchar(ch);
      break;

    case LIST:
      switch (ch) {
      case '(':
        nestlvl++;
        putchar(ch);
        break;
      case ')':
        nestlvl--;
        putchar(ch);
        break;
      case '\"':
        state = STRING;
        putchar(ch);
        break;
      default:
        if (isspace(ch)) {
          putchar(ch);
        } else {
          state = SYMBOL;
          printf("'%c", ch);
        }
      }
    }
  }

  return 0;
}