Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added

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.11 New Features

This Commit has the following changes:

  1. Add While Loop
  2. Add Do/Else Loop
  3. SetXY - Y Coordinate reversed to match math coordinate.
  4. Understore is allowed in variable names.
  5. Add global variables: turtlex, turtley and turtleangle
  6. Add Unit Tests npm test

Screenshots

In LOGO programming language, there isn't a While Loop. The boolean expression is evaluated per loop iteration. For example:

make "x 0
make "sum 0
while :x<=100 [
  make "sum :sum+:x
  make "x :x+1
]
text [sum is :sum]

The Do/Else is a syntax sugar as if (condition) { while (condition) { /* loop */} } else { / * else */}

cs ht make "x 10
do :x<4 [
  fd 100 rt 90 
  make "x :x+1
] else [
  repeat 5 [fd 100 rt 144]
]

The above draws a square if x=0 and a star if x is set to above 4.

to spiral :size
  if (:size>30) [stop]  ; an exit condition
  fd :size rt 15        ; many lines of action
  spiral :size*1.02    ; the tailend recursive call
end

can be re-written in non-recursive DO loop (the else is optional).

to spiral :size
  do :size<=30 [
     fd :size rt 15  
     make "size :size*1.02
  ] else [
     console [:size is larger than 30]
  ]
end

image.png

Implement the LOGO/DO loop in Javascript

case "do":
    find_left = getNextWord(s, y.next, U);
    if (find_left.word != '[') {
        this.pushErr(word, LOGO_ERR_MISSING_LEFT, find_left.word);
        return false;
    }
    repeat_left = find_left.next;
    find_right = repeat_left + 1;
    nested = 1;
    // need to match [ and ]
    while (find_right < U) {
        if (s[find_right] == '[') {
            nested ++;
        }                                               
            if (s[find_right] == ']') {
                nested --;
                if (nested == 0) {
                    break;
                }
            }
            find_right ++;
    }
    if (find_right >= U) {
        this.pushWarning(word, LOGO_ERR_MISSING_RIGHT);                     
    }
    let do_exp = word_next;                                                 
    word_next = this.evalVars(do_exp);          
    ifelse = iftrue(word_next);
    if (word_next === '') {
        this.pushErr(word, LOGO_ERR_MISSING_EXP, word_next);
        return false;
    }                                               
    while (iftrue(word_next)) {
        // do body
        if (!this.run(s, repeat_left, find_right, depth + 1)) {     
            return false;
        }
        word_next = this.evalVars(do_exp);
    }   
    find_else = getNextWord(s, find_right + 1, U);
    if (find_else.word.toLowerCase() == 'else') {
        let else_block = getNextBody(s, find_else.next, U);
        if (else_block.ch != '[') {
            this.pushErr(word, LOGO_ERR_MISSING_LEFT, else_block.ch);
            return false;
        }
        if (!ifelse) {
            // else body
            if (!this.run(s, else_block.left, else_block.right, depth + 1)) {
                return false;
            }
        }
        i = else_block.right + 1;
    } else {
        // no else block
        i = find_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:  

Great information here love your post

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

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.32
JST 0.088
BTC 59996.92
ETH 1580.26
USDT 1.00
SBD 0.42