Author Topic: Get Variable Name as string  (Read 4304 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Get Variable Name as string
« on: May 05, 2020, 09:58:22 am »
Hello. I was trying to get the name of the variables used in QB64 as they appear in the source code. I tried using the stringify method from C++ but of course it gives me the C++ interpretation that QB64 does which gives it an entirely new name. Is there a way to get the name of the variable used in the QB64 source in a similar fashion as to how it is done in C++ using the stringify preprocessor? It would be quite an amazing thing if so.

Code: QB64: [Select]
  1. DECLARE LIBRARY "getvariablename"
  2.     FUNCTION GET_VARIABLE_NAME$ (variable AS STRING)
  3. PRINT GET_VARIABLE_NAME(test$)  

Output of code: (char*)(__STRING_TEST)->chr
 [ You are not allowed to view this attachment ]  
« Last Edit: May 05, 2020, 10:04:44 am by SpriggsySpriggs »
Shuwatch!

FellippeHeitor

  • Guest
Re: Get Variable Name as string
« Reply #1 on: May 05, 2020, 10:37:19 am »
I don't know if there is way, I'm just curious about your motivation. What do you intend to accomplish?

Also: For the final binary, the BASIC code never existed, so __STRING_TEST is as close as I think you'll get.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Variable Name as string
« Reply #2 on: May 05, 2020, 10:47:42 am »
I don't know if there is way, I'm just curious about your motivation. What do you intend to accomplish?
What I'm hoping to do is match MySQL column names to local variable names. For example:
Code: QB64: [Select]
  1. IF DB_RESULT(1,1) = GET_VARIABLE_NAME(customerId) THEN
  2. 'do stuff
This might help me to make my code more accurate in database queries. Perhaps. Maybe not. I do something similar in C# when using a dataset and accessing it like this:
string customerId = EdiCustomers["customerId"].ToString()
Doing so grabs the data at the column name customerId and assigns it to the local variable of the same name. I was thinking that if I at least had the ability to access the name of the local variable then I could do something along the same lines.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Get Variable Name as string
« Reply #3 on: May 05, 2020, 11:05:11 am »
« Last Edit: May 05, 2020, 11:07:58 am by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Get Variable Name as string
« Reply #4 on: May 05, 2020, 02:32:56 pm »
@Mark: Okay, now after reading this thread I understand the post you linked from here.

To me, reading the variables in the source code means opening the source file, and parsing out things like A$, x!,type variable names, etc. So not the values represented, but the actual variable names used. That's what I'm getting from this thread, but I'm not going to go in depth with how to do it, because my spidey sense tells me I may be barking up the wrong programing tree.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get Variable Name as string
« Reply #5 on: May 05, 2020, 02:40:15 pm »
...opening the source file, and parsing out things like A$, x!,type variable names, etc. So not the values represented, but the actual variable names used.

Exactly, Pete. I'm wanting to get the actual name as represented before compilation. Something that can get the value as how QB64 sees it in the IDE rather than during the compilation process when it gets converted to C++ code. I saw B+'s idea but it doesn't really do what I'm looking for. I suppose hard coding a variable with the name of another variable (what B+ showed) is a way of doing it.
Shuwatch!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Get Variable Name as string
« Reply #6 on: May 05, 2020, 03:04:53 pm »
To do that:

1) In your program, open the source code for BINARY or INPUT. BINARY read is a lot faster.

2) In your code, write a sub-routine that parses out all the variables and then eliminates the duplicates. That will leave you with an array filled with unique variables, to compare to your database.

Now writing the code to parse out variables is easy for strings that end in $, or numeric variables that end in %, !, etc. If you have types, DIM variables as strings, or don't use numeric identifiers like %,!, etc., that code gets a lot more complicated to write.

I made a rudimentary variable retrieving routine to help me keep up with the variable names I had in really large programs. It didn't do some of the more complicated stuff I mentioned, so I never published it. Without the more complicated parsing abilities, it just wouldn't be all purpose enough for everyone.

I'm out for a bit, but I'll return to this thread tonight. If it helps, I can dig up what I have, I think I know what computer I have it on, and if you could understand it, you could build on it, if needed.

Of course by posting what I did above, when I come back tonight there will be at least one post with a variable detection routine here, but hey, that's a good thing. I'm not the only genius here.... just the best looking one. :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Get Variable Name as string
« Reply #7 on: May 05, 2020, 03:12:31 pm »
Well I can see how you can get a list of variable names and the task would be made easier with OPTION _EXPLICIT where you are forced to DIM everything, UDT's might be tricky.

I am curious how you will connect the variable names to ... what? after complied.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Get Variable Name as string
« Reply #8 on: May 05, 2020, 05:10:56 pm »
Half- followed this thread so forgive me but what about vwatch?
You're not done when it works, you're done when it's right.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Get Variable Name as string
« Reply #9 on: May 05, 2020, 08:48:28 pm »
Dig some digging and found a couple. Wow, way back to 2000!

