1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
%require "3.0"
/* By default, Bison emits a parser using symbols prefixed with "yy". Graphviz
* contains multiple Bison-generated parsers, so we alter this prefix to avoid
* symbol clashes.
*/
%define api.prefix {ex_}
%{
/*
* Glenn Fowler
* AT&T Research
*
* expression library grammar and compiler
*/
#include <assert.h>
#include <expr/exop.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ast/ast.h>
#include <util/arena.h>
#include <util/gv_ctype.h>
#include <util/streq.h>
%}
%union
{
struct Exnode_s*expr;
double floating;
struct Exref_s* reference;
struct Exid_s* id;
long long integer;
int op;
char* string;
}
%start program
%token MINTOKEN
%token INTEGER
%token UNSIGNED
%token CHARACTER
%token FLOATING
%token STRING
%token VOIDTYPE
%token ADDRESS
%token ARRAY
%token BREAK
%token CALL
%token CASE
%token CONSTANT
%token CONTINUE
%token DECLARE
%token DEFAULT
%token DYNAMIC
%token ELSE
%token EXIT
%token FOR
%token FUNCTION
%token GSUB
%token ITERATE
%token ITERATOR
%token ID
%token IF
%token LABEL
%token MEMBER
%token NAME
%token POS
%token PRAGMA
%token PRE
%token PRINT
%token PRINTF
%token PROCEDURE
%token QUERY
%token RAND
%token RETURN
%token SCANF
%token SPLIT
%token SPRINTF
%token SRAND
%token SSCANF
%token SUB
%token SUBSTR
%token SWITCH
%token TOKENS
%token UNSET
%token WHILE
%token F2I
%token F2S
%token I2F
%token I2S
%token S2B
%token S2F
%token S2I
%token F2X
%token I2X
%token S2X
%token X2F
%token X2I
%token X2S
%token X2X
%token XPRINT
%left <op> ','
%right <op> '='
%right <op> '?' ':'
%left <op> OR
%left <op> AND
%left <op> '|'
%left <op> '^'
%left <op> '&'
%binary <op> EQ NE
%binary <op> '<' '>' LE GE
%left <op> LSH RSH
%left <op> '+' '-' IN_OP
%left <op> '*' '/' '%'
%right <op> '!' '~' '#' UNARY
%right <op> INC DEC
%right <op> CAST
%left <op> '('
%type <expr> statement statement_list arg_list
%type <expr> else_opt expr_opt expr
%type <expr> args variable assign
%type <expr> dcl_list dcl_item index
%type <expr> initialize switch_item constant
%type <expr> formals formal_list formal_item
%type <reference> members
%type <id> ID LABEL NAME
%type <id> CONSTANT ARRAY FUNCTION DECLARE
%type <id> EXIT PRINT PRINTF QUERY
%type <id> RAND SRAND
%type <id> SPRINTF PROCEDURE name dcl_name
%type <id> GSUB SUB SUBSTR
%type <id> SPLIT TOKENS splitop
%type <id> IF WHILE FOR ITERATOR
%type <id> BREAK CONTINUE print member
%type <id> RETURN DYNAMIC SWITCH UNSET
%type <id> SCANF SSCANF scan
%type <floating> FLOATING
%type <integer> INTEGER UNSIGNED array
%type <string> STRING
%token MAXTOKEN
/* ask Bison to generate a table, yytname, containing string representations
* of all the above tokens
*/
%token-table
%{
#include <expr/exgram.h>
void ex_error(const char *message);
%}
%%
program : statement_list action_list
{
if ($1) {
if (expr.program->main.value)
exfreenode(expr.program, expr.program->main.value);
if ($1->op == S2B)
{
Exnode_t *x = $1;
$1 = x->data.operand.left;
x->data.operand.left = 0;
exfreenode(expr.program, x);
}
expr.program->main.lex = PROCEDURE;
expr.program->main.value = exnewnode(expr.program, PROCEDURE, true, $1->type, NULL, $1);
}
}
;
action_list : /* empty */
| action_list action
;
action : LABEL ':' {
if (expr.procedure)
exerror("no nested function definitions");
$1->lex = PROCEDURE;
expr.procedure = $1->value = exnewnode(expr.program, PROCEDURE, true, $1->type, NULL, NULL);
expr.procedure->type = INTEGER;
static Dtdisc_t disc = {.key = offsetof(Exid_t, name)};
if (expr.assigned && !streq($1->name, "begin"))
{
if (!(expr.procedure->data.procedure.frame = dtopen(&disc, Dtset)) ||
!dtview(expr.procedure->data.procedure.frame, expr.program->symbols))
exnospace();
expr.program->symbols = expr.program->frame = expr.procedure->data.procedure.frame;
}
} statement_list
{
expr.procedure = NULL;
if (expr.program->frame)
{
expr.program->symbols = expr.program->frame->view;
dtview(expr.program->frame, NULL);
expr.program->frame = NULL;
}
if ($4 && $4->op == S2B)
{
Exnode_t *x = $4;
$4 = x->data.operand.left;
x->data.operand.left = 0;
exfreenode(expr.program, x);
}
$1->value->data.procedure.body = excast(expr.program, $4, $1->type, NULL, 0);
}
;
statement_list : /* empty */
{
$$ = 0;
}
| statement_list statement
{
if (!$1)
$$ = $2;
else if (!$2)
$$ = $1;
else if ($1->op == CONSTANT)
{
exfreenode(expr.program, $1);
$$ = $2;
}
else $$ = exnewnode(expr.program, ';', true, $2->type, $1, $2);
}
;
statement : '{' statement_list '}'
{
$$ = $2;
}
| expr_opt ';'
{
$$ = ($1 && $1->type == STRING) ? exnewnode(expr.program, S2B, true, INTEGER, $1, NULL) : $1;
}
| DECLARE {expr.declare = $1->type;} dcl_list ';'
{
$$ = $3;
expr.declare = 0;
}
| IF '(' expr ')' statement else_opt
{
if (exisAssign ($3))
exwarn ("assignment used as boolean in if statement");
if ($3->type == STRING)
$3 = exnewnode(expr.program, S2B, true, INTEGER, $3, NULL);
else if (!INTEGRAL($3->type))
$3 = excast(expr.program, $3, INTEGER, NULL, 0);
$$ = exnewnode(expr.program, $1->index, true, INTEGER, $3, exnewnode(expr.program, ':', true, $5 ? $5->type : 0, $5, $6));
}
| FOR '(' variable ')' statement
{
$$ = exnewnode(expr.program, ITERATE, false, INTEGER, NULL, NULL);
$$->data.generate.array = $3;
if (!$3->data.variable.index || $3->data.variable.index->op != DYNAMIC)
exerror("simple index variable expected");
$$->data.generate.index = $3->data.variable.index->data.variable.symbol;
if ($3->op == ID && $$->data.generate.index->type != INTEGER)
exerror("integer index variable expected");
exfreenode(expr.program, $3->data.variable.index);
$3->data.variable.index = 0;
$$->data.generate.statement = $5;
}
| FOR '(' expr_opt ';' expr_opt ';' expr_opt ')' statement
{
if (!$5)
{
$5 = exnewnode(expr.program, CONSTANT, false, INTEGER, NULL, NULL);
$5->data.constant.value.integer = 1;
}
else if ($5->type == STRING)
$5 = exnewnode(expr.program, S2B, true, INTEGER, $5, NULL);
else if (!INTEGRAL($5->type))
$5 = excast(expr.program, $5, INTEGER, NULL, 0);
$$ = exnewnode(expr.program, $1->index, true, INTEGER, $5, exnewnode(expr.program, ';', 1, 0, $7, $9));
if ($3)
$$ = exnewnode(expr.program, ';', true, INTEGER, $3, $$);
}
| ITERATOR '(' variable ')' statement
{
$$ = exnewnode(expr.program, ITERATOR, false, INTEGER, NULL, NULL);
$$->data.generate.array = $3;
if (!$3->data.variable.index || $3->data.variable.index->op != DYNAMIC)
exerror("simple index variable expected");
$$->data.generate.index = $3->data.variable.index->data.variable.symbol;
if ($3->op == ID && $$->data.generate.index->type != INTEGER)
exerror("integer index variable expected");
exfreenode(expr.program, $3->data.variable.index);
$3->data.variable.index = 0;
$$->data.generate.statement = $5;
}
| UNSET '(' DYNAMIC ')'
{
if ($3->local == NULL)
exerror("cannot apply unset to non-array %s", $3->name);
$$ = exnewnode(expr.program, UNSET, false, INTEGER, NULL, NULL);
$$->data.variable.symbol = $3;
$$->data.variable.index = NULL;
}
| UNSET '(' DYNAMIC ',' expr ')'
{
if ($3->local == NULL)
exerror("cannot apply unset to non-array %s", $3->name);
if (($3->index_type > 0) && ($5->type != $3->index_type))
exerror("%s indices must have type %s, not %s",
$3->name, extypename(expr.program, $3->index_type),extypename(expr.program, $5->type));
$$ = exnewnode(expr.program, UNSET, false, INTEGER, NULL, NULL);
$$->data.variable.symbol = $3;
$$->data.variable.index = $5;
}
| WHILE '(' expr ')' statement
{
if (exisAssign ($3))
exwarn ("assignment used as boolean in while statement");
if ($3->type == STRING)
$3 = exnewnode(expr.program, S2B, true, INTEGER, $3, NULL);
else if (!INTEGRAL($3->type))
$3 = excast(expr.program, $3, INTEGER, NULL, 0);
$$ = exnewnode(expr.program, $1->index, true, INTEGER, $3, exnewnode(expr.program, ';', true, 0, NULL, $5));
}
| SWITCH '(' expr {expr.declare=$3->type;} ')' '{' switch_list '}'
{
Switch_t* sw = expr.swstate;
$$ = exnewnode(expr.program, $1->index, true, INTEGER, $3, exnewnode(expr.program, DEFAULT, true, 0, sw->defcase, sw->firstcase));
expr.swstate = expr.swstate->prev;
free(sw->base);
free(sw);
expr.declare = 0;
}
| BREAK expr_opt ';'
{
loopop:
if (!$2)
{
$2 = exnewnode(expr.program, CONSTANT, false, INTEGER, NULL, NULL);
$2->data.constant.value.integer = 1;
}
else if (!INTEGRAL($2->type))
$2 = excast(expr.program, $2, INTEGER, NULL, 0);
$$ = exnewnode(expr.program, $1->index, true, INTEGER, $2, NULL);
}
| CONTINUE expr_opt ';'
{
goto loopop;
}
| RETURN expr_opt ';'
{
if ($2)
{
if (expr.procedure && !expr.procedure->type)
exerror("return in void function");
$2 = excast(expr.program, $2, expr.procedure ? expr.procedure->type : INTEGER, NULL, 0);
}
$$ = exnewnode(expr.program, RETURN, true, $2 ? $2->type : 0, $2, NULL);
}
;
switch_list : /* empty */
{
Switch_t* sw;
if (!(sw = calloc(1, sizeof(Switch_t)))) {
exnospace();
} else {
expr.swstate = sw;
sw->type = expr.declare;
size_t n = 8;
if (!(sw->base = calloc(n, sizeof(Extype_t*)))) {
exnospace();
n = 0;
}
sw->cap = n;
}
}
| switch_list switch_item
;
switch_item : case_list statement_list
{
Switch_t* sw = expr.swstate;
$$ = exnewnode(expr.program, CASE, true, 0, $2, NULL);
if (sw->cur > 0)
{
if (sw->lastcase)
sw->lastcase->data.select.next = $$;
else
sw->firstcase = $$;
sw->lastcase = $$;
const size_t n = sw->cap;
sw->cur = 0;
$$->data.select.constant = gv_arena_alloc(&expr.program->vm,
alignof(Extype_t *),
(n + 1) * sizeof(Extype_t*));
memcpy($$->data.select.constant, sw->base, n * sizeof(Extype_t*));
}
else
$$->data.select.constant = 0;
if (sw->def)
{
sw->def = 0;
if (sw->defcase)
exerror("duplicate default in switch");
else
sw->defcase = $2;
}
}
;
case_list : case_item
| case_list case_item
;
case_item : CASE constant ':'
{
if (expr.swstate->cur >= expr.swstate->cap)
{
size_t n = expr.swstate->cap;
if (!(expr.swstate->base = realloc(expr.swstate->base, sizeof(Extype_t*) * 2 * n)))
{
exerror("too many case labels for switch");
n = 0;
}
expr.swstate->cap = 2 * n;
}
if (expr.swstate->base != NULL)
{
$2 = excast(expr.program, $2, expr.swstate->type, NULL, 0);
expr.swstate->base[expr.swstate->cur++] = &$2->data.constant.value;
}
}
| DEFAULT ':'
{
expr.swstate->def = 1;
}
;
dcl_list : dcl_item
| dcl_list ',' dcl_item
{
if ($3)
$$ = $1 ? exnewnode(expr.program, ',', true, $3->type, $1, $3) : $3;
}
;
dcl_item : dcl_name {checkName ($1); expr.id=$1;} array initialize
{
$$ = 0;
if (!$1->type || expr.declare)
$1->type = expr.declare;
if ($4 && $4->op == PROCEDURE)
{
$1->lex = PROCEDURE;
$1->type = $4->type;
$1->value = $4;
}
else
{
if ($1->type == 0) {
exerror("%s: a variable cannot be void typed", $1->name);
}
$1->lex = DYNAMIC;
$1->value = exnewnode(expr.program, 0, false, 0, NULL, NULL);
if ($3 && $1->local == NULL)
{
static Dtdisc_t disc_key = {
.key = offsetof(Exassoc_t, key),
.size = sizeof(Extype_t),
.comparf = cmpKey,
};
static Dtdisc_t disc_name = {.key = offsetof(Exassoc_t, name)};
Dtdisc_t *const disc = $3 == INTEGER ? &disc_key : &disc_name;
if (!($1->local = dtopen(disc, Dtoset)))
exerror("%s: cannot initialize associative array", $1->name);
$1->index_type = $3; /* -1 indicates no typechecking */
}
if ($4)
{
if ($4->type != $1->type)
{
$4->type = $1->type;
$4->data.operand.right = excast(expr.program, $4->data.operand.right, $1->type, NULL, 0);
}
$4->data.operand.left = exnewnode(expr.program, DYNAMIC, false, $1->type, NULL, NULL);
$4->data.operand.left->data.variable.symbol = $1;
$$ = $4;
}
else if (!$3)
$1->value->data.value = exzero($1->type);
}
}
;
dcl_name : NAME
| DYNAMIC
| ID
| FUNCTION
;
name : NAME
| DYNAMIC
;
else_opt : /* empty */
{
$$ = 0;
}
| ELSE statement
{
$$ = $2;
}
;
expr_opt : /* empty */
{
$$ = 0;
}
| expr
;
expr : '(' expr ')'
{
$$ = $2;
}
| '(' DECLARE ')' expr %prec CAST
{
$$ = ($4->type == $2->type) ? $4 : excast(expr.program, $4, $2->type, NULL, 0);
}
| expr '<' expr
{
long rel;
relational:
rel = INTEGER;
goto coerce;
binary:
rel = 0;
coerce:
if (!$1->type)
{
if (!$3->type)
$1->type = $3->type = rel ? STRING : INTEGER;
else
$1->type = $3->type;
}
else if (!$3->type)
$3->type = $1->type;
if ($1->type != $3->type)
{
if ($1->type == STRING)
$1 = excast(expr.program, $1, $3->type, $3, 0);
else if ($3->type == STRING)
$3 = excast(expr.program, $3, $1->type, $1, 0);
else if ($1->type == FLOATING)
$3 = excast(expr.program, $3, FLOATING, $1, 0);
else if ($3->type == FLOATING)
$1 = excast(expr.program, $1, FLOATING, $3, 0);
}
if (!rel)
rel = ($1->type == STRING) ? STRING : (($1->type == UNSIGNED) ? UNSIGNED : $3->type);
$$ = exnewnode(expr.program, $2, true, rel, $1, $3);
if (!expr.program->errors && $1->op == CONSTANT && $3->op == CONSTANT)
{
$$->data.constant.value = exeval(expr.program, $$, NULL);
/* If a constant string, re-allocate from program heap. This is because the
* value was constructed from string operators, which create a value in the
* temporary heap, which is cleared when exeval is called again.
*/
if ($$->type == STRING) {
$$->data.constant.value.string =
gv_arena_strdup(&expr.program->vm, $$->data.constant.value.string);
}
$$->binary = false;
$$->op = CONSTANT;
exfreenode(expr.program, $1);
exfreenode(expr.program, $3);
}
else if (!BUILTIN($1->type) || !BUILTIN($3->type)) {
checkBinary(expr.program, $1, $$, $3);
}
}
| expr '-' expr
{
goto binary;
}
| expr '*' expr
{
goto binary;
}
| expr '/' expr
{
goto binary;
}
| expr '%' expr
{
goto binary;
}
| expr LSH expr
{
goto binary;
}
| expr RSH expr
{
goto binary;
}
| expr '>' expr
{
goto relational;
}
| expr LE expr
{
goto relational;
}
| expr GE expr
{
goto relational;
}
| expr EQ expr
{
goto relational;
}
| expr NE expr
{
goto relational;
}
| expr '&' expr
{
goto binary;
}
| expr '|' expr
{
goto binary;
}
| expr '^' expr
{
goto binary;
}
| expr '+' expr
{
goto binary;
}
| expr AND expr
{
logical:
if ($1->type == STRING)
$1 = exnewnode(expr.program, S2B, true, INTEGER, $1, NULL);
else if (!BUILTIN($1->type))
$1 = excast(expr.program, $1, INTEGER, NULL, 0);
if ($3->type == STRING)
$3 = exnewnode(expr.program, S2B, true, INTEGER, $3, NULL);
else if (!BUILTIN($3->type))
$3 = excast(expr.program, $3, INTEGER, NULL, 0);
goto binary;
}
| expr OR expr
{
goto logical;
}
| expr ',' expr
{
if ($1->op == CONSTANT)
{
exfreenode(expr.program, $1);
$$ = $3;
}
else
$$ = exnewnode(expr.program, ',', true, $3->type, $1, $3);
}
| expr '?' {expr.nolabel=1;} expr ':' {expr.nolabel=0;} expr
{
if (!$4->type)
{
if (!$7->type)
$4->type = $7->type = INTEGER;
else
$4->type = $7->type;
}
else if (!$7->type)
$7->type = $4->type;
if ($1->type == STRING)
$1 = exnewnode(expr.program, S2B, true, INTEGER, $1, NULL);
else if (!INTEGRAL($1->type))
$1 = excast(expr.program, $1, INTEGER, NULL, 0);
if ($4->type != $7->type)
{
if ($4->type == STRING || $7->type == STRING)
exerror("if statement string type mismatch");
else if ($4->type == FLOATING)
$7 = excast(expr.program, $7, FLOATING, NULL, 0);
else if ($7->type == FLOATING)
$4 = excast(expr.program, $4, FLOATING, NULL, 0);
}
if ($1->op == CONSTANT)
{
if ($1->data.constant.value.integer)
{
$$ = $4;
exfreenode(expr.program, $7);
}
else
{
$$ = $7;
exfreenode(expr.program, $4);
}
exfreenode(expr.program, $1);
}
else
$$ = exnewnode(expr.program, '?', true, $4->type, $1, exnewnode(expr.program, ':', true, $4->type, $4, $7));
}
| '!' expr
{
iunary:
if ($2->type == STRING)
$2 = exnewnode(expr.program, S2B, true, INTEGER, $2, NULL);
else if (!INTEGRAL($2->type))
$2 = excast(expr.program, $2, INTEGER, NULL, 0);
unary:
$$ = exnewnode(expr.program, $1, true, $2->type == UNSIGNED ? INTEGER : $2->type, $2, NULL);
if ($2->op == CONSTANT)
{
$$->data.constant.value = exeval(expr.program, $$, NULL);
$$->binary = false;
$$->op = CONSTANT;
exfreenode(expr.program, $2);
}
else if (!BUILTIN($2->type)) {
checkBinary(expr.program, $2, $$, 0);
}
}
| '#' DYNAMIC
{
if ($2->local == NULL)
exerror("cannot apply '#' operator to non-array %s", $2->name);
$$ = exnewnode(expr.program, '#', false, INTEGER, NULL, NULL);
$$->data.variable.symbol = $2;
}
| '#' ARRAY
{
$$ = exnewnode(expr.program, '#', false, INTEGER, NULL, NULL);
$$->data.variable.symbol = $2;
}
| '~' expr
{
goto iunary;
}
| '-' expr %prec UNARY
{
goto unary;
}
| '+' expr %prec UNARY
{
$$ = $2;
}
| '&' variable %prec UNARY
{
$$ = exnewnode(expr.program, ADDRESS, false, T($2->type), $2, NULL);
}
| ARRAY '[' args ']'
{
$$ = exnewnode(expr.program, ARRAY, true, T($1->type), call(0, $1, $3), $3);
}
| FUNCTION '(' args ')'
{
$$ = exnewnode(expr.program, FUNCTION, true, T($1->type), call(0, $1, $3), $3);
}
| GSUB '(' args ')'
{
$$ = exnewsub (expr.program, $3, GSUB);
}
| SUB '(' args ')'
{
$$ = exnewsub (expr.program, $3, SUB);
}
| SUBSTR '(' args ')'
{
$$ = exnewsubstr (expr.program, $3);
}
| splitop '(' expr ',' DYNAMIC ')'
{
$$ = exnewsplit (expr.program, $1->index, $5, $3, NULL);
}
| splitop '(' expr ',' DYNAMIC ',' expr ')'
{
$$ = exnewsplit (expr.program, $1->index, $5, $3, $7);
}
| EXIT '(' expr ')'
{
if (!INTEGRAL($3->type))
$3 = excast(expr.program, $3, INTEGER, NULL, 0);
$$ = exnewnode(expr.program, EXIT, true, INTEGER, $3, NULL);
}
| RAND '(' ')'
{
$$ = exnewnode(expr.program, RAND, false, FLOATING, NULL, NULL);
}
| SRAND '(' ')'
{
$$ = exnewnode(expr.program, SRAND, false, INTEGER, NULL, NULL);
}
| SRAND '(' expr ')'
{
if (!INTEGRAL($3->type))
$3 = excast(expr.program, $3, INTEGER, NULL, 0);
$$ = exnewnode(expr.program, SRAND, true, INTEGER, $3, NULL);
}
| PROCEDURE '(' args ')'
{
$$ = exnewnode(expr.program, CALL, true, $1->type, NULL, $3);
$$->data.call.procedure = $1;
}
| PRINT '(' args ')'
{
$$ = exprint(expr.program, $1, $3);
}
| print '(' args ')'
{
$$ = exnewnode(expr.program, $1->index, false, $1->type, NULL, NULL);
if ($3 && $3->data.operand.left->type == INTEGER)
{
$$->data.print.descriptor = $3->data.operand.left;
$3 = $3->data.operand.right;
}
else
switch ($1->index)
{
case QUERY:
$$->data.print.descriptor = exnewnode(expr.program, CONSTANT, false, INTEGER, NULL, NULL);
$$->data.print.descriptor->data.constant.value.integer = 2;
break;
case PRINTF:
$$->data.print.descriptor = exnewnode(expr.program, CONSTANT, false, INTEGER, NULL, NULL);
$$->data.print.descriptor->data.constant.value.integer = 1;
break;
case SPRINTF:
$$->data.print.descriptor = 0;
break;
}
$$->data.print.args = preprint($3);
}
| scan '(' args ')'
{
Exnode_t* x;
$$ = exnewnode(expr.program, $1->index, false, $1->type, NULL, NULL);
if ($3 && $3->data.operand.left->type == INTEGER)
{
$$->data.scan.descriptor = $3->data.operand.left;
$3 = $3->data.operand.right;
}
else
switch ($1->index)
{
case SCANF:
$$->data.scan.descriptor = 0;
break;
case SSCANF:
if ($3 && $3->data.operand.left->type == STRING)
{
$$->data.scan.descriptor = $3->data.operand.left;
$3 = $3->data.operand.right;
}
else
exerror("%s: string argument expected", $1->name);
break;
}
if (!$3 || !$3->data.operand.left || $3->data.operand.left->type != STRING)
exerror("%s: format argument expected", $1->name);
$$->data.scan.format = $3->data.operand.left;
for (x = $$->data.scan.args = $3->data.operand.right; x; x = x->data.operand.right)
{
if (x->data.operand.left->op != ADDRESS)
exerror("%s: address argument expected", $1->name);
x->data.operand.left = x->data.operand.left->data.operand.left;
}
}
| variable assign
{
if ($2)
{
if ($1->op == ID && !expr.program->disc->setf)
exerror("%s: variable assignment not supported", $1->data.variable.symbol->name);
else
{
if (!$1->type)
$1->type = $2->type;
else if ($2->type != $1->type)
{
$2->type = $1->type;
$2->data.operand.right = excast(expr.program, $2->data.operand.right, $1->type, NULL, 0);
}
$2->data.operand.left = $1;
$$ = $2;
}
}
}
| INC variable
{
pre:
if ($2->type == STRING)
exerror("++ and -- invalid for string variables");
$$ = exnewnode(expr.program, $1, false, $2->type, $2, NULL);
$$->subop = PRE;
}
| variable INC
{
pos:
if ($1->type == STRING)
exerror("++ and -- invalid for string variables");
$$ = exnewnode(expr.program, $2, false, $1->type, $1, NULL);
$$->subop = POS;
}
| expr IN_OP DYNAMIC
{
if ($3->local == NULL)
exerror("cannot apply IN to non-array %s", $3->name);
if ($3->index_type > 0 && $1->type != $3->index_type)
exerror("%s indices must have type %s, not %s",
$3->name, extypename(expr.program, $3->index_type),extypename(expr.program, $1->type));
$$ = exnewnode(expr.program, IN_OP, false, INTEGER, NULL, NULL);
$$->data.variable.symbol = $3;
$$->data.variable.index = $1;
}
| expr IN_OP ARRAY
{
if ($3->index_type > 0 && $1->type != $3->index_type)
exerror("%s indices must have type %s, not %s",
$3->name, extypename(expr.program, $3->index_type),extypename(expr.program, $1->type));
$$ = exnewnode(expr.program, IN_OP, false, INTEGER, NULL, NULL);
$$->data.variable.symbol = $3;
$$->data.variable.index = $1;
}
| DEC variable
{
goto pre;
}
| variable DEC
{
goto pos;
}
| constant
;
splitop : SPLIT
| TOKENS
;
constant : CONSTANT
{
$$ = exnewnode(expr.program, CONSTANT, false, $1->type, NULL, NULL);
if (!expr.program->disc->reff)
exerror("%s: identifier references not supported", $1->name);
else
$$->data.constant.value = expr.program->disc->reff(expr.program, $$, $1, NULL);
}
| FLOATING
{
$$ = exnewnode(expr.program, CONSTANT, false, FLOATING, NULL, NULL);
$$->data.constant.value.floating = $1;
}
| INTEGER
{
$$ = exnewnode(expr.program, CONSTANT, false, INTEGER, NULL, NULL);
$$->data.constant.value.integer = $1;
}
| STRING
{
$$ = exnewnode(expr.program, CONSTANT, false, STRING, NULL, NULL);
$$->data.constant.value.string = $1;
}
| UNSIGNED
{
$$ = exnewnode(expr.program, CONSTANT, false, UNSIGNED, NULL, NULL);
$$->data.constant.value.integer = $1;
}
;
print : PRINTF
| QUERY
| SPRINTF
;
scan : SCANF
| SSCANF
;
variable : ID members
{
$$ = makeVar(expr.program, $1, 0, 0, $2);
}
| DYNAMIC index members
{
Exnode_t *n = exnewnode(expr.program, DYNAMIC, false, $1->type, NULL, NULL);
n->data.variable.symbol = $1;
n->data.variable.reference = 0;
if (((n->data.variable.index = $2) == 0) != ($1->local == NULL))
exerror("%s: is%s an array", $1->name, $1->local != NULL ? "" : " not");
if ($1->local != NULL && $1->index_type > 0) {
if ($2->type != $1->index_type)
exerror("%s: indices must have type %s, not %s",
$1->name, extypename(expr.program, $1->index_type),extypename(expr.program, $2->type));
}
if ($3) {
n->data.variable.dyna = exnewnode(expr.program, 0, false, 0, NULL, NULL);
$$ = makeVar(expr.program, $1, $2, n, $3);
}
else $$ = n;
}
| NAME
{
$$ = exnewnode(expr.program, ID, false, STRING, NULL, NULL);
$$->data.variable.symbol = $1;
$$->data.variable.reference = 0;
$$->data.variable.index = 0;
$$->data.variable.dyna = 0;
if (!(expr.program->disc->flags & EX_UNDECLARED))
exerror("unknown identifier");
}
;
array : /* empty */
{
$$ = 0;
}
| '[' ']'
{
$$ = -1;
}
| '[' DECLARE ']'
{
/* If DECLARE is VOID, its type is 0, so this acts like
* the empty case.
*/
if (INTEGRAL($2->type))
$$ = INTEGER;
else
$$ = $2->type;
}
;
index : /* empty */
{
$$ = 0;
}
| '[' expr ']'
{
$$ = $2;
}
;
args : /* empty */
{
$$ = 0;
}
| arg_list
{
$$ = $1->data.operand.left;
$1->data.operand.left = $1->data.operand.right = 0;
exfreenode(expr.program, $1);
}
;
arg_list : expr %prec ','
{
$$ = exnewnode(expr.program, ',', true, 0, exnewnode(expr.program, ',', true, $1->type, $1, NULL), NULL);
$$->data.operand.right = $$->data.operand.left;
}
| arg_list ',' expr
{
$1->data.operand.right = $1->data.operand.right->data.operand.right = exnewnode(expr.program, ',', true, $1->type, $3, NULL);
}
;
formals : /* empty */
{
$$ = 0;
}
| DECLARE
{
$$ = 0;
if ($1->type)
exerror("(void) expected");
}
| formal_list
;
formal_list : formal_item
{
$$ = exnewnode(expr.program, ',', true, $1->type, $1, NULL);
}
| formal_list ',' formal_item
{
Exnode_t* x;
Exnode_t* y;
$$ = $1;
for (x = $1; (y = x->data.operand.right); x = y);
x->data.operand.right = exnewnode(expr.program, ',', true, $3->type, $3, NULL);
}
;
formal_item : DECLARE {expr.declare=$1->type;} name
{
if ($1->type == 0) {
exerror("%s: parameters to functions cannot be void typed", $3->name);
}
$$ = exnewnode(expr.program, ID, false, $1->type, NULL, NULL);
$$->data.variable.symbol = $3;
$3->lex = DYNAMIC;
$3->type = $1->type;
$3->value = exnewnode(expr.program, 0, false, 0, NULL, NULL);
expr.procedure->data.procedure.arity++;
expr.declare = 0;
}
;
members : /* empty */
{
$$ = expr.refs = 0;
}
| member
{
Exref_t* r;
r = ARENA_NEW(&expr.program->vm, Exref_t);
r->symbol = $1;
expr.refs = r;
r->next = 0;
r->index = 0;
$$ = expr.refs;
}
| '.' ID member
{
Exref_t* r;
Exref_t* l;
r = ARENA_NEW(&expr.program->vm, Exref_t);
r->symbol = $3;
r->index = 0;
r->next = 0;
l = ARENA_NEW(&expr.program->vm, Exref_t);
l->symbol = $2;
l->index = 0;
l->next = r;
expr.refs = l;
$$ = expr.refs;
}
;
member : '.' ID
{
$$ = $2;
}
| '.' NAME
{
$$ = $2;
}
;
assign : /* empty */
{
$$ = 0;
}
| '=' expr
{
$$ = exnewnode(expr.program, '=', true, $2->type, NULL, $2);
$$->subop = $1;
}
;
initialize : assign
| '(' {
if (expr.procedure)
exerror("%s: nested function definitions not supported", expr.id->name);
expr.procedure = exnewnode(expr.program, PROCEDURE, true, expr.declare, NULL, NULL);
if (!streq(expr.id->name, "begin"))
{
static Dtdisc_t disc = {.key = offsetof(Exid_t, name)};
if (!(expr.procedure->data.procedure.frame = dtopen(&disc, Dtset)) || !dtview(expr.procedure->data.procedure.frame, expr.program->symbols))
exnospace();
expr.program->symbols = expr.program->frame = expr.procedure->data.procedure.frame;
}
expr.declare = 0;
} formals {
expr.id->lex = PROCEDURE;
expr.id->type = expr.procedure->type;
expr.declare = 0;
} ')' '{' statement_list '}'
{
$$ = expr.procedure;
expr.procedure = NULL;
if (expr.program->frame)
{
expr.program->symbols = expr.program->frame->view;
dtview(expr.program->frame, NULL);
expr.program->frame = NULL;
}
// dictionary of locals no longer required, now that we have parsed the body
(void)dtclose($$->data.procedure.frame);
$$->data.procedure.frame = NULL;
$$->data.procedure.args = $3;
$$->data.procedure.body = excast(expr.program, $7, $$->type, NULL, 0);
/*
* NOTE: procedure definition was slipped into the
* declaration initializer statement production,
* therefore requiring the statement terminator
*/
exunlex(expr.program, ';');
}
;
%%
const char *exop(size_t index) {
/* yytname is generated by the %token-table directive */
/* find the index of MINTOKEN */
size_t minid;
for (minid = 0; yytname[minid] != NULL; ++minid) {
if (strcmp(yytname[minid], "MINTOKEN") == 0) {
break;
}
}
assert(yytname[minid] != NULL
&& "failed to find MINTOKEN; incorrect token list in exparse.y?");
/* find the requested token */
{
size_t i, j;
for (i = j = minid; yytname[i] != NULL; ++i) {
/* if this token is not a word, skip it */
{
size_t k;
for (k = 0; yytname[i][k] != '\0'; ++k) {
if (yytname[i][k] != '_' && !gv_isalnum(yytname[i][k])) {
break;
}
}
if (yytname[i][k] != '\0') {
continue;
}
}
if (j == index + minid) {
return yytname[i];
}
++j;
}
}
/* failed to find the requested token */
return NULL;
}
void ex_error(const char *message) {
exerror("%s", message);
}
#include <expr/exgram.h>
|