3

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.

tukan
  • 17,050
  • 1
  • 20
  • 48
rnso
  • 23,686
  • 25
  • 112
  • 234
  • It would be nice if you upvote and/or accept the answer Smalltalk answers, because based on your code I'm not sure if you for example read at https://stackoverflow.com/questions/55931862/why-this-class-instance-variable-is-not-being-intialized – tukan May 02 '19 at 07:39

1 Answers1

3

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.

tukan
  • 17,050
  • 1
  • 20
  • 48
  • Great answer. Clarifies a lot. – rnso May 02 '19 at 08:18
  • @mso I'm glad you like it. Learning Smalltalk takes time, be patient :). – tukan May 02 '19 at 08:23
  • 1
    @tukan: maybe it's worth mentioning that `stc` is "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). OTOH, `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`. – J.V. May 02 '19 at 08:36
  • I was using command `stc` only and it was giving error. I downloaded file here from https://swing.fit.cvut.cz/projects/stx-jv , unzipped it and running executable files in `bin` folder. There are only `stc` and `stx` executable files in `bin` folder. There is no `smalltalk` command file in that folder or elsewhere in my system. How can I run my script file with `stx` command? – rnso May 02 '19 at 12:39
  • @rnso: When you download pre-packaged archive from SWING (so-called "toy archive"), the script is named `stx` When you build Smalltalk/X jv-branch yourselves, then it is `smalltalk`. I know, this is little confusing, the reasons are mainly historical. HTH – J.V. May 02 '19 at 12:47
  • Regarding my goal, I am trying to learn another language. I find smalltalk to be very well structured/organized with great integrated environment. However, lack of third party software for more complex tasks as machine learning and medical application seems to be its major limitation. – rnso May 02 '19 at 12:53
  • @mso Well there is always a catch. You can either program it in via `FFI` or via `C` (which can be challanging if you did not use it before). Other option is to use Java library as Smalltalk/X-jv branch is able to run Java code directly via VM. The issue there is the number of people actively using the language equals the number of libraries available. I love the elegance of Smalltalk compared to others. – tukan May 02 '19 at 13:30
  • @tukan : Do you mean that we can use java packages in Smalltalk/x-jv? That will be really interesting but I could not find any information regarding that on searching the net. – rnso May 02 '19 at 15:31
  • @rnso I think that deserves separate question, which would be best answered by J.V. .). – tukan May 02 '19 at 16:13
  • 1
    See: https://stackoverflow.com/questions/55956581/how-to-have-java-library-package-access-in-smalltalk – rnso May 02 '19 at 16:22