ReadEvalPrintLoop.st
author Claus Gittinger <cg@exept.de>
Thu, 07 Dec 2006 18:25:18 +0100
changeset 10254 e576d49220c2
parent 10253 48588709c555
child 10255 a50472b6354a
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

Object subclass:#ReadEvalPrintLoop
	instanceVariableNames:'inputStream outputStream errorStream compiler prompt
		doChunkFormat'
	classVariableNames:''
	poolDictionaries:''
	category:'* uncategorized *'
!

!ReadEvalPrintLoop class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!ReadEvalPrintLoop methodsFor:'accessing'!

compiler:something
    compiler := something.
!

doChunkFormat:something
    doChunkFormat := something.

    "Created: / 07-12-2006 / 18:24:04 / cg"
!

error:something
    errorStream := something.

    "Created: / 07-12-2006 / 17:33:39 / cg"
!

input:something
    inputStream := something.

    "Modified: / 07-12-2006 / 17:33:31 / cg"
!

output:something
    outputStream := something.

    "Created: / 07-12-2006 / 17:27:48 / cg"
!

prompt:something
    prompt := something.
! !

!ReadEvalPrintLoop methodsFor:'evaluation'!

readEvalPrintLoop
    "{ Pragma: +optSpace }"

    "simple read-eval-print loop for non-graphical Minitalk.
     If the chunkFormat-argument is true, chunks are read.
     Otherwise, lines up to an empty line (or EOF) are read."

    [
        |input output error lines chunk compilerClass|

        input := inputStream ? Stdin.
        output := outputStream ? Stdout.
        error := errorStream ? Stderr.
        compilerClass := compiler ? Compiler.

        prompt notNil ifTrue:[
            output nextPutAll:prompt.
        ].

        input atEnd ifTrue:[
            ^ self.
        ].

        (doChunkFormat ? true) ifTrue:[
            input skipSeparators.
            chunk := input nextChunk.
        ] ifFalse:[
            lines := OrderedCollection new.
            [
                |line|

                line := input nextLine.
                line notEmptyOrNil ifTrue:[
                    (line startsWith:'#') ifFalse:[
                        lines add:line.
                    ]
                ].
                line notEmptyOrNil.
            ] whileTrue.
            chunk := lines asStringWith:Character cr.
        ].

        chunk notEmptyOrNil ifTrue:[
            AbortAllOperationRequest handle:[:ex |
                error nextPutLine:('Evaluation aborted: ', ex description)
            ] do:[
                |value|

                value := (compilerClass new requestor:self) evaluate:chunk.
                value printOn:output.
                output cr.
            ].
        ].
    ] loop.

    "
     (ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop
    "

    "Created: / 07-12-2006 / 17:27:21 / cg"
! !

!ReadEvalPrintLoop class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/ReadEvalPrintLoop.st,v 1.5 2006-12-07 17:25:18 cg Exp $'
! !