Smalltalk解析错误

时间:2014-03-26 00:36:14

标签: parsing syntax smalltalk gnu-smalltalk

我是smalltalk的新手,我无法弄清楚为什么我得到这个解析错误。错误是:

/newanimal.st:52: parse error, expected ')'

我试图循环遍历名为grid的集合,这是一个字典集合,在每个字典中我想比较每行和col的值,看它们是否匹配传递给方法的值。现在我只能让它接受一个方法的参数,如果有人可以告诉我如何传递多个参数,这将是很棒的。另外我使用gnu smalltalk和gst来测试这个。我正在运行的命令如下:(FileStream open: 'newanimal.st' mode: 'r') fileIn . !

第52行是:

    (tempAnimal at: type := 'lynx') ifTrue: temp := tempAnimal at: row 

以下是其余代码供参考。

Object subclass: #simulation .
simulation instanceVariableNames: ' ' .
simulation class instanceVariableNames: '' .
simulation comment: 'This is the class that runs the simulation' .

simulation class extend [
    initialize [

    ]
    new [

    ]
    setup [

    ]
    step [

    ]
    status [

    ]
    rand [
        |num|
            num := Random new .
            num := (num nextValue) * 10 .
            num := num asInteger.
            ^ num .
    ]
]
simulation extend [

]

    Object subclass: #grid .
grid instanceVariableNames: ' ' .
grid class instanceVariableNames: ' grida ' .
grid comment: 'I represent the grid and animal locations' .

grid class extend [
    initialize [
        grida := Set new .
    ]
    new [ "Builds arrays of correct size"
        (grida = nil) ifTrue: self initialize . 
        ^ super new grida .
    ]
    getLynxesAtRow:col ["Returns number of lynxes in a grid cell"
        | tempAnimal temp count |
        count := 0 
        grid do: [ :each |
            tempAnimal := grid at: each .
            (tempAnimal at: type := 'lynx') ifTrue: temp := tempAnimal at: row 
            (temp := row) ifTrue: temp := tempAnimal at: col 
            (temp := col) ifTrue: count := count + 1 
            ].
        ^ count .
    ]
    getRabbitsAtRow:col ["Returns number of rabbits in a grid cell"
        | tempAnimal temp count |
        count := 0 
        grid do: [ :each |
            tempAnimal := grid at: each 
            (tempAnimal at: type := 'rabbit') ifTrue: temp := tempAnimal at: row 
            (temp := row) ifTrue: temp := tempAnimal at: col 
            (temp := col) ifTrue: count := count + 1 .
            ] 
        ^ count .
    ]
    placerow: animal [
        | row col |     
        row := simulation rand .
        col := simulation rand .
        animal add: 'row' -> row .
        animal add: 'col' -> col .
        ^ animal .
    ]
    removerow: col [
        | tempAnimal temp tempid |
        grid do: [ :each |
            tempAnimal := grid at: each
            tempid := tempAnimal at: id
            temp := tempAnimal at: row 
            (temp := row) ifTrue: temp := tempAnimal at: col 
            (temp := col) ifTrue: grid remove: each .
    ]
]


    Object subclass: #animal .
animal instanceVariableNames: ' '.
animal class instanceVariableNames: ' id type animals '.
animal comment: 'I am the class for all animals' .

animal class extend [
    initialize [
        aniamls := Dictionary new .
    ]
    create: type [
        (animals := nil) ifTrue: self initialize
        (type := 'rabbit') ifTrue: animals := rabbit new .
        (type := 'lynx') ifTrue: animals := lynx new .
        ^ animals .
    ]
    getid ["returns the animals unique id"
        | tempAnimal temp |
        tempAnimal := grid asArray at: id .
        temp := 
    ]
    getrow: id ["returns the animals grid row"
        | tempAnimal temp |
        grid do: [:each | 
            tempAnimal := grid at: each .
            (tempAnimal at: id := id) ifTrue: (temp:= tempAnimal at: row. ^ temp ) . ]

    ]
    getcol: id ["returns the animals grid col"
        | tempAnimal temp |
        grid do: [:each | 
            tempAnimal := grid at: each .
            (tempAnimal at: id := id) ifTrue: (temp:= tempAnimal at: col. ^ temp ) . ]

    ]
    getdirection: id ["returns the animals movement direction"
        | tempAnimal temp |
        grid do: [:each | 
            tempAnimal := grid at: each .
            (tempAnimal at: id := id) ifTrue: (temp:= tempAnimal at: direction. ^ temp ) . ]
    ]
    setdirection ["sets animals movement direction"
        | direction |
        direction := simulation rand .
        ^ direction .
    ]
]

animal subclass: #lynx
lynx instanceVariableNames: ' direction '.
lynx class instanceVariableNames: ' lynxdictionary '. 
lynx comment: 'I am the subclass of animal that is lynxs' .

lynx class extend [
    new [
        lynxdictionary := Dictionary new .
        lynxdictionary add: 'type' -> 'lynx'
        direction := animal setdirection .
        lynxdictionary add: 'direction' -> direction
        lynxdictionary := grid placerow:lynxdictionary .
        ^ lynxdictionary .
    ]
    act [
        | row col tempAniaml |
    ]
]

animal subclass: #rabbit
rabbit instanceVariableNames: ' direction '.
rabbit class instanceVariableNames: ' rabbitdictionary '.
rabbit comment: 'I am the subclass of animal this is rabbits'.

rabbit class extend [
    new [
        rabbitdictionary := Dictionary new .
        rabbitdictionary add: 'type' -> 'rabbit'
        direction := animal setdirection
        rabbitdictionary add: 'direction' -> direction
        rabbitdictionary := grid place:rabbitdictionary .
        ^ rabbitdictionary . 
    ]
    act [

    ]
]

2 个答案:

答案 0 :(得分:1)

我认为您想使用' ='或者' =='作为比较运算符,而不是':='这是作业。

使用句点终止所有语句不会有什么坏处,只是为了避免在解析器的部分混淆。

答案 1 :(得分:1)

除了

  • 检查比较运算符和
  • 每个陈述末尾的句号,
正如Chris Gerken所提到的,你应该

  • 将块发送到ifTrue:和ifFalse:

    (1< 2)ifTrue:["做点什么" ]

请参阅:GNU Smalltalk Manual 6.6.1 Conditions and decision making

相关问题