Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Dec 28, 2024
1 parent 38ec040 commit b713ae8
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 79 deletions.
30 changes: 18 additions & 12 deletions source/parser/helper.civet
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,42 @@ declareHelper := {
}
"""
], ";\n"]
stringRange(stringRangeRef): void
revRange(revRangeRef): void
state.prelude.push ["", [
preludeVar
stringRangeRef
ts [ ": (start: number, length: number, step: number) => string[]" ]
revRangeRef
ts [ ": (start: number, end: number) => number[]" ]
" "
"""
= (start, length, step) => {
= (start, end) => {
const length = start - end;
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i * step);
arr[i] = start - i;
}
return arr;
}
"""
], ";\n"]
revRange(revRangeRef): void
stringRange(stringRangeRef): void
state.prelude.push ["", [
preludeVar
revRangeRef
ts [ ": (start: number, end: number) => number[]" ]
stringRangeRef
ts [ ": (start: number, length: number, step: number) => string[]" ]
" "
"""
= (start, end) => {
const length = start - end;
= (start, length, step) => {
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = start - i;
if (step > 0) {
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);
}
}
return arr;
}
Expand Down
61 changes: 41 additions & 20 deletions test/compat/coffee-range.civet
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
{ testCase, evalsTo } from "../helper.civet"

describe "coffeeRange", ->
evalsTo """
"civet coffeeRange";
[0..100]
""", [0..100]

evalsTo """
"civet coffeeRange";
[0...100]
""", [0...100]

evalsTo """
"civet coffeeRange";
[100..0]
""", [100>=..>=0]

evalsTo """
"civet coffeeRange";
[100...0]
""", [100>=..>0]
it "coffeeRange behaves like range", ->
evalsTo """
"civet coffeeRange"
[0..100]
""", [0..100]

evalsTo """
"civet coffeeRange"
[0...100]
""", [0...100]

evalsTo """
"civet coffeeRange"
[100..0]
""", [100>=..>=0]

evalsTo """
"civet coffeeRange"
[100...0]
""", [100>=..>0]

evalsTo """
"civet coffeeRange"
[' '..'~']
""", [' '..'~']

evalsTo """
"civet coffeeRange"
[' '...'~']
""", [' '...'~']

evalsTo """
"civet coffeeRange"
['~'..' ']
""", ['~'>=..>=' ']

evalsTo """
"civet coffeeRange"
['~'...' ']
""", ['~'>=..>' ']

testCase """
[0..10]
---
"civet coffeeRange";
"civet coffeeRange"
[0..10]
---
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Expand Down
119 changes: 72 additions & 47 deletions test/range.civet
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,46 @@ describe "range", ->
range(0,256)
"""

closedAscendingArr := [0..100]
halfClosedAscendingArr := [0...100]
openAscendingArr := [1..<100]

evalsTo '[0..100]', closedAscendingArr
evalsTo '[0..<=100]', closedAscendingArr
evalsTo '[0<=..<=100]', closedAscendingArr
evalsTo '[0...100]', halfClosedAscendingArr
evalsTo '[0..<100]', halfClosedAscendingArr
evalsTo '[0<=..<100]', halfClosedAscendingArr
evalsTo '[0<..<100]', openAscendingArr

closedDescendingArr: number[] := [100>=..>=0]
halfClosedDescendingArr: number[] := [100>=..>0]
openDescendingArr: number[] := [99>=..>0]

evalsTo '[100>=..>=0]', closedDescendingArr
evalsTo '[100>=..>0]', halfClosedDescendingArr
evalsTo '[100>..>0]', openDescendingArr

stringClosedAscendingArr := [' '..'~']
stringHalfClosedAscendingArr := [' '...'~']
stringOpenAscendingArr := ['!'..<'~']

evalsTo "[' '..'~']", stringClosedAscendingArr
evalsTo "[' '..<='~']", stringClosedAscendingArr
evalsTo "[' '<=..<='~']", stringClosedAscendingArr
evalsTo "[' '...'~']", stringHalfClosedAscendingArr
evalsTo "[' '..<'~']", stringHalfClosedAscendingArr
evalsTo "[' '<=..<'~']", stringHalfClosedAscendingArr
evalsTo "[' '<..<'~']", stringOpenAscendingArr

