Re: A very productive day using FORTH
- From: "The Beez'" <hansoft@xxxxxxxxxxx>
- Date: 20 Mar 2007 02:10:35 -0700
On 19 mrt, 19:00, hel...@xxxxxxxxx wrote:
Problem was to analyze a big database of linguistic data, represented
in XML and custom SGML-like representations. <snip>
Has anyone else used FORTH for linguistic research tasks?
Not as much as for linguistic research. However, I did use Forth to
analyse an XML file. I treated the tags as words (and at times as
terminator). This approach was not only much faster than using
ordinary languages, but also very easy to maintain. For those
interested, I added the significant parts here. The file was generated
by Cisco Works.
Hans Bezemer
[needs lib/throw.4th]
\ structure to store data
struct
1 field Dirty \ flag: dirty buffer
17 field IPServer \ IP address of server
33 field AltServer \ Hostname of server
33 field AltSwitch \ Hostname of switch
17 field Poort \ Port of server
33 field PoortNaam \ Naam van poort
33 field Duplex \ Duplex description
17 field Mbit \ Speed in Mbit
33 field V-LAN \ Applicable VLAN
33 field MAC \ MAC address of server
end-struct /Patch
/Patch string Patch
today weekday value thisday \ calculate weekday
char < constant "<" \ ASCII code character "<"
char > constant ">" \ ASCII code character ">"
char / constant "/" \ ASCII code character "/"
char . constant "." \ ASCII code character "."
char , constant "," \ ASCII code character ","
char ' constant "'" \ ASCII code character "'"
250 constant /GO \ maximum number of statements/
GO
: is-bl bl = ; \ true if a space
: is-slash "/" = ; \ true if a slash
: is-dot "." = ; \ true if a dot
: is-no-digit is-digit 0= ; \ true if not a digit
: <tag "<" parse ; ( -- a n)
: tag> ">" parse ; ( -- a n)
: <tag> <tag 2drop tag> ; ( -- a n)
: chop 1 /string ; ( a n -- a+1 n-1)
: .' "'" emit ; ( --)
: ., "," emit space ; ( --)
: >> count .' type .' ., ; ( a n --)
: GO cr ." GO" cr ; ( --)
: ?GO dup /GO = if /GO - GO else cr 1+ then ;
: clean s>lower -leading -trailing ; ( a1 n1 -- a2 n2)
: >cell clean number ; ( a n1 -- n2)
: Numfield> is is-type split >cell ; ( a1 n1 xt -- a2 n2 n3)
: DateField> NumField> -rot chop ; ( a1 n1 xt -- n3 a2 n2)
: clear Patch /Patch 0 fill ; ( --)
: tag>? tag> compare if E.USER throw then ;
: field! <tag clean rot place tag>? ; ( a1 n1 a2 --)
: IP> ['] is-dot NumField> <# "." hold # # # #> Patch -> IPServer ;
( a1 n1 -- a2 n2 a3 n3 a4)
: /UTData ( --)
Patch -> AltServer count dup 0= if 2drop Patch -> PoortNaam count
then
dup >r Patch -> AltServer place r> \ use PortName if Hostname
empty
0<> Patch -> Dirty c@ and if \ if we have a dirty buffer,
write sql
." INSERT INTO Patch ( IPServer, AltServer, AltSwitch, Poort,
Duplex, Mbit, VLAN, MAC, WeekDay ) VALUES ( "
Patch -> IPServer >>
Patch -> AltServer >>
Patch -> AltSwitch >>
Patch -> Poort >>
Patch -> Duplex >>
Patch -> Mbit count type .,
Patch -> V-LAN >>
Patch -> MAC >>
thisday . [char] ) emit ?GO
then clear \ signal buffer is clean
;
\ check if data is recent
enough
: LastSeen ( --)
today jday <tag \ get julian date today
['] is-slash DateField> \ get year
['] is-slash DateField> \ get month
['] is-bl DateField> 2drop swap rot \ get day
jday - 30 > if clear then \ less than a month old?
s" /LastSeen" tag>? \ check closing tag
;
\ convert the IP address
: IPAddress ( --)
<tag IP> place chop IP> +place chop IP> +place chop
>cell <# # # # #> Patch -> IPServer +place s" /IPAddress" tag>?
;
\ convert the hostname
: HostName ( --)
['] is-dot is is-type \ check for a dot
<tag split clean Patch -> AltServer place 2drop
s" /HostName" tag>? \ check closing tag
;
: Port ( --)
['] is-no-digit is is-type \ setup for -SPLIT
<tag clean -split Patch -> Poort place
number <# # # #> Patch -> Poort +place
s" /Port" tag>? \ check closing tag
;
\ convert the following fields
: UTData true Patch -> Dirty c! ; ( --)
: PortSpeed <tag clean 1- Patch -> Mbit place s" /PortSpeed" tag>? ;
: MACAddress s" /MACAddress" Patch -> MAC field! ;
: DeviceName s" /DeviceName" Patch -> AltSwitch field! ;
: VLAN s" /VLAN" Patch -> V-LAN field! ;
: PortName s" /PortName" Patch -> PoortNaam field! ;
: PortDuplex s" /PortDuplex" Patch -> Duplex field! ;
.
- Follow-Ups:
- Re: A very productive day using FORTH
- From: helmwo
- Re: A very productive day using FORTH
- From: Leo Wong
- Re: A very productive day using FORTH
- References:
- A very productive day using FORTH
- From: helmwo
- A very productive day using FORTH
- Prev by Date: Re: Julian Noble's Funeral
- Next by Date: Re: What is Forth best at?
- Previous by thread: Re: A very productive day using FORTH
- Next by thread: Re: A very productive day using FORTH
- Index(es):
Relevant Pages
|
Loading