HP Forums
Sharing Code Between Programs - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Sharing Code Between Programs (/thread-17939.html)



Sharing Code Between Programs - IHarwell - 01-16-2022 08:44 PM

I can't be the only person annoyed that the documentation for the Prime describes "calling programs from other programs" basically amounts to "there's a call stack, but you can't call functions from other files."

With that said, I found a rather straightforward way to call code contained in other files. I haven't tested the feature too extensively, but here's what I've found so far:

Let's say you want to make a function library for your programs to share. In my case, I wanted to have a way to draw the "shift" symbol in the correct place for any of my programs. This is the "library" file I created.

Code:
#cas
DrawShiftMark():=
BEGIN
 LOCAL c2:=RGB(0,255,255);
 FILLPOLY_P(G0,{{2,7},{2,3},{1,3},{3,1},{5,3},{4,3},{4,7}},c2);
 PIXON_P(G0,11,2,c2);
 LINE_P(G0,8,1,10,1,c2);
 LINE_P(G0,7,2,7,3,c2);
 LINE_P(G0,8,4,10,4,c2);
 LINE_P(G0,11,5,11,6,c2);
 LINE_P(G0,8,7,10,7,c2);
 PIXON_P(G0,7,6,c2);
END;

ClearShiftMark():=
BEGIN
 LOCAL x_left:=1;
 LOCAL x_right:=11;
 LOCAL y_top:=1;
 LOCAL y_bottom:=7;
 LOCAL Y;
 FOR Y FROM y_top TO y_bottom DO
  LOCAL bg:=GETPIX_P(G0,x_right+1,Y);
  LINE_P(G0,x_left,Y,x_right,Y,bg);
 END;
END;
#end

This file creates the functions and makes them available through the CAS system.

To call them, there are two options that currently occur to me. The first is more direct, and looks like:
Code:
CAS("DrawShiftMark()");

The second basically uses a verbose import statement by wrapping the CAS command in a function.

Code:
LOCAL DrawShiftMark()
BEGIN
 CAS("DrawShiftMark()");
END;

While it's a workable solution as-is, this approach is vulnerable to name collisions. I'll look into using the "folders" system that I ran across and posted about in an earlier thread to see if I can get something like namespaces working. Until then, I thought it was a pretty neat technique that enables more compact programs than could be done otherwise.

If anyone has related ideas or has any ways to extend this, I'd be very interested to take a look. Smile

P.S. The current holdup for implementing namespaces is due to the EVAL function doing weird things with the "END" keyword. There may be a workaround, but I'll have to test it out a bit.


RE: Sharing Code Between Programs - Didier Lachieze - 01-16-2022 10:15 PM

Any reason to have these defined as CAS programs while they don’t use any CAS function?

I would rather define them as PPL programs and with EXPORT they would be callable from other programs :

Code:
#pragma mode( separator(.,;) integer(h32) )

EXPORT DrawShiftMark()
BEGIN
 LOCAL c2:=RGB(0,255,255);
 FILLPOLY_P(G0,{{2,7},{2,3},{1,3},{3,1},{5,3},{4,3},{4,7}},c2);
 PIXON_P(G0,11,2,c2);
 LINE_P(G0,8,1,10,1,c2);
 LINE_P(G0,7,2,7,3,c2);
 LINE_P(G0,8,4,10,4,c2);
 LINE_P(G0,11,5,11,6,c2);
 LINE_P(G0,8,7,10,7,c2);
 PIXON_P(G0,7,6,c2);
END;

EXPORT ClearShiftMark()
BEGIN
 LOCAL x_left:=1;
 LOCAL x_right:=11;
 LOCAL y_top:=1;
 LOCAL y_bottom:=7;
 LOCAL Y;
 FOR Y FROM y_top TO y_bottom DO
  LOCAL bg:=GETPIX_P(G0,x_right+1,Y);
  LINE_P(G0,x_left,Y,x_right,Y,bg);
 END;
END;



RE: Sharing Code Between Programs - IHarwell - 01-16-2022 10:44 PM

(01-16-2022 10:15 PM)Didier Lachieze Wrote:  Any reason to have these defined as CAS programs while they don’t use any CAS function?

I would rather define them as PPL programs and with EXPORT they would be callable from other programs :

Code:
#pragma mode( separator(.,;) integer(h32) )

EXPORT DrawShiftMark()
BEGIN
 LOCAL c2:=RGB(0,255,255);
 FILLPOLY_P(G0,{{2,7},{2,3},{1,3},{3,1},{5,3},{4,3},{4,7}},c2);
 PIXON_P(G0,11,2,c2);
 LINE_P(G0,8,1,10,1,c2);
 LINE_P(G0,7,2,7,3,c2);
 LINE_P(G0,8,4,10,4,c2);
 LINE_P(G0,11,5,11,6,c2);
 LINE_P(G0,8,7,10,7,c2);
 PIXON_P(G0,7,6,c2);
END;

EXPORT ClearShiftMark()
BEGIN
 LOCAL x_left:=1;
 LOCAL x_right:=11;
 LOCAL y_top:=1;
 LOCAL y_bottom:=7;
 LOCAL Y;
 FOR Y FROM y_top TO y_bottom DO
  LOCAL bg:=GETPIX_P(G0,x_right+1,Y);
  LINE_P(G0,x_left,Y,x_right,Y,bg);
 END;
END;

Weirdly, I was getting errors with that method. Ideally, I'd like to arrange these as "UI.DrawShift" and "UI.ClearShift". Unfortunately, that requires CAS to do. Getting it to work is proving challenging. They appear to have gone to great lengths to prevent the use of those structures in code.


RE: Sharing Code Between Programs - BruceH - 01-19-2022 12:29 AM

Have you tried attaching the code to an app and calling that?

Which I think you can do by calling

Code:
var := appname.functioname(params)

Sorry to be so vague - don't have the Prime to hand but I'm sure I recall CdB mentioning it at one time.