stringClosedDescendingArr := ['~'>=..>=' ']
stringHalfClosedDescendingArr := ['~'>=..>' ']
stringOpenDescendingArr := ['}'>=..>' ']

evalsTo "['~'>=..>=' ']", stringClosedDescendingArr
evalsTo "['~'>=..>' ']", stringHalfClosedDescendingArr
evalsTo "['~'>..>' ']", stringOpenDescendingArr
it "evals to the correct ranges", ->
closedAscendingArr := i for let i = 0; i <= 100; i++ // [0..100]
halfClosedAscendingArr := i for let i = 0; i < 100; i++ // [0..<100]
openAscendingArr := i for let i = 1; i < 100; i++ // [0<..<100]

evalsTo '[0..100]', closedAscendingArr
evalsTo '[0..<=100]', closedAscendingArr
evalsTo '[0<=..<=100]', closedAscendingArr
evalsTo '[0...100]', halfClosedAscendingArr
evalsTo '[0..<100]', halfClosedAscendingArr
evalsTo '[0<=..<100]', halfClosedAscendingArr
evalsTo '[0<..<100]', openAscendingArr

closedDescendingArr := closedAscendingArr.reverse()
halfClosedDescendingArr := i for let i = 100; i > 0; i-- // [100>=..>0]
openDescendingArr := openAscendingArr.reverse()

evalsTo '[100>=..>=0]', closedDescendingArr
evalsTo '[100>=..>0]', halfClosedDescendingArr
evalsTo '[100>..>0]', openDescendingArr

stringClosedAscendingArr := [' '..'~']
stringHalfClosedAscendingArr := [' '...'~']
stringOpenAscendingArr := ['!'..<'~']

evalsTo "[' '..'~']", stringClosedAscendingArr
evalsTo "[' '..<='~']", stringClosedAscendingArr
evalsTo "[' '<=..<='~']", stringClosedAscendingArr
evalsTo "[' '...'~']", stringHalfClosedAscendingArr
evalsTo "[' '..<'~']", stringHalfClosedAscendingArr
evalsTo "[' '<=..<'~']", stringHalfClosedAscendingArr
evalsTo "[' '<..<'~']", stringOpenAscendingArr

stringClosedDescendingArr := stringClosedAscendingArr.reverse()
stringHalfClosedDescendingArr := ['~'>=..>' ']
stringOpenDescendingArr := stringOpenAscendingArr.reverse()

evalsTo "['~'>=..>=' ']", stringClosedDescendingArr
evalsTo "['~'>=..>' ']", stringHalfClosedDescendingArr
evalsTo "['~'>..>' ']", stringOpenDescendingArr

testCase """
[0..255]
Expand Down Expand Up @@ -215,8 +216,14 @@ describe "range", ->
var stringRange: (start: number, length: number, step: number) => string[] = (start, length, step) => {
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i * step);
if (step > 0) {
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);
}
}
return arr;
};
Expand Down Expand Up @@ -405,8 +412,14 @@ describe "range", ->
var stringRange: (start: number, length: number, step: number) => string[] = (start, length, step) => {
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i * step);
if (step > 0) {
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);
}
}
return arr;
};
Expand All @@ -421,8 +434,14 @@ describe "range", ->
var stringRange: (start: number, length: number, step: number) => string[] = (start, length, step) => {
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i * step);
if (step > 0) {
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);
}
}
return arr;
};
Expand All @@ -437,8 +456,14 @@ describe "range", ->
var stringRange: (start: number, length: number, step: number) => string[] = (start, length, step) => {
if (length <= 0) return [];
const arr = Array(length);
for (let i = 0; i < length; ++i) {
arr[i] = String.fromCharCode(start + i * step);
if (step > 0) {
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);
}
}
return arr;
};
Expand Down

0 comments on commit b713ae8

Please sign in to comment.