partial hw2 and lecturenotes
authorBernhard Urban <lewurm@gmail.com>
Tue, 1 Nov 2011 15:04:13 +0000 (16:04 +0100)
committerBernhard Urban <lewurm@gmail.com>
Tue, 1 Nov 2011 15:04:13 +0000 (16:04 +0100)
.gitignore [new file with mode: 0644]
2hw.fs [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..7c81947
--- /dev/null
@@ -0,0 +1 @@
+lol.out
diff --git a/2hw.fs b/2hw.fs
new file mode 100644 (file)
index 0000000..42c8bc5
--- /dev/null
+++ b/2hw.fs
@@ -0,0 +1,100 @@
+\ http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Tutorial.html
+\ all `ass*' words are `( -- )' if not stated otherwise.
+
+: rr ( -- ) s" 2hw.fs" included ;
+: :r rr ; \ ghci style
+
+
+\ ============================== 3.24 ========================================
+\ Assignment: Emit ( c -- ) types c as character (not a number). Implement
+\ type ( addr u -- ).
+
+: type ( addr u -- )
+       swap 1 chars - swap
+       0 u+do
+               char+ dup c@ emit
+       loop drop ;
+\ ============================== 3.27 ========================================
+\ Assignment: How could you modify copy-file so that it copies until a second
+\ line is matched? Can you write a program that extracts a section of a text
+\ file, given the line that starts and the line that terminates that section? 
+
+s" 1hw.fs" r/o open-file throw Value fd-in
+s" lol.out" w/o create-file throw Value fd-out
+512 Constant max-line
+Create line-buffer  max-line 2 + allot
+
+\ TODO
+: copy-file ( -- )
+       begin
+               line-buffer max-line fd-in read-line throw
+       while
+               line-buffer swap fd-out write-line throw
+       repeat ;
+
+\ ============================== 3.29 ========================================
+: map-array ( ... addr u xt -- ... )
+       \ executes xt ( ... x -- ... ) for every element of the array starting
+       \ at addr and containing u elements
+       { xt }
+       cells over + swap ?do
+               i @ xt execute
+       1 cells +loop ;
+
+\ ============================== 3.31 ========================================
+: defer
+       create ['] abort ,
+       does> @ execute ;
+
+: is ' >body ! ;
+
+\ aufruf:
+\ > defer lulz
+\ > lulz ( abort )
+\ > s" lulz" find-name 48 dump ( mitschrift, leet hax )
+\ > ' + is lulz
+\ > 2 3 lulz . ( 5 )
+\ > s" lulz" find-name 48 dump ( mitschrift, leet hax )
+
+\ ============================== 3.33 ========================================
+: my-2dup
+       postpone over postpone over ; immediate
+: foodup my-2dup ; \ see foodup
+
+\ ============================== 3.34 ========================================
+: ]L ] postpone literal ;
+
+\ ============================== 3.37 ========================================
+: >order { wid -- }
+       get-order wid swap 1+ set-order ;
+
+\ > order
+\ > forth-wordlist >order
+\ > order \ "ich schieb nochmal `Forth' rauf"
+
+\ ===== misc stuff
+
+\ \ wie mach ich eine "gegenseitige" rekursion?
+\ > defer x
+\ > : y x ;
+\ > : (x) y ;
+\ > ' (x) is x
+\ \ controlbla...
+\ > : foo
+\ >    begin x if y then x until ; \ das `then' macht backpatching !
+\ > see foo \ ist falsch, weil buggy `see' :/
+\ > simple-see foo
+
+\ \ major hax0r
+\ > hex \ HAX
+\ > : bar if x
+\ > [ .s ] \ jetzt wird zeug am stack vom compilezeit stuff angezeigt, w00t
+\ > else
+\ > [ .s ]
+\ > y then
+\ > [ .s ]
+\ > ;
+\ > simple-see bar
+\ \ `?branch' bedingter sprung, `branch' unbedingter sprung
+
+\ btw, `cs-roll' manipuliert den stack zur compilezeit... (?)