Friday, November 2, 2018

Simple macro expander

This is backed by by a macro table of symbol expansions. If it finds the '$', then it immediately expands the macro right there in the argument list. Because it is recursive, it will expand any macros that are part of another macro. Works fin along with the symbol table, and good enough for building expressive control over cursors and matching inside join. It generates an argument list the same way c programs present argument list in the main routine, an array of arg pointers. So when I call a command, all the macros have already been expanded.

It can look for the assignment operator  '=', and output from a command is treated like a macro definition, placed in the table.  If their is no '$', it default to $Console, a built in macro.  Then $Console prints the result of the last command.  Very simple and good enough for the lab.

I have the freedom to develop commandlets for managing the join process knowing they can easily be transformed into standard shell macros. All the attachments, except the few core, will likely be DLLs, dynamically linked as needed.  I will write a simple Import commandlet that loads the dll file.


// Find arguments and expand macros
int ParseArgs(char ** ptrs, char * args) {
 int i=0,j;
 char *ch;
 Name * entry;
 do {
  while(*args && isspace(*args)) args++; // white space
  if(!*args) return(i);
  if(*args == '$') {
   args++;
   entry = get_entry(args);
   args=args+strlen(entry->name);
   ch = entry->expand;
   strmove(args,ch);  // move the string to make macro room
   j= ParseArgs(ptrs+i,args); // Drill down for any macros containing macros
   i = i+j; // Update the argument count
   return(i);
  }
  else {
   ptrs[i]=args;
   args++;
   i++;
  }

  while(*args &&  isalnum(*args) ) args++;
  if(*args) {
   *args=0;
   args++;
  } 
 } while(*args);
 return(i);  
}

No comments: