0

I am new to smalltalk and i am having trouble figuring out why I am getting this parse error. The error is:

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

I trying to loop through the set named grid, which is a collection of dictionaries and in each dictionary i want to compare the values of each row and col to see if they match the values that were passed to the method. Right now I can only get it to accept one parameter to the method, if anyone could show me how to pass more than one parameter that would be awesome as well. Also I am using gnu smalltalk and gst to test this. The command I am running is as follows : (FileStream open: 'newanimal.st' mode: 'r') fileIn . !

Line 52 is:

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

Here is the rest of the code for reference.

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 [

    ]
]
user2793027
  • 83
  • 1
  • 3
  • 15

2 Answers2

1

I think you want to use '=' or '==' as your comparison operator, not ':=' which is the assignment.

Wouldn't hurt to terminate all statements with a period, just to avoid confusion on the parser's part.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
  • Ahh good call, but I have added the periods and changed the comparison operators but I am still getting the same error. However I think I have narrowed it down to the loops that I am using in the getLynxAtRow:col method and getRabbitAtRow:col – user2793027 Mar 26 '14 at 03:41
1

Apart from

  • checking the comparison operators and
  • the periods at the end of each statement,

as Chris Gerken mentions, you should

  • send blocks to ifTrue: and ifFalse:

    (1 < 2) ifTrue: [ "do something" ]

See: GNU Smalltalk Manual 6.6.1 Conditions and decision making

MartinW
  • 4,966
  • 2
  • 24
  • 60
  • Ok, in those blocks should it be all on the same line as well? Or could I put in a newline? Like: `(1<2) ifTrue: [\n "do something"\n ]` Edit: that does not look right in a comment – user2793027 Mar 26 '14 at 15:36
  • This is only a matter of style. You can do as you wish. – MartinW Mar 26 '14 at 15:46
  • *Blocks* means instances of the class BlockClosure. The square brackets are the literal notation to create such block closures. See [GNU Smalltalk Library Reference](http://www.gnu.org/software/smalltalk/manual-base/gst-base.html#BlockClosure) for what a BlockClosure is. – MartinW Mar 26 '14 at 15:54