To give you an idea of the syntax of the language, please see the examples below.
For a full example application, take a look at eg/mon in the source package.
println " 4! = {=factorial(4)}"
println "15! = {=factorial(15)}"
test "recurse_1" group "functions" assert fail
doc "Factorial test\n"
assert factorial(4)==24
assert factorial(15)==1307674368000
endtest
define factorial(n)
on n>1 do return n*factorial(n-1)
return 1
end
02 word frequency
# small processing test for text: equivalent code for knuth vs mcilroy
# tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed ${1}q
# example usage: collect files, take first 5000 lines, find top 100, re-sort:
# zcat /usr/share/man/man5/sa* | head -5000 | ./mcilroy 100 | sort -n
input count param 1
# read file in, lower case, remove non-words, squeeze, stuff words in array
allwords=read_file("/dev/stdin").lower.replace("[^a-zA-Z']"," ").tr("s"," ").split(" ")
# count word occurrences
var c map
foreach w in allwords
on c[w]==nil do c[w]=0
c[w]+=1
endfor
# build output string
out=""
foreach p in c
out+=p+" "+key_p+"\n"
endfor
# display 'count' lines
pos=0
foreach wc in fieldsort(out,1,"n",true)
pos++
on pos>count do break
println wc
endfor
03 sample standard deviation
define stddev(set)
avg=avg(set)
foreach x in set
deviances=append(deviances,x-avg)
endfor
foreach x in deviances
devsqr=append(devsqr,x*x)
endfor
ssd=sqrt(sum(devsqr)/(len(set)-1))
return ssd
end
a=[1,2,3,4,5]
println "Sample Standard Deviation of ",a," is ",stddev(a)
# concentric circles
max_reps=1
input max_reps optarg 1
cls
cursoroff()
h=term_h()
w=term_w()
cx=as_int(w/2)
cy=as_int(h/2)
seed(-1)
for nr=1 to max_reps
for e=0 to cy-1
cb="[#b{=rand(7)}] "
for r=0 to 359
px=cx-1+2*e*sin(r)
py=1.0+cy+e*cos(r)
at py,px,cb
endfor
endfor
endfor
cursoron()
at h-3,1,"[#default]"
at h-2,1,"h,w={h},{w}"
05 web server
HOST="localhost"
PORT=8080
HOME_PATH="eg/www/exmachina"
trap("int","cleanup(sid)")
define cleanup(s)
web_serve_stop(s)
log "Server [#{s}] Stopped.\n"
exit 0
end
sid = web_serve_start(HOME_PATH,PORT,HOST)
log "Server [#{sid}] Started.\n"
web_serve_path(sid, "s", "^/$", "http://{HOST}:{PORT}/index.html" )
web_serve_path(sid, "s", `/(.*\.(|html?|js|css|png|jpg|ico))$`, `http://{HOST}:{PORT}/$1` )
web_serve_path(sid, "s", "^/(.*)/$", "http://{HOST}:{PORT}/$1/index.html" )
while web_serve_up(sid)
# in process tasks go here:
pause 200
endwhile
06 example tail equivalent
stdin="/dev/stdin"
testmode=false
logfile=stdin
input logfile optarg 1 is "filename"
input offset_file optarg 2 is "offset filename"
input tm_inp optarg 3 is "test mode"
on tm_inp!="" do testmode=as_bool(tm_inp)
if argc()==0
println "Usage:"
println " tail [file_name] [offset_file_name] [test_mode_bool]"
exit 126
endif
on file_mode(logfile) == -1 do exit 66,"File {logfile} cannot be read."
on offset_file=="" and logfile!=stdin do offset_file=logfile+".offset"
fh=fopen(logfile,"r")
on fh==nil do exit 66,"File {logfile} cannot be read."
inode=0
ino=0
offset=0
oh=fopen(offset_file,"r")
if oh!=nil
if oh.ftell!=nil
inode=fread(oh,"\n").as_int64
offset=fread(oh,"\n").as_int64
endif
fclose(oh)
size=file_size(logfile)
on size==-1 do exit 65,"Cannot get {logfile} file size."
ino=stat(logfile).Ino
if inode==ino
on offset==size do exit 0
if offset>size
offset = 0
println "[#2]***"
println "*** WARNING ***: Log file {logfile} is smaller than last time checked!"
println "*** This could indicate tampering.[#-]"
endif
endif
on inode!=ino or offset>size do offset=0
fh.fseek(as_int(offset),0)
endif
while not feof(fh)
println fread(fh,"\n")
endwhile
on logfile!=stdin do size=fh.ftell
fclose(fh)
if not testmode and offset_file!=""
oh=fopen(offset_file,"w")
on oh==nil do exit 73,"File {offset_file} cannot be created. Check your permissions."
fwrite(oh,"{ino}\n{size}\n")
fclose(oh)
endif
07 memoizing fibonacci calculation
define fib(n)
if globkey("memo",string(n))
m=memo[n]
else
m=fib(n-2)+fib(n-1)
@memo[n] = m
endif
return m
end
memo[0]=0
memo[1]=1
memo[2]=1
input x param 1
println fib(x)
08 simple quick sort
define qs(a)
on a.len <= 1 do return a
return qs(a[1:] ?> `# <= a[0]`) + a[0:1] + qs(a[1:] ?> `# > a[0]`)
end
var arr [1000] int
for e=0 to arr.len - 1
arr[e]=rand(100000)
endfor
println qs(arr)
09 process directory listing
path="."
input path optarg 1
d=dir(path)
maxs=0
foreach f in d
on f.name.len > maxs do maxs=f.name.len
endfor
foreach f in d
print ("%s %-{maxs}s %10d[#-]\n").format(f.is_dir?"D[#1]":"F[#4]",f.name,f.size)
endfor