/*
* parser.c --
*
- * Copyright 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
+ * Copyright 2011, 2012, 2013, 2014, 2015, 2016 Free Software Foundation, Inc.
* ------------------------------------------------------------------------
*
* GNU XBoard is free software: you can redistribute it and/or modify
#include "config.h"
#include <stdio.h>
+#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "common.h"
'S', 0213, 0342, 0,
'R', 0224, 0362, 0,
'B', 0212, 0160, 0,
- 'N', 0225, 0340, 0,
+ 'N', 0214, 0152, 0,
'L', 0215, 0201, 0,
- 'P', 0214, 0152, 0,
- 'r', 0224, 0156, 0,
- 'b', 0227, 0264, 0,
+ 'P', 0225, 0340, 0,
+ 'r', 0227, 0264, 0,
+ 'b', 0224, 0156, 0,
'p', 0202, 0306, 0,
'r', 0227, 0263, 0,
'+', 0220, 0254, 0,
int i;
if((*q & 0x80) == 0) return 0; // plain ASCII, refuse to parse
-fprintf(debugFP, "kanji %03o %03o\n", *q, q[1]);
if((**p & 0xC0) == 0x80) { // this is an illegal starting code in utf-8, so assume shift-JIS
for(i=start+JIS; kanjiTab[i]; i+=4) {
if(q[0] == kanjiTab[i+1] && q[1] == kanjiTab[i+2]) {
PromoSuffix (char **p)
{
char *start = *p;
+ if(**p == ' ') return NULLCHAR; // common case, test explicitly for speed
if(**p == 'e' && (Match("ep", p) || Match("e.p.", p))) { *p = start; return NULLCHAR; } // non-compliant e.p. suffix is no promoChar!
if(**p == '+' && IS_SHOGI(gameInfo.variant)) { (*p)++; return '+'; }
if(**p == '=' || (gameInfo.variant == VariantSChess) && **p == '/') (*p)++; // optional = (or / for Seirawan gating)
if(**p == '+') (*p)++, promoted++;
if(**p >= 'a' && **p <= 'z' && (*p)[1]== '@') piece =*(*p)++ + 'A' - 'a'; else
if(**p >= 'A' && **p <= 'Z') {
+ static char s[] = SUFFIXES;
+ char *q;
piece = *(*p)++; // Note we could test for 2-byte non-ascii names here
+ if(q = strchr(s, **p)) (*p)++, piece += 64*(q - s + 1);
if(**p == '/') slash = *(*p)++;
}
while(n < 4) {
else if(toY >= BOARD_HEIGHT || toY < 0) return ImpossibleMove; // vert off-board to-square
if(toX < BOARD_LEFT || toX >= BOARD_RGHT) return ImpossibleMove;
if(piece) {
- cl.pieceIn = CharToPiece(wom ? piece : ToLower(piece));
+ cl.pieceIn = CharToPiece(wom ? piece : piece + 'a' - 'A');
if(cl.pieceIn == EmptySquare) return ImpossibleMove; // non-existent piece
- if(promoted) cl.pieceIn = (ChessSquare) (CHUPROMOTED cl.pieceIn);
+ if(promoted) cl.pieceIn = (ChessSquare) (CHUPROMOTED(cl.pieceIn));
} else cl.pieceIn = EmptySquare;
if(separator == '@' || separator == '*') { // drop move. We only get here without from-square or promoted piece
fromY = DROP_RANK; fromX = cl.pieceIn;
}
if( type[1] != type[2] && // means fromY is of opposite type as ToX, or NOTHING
(type[0] == NOTHING || type[0] == type[2]) ) { // well formed
-
+ int suffix = 7;
fromX = (currentMoveString[0] = coord[0] + 'a') - AAA;
fromY = (currentMoveString[1] = coord[1] + '0') - ONE;
currentMoveString[4] = cl.promoCharIn = PromoSuffix(p);
currentMoveString[5] = NULLCHAR;
+ if(**p == 'x' && !cl.promoCharIn) { // other leg follows
+ char *q = *p;
+ int x = *++*p, y;
+ ++*p; y = Number(p);
+ if(**p == '-' || **p == 'x') { // 3-leg move!
+ currentMoveString[7] = (kill2X = toX) + AAA; // what we thought was to-square is in fact 1st kill-square of two
+ currentMoveString[8] = (kill2Y = toY) + ONE; // append it after 2nd kill-square
+ toX = x - AAA; // kludge alert: this will become 2nd kill square
+ toY = y + '0' - ONE;
+ suffix += 2;
+ } else *p = q; // 2-leg move, rewind to leave reading of 2nd leg to code below
+ }
if(!cl.promoCharIn && (**p == '-' || **p == 'x')) { // Lion-type multi-leg move
currentMoveString[5] = (killX = toX) + AAA; // what we thought was to-square is in fact kill-square
currentMoveString[6] = (killY = toY) + ONE; // append it as suffix behind long algebraic move
currentMoveString[4] = ';';
- currentMoveString[7] = NULLCHAR;
+ currentMoveString[suffix+1] = NULLCHAR;
// read new to-square (VERY non-robust! Assumes correct (non-alpha-rank) syntax, and messes up on errors)
toX = cl.ftIn = (currentMoveString[2] = *++*p) - AAA; ++*p;
toY = cl.rtIn = (currentMoveString[3] = Number(p) + '0') - ONE;
+ currentMoveString[suffix] = cl.promoCharIn = PromoSuffix(p);
}
if(type[0] != NOTHING && type[1] != NOTHING && type[3] != NOTHING) { // fully specified.
ChessSquare realPiece = boards[yyboardindex][fromY][fromX];
// Note that Disambiguate does not work for illegal moves, but flags them as impossible
if(piece) { // check if correct piece indicated
- if(PieceToChar(realPiece) == '~') realPiece = (ChessSquare) (DEMOTED realPiece);
+ if(PieceToChar(realPiece) == '~') realPiece = (ChessSquare) (DEMOTED(realPiece));
if(!(appData.icsActive && PieceToChar(realPiece) == '+') && // trust ICS if it moves promoted pieces
piece && realPiece != cl.pieceIn) return ImpossibleMove;
} else if(!separator && **p == '+') { // could be a protocol move, where bare '+' suffix means shogi-style promotion
- if(realPiece < (wom ? WhiteCannon : BlackCannon) && PieceToChar(PROMOTED realPiece) == '+') // seems to be that
+ if(realPiece < (wom ? WhiteCannon : BlackCannon) && PieceToChar(PROMOTED(realPiece)) == '+') // seems to be that
currentMoveString[4] = cl.promoCharIn = *(*p)++; // append promochar after all
}
result = LegalityTest(boards[yyboardindex], PosFlags(yyboardindex), fromY, fromX, toY, toX, cl.promoCharIn);
if (yyskipmoves) return (int) AmbiguousMove; /* not disambiguated */
if (wom) {
- rf = 0;
- rt = 0;
+ rf = castlingRank[0];
+ rt = castlingRank[0];
king = WhiteKing;
} else {
- rf = BOARD_HEIGHT-1;
- rt = BOARD_HEIGHT-1;
+ rf = castlingRank[3];
+ rt = castlingRank[3];
king = BlackKing;
}
ff = (BOARD_WIDTH-1)>>1; // this would be d-file
/* ICS wild castling */
ft = castlingType == 1 ? BOARD_LEFT+1 : (gameInfo.variant == VariantJanus ? BOARD_RGHT-2 : BOARD_RGHT-3);
} else {
+ char *q;
ff = BOARD_WIDTH>>1; // e-file
ft = castlingType == 1 ? BOARD_RGHT-2 : BOARD_LEFT+2;
+ if(pieceDesc[king] && (q = strchr(pieceDesc[king], 'O'))) { // redefined to non-default King stride
+ ft = (castlingType == 1 ? ff + atoi(q+1) : ff - atoi(q+1));
+ }
}
if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
if (wom) {