LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk: how to achieve a 64character column file from a list of FORTH commands (and other things) ? (https://www.linuxquestions.org/questions/programming-9/awk-how-to-achieve-a-64character-column-file-from-a-list-of-forth-commands-and-other-things-4175725681/)

floppy_stuttgart 06-03-2023 06:33 AM

awk: how to achieve a 64character column file from a list of FORTH commands (and other things) ?
 
Hello,
I have following file which I want to be restructured as output (a Forth program working on a Debian 64bits cross-development platform)

Code:

3.14159265359e FCONSTANT PINB
FVARIABLE B
FVARIABLE A
FVARIABLE TV
1.0e FVARIABLE R
DECIMAL
: PERE12
 B STO
 ." Par B: " F. CR CR
 X=0?
 IF F+
 4.0E0
 F*
 ." ELL PERI = " F. EXIT THEN
 X<>Y
 ." Par A: " F. CR CR
 A STO
 X=0?
 IF F+
 4.0E
 F*
 ." ELL PERI = " F. EXIT THEN
 FENTER F*
 X<>Y FENTER F*
 F+ TV STO
 FDROP
 1.0E0 R STO
 FDROP
 BEGIN
 A RCL FENTER
 B RCL FENTER
 RDN
 F*
 SQRT B STO
 RDN
 F-
 CHS
 R RCL
 2.0e
 F*
 R STO
 FDROP
 LASTX
 F/
 A RCL
 X<>Y
 F- A STO
 FDROP
 B RCL
 X<>Y
 LASTX X^2
 R RCL
 F* TV RCL F-
 CHS TV STO FDROP
 X=Y? UNTIL
 1/X
 TV RCL
 F*
 PINB
 F*
 ." ELL PERI = " F. CR ;
: TEST
 3.0e
 11.0e
 PERE12 ;


and would like to have it reformatted with awk in following output

Code:

3.14159265359E0 FCONSTANT PINB
FVARIABLE B
FVARIABLE A
FVARIABLE TV
1.0E0 FVARIABLE R
DECIMAL
: PERE12 B STO ." Par B: " F. CR CR X=0? IF F+ 4.0E0 F*
." ELL PERI = " F. EXIT THEN X<>Y ." Par A: " F. CR CR A
STO X=0? IF F+ 4.0E0 F* ." ELL PERI = " F. EXIT THEN
FENTER F* X<>Y FENTER F* F+ TV STO FDROP 1.0E0 R STO FDROP
BEGIN A RCL FENTER B RCL FENTER RDN F* SQRT B STO RDN F- CHS R
RCL 2.0E0 F* R STO FDROP LASTX F/ A RCL X<>Y F- A STO FDROP B
RCL X<>Y LASTX X^2 R RCL F* TV RCL F- CHS TV STO FDROP X=Y?
UNTIL 1/X TV RCL F* PINB F* ." ELL PERI = " F. CR ;
: TEST 3.0E0 11.0E0 PERE12 ;

why awk? because the first file above is a result of a filter with awk therefore I would like to keep awk.

I achieved the second posted file with manual work. Thats ok: take not much time. But for the "beauty", it would be good if the output is achieved straight with awk.

Target:
a) rework all lines between ": " and " ;" only (for : and ; not within brackets "")
b) file output with with 64 characters columns
c) file recognizing 1.0e or 4.0E and changing it to e0 or E0 (= 1.0E0 or 4.0E0) with no number after "E" exponent of the number
d) .... (more to come.. but of lower priority)

Any advice is welcome. No hurry; manually is so far fine. Thanks to everyone.

NevemTeve 06-03-2023 07:23 AM

What is this line supposed to do?
Code:

1.0E0 FVARIABLE R
I would guess it does the same as
Code:

FVARIABLE R ( ___ )
1.0E0      ( ___ 1.0 )


floppy_stuttgart 06-03-2023 10:13 AM

Quote:

Originally Posted by NevemTeve (Post 6434470)
What is this line supposed to do?
Code:

1.0E0 FVARIABLE R
I would guess it does the same as
Code:

FVARIABLE R
1.0E0


