Author Topic: $REPLACE  (Read 6107 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
$REPLACE
« on: September 16, 2018, 06:16:17 am »
Grab the below file, toss it into your QB64/source folder, and compile.  (Untick the OUTPUT EXE TO SOURCE FOLDER option first or else it'll fuzzle out -- QB64 expects files/folders to be in certain places.)

Done correctly, you'll have a QB64-REPLACE.exe which will automatically run itself and give you a new, unedited, non-error-trapped, command OF DOOM!!!

Usage is rather simple:

$REPLACE whatever = whateverelse

Then, anytime we type whatever into our program, it automatically will change whatever to whateverelse for us.

For example, let's say we type the following code into the IDE:

Code: QB64: [Select]
  1. $REPLACE ?? = _PRINTSTRING(
  2. ??1,1),"Hello World"

The moment we hit enter after the second line, what we'll see is:

Code: QB64: [Select]
  1. $REPLACE ?? = _PRINTSTRING(
  2. _PRINTSTRING(1,1),"Hello World"

That ?? got replaced with _PRINTSTRING( -- just as we told it to.

Unfortunately, it also means that if we type: PRINT "Whut??", it's going to change that to PRINT "Whut_PRINTSTRING("....

This is a very simple little replace routine, and it'll swap out code ANYTIME it makes a match, so don't be silly and do something like $REPLACE X = Xpoint....   If you do, you'll probably destroy the universe with that X being replaced over and over and over and over in Xpoint...

This wouldn't be a command for the squeamish, I'd assure you that -- and it has none of the usual error-checking, or "idiot proofing", which we normally see with the IDE.  Folks who would be interested in using something like this would need to set themselves some "quick commands" to use with it:

$REPLACE SDP2P = SL_SPRITE_DISTANCE_POINT_TO_POINT

Then, instead of having to type the whole long command, one could type SDP2P and have the IDE automatically replace the shortcut with the actual command for them, as long as they weren't wanting to use SDP2P for something else inside their program...
* qb64-REPLACE.bas (Filesize: 1.03 MB, Downloads: 356)
« Last Edit: September 16, 2018, 11:02:59 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #1 on: September 16, 2018, 06:31:45 am »
Side note, for those curious: 

You can reset/change the value for your code in a top to bottom method (similar to the _DEFINE commands).

Say we have a patch of code like the following:


$REPLACE x = y


$REPLACE x = z


Now, if we type in x = 2 between the first and second replace statement, we'd get y = 2 for the output.  If we type x = 1 before the replace statements, we'd have x = 1.  If we type x = 3 at the end of the program, we'd have z = 3, much as the below:

x = 1
$REPLACE x = y
y = 2
$REPLACE x = z
z = 3

Now note, we never typed y = 2 or z = 3... We typed "x = 2" and "x = 3", and the replace routine did its magic for us. 

We only replace typing AFTER the command has been issued for the IDE; it's not retroactive on us.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: $REPLACE
« Reply #2 on: September 16, 2018, 07:20:07 am »
Hi Steve,

I do not know if it should so be, but replacement remains, even if $REPLACE is deleted from IDE.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
Re: $REPLACE
« Reply #3 on: September 16, 2018, 07:23:47 am »
my qb64-replace.exe is in qb64 root, not in \source subdir, for info

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #4 on: September 16, 2018, 07:34:58 am »
Hi Steve,

I do not know if it should so be, but replacement remains, even if $REPLACE is deleted from IDE.

I overlooked resetting the lists and counter, in case someone deleted a $REPLACE.  I'll fix that later this afternoon -- me and the wife are currently heading out for breakfast before going to church.   (If anyone wants to fix it for themselves, open the file, search for ide3: and set ReplaceCount = 0, and REDIM REplaceList(100) As string: REDIM ReplaceWith(100) AS STRING, there in that list of variables which reset each pass the IDE starts over.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #5 on: September 16, 2018, 07:36:04 am »
my qb64-replace.exe is in qb64 root, not in \source subdir, for info

It'd have to be in root, or it wouldn't run at all.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: $REPLACE
« Reply #6 on: September 16, 2018, 08:44:06 am »
This is better than using Search > Change ? (With which you can verify what is being changed.)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #7 on: September 16, 2018, 09:25:56 am »
This is better than using Search > Change ? (With which you can verify what is being changed.)

In my opinion, it would be.  You just set a shortcut of sorts with $REPLACE, then can forever use that shortcut as you code, instead of needing to Search/Change or type the whole name.

Think of writing code like this:

Code: QB64: [Select]
  1. $REPLACE q1 = MyCustomType.
  2. GET #1, ,q1x
  3. GET #1, ,q1y
  4. q1x = q1x + 1
  5. q1y = q1y + 1
  6. PUT #1, , q1x
  7. PUT #1, , q1y

And PRESTO!!  Each line as you type it has MyCustomType. Replace the q1 automatically.

Code: QB64: [Select]
  1. GET #1, ,MyCustomType.x
  2. GET #1, ,MyCustomType.y
  3. MyCustomType.x =MyCustomType.x + 1
  4. MyCustomType.y =MyCustomType.y + 1
  5. PUT #1, , MyCustomType.x
  6. PUT #1, , MyCustomType.y

Set q1 to designate "quick code 1", and all you'd need to do is type it as a quick shortcut to avoid typing the whole "MyCustomType." label.

Which, I'd find much easier to write and use automatically, than trying to track and do via Search/Change in the edit menu.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: $REPLACE
« Reply #8 on: September 16, 2018, 09:33:34 am »
Oh you are using it in reverse of what I imagined, develop in easy type then replace with the long stuff then reverse again for changes. Nice!

That would help keep code self documenting with descriptive variable names when stored.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #9 on: September 16, 2018, 09:46:16 am »
Oh you are using it in reverse of what I imagined, develop in easy type then replace with the long stuff then reverse again for changes. Nice!

That would help keep code self documenting with descriptive variable names when stored.

Where I'd use it would be for libraries like Terry's Sprite Library, as he described here: https://www.qb64.org/forum/index.php?topic=537.msg4371#msg4371

That's a lot of typing to do, (though it is perfectly descriptive naming,) and I'm a generally lazy coder.  What I'd do is set a $REPLACE library up top in the code, and then use my designated shortcuts to avoid typing all those words/letters out.

$REPLACE GDP2P = SL_GET_DISTANCE_POINT_TO_POINT <--- at top of my code....

Then I'd just type GDP2P into the IDE when coding and it'd automatically replace that shortcut with the proper command for me, saving me a lot of typing over the course of a whole program.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #10 on: September 16, 2018, 11:06:45 am »
Updated attachment under the first post, with changes as I mentioned under Reply #4.  Replace lists now properly clear themselves if we remove $REPLACE from our code, whereas they failed to do so before.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: $REPLACE
« Reply #11 on: September 16, 2018, 11:40:40 am »
My 2 cents.

$REPLACE being preceded by $ like other compiler metacommands looks like it will be a compiler directive too, but it doesn't seem like it's the case by the descriptions above. It sounds more like an IDE specific feature.

I'm comparing this to Microsoft Word's AutoText feature, which does on-the-fly replacements as you type. You don't add a directive to the top of your document that will trigger it. Instead, you add it to a table that is maintained in the program, so the lookup can be done.

My opinion is that having it set in code would require the compiler to also do the replacements via command-line compilation. This should be the case especially since many of our users are QB64 fans but not necessarily fans of the IDE we ship.

On the other hand, the first idea that was discussed was an ALIAS command that would have the compiler *treat* (as opposed to replace) occurrences of a user-defined abbreviation as another command. That sounds way more useful and would be something to save on typing. It could even be used to create aliases to long native commands like _DISPLAYORDER or _PRINTSTRING (something many of us already do with a custom SUB/FUNCTION with a shorter name).
« Last Edit: September 16, 2018, 11:42:14 am by FellippeHeitor »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: $REPLACE
« Reply #12 on: September 16, 2018, 12:01:23 pm »
Ah yes! with custom subs and functions you can customize your use of the procedure along with the name.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: $REPLACE
« Reply #13 on: September 16, 2018, 01:12:34 pm »
Quote
On the other hand, the first idea that was discussed was an ALIAS command that would have the compiler *treat* (as opposed to replace) occurrences of a user-defined abbreviation as another command. That sounds way more useful and would be something to save on typing. It could even be used to create aliases to long native commands like _DISPLAYORDER or _PRINTSTRING (something many of us already do with a custom SUB/FUNCTION with a shorter name).

_ALIAS actually works the exact say way this does, except it doesn't update layout$ afterwards.  After thinking on it and working with it, this method seems like a vast improvement to me, instead....

For example, what's wrong with this code snippet?

Code: QB64: [Select]
  1. C (2).H = F

By itself, all anyone can do is shrug...  I'd have to include a list of aliases for it to be relevant and make sense.

ALIAS Control AS C
ALIAS Hidden AS H
ALIAS False AS F

Why isn't my control hiding in Inform?  Well, now you can see!  ;)

By using $REPLACE, the user can still type it as C (2).H = F  -- not that anyone would actually be that extreme shortcutting, I hope -- but it'd automatically fix it to the proper version for them:  Control (2).Hidden = False

***************

_ALIAS allows us to basically use one command for another, but it requires we share that list of aliases when discussing the code, as well.  $REPLACE, on the other hand, would let us shortcut typing smaller words, but having them automagically change to the proper ones when we're finished.

****************

As for it starting with a $ sign, similar to a metacommand, it's because I kinda think of it as one.  As defined here:  http://www.yourdictionary.com/metacommand

Quote
(computing) A command that is not part of the language being written but serves as a directive to the interpreter or compiler.

It's not a command which affects our program -- not actually part of the language -- but it's a command which affects the IDE itself, as we write our program.

However, thinking of it like Word's AutoText definitely isn't wrong.  The only difference is you don't need to create a table and save it, defining the same keystrokes to permanent replacement values, as you can build the table as needed for each project.

MS1 might be a quick replacement typo if I'm using Terry's Sprite Library and want to move a sprite, but I'm not going to use his library for all my projects.  Elsewhere, I might code it to represent MouseState1.  $REPLACE lets me set the value *by program*, and not *by IDE lookup list* as Word does it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
Re: $REPLACE
« Reply #14 on: September 16, 2018, 02:35:48 pm »
So I tried it with

$REPLACE Steve = Clippy
Get #1, Steve

I ran it and my system crashed. I opened the system log files and the only error message I got for the event was: WTF

Please advise...

Anyway, Interesting. Apparently you reached that point where things like typing variables with long well described names had to be dealt with in a better way. I used to just do things like a1$ and do a universal word change in notepad or the IDE when the program was finished. I kept an index legend of the variable names like a1$ = helloworldvariabletoprinthelloworldtothescreenforthisexcitingprogramexampleiputtogetherinseverallongpainstakingweeksofwork$

So this effect is pretty cool, as it do that on the fly... well, unless yo code in Notepad, like I often do. That's where the metacommand confusion could be a problem. Someone will use it a non-QB64 IDE and expect when compiled that $REPLACE will convert it for them, and it won't. Other than that happening, it's a really cool feature because having it on the fly means we get to enjoy reading our code with the more descriptive variables, etc., without thy laborious extra typing or having to go through a bunch of universal replacements when the project is at a stopping point.

Pete



« Last Edit: September 16, 2018, 05:11:41 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/