2016-09-06 00:02:35 +00:00
|
|
|
//
|
|
|
|
// TapeParser.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/09/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef TapeParser_hpp
|
|
|
|
#define TapeParser_hpp
|
|
|
|
|
2016-11-06 21:13:13 +00:00
|
|
|
#include "../Tape.hpp"
|
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
#include <cassert>
|
2016-11-06 21:13:13 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
namespace Tape {
|
2016-09-06 00:02:35 +00:00
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
template <typename SymbolType> class Parser {
|
2016-09-06 00:02:35 +00:00
|
|
|
public:
|
2016-09-06 02:06:39 +00:00
|
|
|
/// Resets the error flag.
|
2017-06-07 21:21:57 +00:00
|
|
|
void reset_error_flag() { error_flag_ = false; }
|
2016-09-06 02:06:39 +00:00
|
|
|
/// @returns @c true if an error has occurred since the error flag was last reset; @c false otherwise.
|
2017-06-07 21:21:57 +00:00
|
|
|
bool get_error_flag() { return error_flag_; }
|
2016-11-06 20:25:18 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Asks the parser to continue taking pulses from the tape until either the subclass next declares a symbol
|
|
|
|
or the tape runs out, returning the most-recently declared symbol.
|
|
|
|
*/
|
2017-06-07 14:05:43 +00:00
|
|
|
SymbolType get_next_symbol(const std::shared_ptr<Storage::Tape::Tape> &tape) {
|
2017-06-07 21:21:57 +00:00
|
|
|
while(!has_next_symbol_ && !tape->is_at_end()) {
|
2016-11-06 20:25:18 +00:00
|
|
|
process_pulse(tape->get_next_pulse());
|
|
|
|
}
|
2017-07-22 19:40:22 +00:00
|
|
|
if(!has_next_symbol_ && tape->is_at_end()) mark_end();
|
2017-06-07 21:21:57 +00:00
|
|
|
has_next_symbol_ = false;
|
|
|
|
return next_symbol_;
|
2016-11-06 20:25:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:21:57 +00:00
|
|
|
/*!
|
|
|
|
This class provides a single token of lookahead; return_symbol allows the single previous
|
|
|
|
token supplied by get_next_symbol to be returned, in which case it will be the thing returned
|
|
|
|
by the next call to get_next_symbol.
|
|
|
|
*/
|
|
|
|
void return_symbol(SymbolType symbol) {
|
|
|
|
assert(!has_next_symbol_);
|
|
|
|
has_next_symbol_ = true;
|
|
|
|
next_symbol_ = symbol;
|
|
|
|
}
|
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
/*!
|
|
|
|
@returns `true` if there is no data left on the tape and the WaveType queue has been exhausted; `false` otherwise.
|
|
|
|
*/
|
|
|
|
bool is_at_end(const std::shared_ptr<Storage::Tape::Tape> &tape) {
|
|
|
|
return tape->is_at_end() && !has_next_symbol_;
|
|
|
|
}
|
2017-06-07 21:21:57 +00:00
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
protected:
|
2016-11-06 20:25:18 +00:00
|
|
|
/*!
|
2017-07-15 23:07:35 +00:00
|
|
|
Should be implemented by subclasses. Consumes @c pulse.
|
2016-11-06 20:25:18 +00:00
|
|
|
*/
|
2017-07-15 23:07:35 +00:00
|
|
|
virtual void process_pulse(const Storage::Tape::Tape::Pulse &pulse) = 0;
|
2016-09-06 00:02:35 +00:00
|
|
|
|
2017-06-09 01:32:27 +00:00
|
|
|
/*!
|
|
|
|
An optional implementation for subclasses; called to announce that the tape has ended: that
|
|
|
|
no more process_pulse calls will occur.
|
|
|
|
*/
|
|
|
|
virtual void mark_end() {}
|
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
/*!
|
|
|
|
Sets @c symbol as the newly-recognised symbol.
|
|
|
|
*/
|
|
|
|
void push_symbol(SymbolType symbol) {
|
|
|
|
has_next_symbol_ = true;
|
|
|
|
next_symbol_ = symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_error_flag() {
|
|
|
|
error_flag_ = true;
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:14:22 +00:00
|
|
|
bool error_flag_ = false;
|
2017-07-15 23:07:35 +00:00
|
|
|
SymbolType next_symbol_;
|
2017-11-10 03:14:22 +00:00
|
|
|
bool has_next_symbol_ = false;
|
2017-07-15 23:07:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
A partly-abstract base class to help in the authorship of tape format parsers;
|
|
|
|
provides hooks for receipt of pulses, which are intended to be classified into waves,
|
|
|
|
and for symbol identification from waves.
|
|
|
|
|
|
|
|
Very optional, not intended to box in the approaches taken for analysis. See also
|
|
|
|
the PLLParser.
|
|
|
|
*/
|
|
|
|
template <typename WaveType, typename SymbolType> class PulseClassificationParser: public Parser<SymbolType> {
|
|
|
|
public:
|
|
|
|
virtual void process_pulse(const Storage::Tape::Tape::Pulse &pulse) = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
process_pulse should either call @c push_wave or to take no action.
|
|
|
|
*/
|
|
|
|
|
2016-09-06 00:02:35 +00:00
|
|
|
protected:
|
2017-07-15 23:07:35 +00:00
|
|
|
/*!
|
|
|
|
Sets @c symbol as the newly-recognised symbol and removes @c nunber_of_waves waves from the front of the list.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
Expected to be called by subclasses from @c process_pulse when it recognises that the first @c number_of_waves
|
|
|
|
waves together represent @c symbol.
|
|
|
|
*/
|
|
|
|
void push_symbol(SymbolType symbol, int number_of_waves) {
|
|
|
|
Parser<SymbolType>::push_symbol(symbol);
|
|
|
|
remove_waves(number_of_waves);
|
|
|
|
}
|
2016-09-06 02:06:39 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Adds @c wave to the back of the list of recognised waves and calls @c inspect_waves to check for a new symbol.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2016-09-06 02:06:39 +00:00
|
|
|
Expected to be called by subclasses from @c process_pulse as and when recognised waves arise.
|
|
|
|
*/
|
2017-06-07 14:05:43 +00:00
|
|
|
void push_wave(WaveType wave) {
|
2017-06-07 21:21:57 +00:00
|
|
|
wave_queue_.push_back(wave);
|
|
|
|
inspect_waves(wave_queue_);
|
2016-09-06 00:02:35 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 02:06:39 +00:00
|
|
|
/*!
|
|
|
|
Removes @c nunber_of_waves waves from the front of the list.
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2016-09-06 02:06:39 +00:00
|
|
|
Expected to be called by subclasses from @c process_pulse if it is recognised that the first set of waves
|
|
|
|
do not form a valid symbol.
|
|
|
|
*/
|
2017-06-07 14:05:43 +00:00
|
|
|
void remove_waves(int number_of_waves) {
|
2017-06-07 21:21:57 +00:00
|
|
|
wave_queue_.erase(wave_queue_.begin(), wave_queue_.begin()+number_of_waves);
|
2016-09-06 00:02:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
private:
|
2016-09-06 02:06:39 +00:00
|
|
|
/*!
|
2017-07-15 23:07:35 +00:00
|
|
|
Should be implemented by subclasses. Inspects @c waves for a potential new symbol. If one is
|
|
|
|
found should call @c push_symbol. May wish alternatively to call @c remove_waves to have entries
|
|
|
|
removed from the start of @c waves that cannot form a valid symbol. Need not do anything while
|
|
|
|
the waves at the start of @c waves may end up forming a symbol but the symbol is not yet complete.
|
2016-09-06 02:06:39 +00:00
|
|
|
*/
|
2017-07-15 23:07:35 +00:00
|
|
|
virtual void inspect_waves(const std::vector<WaveType> &waves) = 0;
|
2016-09-06 00:02:35 +00:00
|
|
|
|
2017-07-15 23:07:35 +00:00
|
|
|
std::vector<WaveType> wave_queue_;
|
|
|
|
};
|
|
|
|
|
2016-11-06 21:13:13 +00:00
|
|
|
}
|
2016-09-06 00:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TapeParser_hpp */
|