ref: 4415dde6d3345b21d49848fd7e76b2860f58512e
parent: 023f5eca58471db968aee3de7729c1c9b0540952
author: aiju <devnull@localhost>
date: Thu Dec 6 05:56:32 EST 2018
forp: change indexing to verilog-like semantics
--- a/sys/man/1/forp
+++ b/sys/man/1/forp
@@ -89,8 +89,9 @@
The valid operators are listed below, in decreasing precedence. Note that logical operations treat all non-zero values as 1, whereas bitwise operators operate on all bits independently.
.TP "\w'\fL<\fR, \fL<=\fR, \fL>\fR, \fL>=\fR 'u"
\fL[]\fR
-Array indexing. The syntax is \fIvar\fL[\fIidx\fL:\fIn\fR] to address \fIn\fR bits with the least-significant bit at \fIidx\fR.
-Omiting \fL:\fIn\fR addresses a single bit.
+Array indexing. The syntax is \fIvar\fL[\fIa\fL:\fIb\fR], with \fIa\fR denoting the MSB and \fIb\fR denoting the LSB.
+Omiting \fL:\fIb\fR addresses a single bit.
+The result is always treated as unsigned.
.TP
\fL!\fR, \fL~\fR, \fL+\fR, \fL-\fR
(Unary operators) Logical and bitwise "not", unary plus (no-op), arithmetic negation. Because of promotion, \fL~\fR and \fL-\fR operate beyond the width of variables.
--- a/sys/src/cmd/forp/cvt.c
+++ b/sys/src/cmd/forp/cvt.c
@@ -251,9 +251,9 @@
{
int i, j, k, s;
- j = mptoi(n2->num);
- if(n3 == nil) k = j;
- else k = mptoi(n3->num);
+ k = mptoi(n2->num);
+ if(n3 == nil) j = k;
+ else j = mptoi(n3->num);
if(j > k){
nodevars(rn, 1);
return;
@@ -492,7 +492,7 @@
case ASTIDX:
if(n->n2->type != ASTNUM || n->n3 != nil && n->n3->type != ASTNUM)
error(n, "non-constant in indexing expression");
- convert(n->n1, (n->n3 != nil ? mptoi(n->n3->num) : mptoi(n->n2->num)) + 1);
+ convert(n->n1, n->n3 != nil ? mptoi(n->n3->num) - mptoi(n->n2->num) + 1 : 1);
opidx(n, n->n1, n->n2, n->n3);
break;
case ASTTERN: