簡體   English   中英

smalltalk中的簡單類定義錯誤

[英]Simple class definition error in smalltalk

我試圖使用smalltalk / x-jv分支 我有以下簡單的代碼:

Object subclass: Myclass[
    |mainval|
    init [mainval := 555]
    getmainval [^mainval]
]

gc := Myclass new.
gc init.
gc getmainval printNl.

我試圖在命令行上運行它與stc命令smalltalk / x-jv,但它無法正常工作。 以下是錯誤:

$ ./stc testsrc.st 
testsrc.st, line 1: Error: syntax error in 'class definition' near "Myclass" (char/token=286 / 0x11e) (fileIn expression)

問題出在哪里?如何解決? 謝謝你的幫助。

編輯 - 添加有關stcstx

我擔心你不能直接在Smalltalk / X(-jv分支)中使用GNU Smalltalk代碼。 同樣很高興看到Smalltalk問題系列中你的最終目標是什么。

重要的是要了解Smalltalk已設計為在IDE中工作,如果您要構建應使用IDE提供的應用程序。 如果您想構建一個示例應用程序,那么Smalltalk / X甚至可以為其提供指導 當然,這並不意味着您無法從命令行啟動腳本(Smalltalk / X在shell中是強大的)。

據說有一個由我自己在BitBucket上完成的Sublime Text 3Smalltalk / X高亮包文件。 我創建它主要是為Smalltalk及其嵌入式C突出顯示。

首先,您可能正在使用stx可執行文件,而不是stc stcsmalltalk-to-C 編譯器的縮寫。 stc生成一個C代碼 ,然后由C編譯器編譯成一個目標文件 ,然后該目標文件可以與最終的可執行文件 (以及其他smalltalk類和運行時)一起鏈接

smalltalkstx是一個可以執行smalltalk腳本或打開一個完整的IDE的啟動器。 如果你熟悉Java,認為stc作為javacsmalltalkstx作為java

您可以使用名為smalltalk的啟動程序(用於* nix的bash腳本和用於Windows的批處理/ powershell),它最后使用stx.com ,但提供了一些附加功能。

使用smalltalk --help查看命令行選項。

首先,我將從一個簡單的單行開始,您可以使用:

stx.com -I --quick --eval "Transcript showCR: 'A message on stdout on Transcript'
A message on stdout on Transcript

在Windows上,如果你使用smalltalk你會得到更多信息:

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.

現在讓我們轉到您的腳本問題

一開始,最好的方法是在IDE中創建類並執行它的fileOut。 然后,您將看到.st文件應具有的正確結構。

我已經為您創建了一個簡單的文件script.st (這與您從IDE上的fileOut獲得的內容相似):

"{ 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.

你是怎么經營這樣一個人的?

smalltalk --execute script.st

輸出將是: 555

如果你想要沒有“對象”的腳本(好吧一切都是Smalltalk中的對象,但是你沒有在這里定義一個類)你可以做簡單的transcript.st

| mainValue |

mainValue := 555.
Transcript showCR: mainValue.

再次執行它: smalltalk --execute transcript.st得到相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM