Last active
August 13, 2017 21:53
-
-
Save SeanCline/77041085d206d27e8841612edb22032a to your computer and use it in GitHub Desktop.
Implementing the assembly-like language syntax in Human Resources Machine using the C preprocessor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <deque> | |
#include <vector> | |
struct hrm_num { | |
static int validate(int n) | |
{ | |
if (n < -999 || n > 999) | |
throw "Invalid number!"; | |
return n; | |
} | |
hrm_num() : num_(1337) {} | |
hrm_num(int n) : num_(validate(n)) {} | |
hrm_num(const hrm_num& other) : num_(validate(other.num_)) {} | |
hrm_num& operator=(const hrm_num& other) | |
{ | |
num_ = other.num_; | |
return *this; | |
} | |
hrm_num operator+(hrm_num rhs) | |
{ | |
validate(num_); | |
validate(rhs.num_); | |
return validate(num_ + rhs.num_); | |
} | |
hrm_num operator-(hrm_num rhs) | |
{ | |
validate(num_); | |
validate(rhs.num_); | |
return validate(num_ - rhs.num_); | |
} | |
friend std::istream& operator<<(std::istream& is, hrm_num& n) | |
{ | |
return is >> n.num_; | |
} | |
friend std::ostream& operator<<(std::ostream& os, const hrm_num& n) | |
{ | |
return os << n.num_; | |
} | |
private: | |
int num_; | |
}; | |
hrm_num hand; | |
std::deque<hrm_num> inbox_queue = { 1, 2, 3, 4 }; | |
typedef size_t tile_index; | |
std::vector<hrm_num> tiles(256); | |
struct stub { | |
stub() : nullary_fn_(nullptr), unary_fn_(nullptr) | |
{} | |
stub(void(*fn)()) : nullary_fn_(fn), unary_fn_(nullptr) | |
{} | |
stub(void(*fn)(tile_index)) : nullary_fn_(nullptr), unary_fn_(fn) | |
{} | |
stub operator&(tile_index i) | |
{ | |
unary_fn_(i); | |
return stub(); | |
} | |
stub operator,(stub s) | |
{ | |
if (nullary_fn_) | |
nullary_fn_(); | |
return stub(); | |
} | |
void(*nullary_fn_)(); | |
void(*unary_fn_)(tile_index); | |
}; | |
void inbox() | |
{ | |
hand = inbox_queue.at(0); | |
inbox_queue.pop_front(); | |
} | |
void outbox() | |
{ | |
std::cout << hand << "\n"; | |
hand = hrm_num(); | |
} | |
void copyto(tile_index i) | |
{ | |
tiles.at(i) = hand; | |
} | |
void copyfrom(tile_index i) | |
{ | |
hand = tiles[i]; | |
} | |
void add(tile_index i) | |
{ | |
hand = hand + tiles[i]; | |
} | |
void sub(tile_index i) | |
{ | |
hand = hand + tiles[i]; | |
} | |
void bumpup(tile_index i) | |
{ | |
tiles[i] = hand = tiles[i] + 1; | |
} | |
void bumpdn(tile_index i) | |
{ | |
tiles[i] = hand = tiles[i] - 1; | |
} | |
#define BEGIN_HRM int main() { stub() | |
#define END_HRM ,stub(); } | |
#define INBOX , stub(); stub(inbox) | |
#define OUTBOX , stub(); stub(outbox) | |
#define COPYTO , stub(); stub(copyto) & | |
#define COPYFROM , stub(); stub(copyfrom) & | |
#define ADD , stub(); stub(add) & | |
#define SUB , stub(); stub(sub) & | |
#define BUMPUP , stub(); stub(bumpup) & | |
#define BUMPDN , stub(); stub(bumpdn) & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hrm.h" | |
// https://github.com/atesgoral/hrm-solutions/blob/master/solutions/10-Octoplier-Suite-9.36/32.32.specific-nanashi-juanto.asm | |
BEGIN_HRM | |
INBOX | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
OUTBOX | |
INBOX | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
OUTBOX | |
INBOX | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
OUTBOX | |
INBOX | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
COPYTO 0 | |
ADD 0 | |
OUTBOX | |
END_HRM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment