Napier88 Programming Tutorial
jlopes@jerry:~/src/lapa:48> nps types.N LapaTypes -t bulk -t Lists
jlopes@jerry:~/src/lapa:49> cat types.N
! nps types.N LapaTypes -t bulk -t Lists
!--
!-- types for Lapa 95/96
!-- jlopes, Maio 96
!--
rec type Rua is Table[string,RuaInfo]
& RuaInfo is structure(restaurantes: List[string];
monumentos: List[string];
edificios: List[string])
& Edificio is Table[string,EdificioInfo]
& EdificioInfo is structure(rua: string;
grelha: Grelha;
descricao,
interesse: string;
tipo: TipoEdificio)
& TipoEdificio is variant(restaurante: Restaurante;
monumento: Monumento;
outro: null)
& Restaurante is structure(pratos: List[Prato])
& Prato is structure(nome: string;
preco: int;
descricao: string)
& Monumento is structure(horario: Horario;
entrada: int;
imagens: List[image])
& Horario is structure(abertura, fecho: int)
& Grelha is structure(x, y: int)
jlopes@jerry:~/src/lapa:50>
jlopes@jerry:~/src/lapa:59> npc mk_lapa.N -t theStore
jlopes@jerry:~/src/lapa:60> npr mk_lapa.out
Inserted "Lapa" into User
Inserted "Data" into Lapa
jlopes@jerry:~/src/lapa:61> cat mk_lapa.N
! npc mk_lapa.N -t theStore
!--
!-- make contexts for Lapa 95/96
!-- jlopes, Abr 96
!--
use theStore() with User, Library: env in
use Library with Environment, IO: env in
use Environment with environment: proc(->env) in
use IO with writeString: proc(string) in
begin
if User contains Lapa: env do
begin
writeString("Dropping '"Lapa'" from User'n")
drop Lapa from User
end
in User let Lapa= environment()
writeString("Inserted '"Lapa'" into User'n")
use User with Lapa: env in
begin
in Lapa let Data= environment()
writeString("Inserted '"Data'" into Lapa'n")
end
end
jlopes@jerry:~/src/lapa:62>
jlopes@jerry:~/src/lapa:90> npc ruas_L.N -t theStore -t bulk -t LapaTypes
jlopes@jerry:~/src/lapa:91> npr ruas_L.out
Inserted Table "ruas" into Lapa
Stubs for "Rua" inserted into Lapa
jlopes@jerry:~/src/lapa:92> cat ruas_L.N
! npc ruas_L.N -t theStore -t bulk -t LapaTypes
!--
!-- make stubs for Rua in Lapa 95/96
!-- jlopes, Abr 96
!--
use theStore() with Library, GlasgowLibraries, User: env in
use Library with Tables, IO: env in
use Tables with tableGen: proc[K,D]( Comparison[K] -> Table[K,D] ) in
use IO with writeString: proc(string) in
use GlasgowLibraries with Miscellany: env in
use Miscellany with uninitialised_void: proc(string) in
use User with Lapa: env in
begin
let eqStr= proc(s1,s2: string -> bool); s1 = s2
let ltStr= proc(s1,s2: string -> bool); s1 < s2
let comparison= Comparison[string](ordered: struct(equal= eqStr;lessThan= ltStr))
in Lapa let ruas:= tableGen[string,RuaInfo](comparison)
writeString("Inserted Table '"ruas'" into Lapa'n")
in Lapa let addRua:= proc(); uninitialised_void("addRua")
in Lapa let changeRua:= proc(); uninitialised_void("changeRua")
in Lapa let removeRua:= proc(); uninitialised_void("removeRua")
in Lapa let lookupRua:= proc(); uninitialised_void("lookupRua")
writeString("Stubs for '"Rua'" inserted into Lapa'n")
end
jlopes@jerry:~/src/lapa:93>
jlopes@jerry:~/src/lapa:114> npc ruas_S.N -t theStore -t Lists -t LapaTypes
jlopes@jerry:~/src/lapa:115> npr ruas_S.out
Value for "addRua" changed
jlopes@jerry:~/src/lapa:116> cat ruas_S.N
! npc ruas_S.N -t theStore -t Lists -t LapaTypes
!--
!-- provide proc values for Rua in Lapa 95/96
!-- jlopes, Abr 96
!--
use theStore() with User, Library: env in
use Library with System, IO: env in
use System with stabilise: proc() in
use IO with readLine: proc( -> string); writeString: proc(string) in
use User with Lapa: env in
use Lapa with ruas: Rua; addRua, changeRua, removeRua, lookupRua: proc() in
begin
addRua:= proc()
begin
writeString("Qual o nome da rua ? ")
let nome= readLine()
project ruas(lookup)(nome) as N onto
present: writeString("Rua ja'' existente!'n")
default: begin
ruas(enter)(nome,RuaInfo(List[Restaurante](empty:nil), List[Monumento](empty:nil), List[Edificio](empty:nil)))
stabilise()
writeString("Rua " ++ nome ++ " inserted.'n")
end
end
writeString("Value for '"addRua'" changed'n")
!* and similarly for the others
end
jlopes@jerry:~/src/lapa:117>
jlopes@jerry:~/src/lapa:131> npc ruas_T.N -t theStore -t LapaTypes
jlopes@jerry:~/src/lapa:132> npr ruas_T.out
Testing "addRua"...
Qual o nome da rua ? "Boavista"
Rua Boavista inserted.
All tests done.
jlopes@jerry:~/src/lapa:133> cat ruas_T.N
! npc ruas_T.N -t theStore -t LapaTypes
!--
!-- test proc values for Rua in Lapa 95/96
!-- jlopes, Abr 96
!--
use theStore() with User, Library: env in
use Library with IO: env in
use IO with writeString: proc(string) in
use User with Lapa: env in
use Lapa with addRua, changeRua, removeRua, lookupRua: proc() in
begin
writeString("Testing '"addRua'"...'n" )
addRua()
writeString("All tests done.'n")
!* and similarly for the others
end
jlopes@jerry:~/src/lapa:134>
! npc sample-pic.N -t theStore -t win -t tools -t general
!--
!-- examples of graphical data types usage
!-- jlopes, Abr 96
!--
use theStore() with Library: env in
use Library with System, Win, RasterRules, FailValues, Arithmetical, Font, Graphical, IO: env in
use System with abort: proc() in
use Win with Tools, Generators: env in
use Tools with
menuGen: proc(*Appearance,*proc(int) -> MenuPack);
lightButtonGen: proc( Appearance,proc(int) -> ButtonPack) in
use Generators with
windowGen: proc(-> Window);
eventMonitorGen: proc(proc(-> Event),Application,Optional[proc(-> bool)] -> proc());
screenWindowManagerGen: proc(int,string -> ScreenWindowManagerPack) in
use RasterRules with copyRule: int in
use FailValues with
windowManagerFailValue: WindowManager;
menuPackFailValue: MenuPack;
screenWindowManagerPackFailValue: ScreenWindowManagerPack in
use Arithmetical with
pi: real; cos,sin: proc(real -> real);
float: proc(int -> real) in
use Font with courR12: FontPack in
use Graphical with Raster,Outline : env in
use Raster with
xDim,yDim : proc( image -> int ) in
use Outline with
makeDrawFunction : proc( string -> DrawFunction ) in
use IO with writeString: proc(string) in
begin
!-- *************** pic examples ***************
let arcPic= proc(r,theta1,theta2,step: real -> pic)
begin
let theta:= theta1
let result:= [r * cos(theta),r * sin(theta)]
repeat
begin
theta:= theta + step
result:= result ^ [r * cos(theta),r * sin(theta)]
end
while theta <= theta2
result
end
let circlePic= proc(r,step: real -> pic)
arcPic(r,0.0,2.0*pi,step)
let buttonPic= proc(x,y: int -> pic)
begin
let fx= float(x); let fy= float(y)
let step= 0.05; let fr1= 8.0
!* result
(((( [ fr1, 0.0 ] ^
shift arcPic( fr1,-pi/2.0,0.0,step ) by fx-fr1,fr1 ) ^
shift arcPic( fr1,0.0,pi/2.0,step ) by fx-fr1,fy-fr1 ) ^
shift arcPic( fr1,pi/2.0,pi,step ) by fr1,fy-fr1 ) ^
shift arcPic( fr1,pi,3.0*pi/2.0,step ) by fr1,fr1 )
end
!-- *************** image example ***************
let buttonImage=
begin
let stringImage= courR12(stringToTile)(" Accept ")
let x= xDim(stringImage); let y = yDim(stringImage)
let temp= image x + 8 by y + 8 of off
copy stringImage onto limit temp at 4,4
let buttonBorder= buttonPic(x,y)
let drawFunction= makeDrawFunction("image" )
drawFunction'imageDraw(temp, buttonBorder,0.0,float(x),0.0,float(y))
temp
end
!...
!...
!-- *************** WIMP interface ***************
!-- dummies
let windowManager:= windowManagerFailValue
let myMenu:= menuPackFailValue
!-- *************** button example ***************
!-- quit button
let finished:= false
let quitAction= proc(buttonNo: int); finished:= true
let quitButton:= lightButtonGen(Appearance(textual: "QUIT"),quitAction)
!-- *************** menu example ***************
let undisplay= proc(); windowManager(undisplay)(myMenu(window))
let dummyAction= proc(); writeString("Option selected'n")
myMenu:= menuGen( vector @1 of [Appearance(textual:"Option 1"),
Appearance(textual:"Option 2"),
Appearance(textual:"Option 3"),
Appearance(textual:"Option 4")
],
vector @1 of [proc(buttonNo:int); { undisplay(); dummyAction() },
proc(buttonNo:int); { undisplay(); dummyAction() },
proc(buttonNo:int); { undisplay(); dummyAction() },
proc(buttonNo:int); { undisplay(); dummyAction() }
]
)
!-- *************** window example ***************
!-- image window
let imageWindow:= windowGen()
imageWindow(setTitle)("AN EXAMPLE")
imageWindow(setSize)(Rect(Pos(0,0),Pos(300,300)))
!-- copy buttonImage into window
imageWindow(imageRaster)(Limit(Pos(200,200),Size(xDim(buttonImage),yDim(buttonImage))),buttonImage,copyRule,true)
!...
!...
!-- *************** interaction example ***************
!-- root window manager
let swManagerPack= screenWindowManagerGen(1,"my_machine.my_domain.pt:0.0")
if swManagerPack = screenWindowManagerPackFailValue do abort() !* Cannot open X window
windowManager:= swManagerPack(windowManager)
!-- display quitButton, myMenu & buttonImage
windowManager(display)(DisplayInfo(quitButton(window),Pos(500,500),Level(true,1)),false)
windowManager(display)(DisplayInfo(myMenu(window),Pos(200,500),Level(true,1)),false)
windowManager(display)(DisplayInfo(imageWindow,Pos(0,0),Level(true,1)),false)
!-- go for eventMonitor
let eventMonitor= eventMonitorGen(
swManagerPack(gatherEvents),
windowManager(getDisplayWindow)()(getApplication)(),
Optional[proc(-> bool)](present: proc(-> bool); finished))
!-- start processing events
eventMonitor()
!-- when it quits, undisplay all windows and close screen
windowManager(undisplay)(imageWindow)
swManagerPack(closeScreen)()
end
?
João Correia Lopes
([email protected]).
Last modified: Mon Apr 7 10:56:26 1997
J. Lopes Home page
Course page (Tópicos Avançados de Bases de Dados)