Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make arith overload form more concise in operators module.

Update sequence arithmetic.
Add updateAssign overload for sequence arith.
Update compiler to handle updateAssignment of variadic arith correctly.
 - cat updateassign handled as special case.
Update tests to pass using new arith.
  • Loading branch information...
commit d8534b55e512ed201eeac969ab458b0f472778c2 1 parent c25eb50
Jeremy Clifford authored
View
64 compiler/src/codegen.cpp
@@ -3626,10 +3626,18 @@ bool codegenStatement(StatementPtr stmt,
CallPtr call = new Call(
operator_expr_indexUpdateAssign(), new ExprList()
);
- call->parenArgs->add(updateOperatorExpr(x->op));
- call->parenArgs->add(y->expr);
- call->parenArgs->add(y->args);
- call->parenArgs->add(x->right);
+ if (x->op == UPDATE_CAT) {
+ call->parenArgs->add(operator_expr_cat());
+ call->parenArgs->add(y->expr);
+ call->parenArgs->add(y->args);
+ call->parenArgs->add(x->right);
+ } else {
+ call->parenArgs->add(operator_expr_arith());
+ call->parenArgs->add(y->expr);
+ call->parenArgs->add(y->args);
+ call->parenArgs->add(updateOperatorExpr(x->op));
+ call->parenArgs->add(x->right);
+ }
return codegenStatement(new ExprStatement(call.ptr()), env, ctx);
}
}
@@ -3638,11 +3646,20 @@ bool codegenStatement(StatementPtr stmt,
CallPtr call = new Call(
operator_expr_staticIndexUpdateAssign(), new ExprList()
);
- call->parenArgs->add(updateOperatorExpr(x->op));
- call->parenArgs->add(y->expr);
- ValueHolderPtr vh = sizeTToValueHolder(y->index);
- call->parenArgs->add(new StaticExpr(new ObjectExpr(vh.ptr())));
- call->parenArgs->add(x->right);
+ if (x->op == UPDATE_CAT) {
+ call->parenArgs->add(operator_expr_cat());
+ call->parenArgs->add(y->expr);
+ ValueHolderPtr vh = sizeTToValueHolder(y->index);
+ call->parenArgs->add(new StaticExpr(new ObjectExpr(vh.ptr())));
+ call->parenArgs->add(x->right);
+ } else {
+ call->parenArgs->add(operator_expr_arith());
+ call->parenArgs->add(y->expr);
+ ValueHolderPtr vh = sizeTToValueHolder(y->index);
+ call->parenArgs->add(new StaticExpr(new ObjectExpr(vh.ptr())));
+ call->parenArgs->add(updateOperatorExpr(x->op));
+ call->parenArgs->add(x->right);
+ }
return codegenStatement(new ExprStatement(call.ptr()), env, ctx);
}
else if (x->left->exprKind == FIELD_REF) {
@@ -3652,17 +3669,32 @@ bool codegenStatement(StatementPtr stmt,
CallPtr call = new Call(
operator_expr_fieldRefUpdateAssign(), new ExprList()
);
- call->parenArgs->add(updateOperatorExpr(x->op));
- call->parenArgs->add(y->expr);
- call->parenArgs->add(new ObjectExpr(y->name.ptr()));
- call->parenArgs->add(x->right);
+ if (x->op == UPDATE_CAT) {
+ call->parenArgs->add(operator_expr_cat());
+ call->parenArgs->add(y->expr);
+ call->parenArgs->add(new ObjectExpr(y->name.ptr()));
+ call->parenArgs->add(x->right);
+ } else {
+ call->parenArgs->add(operator_expr_arith());
+ call->parenArgs->add(y->expr);
+ call->parenArgs->add(new ObjectExpr(y->name.ptr()));
+ call->parenArgs->add(updateOperatorExpr(x->op));
+ call->parenArgs->add(x->right);
+ }
return codegenStatement(new ExprStatement(call.ptr()), env, ctx);
}
}
CallPtr call = new Call(operator_expr_updateAssign(), new ExprList());
- call->parenArgs->add(updateOperatorExpr(x->op));
- call->parenArgs->add(x->left);
- call->parenArgs->add(x->right);
+ if (x->op == UPDATE_CAT) {
+ call->parenArgs->add(operator_expr_cat());
+ call->parenArgs->add(x->left);
+ call->parenArgs->add(x->right);
+ } else {
+ call->parenArgs->add(operator_expr_arith());
+ call->parenArgs->add(x->left);
+ call->parenArgs->add(updateOperatorExpr(x->op));
+ call->parenArgs->add(x->right);
+ }
return codegenStatement(new ExprStatement(call.ptr()), env, ctx);
}
View
13 compiler/src/desugar.cpp
@@ -120,13 +120,12 @@ ExprPtr desugarVariadicOp(VariadicOpPtr x) {
ExprPtr updateOperatorExpr(int op) {
switch (op) {
- case UPDATE_ADD : return operator_expr_add();
- case UPDATE_SUBTRACT : return operator_expr_subtract();
- case UPDATE_MULTIPLY : return operator_expr_multiply();
- case UPDATE_DIVIDE : return operator_expr_divide();
- case UPDATE_QUOTIENT : return operator_expr_quotient();
- case UPDATE_REMAINDER : return operator_expr_remainder();
- case UPDATE_CAT : return operator_expr_cat();
+ case UPDATE_ADD : return operator_expr_add();
+ case UPDATE_SUBTRACT : return operator_expr_subtract();
+ case UPDATE_MULTIPLY : return operator_expr_multiply();
+ case UPDATE_DIVIDE : return operator_expr_divide();
+ case UPDATE_QUOTIENT : return operator_expr_quotient();
+ case UPDATE_REMAINDER : return operator_expr_remainder();
default :
assert(false);
return NULL;
View
13 compiler/src/evaluator.cpp
@@ -2114,9 +2114,16 @@ TerminationPtr evalStatement(StatementPtr stmt,
if (pvLeft->isTemp)
error(x->left, "cannot assign to a temporary");
CallPtr call = new Call(operator_expr_updateAssign(), new ExprList());
- call->parenArgs->add(updateOperatorExpr(x->op));
- call->parenArgs->add(x->left);
- call->parenArgs->add(x->right);
+ if (x->op == UPDATE_CAT) {
+ call->parenArgs->add(operator_expr_cat());
+ call->parenArgs->add(x->left);
+ call->parenArgs->add(x->right);
+ } else {
+ call->parenArgs->add(operator_expr_arith());
+ call->parenArgs->add(x->left);
+ call->parenArgs->add(updateOperatorExpr(x->op));
+ call->parenArgs->add(x->right);
+ }
return evalStatement(new ExprStatement(call.ptr()), env, ctx);
}
View
3  compiler/src/parser.cpp
@@ -1290,7 +1290,8 @@ static bool updateOp(int &op) {
int p = save();
const char *s[] = {"+=", "-=", "*=", "/=","\\=", "%=", "++=", NULL};
const int ops[] = {UPDATE_ADD, UPDATE_SUBTRACT, UPDATE_MULTIPLY,
- UPDATE_DIVIDE, UPDATE_QUOTIENT, UPDATE_REMAINDER, UPDATE_CAT};
+ UPDATE_DIVIDE, UPDATE_QUOTIENT, UPDATE_REMAINDER,
+ UPDATE_CAT};
for (const char **a = s; *a; ++a) {
restore(p);
if (symbol(*a)) {
View
6 lib-clay/deques/deques.clay
@@ -44,11 +44,11 @@ record DequeCoordinate[T] (
dec(i.current);
}
-[T, I | Integer?(I)] overload updateAssign(static add, ref i: DequeCoordinate[T], n: I) {
+[T, I | Integer?(I)] overload updateAssign(static arith, ref i: DequeCoordinate[T],static add, n: I) {
i += PtrInt(n);
}
-[T] overload updateAssign(static add, ref i: DequeCoordinate[T], n: PtrInt) {
+[T] overload updateAssign(static arith, ref i: DequeCoordinate[T], static add, n: PtrInt) {
var offset = n + PtrInt(i.current - i.first);
if (offset >= 0 and offset < DequeBufferSSize(T))
i.current += n;
@@ -69,7 +69,7 @@ record DequeCoordinate[T] (
j += n;
}
-[T, I | Integer?(I)] overload updateAssign(static subtract, ref i: DequeCoordinate[T], n: I) {
+[T, I | Integer?(I)] overload updateAssign(static arith, ref i: DequeCoordinate[T], static subtract,n: I) {
i += -PtrInt(n);
}
View
96 lib-clay/operators/operators.clay
@@ -235,8 +235,8 @@ inline overload assign(ref dest:T, rvalue src:T) {
}
[F]
-inline overload updateAssign(static F, ref dest, forward src) {
- dest = F(dest, src);
+inline overload updateAssign(static F, ref dest, forward ..src) {
+ dest = F(dest, ..src);
}
inline overload notEquals?(a, b) = not equals?(a, b);
@@ -268,13 +268,13 @@ inline overload fieldRefAssign(dest, static FIELD, forward src) {
}
[F, FIELD]
-inline overload fieldRefUpdateAssign(static F, dest, static FIELD, forward src) {
- updateAssign(static F, fieldRef(dest, static FIELD), src);
+inline overload fieldRefUpdateAssign(static F, dest, static FIELD, forward ..src) {
+ updateAssign(static F, fieldRef(dest, static FIELD), ..src);
}
[F, FIELD, T | RValue?(fieldRef(typeToLValue(T), static FIELD))]
-inline overload fieldRefUpdateAssign(static F, dest:T, static FIELD, forward src) {
- fieldRefAssign(dest, static FIELD, F(fieldRef(dest, static FIELD), src));
+inline overload fieldRefUpdateAssign(static F, dest:T, static FIELD, forward ..src) {
+ fieldRefAssign(dest, static FIELD, F(fieldRef(dest, static FIELD), ..src));
}
[i]
@@ -283,12 +283,12 @@ inline overload staticIndexAssign(dest, static i, forward src) {
}
[F, i]
-inline overload staticIndexUpdateAssign(static F, dest, static i, forward src) {
- updateAssign(static F, staticIndex(dest, static i), src);
+inline overload staticIndexUpdateAssign(static F, dest, static i, forward ..src) {
+ updateAssign(static F, staticIndex(dest, static i), ..src);
}
[F, i, T | RValue?(staticIndex(typeToLValue(T), static i))]
-inline overload staticIndexUpdateAssign(static F, dest:T, static i, forward src) {
+inline overload staticIndexUpdateAssign(static F, dest:T, static i, forward ..src) {
staticIndexAssign(dest, static i, F(staticIndex(dest, static i), src));
}
@@ -307,11 +307,11 @@ inline overload indexAssign(dest, forward ..indicesAndSrc:T) {
inline overload indexUpdateAssign(static F, dest, forward ..indicesAndSrc:A) {
updateAssign(
static F,
- index(dest, ..withoutNthValue(
- static countValues(..A)-1,
+ index(dest, ..takeValues(
+ static countValues(..A)-2,
..indicesAndSrc
)),
- lastValue(..indicesAndSrc),
+ ..dropValues(static countValues(..A)-2,..indicesAndSrc),
);
}
@@ -322,7 +322,7 @@ inline overload indexUpdateAssign(static F, dest, forward ..indicesAndSrc:A) {
typeToLValue(T),
..mapValues(
typeToLValue,
- ..withoutNthValue(static countValues(..A)-1, ..A)
+ ..takeValues(static countValues(..A)-2, ..A)
)
)
)
@@ -330,13 +330,13 @@ inline overload indexUpdateAssign(static F, dest, forward ..indicesAndSrc:A) {
inline overload indexUpdateAssign(static F, dest:T, forward ..indicesAndSrc:A) {
indexAssign(
dest,
- ..withoutNthValue(static countValues(..A)-1, ..indicesAndSrc),
+ ..takeValues(static countValues(..A)-2, ..indicesAndSrc),
F(
index(
dest,
- ..withoutNthValue(static countValues(..A)-1, ..indicesAndSrc)
+ ..takeValues(static countValues(..A)-2, ..indicesAndSrc)
),
- lastValue(..indicesAndSrc)
+ ..dropValues(static countValues(..A)-2,..indicesAndSrc)
)
);
}
@@ -347,54 +347,22 @@ inline overload indexUpdateAssign(static F, dest:T, forward ..indicesAndSrc:A) {
//
overload arith(forward ..args)
- = arithAdd(..arithSub(..arithRem(..arithQuo(
- ..arithDiv(..arithMul(..args))))));
-
-arithAdd(forward a,fn,forward ..args) = forward a,fn,..arithAdd(..args);
-overload arithAdd(forward a,fn, forward b) = forward fn(a,b);
-overload arithAdd(forward a,static add,forward b, forward ..args)
- = forward ..arithAdd(add(a,b), ..args);
-overload arithAdd(forward a,static add,forward b)
- = forward add(a,b);
-overload arithAdd(forward a) = forward a;
-
-arithSub(forward a,fn,forward ..args) = forward a,fn,..arithSub(..args);
-overload arithSub(forward a,fn,forward b) = forward a,fn,b;
-overload arithSub(forward a,static subtract,forward b,forward ..args)
- = forward ..arithSub(subtract(a,b), ..args);
-overload arithSub(forward a,static subtract,forward b)
- = forward subtract(a,b);
-overload arithSub(forward a) = forward a;
-
-arithRem(forward a,fn,forward ..args) = forward a,fn,..arithRem(..args);
-overload arithRem(forward a,fn,forward b) = forward a,fn,b;
-overload arithRem(forward a,static remainder,forward b,forward ..args)
- = forward ..arithRem(remainder(a,b), ..args);
-overload arithRem(forward a,static remainder,forward b)
- = forward remainder(a,b);
-overload arithRem(forward a) = forward a;
-
-arithQuo(forward a,fn,forward ..args) = forward a,fn,..arithQuo(..args);
-overload arithQuo(forward a,fn,forward b) = forward a,fn,b;
-overload arithQuo(forward a,static quotient,forward b,forward ..args)
- = forward ..arithQuo(quotient(a,b), ..args);
-overload arithQuo(forward a,static quotient,forward b)
- = forward quotient(a,b);
-overload arithQuo(forward a) = forward a;
-
-arithDiv(forward a,fn,forward ..args) = forward a,fn,..arithDiv(..args);
-overload arithDiv(forward a,fn,forward b) = forward a,fn,b;
-overload arithDiv(forward a,static divide,forward b,forward ..args)
- = forward ..arithDiv(divide(a,b), ..args);
-overload arithDiv(forward a,static divide,forward b) = forward divide(a,b);
-overload arithDiv(forward a) = forward a;
-
-arithMul(forward a,fn,forward ..args) = forward a,fn,..arithMul(..args);
-overload arithMul(forward a,fn,forward b) = forward a,fn,b;
-overload arithMul(forward a,static multiply,forward b,forward ..args)
- = forward ..arithMul(multiply(a,b), ..args);
-overload arithMul(forward a,static multiply,forward b)
- = forward multiply(a,b);
+ = arithp(add,..arithp(subtract,..arithp(remainder,..arithp(quotient,
+ ..arithp(divide,..arithp(multiply,..args))))));
+
+private define arithp;
+
+overload arithp(p,forward a,fn,forward ..args)
+ = forward a,fn,..arithp(p,..args);
+overload arithp(p,forward a,fn, forward b) = forward fn(a,b);
+overload arithp(p,forward a) = forward a;
+
+[p,fn | p==fn]
+overload arithp(static p,forward a,static fn,forward b, forward ..args)
+ = forward ..arithp(p,fn(a,b), ..args);
+[p,fn | p==fn]
+overload arithp(static p,forward a,static fn,forward b)
+ = forward fn(a,b);
//
View
7 lib-clay/sequences/sequences.clay
@@ -124,6 +124,13 @@ interleave(seq:S, betweenFn, fn) {
// arithmetic
//
+[S | Sequence?(S)]
+overload updateAssign(static arith, ret:S, ..args) {
+ for (i,r in enumerated(ret))
+ r = arith(r,..reduceToScaler(i,..args));
+}
+
+
[..S | anyValues?(Sequence?,..S)]
overload arith(..args:S) {
var ret = Vector[seqType(Int8,..args)]();
View
14 test/operators/extendedassignment/main.clay
@@ -11,8 +11,8 @@ overload indexAssign(x:Foo, index, value) {
println("indexAssign(x, ", index, ", ", value, ")");
}
-overload indexUpdateAssign(op, x:Foo, index, value) {
- println("indexUpdateAssign(", op, ", x, ", index, ", ", value, ")");
+overload indexUpdateAssign(op, x:Foo, index, ..args) {
+ println("indexUpdateAssign(", ..weaveValues(",",op,"x",index,..args), ")");
}
[i]
@@ -21,8 +21,8 @@ overload staticIndexAssign(x:Foo, static i, value) {
}
[i]
-overload staticIndexUpdateAssign(op, x:Foo, static i, value) {
- println("staticIndexUpdateAssign(", op, ", x, ", i, ", ", value, ")");
+overload staticIndexUpdateAssign(op, x:Foo, static i, ..args) {
+ println("staticIndexUpdateAssign(", ..weaveValues(",",op,"x",i,..args), ")");
}
[FIELD]
@@ -31,8 +31,8 @@ overload fieldRefAssign(x:Foo, static FIELD, value) {
}
[FIELD]
-overload fieldRefUpdateAssign(op, x:Foo, static FIELD, value) {
- println("fieldRefUpdateAssign(", op, ", x, ", FIELD, ", ", value, ")");
+overload fieldRefUpdateAssign(op, x:Foo, static FIELD, ..args) {
+ println("fieldRefUpdateAssign(", ..weaveValues(",",op,"x",FIELD, ..args), ")");
}
main() {
@@ -42,5 +42,5 @@ main() {
foo.bar = "hola";
foo[30] += 100;
foo.40 *= 200;
- foo.baz /= 300;
+ foo.baz \= 300;
}
View
6 test/operators/extendedassignment/out.txt
@@ -1,6 +1,6 @@
indexAssign(x, 10, hello)
staticIndexAssign(x, 20, hi)
fieldRefAssign(x, bar, hola)
-indexUpdateAssign(add, x, 30, 100)
-staticIndexUpdateAssign(multiply, x, 40, 200)
-fieldRefUpdateAssign(divide, x, baz, 300)
+indexUpdateAssign(arith,x,30,add,100)
+staticIndexUpdateAssign(arith,x,40,multiply,200)
+fieldRefUpdateAssign(arith,x,baz,quotient,300)
Please sign in to comment.
Something went wrong with that request. Please try again.