Ahh.. perhaps you identified something I missed in my program.
The target was to initialize the float variable R with 1.0 but since you are asking, the correct would be "FVARIABLE R" only (no 1.0 before or after) since I initialize later to 1.0. Good remark/question. (however it worked, only it had the side effect I just understood now, a 1.0 appeared in the float stack). I am a beginner (this is the first Forth program in my life).

shruggy 06-13-2023 06:35 AM

Perhaps not quite what you wanted, but could be the first step
Code:

#!/usr/bin/awk -f
BEGIN{wrap=54}
/^:/{
        line=""
        flag=1
}
flag{
        line=line $0
        if (length(line)>wrap){
                print line
                line=""
                next
        }
}
!flag{print}
/;$/{
        flag=0
        if (length(line)<=wrap) print line
}


MadeInGermany 06-13-2023 12:54 PM

The following variant looks for the ending semicolon only in the formatted block, and then prints it unconditionally.
Code:

#!/usr/bin/awk -f
BEGIN{wrap=54}
/^:/{
        line=""
        flag=1
}
flag{
        line=line $0
        if (/;$/){
          flag=0
          print line
          next
        }
        if (length(line)>wrap){
                print line
                line=""
        }
        next
}
{print}


floppy_stuttgart 06-16-2023 10:10 AM

Thanks. I am using it in a terminal. Good.
awk -f filter1 PERE12.fth > temp.txt
awk -f filter2 temp.txt > RESULT.SRC
Is there any possibility to make only 1 command out of the 2 above?
(filter2 is from the post above)

shruggy 06-16-2023 10:21 AM

What does filter1 do?

floppy_stuttgart 06-16-2023 12:04 PM

Quote:

Originally Posted by shruggy (Post 6436857)
What does filter1 do?

verbal description below (since the awk script is a problem under my account to be uploaded; as txt or as pdf; I receive the message "
Sorry, you have been blocked You are unable to access linuxquestions.org").

Code:

# This program helps for filtering commented files on a PC.
# The cleaned-up files with this script will be later uploaded
# into the HP programming board
# It will delete following type of comments:
#  xxxx \ yyyy  -> changed to xxxx (deleted from \ on)
#  D_xxx in a line -> line deleted
#  G_xxx in a line -> changed to xxx (G_ deleted)
#  line with blanks -> whole line deleted
#  " ( .... )" identified -> replaced by "  "
#  nothing in a line = only <cr> or <tab> -> whole line deleted
#


sundialsvcs 06-16-2023 01:48 PM

Unfortunately for all of us, "FORTH" is a thoroughly obsolete technology which, much like "MUMPS," still refuses to die.

When "digital computers were still too weak to get out of their own way," these systems actually made sense. (And, yes, I definitely remember those days, entirely without(!) nostalgia.)

MadeInGermany 06-17-2023 01:26 AM

Quote:

Originally Posted by sundialsvcs (Post 6436899)
Unfortunately for all of us, "FORTH" is a thoroughly obsolete technology which, much like "MUMPS," still refuses to die.

When "digital computers were still too weak to get out of their own way," these systems actually made sense. (And, yes, I definitely remember those days, entirely without(!) nostalgia.)

Perhaps you think that all current firmware is written in X86 assembler.
But let me cite some Web articles.

Quote:

Forth is still used today in many embedded systems (small computerized devices) because of its portability, efficient memory use, short development time, and fast execution speed.
Quote:

Forth has a niche in astronomical and space applications as well as a history in embedded systems. The Open Firmware boot ROMs used by Apple, IBM, Sun, and OLPC XO-1 contain a Forth environment. Forth has often been used to bring up new hardware.

shruggy 06-17-2023 04:47 AM

Quote:

Originally Posted by floppy_stuttgart (Post 6436883)
Code:

# This program helps for filtering commented files on a PC.
# The cleaned-up files with this script will be later uploaded
# into the HP programming board
# It will delete following type of comments:
#  xxxx \ yyyy  -> changed to xxxx (deleted from \ on)
#  D_xxx in a line -> line deleted
#  G_xxx in a line -> changed to xxx (G_ deleted)
#  line with blanks -> whole line deleted
#  " ( .... )" identified -> replaced by "  "
#  nothing in a line = only <cr> or <tab> -> whole line deleted
#


I don't see why you cannot do both things with one awk script.

MadeInGermany 06-17-2023 11:49 AM

Quote:

" ( .... )" identified -> replaced by " "
Please give examples for input and desired output!
Or copy/paste the relevant awk code.

MadeInGermany 06-19-2023 05:12 AM

After studying the forth description in Wikipedia I have made the following:
Code:

#!/usr/bin/awk -f
BEGIN { wrap=64 }

# Delete \ comment
{ sub(/\\ .*/, "") }

# Delete line that starts with D_
$1 ~ /^D_/ { next }

# Delete a G_ prefix
$1 ~ /^G_/ { sub(/G_/, "") }

# Delete ( Comment )
{ gsub(/[(] [^)]* [)]/, "") }

