Supported operators, functions and statements (reserved words): + - * / ^ mod and or xor not > < >= <= <> = sqr() log() exp() sin() cos() tan() atn() pi abs() sgn() int() rnd() peek() val() asc() len() mid$() right$() left$() str$() chr$() lcase$() ucase$() goto if then else endif gosub return for to step next while wend select case rem let dim erase data read restore field$() input print open for output append as close# load save random lof loc get put inkey$ input$ eof() files fgetbyte# fseek# fputbyte run stop end exit quit cont renum new clear date$ time$ timer sound morse say doevents inputbox home cls gotoxy htab vtab pos() menu graphics sprite pset moveto lineto window scrn mouse() varptr peek() poke fre push() pop isarray mat sub call usr bload bsave def fn local exec type class extends string integer single double longint asin acos sinh cosh tanh log10 floor true false ubound Some Simple Examples: for i=1 to 10 : print i : next i c$ = a$ + b$ : rem string concatenation a$ = inkey$ : rem inkey$ is non-blocking load "sieve.bas" : rem load TEXT file as a Basic program : rem file names should have .bas extension open "filename" for output as #1 : print #1, "hello world" : close #1 graphics 0 : moveto 10,10 : lineto 110,80 : rem draw a line --- Additional Chipmunk Basic Macintosh specific commands and functions: - Mac Graphics GRAPHICS 0 :' show graphics window graphics -1 :' hide graphics window graphics WINDOW x, y, width, height :' graphics window setup :' (x,y) location, width and height graphics WINDOW width, height :' adjust the size graphics MOVETO x,y :' Move pen To x,y graphics LINETO x,y :' draw Line To x,y graphics moveto 10,20 : graphics lineto 30,50 :' example :' that draws a line from (10,20) to (30,50) graphics CIRCLE x :' circle of diameter x :' previous MOVETO x,y s ets center location graphics OVAL x,y :' oval x wide by y high graphics RECT x1,y1, x2,y2 :' FrameRect graphics FILLRECT x1,y1, x2,y2,pat# :' PaintRect graphics FILLRECT x1,y1, x2,y2,-1 :' EraseRect graphics OVAL x1,y1, x2,y2 :' FrameOval graphics FILLOVAL x1,y1, x2,y2,pat# :' PaintOval graphics PENSETUP xsize, ysize [, mode, pat# ] :' modes 8-15 :' modes - 8 patCopy, 9 patOr, 10 patXor, 11 patBic :' pat# - 1 is black, 4 is 50% dots, 12 is bricks, etc. graphics COLOR r,g,b :' red green blue 0-100% graphics COLOR 100,0,0 :' changes new color to red graphics COLOR 0,0,0 :' use default black graphics pset x,y :' plot 1 point at x,y graphics TRIANGLE x1,y1,x2,y2,x3,y3 {,r,g,b} :' draw triangle graphics PICT x,y, n :' draw 'pict' resource# n graphics PICT x,y, pictfilename$ :' draw .pict file graphics DRAWTEXT a$ :' DrawText :' use MOVETO x,y to position text lower left corner graphics TEXTSETUP f, s, m :' set up text font, size, mode graphics TEXTSETUP -1,-1 :' set up bold font graphics TEXTSETUP -1,-2 :' set up italic font graphics TEXTSETUP -1,0 :' restore normal font graphics (-38) :' return graphics window max width graphics (-39) :' return graphics window max height graphics (-34) :' return graphics window pixel depth fn scrn(x, y, 0) :' return graphics pixel value at x,y fn scrn(x, y, 1) :' return red component fn scrn(x, y, 2) :' return green component fn scrn(x, y, 3) :' return blue component :' may need to wait at least 1/4th of a second :' for the scrn function to return any recently drawn pixels :' the graphics 0 command may hasten the display update SPRITE n x, y, id :' sprite n @ x,y using ICN# id :' n from 1 to 63, 1 frontmost :' ICN# 128 - 141 included with app :' cicn requires ICN# with same id. sprite n UP x :' sprite #n move up x pixels sprite n DOWN x :' also returns new sprite y coord sprite n LEFT x :' move LEFT (not turn as in Logo!) sprite n RIGHT x sprite n TURN d :' turn counter-clockwise d degrees sprite n TURNRIGHT d :' turn clockwise d degrees sprite n TURNLEFT d sprite n TURNTO d :' turn to d degrees from x axis sprite n FORWARD x :' move forward x pixels sprite n PENUP sprite n PENDOWN sprite n COLLISION :' returns 1st sprite# in contact sprite id on x,y :' paste a fixed sprite image @(x,y) graphics WINDOW -1,-1, x,y, 2 :' scroll graphics background ' Too small a "Preferred Size" (as set from Finder Get Info window) ' may limit the maximum graphics window size. ' Under Mac OS X, execute these commands, then exit and restart to ' try and increase maximum graphics window size. fn saveaspng(fname$, x,y, w,h) :' saves pixels to a PNG file :' preceed with graphics 0 + wait 1 second fn saveaspng(fname$, x,y, w,h, 32896) :' with 50% grey as transparancy graphics PICT x,y, filename$ :' draws graphics from image file at (x,y) graphics 0 :' refresh graphics window now :' for control of graphics animation update rate - Mac Sound commands morse "CQ DE N6YWU",16,40,13,700 :' play morse code :' MORSE string, dot_wpm, volume, letter_wpm, freq say "hello world" :' *requires* the Speech Manager say "faster", 196, 44, 1 :' string, rate, pitch, voice# say :' reads the last 12 lines of the console window sound 440, 0.5, 30 :' freq, secs_duration, vol(0-100) sound 0, 8192 :' play snd resource #8192 sound n,freq(0),dur,vol(0) :' play n tones from arrays freq & vol :' 1 < n < 9 => up to 8 multivoice tones (chords) ' Quicktime MIDI synthesis sound -2, midi_voice, midi_key, velocity, secs_dur [,channel] :' play note using Quicktime MIDI synthesizer :' midi_voice = 1 -> piano :' midi_key = 60 -> middle C :' midi_velocity = 100 -> loud :' simultaneous channels 1 to 4 supported :' default channel is 1 sound -3, midi_voice, midi_key, velocity, secs_dur [,channel] :' wait for previous note to finish then play sound -4, midi_voice, midi_key, velocity, secs_dur :' play on any free channel without waiting fn playsound( fname$, n ) :' play up to n seconds from sound file fname$ ' sampled sound support sound -1, a(0), n :' play n samples from array a() :' at sample rate 11025 sound -1, a%(0), n, 44100, 16 :' play sampled sound :' 16 bit quality at 44100 sample rate fn record(a(0),n) fn record(a%(0),n) :' records n samples of sound into a() or a%() :' 16-bit quality at a sample rate of 11025 fn record(a%(0),n, 44100) :' record 16-bit mono at a sample rate of 44100 - Mac User interface functions and commands cls : rem clears console & graphics windows gotoxy 5,10 : print "here" : rem move text cursor (0,0 origin) x = pos(0) : rem text cursor horizontal location y = pos(-1) : rem vertical text cursor location a$ = inputbox("prompt", "title", "default", 0) :' gets string input using default dialog a$ = inputbox("prompt", "title", "default", id) :' if id > 1024 this will use a custom DLOG #id :' with item 1 = OK, item 2 = cancel :' prompt replaces 1st static text item :' default replaces 1st edit text item mouse(0) ' returns 1 if the mouse button is down mouse(1) ' x coordinate in current window mouse(2) ' y coordinate in current window mouse(3) ' last x click location (set to -1 after read) mouse(4) ' last y click location (set to -1 after read) :' mouse() must follow a graphics command :' to get a location in the graphics window. graphics BUTTON title$, x,y, w,h, key_code :' draw a standard button at (x,y) size (w,h) :' will input chr$(key_code) when pressed graphics BUTTON title$, x,y, w,h, e$ :' button will exec(e$) when pressed graphics BUTTON "",-1 :' remove all buttons & sliders menu 4, title$, itemlist$, eventlist$ :' setup menu menu 4, "myMenu", "item1;item2", "foo1();foo2()" :' example :' sets up the 4th menu (only 4th menu is implemented.) :' calls your subroutines foo1() & foo2() on menu select. :' subroutines must be previously defined. :' see Apple documentation for menu options menu 4, "" :' removes your menu fn doevents() :' poll for button and menu events - Additional Mac specific functions and commands: open "SFGetFile" for input as #2 :' dialog prompt for filename open "SFPutFile" for output as #5 :' uses dialog box for name open f$ for data input as #4 :' data file, no CR-LF translation open f$ for data input :' use file for snd & pict, etc. resources a$ = field$("aa bb cc", 2, " ") :' returns "bb", (awk fields) a$ = field$(a$, n, chr$(9)) :' return nth item in TSV line a$ - AppleScript scripting. Try this script from a script editor: tell application "chipmunk-basic" to DoScript "eval ( 5 * 7 )" tell application "chipmunk-basic" to DoScript "eval date$" :' eval will return a string result to the script editor. tell application "chipmunk-basic" to DoScript "print 22 * 7" tell application "chipmunk-basic" to DoScript "say 'hello'" :' Tells Chipmunk Basic to executes a statement. tell application "chipmunk-basic" to DoScript ("runStatus") :' returns whether a Basic program is running - ICN#, cicn, PICT & BCMD resources may be stored the resource fork of any file, not just the "mumble.bas" program file. Use: open "foo.rsrc" for data input : rem with no "as" token, :' this makes the file "foo.rsrc" the current resource file - A few experimental commands which may only work in OS X 10.2 or above : open "COM1:/dev/cu.modem:1200,N,8,1" for output as #1 open "COM1:/dev/cu.modem:1200,N,8,1" for input as #2 :' opens serial port with given unix path and baud rate fn serialpath(n) :' returns the path string for the n-th serial port :' starting with 1 (usually the internal modem) :' example: 100 rem serial i/o example 120 sp1$ = fn serialpath(1) : print sp1$ 130 if sp1$ = "" then stop 140 sp2$ = fn serialpath(2) : print sp2$ 160 config$ = "COM1:" + sp1$ + ":9600,N,8,1" 180 print config$ : print 200 open config$ for input as #2 220 open config$ for output as #1 240 k = 0 260 while k <> 3 : rem control-C to exit 280 a$ = input$(1,2) 320 print a$; 340 k$ = inkey$ : k = asc(k$) 360 if (k > 3) then fputbyte k,#1 380 wend 420 close #2 : close #1 440 print : print "done" 460 end :' Note that Chipmunk Basic unix pipes and sockets also work :' under OS X . See the man page for details. open "socket:www.yahoo.com",80 for input as #1 open "socket:www.yahoo.com",80 for output as #2 :' open tcp/ip socket to a web server sys(command$) :' execute unix terminal command :' no stdout/output provisioned system$(command$) :' execute unix terminal command :' returns 1st line of standard output dir :' displays files in current working directory - A few other experimental commands & functions (beta test) : fn version$() :' returns version & graphics capability ' To change the command console window's location & size, try: fn midiinput(n) :' was macfunction( "midiInput", n ) :' n = 0 for time (returns 0 if no new note available) :' n = 2 for channel of last note :' n = 3 for note of last note :' n = 4 for velocity of last note fn midi(2) :' init a midi data output channel fn midi(3, note, velocity) :' send midi noteon/off :' set velocity to 0 for noteoff fn fft1( 1, x(0), y(0), n ) :' fft arrays x,y (real & imaginary vectors, length n) :' n must be a power of 2, array sizes must be >= n fn fft1(-1, x(0), y(0), n ) :' inverse fft fn plot1( 0, f$, x$, y$, x0, x1, n) :' draw autoscaled xy plot fn plot1( 0, "sin(x)", "x", "y", 0, 7, 200 ) :' plot 200 points from 0 to 7 of sin(x) fn plot1( 1, f$, x$, y$, x0, x1, n) :' plot using scaling & grid of previous plot fn plot1( 4, f$, a$, r$, a0, a1, n) :' autoscaling polar plot fn plot2( 0, n, x(0), y(0) ) :' parametric plot of n points from arrays x & y x = 0.5 x2 = fn solve1("2*x=cos(x)", "x") :' attempts to solve for variable "x" :' start by assigning a decent guess to variable :' repeat function call for convergence if necessary :' may work for very "well behaved" functions only! fn math$("add$", n$, m$) :' 250 digit precision math on number strings :' commands: "add$" "sub$" "mul$" "div$" fn math$("powm", a$, e$, m$) :' commands: "powm" for raise-to-exponent-modulo bsave "filename", ptr, len :' binary memory save i = bload("filename", ptr, len) :' binary file load to mem :' use with varptr() to get memory addresses fn kill( filename$ ) :' deletes the named file :' returns 0 on success sprite 0, id, gif_filename$, x,y, 129 :' draw gif/jpeg file as a sprite at (x,y) - A few experimental Core Bluetooth functions (beta test) : fn btle.init() ’ initializes Bluetooth LE/4.0 access ’ starts a BLE scan ’ returns 0 if the scan has started fn btle.list() ’ presents a list of discovered BLE peripherals fn btle.selected() ’ returns the UUID string for the selected device fn btle.info(n,1) ’ returns the UUID of the nth listed device fn btle.info(n,2) ’ returns the name of the nth listed device fn btle.rssi(n) ’ returns the RSSI of the nth listed device fn btle.connect(1,uuid$) ’ connects to a BLE device fn btle.busy() ' waiting on a connection, notification or read fn btle.connect(1,uuid$,service$,chctrstc$) ’ connects and requests a notification fn btle.request(1,service$,chctrstc$) ’ read request for a BLE characteristic fn btle.response$(1,a%(0)) ’ the read data (if any yet) as a string ’ set to "" after the first read fn btle.responsedata(1,1,a%(0)) ’ the byte count of the read data (if any yet) ' the bytes are return in integer array ' with 2 bytes per array element ’ set to 0 after the first read fn btle.write(1,service$,chctrstc$,data$) ’ request a write to a BLE characteristic fn btle(9) ’ disconnects from BLE device Warning. Use at your own risk. Alpha and experimental commands may go away in future releases.