Skip to content

Commit

Permalink
add /* .. */ comments, dotxy, js
Browse files Browse the repository at this point in the history
  • Loading branch information
SteakOverCooked authored and SteakOverCooked committed Mar 3, 2018
1 parent c16f895 commit bfd6156
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -2,6 +2,7 @@
I believe this is the First Open Source Logo Programming in Chrome Extension.

![](https://github.com/DoctorLai/LogoTurtle/blob/master/images/if-else.jpg?raw=true)
![](https://helloacm.com/wp-content/uploads/2018/03/logo-tree.jpg)

# Install
Just install via [Chrome Webstore](https://chrome.google.com/webstore/detail/logo-turtle/dcoeaobaokbccdcnadncifmconllpihp).
Expand All @@ -18,5 +19,11 @@ Github: https://github.com/DoctorLai/LogoTurtle/
5. Submit a pull request.

# Blog Posts
- v0.0.2: [ShowTurtle, HideTurtle, Color, Width and Help.](https://helloacm.com/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!](https://helloacm.com/teach-your-kids-programming-the-first-logo-interpreter-turtle-graphics-in-chrome-extension/)
- v0.0.7: [Turtle Programming v0.0.7: Functions with Parameters + Recursion!](https://helloacm.com/turtle-programming-v0-0-7-functions-with-parameters-recursion/)
- v0.0.6: [Turtle Programming v0.0.6: Adding Circle, MoveTo, Turn and Screen!](https://helloacm.com/turtle-programming-v0-0-6-adding-circle-moveto-turn-and-screen/)
- v0.0.5: [Turtle Programming v0.0.5: Adding IF/ELSE and STOP!](https://helloacm.com/turtle-programming-v0-0-5-adding-if-else-and-stop/)
- v0.0.4: [LogoTurtle: Make Variables and Comments](https://helloacm.com/logoturtle-make-variables-and-comments/)
- v0.0.3: [Turtle Graphics Programming Update: Adding text, jump, dot, fontsize, download as png](https://helloacm.com/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](https://helloacm.com/logoturtle-v0-0-2-showturtle-hideturtle-color-width-and-help/).
- Teach Your Kids Programming - The First Logo Interpreter (Turtle Graphics) in Chrome Extension!
[v0.0.1](https://helloacm.com/teach-your-kids-programming-the-first-logo-interpreter-turtle-graphics-in-chrome-extension/)
13 changes: 6 additions & 7 deletions js/logocanvas.js
Expand Up @@ -151,9 +151,14 @@ class LogoCanvas {

// draw a dot
dot() {
this.ctx.fillRect(this.x + this.cx, this.y + this.cy, this.lineWidth, this.lineWidth);
this.dotxy(this.x, this.y);
}

// draw a dot at (x, y)
dotxy(x, y) {
this.ctx.fillRect(x + this.cx, y + this.cy, this.lineWidth, this.lineWidth);
}

// pen up
pu() {
this.pendown = false;
Expand Down Expand Up @@ -205,12 +210,6 @@ class LogoCanvas {

// moveTo
moveTo(nx, ny) {
let ox = nx + this.cx;
let oy = ny + this.cy;
ox = ox % this.width;
oy = oy % this.height;
nx = ox - this.cx;
ny = oy - this.cy;
if (this.pendown) {
this.drawTo(nx, ny)
}
Expand Down
105 changes: 80 additions & 25 deletions js/logoparser.js
Expand Up @@ -80,44 +80,64 @@ class LogoParser {
}
}

// jump comments and white spaces
skipTo(s, i, U) {
// skip for white spaces and newlines
while ((i < U) && (isSpace(s[i]) || s[i] == '\n')) {
i ++;
}
if (i >= U) { // reach block end
return i;
}
// skip comments till the end
if ((s[i] == ';') || (s[i] == '#')) {
i ++;
while ((i < U) && (s[i] != '\n')) {
i ++;
}
i ++;
}
// skip // comments
if (i + 1 < U) {
if ((s[i] == '/') && (s[i + 1] == '/')) {
i += 2;
while ((i < U) && (s[i] != '\n')) {
i ++;
}
i ++;
}
}
// skip /* */ comments
if (i + 1 < U) {
if ((s[i] == '/') && (s[i + 1] == '*')) {
i += 2;
while ((i < U) && ((s[i] != '*') || (s[i + 1] != '/'))) {
i ++;
}
i += 2;
}
}
return i;
}

// parse a LOGO program source code
// source - s
// left index - i
// right index (not contain) - U
run(s, i, U, depth = 0) {
let find_left, find_right, repeat_left, repeat_right, find_else;
let nested, expr, second_word, second_word_word;
let nested, expr, second_word, second_word_word, find_next_body;
if (depth > MAX_DEPTH) {
// Stack Overflow e.g. recursion without terminating conditions
this.pushErr("", LOGO_ERR_STACK_OVERFLOW, depth);
return false;
}
while (i < U) {
// skip for white spaces and newlines
while ((i < U) && (isSpace(s[i]) || s[i] == '\n')) {
i ++;
}
if (i >= U) { // reach block end
// jump Comments and white spaces
i = this.skipTo(s, i, U);
if (i >= U) {
break;
}
// skip comments till the end
if ((s[i] == ';') || (s[i] == '#')) {
while ((i < U) && (s[i] != '\n')) {
i ++;
}
i ++;
continue;
}
// skip // comments
if (i + 1 < U) {
if ((s[i] == '/') && (s[i + 1] == '/')) {
while ((i < U) && (s[i] != '\n')) {
i ++;
}
i ++;
continue;
}
}
// get next word and next index
let x = getNextWord(s, i, U);
i = x.next;
Expand Down Expand Up @@ -246,6 +266,28 @@ class LogoParser {
this.logo.circle(parseFloat(word_next));
i = y.next;
break;
case "dotxy":
word_next = this.evalVars(word_next);
if ((word_next == '') || (!isNumeric(word_next))) {
this.pushErr(word, LOGO_ERR_MISSING_NUMBERS, word_next);
return false;
}
second_word = getNextWord(s, y.next, U);
second_word_word = second_word.word;
expr = this.evalVars(second_word_word);
try {
second_word_word = eval(expr);
} catch (e) {
this.pushErr(word, LOGO_ERR_EVAL, expr);
return false;
}
if ((second_word_word == '') || (!isNumeric(second_word_word))) {
this.pushErr(word, LOGO_ERR_MISSING_NUMBERS, second_word_word);
return false;
}
this.logo.dotxy(word_next, second_word_word);
i = second_word.next;
break;
case "moveto":
word_next = this.evalVars(word_next);
if ((word_next == '') || (!isNumeric(word_next))) {
Expand Down Expand Up @@ -316,7 +358,7 @@ class LogoParser {
if (word_next != '[') {
this.pushErr(word, LOGO_ERR_MISSING_LEFT);
}
let find_next_body = getNextBody(s, i, U);
find_next_body = getNextBody(s, i, U);
if (find_next_body.right >= U) {
this.pushErr(word, LOGO_ERR_MISSING_RIGHT);
return false;
Expand All @@ -325,6 +367,19 @@ class LogoParser {
this.logo.drawText(text_to_print);
i = find_next_body.right + 1;
break;
case "js":
if (word_next != '[') {
this.pushErr(word, LOGO_ERR_MISSING_LEFT);
}
find_next_body = getNextBody(s, i, U);
if (find_next_body.right >= U) {
this.pushErr(word, LOGO_ERR_MISSING_RIGHT);
return false;
}
let js_code = s.substring(find_next_body.left + 1, find_next_body.right);
eval(js_code);
i = find_next_body.right + 1;
break;
case "if":
word_next = this.evalVars(word_next);
if (word_next === '') {
Expand Down
3 changes: 3 additions & 0 deletions main.html
Expand Up @@ -56,6 +56,7 @@ <h4 id="text_ui_language">UI Language</h4>
<div id="tabs-help">
<h4 id='text_logs'>Logs</h4>
<ul>
<li><a target=_blank href="https://helloacm.com/turtle-programming-v0-0-7-functions-with-parameters-recursion/">v0.0.7: Turtle Programming v0.0.7: Functions with Parameters + Recursion!</a></li>
<li><a target=_blank href="https://helloacm.com/turtle-programming-v0-0-6-adding-circle-moveto-turn-and-screen/">v0.0.6: Turtle Programming: Adding Circle, MoveTo, Turn and Screen!</a></li>
<li><a target=_blank href="https://helloacm.com/turtle-programming-v0-0-5-adding-if-else-and-stop/">v0.0.5: Turtle Programming v0.0.5: Adding IF/ELSE and STOP!</a></li>
<li><a target=_blank href="https://helloacm.com/logoturtle-make-variables-and-comments/">v0.0.4: LogoTurtle: Make Variables and Comments</a></li>
Expand Down Expand Up @@ -90,6 +91,8 @@ <h4 id='supported_commands'>Support Commands</h4>
<li>SCREEN</li>
<li>MOVETO</li>
<li>TO, END</li>
<li>DOTXY</li>
<li>JS</li>
</ul>
</div>
<div id="tabs-log">
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Expand Up @@ -3,7 +3,7 @@
"name": "Logo Turtle",
"short_name": "LogoTurtle",
"default_locale": "en",
"version": "0.0.7",
"version": "0.0.8",
"offline_enabled": true,
"browser_action": {
"default_icon": "icon.png",
Expand Down

0 comments on commit bfd6156

Please sign in to comment.