ReadEvalPrintLoop.st
changeset 20933 2a6274961920
parent 20926 238218aac7c4
child 20934 36e46558156d
equal deleted inserted replaced
20932:07814037c10c 20933:2a6274961920
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 2006 by eXept Software AG
     4  COPYRIGHT (c) 2006 by eXept Software AG
     3 	      All Rights Reserved
     5 	      All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
    55         2) interactive line mode. Chunks are any number of lines up to either an empty line or
    57         2) interactive line mode. Chunks are any number of lines up to either an empty line or
    56           a line ending in a period. This is more useful for an interactive REPL, where statements/expressions
    58           a line ending in a period. This is more useful for an interactive REPL, where statements/expressions
    57           are entered linewise by a user.
    59           are entered linewise by a user.
    58 
    60 
    59     The input can is switched to non-chunk format whenever a line with a '#' in the first column appears.
    61     The input can is switched to non-chunk format whenever a line with a '#' in the first column appears.
       
    62 
       
    63     Try it (but only if you have a console):
       
    64         Smalltalk readEvalPrintLoop
    60 
    65 
    61     [Author:]
    66     [Author:]
    62         Claus Gittinger
    67         Claus Gittinger
    63 "
    68 "
    64 ! !
    69 ! !
   291             ].    
   296             ].    
   292         ].    
   297         ].    
   293     ].
   298     ].
   294 
   299 
   295     [
   300     [
       
   301         |ok|
       
   302 
   296         tmpFile := Filename newTemporary.
   303         tmpFile := Filename newTemporary.
   297         tmpFile contents:code.
   304         tmpFile contents:code.
   298         modifiedTime := tmpFile modificationTime.
   305         modifiedTime := tmpFile modificationTime.
   299         
   306 
   300         OperatingSystem 
   307         ok := OperatingSystem 
   301             executeCommand:('%1 "%2" < /dev/tty' bindWith:editor with:tmpFile pathName)
   308                 executeCommand:('%1 "%2" < /dev/tty' bindWith:editor with:tmpFile pathName)
   302             inputFrom:Stdin 
   309                 inputFrom:Stdin 
   303             outputTo:Stdout 
   310                 outputTo:Stdout 
   304             errorTo:Stderr.
   311                 errorTo:Stderr
   305         
   312                 auxFrom:nil
   306         tmpFile modificationTime ~= modifiedTime ifTrue:[
   313                 environment:nil
       
   314                 inDirectory:nil
       
   315                 lineWise:false
       
   316                 newPgrp:false
       
   317                 showWindow:true
       
   318                 onError:[:status | false].
       
   319                 
       
   320         (ok and:[tmpFile modificationTime ~= modifiedTime]) ifTrue:[
   307             isNewClass ifTrue:[
   321             isNewClass ifTrue:[
   308                 Compiler evaluate:tmpFile contentsOfEntireFile.    
   322                 Compiler evaluate:tmpFile contentsOfEntireFile.    
   309             ] ifFalse:[
   323             ] ifFalse:[
   310                 editFullClass ifTrue:[
   324                 editFullClass ifTrue:[
   311                     tmpFile fileIn.
   325                     tmpFile fileIn.
   323     "
   337     "
   324      self new 
   338      self new 
   325         input:Stdin;
   339         input:Stdin;
   326         cmd_edit:'MyClass foo' readStream
   340         cmd_edit:'MyClass foo' readStream
   327     "
   341     "
       
   342 
       
   343     "Modified: / 08-11-2016 / 21:49:56 / cg"
   328 !
   344 !
   329 
   345 
   330 cmd_exit:lineStream
   346 cmd_exit:lineStream
   331     exitAction value
   347     exitAction value
   332 
   348 
   585                 self errorStream showCR:('Caught in directive: ', ex description).
   601                 self errorStream showCR:('Caught in directive: ', ex description).
   586                 ex suspendedContext fullPrintAll.
   602                 ex suspendedContext fullPrintAll.
   587             ] do:[    
   603             ] do:[    
   588                 ControlInterrupt handle:[:ex |
   604                 ControlInterrupt handle:[:ex |
   589                     self errorStream showCR:('Ignored in directive: ', ex description).
   605                     self errorStream showCR:('Ignored in directive: ', ex description).
       
   606                     ex reject. 
   590                     ex proceed. 
   607                     ex proceed. 
   591                 ] do:[    
   608                 ] do:[    
   592                     self
   609                     self
   593                         perform:('cmd_',cmd) asMutator with:s
   610                         perform:('cmd_',cmd) asMutator with:s
   594                         ifNotUnderstood:[
   611                         ifNotUnderstood:[
   600             ].
   617             ].
   601         ].
   618         ].
   602     ].
   619     ].
   603 
   620 
   604     "Created: / 07-12-2006 / 18:49:17 / cg"
   621     "Created: / 07-12-2006 / 18:49:17 / cg"
       
   622     "Modified: / 08-11-2016 / 21:59:16 / cg"
   605 !
   623 !
   606 
   624 
   607 showModules
   625 showModules
   608     |errStream printModule|
   626     |errStream printModule|
   609 
   627 
   815     ControlInterrupt handle:[:ex |
   833     ControlInterrupt handle:[:ex |
   816         self errorStream nextPutLine:('Caught: ', ex description).
   834         self errorStream nextPutLine:('Caught: ', ex description).
   817         self inputStream atEnd ifTrue:[
   835         self inputStream atEnd ifTrue:[
   818             ex return.
   836             ex return.
   819         ].    
   837         ].    
   820         ex restart.
   838         MiniDebugger enter.
       
   839         ex proceed.
       
   840         "/ ex restart.
   821     ] do:[
   841     ] do:[
   822         |input output error compilerClass|
   842         |input output error compilerClass|
   823 
   843 
   824         "/ re-evaluate these in the loop, so they can be changed dynamically
   844         "/ re-evaluate these in the loop, so they can be changed dynamically
   825         input := self inputStream.
   845         input := self inputStream.
   845             input signalAtEnd:true.
   865             input signalAtEnd:true.
   846             self
   866             self
   847                 basicReadEvalPrintLoopWithInput:input output:output error:error
   867                 basicReadEvalPrintLoopWithInput:input output:output error:error
   848                 compiler:compilerClass prompt:prompt print:(printFlag ? true).
   868                 compiler:compilerClass prompt:prompt print:(printFlag ? true).
   849         ]
   869         ]
   850     ]
   870     ].
   851 
   871     self errorStream nextPutLine:('done.').
   852     "
   872 
       
   873     "
       
   874      Stdin atEnd 
       
   875      Stdin clearEOF
       
   876      Smalltalk readEvalPrintLoop
   853      (ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop
   877      (ReadEvalPrintLoop new prompt:'>') readEvalPrintLoop
   854     "
   878     "
   855 
   879 
   856     "Created: / 07-12-2006 / 17:27:21 / cg"
   880     "Created: / 07-12-2006 / 17:27:21 / cg"
   857     "Modified: / 06-12-2011 / 15:29:03 / cg"
   881     "Modified (comment): / 08-11-2016 / 22:04:03 / cg"
   858 ! !
   882 ! !
   859 
   883 
   860 !ReadEvalPrintLoop methodsFor:'queries'!
   884 !ReadEvalPrintLoop methodsFor:'queries'!
   861 
   885 
   862 autoDefineVariables
   886 autoDefineVariables