Skip to content

Commit

Permalink
split revStringRange as its own helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Dec 29, 2024
1 parent 66daea1 commit 95683a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 63 deletions.
4 changes: 1 addition & 3 deletions source/parser/for.civet
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ function processRangeExpression(start: ASTNode, ws1: ASTNode, range: RangeDots,
. "]"
else
children =
. getHelperRef "stringRange"
. getHelperRef startCode <= endCode ? "stringRange" : "revStringRange"
. "("
. startCode.toString()
. ", "
. length.toString()
. ", "
. (startCode <= endCode).toString()
. ")"
children.unshift range.error if range.error?
else if startValue <? "number" and endValue <? "number"
Expand Down
31 changes: 21 additions & 10 deletions source/parser/helper.civet
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,31 @@ declareHelper := {
state.prelude.push ["", [
preludeVar
stringRangeRef
ts [ ": (start: number, length: number, ascending: boolean) => string[]" ]
ts [ ": (start: number, length: number) => string[]" ]
" "
"""
= (start, length, ascending) => {
= (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
if (ascending) {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
} else {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
return arr;
}
"""
], ";\n"]
revStringRange(revStringRangeRef): void
state.prelude.push ["", [
preludeVar
revStringRangeRef
ts [ ": (start: number, length: number) => string[]" ]
" "
"""
= (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
return arr;
}
Expand Down
70 changes: 20 additions & 50 deletions test/range.civet
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,15 @@ describe "range", ->
---
[' '..'~']
---
var stringRange: (start: number, length: number, ascending: boolean) => string[] = (start, length, ascending) => {
var stringRange: (start: number, length: number) => string[] = (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
if (ascending) {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
} else {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
return arr;
};
stringRange(32, 95, true)
stringRange(32, 95)
"""

testCase """
Expand Down Expand Up @@ -409,87 +403,63 @@ describe "range", ->
---
[' '..<'~']
---
var stringRange: (start: number, length: number, ascending: boolean) => string[] = (start, length, ascending) => {
var stringRange: (start: number, length: number) => string[] = (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
if (ascending) {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
} else {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
return arr;
};
stringRange(32, 94, true)
stringRange(32, 94)
"""

testCase """
..> long with strings
---
['~'..>' ']
---
var stringRange: (start: number, length: number, ascending: boolean) => string[] = (start, length, ascending) => {
var revStringRange: (start: number, length: number) => string[] = (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
if (ascending) {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
} else {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
return arr;
};
stringRange(126, 94, false)
revStringRange(126, 94)
"""

testCase """
<.. long with strings
---
[' '<..'~']
---
var stringRange: (start: number, length: number, ascending: boolean) => string[] = (start, length, ascending) => {
var stringRange: (start: number, length: number) => string[] = (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
if (ascending) {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
} else {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
return arr;
};
stringRange(33, 94, true)
stringRange(33, 94)
"""

testCase """
<..< long with strings
---
[' '<..<'~']
---
var stringRange: (start: number, length: number, ascending: boolean) => string[] = (start, length, ascending) => {
var stringRange: (start: number, length: number) => string[] = (start, length) => {
if (length <= 0) return [];
const arr = Array(length);
if (ascending) {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
} else {
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start - i);
}
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i);
}
return arr;
};
stringRange(33, 93, true)
stringRange(33, 93)
"""

throws """
Expand Down

0 comments on commit 95683a6

Please sign in to comment.