Code: QB64: [Select]
  1. ' It doesn't account for variables that are arrays or those that are found in the LINE() command.
  2. ' >No solution so far.
  3.  
  4. ' It cannot yet differentiate the following: 4! * abc... only picks up 4! as a variable.
  5.  
  6. ' Finally, there was a request for a comma separation output. This would not be difficult. Just adjust the way the data is added to the file.
  7. ' PRINT #F1, A$,
  8.  
  9. ' The variables and strings can be printed in all caps with the ucase$(A$) or A$ = Ucase$(A$).
  10.  
  11. '============================================================================
  12.  
  13. 'Pete's String and Variable finder.
  14.  
  15. 'Finds marked %#!& and unmarked variables as well as strings in any program.
  16.  
  17. 'Opens a table of the findings in Notepad.
  18.  
  19. 'WARNING >>> This program makes a file called 'STRVAR.DAT' in the current
  20. ' directory. If, by coincidence, a file with this name already
  21. ' exits in the current directory, it will be overwritten. Take
  22. ' any necessary precautions to prevent this before running the
  23. ' program.
  24.  
  25. ' A second file called 'TEMPDIR.TMP" is also created if a long-
  26. ' pathway name is input such as Program Files instead of progra~1.
  27.  
  28. ' Remove one of the SHELL commands. One is for Win 95-98, the other 2000/NT/XP
  29. ' If both lines remain, the program will run. A 'bad command or file name'
  30. ' comment will be the only result.
  31.  
  32. ' The program supports searching modules joined by a .MAK file. Input the .MAK
  33. ' file name at the input line and the program will automatically search each
  34. ' of the modules in the .MAK file.
  35.  
  36. '============================================================================
  37. DECLARE SUB LONGPATH (DRIVEX$, PATHWAY$, ORIGDIR$)
  38. COLOR 0, 0
  39. ON ERROR GOTO RESNXT
  40. FILES "XXX.~~~"
  41. LOCATE 1, 1
  42. FOR I = 1 TO 80
  43.     A$ = A$ + CHR$(SCREEN(1, I))
  44. A$ = RTRIM$(A$)
  45. COLOR 7, 0
  46.  
  47. DRIVEX$ = MID$(A$, 1, 3)
  48. PATHWAY$ = MID$(A$, 4)
  49. ORIGDIR$ = DRIVEX$ + PATHWAY$
  50.  
  51. REDIM X$(1000), Y$(1000), Z$(1000)
  52. F1 = FREEFILE: OPEN ORIGDIR$ + "\STRVAR.DAT" FOR INPUT AS F1: CLOSE #F1
  53.  
  54. IF ER1% = 0 THEN
  55.     PRINT " ALERT >>> THIS DIRECTORY CONTAINS A FILE NAMED 'STRVAR.DAT' THIS FILE"
  56.     PRINT " WILL BE OVERWRITTEN. DO NOT CONTINUE IF THIS IS NOT OK. - INPUT: EXIT"
  57.     PRINT " THE 'STRVAR.DAT' FILE WILL BE CREATED IN THIS DIRECTORY: "; ORIGDIR$
  58.     PRINT " NO FILE CONFLICT WAS FOUND."
  59. ER1% = 0
  60.  
  61. PRINT " TYPE 'EXIT' TO QUIT AT ANY INPUT PROMPT"
  62. PRINT " INPUT THE DRIVE THE FILE IS IN [A:\, B:\, C:\]: ";: XX% = CSRLIN: YY% = POS(1)
  63. COLOR 15, 0: LOCATE XX%, YY%: PRINT DRIVEX$: LOCATE XX%, YY%
  64. GOSUB READSCREEN
  65. DRIVEX$ = UCASE$(A1$)
  66.  
  67. IF UCASE$(A1$) = "EXIT" OR UCASE$(A1$) = "'EXIT'" THEN SYSTEM
  68.  
  69.  
  70. PRINT " INPUT THE DIRECTORY THE FILE IS IN: ";: XX% = CSRLIN: YY% = POS(1)
  71. COLOR 15, 0: LOCATE XX%, YY%: PRINT PATHWAY$: LOCATE XX%, YY%
  72. GOSUB READSCREEN
  73. PATHWAY$ = UCASE$(A1$)
  74.  
  75. IF UCASE$(A1$) = "EXIT" OR UCASE$(A1$) = "'EXIT'" THEN SYSTEM
  76.  
  77. REM CHECKS FOR LONG-PATHWAY NAMES
  78. IF INSTR(PATHWAY$, "\") <> 0 THEN
  79.     HOLDPATH$ = PATHWAY$: MODPATH$ = PATHWAY$ + "\": J% = 1
  80.     DO
  81.         PATHWAY$ = MID$(MODPATH$, J%, INSTR(J%, MODPATH$, "\") - J%)
  82.         J% = J% + LEN(PATHWAY$) + 1
  83.         GOSUB CKPATH
  84.         IF MSDOSPATH$ <> "" THEN MSDOSPATH$ = MSDOSPATH$ + "\" + PATHWAY$ ELSE MSDOSPATH$ = PATHWAY$
  85.         IF J% > LEN(HOLDPATH$) THEN EXIT DO
  86.     LOOP
  87.     PATHWAY$ = MSDOSPATH$
  88.     GOSUB CKPATH
  89.  
  90.  
  91. PRINT " INPUT THE .BAS FILE OR INPUT THE .MAK FILE: ";: XX% = CSRLIN: YY% = POS(1)
  92. COLOR 15, 0: LOCATE XX%, YY%
  93. GOSUB READSCREEN
  94.  
  95. IF UCASE$(A1$) = "EXIT" OR UCASE$(A1$) = "'EXIT'" THEN SYSTEM
  96.  
  97. IF INSTR(A1$, ".") = 0 THEN A1$ = A1$ + ".BAS": REM ADDS A BASIC EXTENSION IF LEFT OFF.
  98.  
  99. LOCATE 13, 1
  100.  
  101. ORIGFILENAME$ = UCASE$(A1$)
  102. FILENAME$ = DRIVEX$ + PATHWAY$ + "\" + UCASE$(A1$)
  103.  
  104. ON ERROR GOTO CHECKFILE
  105. REM CHECK FOR FILE
  106. F2 = FREEFILE: OPEN FILENAME$ FOR INPUT AS #F2: CLOSE #2
  107.  
  108. IF MID$(FILENAME$, LEN(FILENAME$) - 3, 4) = ".MAK" THEN
  109.     F2 = FREEFILE: OPEN FILENAME$ FOR INPUT AS #F2
  110.     MAK% = 1
  111.  
  112.     IF MAK% = 1 THEN
  113.         IF NOT EOF(F2) THEN
  114.             LINE INPUT #F2, FILENAME$
  115.             FILENAME$ = DRIVEX$ + PATHWAY$ + "\" + UCASE$(FILENAME$)
  116.         ELSE
  117.             EXIT DO
  118.         END IF
  119.     END IF
  120.  
  121.     PRINT " SEARCHING FILE: "; FILENAME$
  122.  
  123.     F1 = FREEFILE
  124.     OPEN FILENAME$ FOR INPUT AS #F1
  125.     DO
  126.         LINE INPUT #F1, A$
  127.         REM - REMOVE ALL QUOTED SEGMENTS
  128.         DO
  129.             IF INSTR(A$, CHR$(34)) <> 0 THEN
  130.                 AX1$ = MID$(A$, 1, INSTR(A$, CHR$(34)) - 1)
  131.                 AX2$ = MID$(A$, INSTR(LEN(AX1$) + 2, A$, CHR$(34)) + 1)
  132.                 A$ = AX1$ + AX2$
  133.                 K% = INSTR(A$, CHR$(34)): IF K% = 0 THEN EXIT DO ELSE IF INSTR(K% + 1, A$, CHR$(34)) = 0 THEN EXIT DO: REM ONLY ON QUOTE MARK.
  134.             ELSE
  135.                 EXIT DO
  136.             END IF
  137.         LOOP
  138.  
  139.         IF INSTR(A$, "=") OR INSTR(A$, "$") OR INSTR(A$, "%") OR INSTR(A$, "!") OR INSTR(A$, "#") OR INSTR(A$, "&") THEN
  140.             A2$ = LTRIM$(RTRIM$(A$))
  141.             DO
  142.                 IF INSTR(A2$, " ") = 0 THEN A1$ = A2$ ELSE A1$ = MID$(A2$, 1, INSTR(A2$, " "))
  143.                 IF INSTR(A1$, "$") <> 0 THEN
  144.                     IF MID$(A1$, 1, 4) <> "MID$" AND MID$(A1$, 1, 6) <> "LTRIM$" AND MID$(A1$, 1, 6) <> "RTRIM$" AND MID$(A1$, 1, 6) <> "RIGHT$" AND MID$(A1$, 1, 5) <> "LEFT$" AND MID$(A1$, 1, 6) <> "SPACE$" AND MID$(A1$, 1, 7) <> "STRING$" THEN
  145.                         IF MID$(A1$, 1, 6) <> "LCASE$" AND MID$(A1$, 1, 6) <> "UCASE$" AND MID$(A1$, 1, 5) <> "INSTR" AND MID$(A1$, 1, 6) <> "INKEY$" AND MID$(A1$, 1, 6) <> "IOCTL$" AND MID$(A1$, 1, 5) <> "TIME$" AND MID$(A1$, 1, 4) <> "CHR$" THEN
  146.                             IF MID$(A1$, 1, 7) <> "VARPTR$" AND MID$(A1$, 1, 4) <> "MKD$" AND MID$(A1$, 1, 7) <> "MKDMBF$" AND MID$(A1$, 1, 4) <> "MKI$" AND MID$(A1$, 1, 4) <> "MKL$" AND MID$(A1$, 1, 4) <> "MKS$" AND MID$(A1$, 1, 4) <> "STR$" THEN
  147.                                 IF MID$(A1$, 1, 7) <> "MKSMBF$" AND MID$(A1$, 1, 4) <> "OCT$" AND MID$(A1$, 1, 4) <> "HEX$" AND MID$(A1$, 1, 9) <> "'$DYNAMIC" AND MID$(A1$, 1, 8) <> "'$STATIC" AND MID$(A1$, 1, 9) <> "'$INCLUDE" AND MID$(A1$, 1, 3) <> "LEN" THEN
  148.                                     IF MID$(A1$, 1, 8) <> "$DYNAMIC" AND MID$(A1$, 1, 8) <> "$STATIC" AND MID$(A1$, 1, 8) <> "$INCLUDE" THEN
  149.                                         A3$ = A1$
  150.                                         IF MID$(A3$, 1, 1) <> CHR$(34) THEN
  151.                                             IF INSTR(A3$, "$") < LEN(A3$) THEN A3$ = MID$(A3$, 1, INSTR(A3$, "$"))
  152.                                             IF MID$(A3$, 1, 1) = "(" THEN A3$ = MID$(A3$, 2)
  153.                                             IF INSTR(A3$, "(") <> 0 AND INSTR(A3$, ")") = 0 OR INSTR(A3$, "'") <> 0 THEN
  154.                                             ELSE
  155.                                                 A3$ = RTRIM$(LTRIM$(UCASE$(A3$)))
  156.                                                 I = 0
  157.                                                 DO
  158.                                                     I = I + 1
  159.                                                     IF X$(I) = A3$ THEN J% = -1: EXIT DO
  160.                                                     IF X$(I) = "" THEN X$(I) = A3$: EXIT DO
  161.                                                 LOOP
  162.                                             END IF
  163.                                         END IF
  164.                                     END IF
  165.                                 END IF
  166.                             END IF
  167.                         END IF
  168.                     END IF
  169.                 END IF
  170.  
  171.                 REM VARIABLES !#%&
  172.                 RESTORE VARIABLES
  173.                 DO
  174.                     READ VAR1$
  175.                     IF VAR1$ = "EOF" THEN EXIT DO
  176.                     IF INSTR(A1$, VAR1$) <> 0 THEN
  177.                         A3$ = A1$
  178.                         A3$ = RTRIM$(LTRIM$(UCASE$(A3$)))
  179.                         IF MID$(A3$, LEN(A3$), 1) = "," THEN A3$ = MID$(A3$, 1, LEN(A3$) - 1)
  180.                         IF MID$(A3$, 1, 1) = "(" THEN A3$ = MID$(A3$, 2)
  181.                         IF MID$(A3$, 1, 1) = VAR1$ OR INSTR(A3$, VAR1$) < LEN(A3$) AND MID$(A3$, INSTR(A3$, VAR1$) + 1, 1) <> "(" THEN
  182.                         ELSE
  183.                             IF INSTR(A3$, "(") <> 0 AND INSTR(A3$, ")") = 0 OR INSTR(A3$, "'") <> 0 THEN
  184.                             ELSE
  185.                                 I = 0
  186.                                 DO
  187.                                     I = I + 1
  188.                                     IF Y$(I) = A3$ THEN J% = -1: EXIT DO
  189.                                     IF Y$(I) = "" THEN Y$(I) = A3$: EXIT DO
  190.                                 LOOP
  191.                             END IF
  192.                         END IF
  193.                     END IF
  194.                 LOOP
  195.  
  196.                 REM UNMARKED VARIABLES
  197.                 VAR1$ = "="
  198.                 IF INSTR(A1$, VAR1$) <> 0 THEN
  199.                     A3$ = OLDA3$
  200.                     A3$ = RTRIM$(LTRIM$(UCASE$(A3$)))
  201.  
  202.                     IF MID$(A3$, 1, 4) <> "MID$" AND MID$(A3$, 1, 6) <> "LTRIM$" AND MID$(A3$, 1, 6) <> "RTRIM$" AND MID$(A3$, 1, 6) <> "RIGHT$" AND MID$(A3$, 1, 5) <> "LEFT$" AND MID$(A3$, 1, 6) <> "SPACE$" AND MID$(A3$, 1, 7) <> "STRING$" THEN
  203.                         IF MID$(A3$, 1, 6) <> "LCASE$" AND MID$(A3$, 1, 6) <> "UCASE$" AND MID$(A3$, 1, 5) <> "INSTR" AND MID$(A3$, 1, 6) <> "INKEY$" AND MID$(A3$, 1, 6) <> "IOCTL$" AND MID$(A3$, 1, 5) <> "TIME$" AND MID$(A3$, 1, 4) <> "CHR$" THEN
  204.                             IF MID$(A3$, 1, 7) <> "VARPTR$" AND MID$(A3$, 1, 4) <> "MKD$" AND MID$(A3$, 1, 7) <> "MKDMBF$" AND MID$(A3$, 1, 4) <> "MKI$" AND MID$(A3$, 1, 4) <> "MKL$" AND MID$(A3$, 1, 4) <> "MKS$" AND MID$(A3$, 1, 4) <> "STR$" THEN
  205.                                 IF MID$(A3$, 1, 7) <> "MKSMBF$" AND MID$(A3$, 1, 4) <> "OCT$" AND MID$(A3$, 1, 4) <> "HEX$" AND MID$(A3$, 1, 9) <> "'$DYNAMIC" AND MID$(A3$, 1, 8) <> "'$STATIC" AND MID$(A3$, 1, 9) <> "'$INCLUDE" AND MID$(A3$, 1, 3) <> "LEN" THEN
  206.                                     IF MID$(A3$, 1, 8) <> "$DYNAMIC" AND MID$(A3$, 1, 8) <> "$STATIC" AND MID$(A3$, 1, 8) <> "$INCLUDE" THEN
  207.  
  208.                                         IF A3$ <> "" THEN
  209.                                             IF MID$(A3$, LEN(A3$), 1) <> "$" AND A3$ <> "LEN" AND MID$(A3$, LEN(A3$), 1) > "A" AND MID$(A3$, LEN(A3$), 1) < "Z" THEN
  210.                                                 IF INSTR(A3$, "'") = 0 THEN
  211.                                                     I = 0
  212.                                                     DO
  213.                                                         I = I + 1
  214.                                                         IF Z$(I) = A3$ THEN J% = -1: EXIT DO
  215.                                                         IF Z$(I) = "" THEN Z$(I) = A3$: EXIT DO
  216.                                                     LOOP
  217.                                                 END IF
  218.                                             END IF
  219.                                         END IF
  220.                                     END IF
  221.                                 END IF
  222.                             END IF
  223.                         END IF
  224.                     END IF
  225.                 END IF
  226.  
  227.                 OLDA3$ = A1$
  228.                 A2$ = MID$(A2$, LEN(A1$) + 1)
  229.                 IF A2$ = "" THEN EXIT DO
  230.             LOOP
  231.         END IF
  232.     LOOP UNTIL EOF(F1)
  233.     CLOSE F1
  234.     IF MAK% = 0 THEN EXIT DO
  235.  
  236. F1 = FREEFILE: OPEN ORIGDIR$ + "\STRVAR.DAT" FOR OUTPUT AS F1
  237.  
  238. A$ = "TABLE OF VARIABLES AND STRINGS FOR: " + ORIGFILENAME$
  239. PRINT #F1, A$
  240. PRINT #F1, ""
  241.  
  242. I = 0
  243.     I = I + 1
  244.     IF X$(I) = "" THEN I = I - 1: EXIT DO
  245.     NUM$ = LTRIM$(STR$(I))
  246.     SELECT CASE LEN(NUM$)
  247.         CASE 1: NUM$ = "00" + NUM$
  248.         CASE 2: NUM$ = "0" + NUM$
  249.     END SELECT
  250.     PRINT #F1, "STRING NAME " + NUM$ + " = "; X$(I)
  251.  
  252. IF I <> 0 THEN PRINT #F1, ""
  253.  
  254. I = 0
  255.     I = I + 1
  256.     IF Y$(I) = "" THEN I = I - 1: EXIT DO
  257.     NUM2$ = LTRIM$(STR$(I))
  258.     SELECT CASE LEN(NUM2$)
  259.         CASE 1: NUM2$ = "00" + NUM2$
  260.         CASE 2: NUM2$ = "0" + NUM2$
  261.     END SELECT
  262.     PRINT #F1, "VARIABLES MARKED " + NUM2$ + " = "; Y$(I)
  263.  
  264. IF I <> 0 THEN PRINT #F1, ""
  265.  
  266. I = 0
  267.     I = I + 1
  268.     IF Z$(I) = "" THEN EXIT DO
  269.     NUM3$ = LTRIM$(STR$(I))
  270.     SELECT CASE LEN(NUM3$)
  271.         CASE 1: NUM3$ = "00" + NUM3$
  272.         CASE 2: NUM3$ = "0" + NUM3$
  273.     END SELECT
  274.     PRINT #F1, "VARIABLES UNMARKED " + NUM3$ + " = "; Z$(I)
  275. CLOSE #F1
  276.  
  277. A$ = INKEY$
  278. LOCATE 22, 1
  279.  
  280. REM - FOR WIN 95-98
  281. CHDIR ORIGDIR$
  282. REM SHELL "COMMAND /C START NOTEPAD " + ORIGDIR$ + "\STRVAR.DAT"
  283.  
  284. REM - FOR WIN 2000\NT\XP\Win 7 and 8
  285. SHELL "CMD /C START NOTEPAD " + ORIGDIR$ + "\STRVAR.DAT"
  286.  
  287. A$ = INKEY$
  288.  
  289. READSCREEN:
  290. LINE INPUT ; A$: COLOR 7, 0
  291. A1$ = ""
  292. LOCATE XX%, YY%
  293. FOR I = 1 TO 81 - POS(1)
  294.     A1$ = A1$ + CHR$(SCREEN(XX%, YY% + I - 1))
  295. A1$ = RTRIM$(A1$)
  296.  
  297. CKPATH:
  298. IF MSDOSPATH$ = "" THEN CHDIR DRIVEX$ ELSE CHDIR MSDOSPATH$
  299. IF LEN(PATHWAY$) > 8 AND INSTR(PATHWAY$, "\") = 0 THEN
  300.     IF INSTR(PATHWAY$, ".") <> 0 THEN
  301.         IF LEN(PATHWAY$) > 12 THEN CALL LONGPATH(DRIVEX$, PATHWAY$, ORIGDIR$)
  302.     ELSE
  303.         CALL LONGPATH(DRIVEX$, PATHWAY$, ORIGDIR$)
  304.     END IF
  305.  
  306. RESNXT:
  307.  
  308. ERH:
  309. ER1% = ERR
  310.  
  311. CHECKFILE:
  312. IF ERR = 53 THEN PRINT " FILE NOT FOUND. RESETTING PROGRAM...": SLEEP 3: RESUME RUNPRO
  313. GOTO ER1
  314.  
  315. ER1:
  316. PRINT " ERROR"; ERR; "CANNOT CONTINUE. RESETTING PROGRAM...": SLEEP 3: RESUME RUNPRO
  317. RESUME RUNPRO
  318.  
  319. RUNPRO:
  320. CHDIR ORIGDIR$
  321.  
  322. VARIABLES:
  323. DATA %,!,#,&,EOF
  324.  
  325. SUB LONGPATH (DRIVEX$, PATHWAY$, ORIGDIR$)
  326.  
  327.     DIRSEARCH:
  328.     SHELL "DIR /A:D /O:N > " + ORIGDIR$ + "\TEMPDIR.TMP"
  329.  
  330.     GOSUB FXN1
  331.  
  332.     REM - CONVERTS LONG-FILE WINDOWS PATHWAY NAME TO MS-DOS 8.3 FORMAT.
  333.     IF A2$ <> "" THEN PATHWAY$ = A2$
  334.  
  335.     CHDIR ORIGDIR$
  336.     GOTO ENDSRCHDIRPF
  337.  
  338.     FXN1:
  339.     F1 = FREEFILE
  340.     OPEN ORIGDIR$ + "\TEMPDIR.TMP" FOR INPUT AS #F1
  341.     DO
  342.         IF EOF(F1) THEN EXIT DO
  343.         LINE INPUT #F1, A$
  344.         A1$ = RTRIM$(MID$(UCASE$(A$), 45))
  345.         IF A1$ = PATHWAY$ THEN A2$ = RTRIM$(MID$(A$, 1, 12)): EXIT DO
  346.     LOOP UNTIL EOF(F1)
  347.     CLOSE #F1
  348.     KILL ORIGDIR$ + "\TEMPDIR.TMP"
  349.     RETURN
  350.  
  351.     ENDSRCHDIRPF:
  352.  
  353.  

And a slightly updated and smaller utility...

Code: QB64: [Select]
  1. WIDTH 80, 43
  2. ' Variables
  3. DIM variable$(10000)
  4. DIM FileEntry$(10000)
  5. '
  6. vp1 = 4
  7. vp2 = 40
  8. fcoloxr = 7
  9. bcoloxr = 0
  10. hfcoloxr = 0
  11. hbcoloxr = 7
  12. menutop1 = 1
  13. menuleft1 = 1
  14. menuwidtxh1 = 80
  15. menuheight1 = 2
  16. menutop2 = 4
  17. menuleft2 = 2
  18. menuwidtxh2 = 80
  19. menuheight2 = vp2 - vp1 + 1
  20. rprompt = 1
  21. promptwidtxh = 80
  22. marginleft = 2
  23.  
  24. SHELL _HIDE "dir /b *.bas > tmp.tmp"
  25. SHELL _DONTWAIT "notepad tmp.tmp"
  26. OPEN "tmp.tmp" FOR INPUT AS #1
  27. OPEN "tmp.txt" FOR OUTPUT AS #2
  28.     LINE INPUT #1, a$
  29.     i = i + 1
  30.     FileEntry$(i) = a$
  31. indexmenumax = i
  32. indexmenu = 1
  33. prompt$ = "Highlight and Select a File to Get a List of Variables"
  34. LOCATE 2, 40 - LEN(prompt$) \ 2: PRINT prompt$;
  35. VIEW PRINT vp1 TO vp2
  36. ' Make Selection Menu
  37. GOSUB menu
  38.  
  39. OPEN FileEntry$(indexmenu) FOR INPUT AS #1
  40. prompt$ = "Variables for File: " + FileEntry$(indexmenu)
  41. LOCATE 2, 40 - LEN(prompt$) \ 2: PRINT prompt$;
  42. VIEW PRINT vp1 TO vp2
  43.  
  44.     LINE INPUT #1, a$
  45.     REM GOSUB temp
  46.     GOSUB rewrite
  47.     IF INSTR(a$, "=") THEN
  48.         GOSUB analysis
  49.     END IF
  50. CLOSE #1, #2
  51. SHELL _DONTWAIT "NOTEPAD tmp.txt"
  52.  
  53. prompt$ = "Run Again? Y/N"
  54. LOCATE 3, 40 - LEN(prompt$) \ 2: PRINT prompt$;
  55.     GOSUB keyroutine
  56.     SELECT CASE UCASE$(keyboard$)
  57.         CASE "Y"
  58.             RUN
  59.         CASE "N"
  60.             EXIT WHILE
  61.         CASE CHR$(27)
  62.             SYSTEM
  63.     END SELECT
  64.  
  65. analysis:
  66. x$ = ""
  67. a$ = " " + a$
  68. flag = -1
  69. FOR i = 1 TO LEN(a$)
  70.     IF MID$(UCASE$(a$), i, 5) = " CHR$" THEN flag = 0
  71.     IF MID$(UCASE$(a$), i, 8) = " CSRLIN " THEN flag = 0
  72.     IF MID$(UCASE$(a$), i, 6) = " DATA " THEN flag = 0
  73.     IF MID$(UCASE$(a$), i, 6) = " DATE$" THEN flag = 0
  74.     IF MID$(UCASE$(a$), i, 5) = " POS(" THEN flag = 0
  75.     IF MID$(UCASE$(a$), i, 5) = " LEN(" THEN flag = 0
  76.     IF MID$(UCASE$(a$), i, 4) = " IS " THEN flag = 0
  77.     IF MID$(UCASE$(a$), i, 9) = " ENVIRON " THEN flag = 0
  78.     IF MID$(UCASE$(a$), i, 9) = " ENVIRON$(" THEN flag = 0
  79.     IF MID$(UCASE$(a$), i, 5) = " EOF(" THEN flag = 0
  80.     IF MID$(UCASE$(a$), i, 6) = " HEX$(" THEN flag = 0
  81.     IF MID$(UCASE$(a$), i, 7) = " INKEY$" THEN flag = 0
  82.     IF MID$(UCASE$(a$), i, 7) = " INSTR(" THEN flag = 0
  83.     IF MID$(UCASE$(a$), i, 5) = " NOT " THEN flag = 0
  84.     IF MID$(UCASE$(a$), i, 6) = " OCT$(" THEN flag = 0
  85.     IF MID$(UCASE$(a$), i, 5) = " REM " THEN flag = 0
  86.     IF MID$(UCASE$(a$), i, 6) = " TIME$" THEN flag = 0
  87.  
  88.     SELECT CASE flag
  89.         CASE 0
  90.             IF MID$(a$, i, 1) = ":" THEN flag = -1
  91.         CASE 1
  92.             IF MID$(a$, i, 1) = CHR$(34) THEN flag = -1
  93.         CASE -1
  94.             SELECT CASE MID$(a$, i, 1)
  95.                 CASE " "
  96.                     x1$ = LTRIM$(RTRIM$(x$))
  97.                     x$ = ""
  98.                     variable$ = x1$
  99.                 CASE CHR$(34)
  100.                     flag = 1
  101.                 CASE "'"
  102.                     flag = 0
  103.                 CASE "="
  104.                     IF INSTR(variable$, "(") THEN
  105.                         variable$ = MID$(variable$, 1, INSTR(variable$, "(") - 1) + "()"
  106.                     END IF
  107.                     GOSUB unique
  108.                     IF unique THEN
  109.                         LOCATE CSRLIN, marginleft
  110.                         PRINT variable$
  111.                         PRINT #2, variable$
  112.                         cnt = cnt + 1
  113.                         variable$(cnt) = variable$
  114.                         unique = 0
  115.                     END IF
  116.             END SELECT
  117.     END SELECT
  118.     IF flag = -1 THEN x$ = x$ + MID$(a$, i, 1)
  119.  
  120. unique:
  121. FOR j = 1 TO cnt
  122.     IF LCASE$(variable$(j)) = LCASE$(variable$) THEN EXIT FOR
  123. IF j > cnt THEN unique = -1
  124.  
  125. temp:
  126. ' Only used to experiment with no spaces in an entry.
  127. x1$ = ""
  128. FOR i = 1 TO LEN(a$)
  129.     IF MID$(a$, i, 1) <> " " THEN
  130.         x1$ = x1$ + MID$(a$, i, 1)
  131.     END IF
  132. a$ = x1$
  133.  
  134. rewrite:
  135. qflag = 0
  136. a1$ = ""
  137. FOR i = 1 TO LEN(a$)
  138.     x$ = MID$(a$, i, 1)
  139.     IF x$ = CHR$(34) THEN qflag = ABS(qflag) - 1
  140.     IF qlag = 0 THEN
  141.         SELECT CASE ASC(UCASE$(x$))
  142.             CASE 32, 33, 35 TO 38, 40, 41, 48 TO 57, 65 TO 90
  143.                 a1$ = a1$ + x$
  144.             CASE ELSE
  145.                 IF MID$(a$, i - 1, 1) <> " " THEN a1$ = a1$ + " "
  146.                 a1$ = a1$ + x$
  147.                 IF MID$(a$, i + 1, 1) <> " " THEN a1$ = a1$ + " "
  148.         END SELECT
  149.     END IF
  150. a$ = a1$
  151.  
  152. menu:
  153. FOR i = 1 TO menuheight2
  154.     LOCATE menutop2 + i - 1, menuleft2
  155.     PRINT FileEntry$(i);
  156. LOCATE menutop2, menuleft2
  157. COLOR hfcoloxr, hbcoloxr
  158. PRINT FileEntry$(indexmenu);
  159. COLOR fcoloxr, bcoloxr
  160.     GOSUB keyroutine
  161.     GOSUB MainSelect
  162.     IF keyboard$ = CHR$(13) THEN EXIT WHILE
  163.  
  164.  
  165. keyroutine:
  166. keyboard$ = INKEY$
  167.  
  168.  
  169. MainSelect:
  170. IF keyboard$ <> "" THEN
  171.     IF keyboard$ = CHR$(27) THEN SYSTEM
  172.     SELECT CASE keyboard$
  173.         CASE CHR$(0) + "H"
  174.             GOSUB highlightmenu
  175.         CASE CHR$(0) + "P"
  176.             GOSUB highlightmenu
  177.         CASE CHR$(13)
  178.  
  179.     END SELECT
  180.  
  181.  
  182. highlightmenu:
  183. SELECT CASE keyboard$
  184.     CASE CHR$(0) + "H"
  185.         IF indexmenu > 1 THEN
  186.             IF CSRLIN = vp1 THEN
  187.                 CLS 2
  188.                 FOR i = 1 TO menuheight2
  189.                     LOCATE menutop2 + i - 1, menuleft2
  190.                     IF indexmenu > menuheight2 THEN
  191.                         PRINT FileEntry$(indexmenu - menuheight2 + i - 1);
  192.                     END IF
  193.                 NEXT
  194.                 indexmenu = indexmenu - 1
  195.                 IF indexmenu < 1 THEN indexmenu = 1
  196.                 LOCATE menutop2 + menuheight2 - 1, menuleft2
  197.                 COLOR hfcoloxr, hbcoloxr
  198.                 PRINT FileEntry$(indexmenu);
  199.                 COLOR fcoloxr, bcoloxr
  200.             ELSE
  201.                 COLOR fcoloxr, bcoloxr
  202.                 LOCATE CSRLIN, menuleft2
  203.                 PRINT FileEntry$(indexmenu);
  204.                 indexmenu = indexmenu - 1
  205.                 LOCATE CSRLIN - 1, menuleft2
  206.                 COLOR hfcoloxr, hbcoloxr
  207.                 PRINT FileEntry$(indexmenu);
  208.                 COLOR fcoloxr, bcoloxr
  209.             END IF
  210.         END IF
  211.     CASE CHR$(0) + "P"
  212.         IF CSRLIN = vp2 THEN
  213.             IF indexmenu < indexmenumax THEN
  214.                 CLS 2
  215.                 FOR i = 1 TO menuheight2
  216.                     LOCATE menutop2 + i - 1, menuleft2
  217.                     PRINT FileEntry$(indexmenu + i);
  218.                 NEXT
  219.                 indexmenu = indexmenu + 1
  220.                 LOCATE menutop2, menuleft2
  221.                 COLOR hfcoloxr, hbcoloxr
  222.                 PRINT FileEntry$(indexmenu);
  223.                 COLOR fcoloxr, bcoloxr
  224.             END IF
  225.         ELSE
  226.             IF indexmenu < indexmenumax THEN
  227.                 COLOR fcoloxr, bcoloxr
  228.                 LOCATE CSRLIN, menuleft2
  229.                 PRINT FileEntry$(indexmenu);
  230.                 indexmenu = indexmenu + 1
  231.                 LOCATE CSRLIN + 1, menuleft2
  232.                 COLOR hfcoloxr, hbcoloxr
  233.                 PRINT FileEntry$(indexmenu);
  234.                 COLOR fcoloxr, bcoloxr
  235.             END IF
  236.         END IF
  237.  

Note, these were made prior to qb64, qb64 keyword differentiation is not present in these code examples.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Get Variable Name as string
« Reply #10 on: May 06, 2020, 10:49:44 am »
@Pete, do you remember why you wanted a list of variable names?

I confess not understanding SpriggsySpriggs  (< opps mispelled sorry!) answer to Fellippe.

The only 2 apps I can think of for needing a list of variable names is color highlighting for bas editor or for an interpreter.

Does anyone else know of application?

It does sound like a Rosetta Code Challenge kind of thing.
« Last Edit: May 07, 2020, 11:10:58 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Get Variable Name as string
« Reply #11 on: May 06, 2020, 03:25:44 pm »
Perhaps an AI might want to create a variable to it's own program.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Get Variable Name as string
« Reply #12 on: May 06, 2020, 03:49:46 pm »
Perhaps an AI might want to create a variable to it's own program.

Ah yes! An AI that modifies bas files, say to automatically improve code, we may be at edge of Code Fiction territory :)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Get Variable Name as string
« Reply #13 on: May 06, 2020, 06:47:26 pm »
Ah yes! An AI that modifies bas files, say to automatically improve code, we may be at edge of Code Fiction territory :)

Before I added our precompiler into QB64, I used external AI to improve code and add functionality.   (I still do, for some things...)

For example, I can write a program like this:


Code: [Select]
'#WB:ME
'#LICENSE:WTFPL
'#CD:DATE$
'#UD:DATE$

None of that means anything to QB64 itself -- it just reads it as comments -- but instead of using QB64.EXE to compile that program, I'll use QB64-Precomp.EXE.

QB64-Precomp will then read that and translate those lines as needed into a different BAS file. 

'WB:will generate a series of lines similar to:
Code: [Select]
Center, 10, "Written By"
Center, 11, "Steven McNeill"
Center, 12, "230 McNeil Hill RD, Pilot, VA"
Center, 13, "(smcneill@swva.net)"
Pause, 15
CLS

'LICENSE: will insert the appropriate license into my code, to display to the end user.

'CD: is the concept/creation date.  It replaces DATE$ with the current date when compiled the first time.

'UD: is the Updated Date, which remains and appends a series of current date/time when compiled.

By parsing those 4 little unique variable names from the code, it saves me a lot of repeative writing in my programs and improves readability/scrollability for the rest of the program which does whatever I'm trying to do with my code.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Get Variable Name as string
« Reply #14 on: May 07, 2020, 12:56:55 am »
@Pete, do you remember why you wanted a list of variable names?

I confess not understanding SpriggsySpiggs answer to Fellippe.

The only 2 apps I can think of for needing a list of variable names is color highlighting for bas editor or for an interpreter.

Does anyone else know of application?

It does sound like a Rosetta Code Challenge kind of thing.

I may have put the first program together as a response to someone asking for such a program at QBF, many years ago. The second one, I believe I used once or twice, myself. Why? Probably to get a better handle on the programs, to rename certain variables without duplicating a name already in use,  etc. While many programmers completely plan out their projects before they code it, I simply code as I go. That holds true for the small stuff as well as the largest apps I've made over the years.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/