-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstructions.h
99 lines (87 loc) · 2.54 KB
/
Instructions.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Instructions is a library that helps you build programs based on instructions
It provides a standard set of instruction types, along with helper functions that properly allocate parameters for those instructions
Please visit the JMM Instruction Book for help
*/
#pragma once
#include <string> // FOR PRINT
#include <thread> // FOR WAIT
#define MSTRLEN 128
using String = char[MSTRLEN];
size_t CurrentOffset = 24;
bool WantsToEnd = false;
namespace JMM
{
enum Type
{
PRINT,
BREAK,
CLEAR,
WAIT,
JUMP
};
enum InstructionSizes
{
PRINTSZ = sizeof(String) + 28,
BREAKSZ = 28,
CLEARSZ = 28,
WAITSZ = sizeof(int) + 28,
JUMPSZ = sizeof(size_t) + 28
};
void Print(String Content)
{
JMM::Type* NewPrintInstruction = JMM::Allocate<JMM::Type>();
*NewPrintInstruction = Type::PRINT;
std::strncpy(*JMM::Allocate<String>(), Content, MSTRLEN - 1);
}
void Break()
{
JMM::Type* BreakInstruction = JMM::Allocate<JMM::Type>();
*BreakInstruction = Type::BREAK;
}
void Clear()
{
JMM::Type* ClearInstruction = JMM::Allocate<JMM::Type>();
*ClearInstruction = Type::CLEAR;
}
void Wait(int Milliseconds)
{
JMM::Type* WaitInstruction = JMM::Allocate<JMM::Type>();
*WaitInstruction = Type::WAIT;
*JMM::Allocate<int>() = Milliseconds;
}
void JumpInstruction(size_t OffsetToAdd)
{
JMM::Type* JumpInstruction = JMM::Allocate<JMM::Type>();
*JumpInstruction = Type::JUMP;
*JMM::Allocate<size_t>() = OffsetToAdd;
}
template <typename T>
void Jump()
{
CurrentOffset += sizeof(T) + 24;
}
void ExecuteInstruction(JMM::Type Instruction)
{
switch (Instruction)
{
case JMM::Type::PRINT:
std::cout << JMM::GetValueFromOffset<std::string>(CurrentOffset + 28).c_str() << std::endl;
Jump<String>(); // add the string position to the current offset
break;
case JMM::Type::BREAK:
WantsToEnd = true;
break;
case JMM::Type::CLEAR:
system("cls");
break;
case JMM::Type::JUMP:
CurrentOffset += JMM::GetValueFromOffset<size_t>(CurrentOffset + 28) + 24;
break;
case JMM::Type::WAIT:
std::this_thread::sleep_for(std::chrono::milliseconds(JMM::GetValueFromOffset<int>(CurrentOffset + 28)));
Jump<int>();
break;
}
}
}