% Content-encoding: UTF-8 \documentclass[ngerman]{article} \usepackage[utf8]{inputenc} \usepackage{multicol,babel} \usepackage{xspace} \setcounter{secnumdepth}{0} \setcounter{tocdepth}{0} %\newcommand{\code}[1]{\texttt{#1}} %\newcommand{\ret}{\textsf{$<$ret$>$}\xspace} %\newcommand{\ret}{$\hookleftarrow$\xspace} \renewcommand{\reftextbefore}{auf der vorherigen Seite} \renewcommand{\reftextfacebefore}{auf der gegenüberliegenden Seite} \renewcommand{\reftextafter}{auf der nächsten Seite} \renewcommand{\reftextfaceafter}{auf der gegenüberliegenden Seite} \renewcommand{\figurename}{Abbildung} %\begin{document} \title{Fingerübungen in Forth: parsing} \author{Anton Ertl, Michael Kalus} \maketitle Neulich dachten wir darüber nach, wie die Datei \texttt{m168def.inc} mit Forth interpretiert werden könnte. Diese Datei wird vom Assembler AVRA benutzt, um allen Registern und besonderen Bits des atmega168 einen Namen zu geben. Für einen Crosscompiler müssten diese Konstanten übernommen werden. Hier ist ein Beispiel aus der Datei: \begin{quote} \begin{listing}{1} #ifndef _M168DEF_INC_ #define _M168DEF_INC_ #pragma partinc 0 ; ***** SPECIFY DEVICE *************************************************** .device ATmega168 #pragma AVRPART ADMIN PART_NAME ATmega168 .equ SIGNATURE_000 = 0x1e .equ SIGNATURE_001 = 0x94 .equ SIGNATURE_002 = 0x06 #pragma AVRPART CORE CORE_VERSION V2E ; ***** I/O REGISTER DEFINITIONS ***************************************** ; NOTE: ; Definitions marked "MEMORY MAPPED"are extended I/O ports ; and cannot be used with IN/OUT instructions .equ UDR0 = 0xc6 ; MEMORY MAPPED .equ UBRR0H = 0xc5 ; MEMORY MAPPED .equ UBRR0L = 0xc4 ; MEMORY MAPPED .equ UCSR0C = 0xc2 ; MEMORY MAPPED .equ UCSR0B = 0xc1 ; MEMORY MAPPED .equ UCSR0A = 0xc0 ; MEMORY MAPPED .equ TWAMR = 0xbd ; MEMORY MAPPED .equ TWCR = 0xbc ; MEMORY MAPPED .equ TWDR = 0xbb ; MEMORY MAPPED .equ TWAR = 0xba ; MEMORY MAPPED .. \end{listing} \end{quote} Um Fehler zu vermeiden, soll die Datei nicht verändert werden. Wie können also aus den \texttt{.equ}--Konstanten nun Forth--Konstanten werden? \section{Erster Versuch von Michael} So eine \texttt{.equ}--Zeile hat Ähnlichkeit mit einem Forth--Ausdruck: \begin{quote} \begin{listing}{28} : QWER 0x1122 ; \ ich bin ein Kommentar \end{listing} \end{quote} Drum versuchte er, die Zeilen zu interpretieren, indem er diese Ähnlichkeit abbildete. \begin{quote} \begin{listing}{29} vocabulary converter converter definitions : _; [compile] ; ; immediate : ; [compile] ; [compile] \ _; immediate : .equ [compile] : _; immediate : = _; immediate .equ QWER = 0x1122 ; this is a comment. \end{listing} \end{quote} Führt man nun \texttt{QWER} aus, findet man \texttt{0x1122} auf dem Stack und der Kommentar ist weg. Das geht also. Aber warum geht das? Und gibt es einen anderen Weg? \section{Antons Lösungen} Zunächst zeigte er, dass der Ansatz, die Ähnlichkeit zu verwenden, auch noch anders gemacht werden kann. \begin{quote} \begin{listing}{1} wordlist constant converter-wordlist get-current converter-wordlist set-current : ; postpone ; 0 parse 2drop ; immediate : .equ : ; : = ; immediate set-current converter-wordlist >order .equ QWER = 0x1122 ; this is a comment. previous \end{listing} \end{quote} Doch ist man damit nicht flexibel genug, auch andere Konstrukte aus der Datei zu übersetzen. Zeilenweise zu parsen, scheint da besser zu sein. Und man wird die Kommentare eleganter los. \begin{quote} \begin{listing}{10} : .equ : parse-name 2drop ( the "=" ) parse-name evaluate postpone ; ; : ; source nip >in ! ; .equ QWER = 0x1122 ; this is a comment. \end{listing} \end{quote} Auch diese Fassung sollte, so wie die vorherige, in eine eigene Wordlist gesetzt werden, weil man sonst keine Colon--Definitionen mehr beenden kann. Probiert's mal aus. Viel Spaß beim Knobeln. Quelle: comp.lang.forth \vfill \hfil \includegraphics[height=7cm]{2010-0203/Hand-Kunst} \includegraphics[height=7cm]{2010-0203/Hand-Mensch} \hfil \vfill \end{document}