help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] [RFC] Smalltalk scripting syntax


From: Paolo Bonzini
Subject: [Help-smalltalk] [RFC] Smalltalk scripting syntax
Date: Fri, 09 Mar 2007 09:00:27 +0100
User-agent: Thunderbird 1.5.0.10 (Macintosh/20070221)

Hi everybody,

in the next few months, Daniele Sciascia will work on the
implementation of a scripting syntax for GNU Smalltalk.
The aim of this is to provide a better programming experience
than is possible with the file-out (bang-separated chunks)
format.

This is an ambitious project, and it is best if no false
starts are made on the syntax.  Therefore, I am attaching
two examples (taken from RandomInt.st and Queens.st in the
gst distribution) converted to the new syntax.

(We still have no parser, so I'm not completely sure that
it will work exactly as I attach it, but it is enough to
give the idea).

Speak now if you wish. :-)

Paolo
Class name: Integer extends: RandomInteger [
    <comment: 'My instances are random integers'>
    <category: 'Examples-Useful'>

    "Define a class variable"
    Source := Random new.

    "Define class methods.  An alternative syntax for this is shown
     in the Queens example (both are acceptable)."
    Class name: RandomInteger class [
        new [
            <category: 'instance creation'>
            ^self error: 'Must use between:and:'
        ]

        between: low and: high [
            <category: 'instance creation'>
            | i range |

            (Source = nil) ifTrue: [ Source := Random new ].
            range := high - low + 1.
            i := (Source next * range) truncated.
            ^i + low
        ]
    ]
]

Class name: NullChessPiece extends: Object [
    <category: 'Examples-Classic'>

    do: aBlock [
        "Evaluate aBlock passing all the remaining solutions"
        <category: 'enumerating'>
        | result |
        [ result := self next. result notNil ] whileTrue: [
            aBlock value: result
        ]
    ]

    next [
        "Answer a solution, or nil if there aren't anymore"
        <category: 'enumerating'>
        ^self move
            ifTrue: [ self result ]
            ifFalse: [ nil ]
    ]

    result [
        "Answer all the queens' rows, up to and including the receiver"
        <category: 'enumerating'>
        ^OrderedCollection new
    ]

    move [
        "Move the queen so that it is not menaced, backtracking if necessary.
         Answer whether a position can be found.
         If the null queen is asked to advance, the search tree has been walked
         entirely - so return false."
        <category: 'solving'>
        ^false
    ]

    menaces: test [
        "Answer whether a queen is menaced in the given position by the queens
         up to and including the receiver. The null queen does not menace
         anything."
        <category: 'solving'>
        ^false
    ]
]

Class: ChessPiece extends: NullChessPiece [
    | row column neighbor rows |

    <category: 'Examples-Classic'>

    ChessPiece class >> test: side [
        <category: 'testing'>
        | line n |
        (line := String new: side * 2 + 1)
            atAll: (1 to: side * 2 + 1 by: 2) put: $|;
            atAll: (2 to: side * 2 + 1 by: 2) put: $_.

        n := 0.
        (self board: side) do: [ :result |
            n := n + 1.
            Transcript
                space;
                next: side * 2 - 1 put: $_;
                nl.

            result do: [:x |
                line at: x + x put: $*.
                Transcript nextPutAll: line; nl.
                line at: x + x put: $_.
            ].
            Transcript nl.
        ].
        Transcript nl.
        ^n
    ]

    ChessPiece class >> board: n [
        <category: 'instance creation'>

        "Answer a ChessPiece which will return results for a chessboard of
         side n"
        ^(1 to: n) inject: NullChessPiece new into: [ :neighbor :column |
            self new
                setColumn: column
                rows: n
                neighbor: neighbor
        ]
    ]

    setColumn: aNumber rows: n neighbor: aChessPiece [
        "Initialize the receiver to work on column aNumber of a chessboard of
         side n, having aChessPiece as a neighbor"
        <category: 'private'>

        column := aNumber.
        rows := n.
        neighbor := aChessPiece.
        row := 0.
    
        "Put all the queens but the last in some place where they are safe. The
         last will be requested by sending #next"
        self neighbor move.
        ^self
    ]

    advance [
        "Move the receiver one row further if possible, else backtrack and move
         to the first row. Answer whether there was a safe position for the
         neighbor (in the first case, the neighbor was already in a safe
         position, so answer true!)"
        <category: 'solving'>

        ^row = rows
            ifTrue: [ row := 1. self neighbor move ]
            ifFalse: [ row := row + 1. true ].
    ]

    row [
        <category: 'accessing'>
        ^row
    ]

    column [
        <category: 'accessing'>
        ^column
    ]

    neighbor [
        <category: 'accessing'>
        ^neighbor
    ]

    menaces: test [
        <category: 'inherited'>
        "Answer whether the receiver or any of the pieces above it menace the
         `test' piece if it stays where its #row and #column methods say.
         This method will test if the receiver itself menaces the tested
         piece and if not will delegate the choice to the neighbor."
        self subclassResponsibility
    ]

    move [
        <category: 'inherited'>
        "Here and in #advance is where the search really takes place.
         We advance the queen to the next cell; if the edge has been reached,
         #advance takes care of backtracking by sending #move to the neighbor
         (which in turn could backtrack).  If the queen is safe there, return
         true; else we advance the queen once more and check again.
         Sooner or later every queen will be aligned on the right edge and each
         one will be ask its neighbor to advance.  So the first queen will send
         #move to the NullChessPiece, the NullChessPiece will answer false, and
         all the invocations of #move will in turn answer false, terminating the
         search."
        [   self advance ifFalse: [ ^false ].
            self neighbor menaces: self
        ] whileTrue: [ ].
        ^true
    ]


    result [
        <category: 'inherited'>
        ^self neighbor result
            addLast: row;
            yourself
    ]
]


Class name: Queen extends: ChessPiece [
    <category: 'Examples-Classic'>

    menaces: test [
        "Answer whether the receiver or any of the pieces above it menace the
         `test' piece if it stays where its #row and #column methods say."
        <category: 'inherited'>

        | columnDifference rowDifference |
        columnDifference := (test column - self column) abs.
        rowDifference := (test row - self row) abs.

        rowDifference = 0                       ifTrue: [ ^true ].
        rowDifference = columnDifference        ifTrue: [ ^true ].

        ^self neighbor menaces: test
    ]
]

(Queen test: 8) printNl.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]