LPC

Contributed by Scott Anderson

LPC is an object-oriented C-like language that shares elements of LISP and Smalltalk. LPC is a special-purpose language typically used to create on-line communities like MUDs or chat rooms. This code was written for the MudOS LP driver.

Features of LPC:
The code is developed in a self-contained environment much like a Unix shell. Note that there is no such thing as a command-line program in an LP driver. To execute code in an object, it needs to a) conform to the interface for a verb or command (and be located in a hashed directory in the user's path), b) have a function called directly with a 'call' verb or something similar, or c) have code in its "create" function which is called automatically when the driver creates the object. Objects are represented by class files (the .c files in the example) which can instantiate themselves (as I do with the testshape class) or be cloned multiple times (as with the circle and rectangle objects). Objects persist in the environment until they are explicitly unloaded, or until the driver halts.

FILE: shape.c

// x and y origin points
int originX = 0, originY = 0;

// create routine for a shape
varargs void create(int x, int y)
{
  originX = x; originY = y;
}

// move the shape to a new origin
void moveTo(int newX, int newY)
{
  originX = newX;
  originY = newY;
}

// move the shape relative to the current origin
void moveRelative(int deltaX, int deltaY)
{
  originX += deltaX;
  originY += deltaY;
}

FILE: rectangle.c

// Inherit the shape object.
// Note that the MudOS driver needs an absolute path;
// typically the directory would be aliased with a
// #define statement in a .h file.

inherit "/wiz/malraux/shapes/shape";

// width and height variables.
int width = 0, height = 0;

// create the rectangle
varargs void create(int x, int y, int w, int h)
{
  // Call the inherited create method.
  // For multiple inheritance, the specific superclass
  // would be specified, like "shape::create()"
  ::create(x, y);

  width = w;
  height = h;
}

// set the width of the rectangle
void setWidth(int newW)
{
  width = newW;
}

// set the height of the rectangle
void setHeight(int newH)
{
  height = newH;
}

// draw the rectangle
void draw()
{
  printf("Drawing rectangle at: (%d, %d), width: %d, height: %d\n", originX, originY, width, height);
}

FILE: circle.c

// Inherit the shape object.
// Note that the MudOS driver needs an absolute path;
// typically the directory would be aliased with a
// #define statement in a .h file.

inherit "/wiz/malraux/shapes/shape";

// radius variable
int radius = 0;

// create the circle
varargs void create(int x, int y, int r)
{
  // Call the inherited create method.
  // For multiple inheritance, the specific superclass
  // would be specified, like "shape::create()"
  ::create(x, y);

  radius = r;
}

// set the radius of the circle
void setRadius(int newR)
{
  radius = newR;
}

// draw the circle
void draw()
{
  printf("Drawing circle at: (%d, %d), radius: %d\n", originX, originY, radius);
}

FILE: testshape.c

// Run the test code when this object is loaded
void create()
{
  // define an array of objects
  object *aShapes;
  object r;
  int i;

  // fill the array with some shapes
  aShapes = ({ clone_object("/wiz/malraux/shapes/rectangle", 10, 20, 5, 6),
	       clone_object("/wiz/malraux/shapes/circle", 15, 25, 8) });

  // display the polymorphism of the shapes
  for (i = 0; i < sizeof(aShapes); i++)
    {
      aShapes[i]->draw();
      aShapes[i]->moveRelative(100, 100);
      aShapes[i]->draw();
    }

  // call a rectangle-specific function
  r = clone_object("/wiz/malraux/shapes/rectangle", 0, 0, 15, 15);
  r->setWidth(30);
  r->draw();
}

Jim Weirich / jweirich@one.net