The Small Scripting Language
by Thiadmer Riemersma


Listing One
#include <stdio.h>
#include <stdlib.h>
#include "amx.h"

void *loadprogram(AMX *amx,char *filename)
{
  FILE *fp;
  AMX_HEADER hdr;
  void *program = NULL;

  if ((fp = fopen(filename,"rb")) != NULL) {
    fread(&hdr, sizeof hdr, 1, fp);
    if ((program = malloc((int)hdr.stp)) != NULL) {
      rewind(fp);
      fread(program, 1, (int)hdr.size, fp);
      fclose(fp);
      if (amx_Init(amx,program,NULL) == AMX_ERR_NONE)
        return program;
      free(program);
    } /* if */
  } /* if */
  return NULL;
}

int main(int argc,char *argv[])
{
extern AMX_NATIVE_INFO core_Natives[];
extern AMX_NATIVE_INFO console_Natives[];

  AMX amx;
  cell ret;
  int err;
  void *program;

  if (argc != 2 || (program = loadprogram(&amx,argv[1])) == NULL) {
    printf("Usage: SRUN <filename>\n\n"
           "The filename must include the extension\n");
    return 1;
  } /* if */

  core_Init();
  amx_Register(&amx, core_Natives, -1);
  err = amx_Register(&amx, console_Natives, -1);

  if (err == AMX_ERR_NONE)
    err = amx_Exec(&amx, &ret, AMX_EXEC_MAIN, 0);

  if (err != AMX_ERR_NONE)
    printf("Run time error %d on line %ld\n", err, amx.curline);
  else if (ret != 0)
    printf("%s returns %ld\n", argv[1], (long)ret);

  free(program);
  core_Exit();

  return 0;
}


Listing Two
/* Print all primes below 100, using the
 * "Sieve of Eratosthenes" algorithm */
#include <console>
main()
    {
    const max_primes = 100;
    new series[max_primes] = { true, ... };

    for (new i = 2; i < max_primes; ++i)
        if (series[i])
            {
            printf("%d ", i);
            /* filter all multiples of this "prime" from the list */
            for (new j = 2 * i; j < max_primes; j += i)
                series[j] = false;
            }
    }


Listing Three
/* illustration of Zeller's congruence algorithm to
 * calculate the day of the week given a date */
#include <console>

weekday(day, month, year)
    {
    if (month <= 2)
        month += 12, --year;
    new j = year % 100;
    new e = year / 100;
    return (day + (month+1)*26/10 + j + j/4 + e/4 - 2*e) % 7;
    }
readdate(&day, &month, &year)
    {
    print("Give a date (dd-mm-yyyy): ");
    day = getvalue(_,'-','/');
    month = getvalue(_,'-','/');
    year = getvalue();
    }
main()
    {
    new day, month, year;
    readdate(day, month, year);

    new wkday = weekday(day, month, year);
    printf("The date %d-%d-%d falls on a ", day, month, year);
    switch (wkday)
        {
        case 0:
            print("Saturday");
        case 1:
            print("Sunday");
        case 2:
            print("Monday");
        case 3:
            print("Tuesday");
        case 4:
            print("Wednesday");
        case 5:
            print("Thursday");
        case 6:
            print("Friday");
        }
    print("^n");
    }





