GHS Macro Commands?

Discussion in 'Software' started by GhrarhG, Feb 8, 2006.

  1. GhrarhG
    Joined: Oct 2005
    Posts: 4
    Likes: 0, Points: 0, Legacy Rep: 10
    Location: Glen Cove, NY

    GhrarhG New Member

    Hello all,

    I am currently a student at Webb Institute, and for the remainder of my winter work term, I will be working with GHS. I have become somewhat familiar with the program. I have created some macros, but I am unsure of what all the commands are. I do not have any previous examples to work from, and the manuals are aweful. Are the macro commands capable of looping? The ghsport website seems to be rather useless as well. Can anyone send me a link or possibly previous macros they have written containing commands other then the basic plus, minus, atan, cos, if/ then statements.

    Thank you,
    Jon
     
  2. ABoatGuy
    Joined: Aug 2004
    Posts: 208
    Likes: 6, Points: 18, Legacy Rep: 79
    Location: LeftCoast

    ABoatGuy Member

  3. GhrarhG
    Joined: Oct 2005
    Posts: 4
    Likes: 0, Points: 0, Legacy Rep: 10
    Location: Glen Cove, NY

    GhrarhG New Member

    I did check the support page. That is where I learned how to write the macros that I currently have. I did not see any that had the loop command. I was not sure if this was possible. I am trying to create an interative process, and I assume GHS could do it since some of the commands like maxweight are solved for. I'm thinking macros may not be powerful enough for such tasks.

    Thanks,
    Jon
     
  4. GHS
    Joined: Oct 2005
    Posts: 11
    Likes: 0, Points: 1, Legacy Rep: 10
    Location: Port Townsend Wa

    GHS Junior Member

    Jon,

    Feel to to call GHS support at 360-385-6212 or send an email to support@ghsport.com with any questions.

    Mike Roth
    Manager
    Creative Sustems Inc.
     
  5. GHS
    Joined: Oct 2005
    Posts: 11
    Likes: 0, Points: 1, Legacy Rep: 10
    Location: Port Townsend Wa

    GHS Junior Member

    Introduction to Macros in GHS

    Introduction to Macros in GHS


    "Macros" are found in various kinds computer programs. The "mainline application" programs such as word processors, spread sheets and data-base programs often provide something called a "macro" facility. "Macro" literally means "large", and in computer jargon it generally refers to a command which may be made up of a large number of other commands.

    Most of the macro facilities found in application programs are built-in versions of what was a common utility a few years ago: the "keyboard macro" program. They function basically by recording keystrokes, allowing you to collect the keystrokes as a named "macro" and "play them back" at a later time by invoking the macro name.

    However such "macro" facilities are awkward and of limited use compared to an older tradition in which the macro is an extension of a programming language. Most assembly languages have macros of this type as do a few higher- level languages. Even DOS, which is itself a rudimentary language, has a macro facility known as the "batch file". It is this type of "language-based macro" which BHS implements; and in the following discussion, the term "macro" will be used to mean this kind of macro.

    The concept of the macro is very simple: a macro is a collection of commands which, when the macro name is invoked, causes those commands to be executed. This alone is a powerful thing; but its power is vastly increased when "parameter substitution" is performed.

    If you know a programming language such as FORTRAN, C, Pascal or a modern version of BASIC, you are familiar with the concept of the "subroutine" (or "subprogram" or "procedure", depending on the terminology of the particular language). These all have the ability to "pass parameters" which are referenced as symbols (usually variable names) within the subroutine. Although you can also "pass parameters" when invoking a macro, the interpretation of the parameters in macros is a little different.

    When you define a macro, you are simply storing a collection of text. The system does not require that it make sense at the time it is defined. Therefore, your parameter substitution may take place anywhere within the macro. You can even supply whole commands as parameters. This leads to an extremely broad range of possibilities and is what makes the macro so powerful. However, one of the liabilities of the macro, compared with the subroutine, is that the system cannot warn you of syntax errors within the macro until it is actually executed.

    In GHS, the form of a macro definition is

    MACRO name
    body
    /

    where "name" is an arbitrary name by which the macro is thereafter referenced and "body" is one or more lines of text. The single slash on a line by itself marks the end of the macro definition. If a macro by the same name already exists, it is replaced by the new definition. If "body" is missing, it is a "null" macro and the definition has no effect except that if a macro by the same name does exist, that macro is "erased" -- this is how you delete a macro.

    Other than storing a series of text lines, the MACRO command does nothing. Therefore it may appear anywhere in a command stream. It is usually placed in a Run File.

    Making use of a previously-defined macro simply requires the name of the macro preceded by a "dot" or period. Thus, if a macro named XYZ has been defined,

    .XYZ

    will cause the text in its "body" to executed as a series of commands.

    If parameters are involved in a macro definition, they are represented by the appearance, in the macro body, of the 2-character string "%n" where "n" is a single digit 1 through 9. This is the same convention which is used by DOS in batch files. For example,

    MACRO MSDRAFT
    DRAFT = %1 @ MS
    /

    defines a macro named MSDRAFT which contains one line of text. The "%1" is interpreted as "parameter #1 goes here". It's sometimes called a "dummy" parameter. If you execute this macro by saying

    .MSDRAFT 12

    then

    DRAFT = 12 @ MS

    is the actual command which the system will receive.

    Consider another example:

    MACRO LOAD
    TANK=%1
    TYPE=INTACT
    CONTENTS=%3
    LOAD=%2
    /

    This might be executed by

    .LOAD FO1.P, .95, SW

    yielding the command sequence

    TANK=FO1.P
    TYPE=INTACT
    CONTENTS=SW
    LOAD=.95

    Note that even though the macro is named LOAD, the same as the name of an intrinsic command, there is no confusion since a macro invocation is distinguished by the leading "dot".

    Also note that the dummy parameters can occur in any order within the body of the macro definition. At execution time, the order in which the actual parameters are given determines their identification with the %1, %2, etc. within the macro body.

    If there are more dummy parameters in the body than there are actual parameters given at the execution, the missing parameters will be considered to be empty, and the result of the substitution is that the extra dummy parameters are removed. For example,

    MACRO WT
    TANK= WINGTK%1%2
    /

    .WT 3, .S yields TANK= WINGTK3.S

    while

    .WT 3 yields TANK= WINGTK3

    A very important ability of GHS macros is that they can invoke other macros. For example,

    MACRO LDWT
    .WT %1, *
    TYPE= INTACT
    LOAD= %2
    CONTENTS= FO
    /

    MACRO LDWT1-3
    .LDWT 1, %1
    .LDWT 2, %1
    .LDWT 3, %1
    /

    and the execution,

    .LDWT1-3 .98

    would put 98% fuel oil in three (pairs of) tanks.

    As another example, consider the following macro:

    MACRO HEADING
    PAGE
    \****** Condition: %1 ******\
    /

    This would work fine if you passed it a single word, such as

    .HEADING DEPARTURE

    but what if you wanted to use two or more words? If you threw some extra dummy parameters into the definition (eg. "*** Condition: %1%2%3 ***"), there would be no spaces between the words; and "*** Condition: %1 %2 %3 ***" would give you an extra space or two if you happened to use less than three words.

    The better solution is to enclose a multi-word parameter in quotation marks. For example,

    .HEADING "Wing Tanks Damaged"

    would take all three words, strip off the quotation marks and then substitute them all for %1.

    Using the IF control command, you can make a macro do different things depending on some variable or parameter. For example, you can check that the proper number of parameters is supplied at execution time:

    MACRO LOAD
    IF "%3"="" THEN MESSAGE "Parameter missing! | EXIT
    TANK= %1
    TYPE= INTACT
    CONTENTS= %3
    LOAD= %2
    /

    Another powerful feature of macros is their ability to create or "spawn" other macros. This is often used when implementing menus in GHS. For example,

    MACRO WSET
    MACRO ESC
    EXIT MAIN
    //
    MENU " Weight setup " %1
    \
    \A\.ADD\Add a weight item
    \D\.DEL\Delete a weight item
    /

    spawns a little macro named ESC prior te executing the MENU command. Notice that MACRO ESC is treated as ordinary text when WSET is defined, with the exception that one of the slashes is removed from its terminating line. Thus, when WSET is executed, it right away presents the system with a macro definition.

    Incidentally, when a GHS process is aborted by your pressing the Escape key, it looks for the presence of a macro named ESC, and if it finds one it executes ESC instead of returning to the normal input stream.

    When a macro is defined within a macro as in the example above, you must be careful to avoid unintended parameter substitution in the inner macro. Actually, you cannot avoid parameter substitution because every %n within the macro is replaced when the macro is used. However you can use a little trick to get what you want anyway. For example, If the inner macro needs one parameter, you would like it to retain a %1 after parameter substitution has taken place. This can be done by putting %%91 in place of the %1, assuming that the parent macro will always have less than 9 parameters. The %9 is replaced by nothing, leaving the %1 you wanted.

    Although these brief examples were selected to demonstrate various features of GHS macros, the primary, overriding purpose in applying macros is to reduce repetition. Whenever you find that you have to repeat a certain sequence of commands several times, it is almost always better to construct a macro which contains the sequence and then execute the macro. This reduces the repetition within the text of the Run File and thereby reduces the chance of making an input error.

    While macros can be placed in any Run File (and you will find that most Run Files are more compact and easier to check when they use macros) it is sometimes convenient to organize general-purpose macros into separate "Library Files" so that they may be more conveniently used in any job. In other respects, a Library File is no different from any other Run File. When executing the GHS program from DOS, you can indicate that you want to have it read a given Library File by including the file name after the "/L" parameter.

    For some people, the best way to learn about using macros is to simply experiment with using them. Others learn better by studying examples. Examples of macro Library Files can be found in the GHS distribution files which have the file name extension ".LIB".
     
  6. GHS
    Joined: Oct 2005
    Posts: 11
    Likes: 0, Points: 1, Legacy Rep: 10
    Location: Port Townsend Wa

    GHS Junior Member

    Answer about GHS macro looping

    Just to supplement the macro information posted from GHS's rather useful website, the answer to Jon's question is that yes, GHS macros do support looping. If a macro is named MACNAME, just insert the EXIT MACNAME command to loop back up to the top of the macro. Along with the IF statement, this permits general-purpose looping and control structures.

    >the manuals are aweful.

    Jon, I think that must be a typo - you must have meant to describe GHS's manuals as "awesome"! :^)

    Seriously, I "resemble that remark" because I know firsthand that GHS's printed manuals and online GHSHELP information contain complete reference documentation on all aspects of the program, which are scrupulously updated at the time of each year's new release with the latest & greatest features and any corrections that might be required.

    That said, we could certainly do a better job creating tutorials and advertising about all of GHS's features so that people will know they exist and how to use them. Jon, thanks for pointing out that the issue of macro looping is not evident in the manual, and I'll try to make it clearer in future editions.

    In the meantime, I suggest that you go to the Wizard menu in GHS32, select "All" to see a complete list, choose a wizard, and click the "Edit" button. Most of these wizards are unprotected and represent living examples of the kind of complex and useful systems that can be programmed using the MACRO and TEMPLATE dialog box commands in GHS.

    Stephen Schumacher
    Senior Programmer
    Creative Systems Inc.
     
  7. GhrarhG
    Joined: Oct 2005
    Posts: 4
    Likes: 0, Points: 0, Legacy Rep: 10
    Location: Glen Cove, NY

    GhrarhG New Member

    GHS

    I did not mean to offend. I simply meant that with respect to the macro commands, the manuals seem to leave many things out. Simple things like explanations of possible commands, eg: sqrt, times, plus, atan, etc. The only place to learn these commands is from example on the website. However, none of these commands show a sqrt function or an arc sin function. I had to figure out how to write an arc sin using arc tan and the pythagorean theorem, and it seemed to me a page in the manual explaining all possible macro functions would have been a much better way to explain the possible commands rather than saying, see examples. No offense intended, I appologize.

    Jon
     
  8. GHS
    Joined: Oct 2005
    Posts: 11
    Likes: 0, Points: 1, Legacy Rep: 10
    Location: Port Townsend Wa

    GHS Junior Member

    Jon -

    No offense taken, as I tried to indicate by the smiley face in my reply. :^) Certain aspects of our manuals (such as the desireability of additional macro examples which you pointed out) could certainly bear improvement, so I appreciate your feedback.

    >Simple things like explanations of possible commands, eg: sqrt, times, plus,
    > atan, etc. The only place to learn these commands is from example on the
    > website. However, none of these commands show a sqrt function or an arc
    >sin function. I had to figure out how to write an arc sin using arc tan and
    >the pythagorean theorem

    There's a list of these operators in the manual (and online help) page for the SET command, which is the only command for which they are available. There is in fact a SQRT operator with that precise name. You're right on about how to calculate ARCSIN(X)=ARCTAN(X/(1-X*X)). If you use ARCSIN a lot, you can create a macro to perform that operation and deal with the special cases (e.g. when X*X=1).

    Stephen Schumacher
    Senior Programmer
    Creative Systems Inc.
     

  9. GHS
    Joined: Oct 2005
    Posts: 11
    Likes: 0, Points: 1, Legacy Rep: 10
    Location: Port Townsend Wa

    GHS Junior Member

    Ooops, that's ARCSIN(X)=ARCTAN(X/SQRT(1-X*X)).

    Stephen Schumacher
    Senior Programmer
    Creative Systems Inc.
     
Loading...
Forum posts represent the experience, opinion, and view of individual users. Boat Design Net does not necessarily endorse nor share the view of each individual post.
When making potentially dangerous or financial decisions, always employ and consult appropriate professionals. Your circumstances or experience may be different.