ReadEvalPrintLoop.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 28 Sep 2011 20:07:49 +0100
branchjv
changeset 17865 598963c6ff8e
parent 17846 24edc476ac18
child 17869 9610c6c94e71
permissions -rw-r--r--
Recommited from itself

"
 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 traceFlag timingFlag exitAction'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Support'
!

!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.
"
!

documentation
"
    A simple read-eval-print loop for non-GUI or stscript operation.
    A line starting with '?' shows the usage message.
    Lines starting with '#' are directives:
        #exit   - exit the rep-loop

"
! !

!ReadEvalPrintLoop methodsFor:'accessing'!

compiler:something
    compiler := something.
!

doChunkFormat
    ^ doChunkFormat ? true

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

doChunkFormat:something
    doChunkFormat := something.

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

error:something
    errorStream := something.

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

errorStream
    ^ errorStream ? Transcript ? Stderr

    "Created: / 07-12-2006 / 19:11:56 / cg"
!

input:something
    inputStream := something.

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

inputStream
    ^ inputStream ? Stdin

    "Created: / 07-12-2006 / 19:12:13 / cg"
!

output:something
    outputStream := something.

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

outputStream
    ^ outputStream ? Stdout

    "Created: / 07-12-2006 / 19:12:27 / cg"
!

prompt:something
    prompt := something.
! !

!ReadEvalPrintLoop methodsFor:'compiler interface-error handling'!

correctableError:message position:pos1 to:pos2 from:aCompiler
    "compiler notifies us of an error - ignore it"

    ^ false
!

correctableSelectorWarning:aString position:relPos to:relEndPos from:aCompiler
    "compiler notifies us of a warning - ignore it"

    ^ false
!

correctableWarning:message position:pos1 to:pos2 from:aCompiler
    "compiler notifies us of an error - ignore it"

    ^ false

    "Created: / 02-11-2010 / 13:29:22 / cg"
!

error:aString position:relPos to:relEndPos from:aCompiler
    "compiler notifies us of a warning - ignore it"

    ^ false
!

unusedVariableWarning:aString position:relPos to:relEndPos from:aCompiler
    "compiler notifies us of a warning - ignore it"

    ^ false
!

warning:aString position:relPos to:relEndPos from:aCompiler
    "compiler notifies us of a warning - ignore it"

    ^ self
! !

!ReadEvalPrintLoop methodsFor:'directives'!

cmd_clear:lineStream
    self cmd_setOrClear:lineStream to:false

    "Created: / 07-12-2006 / 19:04:50 / cg"
!

cmd_exit:lineStream
    exitAction value

    "Created: / 07-12-2006 / 18:55:46 / cg"
!

cmd_help:lineStream
    self errorStream
        nextPutAll:
'Everything entered up to an empty line is called a "chunk" and evaluated.
Lines starting with "#" are commands to the read-eval-print interpreter.

Valid commands are:
    #help ............... this text
    #exit ............... exit interpreter loop
    #use <package>....... use (load) a package
        stx:libwidg ........ GUI package
        stx:libtool ........ IDE tool package
    #show <what> ........ show info
        variables .......... interpreter variables
        processes .......... processes
        memory ............. memory usage
        flags .............. flags
    #set/clear <flag> ... set or clear a flag
        trace .............. tracing execution
        timing ............. timing execution

MiniDebugger shows its help with "?".
'

    "Created: / 07-12-2006 / 18:54:20 / cg"
!

cmd_set:lineStream
    self cmd_setOrClear:lineStream to:true

    "Modified: / 07-12-2006 / 19:04:46 / cg"
!

cmd_setOrClear:lineStream to:aBoolean
    |what|

    lineStream skipSeparators.
    what := lineStream nextAlphaNumericWord.
    (what startsWith:'tra') ifTrue:[
        traceFlag := aBoolean.
        ^ self.
    ].
    (what startsWith:'tim') ifTrue:[
        timingFlag := aBoolean.
        ^ self.
    ].

    self errorStream nextPutLine:'?? which flag ?'.

    "Modified: / 07-12-2006 / 19:13:34 / cg"
!

