.Px
Greet: module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
Rand: module
{
PATH: con "/dis/lib/rand.dis";
# init sets a seed
init: fn(seed: int);
# rand returns something in 0 .. modulus-1
# (if 0 < modulus < 2^31)
rand: fn(modulus: int): int;
# (if 0 < modulus < 2^53)
bigrand: fn(modulus: big): big;
};
implement Greet;
include "sys.m";
include "draw.m";
include "greet.m";
sys: Sys;
init(ctxt: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
if ((tl argv) != nil)
sys->print("Hello, %s!\n", hd (tl argv));
else
sys->print("Hello, World!\n");
}
implement Rand;
include "rand.m";
rsalt: big;
init(seed: int)
{
rsalt = big seed;
}
MASK: con (big 1<<63)-(big 1);
rand(modulus: int): int
{
rsalt = rsalt * big 1103515245 + big 12345;
if(modulus <= 0)
return 0;
return int (((rsalt&MASK)>>10) % big modulus);
}
bigrand(modulus: big): big
{
rsalt = rsalt * big 1103515245 + big 12345;
if(modulus <= big 0)
return big 0;
return ((rsalt&MASK)>>10) % modulus;
}
rand := load Rand Rand->PATH;
implement Greet;
include "sys.m"; include "draw.m";
Greet: module {
init: fn(ctxt: ref Display->Draw, args: list of string);
};
init(ctxt: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
if ((tl argv) != nil)
sys->print("Hello, %s!\n", hd (tl argv));
else
sys->print("Hello, World!\n");
}
sys = load Sys Sys->PATH; mymod = load MyMod "/mymodules/mymod.dis";
sys := load Sys Sys->PATH;
sys->print("Hello, World!\n");
Sys: module
{
PATH: con "$Sys";
...
}
sys := load Sys Sys->PATH;
print: import sys;
print("Hello, World!\n");
greetimport.b
1 implement Command;
2 include "sys.m";
3 include "draw.m";
4 sys: Sys;
5 print: import sys;
6 Command: module {
7 init: fn(ctxt: ref Draw->Context, argv: list of string);
8 };
9 init(ctxt: ref Draw->Context, argv: list of string) {
10 sys = load Sys Sys->PATH;
11 if (tl argv != nil)
12 print("Hello, %s!\n", hd tl argv);
13 else
14 print("Hello, World!\n");
15 }
stderr := sys->fildes(2);
sys->fprint(stderr, "error: %s\n", msg);
cfgfd := sys->open("program.cfg", Sys->OREAD);
Pretty picture of file permissions File Permissions
tmp := sys->create("/tmp/file.tmp", Sys->ORDWR | Sys->ORCLOSE, 8r600);
tmpbuf := array[] of byte; n = sys->read(cfgfd, tmpbuf, 80);
write(tmp, tmpbuf, 80);
sys->print("Hello, %s!\n", hd (tl argv));
Hello, Inferno!
stderr := sys->fildes(2); # get the FD for stderr ... sys->fprint(stderr, "error: %s\n'', msg);
%[flags]verb
num := 123;
for (i := 1; i <= 5; i++) {
sys->print("%8d %8d %8d %8d %8d\n",
i, i*i, i*i*i, i*i*i*i, i*i*i*i*i);
}
sys->print("%d\n", num);
sys->print("%10d\n\n", num);
1 1 1 1 1 2 4 8 16 32 3 9 27 81 243 4 16 64 256 1024 5 25 125 625 3125 123 123
decnum := 2345.678901;
sys->print("%.3f\n", decnum);
2345.679
str := "Hello, Inferno!";
sys->print("%5.10s\n\n", str);
Hello, Inf
num := 123;
sys->print("%2.5d\n", num);
00123
num := 123;
decnum := 2345.678901;
sys->print("Right-justified: %5d\n", num);
sys->print("Left-justified: %-5d\n", num);
sys->print("$%9.2f\n\n", decnum);
Right-justified: 123 Left-justified: 123 $ 2345.68
num := 123;
sys->print("%#d\n", num);
sys->print("%#e\n", real num);
sys->print("%x\n", num);
sys->print("%#X\n", num);
123 123.000000 7b 0X7B
include "sys.m";
include "draw.m";
sys: Sys;
Command: 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;
num := 123;
decnum := 2345.678901;
scinum := 0.0000987;
str := "Hello, Inferno!";
# field width
for (i := 1; i <= 5; i++) {
sys->print("%8d %8d %8d %8d %8d\n",
i, i*i, i*i*i, i*i*i*i, i*i*i*i*i);
}
sys->print("%d\n", num);
sys->print("%10d\n\n", num);
# precision
sys->print("%.3f\n", decnum);
sys->print("%2.5d\n", num);
sys->print("%5.10s\n\n", str);
# scientific notation
sys->print("%e\n", scinum);
sys->print("%E\n", decnum);
sys->print("%g\n", decnum);
sys->print("%g\n\n", scinum);
# justification
sys->print("Right-justified: %5d\n", num);
sys->print("Left-justified: %-5d\n", num);
sys->print("$%9.2f\n\n", decnum);
# Decimal and Hexadecimal
sys->print("%#d\n", num);
sys->print("%x\n", num);
sys->print("%#X\n", num);
}
1 1 1 1 1 2 4 8 16 32 3 9 27 81 243 4 16 64 256 1024 5 25 125 625 3125 123 123 2345.679 00123 Hello, Inf 9.870000e-05 2.345679E+03 2345.678901 9.87e-05 Right-justified: 123 Left-justified: 123 $ 2345.68 123.0 7b 0X7B
spawn func(1, 2);
1 implement Command;
2 include "sys.m";
3 include "draw.m";
4 sys: Sys;
5 Command: module {
6 init: fn(ctxt: ref Draw->Context, argv: list of string);
7 };
8 init(ctxt: ref Draw->Context, argv: list of string) {
9 sys = load Sys Sys->PATH;
10 for (i:=1; i<=5; i++) {
11 spawn func(i);
12 sys->print("%2d\n", i);
13 }
14 }
15 func(n: int) {
16 sys->print("%2.2d\n", n);
17 }
divine$ threadx 1 1 2 2 3 4 5 3 4 divine$ 5
c: chan of int;
c = chan of int;
c <-= 4; # send int on 'c'
i := <-c; # assign value received on c to i</ProgramText> func(<-c); # pass value received on c to func</ProgramText>
outchan := chan of string;
inchan := chan of int;
i := int;
alt {
i = <- inchan =>
...
outchan <- = "sent" =>
...
}
1 implement Command;
2 include "sys.m";
3 include "draw.m";
4 sys: Sys;
5 print: import sys;
6 sc: chan of int;
7 rc: chan of int;
8 Command: module {
9 init: fn(ctxt: ref Draw->Context, argv: list of string);
10 };
11 init(ctxt: ref Draw->Context, argv: list of string) {
12 sys = load Sys Sys->PATH;
13 sc = chan of int;
14 rc = chan of int;
15 for (i:=1; i<=5; i++) {
16 spawn func();
17 print("%2d\n", i);
18 sc<- = i;
19 <-rc;
20 }
21 }
22 func() {
23 i := <-sc;
24 print(" %2d\n", i);
25 rc<- = i;
26 }
implement Command;
include "sys.m";
include "draw.m";
include "chanx.m";
sys: Sys;
print: import sys;
cx: ChanX;
sc: chan of int;
rc: chan of int;
Command: 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;
cx = load ChanX "./chanx2b.dis";
sc = chan of int;
rc = chan of int;
for (i:=1; i<=5; i++) {
spawn cx->func(sc, rc);
print("%2d\n", i);
sc<- = i;
func(<-rc);
}
}
func(i: int) {
print("%6d\n", i);
}
chanx2b.m
ChanX: module {
func: fn(sc: chan of int, rc: chan of int);
};
chanx2b.b
implement ChanX;
include "sys.m";
include "draw.m";
include "chanx.m";
sys: Sys;
print: import sys;
func(sc: chan of int, rc: chan of int) {
sys = load Sys Sys->PATH;
i: int;
i = <-sc;
print("%4d\n", i);
rc <- = i;
}
1 implement WmAltX;
2 include "sys.m";
3 include "draw.m";
4 include "tk.m";
5 include "wmlib.m";
6 sys: Sys;
7 draw: Draw;
8 Display, Image: import draw;
9 tk: Tk;
10 wmlib: Wmlib;
11 WmAltX: module {
12 init: fn(ctxt: ref Draw->Context, argv: list of string);
13 };
14 win_cfg := array[] of {
15 "frame .ft",
16 "text .ft.t -yscrollcommand {.ft.s set} -width 40w
17 -height 15h",
18 "scrollbar .ft.s -command {.ft.t yview}",
19 "focus .ft.t",
20 "pack .ft.s -side right -fill y",
21 "frame .fb.a -bd 2 -relief sunken",
22 "pack .ft.t -side left -fill y",
23 "button .fb.a.b -height 2h -text {Button 1}
24 -command {send cmd 1}",
25 "pack .fb.a.b -padx 1 -pady 1",
26 "button .fb.c -height 2h -text {Button 2}
27 -command {send cmd 2}",
28 "button .fb.q -height 2h -text {Quit}
29 -command {send cmd Q}",
30 "frame .fb",
31 "pack .fb.a .fb.c .fb.q -padx 5 -pady 5 -side left",
32 "pack .ft .fb",
33 "pack propagate . 0",
34 "update",
35 };
36 init(ctxt: ref Draw->Context, argv: list of string) {
37 sys = load Sys Sys->PATH;
38 draw = load Draw Draw->PATH;
39 tk = load Tk Tk->PATH;
40 wmlib = load Wmlib Wmlib->PATH;
41 wmlib->init();
42 (win, menubut) := wmlib->titlebar(ctxt.screen,
43 "-x 5 -y 5", "WmAltX", 0);
44 cmd := chan of string;
45 tk->namechan(win, cmd, "cmd");
46 wmlib->tkcmds(win, win_cfg);
47 for(;;) alt {
48 menu := <-menubut =>
49 if(menu[0] == 'e') {
50 tk->cmd(win, ".ft.t insert end '"+
51 "Close button pressed.\n");
52 tk->cmd(win, "update");
53 wmlib->dialog(win, "warning -fg red",
54 ``Close Button", "Close Button pressed!", 0,
55 "Close"::nil);
56 exit;
57 }
58 wmlib->titlectl(win, menu);
59 s := <-cmd =>
60 case s[0] {
61 '1' =>
62 tk->cmd(win, ".ft.t insert end '"+
63 "Button 1 pressed.\n");
64 tk->cmd(win, "update");
65 break;
66 '2' =>
67 tk->cmd(win, ".ft.t insert end '"+
68 "Button 2 pressed.\n");
69 tk->cmd(win, "update");
70 break;
71 'Q' =>
72 tk->cmd(win, ".ft.t insert end '"+
73 "Quit button pressed.\n");
74 tk->cmd(win, "update");
75 wmlib->dialog(win, "info", "Quit Button",
76 "Quit button pressed!", 0,
77 "Quit"::nil);
78 exit;
79 }
80 }
81 }
drawgreet.b:
1 implement DrawGreet;
2 include "sys.m";
3 include "draw.m";
4 sys: Sys;
5 draw: Draw;
6 Display, Font, Rect, Point, Image, Screen: import draw;
7 display: ref Display;
8 disp, img: ref Image;
9 font: Font;
10 DrawGreet : module {
11 init: fn(ctxt: ref Draw->Context, argv: list of string);
12 };
13 init(ctxt: ref Draw->Context, argv: list of string) {
14 sys = load Sys Sys->PATH;
15 draw = load Draw Draw->PATH;
16 display = Display.allocate(nil);
17 disp = display.image;
18 spawn refresh(display);
19 white := display.color(Draw->White); # color predefined by Draw
20 yellow := display.color(Draw->Yellow);
21 black := display.color(Draw->Black);
22 ones := display.ones; # pixel mask
23 font = Font.open(display, "/fonts/lucidasans/unicode.9x24.font");
24 if (tl argv != nil)
25 str := "Hello, " + (hd tl argv) + "!";
26 else
27 str = "Hello, World!";
28 strctr := (font.width(str) / 2);
29 img := display.open("/icons/logon.bit");
30 imgxctr := img.r.max.x / 2;
31 dispctr := disp.r.max.x / 2;
32 disp.draw(disp.r, white, ones, (0,0));
33 disp.draw(disp.r, img, ones, (-(dispctr-imgxctr),-10));
34 disp.text(((dispctr - strctr), img.r.max.y+20), black,
35 (0,0), font, str);
36 sys->sleep(10000);
37 disp.draw(disp.r, white, ones, (0,0));
38 }
39 refresh(display: ref Display) {
40 display.startrefresh();
41 }
inferno% drawgreet Inferno
<IMAGE xml:link="simple" href="img/hellodraw.gif">
Figure XX. Output of drawgreet.b
prefabgreet.b
1 implement PrefabGreet;
2 include "sys.m";
3 include "draw.m";
4 include "prefab.m";
5 sys: Sys;
6 draw: Draw;
7 Display, Font, Rect, Point, Image, Screen: import draw;
8 prefab: Prefab;
9 Style, Element, Compound, Environ: import prefab;
10 PrefabGreet : module {
11 init: fn(ctxt: ref Draw->Context, argv: list of string);
12 };
13 init(ctxt: ref Draw->Context, argv: list of string) {
14 sys = load Sys Sys->PATH;
15 draw = load Draw Draw->PATH;
16 prefab = load Prefab Prefab->PATH;
17 display := Display.allocate(nil);
18 disp := display.image;
19 spawn refresh(display);
20 white := display.color(Draw->White);
21 yellow := display.color(Draw->Yellow);
22 black := display.color(Draw->Black);
23 grey := display.rgb(160,160,160);
24 ones := display.ones;
25 screen := Screen.allocate(disp, white, 1);
26 textfont := Font.open(display, "/fonts/lucidasans/unicode.13.font");
27 titlefont := Font.open(display, "/fonts/lucidasans/italiclatin1.10.font");
28 win_style := ref Style(
29 titlefont, # title font
30 textfont, # text font
31 grey, # element color
32 black, # edge color
33 yellow, # title color
34 black, # text color
35 white); # highlight color
36 win := ref Environ(screen, win_style);
37 if ((tl argv) != nil)
38 msg := "Hello, " + (hd(tl argv)) + "!";
39 else
40 msg = "Hello, World!";
41 icon := display.open("/icons/lucent.bit");
42 dy := (icon.r.dy()-textfont.height)/2;
43 wintitle := Element.text(win, "Inferno Prefab Example",
44 ((0,0),(0,0)), Prefab->ETitle);
45 ie := Element.icon(win, icon.r, icon, ones);
46 te := Element.text(win, msg, ((0,dy),(0,dy)),
47 Prefab->EText);
48 se:= Element.separator(win, ((0,0), (10,0)),
49 display.zeros, display.zeros);
50 le := Element.elist(win, ie, Prefab->EHorizontal);
51 le.append(se); # add space between icon and text
52 le.append(te);
53 le.append(se); # add space between text and border
54 le.adjust(Prefab->Adjpack, Prefab->Adjleft);
55 c:= Compound.box(win, (20,20), wintitle, le);
56 c.draw();
57 sys->sleep(10000); # Prefab elements are gc'd
58 }
59 refresh(display: ref Display) {
60 display.startrefresh();
61 }
inferno% prefabgreet Inferno
<IMAGE xml:link="simple" href="img/helloprefab.gif">
Figure XX. Output of prefabgreet.b
tkgreet.b:
1 implement TkGreet;
2 include "sys.m";
3 include "draw.m";
4 include "tk.m";
5 include "wmlib.m";
6 sys: Sys;
7 tk: Tk;
8 wmlib: Wmlib;
9 msg: string;
10 TkGreet : module {
11 init: fn(ctxt: ref Draw->Context, argv: list of string);
12 };
13 init(ctxt: ref Draw->Context, argv: list of string) {
14 sys = load Sys Sys->PATH;
15 tk = load Tk Tk->PATH;
16 wmlib = load Wmlib Wmlib->PATH;
17 wmlib->init();
18 if (tl argv != nil)
19 msg = "Hello, " + (hd tl argv) + "!";
20 else
21 msg = "Hello, World!";
22 win_cfg := array [] of {
23 "label .i -bitmap @/icons/logon.bit",
24 "label .t -text {" + msg + "\n" + "}",
25 "focus .t",
26 "button .b -text {OK} -width 10h
27 -command {send cmd O}",
28 "pack .i .t .b -padx 5 -pady 5",
29 "pack propagate . 0",
30 "update",
31 };
32 (title, menubut) := wmlib->titlebar(ctxt.screen,
33 "-x 50 -y 25", "Limbo/Tk Example", wmlib->Hide);
34 cmd := chan of string;
35 tk->namechan(title, cmd, "cmd");
36 wmlib->tkcmds(title, win_cfg);
37 for(;;) alt {
38 click := <- menubut =>
39 if (click[0] == 'e')
40 return;
41 wmlib->titlectl(title, click);
42 if (click[0] == 't')
43 tk->cmd(title, "focus .t");
44 s := <- cmd =>
45 case s[0] {
46 'O' => exit;
47 }
48 }
49 }
inferno% tkgreet Inferno
<IMAGE xml:link="simple" href="img/hellotk.gif">
Figure XX. Output of tkgreet.b