git @ Cat's Eye Technologies yoob / master src / lang / YpsilaxState.java
master

Tree @master (Download .tar.gz)

YpsilaxState.java @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * tc.catseye.yoob.YpsilaxState -- Ypsilax for yoob
 * The source code in this file has been placed into the public domain.
 */

/*
 * A tc.catseye.yoob.YpsilaxState tries to implement the semantics of the
 * Ypsilax grid-rewriter, for running under the yoob framework.
 */

package tc.catseye.yoob.ypsilax;

import tc.catseye.yoob.Error;
import tc.catseye.yoob.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.Random;
import java.util.Iterator;


class Ypsilax implements Language {
    public String getName() {
        return "Ypsilax";
    }

    public int numPlayfields() {
        return 1;
    }

    public int numTapes() {
        return 0;
    }

    public boolean hasProgramText() {
        return false;
    }

    public boolean hasInput() {
        return false;
    }

    public boolean hasOutput() {
        return false;
    }

    public List<String> exampleProgramNames() {
        ArrayList<String> names = new ArrayList<String>();
        names.add("transform it");
        names.add("classic example");
        names.add("abracadabra");
        names.add("escaped");
        names.add("multisymbol pattern");
        names.add("grow tape");
        names.add("wildcard");
        //names.add("higher-order");
        names.add("self-modifying");
        return names;
    }

    public YpsilaxState loadExampleProgram(int index) {
        String[][] program = {
          {
            "DDDD",
            "",
            "(  )",
            " D* ",
            "",
            "DDDD",
          },
          {
            "(      )  (      )",
            "  #            #",
            "  # ###    ### #",
            "  #            #",
            "",
            "    ###   ###",
            "",
            "    #      #",
            "    #      #",
            "    #    ###",
          },
          {
            "(  )   (  )",
            " AB     BA",
            "ABRACADABRA",
          },
          {
            "*          ",
            "(  )   (  )",
            " AB     BA",
            "ABRACADABRA",
          },
          {
            "(    )",
            " ABBA",
            " ^^  ",
            "ABRACADABRA",
            "^^^^^^^^^^^",
          },
          {
            "(      )",
            " >  -> ",
            " >  -> ",
            " >  -> ",
            "",
            "---->",
            "---->",
            "---->",
          },
          {
            "(   ?)",
            " A?AX",
            "",
            "ABRACADABRA",
            "",
            "...........",
          },/*
          {
            "(       ?) ",
            "",
            "",
            " ?   ?",
            " ----(  )",
            "",
            "",
            "",
            "    -------------",
            "     ABRACADABRA",
            "",
            "",
            "     ABRACADABRA",
          },*/
          {
            "(X   X   )  (X   X   )",
            " (  )(  )    (  )(  )",
            "  AB  BA      BA  AB",
            "",
            "",
            "        (  )",
            "         AB",
            "",
            "",
            "     ABRACADABRA",
          },
        };
        YpsilaxState s = new YpsilaxState();
        s.playfield.load(program[index]);
        return s;
    }

    public YpsilaxState importFromText(String text) {
        YpsilaxState s = new YpsilaxState();
        s.playfield.load(text.split("\\r?\\n"));
        return s;
    }

    public List<String> getAvailableOptionNames() {
        ArrayList<String> names = new ArrayList<String>();
        return names;
    }

    private static final String[][] properties = {
        {"Author", "Chris Pressey"},
        {"Implementer", "Chris Pressey"},
        {"Implementation notes",
         "In this version, patterns detected in the playfield are always within the " +
         "current bounds of the playfield; in other words, even if the pattern contains " +
         "spaces or wildcards, it will not match the assumed-empty space surrounding " +
         "the defined part of the playfield."},
    };

    public String[][] getProperties() {
        return properties;
    }

}

class Rule extends OverlayPlayfield<CharacterElement> {
    private IntegerElement width, height;
    private CharacterElement wildcard;
    private static final IntegerElement TWO = new IntegerElement(2);

    public Rule(Playfield<CharacterElement> p, IntegerElement x, IntegerElement y,
                IntegerElement width, CharacterElement wildcard) {
        super(p, x, y, width, width.divide(TWO));
        this.width = width;
        this.height = width.divide(TWO);
        this.wildcard = wildcard;
    }

    public Rule(Playfield<CharacterElement> p, IntegerElement x, IntegerElement y,
                IntegerElement width, IntegerElement height, CharacterElement wildcard) {
        super(p, x, y, width, height);
        this.width = width;
        this.height = height;
        this.wildcard = wildcard;
    }

    public Rule createMatchSubject() {
        return new Rule(getPlayfield(), getOffsetX(), getOffsetY(),
                        getHeight(), getHeight(), getWildcard());
    }

    public IntegerElement getWidth() {
        return width;
    }

    public IntegerElement getHeight() {
        return height;
    }

    public CharacterElement getWildcard() {
        return wildcard;
    }

