Smalltalk中的简单类定义错误 [英] Simple class definition error in smalltalk

查看:99
本文介绍了Smalltalk中的简单类定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Smalltalk与 smalltalk/x-jv分支一起使用 .我有以下简单代码:

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.

我正在尝试使用smalltalk/x-jv的stc命令在命令行上运行它,但是它不起作用.错误如下:

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.

推荐答案

编辑-添加有关stcstx

恐怕您不能直接在Smalltalk/X(-jv分支)中使用GNU Smalltalk代码.同样很高兴看到您在Smalltalk问题系列中的最终目标是什么.

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.

对您来说重要的是,如果要构建应用程序,则应该使用所提供的IDE,这是因为Smalltalk被设计为可在IDE中运行.如果您要构建示例应用程序,甚至还可以指南适用于Smalltalk/X.当然,这并不意味着您无法从命令行启动脚本(Smalltalk/X在shell上功能强大).

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).

话虽这么说,其中有一个 Smalltalk/我在BitBucket上托管的 Sublime Text 3 的X高亮显示包文件.我主要是为Smalltalk及其嵌入式C高亮创建的.

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.

首先,您可能使用的是stx可执行文件,而不是stc. stcsmalltalk-to-C 编译器的快捷方式. stc生成 C代码,然后可以由 C编译器将其编译为目标文件,然后可以将其链接和最后一个 executable (以及其他smalltalk类和运行时).

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).

smalltalkstx是可以执行Smalltalk脚本或打开功能全面的IDE的启动器.如果您熟悉Java,请考虑将stc定义为javac,将smalltalkstx定义为java.

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.

您可以使用提供的名为smalltalk的启动器(bash脚本,用于* nix,用于Windows的批处理/powershell),最后使用stx.com,但提供了一些附加功能.

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.

使用smalltalk --help参见命令行选项.

首先,我将从一个简单的单线开始,您可以使用它:

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

在Windows上,如果您使用smalltalk,则会获得更多信息:

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.

现在让我们转到您的脚本问题

开始时,最好的方法是在IDE中创建类并对其执行fileOut.然后,您将看到.st文件应具有的正确结构.

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.

我已经为您创建了一个简单的文件script.st(这与从IDE上的fileOut类似):

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

输出将是:555

如果您要编写不带对象"的脚本(那么,所有内容在Smalltalk中都是对象,但这里没有定义类),则可以执行简单的transcript.st:

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.

再次将其执行为:smalltalk --execute transcript.st以得到相同的结果.

again execute it as: smalltalk --execute transcript.st to get identical result.

这篇关于Smalltalk中的简单类定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