LogoTurtle is currently the FIRST and only one Chrome Extension for Turtle Graphics. I have also written a PHP version of Logo Interpreter in 2006 but that runs only on the server.
Along with bug fixes and code tweaks, This Commit has added the support of function declaration and function invoke with parameters
This is the most useful, important and most difficult feature of the LOGO parser!
The following LOGO source code has a two recursive functional call. I have spent quite some time and came across the same problem that I had years ago when writing the PHP Logo Parser. The fix is to copy the variables before recursive calls and restore them once the function calls return so the variables value update do not affect globally.
ht cs to tree :size
if (:size<10) [stop]
fd :size
lt 30 tree :size*0.7 ; left branch, recursive
rt 60 tree :size*0.7 ; right branch, recursive
lt 30
bk :size
end
pu fd -120 pd tree 100
Version 0.0.7 supports single-line comments that are denoted using ; or # or //
The following Javascript parses the TO/END LOGO function pairs and stores it in class attribute this.funs dictionary. The content of the dictionary value is an array of [function parameters, function body].
case "to": // define a function
let next_word;
let funs_name = y.word.trim();
if (!isValidVarName(funs_name)) {
this.pushErr(word, LOGO_ERR_INVALID_FUN_NAME, funs_name);
return false;
}
i = y.next;
let start_fun_idx = i;
let end_fun_idx = -1;
for (;;) {
let prev = i;
next_word = getNextWord(s, i, U);
i = next_word.next;
if (next_word.word.toLowerCase() == 'end') {
end_fun_idx = prev;
break;
}
if ((next_word.word == '' || next_word.next >= U)) {
this.pushErr(word, LOGO_ERR_MISSING_END, next_word);
return false;
}
}
if (end_fun_idx == -1) {
this.pushErr(word, LOGO_ERR_MISSING_END, '');
return false;
}
let funs_s = s.substring(start_fun_idx, end_fun_idx).trim();
if (funs_s && y) {
let ii = 0;
let UU = s.length;
let funs_params = []; // funs parameter
let j = ii;
while (ii < U) {
j = ii;
let to_word = getNextWord(funs_s, ii, UU);
ii = to_word.next;
if (to_word.word.startsWith(':')) {
let to_word_param = to_word.word.substring(1);
if (!isValidVarName(to_word_param)) {
this.pushErr(word, LOGO_ERR_INVALID_VAR_NAME, to_word_param);
return false;
}
funs_params.push(to_word_param);
} else {
break;
}
}
let funs_body = funs_s.substring(j, UU).trim();
// declare the function
this.funs[funs_name] = [funs_params, funs_body];
}
break;
If an App can be written in Javascript, eventually it will be written in Javascript.
Install the Turtle Programming for Kids Now!
Github: https://github.com/DoctorLai/LogoTurtle
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-feature