Think about two problems:
- How to write a macro to decide whether two strings are equal?
- How to write a
if
macro to do the conditional execution? More specifically, write a macroif(c,x,y)
, it returnsx
ifc
is true, andy
ifc
is false (assume thatc
can either be true or false).
First, we define true and false by:
true() {"T"}
false() {"F"}
They are represented by string literals.
To directly implement the eq
macro is quite hard.
We have to first implement the encode
and decode
macro to help us.
Encoding means to encode a string into another string that has some special properties that we can use.
We want cat
to concatenate two strings. It is possible by using rep2
.
cat(x,y) {
rep2("\$","\",x,"$",y)
}
rep2(s,x1,y1,x2,y2) {
var s1 = {
"\$$" = enc(y1);
"\$$$" = enc(y2);
"\$$$$" = enc(x2) + "$";
enc(x2) = "\$$$";
enc(x1) = "\$$";
enc(s)
};
dec(s1)
}
enc(x) {
"$" = "\$";
"\" = "\\";
x
}
dec(x) {
"\\" = "\";
"\$" = "$";
x
}