Skip to content
This repository has been archived by the owner on Sep 28, 2023. It is now read-only.

Commit

Permalink
Handle translation of range/xrange with non-unit step
Browse files Browse the repository at this point in the history
Example:

Following python code:
```
for i in range(4,20,2):
    print i
```

Turns into the following rust code
```
for i in (4..20).step_by(2) {
    println!("{}", i);
}
```
  • Loading branch information
molysgaard committed Jul 11, 2020
1 parent 141752e commit b1c61a2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pyrs/transpiler.py
Expand Up @@ -140,9 +140,17 @@ def visit_Call(self, node):
return "String::from({0})".format(args)

elif fname == "range" or fname == "xrange":
if "," not in args: #one value range translates to 0..n
return "0.." + args
return args.replace(",","..")

vargs = list(map(self.visit, node.args))

if len(node.args)==1:
return '(0..{})'.format(vargs[0])
elif len(node.args)==2:
return '({}..{})'.format(vargs[0],vargs[1])
elif len(node.args)==3:
return '({}..{}).step_by({})'.format(vargs[0], vargs[1], vargs[2])
else:
raise Exception('encountered range() call with unknown parameters: range({})'.format(args))

elif fname == "len":
return "{0}.len()".format(self.visit(node.args[0]))
Expand Down

0 comments on commit b1c61a2

Please sign in to comment.