~bouncepaw/agidel

632d99ede8dd76f53ab935c8663604b8fae9ef24 — Timur Ismagilov 5 years ago f108e3a
Now Agidel is done with Agidel/sh
4 files changed, 122 insertions(+), 4 deletions(-)

M agidel
A agidel.lisp
A disbrace.c
A disbracket.c
M agidel => agidel +1 -4
@@ 1,12 1,9 @@
#!/bin/sh
filename=$1
shift
csi_args="-batch -quiet "
for plugin in "$@"
do
    csi_args+="-require-extension agidel-plugin.$plugin "
done

tmp="(begin $(cat $filename))"
csi $csi_args -e "(begin $(cat $filename))"
csi $csi_args -e "(display (string-append $(cat /dev/stdin)))"
echo

A agidel.lisp => agidel.lisp +7 -0
@@ 0,0 1,7 @@
(shebang!)
(set 'csi_args "-batch -quiet ")
(for-each-cli-arg
 'plugin
 (set 'csi_args + "-require-extension agidel-plugin.$plugin "))
['csi '$csi_args '-e "(display (string-append $(cat /dev/stdin)))"]
['echo]

A disbrace.c => disbrace.c +57 -0
@@ 0,0 1,57 @@
// disbrace.c
//  Part of Agidel project.
//
//  This simple program reads Agidel source code, finds any
//  brace-enclosed expression, converts them to their true form and
//  prints converted source code to stdout.
// 
//  {a b c} → (_brace a b c)

#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>

bool in_string, escaping_char;
// Counter of opened [] pairs which are not closed yet.
int pairs_in_search;

int main() {
  char ch;
  while(read(STDIN_FILENO, &ch, 1) > 0) {
    if (in_string) {
      if (escaping_char) {
        printf("\%c", ch);
        escaping_char = false;
      } else {
        if (ch == '\\') {
          escaping_char = true;
        } else if (ch == '"') {
          printf("%c", ch);
          in_string = false;
        } else {
          printf("%c", ch);
        }
      }
    } else {
      if (ch == '{') {
        printf("(_brace ");
        pairs_in_search++;
      } else if (ch == '}') {
        printf(")");
        pairs_in_search--;
      } else if (ch == '"') {
        in_string = true;
        printf("%c", ch);
      } else {
        printf("%c", ch);
      }
    }
  }

  if (pairs_in_search != 0) {
    fprintf(stderr, "Agidel: %d unmatched brace pairs.", pairs_in_search);
    return 1;
  }

  return 0;
}

A disbracket.c => disbracket.c +57 -0
@@ 0,0 1,57 @@
// disbracket.c
//  Part of Agidel project.
//
//  This simple program reads Agidel source code, finds any
//  bracket-enclosed expression, converts them to their true form and
//  prints converted source code to stdout.
// 
//  [a b c] → (_bracket a b c)

#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>

bool in_string, escaping_char;
// Counter of opened [] pairs which are not closed yet.
int pairs_in_search;

int main() {
  char ch;
  while(read(STDIN_FILENO, &ch, 1) > 0) {
    if (in_string) {
      if (escaping_char) {
        printf("\%c", ch);
        escaping_char = false;
      } else {
        if (ch == '\\') {
          escaping_char = true;
        } else if (ch == '"') {
          printf("%c", ch);
          in_string = false;
        } else {
          printf("%c", ch);
        }
      }
    } else {
      if (ch == '[') {
        printf("(_bracket ");
        pairs_in_search++;
      } else if (ch == ']') {
        printf(")");
        pairs_in_search--;
      } else if (ch == '"') {
        in_string = true;
        printf("%c", ch);
      } else {
        printf("%c", ch);
      }
    }
  }

  if (pairs_in_search != 0) {
    fprintf(stderr, "Agidel: %d unmatched bracket pairs.", pairs_in_search);
    return 1;
  }

  return 0;
}