vtm/quelldatein/aufbau/aktoren/leser.ts
Christian Fraß 74a9792bbf fast fertig
2018-03-26 00:41:10 +02:00

227 lines
4.6 KiB
TypeScript

/*
* Verrückte Turing-Maschinen — A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
module mod_vtm_aufbau
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
export type typ_leser =
{
"richtung": typ_richtung,
"symbol_links": typ_symbol,
"symbol_rechts": typ_symbol,
}
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
function leser_erstellen
(
richtung : typ_richtung,
symbol_links : typ_symbol,
symbol_rechts : typ_symbol
)
: typ_leser
{
return {
"richtung": richtung,
"symbol_links": symbol_links,
"symbol_rechts": symbol_rechts,
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function leser_erstellen_aktor
(
richtung : typ_richtung,
symbol_links : typ_symbol,
symbol_rechts : typ_symbol
)
: typ_aktor
{
return {"art": "leser", "angaben": leser_erstellen(richtung, symbol_links, symbol_rechts)};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function leser_beispiel
(
)
: typ_leser
{
return leser_erstellen(0, 0, 1);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function leser_richtung_lesen
(
leser : typ_leser
)
: typ_richtung
{
return leser.richtung;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function leser_symbol_links_lesen
(
leser : typ_leser
)
: typ_symbol
{
return leser.symbol_links;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function leser_symbol_rechts_lesen
(
leser : typ_leser
)
: typ_symbol
{
return leser.symbol_rechts;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function leser_drehen
(
leser : typ_leser,
inkrement : int = +1
)
: void
{
leser.richtung = richtung_addieren(leser.richtung, inkrement);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function leser_verwenden
(
leser : typ_leser,
figur : typ_figur
)
: void
{
let symbol_ : schnittstelle_fehlermonade<typ_symbol> = figur_lesen(figur);
let summand : typ_richtung;
if (symbol_.ist_schlicht())
{
let symbol : typ_symbol = symbol_.lesen();
if (symbol === leser.symbol_links)
{
figur_schieben(figur);
summand = +2;
}
else if (symbol === leser.symbol_rechts)
{
figur_schieben(figur);
summand = -2;
}
else
{
summand = 0;
}
}
else
{
summand = 0;
}
let richtung : typ_richtung = richtung_addieren(leser.richtung, summand);
figur_bewegen(figur, richtung);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function leser_exportieren
(
leser : typ_leser
)
: any
{
return {
"richtung": richtung_exportieren(leser.richtung),
"symbol_links": symbol_exportieren(leser.symbol_links),
"symbol_rechts": symbol_exportieren(leser.symbol_rechts),
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function leser_importieren
(
roh : any
)
: typ_leser
{
return (
leser_erstellen
(
richtung_importieren(roh["richtung"]),
symbol_importieren(roh["symbol_links"]),
symbol_importieren(roh["symbol_rechts"])
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
let wrap = (angaben => ({"art": "leser", "angaben": angaben}));
let unwrap = (aktor => aktor.angaben);
implementierung_aktor["leser"] =
{
"beispiel": () => wrap(leser_beispiel()),
"drehen": (aktor, inkrement) => leser_drehen(unwrap(aktor), inkrement),
"verwenden": (aktor, figur) => leser_verwenden(unwrap(aktor), figur),
"exportieren": (aktor) => ({"art": "leser", "angaben": leser_exportieren(aktor.angaben)}),
"importieren": (roh) => wrap(leser_importieren(roh)),
}
;
}