Commit 9c314204 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Implement the lexer

parent faea2fea
......@@ -41,6 +41,7 @@ LIBTAROT_VERSION_INFO=0:0:0
%reldir%/model/libtarot-model.la \
%reldir%/ai/libtarot-ai.la \
%reldir%/generator/libtarot-generator.la \
%reldir%/lexer/libtarot-lexer.la \
lib-library/libgnulibrary.la
pkgconfig_DATA += %reldir%/libtarot.pc
......@@ -54,4 +55,5 @@ include %reldir%/features/Makefile.am
include %reldir%/model/Makefile.am
include %reldir%/ai/Makefile.am
include %reldir%/generator/Makefile.am
include %reldir%/lexer/Makefile.am
include %reldir%/scm/Makefile.am
......@@ -372,7 +372,8 @@ ad_hoc_generalize (AdHocGame * game, size_t n_players, int with_call,
return 1;
}
typedef struct {
typedef struct
{
TarotGameEvent base;
unsigned int buffer[78];
} TarotGameEvent78;
......@@ -450,7 +451,7 @@ game_with_call (const TarotGameHandle * game)
}
static inline TarotHand
game_create_hand (size_t n_max, size_t *n, TarotCard *buffer)
game_create_hand (size_t n_max, size_t * n, TarotCard * buffer)
{
TarotHand ret;
ret.n_max = n_max;
......
......@@ -28,7 +28,7 @@
static inline size_t
event_sizeof (size_t n)
{
return (sizeof TarotGameEvent) + event_data_sizeof (n);
return sizeof (TarotGameEvent) + n * sizeof (unsigned int);
}
static inline TarotGameEventT
......@@ -41,7 +41,7 @@ static inline TarotGameEvent *
event_init (size_t n, void *mem)
{
TarotGameEvent *ret = mem;
char *pointer = (char *) (ret + 1);
void *pointer = (void *) (ret + 1);
ret->t = TAROT_SETUP_EVENT;
ret->u.setup.n_players = 4;
ret->u.setup.with_call = 0;
......@@ -99,7 +99,7 @@ event_set_setup (TarotGameEvent * event, size_t n_players, int with_call)
}
static inline TarotHand
create_hand (size_t n_max, size_t *n, TarotCard *buffer)
create_hand (size_t n_max, size_t * n, TarotCard * buffer)
{
TarotHand ret;
ret.n_max = n_max;
......@@ -196,12 +196,12 @@ event_set_deal_all_random (TarotGameEvent * event, size_t n_players,
yarrow256_random (&generator, sizeof (unsigned long), random_buffer);
memcpy (&random_number, random_buffer, sizeof (unsigned long));
if (1)
{
unsigned int pos = random_number % (i + 1);
TarotPlayer hold = owners[pos];
owners[pos] = owners[i];
owners[i] = hold;
}
{
unsigned int pos = random_number % (i + 1);
TarotPlayer hold = owners[pos];
owners[pos] = owners[i];
owners[i] = hold;
}
}
return event_set_deal_all (event, 78, owners);
}
......
......@@ -24,7 +24,19 @@
TarotPlayer
tarot_string_to_player (const char *text)
{
return string_to_player (text);
char *end = NULL;
TarotPlayer ret = player_parse (text, &end);
if (end == (char *) text || *end != '\0')
{
ret = (TarotPlayer) - 1;
}
return ret;
}
TarotPlayer
tarot_player_parse (const char *text, char **end)
{
return player_parse (text, end);
}
size_t
......
......@@ -36,6 +36,8 @@ extern "C"
/* Return ((TarotPlayer) (-1) if error */
TarotPlayer tarot_string_to_player (const char *text);
TarotPlayer tarot_player_parse (const char *text, char **end);
size_t tarot_player_to_string (TarotPlayer p, size_t max, char *dest);
#ifdef __cplusplus
......
......@@ -29,7 +29,7 @@ extern "C"
{
#endif /* __cplusplus */
static TarotPlayer string_to_player (const char *text);
static TarotPlayer player_parse (const char *text, char **end);
static size_t player_to_string (TarotPlayer p, size_t max, char *dest);
#ifdef __cplusplus
......
......@@ -21,27 +21,29 @@
#include <tarot/player_private.h>
static TarotPlayer
string_to_player (const char *text)
static inline TarotPlayer
player_parse (const char *text, char **end)
{
TarotPlayer ret = (TarotPlayer) - 1;
if (*text == 'P')
if (text[0] == 'P')
{
text++;
if (*text >= '1' && *text <= '9')
if (text[1] >= '1' && text[1] <= '9')
{
ret = *text - '1';
text++;
if (*text != '\0')
ret = text[1] - '1';
if (end)
{
ret = (TarotPlayer) - 1;
*end = (char *) text + 2;
}
}
}
else if (end)
{
*end = (char *) text;
}
return ret;
}
static size_t
static inline size_t
player_to_string (TarotPlayer p, size_t max, char *dest)
{
if (max != 0)
......
......@@ -31,6 +31,7 @@
#include <tarot/features.h>
#include <tarot/generator.h>
#include <tarot/ai.h>
#include <tarot/lexer.h>
#ifdef __cplusplus
extern "C"
......
......@@ -27,7 +27,7 @@
#include <assert.h>
static inline TarotHand
create_hand (size_t n_max, size_t *n, TarotCard *buffer)
create_hand (size_t n_max, size_t * n, TarotCard * buffer)
{
TarotHand ret;
ret.n_max = n_max;
......
......@@ -90,7 +90,7 @@ tarot_generator_generate_sizeof (const TarotGenerator * generator)
{
return (generator->n_players * sizeof (TarotAi *)
+ generator->n_players * tarot_ai_sizeof ()
+ tarot_game_event_sizeof () + tarot_game_event_data_sizeof (78)
+ tarot_game_event_sizeof (78)
/* For tarot_ai_react */
+ tarot_features_sizeof ());
}
......@@ -117,10 +117,8 @@ tarot_generator_generate (const TarotGenerator * generator,
workspace_p += tarot_ai_sizeof ();
tarot_ai_set_model (ai[i], model);
}
event = tarot_game_event_init (workspace_p);
workspace_p += tarot_game_event_sizeof ();
tarot_game_event_init_data (event, 78, workspace_p);
workspace_p += tarot_game_event_data_sizeof (78);
event = tarot_game_event_init (78, workspace_p);
workspace_p += tarot_game_event_sizeof (78);
/* Setup event */
if (tarot_game_event_set_setup (event, n_players, with_call) == 0)
{
......
# Makefile.am
#
# Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
noinst_LTLIBRARIES += %reldir%/libtarot-lexer.la
tarotinclude_HEADERS += %reldir%/tarot/lexer.h
%canon_reldir%_libtarot_lexer_la_SOURCES = \
%reldir%/lexer.c \
%reldir%/tarot/lexer_private.h \
%reldir%/tarot/lexer_private_impl.h
%canon_reldir%_libtarot_lexer_la_CFLAGS = \
$(AM_CFLAGS)
%canon_reldir%_libtarot_lexer_la_CXXFLAGS = \
$(AM_CXXFLAGS)
%canon_reldir%_libtarot_lexer_la_CPPFLAGS = \
$(AM_CPPFLAGS)
%canon_reldir%_libtarot_lexer_la_LDFLAGS = \
-no-undefined $(AM_LDADD)
check_PROGRAMS += %reldir%/check
%canon_reldir%_check_SOURCES = \
%reldir%/check.c
%canon_reldir%_check_CFLAGS = \
$(AM_CFLAGS)
%canon_reldir%_check_CXXFLAGS = \
$(AM_CXXFLAGS)
%canon_reldir%_check_CPPFLAGS = \
$(AM_CPPFLAGS)
%canon_reldir%_check_LDADD = \
src/libtarot.la
AM_CPPFLAGS += -I $(srcdir)/%reldir% -I %reldir%
INDENTED += $(%canon_reldir%_libtarot_lexer_la_SOURCES) \
$(%canon_reldir%_check_SOURCES)
LIBTAROT_LEXER_LA = %reldir%/libtarot-lexer.la
/**
* file check.c
*
* Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <tarot/lexer.h>
#include <stdlib.h>
#include <string.h>
int
main ()
{
void *lexer_data = malloc (tarot_lexer_sizeof (256));
TarotLexer *lexer;
int fl, fc, ll, lc;
const char *first_line = "with \"an \\\"escaped\\\\sequence\\\"\"";
if (lexer_data == NULL)
{
abort ();
}
lexer = tarot_lexer_init (256, lexer_data);
if (tarot_lexer_next (lexer, first_line, 1) != 4)
{
abort ();
}
if (tarot_lexer_get_token_type (lexer) != tarot_lexer_token_with ())
{
abort ();
}
tarot_lexer_get_position (lexer, &fl, &fc, &ll, &lc);
if (fl != FIRST_LINE)
{
abort ();
}
if (fc != FIRST_COLUMN)
{
abort ();
}
if (ll != FIRST_LINE)
{
abort ();
}
if (lc != FIRST_COLUMN + 4)
{
abort ();
}
if (tarot_lexer_next (lexer, first_line + 4, 1) != 27)
{
abort ();
}
if (tarot_lexer_get_token_type (lexer) != tarot_lexer_token_string ())
{
abort ();
}
if (strcmp (tarot_lexer_get_string (lexer), "an \"escaped\\sequence\"") !=
0)
{
abort ();
}
tarot_lexer_get_position (lexer, &fl, &fc, &ll, &lc);
if (fl != FIRST_LINE)
{
abort ();
}
if (fc != FIRST_COLUMN + 5)
{
abort ();
}
if (ll != FIRST_LINE)
{
abort ();
}
if (lc != FIRST_COLUMN + 31)
{
abort ();
}
if (tarot_lexer_next (lexer, first_line + 31, 1) != 0)
{
abort ();
}
if (tarot_lexer_get_token_type (lexer) != tarot_lexer_token_eof ())
{
abort ();
}
tarot_lexer_get_position (lexer, &fl, &fc, &ll, &lc);
if (fl != FIRST_LINE)
{
abort ();
}
if (fc != FIRST_COLUMN + 31)
{
abort ();
}
if (ll != FIRST_LINE)
{
abort ();
}
if (lc != FIRST_COLUMN + 31)
{
abort ();
}
return 0;
}
/**
* file tarot/lexer.c
*
* Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <tarot/lexer_private.h>
size_t
tarot_lexer_sizeof (size_t max)
{
return lexer_sizeof (max);
}
size_t
tarot_lexer_alignof ()
{
return lexer_alignof ();
}
TarotLexer *
tarot_lexer_init (size_t max, void *mem)
{
return lexer_init (max, mem);
}
size_t
tarot_lexer_next (TarotLexer * lexer, const char *data, int eof)
{
return lexer_next (lexer, data, eof);
}
int
tarot_lexer_get_token_type (const TarotLexer * lexer)
{
return lexer_get_token_type (lexer);
}
unsigned int
tarot_lexer_get_number (const TarotLexer * lexer)
{
return lexer_get_number (lexer);
}
TarotPlayer
tarot_lexer_get_player (const TarotLexer * lexer)
{
return lexer_get_player (lexer);
}
TarotCard
tarot_lexer_get_card (const TarotLexer * lexer)
{
return lexer_get_card (lexer);
}
const char *
tarot_lexer_get_string (const TarotLexer * lexer)
{
return lexer_get_string (lexer);
}
void
tarot_lexer_get_position (const TarotLexer * lexer, int *fl, int *fc, int *ll,
int *lc)
{
lexer_get_position (lexer, fl, fc, ll, lc);
}
const char *
tarot_lexer_get_error (const TarotLexer * lexer)
{
return lexer_get_error (lexer);
}
int
tarot_lexer_token_help ()
{
return TAROT_LEXER_TOKEN_HELP;
}
int
tarot_lexer_token_version ()
{
return TAROT_LEXER_TOKEN_VERSION;
}
int
tarot_lexer_token_license ()
{
return TAROT_LEXER_TOKEN_LICENSE;
}
int
tarot_lexer_token_eof ()
{
return TAROT_LEXER_TOKEN_EOF;
}
int
tarot_lexer_token_error ()
{
return TAROT_LEXER_TOKEN_ERROR;
}
int
tarot_lexer_token_status ()
{
return TAROT_LEXER_TOKEN_STATUS;
}
int
tarot_lexer_token_load ()
{
return TAROT_LEXER_TOKEN_LOAD;
}
int
tarot_lexer_token_save ()
{
return TAROT_LEXER_TOKEN_SAVE;
}
int
tarot_lexer_token_check ()
{
return TAROT_LEXER_TOKEN_CHECK;
}
int
tarot_lexer_token_setup ()
{
return TAROT_LEXER_TOKEN_SETUP;
}
int
tarot_lexer_token_number ()
{
return TAROT_LEXER_TOKEN_NUMBER;
}
int
tarot_lexer_token_with ()
{
return TAROT_LEXER_TOKEN_WITH;
}
int
tarot_lexer_token_call ()
{
return TAROT_LEXER_TOKEN_CALL;
}
int
tarot_lexer_token_deal ()
{
return TAROT_LEXER_TOKEN_DEAL;
}
int
tarot_lexer_token_player ()
{
return TAROT_LEXER_TOKEN_PLAYER;
}
int
tarot_lexer_token_card ()
{
return TAROT_LEXER_TOKEN_CARD;
}
int
tarot_lexer_token_to ()
{
return TAROT_LEXER_TOKEN_TO;
}
int
tarot_lexer_token_bid ()
{
return TAROT_LEXER_TOKEN_BID;
}
int
tarot_lexer_token_pass ()
{
return TAROT_LEXER_TOKEN_PASS;
}
int
tarot_lexer_token_take ()
{
return TAROT_LEXER_TOKEN_TAKE;
}
int
tarot_lexer_token_push ()
{
return TAROT_LEXER_TOKEN_PUSH;
}
int
tarot_lexer_token_straight ()
{
return TAROT_LEXER_TOKEN_STRAIGHT;
}
int
tarot_lexer_token_double ()
{
return TAROT_LEXER_TOKEN_DOUBLE;
}
int
tarot_lexer_token_keep ()
{
return TAROT_LEXER_TOKEN_KEEP;
}
int
tarot_lexer_token_decl ()
{
return TAROT_LEXER_TOKEN_DECL;
}
int
tarot_lexer_token_slam ()
{
return TAROT_LEXER_TOKEN_SLAM;
}
int
tarot_lexer_token_dog ()
{
return TAROT_LEXER_TOKEN_DOG;
}
int
tarot_lexer_token_discard ()
{
return TAROT_LEXER_TOKEN_DISCARD;
}