# Delete blank line
/^[[:space:]]*$/ { next }

# Format : block ;
$1 == ":" {
  line=""
  flag=1
}
flag {
  nline=(line $0)
  if (length(nline) > wrap) {
    print line
    line=$0
  } else {
    line=nline }
  if ($NF == ";") {
    flag=0
    print line
  }
  next
}
{ print }

It also has the line wraps at 64 boundary as required.

bigearsbilly 06-21-2023 07:37 AM

I like FORTH too, in the Pioneer days there were lots of interesting, small, efficient languages.
I have it on a Rasperry Pico at work to amuse myself while I am downloading Python frameworks for money.

Of course, you should really be writing the FORTH formatter in FORTH.

floppy_stuttgart 06-23-2023 11:13 AM

Quote:

Originally Posted by MadeInGermany (Post 6437004)
Please give examples for input and desired output!
Or copy/paste the relevant awk code.

Code:

: PERE12 ( r1=a  r2=b  -- r=perimeter )
    \    X      Y      Z      T      L    later on HP STACK
    \    b      a      ..      ..      ..
    B STO
    \
    ." Param B:  " G_F. CR CR        \ optional Show X doesnt alter the content of X
    ." Optional which will be deleted later with awk script" B D_F.@ CR          \ optional show the content of the variable
    ." line 131 in emacs of PERE12 " D_RPNS              \ typical optional line to show the content of the stacks
    \
    X=0?                              \ assumption A & B in Y and X
    \
    IF G_F+
        ." line 136  " D_RPNS              \ optional show the content of the stacks
        4.0E0
        \ ." line 138  " D_RPNS              \ optional show the content of the stacks
        G_F*
        ." ELLIPSE PERIM = " G_F. EXIT THEN
    \ ." line 141  " D_RPNS              \ optional show the content of the stacks
    X<>Y
    \ ." line 143  " D_RPNS              \ optional show the content of the stacks
    ." Param A:  " G_F. CR CR
    A STO
\    A D_F.@        \  optional show the content of the variable
    \
    \ ." line 85  " D_RPNS
    \
    X=0?                                \ A      B     
    IF G_F+
        \ ." line 152  " D_RPNS              \ optional show the content of the stacks
        4.0E0
        \ ." line 154  " D_RPNS              \ optional show the content of the stacks
        G_F*
        \ ." line 156  " D_RPNS              \ optional show the content of the stacks
        ." ELLIPSE PERIM = " G_F. EXIT THEN
    \
    \ ." line 159  " D_RPNS              \ optional show the content of the stacks
    \
    FENTER G_F*                          \ A*A    B
    \
    \ ." line 163  " D_RPNS              \ optional show the content of the stacks
    \   
    X<>Y FENTER G_F*                    \ B*B    A*A
    \
    \ ." line 167  " D_RPNS              \ optional show the content of the stacks
    \
    G_F+ TV STO                          \ A^2+B^2 (stored in TV)
    \
    \ ." line 171  " D_RPNS              \ optional show the content of the stacks
    \
    G_FDROP                  \  why cleaning up the stack? necessary?
    1.0E0 R STO
    G_FDROP                  \  why cleaning up the stack? necessary?
