Turtle Programming v0.0.7: Functions with Parameters + Recursion!

in #utopian-io7 years ago (edited)

Introduction to Logo Turtle

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.

Previous Contributions

v0.0.7 New Features

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!

Screenshots

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

image.png

Version 0.0.7 supports single-line comments that are denoted using ; or # or //

How to Parse To/End LOGO function in Javascript?

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;

Roadmap of Chrome Extension: Logo Turtle

  1. Add Functions
  2. Add IF/THEN/ELSE
  3. Add Variables
  4. Add Colors
  5. Add MoveTo
  6. Add PrintText
  7. Add Circle
  8. Add Arc
  9. Add Eraser
  10. Add Fill
  11. Save As Picture
  12. Save As Program
  13. Comments
  14. Add Recursion Support
  15. Add Global/Local Scopes
  16. Sleep
  17. etc. etc.

Technology Stack

If an App can be written in Javascript, eventually it will be written in Javascript.

Chrome Webstore

Install the Turtle Programming for Kids Now!

Contribution Welcome

Github: https://github.com/DoctorLai/LogoTurtle

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@justyy, Upvote is the only thing I can support you.

尽管没有看懂,仍然点赞

nice post, I like your post @justyy

great post about logoturtle @justyy thanks for sharing

Thank you for the contribution. It has been approved.

You can contact us on Discord.

[utopian-moderator]

Hey @justyy I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x