initial checkin
authorClaus Gittinger <cg@exept.de>
Thu, 07 Dec 2006 17:52:09 +0100
changeset 10248 c00913f343f1
parent 10247 dadc23116a80
child 10249 4f4b81940a34
initial checkin
ReadEvalPrintLoop.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ReadEvalPrintLoop.st	Thu Dec 07 17:52:09 2006 +0100
@@ -0,0 +1,105 @@
+"{ Package: 'stx:libbasic' }"
+
+Object subclass:#ReadEvalPrintLoop
+	instanceVariableNames:'inputStream outputStream errorStream compiler prompt chunkFormat'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'* uncategorized *'
+!
+
+
+!ReadEvalPrintLoop methodsFor:'accessing'!
+
+chunkFormat:something
+    chunkFormat := something.
+!
+
+compiler:something
+    compiler := something.
+!
+
+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|
+
+        input := inputStream ? Stdin.
+        output := outputStream ? Stdout.
+        error := errorStream ? Stderr.
+
+        prompt notNil ifTrue:[
+            output nextPutAll:prompt.
+        ].
+
+        input atEnd ifTrue:[
+            ^ self.
+        ].
+
+        (chunkFormat ? 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 := (compiler new requestor:self) evaluate:chunk.
+                value printOn:output
+            ].
+        ].
+    ] loop.
+
+    "Created: / 07-12-2006 / 17:27:21 / cg"
+! !
+
+!ReadEvalPrintLoop class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/libbasic/ReadEvalPrintLoop.st,v 1.1 2006-12-07 16:52:09 cg Exp $'
+! !