1
%operation %virtual struct roll_value *roll(expression *this)
5
struct roll_value *rv = new_roll_single(0);
12
struct roll_value *rv = new_roll_single(this->num);
18
struct roll_value *rv1 = roll(this->expr1);
19
struct roll_value *rv2 = roll(this->expr2);
20
if (rv1->count != 1) {
21
yyerror("Argument 1 to math op isn't scalar");
27
if (rv2->count != 1) {
28
yyerror("Argument 2 to math op isn't scalar");
34
switch (yykind(this)) {
36
rv1->values[0] += rv2->values[0];
39
rv1->values[0] -= rv2->values[0];
42
rv1->values[0] *= rv2->values[0];
45
rv1->values[0] /= rv2->values[0];
48
rv1->values[0] %= rv2->values[0];
51
rv1->values[0] = (int) pow(rv1->values[0], rv2->values[0]);
54
yyerror("Unknown math op");
62
/* XXX scat could also be a math op */
65
struct roll_value *rv1 = roll(this->expr1);
66
struct roll_value *rv2 = roll(this->expr2);
67
if (rv1->count != 1) {
68
yyerror("Argument 1 to scat (.) isn't scalar");
74
if (rv2->count != 1) {
75
yyerror("Argument 2 to scat (.) isn't scalar");
82
while (i < rv2->values[0]) {
86
rv1->values[0] += rv2->values[0];
95
struct roll_value *rv1 = roll(this->expr1);
96
struct roll_value *rv2 = roll(this->expr2);
97
if (rv1->count != 1) {
98
yyerror("Argument 1 to range (..) isn't scalar");
104
if (rv2->count != 1) {
105
yyerror("Argument 2 to range (..) isn't scalar");
111
struct roll_value *rv;
112
if (rv1->values[0] <= rv2->values[0]) {
114
abs(rv1->values[0] - rv2->values[0]) + 1);
115
for (i = rv1->values[0]; i != rv2->values[0];
116
rv1->values[0] < rv2->values[0] ? i++ : i--) {
120
rv->values[c] = rv2->values[0];
123
rv = new_roll_single(0);
133
struct roll_value *rep = roll(this->expr1);
134
if (rep->count != 1) {
135
yyerror("Argument 1 to rep (#) isn't scalar");
140
struct roll_value *rv = new_roll_single(rep->values[0]);
143
for (i = 0; i < rep->values[0]; i++) {
144
struct roll_value *roll = roll(this->expr2);
145
rv->values = (int*)realloc(rv->values,
146
sizeof(int) * (rv->count + roll->count));
147
memcpy(&rv->values[rv->count], roll->values,
148
roll->count * sizeof(int));
149
rv->count += roll->count;
159
struct roll_value *rv1 = roll(this->expr1);
160
struct roll_value *rv2 = roll(this->expr2);
161
rv1->values = (int*)realloc(rv1->values,
162
sizeof(int) * (rv1->count + rv2->count));
163
memcpy(&rv1->values[rv1->count], rv2->values,
164
rv2->count * sizeof(int));
165
rv1->count += rv2->count;
172
struct roll_value *rv = roll(this->expr);
173
if (rv->count != 1) {
174
yyerror("Argument 1 to negate (-) isn't scalar");
185
struct roll_value *rv = roll(this->expr);
186
if (rv->count != 1) {
187
yyerror("Argument 1 to dice (d) isn't scalar");
192
if (rv->values[0] < 1) {
193
yyerror("Argument 1 to dice (d) isn't >= 1");
198
rv->values[0] = 1 + (int) (rv->values[0] * (rand() / (RAND_MAX + 1.0)));
206
struct roll_value *rv = roll(this->expr);
207
for (i = 0; i < rv->count; i++) {
211
rv->values = (int*)realloc(rv->values, sizeof(int) * rv->count);
220
struct roll_value *rv = roll(this->expr);
221
for (i = 0; i < rv->count; i++) {
231
struct roll_value *rv = roll(this->expr);
232
permute(rv->values, rv->count);
238
struct roll_value *rv = roll(this->expr);
240
quicksort(rv->values, 0, rv->count - 1);
247
struct roll_value *rv = roll(this->expr);
248
for (i = 0; i < rv->count/2; i++) {
249
rv->values[i] ^= rv->values[rv->count-1-i];
250
rv->values[rv->count-1-i] ^= rv->values[i];
251
rv->values[i] ^= rv->values[rv->count-1-i];
258
struct roll_value *rv = roll(this->expr);
259
rv->values = realloc(rv->values, sizeof(int));
260
rv->values[0] = rv->count;
265
/* XXX first <-> last and high <-> low could be done in one method per pair */
267
struct roll_value *num = roll(this->expr1);
268
struct roll_value *exp = roll(this->expr2);
269
if (num->count != 1) {
270
yyerror("Argument 1 to first isn't scalar");
276
if (this->type == drop) {
277
int i = exp->count - num->values[0];
283
memcpy(&exp->values[0], &exp->values[exp->count - i],
289
exp->count = min(exp->count, num->values[0]);
296
struct roll_value *num = roll(this->expr1);
297
struct roll_value *exp = roll(this->expr2);
298
if (num->count != 1) {
299
yyerror("Argument 1 to last isn't scalar");
305
if (this->type == drop) {
306
exp->count -= num->values[0];
307
if (exp->count < 0) {
312
if (num->values[0] < exp->count) {
313
memcpy(&exp->values[0], &exp->values[exp->count - num->values[0]],
314
num->values[0] * sizeof(int));
315
exp->count = num->values[0];
323
struct roll_value *num = roll(this->expr1);
324
struct roll_value *exp = roll(this->expr2);
325
if (num->count != 1) {
326
yyerror("Argument 1 to last isn't scalar");
332
int *slist = (int*)malloc(sizeof(int) * exp->count);
333
int *mask = (int*)malloc(sizeof(int) * exp->count);
334
memcpy(slist, exp->values, sizeof(int) * exp->count);
335
memset(mask, 0, sizeof(int) * exp->count);
336
quicksort(slist, 0, exp->count -1);
340
for (i = 0; i < exp->count; i++) {
341
printf("%i", exp->values[i]);
342
if (i < (exp->count-1)) {
348
if (this->type == keep) {
349
for (i = 0; (i < exp->count) && (c < num->values[0]); i++) {
351
for (j = 0; j < num->values[0]; j++) {
352
if (exp->values[i] == slist[j]) {
358
exp->values[p++] = exp->values[i];
364
// XXX something wrong here
365
for (i = 0; (i < exp->count) && (c < (exp->count - num->values[0]));
368
for (j = exp->count - 1; j >= num->values[0]; j--) {
369
if ((exp->values[i] == slist[j]) && (!mask[j])) {
378
exp->values[p++] = exp->values[i];
384
/* for (i = 0; i < exp->count; i++) {
385
printf("%i", exp->values[i]);
386
if (i < (exp->count-1)) {
397
struct roll_value *num = roll(this->expr1);
398
struct roll_value *exp = roll(this->expr2);
399
if (num->count != 1) {
400
yyerror("Argument 1 to last isn't scalar");
406
int *slist = (int*)malloc(sizeof(int) * exp->count);
407
int *mask = (int*)malloc(sizeof(int) * exp->count);
408
memcpy(slist, exp->values, sizeof(int) * exp->count);
409
memset(mask, 0, sizeof(int) * exp->count);
410
quicksort(slist, 0, exp->count -1);
413
if (this->type == keep) {
414
for (i = 0; (i < exp->count) && (c < num->values[0]); i++) {
416
for (j = exp->count - 1; j >= exp->count - num->values[0]; j--) {
417
if (exp->values[i] == slist[j]) {
423
exp->values[p++] = exp->values[i];
429
for (i = 0; (i < exp->count) && (c < (exp->count - num->values[0]));
432
for (j = 0; j < exp->count - num->values[0]; j++) {
433
if ((exp->values[i] == slist[j]) && (!mask[j])) {
440
exp->values[p++] = exp->values[i];
452
struct roll_value *num = roll(this->expr1);
453
struct roll_value *exp = roll(this->expr2);
454
if (num->count != 1) {
455
yyerror("Argument 1 to comparison isn't scalar");
462
for (o = 0; o < exp->count; o++) {
464
switch (this->comp) {
466
c = exp->values[o] == num->values[0];
469
c = exp->values[o] != num->values[0];
472
c = exp->values[o] > num->values[0];
475
c = exp->values[o] < num->values[0];
478
c = exp->values[o] >= num->values[0];
481
c = exp->values[o] <= num->values[0];
484
yyerror("Unexpected comparison (really bad)");
486
if (this->type == drop) {
490
exp->values[n++] = exp->values[o];
500
struct roll_value *if_expr = roll(this->if_expr);
501
if (if_expr->count > 0) {
503
return roll(this->then_expr);
507
return roll(this->else_expr);
513
// variable setzen und in symtab
514
struct symtab *nst = (struct symtab*)malloc(sizeof(struct symtab));
515
nst->name = this->varname;
516
nst->rvalue = roll(this->expr1);
517
nst->next = this->symtab;
518
set_symtab(this->expr2, nst);
519
struct roll_value *ret = roll(this->expr2);
520
// variable wieder vom stack
521
free_roll(nst->rvalue);
528
struct symtab *nst = this->symtab;
529
struct roll_value *ret = NULL;
530
while (nst != NULL) {
531
if (strcmp(this->varname, nst->name) == 0) {
532
/* copy the roll value */
533
ret = (struct roll_value*)malloc(sizeof(struct roll_value));
534
ret->values = (int*)malloc(sizeof(int) * nst->rvalue->count);
535
memcpy(ret->values, nst->rvalue->values,
536
sizeof(int) * nst->rvalue->count);
537
ret->count = nst->rvalue->count;
543
char *emsg = (char*)malloc(sizeof(char)*22+strlen(this->varname));
544
sprintf(emsg, "Variable \"%s\" not found", this->varname);
547
ret = (struct roll_value*)malloc(sizeof(struct roll_value));
548
ret->values = (int*)malloc(sizeof(int) * 1);
557
struct roll_value *list = roll(this->expr1);
558
struct roll_value *ret = (struct roll_value*)
559
malloc(sizeof(struct roll_value));
562
struct symtab *nst = (struct symtab*)malloc(sizeof(struct symtab));
563
nst->name = this->varname;
564
nst->rvalue = (struct roll_value*)malloc(sizeof(struct roll_value));
565
nst->rvalue->count = 1;
566
nst->rvalue->values = (int*)malloc(sizeof(int));
567
nst->next = this->symtab;
568
set_symtab(this->expr2, nst);
570
for (i = 0; i < list->count; i++) {
571
nst->rvalue->values[0] = list->values[i];
572
struct roll_value *cur = roll(this->expr2);
573
/* concat to the current solution */
574
ret->values = (int*)realloc(ret->values,
575
(ret->count + cur->count) * sizeof(int));
576
memcpy(&ret->values[ret->count], cur->values,
577
cur->count * sizeof(int));
578
ret->count += cur->count;
582
free_roll(nst->rvalue);
589
struct roll_value *cur;
590
struct roll_value *ret = (struct roll_value*)malloc(
591
sizeof(struct roll_value));
594
struct symtab *nst = (struct symtab*)malloc(sizeof(struct symtab));
595
nst->name = this->varname;
596
nst->next = this->symtab;
598
set_symtab(this->expr2, nst);
599
cur = roll(this->expr1);
601
/* concat cur to ret */
602
ret->values = (int*)realloc(ret->values,
603
sizeof(int) * (cur->count + ret->count));
604
memcpy(&ret->values[ret->count], cur->values, sizeof(int) * cur->count);
605
ret->count += cur->count;
606
while (cur->count > 0)
608
cur = roll(this->expr2);
609
free_roll(nst->rvalue);
611
/* concat cur to ret */
612
ret->values = (int*)realloc(ret->values,
613
sizeof(int) * (cur->count + ret->count));
614
memcpy(&ret->values[ret->count], cur->values,
615
sizeof(int) * cur->count);
616
ret->count += cur->count;