[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-apl] Near Proof of concept of an )edit somefunction_name

From: Christian Robert
Subject: [Bug-apl] Near Proof of concept of an )edit somefunction_name
Date: Fri, 18 Mar 2016 23:53:03 -0400
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0

When I joined this address@hidden ~two years ago, my first complains the lack 

      )edit function_name

well, with ./configure --with-libapl I can now near do that.

address@hidden:/home/xtian/libapl] $ ls -la
total 16
drwxrwxr-x.  2 xtian xtian   35 18 mar 23:34 ./
drwx------. 26 xtian xtian 4096 18 mar 22:28 ../
-rwxrw-r--.  1 xtian xtian   80 18 mar 21:31 compile*
-rw-rw-r--.  1 xtian xtian 4167 18 mar 23:18 test.cpp

address@hidden:/home/xtian/libapl] $ cat compile

gcc -g -Wall -o test test.cpp -lstdc++ -L/usr/local/lib/apl -lapl

address@hidden:/home/xtian/libapl] $ cat test.cpp
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

#include <apl/libapl.h>

#define TEMPFILE "∆edit∆"

static int always_print (const APL_value apl, int committed)
  return 1;

static int save_to_file (const APL_value apl, int committed)
  FILE *out = fopen (TEMPFILE, "w");

  int y = get_axis (apl,0);
  int x = get_axis (apl,1);

  if (get_rank(apl)==2)  // a matrix
    char *L = (char *) malloc (7*x+1);
    char *cx;
    int  length;

    // Write each line to out, removing trailing blanks

    for (int line=0 ; line < y ; ++line)
      cx = L;
      *cx = '\0';

      for (int col=0 ; col<x ; ++col)
        int ravel = (line*x)+col;

        // retrieve that cell as an int

        if (is_char (apl, ravel))
          int Unicode = get_char (apl, ravel);

          // convert it to utf8 into (*cx)

          Unicode_to_UTF8 (Unicode, cx, &length);

          cx += length;

      // Truncate trailings blanks if any

      if (cx > L)
        for ( cx -= 1 ; *cx && *cx == ' ' ; --cx);

        *(cx+1) = '\0';

      // write the line to the file

      fprintf (out, "%s\n", L);


    free (L);

  fclose (out);

  return 0; // Don't print

int main (void)
  extern result_callback res_callback;

  res_callback = always_print;

  setlocale(LC_ALL, "en_CA.UTF8");

  apl_exec (")libs");
  apl_exec (")libs 0 .");
  apl_exec (")wsid");
  apl_exec (")libs");
  apl_exec ("⎕SYL[7;]");

  apl_exec (")load dump.apl");
  apl_exec ("⎕pw←1000");
  apl_exec ("'⎕io is' ⎕io");
  apl_exec ("'⍳10 is' (⍳10)");
  apl_exec ("D←{24 ⎕cr ⍵}");

  apl_exec (")wsid dump");

  apl_exec ("⎕fx 'z←a IF b' 'z←(0=↑b)/a'");

  // Here I will try to catch the result from monadic ⎕cr and print it into a 
file as utf8 ...

  res_callback = save_to_file;
  apl_exec ("⎕cr 'IF'");
  res_callback = always_print;

  // Now, let the user edit and save the file ...

    char cmd[512];
    const char *EDITOR = getenv("EDITOR");

    if (EDITOR == NULL)
      EDITOR = "/usr/bin/vi";  // set the default editor. Can be nano, pico or 
even emacs

    snprintf (cmd, sizeof(cmd), "%s \"%s\"", EDITOR, TEMPFILE);
    system (cmd);  // Let the user edit the file

  // Here I will read back the file and save it (eg: ⎕fx) into the current 

  apl_command (")reset");   // Be sure the stack is empty so ⎕fx won't fail if 
the syntax is OK but )SI is not empty

    FILE *in = fopen (TEMPFILE, "r");
    char line[8192];                                         // Each line MAX 
of input
    int  Unicode[8192*32];                                   // Max number of 
Unicode to make a whole function (256K characters)
    int *U;                                                  // to navigate 
into Unicode
    int MAX_Unicode = sizeof(Unicode)/sizeof(Unicode[0]);    // Max size of a 
whole program/function (arbitrary value, need algo fix)
    int CUR_Unicode = 0;                                     // Cur used 

    while (NULL != fgets (line, sizeof(line), in))
      // *TODO*

      // well, line has something in, hopefulley terminated by "\n\0", let's 
check for that.

      // Convert each UTF8 multichar to one Unicode 32 bits int
      // using: extern int UTF8_to_Unicode(const char * utf, int * length);

      // Append that Unicode to an internal array vector (large enough 
hopefully) always check boundary


    // Now ⎕FX the result, well, I may need ⎕ucs here to translate from my own 
integers to APL string
    // ⎕FX is intelligent enough to split lines on the '\n', but not enough 
    // to not add a blank line at the end of the last function line if it end 
by '\n'.
    // So, remove it if present.

    fclose (in);

  apl_exec (")dump");   // Save current workspace in DUMP format

  res_callback = NULL;

  apl_exec (")off");

  return 0;

I don't see why this can't be included into the official apl executable.

we only need an

   )edit function

who will invoke $EDITOR or /usr/bin/vi

and read the result back, and QUAD-FX it.

it can be done, I think.

my 2 cents,


reply via email to

[Prev in Thread] Current Thread [Next in Thread]