Skip to content

Commit 2b3bc5a

Browse files
authored
Enumerate Instruction Set conversions (#255)
* Enumerate Instruction Set conversions * Replace magic numbers in reverse parsing logic
1 parent 857f4e9 commit 2b3bc5a

File tree

5 files changed

+212
-77
lines changed

5 files changed

+212
-77
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export {
6060
Right,
6161
Either,
6262
ContractBlockParameters,
63+
InstructionType,
64+
INSTRUCTION_STRING_TO_TYPE,
6365
} from './modules/types.js'
6466

6567
export {

src/modules/types.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,56 @@ export const truMatchArray: string[] = ['+=', '-=', '*=', '/=', '=']
560560
*/
561561
export const operandArray: string[] = ['PLH', 'N', 'PLHM', 'TRU', 'TRUM']
562562

563+
/**
564+
* Instruction type enumeration for convertASTToInstructionSet operations
565+
*/
566+
export enum InstructionType {
567+
NUMERIC_LITERAL = 0, // 'N' - Numeric literal value
568+
NOT = 1, // 'NOT' - Logical NOT operation
569+
PLACEHOLDER = 2, // 'PLH' - Placeholder reference
570+
ASSIGNMENT = 3, // '=' - Assignment operation
571+
MAPPED_PLACEHOLDER = 4, // 'PLHM' - Mapped placeholder reference
572+
ADDITION = 5, // '+' - Addition operation
573+
SUBTRACTION = 6, // '-' - Subtraction operation
574+
MULTIPLICATION = 7, // '*' - Multiplication operation
575+
DIVISION = 8, // '/' - Division operation
576+
LESS_THAN = 9, // '<' - Less than comparison
577+
GREATER_THAN = 10, // '>' - Greater than comparison
578+
EQUAL = 11, // '==' - Equality comparison
579+
AND = 12, // 'AND' - Logical AND operation
580+
OR = 13, // 'OR' - Logical OR operation
581+
GREATER_EQUAL = 14, // '>=' - Greater than or equal comparison
582+
LESS_EQUAL = 15, // '<=' - Less than or equal comparison
583+
NOT_EQUAL = 16, // '!=' - Not equal comparison
584+
TRACKER_UPDATE = 17, // 'TRU' - Tracker update operation
585+
MAPPED_TRACKER_UPDATE = 18, // 'TRUM' - Mapped tracker update operation
586+
}
587+
588+
/**
589+
* Mapping from instruction strings to their corresponding instruction type codes
590+
*/
591+
export const INSTRUCTION_STRING_TO_TYPE: Record<string, InstructionType> = {
592+
'N': InstructionType.NUMERIC_LITERAL,
593+
'NOT': InstructionType.NOT,
594+
'PLH': InstructionType.PLACEHOLDER,
595+
'=': InstructionType.ASSIGNMENT,
596+
'PLHM': InstructionType.MAPPED_PLACEHOLDER,
597+
'+': InstructionType.ADDITION,
598+
'-': InstructionType.SUBTRACTION,
599+
'*': InstructionType.MULTIPLICATION,
600+
'/': InstructionType.DIVISION,
601+
'<': InstructionType.LESS_THAN,
602+
'>': InstructionType.GREATER_THAN,
603+
'==': InstructionType.EQUAL,
604+
'AND': InstructionType.AND,
605+
'OR': InstructionType.OR,
606+
'>=': InstructionType.GREATER_EQUAL,
607+
'<=': InstructionType.LESS_EQUAL,
608+
'!=': InstructionType.NOT_EQUAL,
609+
'TRU': InstructionType.TRACKER_UPDATE,
610+
'TRUM': InstructionType.MAPPED_TRACKER_UPDATE,
611+
} as const
612+
563613
/**
564614
* Parameter type enumeration
565615
*/

src/parsing/parser.ts

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
ForeignCallDefinition,
1515
ForeignCallEncodedIndex,
1616
InstructionSet,
17+
InstructionType,
18+
INSTRUCTION_STRING_TO_TYPE,
1719
MappedTrackerDefinition,
1820
matchArray,
1921
Maybe,
@@ -543,54 +545,28 @@ export function buildTrackerList(condition: string): string[] {
543545
return [...trNames, ...truNames]
544546
}
545547

548+
/**
549+
* Gets the instruction type code for a given instruction string.
550+
* This function centralizes the mapping from string instructions to their numeric equivalents
551+
* used in the instruction set processing.
552+
*
553+
* @param instruction - The instruction string to convert (e.g., 'N', 'PLH', '+', 'AND').
554+
* @returns The numeric instruction type code, or the original instruction if not a string operation.
555+
*/
556+
function getInstructionType(instruction: string | number | BigInt): number | string | BigInt {
557+
if (typeof instruction === 'string' && instruction in INSTRUCTION_STRING_TO_TYPE) {
558+
return INSTRUCTION_STRING_TO_TYPE[instruction]
559+
}
560+
return instruction
561+
}
562+
546563
/**
547564
* Cleans the instruction set by replacing string representations of operators with their numeric equivalents.
548565
*
549566
* @param instructionSet - The instruction set to clean.
550567
*/
551568
export function cleanInstructionSet(instructionSet: InstructionSet): number[] {
552-
return instructionSet.map((instruction) => {
553-
if (instruction == 'N') {
554-
return 0
555-
} else if (instruction == 'NOT') {
556-
return 1
557-
} else if (instruction == 'PLH') {
558-
return 2
559-
} else if (instruction == '=') {
560-
return 3
561-
} else if (instruction == 'PLHM') {
562-
return 4
563-
} else if (instruction == '+') {
564-
return 5
565-
} else if (instruction == '-') {
566-
return 6
567-
} else if (instruction == '*') {
568-
return 7
569-
} else if (instruction == '/') {
570-
return 8
571-
} else if (instruction == '<') {
572-
return 9
573-
} else if (instruction == '>') {
574-
return 10
575-
} else if (instruction == '==') {
576-
return 11
577-
} else if (instruction == 'AND') {
578-
return 12
579-
} else if (instruction == 'OR') {
580-
return 13
581-
} else if (instruction == '>=') {
582-
return 14
583-
} else if (instruction == '<=') {
584-
return 15
585-
} else if (instruction == '!=') {
586-
return 16
587-
} else if (instruction == 'TRU') {
588-
return 17
589-
} else if (instruction == 'TRUM') {
590-
return 18
591-
}
592-
return instruction
593-
}) as number[]
569+
return instructionSet.map((instruction) => getInstructionType(instruction)) as number[]
594570
}
595571

596-
export { parseFunctionArguments }
572+
export { parseFunctionArguments, getInstructionType }

src/parsing/reverse-parsing-logic.ts

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ForeignCallOnChain,
1414
TrackerMetadataStruct,
1515
EffectOnChain,
16+
InstructionType,
1617
} from '../modules/types'
1718
import {
1819
CallingFunctionJSON,
@@ -74,49 +75,61 @@ export function reverseParseInstructionSet(
7475
if (currentAction == -1) {
7576
currentAction = Number(instruction)
7677
switch (currentAction) {
77-
case 0:
78+
case InstructionType.NUMERIC_LITERAL:
7879
currentActionIndex = 1
7980
break
80-
case 1:
81+
case InstructionType.NOT:
8182
currentActionIndex = 1
8283
break
83-
case 2:
84+
case InstructionType.PLACEHOLDER:
8485
currentActionIndex = 1
8586
break
86-
case 3:
87+
case InstructionType.ASSIGNMENT:
8788
currentActionIndex = 2
8889
break
89-
case 4:
90+
case InstructionType.MAPPED_PLACEHOLDER:
9091
currentActionIndex = 2
9192
break
92-
case 5:
93+
case InstructionType.ADDITION:
9394
currentActionIndex = 2
9495
break
95-
case 6:
96+
case InstructionType.SUBTRACTION:
9697
currentActionIndex = 2
9798
break
98-
case 7:
99+
case InstructionType.MULTIPLICATION:
99100
currentActionIndex = 2
100101
break
101-
case 8:
102+
case InstructionType.DIVISION:
102103
currentActionIndex = 2
103104
break
104-
case 9:
105+
case InstructionType.LESS_THAN:
105106
currentActionIndex = 2
106107
break
107-
case 10:
108+
case InstructionType.GREATER_THAN:
108109
currentActionIndex = 2
109110
break
110-
case 11:
111+
case InstructionType.EQUAL:
111112
currentActionIndex = 2
112113
break
113-
case 12:
114+
case InstructionType.AND:
114115
currentActionIndex = 2
115116
break
116-
case 17:
117+
case InstructionType.OR:
118+
currentActionIndex = 2
119+
break
120+
case InstructionType.GREATER_EQUAL:
121+
currentActionIndex = 2
122+
break
123+
case InstructionType.LESS_EQUAL:
124+
currentActionIndex = 2
125+
break
126+
case InstructionType.NOT_EQUAL:
127+
currentActionIndex = 2
128+
break
129+
case InstructionType.TRACKER_UPDATE:
117130
currentActionIndex = 3
118131
break
119-
case 18:
132+
case InstructionType.MAPPED_TRACKER_UPDATE:
120133
currentActionIndex = 4
121134
break
122135
default:
@@ -125,7 +138,7 @@ export function reverseParseInstructionSet(
125138
}
126139
} else {
127140
switch (currentAction) {
128-
case 0:
141+
case InstructionType.NUMERIC_LITERAL:
129142
var found = false
130143
for (var raw of stringReplacements) {
131144
if (raw.instructionSetIndex == instructionNumber && raw.type == rawDataIndex) {
@@ -145,7 +158,7 @@ export function reverseParseInstructionSet(
145158
}
146159
currentMemAddress += 1
147160
break
148-
case 1:
161+
case InstructionType.NOT:
149162
for (var memValue of memAddressesMap) {
150163
if (memValue.memAddr == instruction) {
151164
currentInstructionValues.push(memValue.value)
@@ -162,7 +175,7 @@ export function reverseParseInstructionSet(
162175
currentInstructionValues = []
163176
}
164177
break
165-
case 2:
178+
case InstructionType.PLACEHOLDER:
166179
memAddressesMap.push({
167180
memAddr: currentMemAddress,
168181
value: placeHolderArray[instruction].split('~')[0],
@@ -171,7 +184,7 @@ export function reverseParseInstructionSet(
171184
currentMemAddress += 1
172185
retVal = placeHolderArray[instruction].split('~')[0]
173186
break
174-
case 3:
187+
case InstructionType.ASSIGNMENT:
175188
retVal = arithmeticOperatorReverseInterpretation(
176189
instruction,
177190
currentMemAddress,
@@ -185,7 +198,7 @@ export function reverseParseInstructionSet(
185198
currentInstructionValues = []
186199
}
187200
break
188-
case 4:
201+
case InstructionType.MAPPED_PLACEHOLDER:
189202
if (currentActionIndex == 2) {
190203
valueIndex = instruction
191204
} else {
@@ -207,7 +220,7 @@ export function reverseParseInstructionSet(
207220
}
208221

209222
break
210-
case 5:
223+
case InstructionType.ADDITION:
211224
retVal = arithmeticOperatorReverseInterpretation(
212225
instruction,
213226
currentMemAddress,
@@ -221,7 +234,7 @@ export function reverseParseInstructionSet(
221234
currentInstructionValues = []
222235
}
223236
break
224-
case 6:
237+
case InstructionType.SUBTRACTION:
225238
retVal = arithmeticOperatorReverseInterpretation(
226239
instruction,
227240
currentMemAddress,
@@ -235,7 +248,7 @@ export function reverseParseInstructionSet(
235248
currentInstructionValues = []
236249
}
237250
break
238-
case 7:
251+
case InstructionType.MULTIPLICATION:
239252
retVal = arithmeticOperatorReverseInterpretation(
240253
instruction,
241254
currentMemAddress,
@@ -249,7 +262,7 @@ export function reverseParseInstructionSet(
249262
currentInstructionValues = []
250263
}
251264
break
252-
case 8:
265+
case InstructionType.DIVISION:
253266
retVal = arithmeticOperatorReverseInterpretation(
254267
instruction,
255268
currentMemAddress,
@@ -263,7 +276,7 @@ export function reverseParseInstructionSet(
263276
currentInstructionValues = []
264277
}
265278
break
266-
case 9:
279+
case InstructionType.LESS_THAN:
267280
retVal = arithmeticOperatorReverseInterpretation(
268281
instruction,
269282
currentMemAddress,
@@ -277,7 +290,7 @@ export function reverseParseInstructionSet(
277290
currentInstructionValues = []
278291
}
279292
break
280-
case 10:
293+
case InstructionType.GREATER_THAN:
281294
retVal = arithmeticOperatorReverseInterpretation(
282295
instruction,
283296
currentMemAddress,
@@ -291,7 +304,7 @@ export function reverseParseInstructionSet(
291304
currentInstructionValues = []
292305
}
293306
break
294-
case 11:
307+
case InstructionType.EQUAL:
295308
retVal = arithmeticOperatorReverseInterpretation(
296309
instruction,
297310
currentMemAddress,
@@ -306,7 +319,7 @@ export function reverseParseInstructionSet(
306319
currentInstructionValues = []
307320
}
308321
break
309-
case 12:
322+
case InstructionType.AND:
310323
retVal = logicalOperatorReverseInterpretation(
311324
instruction,
312325
currentMemAddress,
@@ -320,7 +333,7 @@ export function reverseParseInstructionSet(
320333
currentInstructionValues = []
321334
}
322335
break
323-
case 13:
336+
case InstructionType.OR:
324337
retVal = logicalOperatorReverseInterpretation(
325338
instruction,
326339
currentMemAddress,
@@ -334,7 +347,7 @@ export function reverseParseInstructionSet(
334347
currentInstructionValues = []
335348
}
336349
break
337-
case 14:
350+
case InstructionType.GREATER_EQUAL:
338351
retVal = arithmeticOperatorReverseInterpretation(
339352
instruction,
340353
currentMemAddress,
@@ -348,7 +361,7 @@ export function reverseParseInstructionSet(
348361
currentInstructionValues = []
349362
}
350363
break
351-
case 15:
364+
case InstructionType.LESS_EQUAL:
352365
retVal = arithmeticOperatorReverseInterpretation(
353366
instruction,
354367
currentMemAddress,
@@ -362,7 +375,7 @@ export function reverseParseInstructionSet(
362375
currentInstructionValues = []
363376
}
364377
break
365-
case 16:
378+
case InstructionType.NOT_EQUAL:
366379
retVal = arithmeticOperatorReverseInterpretation(
367380
instruction,
368381
currentMemAddress,
@@ -376,8 +389,8 @@ export function reverseParseInstructionSet(
376389
currentInstructionValues = []
377390
}
378391
break
379-
case 17:
380-
case 18:
392+
case InstructionType.TRACKER_UPDATE:
393+
case InstructionType.MAPPED_TRACKER_UPDATE:
381394
if (!truUpdated) {
382395
var str = memAddressesMap[currentMemAddress - 1].value
383396
var memVal: any = str.replace('TR:', 'TRU:')

0 commit comments

Comments
 (0)