\
    BEGIN
        A RCL FENTER                    \ An      An
        \ ." line 179  " D_RPNS          \ optional show the content of the stacks
        B RCL FENTER                    \ Bn      Bn    An    An
        RDN                              \ Bn      An    An    Bn
        \ ." line 182  " D_RPNS          \ optional show the content of the stacks
        G_F*                            \ Bn*An  An    Bn    Bn
        \ ." line 184  " D_RPNS          \ optional show the content of the stacks
        SQRT B STO                      \ SQRT(An*Bn)=BN  An  Bn    Bn
        \ ." line 186  " D_RPNS          \ optional show the content of the stacks
        RDN                              \ An      Bn      Bn      BN
        G_F-                            \ Bn-An  Bn      BN      BN
        \ ." line 189  " D_RPNS          \ optional show the content of the stacks
        CHS                              \ An-Bn  Bn      BN      BN
        \ ." line 191  " D_RPNS          \ optional show the content of the stacks
        R RCL                            \ Rn      An-Bn  Bn      BN
        \ ." line 193  " D_RPNS          \ optional show the content of the stacks
        2.0e0                            \ 2.0e    Rn      An-Bn  Bn
        G_F*                            \ 2*Rn    An-Bn  Bn      Bn
        \ ." line 196  " D_RPNS          \ optional show the content of the stacks
        R STO                            \ 2*Rn=RN  An-Bn  Bn    Bn
        G_FDROP                          \ An-Bn  Bn      Bn      Bn
        \ ." line 199  " D_RPNS          \ optional show the content of the stacks
        LASTX                            \ 2.0e    An-Bn  Bn      Bn
        \ ." line 201  " D_RPNS          \ optional show the content of the stacks
        G_F/                            \ (An-Bn)/2.  Bn  Bn      Bn
        \ ." line 203  " D_RPNS          \ optional show the content of the stacks
        A RCL                            \ An  (An-Bn)/2  Bn    Bn
        \ ." line 205  " D_RPNS          \ optional show the content of the stacks
        X<>Y                            \ (An-Bn)/2  An  Bn  Bn
        \ ." line 207  " D_RPNS          \ optional show the content of the stacks
        G_F- A STO                      \ (An+Bn)/2=AN  Bn  Bn  Bn  L..(An-Bn)/2.
        \ ." line 209  " D_RPNS          \ optional show the content of the stacks
        G_FDROP                          \ Bn  Bn  Bn  Bn
        \ ." line 211  " D_RPNS          \ optional show the content of the stacks
        B RCL                            \ BN  Bn  Bn  Bn
        X<>Y                            \ Bn  BN  Bn  Bn
        \ ." line 214  " D_RPNS          \ optional show the content of the stacks
        LASTX X^2
        \ ." line 216  " D_RPNS          \ optional show the content of the stacks
        R RCL                            \ RN ((An-Bn)/2.)^2  Bn  BN
        G_F* TV RCL G_F-                  \ -T+RN*((An-Bn)/2)^2 Bn  BN  BN
        \ ." line 219  " D_RPNS          \ optional show the content of the stacks
        CHS TV STO G_FDROP                \ Bn BN BN BN
        ." line 170  " D_RPNS          \ optional show the content of the stacks
\        WTF??    \ this was included here for testing the awk script with tab in this file
\
    X=Y? UNTIL
    \ ." line 225  " D_RPNS          \ optional show the content of the stacks
    1/X
    \ ." line 227  " D_RPNS          \ optional show the content of the stacks
    TV RCL
    \ ." line 229  " D_RPNS          \ optional show the content of the stacks
    G_F*
    \ ." line 231  " D_RPNS          \ optional show the content of the stacks
    PINB
    \ ." line 233  " D_RPNS          \ optional show the content of the stacks
    G_F*
    \ ." line 235  " D_RPNS          \ optional show the content of the stacks
    \
    \ ***********      OUTPUT the result  *************
    ." ELLIPSE PERIM = " G_F. CR ;

For any reason the awk code cannot be uploaded between "code".

So far, now I am using a script progfilter.sh
Quote:

#!/bin/bash
awk -f hp71bccfilter FILEIN.fth > temp.txt
awk -f forthbuild64column temp.txt > FILEOUT.SRC
And I am happy. Could be in one command instead of 2 commands.. but whatever, it works therefore I move to other tasks.


All times are GMT -5. The time now is 05:35 PM.