Other ways to write things
Sometimes a statement wants a shape that a keyword and its modifiers can’t
give it. REWRITE tells me another way a line may be written, and what to
read it as.
REWRITE SHOW {first}; {second} => PRINT {first}, {second}
SHOW "lives"; 3
SHOW 1 + 1; "is two"
lives 3
2 is two
Before the => is the shape: the words and marks that have to be there,
and names in braces standing for whatever is written in their place. After
it is what the line becomes. I swap one for the other before I look at
anything else, on the same line, so an error still points at the line you
wrote.
Reading old code
This is how a script written for an older BASIC can run as it is. A handful of rules, usually kept in a file of their own and imported:
REWRITE IF {test} THEN => IF {test}
REWRITE ELSEIF {test} THEN => ELSE IF {test}
REWRITE SUB {name}({params}) => FUNCTION {name}({params})
REWRITE END SUB => END FUNCTION
REWRITE CALL {name}({values}) => {name}({values})
REWRITE PRINT {first}; {rest} => PRINT {first}, {rest}
SUB report(score)
IF score >= 90 THEN
PRINT "score"; score; "is great"
ELSEIF score >= 50 THEN
PRINT "score"; score; "is fine"
ELSE
PRINT "score"; score; "needs work"
END IF
END SUB
CALL report(95)
CALL report(20)
score 95 is great
score 20 needs work
Every line under those rules is the old way of writing it, and I read it without complaint.
A few things to know:
- The shape is the whole statement. A rule can’t change a piece in the middle of a line.
- The first rule that fits wins, so put the longer shape first.
- What a rule makes is tried against the rules again. That’s why one rule
for
;handles aPRINTwith any number of them.
From BASIC: This is for you! If you’re bringing old code over, import a file of these rules and port it a piece at a time.
From Python, JavaScript and Lua: It’s a very small macro: one line in, one line out, by shape. No syntax trees, nothing to learn.
Crash-tan’s Tip: Reach for
BINDfirst. A rule is for the shapes a keyword can’t have, like an option word after the values or a mark I don’t otherwise know.