This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GopherAtl's profile picture

Logo interpreter!

Started by GopherAtl, 08 September 2012 - 11:52 PM
GopherAtl #1
Posted 09 September 2012 - 01:52 AM
Update fixed the line drawing routine the other day… but broke it more. Fixed the broken now, released new version. As an added bonus, have some dancing lines (logo api not required. "r" for new random lines, any other key except escape exits. Run on monitor with "monitor side dancinglines")

An interactive interpreter for the Logo turtle graphics language. Automatic monitor support, subroutines implemented with "to" and variables with "let."

Dunno what logo is? Wikipedia can explain.

Adapts to whatever the screen size is; uses 4 lines of the computer console for displaying the command center, but automatically detects an attached monitor and duplicates drawing there without the command center. Big monitors recommended, as the tiny dimensions of the computer's screen won't let you see very much.

Current Logo commands/operators:
SpoilerImplemented commands
forward <dist> / fd <dist> - moves turtle forward <dist> units
left <angle> / lt <angle> - turns left <angle> degrees
right <angle> / rt <angle> - turns right <angle> degrees
penup / pu - picks the pen up, so movement won't draw
pendown / pd - puts the pen down so movement draws
penchar <char> / pc <char> - sets the character drawn by the pen
clean - erases the current drawing
home - returns the turtle to 0,0 angle 0
wait <time> - pauses execution for <time> seconds
repeat <count> [instructions…]
let [name1 value1 name2 value2 … nameN valueN] - for variable assignment

Implemented Operators:
sum <a> <b> - addition
dif </b></a><b><a> <b> - subtraction
mul </b></a><b><a> <b> - multiplication
div </b></a><b><a> <b> - division
sqrt <val> returns square root of <val>

type "quit" to exit the interpreter.


A sample program:


to spiral :startDist :distStep :angleStep :count
  let [d :startDist]
  repeat :count [fd :D/>/> rt :angleStep let [d sum :)/>/> :diststep]]
end
spiral 1 .1 15 300

and here's a screenshot of the output of that program to an 8x6 monitor with setFontScale(.5)
Spoiler


Video of it in action, using the freshly-implemented clear, home, and wait commands to do animation!
Hypnotic Spirals


Planned features:
Spoiler
  • more logo commands - ex. make, freeze, unfreeze, setposx, setposy,
  • user input/output commands
  • infix operators - ex, 1+1 instead of sum 1 1
  • lists (hard-coded for repeat and let currently)
  • if statement and conditional operators
  • saved programs
    • edit command, to launch an editor to create or edit a saved program
    • run command, to load and run a saved program
    • ability to run saved programs directly from shell, ex. "logo myLogoProgram"
Wishlist features
  • Compiler - convert saved logo programs into lua programs
  • Multiple turtles - will require rewriting to event-based model, fairly radical overhaul
  • full lua integration - lua command to execute lua code in logo programs, and moving logo command interpreter to logo api to allow executing logo commands from lua.

it depends on functions in a separate logo api, which it expects to be called simply logo. The api can be used independently of the logo interpreter program to do turtle-like vector drawing.


Latest versions:
v0.3.2 - http://pastebin.com/8jYYn667
logo interpreter v0.2 - http://pastebin.com/x45p4kbX

Installation note: logo interpreter expects logo API to be named "logo" and either be loaded already or in the current directory.


Version history:
Spoilerlogo api
v0.3.2 - http://pastebin.com/8jYYn667
fixed rather serious bug introduced in 0.3.1 that caused roughly half of lines to incorrectly draw as straight horizontal/vertical lines… :D/>/>
v0.3.1 - http://pastebin.com/i4bCF3aM
replaced rough initial line algorithm with improved version based on bresenham's algorithm
v0.3 - http://pastebin.com/9QPphMV3
added home, clean
v0.2 - http://pastebin.com/RsT0tjzz
added support for multiple displays, drawing to regions of screens
v0.1 -
first version. Had forward(), moveTo(), turn(), turnTo(), penUp, penDown, and the line() function.


logo interpreter
v0.1 - http://pastebin.com/z7WT1wnY
first version, interpreter interface, monitor support, basic turtle commands and operators, repeat, let. Req. logo api v0.2 or later.
v0.2 -http://pastebin.com/x45p4kbX
added home, clean, and "to" command to define custom methods. Req. logo API 0.3 or later.
tommyroyall #2
Posted 09 September 2012 - 02:47 AM
This is quite impressive :3.
GopherAtl #3
Posted 11 September 2012 - 07:07 AM
Thanks :D/>/>

Just a minor update, replaced my initial late-night hacked-from-memory line drawing function with a better version implementing Bresenham's algorithm, so there are no longer large errors accumulated in long lines, they start and end where you would expect.
flasbang73 #4
Posted 11 September 2012 - 11:41 AM
Good to see you released this :D/>/>
GopherAtl #5
Posted 11 September 2012 - 07:46 PM
Not able to fix it until later tonight but just noticed there is a major bug in the "improved" line renderer causing many lines to be drawn very wrong. Embarrassing, this. Just ignore API 0.3.1 for now and use 0.3 if you check this out before I fix it.

Also did some tests in SMP and found the performance somewhat lacking; didn't seem to create lag or anything, but it wasn't receiving screen updates nearly as quickly as they were happening. Very disappointing, will do more testing later and see if there's anything I can do to improve it.
tommyroyall #6
Posted 11 September 2012 - 08:01 PM
I have an idea, could you perhaps make a blank tokenizer/parser? Just saying, it'd be a great project for the forums :)/>/>.
justync7 #7
Posted 11 September 2012 - 08:19 PM
I want the code for that spiral cause im not very good at math. :)/>/>
GopherAtl #8
Posted 11 September 2012 - 08:54 PM
The screenshot is output of the sample program above it, actually, but the code's in logo, so it won't help you much with the math of spirals :D/>/>


to spiral :startDist :distStep :angleStep :count
  let [d :startDist]
  repeat :count [fd :)/>/> rt :angleStep let [d sum :P/>/> :diststep]]
end
spiral 1 .1 15 300

in lua, using the logo API directly, this would be something like..

--[[  params:
startDist - the distance to move forward the first time
distStep - the amount to increase the distance by each time
angleStep - the angle, in degrees, to turn after each move
count - how many times to repeat
--]]
function spiral(startDist, distStep, angleStep, count)
  --current distance
  local dist=startDist
  --repeat count times
  for i=1,count do
    --move forward dist, drawing line behind you
    logo.forward(dist)
    --turn angleStep degrees to the right
    logo.turn(angleStep)
    --increase dist by distStep
    dist=dist+distStep
  end
end

Not really much math in there, as you can see. Turtle graphics are very simple like that, heh.
tommyroyall #9
Posted 12 September 2012 - 12:01 AM
Perhaps you could make a lua function of which shell.run()s the logo program and therefore runs logo code?
GopherAtl #10
Posted 12 September 2012 - 10:30 AM
Tommy, I planned to, it's in the planned features section, the ability to run saved programs from the shell directly
tommyroyall #11
Posted 12 September 2012 - 11:21 AM
Epic :3. I could hook it up to the server software I'm writing and have FTP logo, or have it run logo code live :)/>/>.
NonStopGamer #12
Posted 26 March 2013 - 05:00 AM
Make it run on an actual turtle? :P/>
lieudusty #13
Posted 27 March 2013 - 10:58 AM
When I first read the title I thought it would load an image of a logo and make it print out in CC.
amtra5 #14
Posted 27 March 2013 - 06:20 PM
Now make it move "real" turtles :D/>