2 :ID: da7fff9b-0b67-4843-828a-52a404d7f401
5 #+TITLE: Fifth - language
6 #+AUTHOR: Svjatoslav Agejenko
11 Fifth uses a different character table and codes than ASCII (still
12 almost similar). I call it FSCII (Fifth Standard Code for Information
13 Interchange) for example space character is not 32 but 255 instead. I
14 plan to use mainly HEX numbers, and create new characters to represent
15 numeric values. So typical nemric characters "0123..." is treated
16 like ordinary letters.
19 | DEC | HEX | function |
20 |--------+-------+----------------------------------------|
21 | 0 - 15 | 0 - F | HEX numbers |
22 | 252 | FC | backspace |
23 | 253 | FD | tabulator (TAB) |
24 | 254 | FE | carriage return (CR) |
26 | else | | ordinary characters, same as in ASCII. |
28 ** Compilation & miscellaneous
31 First module, control is passed to on startup. Contains
32 initialization routines. Also it is the last core module.
33 All new modules on top of it comes as result of executing
34 external source files.
36 head <name> ( -- ) compiles new dictionary entry without specifying
40 : <name> ( -- ) creates new code module
41 ; ( -- ) ends module (immideate)
42 ex: : hello ." hi there" ;
44 const <name> ( n -- ) defines new constant.
45 ex: 2147483647 const max
47 :i <name> ( -- ) same as ":" but this module will be executed
48 immideately even in compile mode.
51 create <name> ( -- ) same as "head" , but specify module type as data.
52 ex: create LotoResults 5 , 13 , 52 , 12 , 11 , 3 ,
54 allot ( n -- ) allocate n bytes in dictionary.
55 ex: create MyArray 100 allot
57 " <string>" ( -- ) compile string and its size into core.
58 ex: create Mystring " This is it's contects"
60 str <name> <string>" ( -- ) just shorter way for defining strings.
61 ex: str Mystring This is it's contenc"
63 var <name> ( -- ) define new 32 bit variable.
66 ' <module> ( -- n ) return memory address of given entry.
69 forget <name> ( -- ) erases from RAM given entry and all entries what was
73 [ ( -- ) set interpret mode (immideate)
74 ] ( n -- ) set compile mode and compile top stack element
75 in as literal. Together [ .... ] cobination provides good
76 way to compute some values only once, at compile time,
77 rather than every time while program is running.
78 ex: : calculate - [ 4 MyConst1 + MyConst2 * ] ;
80 defer <name> ( -- ) creates new module, with jump instruction.
81 Later address where to jump can be modified by "is" command.
82 This provides method of foward referencing. So you can use
83 modules what not jet exist.
84 is ( address1 address2 -- ) address1 - where to jump, address2 -
85 address of module created by defer command.
87 : run dispver ." running ..." ;
89 : (dispver ." Version 9.99 " ;
90 ' (dispver ' dispver is
92 Now if I type "run" on the screen appears:
93 Version 9.99 running ...
95 asc <char> ( -- ) reads char ascii code and treats it as literal.
97 ex: : BreakLine 30 do asc - emit loop ;
99 : BreakLine 30 do 45 emit loop ;
101 dyninc ( handle -- ) execute code in dynamic memory handle.
102 automatically deallocates it when done.
104 include ( filenumber -- ) execute code in specified file.
106 words ( -- ) display existing blocks in core.
108 bye ( -- ) exit from Fifth
111 Read one byte from input stream.
114 Add one byte "c" to string located at "addr" and updates
118 Read input stream and store it to pad until it finds c .
119 It ignores all "c" bytes until it finds any non "c" byte.
122 input stream: """"This is test !"aoeu idh
123 result: This is test !
125 Is useful for breaking text lines into words.
128 So called safe "fkey". Reads data from input stream
129 but converts characters with ASCII codes: 9 13 10
132 str=str? ( adr1 adr2 -- result )
133 Compares string at "adr1" with string at "adr2", returns
134 true flag if they are equal or false if they are not.
139 Searches whole dictionary for word in "pad". If found,
140 returns it address, if not, returns 0.
143 Execute word located in "pad". Depending on "mode".
145 dta ( addr -- DataAddr )
146 Calculates address of dictionary entry data area, from
149 2num ( -- num result )
150 Attempt to convert string located in "pad" into numeric
151 value. If succeed returns number and true as result.
152 If not, returns whatever and false as result.
154 dadd ( addr length -- )
155 Add to dictionary data located at "addr", with specified
159 Act with number depending on "mode". When interpreting,
164 Add to dictionary data located at "addr"+1 , length is taken
171 Holds input stream parser operation mode.
176 Holds temprorary strings.
179 Pointer to free byte in memory, always at the end of the
180 dictionary. Each time when something is stored
181 by "c," command, pointer is incareased.
184 Pointer to last dictionary word. Each time when new word is
185 compiled or erased by "forget", this pointer is updated.
187 modulechk ( Dstr<filename> -- ) check if module is loaded, if not
190 ne ( entrydata entrytype -- ) Compile new dictionary entry.
191 It's name must be in "pad".
193 ** Conditionals & control flow
195 if ( flag -- ) (immideate)
196 "if 1.. else 2.. then" or
197 "if 1.. then" construction. Conditional execution.
198 Performs "1.." if "flag" was true,
199 elseway performs "2.." if exist. Execution continues after
201 ex: 1 if ." nonzero" else ." zero" then
203 >= ( n1 n2 -- result ) true if (n1 = n2) or (n1 > n2)
204 ex: 5 3 >= if ." first number is greater or equal" then
206 <= ( n1 n2 -- result ) true if (n1 = n2) or (n1 < n2)
207 = ( n1 n2 -- result ) true if n1 = n2
209 do ( count -- ) (immideate)
210 "do .. loop" construction. Performs ".." "count" times.
211 In every step "count" is decareased until it is 0.
212 ex: : test 5 do i .d loop ;
215 doexit ( -- ) exit from "do .. loop"
217 for ( count top -- ) (immideate)
218 "for .. loop" construction. Performs ".." (top - count) times.
219 In every step "count" is incareased until it reaches "top" .
220 ex: : test 4 10 for i .d loop ;
223 forexit ( -- ) exit from "for .. loop"
225 until ( -- ) (immideate)
226 "until .. loop" construction. Performs ".." until flag become
227 true. False by default. Top of return stack holds flag.
229 done ( -- ) exit from "until .. loop"
232 ** Disk & file access
234 diskload ( FromDisk ToMem amount -- )
235 Load specified abount of bytes from disk into memory.
237 disksave ( FromMem ToDisk amount -- )
238 save specified abount of bytes from memory into disk.
240 format ( -- ) Erase all files.
242 fsDfilesize@ ( handle -- size )
243 Return size of opened file.
245 fsDcurloc@ ( handle -- location )
246 Return current location in file.
248 fsDupdated@ ( handle -- updated? )
249 Return true if file was updated,
250 ie. write operations occured.
252 fssave ( FromMem DestFileHandle amount -- )
255 fsload ( SrcFileHandle ToMem amount -- )
258 fseof ( handle -- bytesLeft )
259 Return amount of bytes left till end of file.
260 Useful before read operation.
262 fsls ( -- ) List all files and lists (directories,folders)
265 fslsr ( -- ) Same as "fsls" but recursively scans also sub lists.
267 fscl ( DynStrHand -- )
270 fscreate ( DynStrHand -- DescPnt )
271 Create new file or list. Can create multiple lists at once.
273 "\listGAMES\listSTRATEGY\listSIMWORLD\5th-runme"
274 and only "\listGAMES\" already exist, then
275 "listSTRATEGY" and "listSIMWORLD" lists will be created,
276 and empty file "5th-runme" placed in there.
278 fsDsave ( DynHand<data> DynStrHand<filename> -- )
279 Create new file and save all data from dynamic memory
282 fsDload ( DynStr<SrcFileName> DynHand<DataDest> -- )
283 Load whole file into dynamic memory block.
285 fsDloadnew ( DynStr<SrcFileName> -- DynHand<DataDest> )
286 Load whole file into new dynamic memory block.
290 dynal ( size -- handle )
291 Allocate dynamic memory block and return it's handle.
294 Deallocate dynamic memory block.
296 dynp ( handle -- addr )
297 Returns pointer to memory where dynamic block
300 dyns ( handle -- size )
301 Returns size of dynamic block.
303 dynresize ( NewSize handle -- )
304 Nondestructively resize dynamic block.
306 dync@ ( addr handle )
307 Read one byte from dynamic block.
309 dync! ( byte addr dynhandle )
310 Write one byte to dynamic block.
313 Read 32 bit number from dynamic block.
314 Address will spacify, whitch number, not byte.
316 dyn! ( 32BitNum addr dynhandle )
317 Write 32 bit number to dynamic block.
318 Address will spacify, whitch number, not byte.
320 dyncon ( size "name" -- )
321 Allocate dynamic block with specified size, and
322 create constant honding its handle.
323 ex: 100 dyncon MyNewBlock
326 Write contenc of dynamic memory block to screen.
330 . ( n -- ) print number on screen
332 d. ( n -- ) print number on screen in decimal
334 ? ( addr -- ) print 32 bit value located at addr.
336 ." <string>" ( -- ) print string into screen. Immideately
338 ex: : greeting ." Hello, World" ;
340 tab. ( -- ) print tabulator
342 calccol ( b g r -- c ) calculate color what best matches given
343 Blue Green & Red values. Values must be in range 0 - 255.
345 imgalloc ( xsize ysize -- imgbuf ) allocate image buffer for
348 imgsize ( imgbuf -- ) print on the screen X & Y size of image
351 point ( x y imgbuf -- addr ) returns memory address for specified
354 pset ( color x y imgbuf -- ) set graphic point
356 boxf ( x1 x2 y1 y2 imgbuf color -- ) draw filled box
358 cls ( imgbuf -- ) clear image buffer
360 setpal ( b g r color -- ) set palette value for specified color.
361 values bust be in size 0 - 63.
363 putchar ( char color x y imgbuf -- ) put graphic character in
364 imagebuffer to specified (x & y) location.
366 scroll ( x y imgbuf -- ) scroll in imgbuf.
368 scrollf ( color x y screen -- ) scroll and fill empty space with
371 at! ( x y -- ) set cursor location
372 curc! ( color -- ) set text color
373 curb! ( solor -- ) set backround color
375 colnorm ( -- ) set text color to normal
376 colneg ( -- ) set text color to negative (selected)
378 dyntype ( dynhandle -- ) display contenc of dynamic memory on screen
379 fsdisp ( file -- ) clear screen, display file, and wait for key
381 type ( addr length -- )
382 Types on the screen string, from memory at addr and
386 Types on the screen string, from memory at "addr"+1
387 length is taken from "addr" .
390 Holds handle of screen buffer.
392 copyscreen ( SrcImgHandle DestImgHandle -- ) copy contenc of source
393 image to destination image. Source and destination images
396 ** Math, memory & stack manipulation
398 off ( n -- ) writes 0 to given address, good for zeroing variable.
400 on ( n -- ) writes -1 (true flag) to given address.
403 2dup ( n1 n2 -- n1 n2 n1 n2 )
406 neg ( n1 -- -n1 ) negotiate
407 bit@ ( n bit -- result ) return specified bit from n.
408 ex: 38 2 bit@ (result will be 1)
409 to32bit ( n1 n2 n3 n4 -- n32 ) treat 4 last stack elements as bytes
410 and unite them into 32 bit dword. Most significant byte
412 ex: 12 76 23 11 to32bit result: 186076172
414 to8bit ( n32 -- n1 n2 n3 n4 ) break 32 bit number into 4 bytes.
415 Useful if you need to send 32 bit numbers thru 8 bit COM
417 ex: 186076172 to8bit result: 12 76 23 11
419 mod ( n1 n2 -- reminder ) divide n1 by n2 and returns reminder.
420 ex: 12 5 mod result: 2
422 bound ( low n high -- n ) check if n is in given bounds,
423 if not then incarease/decarease it to match bounds.
424 ex: 5 80 15 bound result: 15
425 5 10 15 bound result: 10
426 5 -10 15 bound result: 5
428 bound? ( low n high -- result ) returns true if n is in the
431 tab ( col -- spaces) calculate amount of spaces to add
432 ta reach next tabulation from given column.
434 count ( addr -- addr+1 n )
435 Useful for returning bytes from constantly incareasing
436 address. Module "type" is nice example.
439 store one byte at memory specified by "h". And incarease
443 store 32 bit number at memory specified by "h". And
446 cmove ( addr1 addr2 n -- )
447 copy "n" amount of bytes from memory at "addr1" to memory
450 rnd ( limit -- result )
451 generates random number in range 0 to "limit"-1.
454 returns absolute value of "n"
456 ** Dynamic & static strings
457 Fifth supports both static and dynamic strings. Static strings must
458 have predefined space reserved, and string mustn't exceed this
459 length. They manipulation is faster. But they use more memory. Static
460 string memory address is used to refer to the string.
462 Dynamic strings can have at any time length form 0 to 0FFh, They take
463 up only memory they currently need. They are held in dynamic memory
464 blocks, so dynamic block handle is used to refer to this string.
466 Both types of strings are stored in the way, where first (0th) byte
467 holds current string length, following bytes are string itself.
476 Dstrlen ( handle -- length )
477 Return string length.
479 c+Dstr ( chr handle -- )
480 Add one byte to end of the string.
482 c+lDstr ( chr handle -- )
483 Add one byte to left side (beginning) of the string.
486 Write contec of string into screen.
488 Dstrsure ( size Dstr -- )
489 Makes sure that at least rquested
490 "size" (amount of characters) is allocated for given
493 Dstr2str ( handle address -- )
494 Copy dyamic string into static memory space.
496 str2Dstr ( address handle -- )
497 Copy static string into dyamic string.
499 Dstr+str ( Dstr addr -- )
500 Add contenc of dynamic string to static string.
502 D" any string" ( -- Dstr )
503 Moves specified string into dynamic string called "defDstr".
505 D> any_string ( -- Dstr )
506 Moves specified string into dynamic string called "defDstr".
507 Space marks end of string!
509 D>2 any_string ( -- Dstr )
510 Moves specified string into dynamic string called "defDstr2".
511 Space marks end of string!
513 Dstr+Dstr ( Dstr1 Dstr2 -- )
514 Adds "Dstr1" to "Dstr2" and places result into "Dstr2".
516 Dstrclear ( Dstr -- )
517 Clears contenc of dynamic string.
519 Dstr2Dstr ( Dstr1 Dstr2 -- )
520 Moves "Dstr1" to "Dstr2".
521 Dstr ( data" name -- )
522 Creates new dynamic string and moves specified data into it.
523 Then creates new constant with given "name" holding created
524 dynamic string handle.
526 ex: Dstr Hello, my name is Sven!" message \ creates it
527 message Dstr. \ tests it
529 Dstrlscan ( char Dstr -- loc )
530 Searches dynamic string for "char", from left to right,
531 returns first found "char" location in string, or 0,
534 Dstrrscan ( char Dstr -- loc )
535 Searches dynamic string for "char", from right to left,
536 returns first found "char" location in string, or 0,
539 Dstrlscane ( char Dstr -- loc )
540 Same as "Dstrlscan" buf returns string length+1 as location.
542 Dstrleft ( amo Dstr -- )
543 Only specified amount of characters from left remains
544 in dynamic string. ie. cut right part out.
546 Dstrright ( amo Dstr -- )
547 Only specified amount of characters from right remains
548 in dynamic string. ie. cut left part out.
550 Dstrcutl ( amo Dstr -- )
551 Cut specified amount of characters from left of dynamic
554 Dstrsp ( char Dstr1 Dstr2 -- )
555 Separate dynamic string in Dstr1 into two parts,
556 using "char" as separator. First part will be stored in
557 "Dstr2", second part in "Dstr1".
558 ex: asc \ \ ..separator
559 D> listF\listLIB\5TH_DRVMOUSE \ ..separate from
560 defDstr2 \ ..place result in
561 Dstrsp \ separation command
562 defDstr Dstr. \ will be: listLIB\5TH_DRVMOUSE
563 defDstr2 Dstr. \ will be: listF
566 Allocates empty dynamic string, and places it's handle
570 Reads dynamic string handle from given address and
571 deallocates (frees) it.
575 mystring1 Dv \ allocates string
579 mystring1 Df ; \ deallocates it again when no longer needed.