Limbo on Lucent Technologies' Inferno
The Towers of Hanoi using the Limbo programming language on Lucent Technologies' Inferno operating system.
#
# The Towers Of Hanoi
# Limbo
# Copyright (C) 1998 Amit Singh. All Rights Reserved.
# http://hanoi.kernelthread.com
#
# Tested under Inferno release 1.0
#
# Use "limbo -g hanoi.b" to compile (-g for debugging).
# This will create a file called hanoi.dis - bytecode
# that would run on Dis.
implement Hanoi;
include "sys.m";
sys: Sys;
stdout: ref Sys->FD;
stderr: ref Sys->FD;
include "draw.m";
LIMIT_N: con 10;
Hanoi: module
{
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
init(ctxt: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
stdout = sys->fildes(1);
stderr = sys->fildes(2);
argv = tl argv;
N: int;
if (len argv != 1) {
sys->fprint(stderr, "usage: Hanoi N\n");
exit;
}
N = int hd argv;
if (N > LIMIT_N) {
sys->fprint(stderr, "error: N too large (max allowed is %d)\n", LIMIT_N);
exit;
}
a:= array[] of {
3, 1, 2
};
dohanoi(N, a);
exit;
}
dohanoi(n: int, a: array of int)
{
if (n <= 0) {
return;
}
b:= array[] of {
a[2], a[1], a[0]
};
dohanoi(n-1, b);
sys->print("move %d --> %d\n", a[1], a[0]);
c:= array[] of {
a[0], a[2], a[1]
};
dohanoi(n-1, c);
return;
}