cmd_show:lineStream
    |what|

    lineStream skipSeparators.
    what := lineStream nextAlphaNumericWord.
    (what startsWith:'var') ifTrue:[
        ^ self.
    ].
    (what startsWith:'proc') ifTrue:[
        MiniDebugger basicNew showProcesses.
        ^ self.
    ].
    (what startsWith:'mem') ifTrue:[
        self errorStream 
            nextPutLine:('overall: ',(ObjectMemory bytesUsed // 1024) printString,' Kb');
            nextPutLine:('free   : ',(ObjectMemory freeSpace // 1024) printString,' Kb');
            nextPutLine:('minorGC: ',(ObjectMemory scavengeCount) printString);
            nextPutLine:('majorGC: ',(ObjectMemory garbageCollectCount) printString).
        ^ self.
    ].
    (what startsWith:'flag') ifTrue:[
        self errorStream 
            nextPutLine:('trace : ',traceFlag printString);
            nextPutLine:('timing: ',timingFlag printString).
        ^ self.
    ].

    self errorStream nextPutLine:'?? show what ?'.

    "Modified: / 07-12-2006 / 19:23:15 / cg"
!

cmd_use:lineStream
    |pkg|

    lineStream skipSeparators.
    pkg := lineStream upToEnd withoutSeparators.
    Smalltalk loadPackage:pkg

    "Created: / 07-12-2006 / 19:07:56 / cg"
!

directive:line
    |s cmd|

    s := line readStream.
    s next. "/ skip the hash
    s skipSeparators.

    cmd := s nextAlphaNumericWord.
    self 
        perform:('cmd_',cmd,':') asSymbol with:s 
        ifNotUnderstood:[   
            self errorStream  
                nextPutAll:'?? invalid command: ';
                nextPutAll:cmd;
                nextPutAll:'. Type "#help" for help.';
                cr.
        ].

    "Created: / 07-12-2006 / 18:49:17 / cg"
! !

!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."

    exitAction := [^ self].

    ControlInterrupt handle:[:ex |
        self errorStream nextPutLine:('Cought: ', ex description).
        ex restart.
    ] do:[
        [
            |input output error lines chunk compilerClass|

            "/ re-evaluate these in the loop, so they can be changed dynamically
            input := self inputStream.
            output := self outputStream.
            error := self errorStream.
            compilerClass := compiler ? Compiler.

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

            input atEnd ifTrue:[
                ^ self.
            ].

            self doChunkFormat ifTrue:[
                input skipSeparators.
                chunk := input nextChunk.
            ] ifFalse:[
                lines := OrderedCollection new.
                [
                    |line|

                    line := input nextLine.
                    line notEmptyOrNil ifTrue:[
                        line = '?' ifTrue:[
                            self cmd_help:nil.
                            prompt notNil ifTrue:[
                                error nextPutAll:prompt.
                            ].
                        ] ifFalse:[
                            (line startsWith:'#') ifTrue:[
                                self directive:line.
                                prompt notNil ifTrue:[
                                    error nextPutAll:prompt.
                                ].
                            ] ifFalse:[
                                lines add:line.
                            ]
                        ]
                    ].
                    line notEmptyOrNil.
                ] whileTrue.
                chunk := lines asStringWith:Character cr.
            ].

            chunk notEmptyOrNil ifTrue:[
                "abortAll is handled, but not asked for here!!"
                AbortAllOperationRequest handle:[:ex |
                    error nextPutLine:('Evaluation aborted: ', ex description)
                ] do:[ 
                    (Error, ControlInterrupt) handle:[:ex |
                        prompt isNil ifTrue:[
                            ex reject
                        ].
                        MiniDebugger enterWithMessage:(ex errorString) mayProceed:true.
                        ex mayProceed ifTrue:[
                            ex proceed.
                        ].
                        error nextPutLine:('Evaluation aborted: ', ex description).
                        ex return.
                    ] do:[
                        |value|

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

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

    "Created: / 07-12-2006 / 17:27:21 / cg"
    "Modified: / 09-02-2011 / 14:00:49 / cg"
! !

!ReadEvalPrintLoop class methodsFor:'documentation'!

version
    ^ '$Id: ReadEvalPrintLoop.st 10695 2011-09-28 19:07:49Z vranyj1 $'
!

version_CVS
    ^ '§Header: /cvs/stx/stx/libbasic/ReadEvalPrintLoop.st,v 1.28 2011/02/09 13:03:41 cg Exp §'
!

version_SVN
    ^ '$Id: ReadEvalPrintLoop.st 10695 2011-09-28 19:07:49Z vranyj1 $'
! !