From c38d92615d504612f6bfb15f75a2c9ff78ee9c71 Mon Sep 17 00:00:00 2001 From: dos-reis Date: Sun, 10 Jun 2012 22:40:43 +0000 Subject: * interp/io.boot (%SourceLine): New. (%LoadUnit): Likewise. (readLoadUnitSource): Likewise. (%Reader): Remove input stream field. Include load unit and next lines fields. Adjust users. --- src/ChangeLog | 8 ++++++ src/interp/io.boot | 54 ++++++++++++++++++++++++++++++++----- src/interp/spad-parser.boot | 15 +++-------- src/interp/spad.lisp | 66 +++++++++++++++++++++------------------------ 4 files changed, 91 insertions(+), 52 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 9a17233e..9c8cc32b 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2012-06-10 Gabriel Dos Reis + + * interp/io.boot (%SourceLine): New. + (%LoadUnit): Likewise. + (readLoadUnitSource): Likewise. + (%Reader): Remove input stream field. Include load unit and next + lines fields. Adjust users. + 2012-06-10 Gabriel Dos Reis * interp/newaux.lisp (MAKEOP): Move to spad-parser.boot. diff --git a/src/interp/io.boot b/src/interp/io.boot index d8b638dd..9f9debbc 100644 --- a/src/interp/io.boot +++ b/src/interp/io.boot @@ -100,6 +100,37 @@ findString(s1,s2,k == 0) == or/[i for i in k..(#s2 - n1) | and/[stringChar(s1,j) = stringChar(s2,i+j) for j in 0..(n1-1)]] +--% +--% Source line +--% string and line number +--% +structure %SourceLine == + Record(str: %String,no: %Short) with + sourceLineString == (.str) + sourceLineNumber == (.no) + +makeSourceLine(s,n) == + mk%SourceLine(s,n) + +--% +--% Load Unit +--% +structure %LoadUnit == + Record(path: %Path,lines: %List %SourceLine) with + loadUnitPath == (.path) + loadUnitLines == (.lines) + +readLoadUnitSource path == + lines := + istream := inputTextFile path + or systemError ['"could not open file: ",:bright path] + try + [makeSourceLine(s,n) for n in 1.. while not eof? istream + | (s := readLine istream) and s ~= %nothing] + finally + closeStream istream + mk%LoadUnit(path,lines) + --% --% Line abstract datatype --% @@ -157,16 +188,27 @@ lineAdvanceChar! l == --% Reader --% structure %Reader == - Record(ins: %InputStream,est: %OutputStream, - lines: %List %String,sline: %Line,idx: %Short) with - readerInput == (.ins) - readerError == (.est) -- error output stream + Record(lu: %LoadUnit,est: %OutputStream, + lines: %List %String,sline: %Line,idx: %Short,nxt: %SourceLine) with + readerUnitSource == (.lu) -- current load unit source + readerError == (.est) -- error output stream readerPendingLines == (.lines) readerSourceLine == (.sline) -- current input line readerLineNumber == (.idx) -- current line number + readerNextLines == (.nxt) -- next source line to process + +makeReader(path,est) == + lu := readLoadUnitSource path + mk%Reader(lu,est,nil,makeLine(),0,loadUnitLines lu) + +readerEoi? rd == + readerNextLines rd = nil -makeReader(ist,ost == 'T) == - mk%Reader(ist,ost,nil,makeLine(),0) +readerReadLine rd == + readerNextLines rd is [x,:xs] => + readerNextLines(rd) := xs + sourceLineString x + %nothing ++ Add line `l' to the stack of pending lines. readerDeferLine(rd,l) == diff --git a/src/interp/spad-parser.boot b/src/interp/spad-parser.boot index 71f2d60a..92fe77f7 100644 --- a/src/interp/spad-parser.boot +++ b/src/interp/spad-parser.boot @@ -61,7 +61,7 @@ $preparseReportIfTrue := false INITIALIZE_-PREPARSE rd == readerLineNumber(rd) := 0 - $preparseLastLine := readLine readerInput rd + $preparseLastLine := readerReadLine rd --% @@ -147,7 +147,7 @@ preparseReadLine1 rs == line := first lines readerPendingLines(rs) := rest lines else - line := expandLeadingTabs readLine readerInput rs + line := expandLeadingTabs readerReadLine rs $preparseLastLine := line not string? line => [readerLineNumber rs] readerLineNumber(rs) := readerLineNumber rs + 1 @@ -1063,7 +1063,6 @@ translateSpad x == ++ ??? finally use the new parser everwhere. parseSpadFile sourceFile == $SPAD: local := true -- we are parsing Spad, - _*EOF_*: local := false -- end of current input? FILE_-CLOSED : local := false -- current stream closed? try -- noise to standard output @@ -1074,18 +1073,13 @@ parseSpadFile sourceFile == $InteractiveMode: local := false -- we need to restore the global input stream state after we -- finished messing with it. - IN_-STREAM: local := MAKE_-INSTREAM sourceFile - rd := makeReader(IN_-STREAM,$OutputStream) + rd := makeReader(sourceFile,$OutputStream) INIT_-BOOT_/SPAD_-READER rd - - -- If soureFile cannot be processed for whatever reasons - -- get out of here instead of being stuck later. - readerInput rd = nil => systemError '"cannot open input source file" INITIALIZE_-PREPARSE rd -- gather parse trees for all toplevel expressions in sourceFile. asts := [] - while not eof? readerInput rd repeat + while not readerEoi? rd repeat $lineStack: local := preparse rd $lineStack = nil => leave nil -- explicit end of input LINE: local := CDAR $lineStack @@ -1095,7 +1089,6 @@ parseSpadFile sourceFile == reverse! asts finally -- clean up the mess, and get out of here ioClear! rd - SHUT readerInput rd --% diff --git a/src/interp/spad.lisp b/src/interp/spad.lisp index 804a6f3f..d8dd240c 100644 --- a/src/interp/spad.lisp +++ b/src/interp/spad.lisp @@ -66,14 +66,13 @@ (defun spad (ifile &aux - (*comp370-apply* (function |printBackendDecl|)) - (*fileactq-apply* (function |printBackendDecl|)) - ($SPAD T) - (OPTIONLIST nil) - (*EOF* NIL) - (|$editFile| ifile) - rd in-stream out-stream) - (declare (special |$Echo| |$editFile| *comp370-apply* *EOF*)) + (*comp370-apply* (function |printBackendDecl|)) + (*fileactq-apply* (function |printBackendDecl|)) + ($SPAD T) + (OPTIONLIST nil) + (|$editFile| ifile) + rd out-stream) + (declare (special |$Echo| |$editFile| *comp370-apply*)) (setq |$InteractiveMode| nil) ;; only rebind |$InteractiveFrame| if compiling (progv (if (not |$InteractiveMode|) '(|$InteractiveFrame|)) @@ -83,33 +82,30 @@ `((FLUID . |true|) (|special| . ,(COPY-TREE |$InitialDomainsInScope|))) (|addBinding| '|$Information| NIL (|makeInitialModemapFrame|))))) - (unwind-protect - (progn - (setq in-stream (open ifile :direction :input)) - (setq rd (|makeReader| in-stream |$OutputStream|)) - (init-boot/spad-reader rd) - (initialize-preparse rd) - (setq out-stream |$OutputStream|) - (loop - (if (|eof?| in-stream) (return nil)) - (catch |$SpadReaderTag| - (progn - (setq |$lineStack| (|preparse| rd)) - (when (null |$lineStack|) - (return nil)) - (when |$lineStack| - (let ((LINE (cdar |$lineStack|))) - (declare (special LINE)) - (|parseNewExpr| rd) - (let ((parseout (|popStack1|)) ) - (when parseout - (let ((|$OutputStream| out-stream)) - (|translateSpad| parseout)) - (format out-stream "~&"))) - )))) - (|ioClear!| rd))) - (shut in-stream)) - T)) + (progn + (setq rd (|makeReader| ifile |$OutputStream|)) + (init-boot/spad-reader rd) + (initialize-preparse rd) + (setq out-stream |$OutputStream|) + (loop + (if (|readerEoi?| rd) (return nil)) + (catch |$SpadReaderTag| + (progn + (setq |$lineStack| (|preparse| rd)) + (when (null |$lineStack|) + (return nil)) + (when |$lineStack| + (let ((LINE (cdar |$lineStack|))) + (declare (special LINE)) + (|parseNewExpr| rd) + (let ((parseout (|popStack1|)) ) + (when parseout + (let ((|$OutputStream| out-stream)) + (|translateSpad| parseout)) + (format out-stream "~&"))) + )))) + (|ioClear!| rd))) + T)) (DEFUN INTEGER-BIT (N I) (LOGBITP I N)) -- cgit v1.2.3