@edadma/logo
v0.3.3
Published
Logo programming language interpreter
Maintainers
Readme
@edadma/logo
A Logo programming language interpreter for JavaScript/TypeScript with React component support.
Installation
npm install @edadma/logoBasic Usage
import { Logo } from '@edadma/logo';
const canvas = document.getElementById('myCanvas');
const logo = new Logo(canvas);
// Run a Logo program
logo.run(`
repeat 4 [
forward 100
right 90
]
`);
// Execute single commands
logo.execute('forward 50');
logo.execute('right 90');
// Clear and reset
logo.clear();React Component
import { LogoCanvas } from '@edadma/logo/react';
import { useRef } from 'react';
function App() {
const logoRef = useRef(null);
const [code, setCode] = useState('repeat 4 [fd 100 rt 90]');
return (
<div>
<textarea value={code} onChange={e => setCode(e.target.value)} />
<button onClick={() => logoRef.current?.run(code)}>Run</button>
<button onClick={() => logoRef.current?.clear()}>Clear</button>
<LogoCanvas
ref={logoRef}
width={600}
height={400}
pathRendering={true}
onError={(e) => console.error(e)}
/>
</div>
);
}LogoCanvas Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| width | number | 600 | Canvas width in pixels |
| height | number | 400 | Canvas height in pixels |
| program | string | - | Logo program to run (auto-runs on change) |
| pathRendering | boolean | true | Use smooth path rendering |
| onError | function | - | Error callback |
| onComplete | function | - | Completion callback |
| className | string | - | CSS class for canvas |
| style | object | - | Inline styles for canvas |
LogoCanvas Ref Methods
interface LogoCanvasRef {
execute(command: string): void;
run(program: string): void;
clear(): void;
getDrawing(): LogoDrawing;
getTurtle(): TurtleState;
getLogo(): Logo | null;
}API Reference
Logo Class
class Logo {
constructor(canvas: HTMLCanvasElement);
run(program: string): void;
execute(command: string): void;
clear(): void;
render(): void;
setPathRendering(enabled: boolean): void;
setAutoRender(enabled: boolean): void;
getDrawing(): LogoDrawing;
getTurtle(): TurtleState;
}Drawing Data
If you want to implement custom rendering:
interface LogoDrawing {
lines: Array<{
x1: number; y1: number;
x2: number; y2: number;
color: string; // e.g., "rgb(0,0,0)"
width: number;
}>;
labels: Array<{
x: number; y: number;
heading: number; // radians
text: string;
}>;
}
interface TurtleState {
x: number;
y: number;
heading: number; // radians, π/2 = north
visible: boolean;
}Supported Logo Commands
Movement
forward <dist>/fd- Move forwardback <dist>/bk- Move backwardright <angle>/rt- Turn right (degrees)left <angle>/lt- Turn left (degrees)setxy <x> <y>- Move to positionhome- Return to centertowards [x y]- Return heading toward pointdistance [x y]- Return distance to pointarc <angle> <radius>- Draw an arc
Pen Control
penup/pu- Lift penpendown/pd- Lower pensetpensize <size>- Set line widthsetcolor <color>- Set pen color (name, number, or [r g b])
Turtle
hideturtle/ht- Hide turtleshowturtle/st- Show turtle
Control Flow
repeat <n> [commands]- Repeat commandsfor [var start end step] [commands]- For loopwhile [condition] [commands]- While loopuntil [condition] [commands]- Until loopforever [commands]- Loop until stop/outputif <cond> [commands]- Conditionalifelse <cond> [yes] [no]- If-else
Procedures
to <name> <:params> ... end- Define procedureoutput <value>/op- Return valuestop- Stop procedure
Math
sum,difference,product,quotient,remaindersin,cos,tan,sqrt,pow,exp,ln,log10asin,acos,atan,atan2- inverse trig (complex-aware)abs,int,round,floor,ceiling,signmin,max- variadic min/maxrandom- random numberpi,e- constants- Infix operators:
+,-,*,/,^,=,<,>,<=,>=
Strings
lowercase,uppercase- case conversionascii,char- character codes
Variables
make "name <value>- Set variable:name- Get variable valuething "name- Get variable valuelocal "name- Declare procedure-local variablelocalmake "name <value>- Declare and set local variable
Lists
first,last,butfirst(bf),butlast(bl)fput,lput,item,countlist,sentence(se),wordrange/iseq- generate sequences:range 5→[0 1 2 3 4]reverse,pick- list utilitiesemptyp,listp,wordp,numberp,memberp- predicates
Time
time- Returns[hours minutes seconds](UTC)date- Returns[year month day](UTC)timemilli- Milliseconds since epoch
Workspace Inspection
namep "name/name? "name- Is variable defined?definedp "name/defined? "name- Is procedure defined?primitivep "name/primitive? "name- Is it a primitive?procedurep "name/procedure? "name- Is it a user procedure?procedures- List all user proceduresprimitives- List all primitivesnames- List all variables
Higher-Order Functions
map <fn> <list>- Apply function to each elementfilter <fn> <list>- Keep elements where fn returns truereduce <fn> <list>- Reduce list to single valueforeach <list> <fn>- Execute fn for each elementapply <fn> <list>- Apply fn with list as arguments
License
ISC
