-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbytecode.cpp
More file actions
74 lines (54 loc) · 2.3 KB
/
Copy pathbytecode.cpp
File metadata and controls
74 lines (54 loc) · 2.3 KB
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
#include "src/bytecode.h"
#include <cstdarg>
namespace fn { namespace bytecode {
NameHash hashName(std::string name) {
return (NameHash)std::hash<std::string>{}(name);
}
CodeBlob iFalse() { return CodeBlob{FN_OP_FALSE}; }
CodeBlob iTrue() { return CodeBlob{FN_OP_TRUE}; }
CodeBlob iAnd() { return CodeBlob{FN_OP_AND}; }
CodeBlob iOr() { return CodeBlob{FN_OP_OR}; }
CodeBlob iNot() { return CodeBlob{FN_OP_NOT}; }
CodeBlob iNumber(Number number) {
return iNumber(number.coefficient, number.exponent);
}
CodeBlob iNumber(Coefficient coefficient, Exponent exponent) {
std::array<CodeByte, 10> bytes = std::array<CodeByte, 10>();
bytes[0] = FN_OP_NUMBER;
bytes[1] = exponent;
bytes[2] = coefficient;
return CodeBlob(bytes);
}
CodeBlob iMultiply() { return CodeBlob{FN_OP_MULTIPLY}; }
CodeBlob iDivide() { return CodeBlob{FN_OP_DIVIDE}; }
CodeBlob iAdd() { return CodeBlob{FN_OP_ADD}; }
CodeBlob iSubtract() { return CodeBlob{FN_OP_SUBTRACT}; }
CodeBlob iEq() { return CodeBlob{FN_OP_EQ}; }
CodeBlob iName(std::string name) { return iName(hashName(name)); }
CodeBlob iName(NameHash name) { return CodeBlob{FN_OP_NAME, name}; }
CodeBlob iLoad(std::string name) { return iLoad(hashName(name)); }
CodeBlob iLoad(NameHash name) { return CodeBlob{FN_OP_LOAD, name}; }
CodeBlob iDefHeader(InstructionIndex length, std::vector<std::string> argNames) {
std::vector<NameHash> argNameHashes = std::vector<NameHash>();
for(auto argName : argNames) {
argNameHashes.push_back(hashName(argName));
}
return iDefHeader(length, argNameHashes);
}
CodeBlob iDefHeader(InstructionIndex length, std::vector<NameHash> argNames) {
CodeBlob blob = CodeBlob{FN_OP_DEF};
blob.append(length);
NumArgs numArgs = (NumArgs)argNames.size();
blob.append(numArgs);
for(auto argName : argNames) {
blob.append(argName);
}
return blob;
}
CodeBlob iCall() { return CodeBlob{FN_OP_CALL}; }
CodeBlob iReturnLast() { return CodeBlob{FN_OP_RETURN_LAST}; }
CodeBlob iJumpIfLastFalse(InstructionIndex jump) { return CodeBlob{FN_OP_FALSE_JUMP, jump}; }
CodeBlob iNewFrame() { return CodeBlob{FN_OP_NEW_FRAME}; }
CodeBlob iCompress() { return CodeBlob{FN_OP_COMPRESS}; }
CodeBlob iExpand() { return CodeBlob{FN_OP_EXPAND}; }
}}