Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #19890
| From | Rob Sciuk <rob@controlq.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | MiniForth sample code ... |
| Date | 2013-02-21 18:37 -0500 |
| Organization | albasani.net |
| Message-ID | <alpine.BSF.2.00.1302211834170.97189@yoko.controlq.com> (permalink) |
In lieu of documentation, the following will highlight some of the features of MiniForth, specifically, the C interface, and the string caching (save) system. This has been tested on RedHat (64bit), FreeBSD 8.x, and PC-BSD. Your mileage might (will) vary on Windows. Cheers, Rob. .( ======================================================= ) cr .( --- MiniForth's TRIVIAL C interface ---) cr .( --- sample code to demonstrate calling C routines from libc ---) cr .( --- or from a .dll or .so file [platform dependant] ---) cr .( --- at runtime. ---) cr .( ======================================================= ) cr cr : load dlopen constant ; ( <str> | NULL -- creates lib ptr ) : entry dlsym dup 0 == if dlerror type cr then ; ( <lib> <str> -- <ptr> looks up a symbol in library ) : code create , , does> dup @ swap 1 cells + @ native ; ( ptr n -- creates new forth word to call C code ) .( 0 load libc loads symbols from the current executable, and creates constant <libc>) cr .( <string> load loads the .dll/.so named by <string> ) cr cr 0 load _libc .( ======================================================= ) cr .( <lib> <string> entry --> looks up a symbol in the .dll/.so and returns its function addr. ) cr .( <n> 0 code uid --> creates a forth interface to a C library with 0 arguments on the stack. ) cr .( for example, lookup getuid, pass it 0 args and create a forth interface word. ) cr .( ======================================================= ) cr .( lets create some new words, uid and gid as follows: ... ) _libc " getuid" entry 0 code uid _libc " getgid" entry 0 code gid .( done! ) cr .( now we can simply execute uid and gid: ) cr cr ." user id: " uid . cr ." group id: " gid . cr cr .( ======================================================= ) cr .( MiniForth saves permanent string constants in a heap, and save will create a constant ptr) cr .( " string" save <name> ) cr .( eg: " /bin/ls -lh" save constant ls ) cr cr " /bin/ls -lh" save constant ls " /usr/bin/clear" save constant clr " vi ccode.rf" save constant edit_cmd _libc " calloc" entry 2 code allocate _libc " free" entry 1 code free _libc " system" entry 1 code shell : edit edit_cmd shell . ; .( ========================== ) cr .( try typing: clr shell . ) cr .( try typing: ls shell . ) cr .( try typing: edit ) cr .( ========================== ) cr
Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread
MiniForth sample code ... Rob Sciuk <rob@controlq.com> - 2013-02-21 18:37 -0500
csiph-web