249 lines
6.2 KiB
TypeScript
249 lines
6.2 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
|
|
{
|
|
|
|
export module mod_model
|
|
{
|
|
|
|
export module mod_actuator
|
|
{
|
|
|
|
export module mod_leser
|
|
{
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
let kind : string = "leser";
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
export type type_leser =
|
|
{
|
|
"direction": mod_direction.type_direction,
|
|
"symbol_links": mod_symbol.type_symbol,
|
|
"symbol_rechts": mod_symbol.type_symbol,
|
|
}
|
|
;
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
function create
|
|
(
|
|
direction : mod_direction.type_direction,
|
|
symbol_links : mod_symbol.type_symbol,
|
|
symbol_rechts : mod_symbol.type_symbol
|
|
)
|
|
: type_leser
|
|
{
|
|
return {
|
|
"direction": direction,
|
|
"symbol_links": symbol_links,
|
|
"symbol_rechts": symbol_rechts,
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
export function create_actuator
|
|
(
|
|
direction : mod_direction.type_direction,
|
|
symbol_links : mod_symbol.type_symbol,
|
|
symbol_rechts : mod_symbol.type_symbol
|
|
)
|
|
: mod_actuator.type_actuator
|
|
{
|
|
return lib_call.wrap(kind, create(direction, symbol_links, symbol_rechts));
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
function example
|
|
(
|
|
)
|
|
: type_leser
|
|
{
|
|
return create(0, 0, 1);
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
export function direction_read
|
|
(
|
|
leser : type_leser
|
|
)
|
|
: mod_direction.type_direction
|
|
{
|
|
return leser.direction;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
export function symbol_links_read
|
|
(
|
|
leser : type_leser
|
|
)
|
|
: mod_symbol.type_symbol
|
|
{
|
|
return leser.symbol_links;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
export function symbol_rechts_read
|
|
(
|
|
leser : type_leser
|
|
)
|
|
: mod_symbol.type_symbol
|
|
{
|
|
return leser.symbol_rechts;
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
function rotate
|
|
(
|
|
leser : type_leser,
|
|
inkrement : int = +1
|
|
)
|
|
: void
|
|
{
|
|
leser.direction = mod_direction.add(leser.direction, inkrement);
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
function use
|
|
(
|
|
leser : type_leser,
|
|
token : mod_token.type_token
|
|
)
|
|
: void
|
|
{
|
|
let symbol_ : lib_errormonade.type_errormonade<mod_symbol.type_symbol> = mod_token.read(token);
|
|
let summand : mod_direction.type_direction;
|
|
if (lib_errormonade.filled(symbol_))
|
|
{
|
|
let symbol : mod_symbol.type_symbol = lib_errormonade.read(symbol_);
|
|
if (symbol === leser.symbol_links)
|
|
{
|
|
mod_token.shift(token);
|
|
summand = +2;
|
|
}
|
|
else if (symbol === leser.symbol_rechts)
|
|
{
|
|
mod_token.shift(token);
|
|
summand = -2;
|
|
}
|
|
else
|
|
{
|
|
summand = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
summand = 0;
|
|
}
|
|
let direction : mod_direction.type_direction = mod_direction.add(leser.direction, summand);
|
|
mod_token.move(token, direction);
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
function export_
|
|
(
|
|
leser : type_leser
|
|
)
|
|
: any
|
|
{
|
|
return {
|
|
"direction": mod_direction.export_(leser.direction),
|
|
"symbol_links": mod_symbol.export_(leser.symbol_links),
|
|
"symbol_rechts": mod_symbol.export_(leser.symbol_rechts),
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
function import_
|
|
(
|
|
raw : any
|
|
)
|
|
: type_leser
|
|
{
|
|
return (
|
|
create
|
|
(
|
|
mod_direction.import_(raw["direction"]),
|
|
mod_symbol.import_(raw["symbol_links"]),
|
|
mod_symbol.import_(raw["symbol_rechts"])
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @author kcf <vidofnir@folksprak.org>
|
|
*/
|
|
lib_trait.attend<signature_actuator>
|
|
(
|
|
trait_actuator,
|
|
kind,
|
|
{
|
|
"example": () => lib_call.wrap(kind, example()),
|
|
"rotate": (actuator, inkrement) => rotate(lib_call.unwrap(actuator), inkrement),
|
|
"use": (actuator, token) => use(lib_call.unwrap(actuator), token),
|
|
"export": (actuator) => ({"kind": kind, "data": export_(actuator.data)}),
|
|
"import": (raw) => lib_call.wrap(kind, import_(raw["data"])),
|
|
}
|
|
)
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|