I am trying to use smalltalk with smalltalk/x-jv branch. I have following simple code:
Object subclass: Myclass[
|mainval|
init [mainval := 555]
getmainval [^mainval]
]
gc := Myclass new.
gc init.
gc getmainval printNl.
I am trying to run it on command line with stc command of smalltalk/x-jv, but it it not working. Following is the error:
$ ./stc testsrc.st
testsrc.st, line 1: Error: syntax error in 'class definition' near "Myclass" (char/token=286 / 0x11e) (fileIn expression)
Where is the problem and how can it be solved? Thanks for your help.
Edit - Adding information about stc and stx
I'm afraid you can't use the GNU Smalltalk code directly within Smalltalk/X(-jv branch). Also it would be nice to see what is your final goal during the Smalltalk question series.
What is important for you to understand that Smalltalk has been designed to work within the IDE if you want to build an application you should use the IDE provided. If you want to build a sample application there is even guide for that for Smalltalk/X. That, of course, do not mean you are unable to start a script from command line (Smalltalk/X is powerfull at shell).
That being said there is a Smalltalk/X highlighting package file for Sublime Text 3 done by myself hosted at BitBucket. I have created it mainly for Smalltalk and its embedded C highlighting.
First you are probably using stx executable and not stc. stc is a shorcut for smalltalk-to-C compiler. stc produces a C code which can then be compiled by a C compiler into an object file which then can be linked with a final executable (together with other smalltalk classes and runtime).
smalltalk or stx is a launcher that can execute smalltalk scripts or open a full-blown IDE. If you're familiar with Java, think of stc as of javac and smalltalk or stx as of java.
You can use the launcher provided called smalltalk (a bash script for *nix and batch/powershell for windows), which is using the stx.com at the end, but providing some additional functionality.
Use smalltalk --help the see the commandline options.
First I will start with a simple one-liner which you can use:
stx.com -I --quick --eval "Transcript showCR: 'A message on stdout on Transcript'
A message on stdout on Transcript
on windows you if you use smalltalk you get more information:
smalltalk -I --quick --eval "Transcript showCR: 'A message on stdout on Transcript'
"[INFO] PowerShell detected: ->TRUE<-.
"[INFO] The latest latest_powershell_version found: 5.1.16299.1004."
"[INFO] With the runtime being: v4.0.30319."
VERBOSE: [INFO] Manual switch detected - configuration is ignored
VERBOSE: [INFO] Executing asynchronously command: C:\prg_sdk\stx8-jv_swing\build\stx\projects\smalltalk\stx.com -I
--quick --eval "Transcript showCR: 'A message on stdout on Transcript'" | Out-null
VERBOSE: A message on stdout on Transcript
VERBOSE:
VERBOSE: [INFO] Exiting from PowerShell with code 0
VERBOSE: [INFO] End. Exiting correctly.
Now lets move to your scripting question
At the beginning the best way is to create the class in IDE and do a fileOut of it. You will then see the correct structure the .st file should have.
I have create a simple file script.st for you (this is simlilar what you would get on a fileOut from IDE):
"{ NameSpace: Smalltalk }"
Object subclass:#MyClass
instanceVariableNames:'mainValue'
classVariableNames:''
poolDictionaries:''
category:''
!
!MyClass methodsFor:'accessing'!
mainValue
^ mainValue
!
mainValue: newValue
mainValue := newValue
! !
!MyClass methodsFor:'initialization & release'!
initialize
super initialize.
mainValue := 555.
! !
gc := MyClass new.
gc initialize.
Transcript showCR: gc mainValue.
How do you run such a sript?
smalltalk --execute script.st
The output will be: 555
If you want to script without "objects" (well everything is object in Smalltalk, but you don't define a class here) you can do simple transcript.st:
| mainValue |
mainValue := 555.
Transcript showCR: mainValue.
again execute it as: smalltalk --execute transcript.st to get identical result.
Related
This Python code shows how to call some process in Windows 10 and to send to it string commands, to read its string responses through stdin, stdout pipes of the process:
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import *
>>> p = Popen("c:/python38/python.exe", stdin=PIPE, stdout=PIPE)
>>> p.stdin.write(b"print(1+9)\n")
11
>>> p.communicate()
(b'10\r\n', None)
>>>
As you can see the python.exe process returned 10 as an answer to print(1+9). Now I want to do the same in Pharo (or Squeak): in Windows 10 OS - I suppose something similar, i.e. short, simple, understandable, really working.
I installed OSProcess, ProcessWrapper (they were missing in Pharo, also its strange that I got warning that they are not marked for Pharo 8.0 and were not checked to work in Pharo 8.0, but OK), and I tried ProcessWrapper, PipeableOSProcess (copy-pasted different snippets from the Web), etc - with zero success! The results were:
nothing happens, python.exe was not started
VM errors console was opened (white console in the bottom of the Pharo, which is controlled with F2 menu)
different exceptions
etc
Would somebody show me simple working example how to start a process and to to send it commands, read answers, then send again, and so on in some loop - I plan to have such communication in a detached thread and to use it as some service, because Pharo, Smalltalk in general is missing most bindings, so then I will use subprocess communication like in "good" old days...
I know how to call a command and to get its output:
out := LibC resultOfCommand: 'dir ', aDir.
but I am talking about another scenario: a communication with a running process interactively (for example, with SSH or similar like in the example above - python.exe).
PS. Maybe it's possible to do it with LibC #pipe:mode even?
Let me start with that the PipeableOsProcess is probably broken on Windows. I have tried it and it just opened a command line and nothing else (it does not freeze my Pharo 8). The whole OSProcess does not work correctly in my eyes.
So I took a shot at LibC which is supposed to not work with Windows.
I’m a module defining access to standard LibC. I’m available under Linux and OSX, but not under Windows for obvious reasons :)
Next is to say that Python's Windows support is probably much better than Pharo's.
The solution, which is more like a workaround using files, is to use LibC and #runCommand: (I tried to come up with a similar example as you had shown above):
| count command result outputFile errorFile |
count := 9+1. "The counting"
command := 'echo ', count asString. "command run at the command line"
outputFile := 'output'. "a file into which the output is redirected"
errorFile := 'error'. "a file where the error output is redirected "
result := LibC runCommand: command, "run the command "
' >', outputFile, "redirect the output to output file"
' 2>', errorFile.
"reading back the value from output file"
outputFile asFileReference contents lines.
"reading back the value from the error file - which is empty in this case"
errorFile asFileReference contents lines.
I need to automate a huge interactive Tcl program using Tcl expect.
As I realized, this territory is really dangerous, as I need to extend the already existing mass of code, but I can't rely on errors actually causing the program to fail with a positive exit code as I could in a regular script.
This means I have to think about every possible thing that could go wrong and "expect" it.
What I currently do is use a "die" procedure instead of raising an error in my own code, that automatically exits. But this kind of error condition can not be catched, and makes it hard to detect errors especially in code not written by me, since ultimately, most library routines will be error-based.
Since I have access to the program's Tcl shell, is it possible to enable fail-on-error?
EDIT:
I am using Tcl 8.3, which is a severe limitation in terms of available tools.
Examples of errors I'd like to automatically exit on:
% puts $a(2)
can't read "a(2)": no such element in array
while evaluating {puts $a(2)}
%
% blublabla
invalid command name "blublabla"
while evaluating blublabla
%
As well as any other error that makes a normal script terminate.
These can bubble up from 10 levels deep within procedure calls.
I also tried redefining the global error command, but not all errors that can occur in Tcl use it. For instance, the above "command not found" error did not go through my custom error procedure.
Since I have access to the program's Tcl shell, is it possible to
enable fail-on-error?
Let me try to summarize in my words: You want to exit from an interactive Tcl shell upon error, rather than having the prompt offered again?
Update
I am using Tcl 8.3, which is a severe limitation in terms of available
tools [...] only source patches to the C code.
As you seem to be deep down in that rabbit hole, why not add another source patch?
--- tclMain.c 2002-03-26 03:26:58.000000000 +0100
+++ tclMain.c.mrcalvin 2019-10-23 22:49:14.000000000 +0200
## -328,6 +328,7 ##
Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp));
Tcl_WriteChars(errChannel, "\n", 1);
}
+ Tcl_Exit(1);
} else if (tsdPtr->tty) {
resultPtr = Tcl_GetObjResult(interp);
Tcl_GetStringFromObj(resultPtr, &length);
This is untested, the Tcl 8.3.5 sources don't compile for me. But this section of Tcl's internal are comparable to current sources, tested using my Tcl 8.6 source installation.
For the records
With a stock shell (tclsh), this is a little fiddly, I am afraid. The following might work for you (though, I can imagine cases where this might fail you). The idea is
to intercept writes to stderr (this is to where an interactive shell redirects error messages, before returning to the prompt).
to discriminate between arbitrary writes to stderr and error cases, one can use the global variable ::errorInfo as a sentinel.
Step 1: Define a channel interceptor
oo::class create Bouncer {
method initialize {handle mode} {
if {$mode ne "write"} {error "can't handle reading"}
return {finalize initialize write}
}
method finalize {handle} {
# NOOP
}
method write {handle bytes} {
if {[info exists ::errorInfo]} {
# This is an actual error;
# 1) Print the message (as usual), but to stdout
fconfigure stdout -translation binary
puts stdout $bytes
# 2) Call on [exit] to quit the Tcl process
exit 1
} else {
# Non-error write to stderr, proceed as usual
return $bytes
}
}
}
Step 2: Register the interceptor for stderr in interactive shells
if {[info exists ::tcl_interactive]} {
chan push stderr [Bouncer new]
}
Once registered, this will make your interactive shell behave like so:
% puts stderr "Goes, as usual!"
Goes, as usual!
% error "Bye, bye"
Bye, bye
Some remarks
You need to be careful about the Bouncer's write method, the error message has already been massaged for the character encoding (therefore, the fconfigure call).
You might want to put this into a Tcl package or Tcl module, to load the bouncer using package req.
I could imagine that your program writes to stderr and the errorInfo variable happens to be set (as a left-over), this will trigger an unintended exit.
Basically, is there some kind of analogue of exit(-1) function in GNU Smalltalk 3.2.5? Or is there a way to configure it so that if it encounters an error during execution it'd return non-zero exit code? I want to be able to detect if gst executed the st code file successfully or if an error (syntax or runtime/exception) occurred.
Yes, it is possible using ObjectMemory quit: 0 or ObjectMemory quit: 1 etc. The source code for ObjectMemory quit:
ObjectMemory class >> quit: exitStatus [
"Quit the Smalltalk environment, passing the exitStatus integer
to the OS. Files are closed and other similar cleanups occur."
<category: 'builtins'>
<primitive: VMpr_ObjectMemory_quit>
SystemExceptions.WrongClass signalOn: exitStatus mustBe: SmallInteger
]
Searching for 'quit' in the source code will provide examples of it in action.
I'm trying to create an application using Seaside in GNU Smalltalk. I'm running GNU Smalltalk 3.2.5 under Linux (Fedora 17).
I have run and executed the simple counter test app just fine. In my application I need to dynamically create some classes and use them. However, gst-remote won't recognize them. Here's my test application (contents of test.st):
Seaside.WAComponent subclass: Test [
| foo |
Test class >> canBeRoot [ ^true ]
initialize [
super initialize.
Object subclass: #Foo instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'test class'.
foo := Foo new.
]
states [ ^{ self } ]
renderContentOn: html [
html heading: 'Foo'.
]
]
Test registerAsApplication: 'test'
On the "server" side, I run the remote server:
$ gst-remote -I seaside.im --server --start=Seaside
gst-remote server started.
On the application side (actually on the same PC, just a different terminal window), I run the following with the noted error:
$ gst-remote -f test.st
gst-remote: error at line 330: Undefined variable 'Foo' referenced.
I don't understand why Foo is undefined. From a gst interactive session, it works fine:
st> Object subclass: #Foo instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'test class'.
Foo
st> foo := Foo new.
a Foo
Why doesn't gst-remote allow this?
GST is not really a first-class citizen for Seaside development, so your best bet for getting this working is to do the first steps of Seaside development in a Pharo image, and then exporting that code to GST. You are likely to run into some issue where changes have been made to Seaside since 2011. Once your understanding of Seaside (in Pharo) is better, it will likely not be too much work to port to GST. If you do the exporting to GST in a CI setup, we'll be able to help you with concrete questions on the Seaside or Seaside-dev mailing lists.
Can I launch Squeak as a REPL (no GUI), where I can enter and evaluate Smalltalk expressions? I know the default image don't allow this. Is there any documentation on how to build a minimum image that can be accessed from a command-line shell?
Here is a (hackish) solution:
First, you need OSProcess, so run this in a Workspace:
Gofer new squeaksource:'OSProcess'; package:'OSProcess';load.
Next, put this in the file repl.st:
OSProcess thisOSProcess stdOut
nextPutAll: 'Welcome to the simple Smalltalk REPL';
nextPut: Character lf; nextPut: $>; flush.
[ |input|
[ input := OSProcess readFromStdIn.
input size > 0 ifTrue: [
OSProcess thisOSProcess stdOut
nextPutAll: ((Compiler evaluate: input) asString;
nextPut: Character lf; nextPut: $>; flush
]
] repeat.
]forkAt: (Processor userBackgroundPriority)
And last, run this command:
squeak -headless path/to/squeak.image /absolute/path/to/repl.st
You can now have fun with a Smalltalk REPL. Dont forget to type in the command:
Smalltalk snapshot:true andQuit:true
if you want to save your changes.
Now, onto the explanation of this solution:
OSProcess is a package that allows to run other processes, read from stdin, and write to stdout and stderr. You can access the stdout AttachableFileStream with OSProcess thisOSProcess (the current process, aka squeak).
Next, you run an infinite loop at userBackgroundPriority (to let other processes run). In this infinite loop, you use Compiler evaluate: to execute the input.
And you run this in a script with a headless image.
As of Pharo 2.0 (and 1.3/1.4 with the fix described below), there are no more hacks necessary. The following snippet will turn your vanilla Pharo image into a REPL server...
From https://gist.github.com/2604215:
"Works out of the box in Pharo 2.0. For prior versions (definitely works in 1.3 and 1.4), first file in https://gist.github.com/2602113"
| command |
[
command := FileStream stdin nextLine.
command ~= 'exit' ] whileTrue: [ | result |
result := Compiler evaluate: command.
FileStream stdout nextPutAll: result asString; lf ].
Smalltalk snapshot: false andQuit: true.
If you want the image to always be a REPL, put the code in a #startup: method; otherwise, pass the script at the command line when you want REPL mode, like:
"/path/to/vm" -headless "/path/to/Pharo-2.0.image" "/path/to/gistfile1.st"
Please visit:
http://map.squeak.org/package/2c3b916b-75e2-455b-b25d-eba1bbc94b84
and Run Smalltalk on server without GUI?
The project http://www.squeaksource.com/SecureSqueak.html includes a REPL package that may provide much of what you are looking for.