    // This should be a method on a Dumper object which takes a Codec.
    public String dump() {
        IntegerElement min_x = getMinX();
        IntegerElement min_y = getMinY();
        IntegerElement max_x = getMaxX();
        IntegerElement max_y = getMaxY();
        IntegerElement x = min_x;
        IntegerElement y = min_y;
        StringBuffer buf = new StringBuffer();

        //System.out.println("(" + getOffsetX() + "," + getOffsetY() + ")_(" + width + "x" + height + ")");
        while (y.compareTo(max_y) <= 0) {
            x = min_x;
            while (x.compareTo(max_x) <= 0) {
                CharacterElement e = get(x, y);
                buf.append(dumpElement(e));
                x = x.succ();
            }
            y = y.succ();
            buf.append("\n");
        }

        return buf.toString();
    }

    public String dumpElement(CharacterElement e) {
        return e.getName();
    }
}

class WildcardMatcher implements Matcher<CharacterElement,CharacterElement> {
    private CharacterElement wildcard;

    public WildcardMatcher(CharacterElement wildcard) {
        this.wildcard = wildcard;
    }

    public boolean match(CharacterElement sought, CharacterElement candidate) {
        if (wildcard != null && sought.getChar() == wildcard.getChar()) {
            return true;
        }
        return candidate.equals(sought);
    }
}

