Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asteroid.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if exists("b:current_syntax")
endif

syn keyword basic with end do load let for function structure in is pattern
syn keyword basic throw this system data global return to step if not
syn keyword basic throw this system data global return to stride if not
syn keyword basic else or and

syn keyword delimeter escape
Expand Down
8 changes: 4 additions & 4 deletions asteroid/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,18 +643,18 @@ def compound(self):
elif tt == 'TO':
self.lexer.match('TO')
v2 = self.exp()
if self.lexer.peek().type == 'STEP':
self.lexer.match('STEP')
if self.lexer.peek().type == 'STRIDE':
self.lexer.match('STRIDE')
v3 = self.exp()
return ('raw-to-list',
('start', v),
('stop', v2),
('step', v3))
('stride', v3))
else:
return ('raw-to-list',
('start', v),
('stop', v2),
('step', ('integer', '1')))
('stride', ('integer', '1')))

else:
return v
Expand Down
2 changes: 1 addition & 1 deletion asteroid/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
(
(IS pattern) |
(IN exp) | /* exp has to be a list */
(TO exp (STEP exp)?) | /* list comprehension */
(TO exp (STRIDE exp)?) | /* list comprehension */
)?

logic_exp0
Expand Down
2 changes: 1 addition & 1 deletion asteroid/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
'pattern' : 'PATTERN',
'repeat' : 'REPEAT',
'return' : 'RETURN',
'step' : 'STEP',
'stride' : 'STRIDE',
'structure' : 'STRUCTURE',
'system' : 'SYSTEM',
'throw' : 'THROW',
Expand Down
2 changes: 1 addition & 1 deletion asteroid/modules/prologue.ast
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function range
with (start:%integer,stop:%integer) do
return [start to stop-1].
with (start:%integer,stop:%integer,inc:%integer) do
return [start to stop-1 step inc].
return [start to stop-1 stride inc].
with stop:%integer do
return [0 to stop-1].
end
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion asteroid/test-suites/action-tests/test073.ast
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
load system math.

let a = [20 to 30].
let array = a @[1 to a @length()-1 step 2] .
let array = a @[1 to a @length()-1 stride 2] .
io @println array.

assert (array == [21,23,25,27,29]).
2 changes: 1 addition & 1 deletion asteroid/test-suites/regression-tests/test044.ast
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

load system io.
load "util".
let slice = [4 to 0 step -1].
let slice = [4 to 0 stride 1].
io @println slice.
assert(slice is [4,3,2,1,0]).
2 changes: 1 addition & 1 deletion asteroid/test-suites/regression-tests/test090.ast
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ load system io.
load "util".

let a = [0,1,2,3].
let b = a@[0 to 3 step 2].
let b = a@[0 to 3 stride 2].
io @println b.
assert (b is [0,2]).
2 changes: 1 addition & 1 deletion asteroid/test-suites/regression-tests/test091.ast
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
load system io.
load "util".

let y = [0 to 10 step 2].
let y = [0 to 10 stride 2].
let z = 4 in y.
io @println z.
assert z.
4 changes: 2 additions & 2 deletions asteroid/test-suites/ug-programs/prgm-011.ast
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
load system io.

-- build a list of odd values
let a = [1 to 10 step 2]. -- list comprehension
let a = [1 to 10 stride 2]. -- list comprehension
io @println ("list: " + a).

-- reverse the list using a slice computed as comprehension
let slice = [4 to 0 step -1]. -- list comprehension
let slice = [4 to 0 stride 1]. -- list comprehension
let b = a @slice.
io @println ("reversed list: " + b).
2 changes: 1 addition & 1 deletion asteroid/test-suites/ug-programs/prgm-022.ast
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load system io.

for i in 0 to 10 step 2 do
for i in 0 to 10 stride 2 do
io @println i.
end
51 changes: 32 additions & 19 deletions asteroid/walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,41 +1437,54 @@ def to_list_exp(node):
(TOLIST,
(START, start),
(STOP, stop),
(STEP, step)) = node
(STRIDE, stride)) = node

assert_match(TOLIST, 'to-list')
assert_match(START, 'start')
assert_match(STOP, 'stop')
assert_match(STEP, 'step')
assert_match(STRIDE, 'stride')

(START_TYPE, start_val, *_) = walk(start)
(STOP_TYPE, stop_val, *_) = walk(stop)
(STEP_TYPE, step_val, *_) = walk(step)
(STRIDE_TYPE, stride_val, *_) = walk(stride)

if START_TYPE != 'integer' or STOP_TYPE != 'integer' or STEP_TYPE != 'integer':
raise ValueError("only integer values allowed in start, stop, or step")
if START_TYPE != 'integer' or STOP_TYPE != 'integer' or STRIDE_TYPE != 'integer':
raise ValueError("only integer values allowed in start, stop, or stride")

out_list_val = []

# TODO: check out the behavior with step -1 -- is this what we want?
# the behavior is start and stop included
if int(step_val) > 0: # generate the list
# If our stride val is > 0
if int(stride_val) > 0: # generate the list
# Get the [i]nitial inde[x] and [e]nd inde[x]
ix = int(start_val)
while ix <= int(stop_val):
out_list_val.append(('integer', ix))
ix += int(step_val)
ex = int(stop_val)

elif int(step_val) == 0: # error
raise ValueError("step size of 0 not supported")
# Get the stride_val
stride_val = int(stride_val)

elif int(step_val) < 0: # generate the list
ix = int(start_val)
while ix >= int(stop_val):
out_list_val.append(('integer', ix))
ix += int(step_val)
# Change the direction of the stride value based on the
# ends of the range. I.e. 5->1 has an implicit direction
# of -1, 1->5 has a direction of +1
direction = (1 if ix < ex else -1)
stride_val *= direction

# We need to modify the ending index to acccount for python
# ranges. For example, for 1->10 we want range(1, 10 + 1).
# Or, for the opposite, we want range(10, 1 - 1) to give
# us the full inclusive range. Thus, we can just add our
# direction
new_ex = ex + direction
for i in range(ix, new_ex, stride_val):
out_list_val.append( ('integer', i) )

elif int(stride_val) == 0: # error
raise ValueError("stride size of 0 not supported")

elif int(stride_val) < 0: # generate the list
raise ValueError("negative stride sizes are not supported")

else:
raise ValueError("{} not a valid step value".format(step_val))
raise ValueError("{} not a valid stride value".format(stride_val))

return ('list', out_list_val)

Expand Down
Loading