view examples/dsl/symbol.cpp @ 15:f5acaf0c8a29

Don't cast through "volatile int". Causes a gcc warning nowadays. XXX: should put something else back here to frighten the optimizer
author David A. Holland
date Tue, 31 May 2022 01:00:55 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*****

 AnaGram Programming Examples

 A Dos Script Language
 Symbol Table Definitions

 Copyright 1993 Parsifal Software. All Rights Reserved.

 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 arising from the use of this software.

 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it
 freely, subject to the following restrictions:

 1. The origin of this software must not be misrepresented; you must not
    claim that you wrote the original software. If you use this software
    in a product, an acknowledgment in the product documentation would be
    appreciated but is not required.
 2. Altered source versions must be plainly marked as such, and must not be
    misrepresented as being the original software.
 3. This notice may not be removed or altered from any source distribution.

*****/

#include "symbol.h"
#include <strdict.h>
#include <charsink.h>
#include <ctype.h>
#include "util.h"

extern string_accumulator sa;
extern string_dictionary  sd;
extern unsigned           stderr_index;

action_pointer copy_action(void);

void release(action_descriptor *d) {
  delete [] d->args;
  delete [] d->ap.pointer;
  delete d;
}

void release(symbol_table_entry &s) {
  switch (s.type) {
    case string_type:
    case value_type: {
      delete [] s.data.text;
      break;
    }
    case action_type: {
      release(s.data.action);
      break;
    }
    default:
      /* not supposed to happen? */
      break;
  }
  s.data.integer = 0;
}

void define_action(int n) {
  unsigned index;
  action_descriptor *d = new action_descriptor;

  d->args = new unsigned[n];
  d->n_args = n;
  d->ap = copy_action();
  while (n--) {
    d->args[n] = sd << (sa--).top();
  }
  index = sd << (sa--).top();
  release(st[index]);
  st[index].type = action_type;
  st[index].data.action = d;
}

void define_integer(const char *name, long value) {
  unsigned index = sd << name;
  release(st[index]);
  st[index].type = integer_type;
  st[index].data.integer = value;
}

void define_string(void) {
  char *text = copy(sa--);
  unsigned index = sd << (sa--).top();
  release(st[index]);
  st[index].type = string_type;
  st[index].data.text = text;
}

const char *dos_internals[] = {
  "CD",
  "CHCP",
  "CHDIR",
  "CLS",
  "COPY",
  "CTTY",
  "DATE",
  "DEL",
  "DIR",
  "ERASE",
  "MD",
  "MKDIR",
  "PATH",
  "PROMPT",
  "RD",
  "REN",
  "RENAME",
  "RMDIR",
  "SET",
  "TIME",
  "TYPE",
  "VER",
  "VERIFY",
  "VOL",
  "CHKDSK",
  "COMP",
  "DISKCOMP",
  "DISKCOPY",
  "KEYB",
  "LABEL",
  "MODE",
  "PRINT",
  "RECOVER",
  NULL,
};

extern internal_commands_descriptor internal_commands[];
extern struct built_ins_descriptor built_ins[];
extern unsigned errorlevel_index;

void set_arg(int n, char *arg) {
  unsigned index;
  (++sa).printf("argv[%d]", n);
  index = sd << (sa--).top();
  st[index].type = string_type;
  st[index].data.text = arg;
}


void init_dos_internals(void) {
  int i;
  unsigned index;
  for (i = 0; dos_internals[i]; i++) {
    st[sd << dos_internals[i]].type = dos_type;
  }
  for (i = 0; internal_commands[i].name; i++) {
    index = sd << internal_commands[i].name;
    st[index].type = internal_type;
    st[index].data.proc = internal_commands[i].proc;
  }
  for (i = 0; built_ins[i].name; i++) {
    index = sd << built_ins[i].name;
    st[index].type = built_in_function_type;
    st[index].data.func = built_ins[i].proc;
  }
  define_integer("errorlevel", 0);
  errorlevel_index = sd["errorlevel"];
  index = stderr_index = sd << "stderr";
  st[index].type = string_type;
  st[index].data.text = NULL;
}