class PlayfieldMatcher<E extends Element> {
    public boolean isMatchAt(Playfield<E> haystack, Playfield<E> needle, Matcher<E,E> m, IntegerElement x, IntegerElement y) {
        IntegerElement width = needle.getMaxX().subtract(needle.getMinX()).succ();
        IntegerElement height = needle.getMaxY().subtract(needle.getMinY()).succ();
      
        if (x.pred().add(width).compareTo(haystack.getMaxX()) > 0 ||
            y.pred().add(height).compareTo(haystack.getMaxY()) > 0) {
            // exceeds the right or bottom edge, so, no
            return false;
        }
        IntegerElement cx, cy, dx, dy;
        for (cx = x, dx = needle.getMinX();
             dx.compareTo(needle.getMaxX()) <= 0;
             cx = cx.succ(), dx = dx.succ()) {
            for (cy = y, dy = needle.getMinY();
                 dy.compareTo(needle.getMinY()) <= 0;
                 cy = cy.succ(), dy = dy.succ()) {
                E soughtElem = needle.get(dx, dy);
                E foundElem = haystack.get(cx, cy);
                //System.out.printf("sought (%s,%s): '%s', found (%s,%s): '%s'\n",
                //     dx, dy, soughtElem.getName(),
                //     cx, cy, foundElem.getName());
                if (m.match(soughtElem, foundElem)) {
                    // add something to the result-list
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public Set<Position> getAllMatches(Playfield<E> haystack, Playfield<E> needle, Matcher<E,E> matcher) {
        Set<Position> results = new HashSet<Position>();

        IntegerElement haystackWidth = haystack.getMaxX().subtract(haystack.getMinX()).succ();
        IntegerElement haystackHeight = haystack.getMaxY().subtract(haystack.getMinY()).succ();
        IntegerElement needleWidth = needle.getMaxX().subtract(needle.getMinX()).succ();
        IntegerElement needleHeight = needle.getMaxY().subtract(needle.getMinY()).succ();

        if (needleWidth.compareTo(haystackWidth) > 0 || needleHeight.compareTo(haystackHeight) > 0) {
            // thing being sought is larger than the thing seeking in, so, no
            return results;
        }

        IntegerElement xSpan = haystackWidth.subtract(needleWidth).succ();
        IntegerElement ySpan = haystackHeight.subtract(needleHeight).succ();

        IntegerElement x, y;
        for (x = IntegerElement.ZERO; x.compareTo(xSpan) < 0; x = x.succ()) {
            for (y = IntegerElement.ZERO; y.compareTo(ySpan) < 0; y = y.succ()) {
                if (isMatchAt(haystack, needle, matcher, x, y)) {
                    results.add(new Position(x, y));
                } else {
                }
            }
        }
        return results;
    }
}

class YpsilaxPlayfield extends BasicPlayfield<CharacterElement> {
    public YpsilaxPlayfield() {
        super(new CharacterElement(' '));
    }

    public YpsilaxPlayfield clone() {
        YpsilaxPlayfield c = new YpsilaxPlayfield();
        c.copyBackingStoreFrom(this);
        return c;
    }

    public List<Position> match(YpsilaxPlayfield pattern) {
        return null;
    }

    public List<Rule> findAllRules() {
        IntegerElement x, y;
        ArrayList<Rule> rules = new ArrayList<Rule>();

        for (x = (IntegerElement)getMinX(); x.compareTo(getMaxX()) <= 0; x = x.succ()) {
            for (y = (IntegerElement)getMinY(); y.compareTo(getMaxY()) <= 0; y = y.succ()) {
                boolean escaped = (!y.isZero() && get(x, y.pred()).getChar() != ' ');
                if (escaped) continue;
                if (get(x, y).getChar() == '(') {
                    //System.out.println("Found a rule start at " + x + ", " + y);
                    IntegerElement x2 = x;
                    CharacterElement g = get(x2, y);
                    while (g.getChar() != ')' && x2.compareTo(getMaxX()) <= 0) {
                        x2 = x2.succ();
                        g = get(x2, y);
                    }
                    if (g.getChar() != ')') continue;
                    //System.out.println("Found a rule from " + x + "," + y + " to " + x2);
                    CharacterElement w = get(x2.pred(), y);
                    if (w.getChar() == ' ') w = null;
                    Rule r = new Rule(this, x.succ(), y.succ(), (x2.subtract(x)).pred(), w);
                    rules.add(r);
                }
            }
        }

        return rules;
    }

    public List<Position> findAllRuleMatches(Rule r) {
        //System.out.printf("%s", r.dump());
        WildcardMatcher matcher = new WildcardMatcher(r.getWildcard());
        PlayfieldMatcher<CharacterElement> pm = new PlayfieldMatcher<CharacterElement>();
        List<Position> positions = new ArrayList<Position>();
        // We only want to match the LEFT side of the rule, so we do this:
        Rule matchSubject = r.createMatchSubject();
        for (Position p : pm.getAllMatches(this, matchSubject, matcher)) {
            // System.out.println("CONSIDERING (" + p.getX() + "," + p.getY() + ")");
            // Discard all the ones that are at or above the rule
            if (p.getY().compareTo(r.getOffsetY().add(r.getHeight())) >= 0) {
                //System.out.println("(" + p.getX() + "," + p.getY() + ")..." + r.getOffsetY());
                positions.add(p);
            }
        }
        //System.out.println("---");
        return positions;
    }

    /*
     * We assume a match was made at the given position.
     */
    public void applyRule(Rule r, Position p) {
        IntegerElement i, j;

        CharacterElement wildcard = r.getWildcard();
        //System.out.printf("applying at %s: %s", p.toString(), r.dump());
        /* Note that r.getHeight() == width/2 ... */
        for(i = IntegerElement.ZERO; i.compareTo(r.getHeight()) < 0; i = i.succ()) {
            for(j = IntegerElement.ZERO; j.compareTo(r.getHeight()) < 0; j = j.succ()) {
                CharacterElement replacement = r.get(i.add(r.getHeight()), j);
                IntegerElement destX = p.getX().add(i);
                IntegerElement destY = p.getY().add(j);
                //System.out.printf("(%s,%s): [%s] '%s' -> (%s,%s)\n", i, j, wildcard, replacement.getChar(), destX, destY);
                if (wildcard == null || wildcard.getChar() != replacement.getChar()) {
                    set(destX, destY, replacement);
                }
            }
        }
    }
}

public class YpsilaxState implements tc.catseye.yoob.State {
    protected YpsilaxPlayfield playfield;
    protected BasicPlayfieldView view;
    private Random rand;
    private static final Ypsilax language = new Ypsilax();
    private boolean halted = false;

    public YpsilaxState() {
        playfield = new YpsilaxPlayfield();
        view = new BasicPlayfieldView();
        rand = new Random();
    }
    
    public YpsilaxState clone() {
        YpsilaxState c = new YpsilaxState();
        c.playfield = this.playfield.clone();
        return c;
    }

    public Language getLanguage() {
        return language;
    }

    public List<Error> step(World world) {
        ArrayList<Error> errors = new ArrayList<Error>();
        List<Rule> rules = playfield.findAllRules();
        if (rules.size() == 0) {
            // no sense continuing.  halt.
            halted = true;
            return errors;
        }
        //System.out.println("rules: " + rules.size());
        Rule r = rules.get(rand.nextInt(rules.size()));
        List<Position> positions = playfield.findAllRuleMatches(r);
        //System.out.println("matches: " + positions.size());
        if (positions.size() > 0) {
            Position p = positions.get(rand.nextInt(positions.size()));
            playfield.applyRule(r, p);
        }
        return errors;
    }

    public Playfield getPlayfield(int index) {
        return playfield;
    }

    public Tape getTape(int index) {
        return null;
    }

    public String getProgramText() {
        return "";
    }

    public int getProgramPosition() {
        return 0;
    }

    public List<Error> setProgramText(String text) {
        ArrayList<Error> errors = new ArrayList<Error>();
        return errors;
    }

    public View getPlayfieldView(int index) {
        return view;
    }

    public View getTapeView(int index) {
        return null;
    }

    public void setOption(String name, boolean value) {
    }

    public String exportToText() {
        return playfield.dump();
    }

    public boolean needsInput() {
        return false;
    }

    public boolean hasHalted() {
        return halted;
    }
}