Macros
Shortening your code the janky way!
You can use a macro one of two ways:
Using the
define
function to customize your development experience.Using the
define
function to replace code in your program.
To use the first type of macro, see the following example:
define(MACHINE_SYMBOL, "!");
define(FUTURE_SYMBOL, "$");
This is setting the future symbol to $
. The default value of this is ?
. The MACHINE_SYMBOL
is typically an empty string, but here it's being set to !
. You would do something like this:
!setCommand(
AWARENESS=0, PAGE=0,
$move(forward)
$goToPage(1)
);
For a full list of these symbols that you can use, see "Define Customization" under the API reference.
The second type of macro is less customization-focused and more productivity-focused. As an example, the following macro will print "Tape:", a newline, and then what is in the tape when out;
is written in the program. It's important to note that I'm still using the !
for MACHINE_SYMBOL
as defined above.
define("!print("Tape:");!print();", "out;");
The use of quotation marks might look a little strange (and it sort of is) but there's two parts to this:
First, the first argument inside of quotation marks is what the marker should replace the target text with. This should all be one one line (otherwise, the RegEx can parse your commands wrong). The second argument is what you want to substitute that text for, so in this case, out;
. Technically, just out
and removing the last semicolon in the first argument would work better, becuase otherwise out ;
would not be replaced properly.
As a language standardization, you are highly reccomended to use all lowercase macro names with no parenthesis, so as not to confuse it with a function.
Here's a program that computes the bitwise AND operation, using both types of these macros:
define(FUTURE_SYMBOL, "?");
define(MACHINE_SYMBOL, "!");
define("!print("Tape:");!print();", "out;");
/*
Should apply and operation in a bitwise manner, leaving result in second "byte" while first is unchanged
Expected output:
Final result:
# 1 0 1 0 1 1 1 -1 # 0 1 1 0 1 1 1 -1
*/
!print("Starting turing machine...");
!setValues(# 1 0 1 0 1 1 1 -1 # 0 1 1 0 1 1 1 -1);
!setPosition(0);
const forward = 8;
const back = -7;
out;
!setCommand(
AWARENESS=0, PAGE=0,
?move(forward)
?goToPage(1)
);
!setCommand(
AWARENESS=1, PAGE=0,
?move(forward)
?goToPage(2)
);
!setCommand(
AWARENESS=-1, PAGE=0,
?stop()
);
!setCommand(
AWARENESS=0, PAGE=1,
?setTape(0)
?move(back)
?goToPage(0)
);
!setCommand(
AWARENESS=1, PAGE=1,
?setTape(0)
?move(back)
?goToPage(0)
);
!setCommand(
AWARENESS=0, PAGE=2,
?setTape(0)
?move(back)
?goToPage(0)
);
!setCommand(
AWARENESS=1, PAGE=2,
?setTape(1)
?move(back)
?goToPage(0)
);
!run();
!print("Final result:");
out;
Running this, you should be able to see the proper program output!
Last updated