Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
map.cpp
Go to the documentation of this file.
1#include "map.hpp"
2
3namespace Phasor {
4
5const std::unordered_map<OpCode, std::string> opCodeToStringMap = {
6 {OpCode::PUSH_CONST, "PUSH_CONST"},
7 {OpCode::POP, "POP"},
8 {OpCode::IADD, "IADD"},
9 {OpCode::ISUBTRACT, "ISUBTRACT"},
10 {OpCode::IMULTIPLY, "IMULTIPLY"},
11 {OpCode::IDIVIDE, "IDIVIDE"},
12 {OpCode::IMODULO, "IMODULO"},
13 {OpCode::FLADD, "FLADD"},
14 {OpCode::FLSUBTRACT, "FLSUBTRACT"},
15 {OpCode::FLMULTIPLY, "FLMULTIPLY"},
16 {OpCode::FLDIVIDE, "FLDIVIDE"},
17 {OpCode::FLMODULO, "FLMODULO"},
18 {OpCode::SQRT, "SQRT"},
19 {OpCode::POW, "POW"},
20 {OpCode::LOG, "LOG"},
21 {OpCode::EXP, "EXP"},
22 {OpCode::SIN, "SIN"},
23 {OpCode::COS, "COS"},
24 {OpCode::TAN, "TAN"},
25 {OpCode::NEGATE, "NEGATE"},
26 {OpCode::NOT, "NOT"},
27 {OpCode::IAND, "IAND"},
28 {OpCode::IOR, "IOR"},
29 {OpCode::IEQUAL, "IEQUAL"},
30 {OpCode::INOT_EQUAL, "INOT_EQUAL"},
31 {OpCode::ILESS_THAN, "ILESS_THAN"},
32 {OpCode::IGREATER_THAN, "IGREATER_THAN"},
33 {OpCode::ILESS_EQUAL, "ILESS_EQUAL"},
34 {OpCode::IGREATER_EQUAL, "IGREATER_EQUAL"},
35 {OpCode::FLAND, "FLAND"},
36 {OpCode::FLOR, "FLOR"},
37 {OpCode::FLEQUAL, "FLEQUAL"},
38 {OpCode::FLNOT_EQUAL, "FLNOT_EQUAL"},
39 {OpCode::FLLESS_THAN, "FLLESS_THAN"},
40 {OpCode::FLGREATER_THAN, "FLGREATER_THAN"},
41 {OpCode::FLLESS_EQUAL, "FLLESS_EQUAL"},
42 {OpCode::FLGREATER_EQUAL, "FLGREATER_EQUAL"},
43 {OpCode::JUMP, "JUMP"},
44 {OpCode::JUMP_IF_FALSE, "JUMP_IF_FALSE"},
45 {OpCode::JUMP_IF_TRUE, "JUMP_IF_TRUE"},
46 {OpCode::JUMP_BACK, "JUMP_BACK"},
47 {OpCode::STORE_VAR, "STORE_VAR"},
48 {OpCode::LOAD_VAR, "LOAD_VAR"},
49 {OpCode::PRINT, "PRINT"},
50 {OpCode::PRINTERROR, "PRINTERROR"},
51 {OpCode::READLINE, "READLINE"},
52 {OpCode::IMPORT, "IMPORT"},
53 {OpCode::HALT, "HALT"},
54 {OpCode::CALL_NATIVE, "CALL_NATIVE"},
55 {OpCode::CALL, "CALL"},
56 {OpCode::SYSTEM, "SYSTEM"},
57 {OpCode::SYSTEM_OUT, "SYSTEM_OUT"},
58 {OpCode::SYSTEM_ERR, "SYSTEM_ERR"},
59 {OpCode::RETURN, "RETURN"},
60 {OpCode::TRUE_P, "TRUE"},
61 {OpCode::FALSE_P, "FALSE"},
62 {OpCode::NULL_VAL, "NULL_VAL"},
63 {OpCode::LEN, "LEN"},
64 {OpCode::CHAR_AT, "CHAR_AT"},
65 {OpCode::SUBSTR, "SUBSTR"},
66 {OpCode::MOV, "MOV"},
67 {OpCode::LOAD_CONST_R, "LOAD_CONST_R"},
68 {OpCode::LOAD_VAR_R, "LOAD_VAR_R"},
69 {OpCode::STORE_VAR_R, "STORE_VAR_R"},
70 {OpCode::IADD_R, "IADD_R"},
71 {OpCode::ISUB_R, "ISUB_R"},
72 {OpCode::IMUL_R, "IMUL_R"},
73 {OpCode::IDIV_R, "IDIV_R"},
74 {OpCode::IMOD_R, "IMOD_R"},
75 {OpCode::FLADD_R, "FLADD_R"},
76 {OpCode::FLSUB_R, "FLSUB_R"},
77 {OpCode::FLMUL_R, "FLMUL_R"},
78 {OpCode::FLDIV_R, "FLDIV_R"},
79 {OpCode::FLMOD_R, "FLMOD_R"},
80 {OpCode::SQRT_R, "SQRT_R"},
81 {OpCode::POW_R, "POW_R"},
82 {OpCode::LOG_R, "LOG_R"},
83 {OpCode::EXP_R, "EXP_R"},
84 {OpCode::SIN_R, "SIN_R"},
85 {OpCode::COS_R, "COS_R"},
86 {OpCode::TAN_R, "TAN_R"},
87 {OpCode::IAND_R, "IAND_R"},
88 {OpCode::IOR_R, "IOR_R"},
89 {OpCode::IEQ_R, "IEQ_R"},
90 {OpCode::INE_R, "INE_R"},
91 {OpCode::ILT_R, "ILT_R"},
92 {OpCode::IGT_R, "IGT_R"},
93 {OpCode::ILE_R, "ILE_R"},
94 {OpCode::IGE_R, "IGE_R"},
95 {OpCode::FLAND_R, "FLAND_R"},
96 {OpCode::FLOR_R, "FLOR_R"},
97 {OpCode::FLEQ_R, "FLEQ_R"},
98 {OpCode::FLNE_R, "FLNE_R"},
99 {OpCode::FLLT_R, "FLLT_R"},
100 {OpCode::FLGT_R, "FLGT_R"},
101 {OpCode::FLLE_R, "FLLE_R"},
102 {OpCode::FLGE_R, "FLGE_R"},
103 {OpCode::PUSH_R, "PUSH_R"},
104 {OpCode::PUSH2_R, "PUSH2_R"},
105 {OpCode::POP_R, "POP_R"},
106 {OpCode::POP2_R, "POP2_R"},
107 {OpCode::NEG_R, "NEG_R"},
108 {OpCode::NOT_R, "NOT_R"},
109 {OpCode::PRINT_R, "PRINT_R"},
110 {OpCode::PRINTERROR_R, "PRINTERROR_R"},
111 {OpCode::READLINE_R, "READLINE_R"},
112 {OpCode::SYSTEM_R, "SYSTEM_R"},
113 {OpCode::SYSTEM_OUT_R, "SYSTEM_OUT_R"},
114 {OpCode::SYSTEM_ERR_R, "SYSTEM_ERR_R"}
115};
116
117const std::unordered_map<std::string, OpCode> stringToOpCodeMap = [] {
118 std::unordered_map<std::string, OpCode> map;
119 for (const auto &pair : opCodeToStringMap)
120 {
121 map[pair.second] = pair.first;
122 }
123 return map;
124}();
125
126std::string opCodeToString(OpCode op)
127{
128 auto it = opCodeToStringMap.find(op);
129 if (it != opCodeToStringMap.end())
130 {
131 return it->second;
132 }
133 return "UNKNOWN";
134}
135
136OpCode stringToOpCode(const std::string &str)
137{
138 auto it = stringToOpCodeMap.find(str);
139 if (it != stringToOpCodeMap.end())
140 {
141 return it->second;
142 }
143 throw std::runtime_error("Unknown opcode string: " + str);
144}
145} // namespace Phasor
The Phasor Programming Language and Runtime.
Definition AST.hpp:11
const std::unordered_map< OpCode, std::string > opCodeToStringMap
Definition map.cpp:5
OpCode
Definition ISA.hpp:12
@ IGREATER_THAN
Pop b, pop a, push a > b.
Definition ISA.hpp:50
@ IEQUAL
Pop b, pop a, push a == b.
Definition ISA.hpp:47
@ SYSTEM_OUT
Call system function and push stdout.
Definition ISA.hpp:79
@ LOG_R
R[rA] = log(R[rB]).
Definition ISA.hpp:125
@ FLMOD_R
R[rA] = R[rB] % R[rC].
Definition ISA.hpp:122
@ SUBSTR
Pop len, pop start, pop s, push s.substr(start, len).
Definition ISA.hpp:91
@ IAND
Pop b, pop a, push a && b.
Definition ISA.hpp:41
@ NOT
Pop a, push !a.
Definition ISA.hpp:38
@ NULL_VAL
Push null.
Definition ISA.hpp:86
@ MOV
Copy register to register: R[rA] = R[rB].
Definition ISA.hpp:103
@ POW
pow()
Definition ISA.hpp:29
@ PRINTERROR_R
Print register to stderr: printerror(R[rA]).
Definition ISA.hpp:155
@ IAND_R
R[rA] = R[rB] && R[rC].
Definition ISA.hpp:132
@ FLMUL_R
R[rA] = R[rB] * R[rC].
Definition ISA.hpp:120
@ IADD
Pop b, pop a, push a + b.
Definition ISA.hpp:18
@ PUSH2_R
Push 2 registers to stack: push2(R[rA], R[rB]).
Definition ISA.hpp:108
@ FLGE_R
R[rA] = R[rB] >= R[rC].
Definition ISA.hpp:147
@ SYSTEM_R
Run an operating system shell command: system(R[rA]).
Definition ISA.hpp:157
@ PUSH_CONST
Push constant from constant pool.
Definition ISA.hpp:14
@ JUMP_IF_TRUE
Jump if top of stack is true (pops value).
Definition ISA.hpp:63
@ FLGT_R
R[rA] = R[rB] > R[rC].
Definition ISA.hpp:145
@ PUSH_R
Push register to stack: push(R[rA]).
Definition ISA.hpp:107
@ POP_R
Pop stack to register: R[rA] = pop().
Definition ISA.hpp:109
@ FLEQUAL
Pop b, pop a, push a == b.
Definition ISA.hpp:53
@ FLNOT_EQUAL
Pop b, pop a, push a != b.
Definition ISA.hpp:54
@ FLADD_R
R[rA] = R[rB] + R[rC].
Definition ISA.hpp:118
@ POP2_R
Pop 2 values from stack to registers: pop2(R[rA], R[rB]).
Definition ISA.hpp:110
@ LOAD_CONST_R
Load constant to register: R[rA] = constants[immediate].
Definition ISA.hpp:104
@ SQRT
sqrt()
Definition ISA.hpp:28
@ FLADD
Pop b, pop a, push a + b.
Definition ISA.hpp:23
@ JUMP
Unconditional jump to offset.
Definition ISA.hpp:61
@ FLLT_R
R[rA] = R[rB] < R[rC].
Definition ISA.hpp:144
@ NOT_R
R[rA] = !R[rB].
Definition ISA.hpp:151
@ IMULTIPLY
Pop b, pop a, push a * b.
Definition ISA.hpp:20
@ READLINE_R
Read line into register: readline(R[rA]).
Definition ISA.hpp:156
@ LOG
log()
Definition ISA.hpp:30
@ CHAR_AT
Pop index, pop s, push s[index].
Definition ISA.hpp:90
@ IMUL_R
R[rA] = R[rB] * R[rC].
Definition ISA.hpp:115
@ FLGREATER_EQUAL
Pop b, pop a, push a >= b.
Definition ISA.hpp:58
@ FLLE_R
R[rA] = R[rB] <= R[rC].
Definition ISA.hpp:146
@ IGREATER_EQUAL
Pop b, pop a, push a >= b.
Definition ISA.hpp:52
@ SIN
sin()
Definition ISA.hpp:32
@ SYSTEM_ERR
Call system function and push stderr.
Definition ISA.hpp:80
@ SQRT_R
R[rA] = sqrt(R[rB]).
Definition ISA.hpp:123
@ ISUB_R
R[rA] = R[rB] - R[rC].
Definition ISA.hpp:114
@ FLDIV_R
R[rA] = R[rB] / R[rC].
Definition ISA.hpp:121
@ COS_R
R[rA] = cos(R[rB]).
Definition ISA.hpp:128
@ CALL_NATIVE
Call a native function: operand is index of function name in constants.
Definition ISA.hpp:76
@ ISUBTRACT
Pop b, pop a, push a - b.
Definition ISA.hpp:19
@ LEN
Pop s, push len(s).
Definition ISA.hpp:89
@ TAN
tan()
Definition ISA.hpp:34
@ FLMODULO
Pop b, pop a, push a % b.
Definition ISA.hpp:27
@ FALSE_P
Push false.
Definition ISA.hpp:85
@ NEG_R
R[rA] = -R[rB].
Definition ISA.hpp:150
@ IDIVIDE
Pop b, pop a, push a / b.
Definition ISA.hpp:21
@ FLOR
Pop b, pop a, push a || b.
Definition ISA.hpp:44
@ STORE_VAR
Pop top of stack, store in variable slot.
Definition ISA.hpp:67
@ ILE_R
R[rA] = R[rB] <= R[rC].
Definition ISA.hpp:138
@ FLDIVIDE
Pop b, pop a, push a / b.
Definition ISA.hpp:26
@ POW_R
R[rA] = pow(R[rB], R[rC]).
Definition ISA.hpp:124
@ EXP
exp()
Definition ISA.hpp:31
@ IMODULO
Pop b, pop a, push a % b.
Definition ISA.hpp:22
@ FLAND_R
R[rA] = R[rB] && R[rC].
Definition ISA.hpp:140
@ JUMP_IF_FALSE
Jump if top of stack is false (pops value).
Definition ISA.hpp:62
@ INOT_EQUAL
Pop b, pop a, push a != b.
Definition ISA.hpp:48
@ FLEQ_R
R[rA] = R[rB] == R[rC].
Definition ISA.hpp:142
@ LOAD_VAR
Push variable value onto stack.
Definition ISA.hpp:68
@ IOR
Pop b, pop a, push a || b.
Definition ISA.hpp:42
@ EXP_R
R[rA] = exp(R[rB]).
Definition ISA.hpp:126
@ RETURN
Return from function.
Definition ISA.hpp:81
@ STORE_VAR_R
Store register to varible: variables[immediate] = R[rA].
Definition ISA.hpp:106
@ READLINE
Read line from input and push onto stack.
Definition ISA.hpp:73
@ IGT_R
R[rA] = R[rB] > R[rC].
Definition ISA.hpp:137
@ FLSUBTRACT
Pop b, pop a, push a - b.
Definition ISA.hpp:24
@ IADD_R
R[rA] = R[rB] + R[rC].
Definition ISA.hpp:113
@ IDIV_R
R[rA] = R[rB] / R[rC].
Definition ISA.hpp:116
@ FLLESS_EQUAL
Pop b, pop a, push a <= b.
Definition ISA.hpp:57
@ SYSTEM_ERR_R
Run shell command and get output: system_out(R[rA], R[rB]).
Definition ISA.hpp:159
@ FLMULTIPLY
Pop b, pop a, push a * b.
Definition ISA.hpp:25
@ ILESS_EQUAL
Pop b, pop a, push a <= b.
Definition ISA.hpp:51
@ HALT
Stop execution.
Definition ISA.hpp:75
@ PRINT_R
Print register: print(R[rA]).
Definition ISA.hpp:154
@ PRINTERROR
Pop top of stack and print to stderr.
Definition ISA.hpp:72
@ TAN_R
R[rA] = tan(R[rB]).
Definition ISA.hpp:129
@ FLSUB_R
R[rA] = R[rB] - R[rC].
Definition ISA.hpp:119
@ JUMP_BACK
Jump backwards (for loops).
Definition ISA.hpp:64
@ FLOR_R
R[rA] = R[rB] || R[rC].
Definition ISA.hpp:141
@ CALL
Call a user function: operand is index of function name in constants.
Definition ISA.hpp:77
@ INE_R
R[rA] = R[rB] != R[rC].
Definition ISA.hpp:135
@ NEGATE
Pop a, push -a.
Definition ISA.hpp:37
@ FLNE_R
R[rA] = R[rB] != R[rC].
Definition ISA.hpp:143
@ FLGREATER_THAN
Pop b, pop a, push a > b.
Definition ISA.hpp:56
@ ILESS_THAN
Pop b, pop a, push a < b.
Definition ISA.hpp:49
@ SIN_R
R[rA] = sin(R[rB]).
Definition ISA.hpp:127
@ IMPORT
Import a module: operand is index of module path in constants.
Definition ISA.hpp:74
@ LOAD_VAR_R
Load variable to register: R[rA] = variables[immediate].
Definition ISA.hpp:105
@ FLLESS_THAN
Pop b, pop a, push a < b.
Definition ISA.hpp:55
@ IOR_R
R[rA] = R[rB] || R[rC].
Definition ISA.hpp:133
@ ILT_R
R[rA] = R[rB] < R[rC].
Definition ISA.hpp:136
@ TRUE_P
Push true.
Definition ISA.hpp:84
@ COS
cos()
Definition ISA.hpp:33
@ POP
Pop top of stack.
Definition ISA.hpp:15
@ FLAND
Pop b, pop a, push a && b.
Definition ISA.hpp:43
@ IMOD_R
R[rA] = R[rB] % R[rC].
Definition ISA.hpp:117
@ PRINT
Pop top of stack and print.
Definition ISA.hpp:71
@ SYSTEM
Call a system function: operand is index of function name in constants.
Definition ISA.hpp:78
@ IEQ_R
R[rA] = R[rB] == R[rC].
Definition ISA.hpp:134
@ IGE_R
R[rA] = R[rB] >= R[rC].
Definition ISA.hpp:139
const std::unordered_map< std::string, OpCode > stringToOpCodeMap
Definition map.cpp:117
OpCode stringToOpCode(const std::string &str)
Definition map.cpp:136
std::string opCodeToString(OpCode op)
Definition map.cpp:126