Simple class definition error in smalltalk

╄→гoц情女王★ 提交于 2019-12-23 17:10:42

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/55946086/simple-class-definition-error-in-smalltalk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!