Turtle Programming v0.0.12: Powerful For Loop, INC, DEC, and on NPM!

in #utopian-io8 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.12 New Features

This Commit has the following changes:

  1. Add Powerful For
  2. Add INC to increment a variable
  3. Add DEC to decrement a variable
  4. StackOverFlow Max Depth set to 1024
  5. Add Aliases: SetColor = SetPC, SetH = Turn
  6. Add Clear that clears all functions and variables.
  7. Export LogoParser and LogoCanvas to NPM: https://www.npmjs.com/package/logoturtle

Screenshots

For in LOGO can be used in a few ways:

  • FOR [START STOP] [CODE]
  • FOR [START STOP STEP] [CODE]
  • FOR [VARIABLE START STOP] [CODE]
  • FOR [VARIABLE START STOP STEP] [CODE]

For example,

ht cs for [i 0 360 45] [seth :i make "n 0 
  repeat 80 [setpc :random*15 
    repeat 8 [fd :n rt 45] inc :n
  ] 
]

image.png

Implement the FOR Loop in Javascript for LOGO

case "for":
    if (word_next != '[') {
        this.pushErr(word, LOGO_ERR_MISSING_LEFT, word_next);
    }               
    find_next_body = getNextBody(s, i, U);
    if (find_next_body.right >= U) {
        this.pushErr(word, LOGO_ERR_MISSING_RIGHT);
        return false;
    }
    let for_code = s.substring(find_next_body.left + 1, find_next_body.right).trim();
    i = find_next_body.right + 1;
    let for_body = getNextBody(s, i, U);
    if (for_body.right >= U) {
        this.pushErr(word, LOGO_ERR_MISSING_RIGHT);
        return false;
    }                   
    let for_code2 = s.substring(for_body.left + 1, for_body.right).trim();
    if ((for_code.length > 0) && (for_code2.length > 0)) {
        // remove surrounding white spaces
        let for_code_arr = for_code.split(' ').map(item => item.trim()).filter(x => x != '');
        // For Syntax: [VarName Start Stop Step]
        let for_index = 0;
        let for_var = "";
        let saved_for_var = null;
        if (isValidVarName(for_code_arr[0])) {
            for_var = for_code_arr[0];
            if (for_var in this.vars) {
                saved_for_var = this.vars[for_var];
            }                           
            this.vars[for_var] = 0;
            for_index ++;
        }
        if (for_code_arr.length - for_index < 2) {
            this.pushErr(word, LOGO_ERR_INVALID_FOR);
            return false;
        }
        if (for_code_arr.length - for_index > 3) {
            this.pushErr(word, LOGO_ERR_INVALID_FOR);
            return false;
        }                       
        let for_start = this.evalVars(for_code_arr[for_index]);
        let for_stop = this.evalVars(for_code_arr[for_index + 1]);
        let for_step = 1;
        if (for_index + 2 < for_code_arr.length) {
            for_step = this.evalVars(for_code_arr[for_index + 2]);
        }
        if ((for_start === '') || (!isNumeric(for_start))) {
            this.pushErr(word, LOGO_ERR_MISSING_NUMBERS, for_start);
            return false;
        }       
        if ((for_stop === '') || (!isNumeric(for_stop))) {
            this.pushErr(word, LOGO_ERR_MISSING_NUMBERS, for_stop);
            return false;
        }   
        if ((for_step === '') || (!isNumeric(for_step))) {
            this.pushErr(word, LOGO_ERR_MISSING_NUMBERS, for_step);
            return false;
        }
        // get for values
        for_start = parseInt(for_start);
        for_stop = parseInt(for_stop);
        for_step = parseInt(for_step);
        // beware of endless loop ^_^
        for (let for_loop = for_start; for_loop <= for_stop; for_loop += for_step) {
            if (for_var != "") {
                this.vars[for_var] = for_loop;
            }
            // if body
            if (!this.run(for_code2, 0, for_code2.length, depth + 1)) {     
                return false;
            }                           
        }
        // restore loop variable
        if ((for_var != "") && saved_for_var !== null) {
            this.vars[for_var] = saved_for_var;
        }
    }
    // parse next token
    i = for_body.right + 1;                 
    break;  

Roadmap of Chrome Extension: Logo Turtle

I believe LogoTurtle is more or less in beta now. Therefore, bug Fixes and any suggestions, please shout @justyy

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:  

你把LOGO玩成了我玩不起的样子……

其实最简单的LOGO 只有8个关键字……

Very nice. I love turtle and your implementation is very nice. Ill have to look at it when I have more time and see if there is anything I can contribute (I built my own turtle in ClojureScript and one in Dart awhile ago).

周末愉快,行长!

周末愉快!

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

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.100
BTC 62210.62
ETH 1769.88
USDT 1.00
SBD 0.38