/
IOComponent.cpp
67 lines (52 loc) · 1.11 KB
/
IOComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// IOComponent.cpp
// Fission
//
// Created by C0deH4cker on 2/25/14.
// Copyright (c) 2014 C0deH4cker. All rights reserved.
//
#include "IOComponent.h"
#include <iostream>
#include "common.h"
#include "Atom.h"
using namespace fsn;
IOComponent::IOComponent(Token type)
: Component(type), pastEOF(false) {}
bool IOComponent::onHit(Atom& atom) {
char c = '\0';
bool destroy = false;
switch(type) {
case Token::IO_NEWLINE:
std::cout.put('\n');
break;
case Token::IO_OUTCHAR:
std::cout.put((char)atom.mass);
break;
case Token::IO_ENDCHAR:
std::cout.put((char)atom.mass);
destroy = true;
break;
case Token::IO_INCHAR:
if(pastEOF) {
return true;
}
if(std::cin.get(c)) {
atom.mass = (int64_t)(unsigned char)c;
atom.energy = 0;
}
else {
atom.energy = 1;
pastEOF = true;
}
break;
case Token::IO_OUTSTR:
if((atom.flags & AtomicFlags::Printing) == AtomicFlags::None) {
atom.mass = 0;
}
atom.flags ^= AtomicFlags::Printing;
break;
default:
fatal("Cell '%c' is not an IOComponent.", (char)type);
}
return destroy;
}