We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

TurtleScript

From Esolang
Jump to navigation Jump to search

TurtleScript is designed by PSTF. It combines classic turtle graphics with a rich set of general‑purpose programming features.

Core Language Features

Data Types

  • Number – integers and floating‑point (e.g. 42, 3.14)
  • String – double‑quoted (e.g. "Hello")
  • Boolean – true and false
  • List – ordered collection of values, written with square brackets (e.g. [1 2 "abc"])
  • Word – a symbol (e.g. :name) – used mainly for variable names, but also as a literal when quoted (e.g. 'hello)

Variables

Variables are dynamically typed.

  • Assignment: make `name value or the shorter name = value (both styles are supported).
  • Example: make `x 10 or x = 10
  • Access: simply use the variable name. Proceeding colon is allowed.

Control Structures

Conditional

if condition [ ... ]  
ifelse condition [ ... ] [ ... ]

Loops

  • repeat n [ ... ] – repeat n times.
  • while condition [ ... ]
  • for [var start end step] [ ... ] – step is optional (default 1).

Procedures

to procedurename [arg1 arg2 ...]
  ... commands ...
end

Procedures can return a value using output expression.

Built‑in Functions (Console‑related)

print expression – writes to the console.
readline – reads a string from the console.
readnumber – reads a number.
halt – halt immediately.
bye – exit the environment.

arithmetic: +, -, *, /, %, ^ (power)

math functions: sqrt, sin, cos, random, round, abs, etc.
string functions: word, sentence, first, butfirst, last, butlast, item, count, member?
list functions: list, fput, lput, butfirst, butlast, first, last, count, pick, reverse

Turtle Graphics Commands

All turtle commands affect the canvas. They take numeric arguments (or expressions that evaluate to numbers).

Command		Description
forward n	Move forward by n pixels.
back n		Move backward by n pixels.
right n		Turn right by n degrees.
left n		Turn left by n degrees.
setpos [x y]	Move directly to absolute coordinates.
setx n		Set x‑coordinate.
sety n		Set y‑coordinate.
setheading n	Set heading in degrees (0 = east).
penup		Lift pen (no drawing).
pendown		Lower pen (drawing).
pencolor c	Set pen colour (e.g. "red" or RGB list).
fill		Fill the enclosed area with current colour.
home		Return to centre, heading east.
showturtle	Make turtle visible.
hideturtle	Hide turtle.
reset		Return to centre, heading east, and clear the canvas.
circle r	Draw a circle with the centre of current position and radius of r.
ellipse r1 r2	Draw an ellipse with the centre of current position,
		horizontal radius of r1 and vertical radius of r2.
clear		Clear the screen while remain the turtle's position and heading.
stamp x		Print x on the canvas.
arc theta r	Draw an arc with the centre of current position, radius of r, and angle of theta.
arc2 theta r	Moves the turtle along the arc with radius of r and angle of theta.
speed		Sets the speed of turtle. If speed was 0, then set to fastest. Valid value range is 0 to 10.

These commands can be used anywhere – inside loops, conditionals, or user‑defined procedures.

Console & Turtle Interaction

The console is the primary interface for typing commands and seeing results.

Turtle commands can be typed directly; their graphical effect appears in the canvas.

The console also accepts multi‑line definitions (procedures) and supports line‑editing with history.

The console is non‑blocking – while turtle animations run, the console remains responsive for new input (in a multi‑threaded implementation).

Output redirection is not supported – all non‑turtle output goes to the console.

Example Session

> print "Welcome to TurtleScript!
Welcome to TurtleScript!
> x = 5
> repeat 4 [ forward 100 right 90 ]   # draws a square
> print "Area of square = x * x
Area of square = 25
> to mycircle :radius
[   repeat 360 [ forward :radius * 0.0174 right 1 ]
[ end
> mycircle 50   # draws a circle on the canvas

Program Execution Modes

  1. Interactive (REPL) – each line is executed immediately.
  2. Script mode – a file with .ts extension can be loaded using load "file.ts"; the interpreter runs the whole file and then returns to REPL.
  3. Batch mode – run a script non‑interactively (useful for testing).

Error Handling

Syntax errors are reported with line numbers and a caret indicator.

Runtime errors (e.g., invalid turtle command, undefined variable) stop execution and print an error message in the console.

A try [ ... ] catch [ ... ] construct is provided for advanced error recovery (optional).


Example

Spiral

to spiral :size :angle :m
  make "n 0
  while [:n < m] [
    forward :n
    right :angle
    make "n :n + 1
    print (sentence "Step :n "Length :n)   ; console output
  ]
end

spiral 5 20 400

Hello, World! by Stamping to Canvas

stamp "Hello, World!"

See Also

Categories