Skip to content

Commit

Permalink
More of cognitive reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Mar 11, 2024
1 parent d86fb7b commit f16023a
Showing 1 changed file with 29 additions and 37 deletions.
66 changes: 29 additions & 37 deletions src/preprocessor/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,51 +253,43 @@ export default function preprocessor (Program: CONTRACT) : string {
function extractArgs (fnArgString: string, needle: number, line: string): { argArray: string[], endPosition: number} {
const argArray : string [] = []
let currArg: string = ''
let pLevel = 0
const Stream = new StringStream(fnArgString)
Stream.index = needle - 1
let stage = 0
Stream.index = fnArgString.indexOf('(', needle)
let pLevel = 1
while (!Stream.EOF()) {
const current = Stream.advance()
switch (stage) {
case 0:
if (current.char === '(') {
pLevel = 1
stage = 1
}
if (current.char === '(') {
pLevel++
currArg += current.char
continue
case 1:
if (current.char === '(') {
pLevel++
break
}
if (current.char === ')') {
pLevel--
if (pLevel !== 0) {
currArg += current.char
continue
}
if (current.char === ')') {
pLevel--
if (pLevel !== 0) {
break
}
// end of arguments
const endArg = currArg.trim()
if (endArg.length === 0 && argArray.length !== 0) {
throw new Error(Program.Context.formatError(line, 'Found empty argument on macro declaration.'))
}
if (endArg.length !== 0) {
argArray.push(currArg.trim())
}
return {
argArray,
endPosition: Stream.index + 1
}
// end of arguments
const endArg = currArg.trim()
if (endArg.length === 0 && argArray.length !== 0) {
throw new Error(Program.Context.formatError(line, 'Found empty argument on macro declaration.'))
}
if (current.char === ',' && pLevel === 1) {
const newArg = currArg.trim()
if (newArg.length === 0) {
throw new Error(Program.Context.formatError(line, 'Found empty argument on macro declaration.'))
}
if (endArg.length !== 0) {
argArray.push(currArg.trim())
currArg = ''
continue
}
return {
argArray,
endPosition: Stream.index + 1
}
}
if (current.char === ',' && pLevel === 1) {
const newArg = currArg.trim()
if (newArg.length === 0) {
throw new Error(Program.Context.formatError(line, 'Found empty argument on macro declaration.'))
}
argArray.push(currArg.trim())
currArg = ''
continue
}
currArg += current.char
}
Expand Down

0 comments on commit f16023a

Please sign in to comment.