//a side-by-side reference sheet//
[#grammar-invocation grammar and invocation] | [#var-expr variables and expressions] | [#stack stack] | [#arithmetic-logic arithmetic and logic] | [#strings strings] _ [#dates-time dates and time] | [#arrays arrays] | [#heap heap] | [#dictionaries dictionaries] | [#functions functions] | [#execution-control execution control] | [#exceptions exceptions] | [#file-handles file handles] | [#files files] _ [#directories directories] | [#processes-environment processes and environment] | [#libraries-namespaces libraries and namespaces] | [#reflection reflection] | [#graphics graphics]
||~ ||~ [#forth forth]||~ [#postscript postscript]|| ||# version-used[#version-used-note version used] _ @< >@||##gray|//Gforth 0.70//##||##gray|//Preview 5.0//##|| ||# show-version[#show-version-note show version] _ @< >@||$ gforth @@–@@version||##gray|//select// About Preview //in// Preview //menu//##|| ||||||~ # grammar-invocation[#grammar-invocation-note grammar and invocation]|| ||~ ||~ forth||~ postscript|| ||# interpreter[#interpreter-note interpreter] _ @< >@||$ echo “1 1 + . cr bye” > /tmp/add.forth _ _ $ gforth /tmp/add.forth||##gray|//none//##|| ||# shebang[#shebang-note shebang] _ @< >@||##gray|//none//##||##gray|//none//##|| ||# repl[#repl-note repl] _ @< >@||$ gforth||##gray|//none//##|| ||# save-image[#save-image-note save image]|| || || ||# load-image[#load-image-note load image]|| || || ||# identifiers-case-sensitive[#identifiers-case-sensitive-note are identifiers case sensitive]||##gray|//no//##||##gray|//yes//##|| ||# eol-comment[#eol-comment-note end-of-line comment]||1 1 + ##gray|\ addition##||1 1 add ##gray|% addition##|| ||# multiple-line-comment[#multiple-line-comment-note multiple line comment]||1 1 ##gray|( comment _ another comment )## +||##gray|//none//##|| ||||||~ # var-expr[#var-expr-note variables and expressions]|| ||~ ||~ forth||~ postscript|| ||[#assignment assignment]||variable a _ 3 a ! _ 7 a @ +||/a 3 def _ 7 currentdict /a get add|| ||[#increment-decrement increment and decrement]||7 1+ _ 7 1-|| || ||[#null null] _ @< >@||##gray|//none//##||null|| ||[#null-test null test] _ @< >@||##gray|//none//##|| || ||[#undefined-word undefined variable access]|| || || ||||||~ # stack[#stack-note stack]|| ||~ ||~ forth||~ postscript|| ||[#stacks stacks]||##gray|//data _ floating point _ return _ locals//##||##gray|//operand _ dictionary _ execution _ graphics state _ clipping path//##|| ||[#stack-underflow stack underflow]|| || || ||[#display-stack display stack]||.s _ ##gray|//displays float stack://## _ f.s||##gray|//none//##|| ||[#clear-stack clear stack]||##gray|//clears data stack://## _ clearstack _ ##gray|//clears data and float stack://## _ clearstacks||clear|| ||[#count-stack count stack]||depth _ fdepth||count|| ||[#copy-stack copy stack] _ @< >@|| ||count copy|| ||[#drop (a – )]||drop _ ##gray|//displays item being dropped://## _ . _ ##gray|//operate on float stack://## _ fdrop _ f.||pop|| ||[#dup (a – a a)]||dup _ fdup||dup|| ||[#swap (a b – b a)]||swap _ fswap||exch|| ||[#nip (a b – b)]||nip _ fnip||exch pop|| ||[#over (a b – a b a)]||over _ fover||1 index|| ||[#tuck (a b – b a b)]||tuck _ ftuck||dup 3 1 roll|| ||[#rot (a b c – b c a)]||rot _ frot||3 -1 roll|| ||[#pick (a b c – a b c a)]||2 pick _ 2 fpick||2 index|| ||[#roll (a b c d – b c d a)]||3 roll _ 3 froll||4 -1 roll|| ||||||~ # arithmetic-logic[#arithmetic-logic-note arithmetic and logic]|| ||~ ||~ forth||~ postscript|| ||# true-false[#true-false-note true and false]||true false _ -1 0||true false|| ||# falsehoods[#falsehoods-note falsehoods] _ @< >@||false 0||false|| ||# logical-op[#logical-op-note logical operators]||and or invert xor||and or not xor|| ||# relational-op[#relational-op-note relational operators]||@@=@@ <> > < >= <= _ f= f<> f> f< f>= f<=||eq ne gt lt ge le|| ||# min-max[#min-max-note min and max]||1 2 min _ 1 2 max|| || ||# three-val-comparision[#three-val-comparison-note three value comparison]|| || || ||# float-literal[#float-literal-note float literal]||##gray|//must contain an// e:## _ 3.14e0||3.14 _ 3.14e0|| ||# num-predicates[#num-predicates-note numeric predicates]|| || || ||# arithmetic-op[#arithmetic-op-note arithmetic operators]||+ - * ##gray|//??//## / mod ##gray|//??//## _ ##gray|//float stack operators://## _ f+ f- f* f/ f||add sub mul div idiv mod exp|| ||# unary-negation[#unary-negation-note unary negation]||negate _ fnegate||neg|| ||integer division|| || || ||integer division by zero|| || || ||float division|| || || ||float division by zero|| || || ||# power[#power-note power]||2e0 32e0 f|| || ||# sqrt[#sqrt-note sqrt]||2e0 fsqrt|| || ||[#square-root-negative-2 sqrt(-2)]||##gray|//pushes this non-literal value onto stack://## _ -na|| || ||# transcendental-func[#transcendental-func-note transcendental functions]||fexp fln fsin fcos ftan fasin facos fatan fatan2 _ ##gray|//also// flog //for base 10 logarithm//##||sqrt exp ln sin cos ##gray|//none//## ##gray|//none//## ##gray|//none//## ##gray|//none//## atan _ ##gray|//also// log //for base 10 logarithm//##|| ||# transcendental-const[#transcendental-const-note transcendental constants]||pi _ 1e0 fexp|| || ||# float-truncation[#float-truncation-note float truncation]||##gray|//return floats//## _ fround ##gray|//??//## floor ##gray|//??//##||round truncate floor ceiling|| ||# absolute-value[#absolute-val-note absolute value] _ ##gray|//and signum//##||-7 abs _ -7e0 fabs||abs|| ||# int-overflow[#int-overflow-note integer overflow]||##gray|//modular arithmetic//##|| || ||[#float-overflow float overflow] _ @< >@|| || || ||[#float-limits float limits]|| || || ||# rational-construction[#rational-construction-note rational construction]|| || || ||# rational-decomposition[#rational-decomposition-note rational decomposition]|| || || ||# complex-construction[#complex-construction-note complex construction]|| || || ||# complex-conjugation[#complex-conjugation-note complex decomposition] _ ##gray|//real and imaginary component, argument, absolute value, conjugate//##|| || || ||[#random-num-note random number] _ ##gray|//uniform integer, uniform float, normal float//##|| ||##gray|rand //generates random integer from 0 to 231-1//:## _ rand 100 mod _ ##gray|//none//## _ ##gray|//none//##|| ||# random-seed[#random-seed-note random seed] _ @< >@|| ||13 srand|| ||# bit-op[#bit-op-note bit operators]||and or xor invert _ ##gray|//logical shift by 3://## _ 7 3 lshift _ 7 3 rshift _ ##gray|//arithmetic shift by 1://## _ 7 2* _ 7 2/||and or xor not _ ##gray|//logical shift by 3://## _ 7 3 bitshift _ 7 -3 bitshift|| ||# binary-octal-hex-literals[#binary-octal-hex-literals-note binary, octal, and hex literals]|| || || ||# radix[#radix-note radix]|| || || ||||||~ # strings[#strings-note strings]|| ||~ ||~ forth||~ postscript|| ||[#string-literals string literals]||##gray|//allocates memory for string and pushes address and length onto stack://## _ s" don’t say "no"”||(don’t say “no”) _ ##gray|//parens and backslashes are escaped with backslashes. Balanced parens do not need to be escaped.//##|| ||[#string-escapes string escapes]||\a \b \e \f \n \r \t \v " \ \, ##gray|//ooo//## \x##gray|//xx//##||\b \f \n \r \t \ ( ) ##gray|//ooo//##|| ||[#string-newline newline in literal]||##gray|//no, use \n//##||##gray|//no, use \n//##|| ||[#character-access character access]|| ||(hello) 0 get|| ||[#string-length length]|| ||(hello) length|| ||[#string-concatenate concatenate]|| || || ||[#split split]|| || || ||[#join join]|| || || ||[#trim trim]|| || || ||[#pad pad]|| || || ||[#number-string-conversion number to string]|| ||7 (12) cvi add _ 73.9 (.037) cvr add|| ||string to number|| || || ||[#case-manipulation case manipulation]|| || || ||[#sprintf sprintf]|| || || ||||||~ # dates-time[#dates-time-note dates and time]|| ||~ ||~ forth||~ postscript|| ||# broken-down-date-time-type[#broken-down-date-time-type-note broken-down date and time type]|| || || ||# current-date-time[#current-date-time-note current date/time]||##gray|\ pushes six values onto stack:## _ date&time|| || ||# unix-epoch[#unix-epoch-note to unix epoch, from unix epoch]|| || || ||# current-unix-epoch[#current-unix-epoch-note current unix epoch]|| || || ||# date-time-to-str[#date-time-to-str-note date and time to string]|| || || ||# format-date[#format-date-note format date]|| || || ||# parse-date[#parse-date-note parse date]|| || || ||# date-subtraction[#date-subtraction-note date subtraction]|| || || ||# add-duration[#add-duration-note add duration]|| || || ||# date-parts[#date-parts-note date parts]|| || || ||# time-parts[#time-parts-note time parts]|| || || ||# build-date-time[#build-date-time-note build broken-down date and time]|| || || ||# dow-doy[#dow-doy-note day of week and day of year]|| || || ||# microseconds[#microseconds-note microseconds]|| || || ||# sleep[#sleep-note sleep]|| || || ||# timeout[#timeout-note timeout]|| || || ||# cpu-usage[#cpu-usage-note cpu usage]|| || || ||||||~ # arrays[#arrays-note arrays]|| ||~ ||~ forth||~ postscript|| ||# array-literal[#array-literal-note literal]|| ||[ 1 2 3 ]|| ||# array-size[#array-size-note size] _ @< >@|| ||[ 1 2 3 ] length|| ||# empty-array-test[#empty-array-test-note empty test] _ @< >@|| || || ||# array-lookup[#array-lookup-note lookup] _ @< >@|| ||[ 1 2 3 ] 0 get|| ||# array-update[#array-update-note update]|| || || ||# array-out-of-bounds[#array-out-of-bounds-note out-of-bounds behavior]|| ||##gray|rangecheck //error//##|| ||# array-element-index[#array-element-index-note element index]|| || || ||# slice-array[#slice-array-note slice]|| || || ||slice to end|| || || ||manipulate back|| || || ||manipulate front|| || || ||# concatenate-array[#concatenate-array-note array concatenate]|| || || ||replicate|| || || ||copy|| || || ||[#array-iteration array iteration]|| ||[ 50 100 150 ] { 100 moveto (foo) show } forall|| ||indexed iteration|| || || ||iterate over range|| || || ||instantiate range as array|| || || ||reverse|| || || ||sort|| || || ||dedupe|| || || ||# membership[#membership-note membership]|| || || ||# intersection[#intersection-note intersection]|| || || ||# union[#union-note union]|| || || ||relative complement, symmetric difference|| || || ||[#array-map map]|| || || ||[#array-filter filter]|| || || ||[#array-reduce reduce]|| || || ||min and max element|| || || ||universal and existential tests|| || || ||[#sample sample w/o replacement]|| || || ||# zip[#zip-note zip]|| || || ||||||~ # heap[#heap-note heap]|| ||[#allocate allocate region on heap]||##gray|//pushes address and// 0 //onto stack://## _ 10 allocate|| || ||[#free free region on heap]||free|| || ||[#resize resize region on heap]|| || || ||[#write write]|| || || ||[#read read]|| || || ||||||~ # dictionaries[#dictionaries-note dictionaries]|| ||~ ||~ forth||~ postscript|| ||[#dictionary-literal dictionary literal]|| ||@@ << @@1 (one) 2 (two) 3 (three)@@ >> @@|| ||[#dictionary-length dictionary length]|| ||@@ << @@1 (one) 2 (two)@@ >> @@length|| ||[#dictionary-element-access dictionary element access]|| ||##gray|//pushes// (one) //onto stack://## _ @@ << @@1 (one) 2 (two)@@ >> @@1 get|| ||[#dictionary-element-not-found element not found behavior]|| ||##gray|undefined //error//##|| ||[#dictionary-set-element set dictionary element]|| ||/h@@ << @@1 (one) 2 (two)@@ >> @@def _ currentdict /h get 3 (three) put|| ||[#range range]|| || || ||||||~ # functions[#functions-note functions]|| ||~ ||~ forth||~ postscript|| ||# def-func[#def-func-note define function]||: add3 + + ; ||/add3 { add add } def|| ||# invoke-func[#invoke-func-note invoke function]||3 7 5 add3||3 7 5 add3|| ||# missing-arg[#missing-arg-note missing argument behavior]||##gray|//a stack underflow error results if the expected number of arguments is not on the stack//##||##gray|//a stack underflow error results if the expected number of arguments is not on the stack//##|| ||# retval[#retval-note return value] _ @< >@||##gray|//values are returned by pushing them onto the stack//##||##gray|//values are returned by pushing them onto the stack//##|| ||# multiple-retval[#multiple-retval-note multiple return values]||##gray|//no limit on the number of values a function can leave on the stack//##||##gray|//no limit on the number of values a function can leave on the stack//##|| ||# recursive-func[#recursive-func-note recursive function]|| || || ||# mutually-recursive-func[#mutually-recursive-func-note mutually recursive functions]|| || || ||# anonymous-func[#anonymous-func-note anonymous function]|| ||{ 2 add }|| ||# invoke-anonymous-func[#invoke-anonymous-func-note invoke anonymous function]|| ||3 { 2 add } exec|| ||# closure[#closure-note closure]|| || || ||# func-as-val[#func-as-val-note function as value]|| || || ||# compose-func[#compose-func-note compose functions]|| || || ||# partial-application[#partial-application-note partial application]|| || || ||# fried-quotation[#fried-quotation-note fried quotation]|| || || ||||||~ # execution-control[#execution-control-note execution control]|| ||~ ||~ forth||~ postscript|| ||[#if-note notes on conditionals]||##gray|//conditionals can only be used in word definitions.//## _ ##gray|endif //can be used in place of// then##|| || ||[#ifelse ifelse]||: ifelse rot if drop else nip then ; _ x @ 0 > x @ x @ negate ifelse||x 0 gt { x } { x neg } ifelse|| ||[#if if]||##gray|//bool//## _ if _ @< >@##gray|//if true//## _ then||##gray|//bool//## { ##gray|//if true//## } if|| ||[#if-else if else]||##gray|//bool//## _ if _ @< >@##gray|//if true//## _ else _ @< >@##gray|//if false//## _ then||##gray|//bool//## { ##gray|//if true//## } { ##gray|//if false//## } ifelse|| ||[#loop-note notes on loops]||##gray|//loops can only be used in word definitions.//## _ ##gray|leave //can be used with// loop //to terminate early//##||##gray|exit //terminates innermost// for, forall, loop, //or// repeat##|| ||[#conditional-loop conditional loop]||begin _ @< >@##gray|//bool//## _ while _ @< >@##gray|//code//## _ repeat||{ _ @< >@##gray|//bool//## { exit } if _ @< >@##gray|//code//## _ } loop|| ||[#infinite-loop infinite loop]||begin _ @< >@##gray|//code//## _ again||{ ##gray|//code//## } loop|| ||[#loop-10-times loop 10 times]||10 0 ?do ##gray|//code//## loop||10 { ##gray|//code//## } repeat|| ||[#for-loop for loop]||10 0 ?do i ##gray|//code//## loop||##gray|//iterator value on stack//## _ 0 20 200 { 72 exch moveto (foo) show } for|| ||[#for-loop-step-2 for loop step 2]||10 0 +do i ##gray|//code//## 2 +loop|| || ||||||~ # exceptions[#exceptions-note exceptions]|| ||~ ||~ forth||~ postscript|| ||[#exception-note notes on exceptions]||##gray|throw //raises an exception if the top of the data stack is nonzero.// exception //puts a nonzero value on the stack. If the exception is not handled the string will be displayed by the interpreter on exit.// catch //can only be used in a word definition.//##|| || ||[#raise-exception raise exception]||s” bam!” exception throw||$error /errorname (foo) put stop|| ||[#handle-exception handle exception]||[‘] ##gray|//risky word//## catch if ##gray|//handle error//## then||{ _ @< >@##gray|//risky code//## _ } stopped { ##gray|//handle error//## }|| ||||||~ # file-handles[#file-handles-note file handles]|| ||~ ||~ forth||~ postscript|| ||standard file handles|| || || ||read line from stdin|| || || ||write line to stdout|| || || ||[#write-stdout write to stdout]||.” hello” cr|| || ||[#redirect-to-file redirect to file]|| || || ||||||~ # files[#files-note files]|| ||~ ||~ forth||~ postscript|| ||file test, regular file text|| || || ||file size|| || || ||is file readable, writable, executable|| || || ||set file permissions|| || || ||touch file|| || || ||copy file, remove file, rename file|| || || ||create symlink, symlink test, readlink|| || || ||generate unused file name|| || || ||||||~ # directories[#directories-note directories]|| ||~ ||~ forth||~ postscript|| ||working directory _ ##gray|//get and set//##|| || || ||build pathname|| || || ||dirname and basename|| || || ||absolute pathname|| || || ||make directory|| || || ||recursive copy|| || || ||remove empty directory|| || || ||remove directory and contents|| || || ||directory test|| || || ||||||~ # processes-environment[#processes-environment-note processes and environment]|| ||~ ||~ forth||~ postscript|| ||# cmd-line-arg[#cmd-line-arg-note command line arguments]|| || || ||# program-name[#program-name-note program name]|| || || ||# env-var[#env-var-note environment variable] _ ##gray|//get, set//##|| || || ||# user-id-name[#user-id-name-note user id and name]|| || || ||# exit[#exit-note exit]||bye||quit|| ||# external-cmd[#external-cmd-note external command]|| || || ||# backticks[#backticks-note backticks]|| || || ||# pid[#pid-note pid] _ ##gray|//and parent pid//##|| || || ||# signal-handler[#signal-handler-note set signal handler]|| || || ||# send-signal[#send-signal-note send signal]|| || || ||||||~ # libraries-namespaces[#libraries-namespaces-note libraries and namespaces]|| ||~ ||~ forth||~ postscript|| ||[#interpret-file interpret file]||foo.fs include _ ##gray|//or//## _ s” foo.fs” included|| || ||[#load-library load library]||require foo.fs _ ##gray|//or//## _ s” foo.fs” required|| || ||hot patch|| || || ||load error|| || || ||main routine in library|| || || ||library path|| || || ||library path environment variable|| || || ||namespace declaration|| || || ||unqualified import of namespace|| || || ||||||~ # reflection[#reflection-note reflection]|| ||~ ||~ forth||~ postscript|| ||[#data-type query data type]||##gray|//none//##||3 type|| ||[#pretty-print pretty print]|| || || ||[#dump-location-and-stack dump location and stack]||@@~~@@|| || ||[#show-code show code]||see ##gray|//word//##|| || ||[#function-documentation documentation]|| || || ||||||~ # graphics[#graphics-note graphics]|| ||~ ||~ forth||~ postscript|| ||[#default-origin default location of origin]|| ||##gray|//lower left corner of page//##|| ||[#default-scale default scale]|| ||##gray|//72 units per inch//##|| ||[#text text]|| ||72 72 moveto (hello) show|| ||[#render-page render page and start a new one]|| ||showpage|| ||[#set-font set current font]|| ||/Helvetica 20 selectfont|| ||[#built-in-fonts built-in fonts]|| ||/Times-Roman /Times-Italic _ /Times-Bold /Times-BoldItalic _ /Helvetica /Helvetica-Oblique _ /Helvetica-Bold /Helvetica-BoldOblique _ /Courier /Courier-Bold _ /Courier-Oblique /Courier-BoldOblique|| ||[#closed-polygon closed polygon]|| ||##gray|//creates outline of triangle://## _ newpath _ 72 72 moveto _ 144 144 lineto _ 144 72 lineto _ closepath _ stroke|| ||[#set-line-width set current line width]|| ||##gray|//default value is 1://## _ 2 setlinewidth|| ||[#circle-arc arc of circle]|| ||##gray|//creates half-circular arc centered at (144,144) with radius 40://## _ newpath _ 144 144 40 90 270 arc _ stroke|| ||[#bezier-curve bezier curve]|| ||##gray|//control points are (144,144), (288,144), (288,288), and (144,288)://## _ newpath _ 144 144 moveto _ 288 144 288 288 144 288 curveto _ stroke|| ||[#filled-polygon filled polygon]|| ||##gray|//creates solid triangle://## _ newpath _ 72 72 moveto _ 144 144 lineto _ 144 72 lineto _ closepath _ fill|| ||[#set-color set color]|| ||##gray|//default color is black. Sets current color to red://## _ 1.0 0.0 0.0 setrgbcolor|| ||[#translate-coordinates translate coordinates]|| ||##gray|//move origin 72 units up and to the right://## _ 72 72 translate|| ||[#scale-coordinates scale coordinates]|| ||##gray|//increase size of unit 72-fold://## _ 72 72 scale|| ||[#rotate-coordindates rotate coordinates]|| ||##gray|//rotate coordinates 90 degrees counterclockwise://## _ 90 rotate|| ||[#graphics-state items in graphics state]|| ||currentpoint _ currentrgbcolor _ currentfont _ currentmatrix _ currentlinewidth|| ||[#save-graphics-state save graphics state]|| ||gsave|| ||[#restore-graphics-state restore graphics state]|| ||grestore|| ||~ ||~ ##EFEFEF|@@__________________________________________________________@@##||~ ##EFEFEF|@@__________________________________________________________@@##||
# version-used-note ++ [#version-used version used]
The version used for the examples in this sheet.
# show-version-note ++ [#show-version show version]
How to get the version.
# grammar-invocation-note + [#grammar-invocation Grammar and Invocation]
# interpreter-note ++ [#interpreter interpreter]
How to invoke the interpreter.
# shebang-note ++ [#shebang shebang]
How to make a source file executable on a UNIX system.
# repl-note ++ [#repl repl]
How to invoke the REPL.
# save-image-note ++ [#save-image save image]
# load-image-note ++ [#load-image load image]
# identifiers-case-sensitive-note ++ [#identifiers-case-sensitive are identifiers case sensitive]
Are words case sensitive?
# eol-comment-note ++ [#eol-comment end-of-line comment]
The syntax for a comment which ends at the end of the line.
# multiple-line-comment-note ++ [#multiple-line-comment multiple line comment]
The syntax for a comment which can span multiple lines.
forth:
Paren delimited comments cannot span multiple lines when using the REPL.
Paren delimited comments do not nest.
# var-expr-note + [#var-expr Variables and Expressions]
# assignment ++ assignment
# stack-note + [#stack Stack]
# arithmetic-logic-note + [#arithmetic-logic Arithmetic and Logic]
# arithmetic-operators ++ operators
# strings-note + [#strings Strings]
# dates-time-note + [#dates-time Dates and Time]
# broken-down-date-time-type-note ++ [#broken-down-date-time-type broken-down date and time type]
# current-date-time-note ++ [#current-date-time current date/time]
# unix-epoch-note ++ [#unix-epoch to unix epoch, from unix epoch]
# current-unix-epoch-note ++ [#current-unix-epoch current unix epoch]
# date-time-to-str-note ++ [#date-time-to-str date and time to string]
# format-date-note ++ [#format-date format date]
# parse-date-note ++ [#parse-date parse date]
# date-subtraction-note ++ [#date-subtraction date subtraction]
# add-duration-note ++ [#add-duration add duration]
# date-parts-note ++ [#date-parts date parts]
# time-parts-note ++ [#time-parts time parts]
# build-date-time-note ++ [#build-date-time build broken-down date and time]
# dow-doy-note ++ [#dow-doy day of week and day of year]
# microseconds-note ++ [#microseconds microseconds]
# sleep-note ++ [#sleep sleep]
# timeout-note ++ [#timeout timeout]
# cpu-usage-note ++ [#cpu-usage cpu usage]
# arrays-note + [#arrays Arrays]
# array-literal-note ++ [#array-literal literal]
Syntax for an array literal.
# array-size-note ++ [#array-size size]
How to get the length of an array.
# empty-array-test-note ++ [#empty-array-test empty test]
How to test whether an array is empty.
# array-lookup-note ++ [#array-lookup lookup]
How to look up the value stored at an index.
# array-update-note ++ [#array-update update]
How to set the value stored at an index.
# array-out-of-bounds-note ++ [#array-out-of-bounds out-of-bounds behavior]
What happens when an attempt is made to access the value at a non-existent index.
# zip-note ++ [#zip zip]
How to convert two sequences into a sequence of pairs.
# heap-note + [#heap Heap]
# dictionaries-note + [#dictionaries Dictionaries]
# functions-note + [#functions Functions]
# def-func-note ++ [#def-func define function]
How to define a function.
# invoke-func-note ++ [#invoke-func invoke function]
How to invoke a function.
# missing-arg-note ++ [#missing-arg missing argument behavior]
What happens when too few arguments are passed to a function.
It is an error in stack-oriented languages when a function tries to read more values from a stack than are there. Since the stack may have had values in it which were not intended for the function, so in general stack-oriented languages cannot detect when too few arguments are passed to a function.
# retval-note ++ [#retval return value]
How to return a value from a function.
# multiple-retval-note ++ [#multiple-retval multiple return values]
# recursive-func-note ++ [#recursive-func recursive function]
# mutually-recursive-func-note ++ [#mutually-recursive-func mutually recursive functions]
# anonymous-func-note ++ [#anonymous-func anonymous function]
# invoke-anonymous-func-note ++ [#invoke-anonymous-func invoke anonymous function]
# closure-note ++ [#closure closure]
# func-as-val-note ++ [#func-as-val function as value]
How to treat a function as a value.
# compose-func-note ++ [#compose-func compose functions]
# partial-application-note ++ [#partial-application partial application]
# fried-quotation-note ++ [#fried-quotation fried quotation]
# execution-control-note + [#execution-control Execution Control]
# exceptions-note + [#exceptions Exceptions]
# file-handles-note + [#file-handles File Handles]
# files-note + [#files Files]
# directories-note + [#directories Directories]
# processes-environment-note + [#processes-environment Processes and Environment]
# cmd-line-arg-note ++ [#cmd-line-arg command line arguments]
# program-name-note ++ [#program-name program name]
# env-var-note ++ [#env-var environment variable]
# user-id-name-note ++ [#user-id-name user id and name]
# exit-note ++ [#exit exit]
# external-cmd-note ++ [#external-cmd external command]
# backticks-note ++ [#backticks backticks]
# pid-note ++ [#pid pid]
# signal-handler-note ++ [#signal-handler set signal handler]
# send-signal-note ++ [#send-signal send signal]
# libraries-namespaces-note + [#libraries-namespaces Libraries and Namespaces]
# reflection-note + [#reflection Reflection]
# graphics-note + [#graphics Graphics]
# forth + [#top Forth]
[http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/ Gforth User Manual] [http://galileo.phys.virginia.edu/classes/551.jvn.fall01/primer.htm A Beginner’s Guide to Forth] [http://thinking-forth.sourceforge.net/ Thinking Forth] [http://lars.nocrew.org/dpans/dpans.htm ANS Forth]
The data stack used by Forth procedures to pass operands consists of cells of machine words. These will be interpreted as signed integers, unsigned integers, or memory addresses, depending upon the operator invoked on them. A Forth interpreter does not perform any checks to ensure type safety.
To work with strings and arrays, a Forth programmer must allocate and free space on the heap with the //allocate// and //free// commands. Strings are then represented on the data stack with two cells: the address of the string and the length of the string. To make working with these string representations easier, there are operators which manipulate the stack two cells at a time: //2drop//, //2dup//, //2swap//.
Most Forth implementations support floating point numbers. Floats are kept on a separate stack, and many data stack operators have a floating point stack analogue. Floating point operators have an “f” prefix: //fdrop//, //fdup//, //fswap//, //f+//, //f*//, //f<//.
# postscript + [#top PostScript]
[http://partners.adobe.com/public/developer/en/ps/PLRM.pdf PostScript Language Reference (pdf)] [http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf PDF Reference (pdf)]
PostScript is a commonly used page description language. It has programming language features one might not expect to find in such a special purpose language such as looping and branching commands, arrays and dictionaries, and even commands which read from standard input and write to standard out.
Like Forth, PostScript is a language with built-in operators that add value to or remove values from an operand stack. Unlike Forth, PostScript keeps track of the type of each value in the operand stack, and it will raise an error if an operator is called on an operand of the wrong type. PostScript also offers string, array, and dictionary data types so that direct access to memory is not needed or even supported.
A PostScript document consists of a sequence of pages, one for each execution of the //showpage// operator. PostScript provides a selection of operators which paint marks such as letters, shapes, or lines onto the page. The painted marks are collected in a buffer. When a //showpage// operator is invoked, the marks are rendered on the page and the buffer is cleared. If two marks overlap, the mark which was painted last will be completely visible, and the earlier mark will be partially or completely obscured as if it had been painted over.
The coordinate system used for determining the location of a painting mark places the origin in the lower left corner of the pa by default. The scale is 72 units per inch, with the x coordinate increasing in a rightward direction and the y coordinate increasing in an upward direction. The program may scale, rotate, or translate the coordinate system as pleased as long as the transformation is linear. The size of the page depends on the output device. Although the origin is by default the lower left corner of the physical page, some printers cannot print on the margin of the page; such printers cannot place a mark at or near the origin. The portions of marks which are outside the renderable window of the output device are ignored.
Some of the behavior of the painting operands is governed by the current graphics state. This reduces the number of operands that must be pushed onto the stack when calling a painting operator. The most commonly used parameters in the current graphics state are the current position, path, color, font, line width, coordinate transformation matrix. The //gsave// and //grestore// will push the current graphics state onto a stack or pop and restore the top value of the graphics state from the same stack.
- PostScript Development
[http://pages.cs.wisc.edu/~ghost/ Ghostscript] [http://www.quite.com/psalter/ PSAlter] [http://en.wikipedia.org/wiki/Preview_(software) Preview]
One can test PostScript code by sending it to a printer, but it is better to have an application which can render the PostScript on a screen. Ghostscript is free and runs on Unix and Linux. PSAlter can be purchased for Windows.
Mac OS X includes an application called //Preview// which renders PDF documents on the screen. It can also be used to render PostScript which it accomplishes by automatically converting the PostScript to PDF. //Preview// doesn’t provide informative error messages. The following PostScript shows how to use the //stopped// operator to catch an error and display the error name and the command that caused the error:
code %!PS /showobject { () exch 40 string cvs show } bind readonly def /showerror { /savederrorname $error /errorname get def /savedcommand $error /command get def initgraphics /Helvetica 20 selectfont 72 644 moveto (this error occurred: ) show 144 572 moveto currentdict /savederrorname get showobject 72 504 moveto (operator causing the error: ) show 144 432 moveto currentdict /savedcommand get showobject $error /newerror false put showpage } bind readonly def
{ /Helvetica-Bold 20 selectfont 72 576 moveto /h << 1 (one) 2 (two) >> def currentdict /h get 3 get show showpage
} stopped { showerror } if /code