Algorithms, Blockchain and Cloud

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


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.

turtle-graphics-logo-programming-chrome-extension Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added

turtle-graphics-logo-programming-chrome-extension

Previous Contributions

  • v0.0.10: Turtle Programming: Fractal Stars, Random, Console, Eraser, SetPC, SetXY, Examples, Wait, Bug Fixes and So much more!
  • v0.0.9: Turtle Programming v0.0.9: Add SetX, SetY, Square and Rect!
  • v0.0.8: Turtle Programming v0.0.8: /* */ comments, dotxy, and javascript!
  • v0.0.7: Turtle Programming v0.0.7: Functions with Parameters + Recursion!
  • v0.0.6: Turtle Programming v0.0.6: Adding Circle, MoveTo, Turn and Screen!
  • v0.0.5: Turtle Programming v0.0.5: Adding IF/ELSE and STOP!
  • v0.0.4: LogoTurtle: Make Variables and Comments
  • v0.0.3: Turtle Graphics Programming Update: Adding text, jump, dot, fontsize, download as png
  • v0.0.2: LogoTurtle v0.0.2: ShowTurtle, HideTurtle, Color, Width and Help.
  • v0.0.1: Teach Your Kids Programming – The First Logo Interpreter (Turtle Graphics) in Chrome Extension!

Logo Programming v0.0.11 New Features

This Commit has the following changes:

  • Add While Loop
  • Add Do/Else Loop
  • SetXY – Y Coordinate reversed to match math coordinate.
  • Understore is allowed in variable names.
  • Add global variables: turtlex, turtley and turtleangle
  • Add Unit Tests npm test

Turtle Graphics 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

turtle-graphics-logo-programming-spiral

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

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am ‘Add some feature’
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request.

–EOF (The Ultimate Computing & Technology Blog) —

964 words
Last Post: AWK Tutorial: When are you expected to produce your next witness block? (STEEMIT)
Next Post: Improve Performance using Asynchronous Design (SteemIt)

The Permanent URL is: Turtle Programming: While Loop, Do/Else Loop and Unit Tests Added (AMP Version)

Exit mobile version