Debugging statements are little snippets of text that print to the screen when a certain part of your code is reached. Since SPL doesn't have any support for strings, we have to add a language feature to support this. Our approach will be that anything enclosed in double-quotes is a debugging statement. This will require a new AST Stmt node class called Debug, which you will have to create.
Some specifics on the debug statements
write 5; "hello!" write 6;would be OK, but not
write "the interruptor!" 5;
if false { "toodaloo" }
should not result in any output.
$ echo "new i := 0; while i < 3 { "here" write i; i := i + 1; }" | ./splc > tmp.ll
$ lli tmp.ll
here
0
here
1
here
2
Below are simple instructions for how to get your compiler set up
for debug's ... of course how to actually do exec for debug
statements is up to you!
// Strips leading and trailing quote marks
// from the given C string
char* quotestrip(char* str) {
char* toret = str;
while (toret[0] == '"') ++toret;
for (int i=0; toret[i] != '\0'; ++i) {
if (toret[i] == '"') toret[i] = '\0';
}
return toret;
}
... and add this to your token rules:
["][^"]*["] {yylval.stmt = new Debug(quotestrip(yytext)); return DEBUG;}
%token<stmt< DEBUGand this to your grammar rules for statements:
| DEBUG {$$ = $1;}
/* A debugging statement embedded in the code. */
class Debug :public Stmt {
private:
string msg;
public:
Debug(const string& themsg) {
msg = themsg;
}
};