compudanzas/src/forth.gmo

99 lines
2.3 KiB
Plaintext

# forth
stack-based and lightweight programming language.
it uses the {postfix} notation.
# reading
## books
this book has a great pace and nice illustrations
=> https://www.forth.com/starting-forth starting forth book
## tutorials
=> http://www.softsynth.com/pforth/pf_tut.php pForth - portable Forth in 'C'
=> http://www.murphywong.net/hello/simple.htm Simple Forth
## articles
=> https://patorjk.com/programming/articles/forththoughts.htm Introduction to Thoughtful Programming and the Forth Philosophy By Michael Misamore
# trying out
=> https://txt.eli.li/pb/forth/ forth editor lifted from easy forth - by eli_oat
# some words
## running
these are some words for doing arithmetic with paces (min/km) and velocities (km/hr) relevant to running.
``` forth words
( running.forth )
: minseg>seg swap 60 * + ; ( min segs -- segs )
: seg>minseg 60 /mod swap ; ( segs -- min segs )
: horminseg>seg minseg>seg swap 3600 * + ; ( hr min segs -- segs )
: minseg/m>seg/1km >r minseg>seg 1000 swap r> */ ; ( min segs metros -- segs1km )
: seg/1km>vel 3600 swap / ; ( segs1km -- vel )
: paso>vel minseg>seg seg/1km>vel ; ( min segs -- vel )
: vel>seg/1km 3600 swap / ; ( vel -- segs1km )
: minseg- minseg>seg >r minseg>seg r> - seg>minseg ; ( min segs min segs -- min seg )
( print )
: .min 0 <# [CHAR] ' HOLD #S #> TYPE ; ( min -- )
: .segpad 0 <# [CHAR] " HOLD # # #> TYPE ; ( segs -- )
: .seg 0 <# [CHAR] " HOLD #S #> TYPE ; ( segs -- )
: .minseg 60 /mod dup 0> IF .min .segpad ELSE drop .seg THEN ; ( segs -- )
: .vel . ." km/hr " ;
: lista-velocidades cr 21 10 do i dup .vel vel>s1k .ms cr loop ;
```
### usage
in order to load them, you'd do:
```
include running.forth
```
for example, to convert a velocity to a pace, and print it:
``` input: 18, output: 3'20"
18 vel>seg/1k .minseg
output:
3'20" ok
```
to do the opposite operation:
``` input: 3 20, output: 18 km/hr
3 20 paso>vel .vel
output:
18 km/hr ok
```
to get the pace of a given segment, using minutes, seconds and distance in meters:
``` input: 1 03 300, output: 3'30"
( 1'03" in 300m )
1 03 300 minseg/m>seg/1k .minseg
output:
3'30" ok
```
to get the difference between two times in minutes, seconds:
``` input: 3 20 3 15, output: 0'05"
( 3'20" - 3'15" )
3 20 3 15 minseg- minseg>seg .minseg
output:
5" ok
```