DEADSOFTWARE

Добавлены функции MIN, MAX и SIZE
[dsw-obn.git] / src / oberon.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdbool.h>
8 #include <math.h>
10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
15 enum {
16 EOF_ = 0,
17 IDENT,
18 MODULE,
19 SEMICOLON,
20 END,
21 DOT,
22 VAR,
23 COLON,
24 BEGIN,
25 ASSIGN,
26 INTEGER,
27 TRUE,
28 FALSE,
29 LPAREN,
30 RPAREN,
31 EQUAL,
32 NEQ,
33 LESS,
34 LEQ,
35 GREAT,
36 GEQ,
37 IN,
38 IS,
39 PLUS,
40 MINUS,
41 OR,
42 STAR,
43 SLASH,
44 DIV,
45 MOD,
46 AND,
47 NOT,
48 PROCEDURE,
49 COMMA,
50 RETURN,
51 CONST,
52 TYPE,
53 ARRAY,
54 OF,
55 LBRACE,
56 RBRACE,
57 RECORD,
58 POINTER,
59 TO,
60 UPARROW,
61 NIL,
62 IMPORT,
63 REAL,
64 CHAR,
65 STRING,
66 IF,
67 THEN,
68 ELSE,
69 ELSIF,
70 WHILE,
71 DO,
72 REPEAT,
73 UNTIL,
74 FOR,
75 BY,
76 LOOP,
77 EXIT
78 };
80 // =======================================================================
81 // UTILS
82 // =======================================================================
84 static void
85 oberon_error(oberon_context_t * ctx, const char * fmt, ...)
86 {
87 va_list ptr;
88 va_start(ptr, fmt);
89 fprintf(stderr, "error: ");
90 vfprintf(stderr, fmt, ptr);
91 fprintf(stderr, "\n");
92 fprintf(stderr, " code_index = %i\n", ctx -> code_index);
93 fprintf(stderr, " c = %c\n", ctx -> c);
94 fprintf(stderr, " token = %i\n", ctx -> token);
95 va_end(ptr);
96 exit(1);
97 }
99 static oberon_type_t *
100 oberon_new_type_ptr(int class)
102 oberon_type_t * x = malloc(sizeof *x);
103 memset(x, 0, sizeof *x);
104 x -> class = class;
105 return x;
108 static oberon_type_t *
109 oberon_new_type_integer(int size)
111 oberon_type_t * x;
112 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
113 x -> size = size;
114 return x;
117 static oberon_type_t *
118 oberon_new_type_boolean()
120 oberon_type_t * x;
121 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
122 return x;
125 static oberon_type_t *
126 oberon_new_type_real(int size)
128 oberon_type_t * x;
129 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
130 x -> size = size;
131 return x;
134 static oberon_type_t *
135 oberon_new_type_char(int size)
137 oberon_type_t * x;
138 x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
139 x -> size = size;
140 return x;
143 static oberon_type_t *
144 oberon_new_type_string(int size)
146 oberon_type_t * x;
147 x = oberon_new_type_ptr(OBERON_TYPE_STRING);
148 x -> size = size;
149 return x;
152 // =======================================================================
153 // TABLE
154 // =======================================================================
156 static oberon_scope_t *
157 oberon_open_scope(oberon_context_t * ctx)
159 oberon_scope_t * scope = calloc(1, sizeof *scope);
160 oberon_object_t * list = calloc(1, sizeof *list);
162 scope -> ctx = ctx;
163 scope -> list = list;
164 scope -> up = ctx -> decl;
166 if(scope -> up)
168 scope -> local = scope -> up -> local;
169 scope -> parent = scope -> up -> parent;
170 scope -> parent_type = scope -> up -> parent_type;
171 scope -> exit_label = scope -> up -> exit_label;
174 ctx -> decl = scope;
175 return scope;
178 static void
179 oberon_close_scope(oberon_scope_t * scope)
181 oberon_context_t * ctx = scope -> ctx;
182 ctx -> decl = scope -> up;
185 static oberon_object_t *
186 oberon_find_object_in_list(oberon_object_t * list, char * name)
188 oberon_object_t * x = list;
189 while(x -> next && strcmp(x -> next -> name, name) != 0)
191 x = x -> next;
193 return x -> next;
196 static oberon_object_t *
197 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
199 oberon_object_t * result = NULL;
201 oberon_scope_t * s = scope;
202 while(result == NULL && s != NULL)
204 result = oberon_find_object_in_list(s -> list, name);
205 s = s -> up;
208 if(check_it && result == NULL)
210 oberon_error(scope -> ctx, "undefined ident %s", name);
213 return result;
216 static oberon_object_t *
217 oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only)
219 oberon_object_t * newvar = malloc(sizeof *newvar);
220 memset(newvar, 0, sizeof *newvar);
221 newvar -> name = name;
222 newvar -> class = class;
223 newvar -> export = export;
224 newvar -> read_only = read_only;
225 newvar -> local = scope -> local;
226 newvar -> parent = scope -> parent;
227 newvar -> parent_type = scope -> parent_type;
228 newvar -> module = scope -> ctx -> mod;
229 return newvar;
232 static oberon_object_t *
233 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
235 if(check_upscope)
237 if(oberon_find_object(scope -> up, name, false))
239 oberon_error(scope -> ctx, "already defined");
243 oberon_object_t * x = scope -> list;
244 while(x -> next && strcmp(x -> next -> name, name) != 0)
246 x = x -> next;
249 if(x -> next)
251 oberon_error(scope -> ctx, "already defined");
254 oberon_object_t * newvar;
255 newvar = oberon_create_object(scope, name, class, export, read_only);
256 x -> next = newvar;
258 return newvar;
261 static oberon_object_t *
262 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
264 oberon_object_t * id;
265 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
266 id -> type = type;
267 oberon_generator_init_type(scope -> ctx, type);
268 return id;
271 // =======================================================================
272 // SCANER
273 // =======================================================================
275 static void
276 oberon_get_char(oberon_context_t * ctx)
278 if(ctx -> code[ctx -> code_index])
280 ctx -> code_index += 1;
281 ctx -> c = ctx -> code[ctx -> code_index];
285 static void
286 oberon_init_scaner(oberon_context_t * ctx, const char * code)
288 ctx -> code = code;
289 ctx -> code_index = 0;
290 ctx -> c = ctx -> code[ctx -> code_index];
293 static void
294 oberon_read_ident(oberon_context_t * ctx)
296 int len = 0;
297 int i = ctx -> code_index;
299 int c = ctx -> code[i];
300 while(isalnum(c))
302 i += 1;
303 len += 1;
304 c = ctx -> code[i];
307 char * ident = malloc(len + 1);
308 memcpy(ident, &ctx->code[ctx->code_index], len);
309 ident[len] = 0;
311 ctx -> code_index = i;
312 ctx -> c = ctx -> code[i];
313 ctx -> string = ident;
314 ctx -> token = IDENT;
316 if(strcmp(ident, "MODULE") == 0)
318 ctx -> token = MODULE;
320 else if(strcmp(ident, "END") == 0)
322 ctx -> token = END;
324 else if(strcmp(ident, "VAR") == 0)
326 ctx -> token = VAR;
328 else if(strcmp(ident, "BEGIN") == 0)
330 ctx -> token = BEGIN;
332 else if(strcmp(ident, "TRUE") == 0)
334 ctx -> token = TRUE;
336 else if(strcmp(ident, "FALSE") == 0)
338 ctx -> token = FALSE;
340 else if(strcmp(ident, "OR") == 0)
342 ctx -> token = OR;
344 else if(strcmp(ident, "DIV") == 0)
346 ctx -> token = DIV;
348 else if(strcmp(ident, "MOD") == 0)
350 ctx -> token = MOD;
352 else if(strcmp(ident, "PROCEDURE") == 0)
354 ctx -> token = PROCEDURE;
356 else if(strcmp(ident, "RETURN") == 0)
358 ctx -> token = RETURN;
360 else if(strcmp(ident, "CONST") == 0)
362 ctx -> token = CONST;
364 else if(strcmp(ident, "TYPE") == 0)
366 ctx -> token = TYPE;
368 else if(strcmp(ident, "ARRAY") == 0)
370 ctx -> token = ARRAY;
372 else if(strcmp(ident, "OF") == 0)
374 ctx -> token = OF;
376 else if(strcmp(ident, "RECORD") == 0)
378 ctx -> token = RECORD;
380 else if(strcmp(ident, "POINTER") == 0)
382 ctx -> token = POINTER;
384 else if(strcmp(ident, "TO") == 0)
386 ctx -> token = TO;
388 else if(strcmp(ident, "NIL") == 0)
390 ctx -> token = NIL;
392 else if(strcmp(ident, "IMPORT") == 0)
394 ctx -> token = IMPORT;
396 else if(strcmp(ident, "IN") == 0)
398 ctx -> token = IN;
400 else if(strcmp(ident, "IS") == 0)
402 ctx -> token = IS;
404 else if(strcmp(ident, "IF") == 0)
406 ctx -> token = IF;
408 else if(strcmp(ident, "THEN") == 0)
410 ctx -> token = THEN;
412 else if(strcmp(ident, "ELSE") == 0)
414 ctx -> token = ELSE;
416 else if(strcmp(ident, "ELSIF") == 0)
418 ctx -> token = ELSIF;
420 else if(strcmp(ident, "WHILE") == 0)
422 ctx -> token = WHILE;
424 else if(strcmp(ident, "DO") == 0)
426 ctx -> token = DO;
428 else if(strcmp(ident, "REPEAT") == 0)
430 ctx -> token = REPEAT;
432 else if(strcmp(ident, "UNTIL") == 0)
434 ctx -> token = UNTIL;
436 else if(strcmp(ident, "FOR") == 0)
438 ctx -> token = FOR;
440 else if(strcmp(ident, "BY") == 0)
442 ctx -> token = BY;
444 else if(strcmp(ident, "LOOP") == 0)
446 ctx -> token = LOOP;
448 else if(strcmp(ident, "EXIT") == 0)
450 ctx -> token = EXIT;
454 static void
455 oberon_read_number(oberon_context_t * ctx)
457 long integer;
458 double real;
459 char * ident;
460 int start_i;
461 int exp_i;
462 int end_i;
464 /*
465 * mode = 0 == DEC
466 * mode = 1 == HEX
467 * mode = 2 == REAL
468 * mode = 3 == LONGREAL
469 * mode = 4 == CHAR
470 */
471 int mode = 0;
472 start_i = ctx -> code_index;
474 while(isdigit(ctx -> c))
476 oberon_get_char(ctx);
479 end_i = ctx -> code_index;
481 if(isxdigit(ctx -> c))
483 mode = 1;
484 while(isxdigit(ctx -> c))
486 oberon_get_char(ctx);
489 end_i = ctx -> code_index;
491 if(ctx -> c == 'H')
493 mode = 1;
494 oberon_get_char(ctx);
496 else if(ctx -> c == 'X')
498 mode = 4;
499 oberon_get_char(ctx);
501 else
503 oberon_error(ctx, "invalid hex number");
506 else if(ctx -> c == '.')
508 mode = 2;
509 oberon_get_char(ctx);
511 while(isdigit(ctx -> c))
513 oberon_get_char(ctx);
516 if(ctx -> c == 'E' || ctx -> c == 'D')
518 exp_i = ctx -> code_index;
520 if(ctx -> c == 'D')
522 mode = 3;
525 oberon_get_char(ctx);
527 if(ctx -> c == '+' || ctx -> c == '-')
529 oberon_get_char(ctx);
532 while(isdigit(ctx -> c))
534 oberon_get_char(ctx);
539 end_i = ctx -> code_index;
542 if(mode == 0)
544 if(ctx -> c == 'H')
546 mode = 1;
547 oberon_get_char(ctx);
549 else if(ctx -> c == 'X')
551 mode = 4;
552 oberon_get_char(ctx);
556 int len = end_i - start_i;
557 ident = malloc(len + 1);
558 memcpy(ident, &ctx -> code[start_i], len);
559 ident[len] = 0;
561 ctx -> longmode = false;
562 if(mode == 3)
564 int i = exp_i - start_i;
565 ident[i] = 'E';
566 ctx -> longmode = true;
569 switch(mode)
571 case 0:
572 integer = atol(ident);
573 real = integer;
574 ctx -> token = INTEGER;
575 break;
576 case 1:
577 sscanf(ident, "%lx", &integer);
578 real = integer;
579 ctx -> token = INTEGER;
580 break;
581 case 2:
582 case 3:
583 sscanf(ident, "%lf", &real);
584 ctx -> token = REAL;
585 break;
586 case 4:
587 sscanf(ident, "%lx", &integer);
588 real = integer;
589 ctx -> token = CHAR;
590 break;
591 default:
592 oberon_error(ctx, "oberon_read_number: wat");
593 break;
596 ctx -> string = ident;
597 ctx -> integer = integer;
598 ctx -> real = real;
601 static void
602 oberon_skip_space(oberon_context_t * ctx)
604 while(isspace(ctx -> c))
606 oberon_get_char(ctx);
610 static void
611 oberon_read_comment(oberon_context_t * ctx)
613 int nesting = 1;
614 while(nesting >= 1)
616 if(ctx -> c == '(')
618 oberon_get_char(ctx);
619 if(ctx -> c == '*')
621 oberon_get_char(ctx);
622 nesting += 1;
625 else if(ctx -> c == '*')
627 oberon_get_char(ctx);
628 if(ctx -> c == ')')
630 oberon_get_char(ctx);
631 nesting -= 1;
634 else if(ctx -> c == 0)
636 oberon_error(ctx, "unterminated comment");
638 else
640 oberon_get_char(ctx);
645 static void oberon_read_string(oberon_context_t * ctx)
647 int c = ctx -> c;
648 oberon_get_char(ctx);
650 int start = ctx -> code_index;
652 while(ctx -> c != 0 && ctx -> c != c)
654 oberon_get_char(ctx);
657 if(ctx -> c == 0)
659 oberon_error(ctx, "unterminated string");
662 int end = ctx -> code_index;
664 oberon_get_char(ctx);
666 char * string = calloc(1, end - start + 1);
667 strncpy(string, &ctx -> code[start], end - start);
669 ctx -> token = STRING;
670 ctx -> string = string;
672 printf("oberon_read_string: string ((%s))\n", string);
675 static void oberon_read_token(oberon_context_t * ctx);
677 static void
678 oberon_read_symbol(oberon_context_t * ctx)
680 int c = ctx -> c;
681 switch(c)
683 case 0:
684 ctx -> token = EOF_;
685 break;
686 case ';':
687 ctx -> token = SEMICOLON;
688 oberon_get_char(ctx);
689 break;
690 case ':':
691 ctx -> token = COLON;
692 oberon_get_char(ctx);
693 if(ctx -> c == '=')
695 ctx -> token = ASSIGN;
696 oberon_get_char(ctx);
698 break;
699 case '.':
700 ctx -> token = DOT;
701 oberon_get_char(ctx);
702 break;
703 case '(':
704 ctx -> token = LPAREN;
705 oberon_get_char(ctx);
706 if(ctx -> c == '*')
708 oberon_get_char(ctx);
709 oberon_read_comment(ctx);
710 oberon_read_token(ctx);
712 break;
713 case ')':
714 ctx -> token = RPAREN;
715 oberon_get_char(ctx);
716 break;
717 case '=':
718 ctx -> token = EQUAL;
719 oberon_get_char(ctx);
720 break;
721 case '#':
722 ctx -> token = NEQ;
723 oberon_get_char(ctx);
724 break;
725 case '<':
726 ctx -> token = LESS;
727 oberon_get_char(ctx);
728 if(ctx -> c == '=')
730 ctx -> token = LEQ;
731 oberon_get_char(ctx);
733 break;
734 case '>':
735 ctx -> token = GREAT;
736 oberon_get_char(ctx);
737 if(ctx -> c == '=')
739 ctx -> token = GEQ;
740 oberon_get_char(ctx);
742 break;
743 case '+':
744 ctx -> token = PLUS;
745 oberon_get_char(ctx);
746 break;
747 case '-':
748 ctx -> token = MINUS;
749 oberon_get_char(ctx);
750 break;
751 case '*':
752 ctx -> token = STAR;
753 oberon_get_char(ctx);
754 if(ctx -> c == ')')
756 oberon_get_char(ctx);
757 oberon_error(ctx, "unstarted comment");
759 break;
760 case '/':
761 ctx -> token = SLASH;
762 oberon_get_char(ctx);
763 break;
764 case '&':
765 ctx -> token = AND;
766 oberon_get_char(ctx);
767 break;
768 case '~':
769 ctx -> token = NOT;
770 oberon_get_char(ctx);
771 break;
772 case ',':
773 ctx -> token = COMMA;
774 oberon_get_char(ctx);
775 break;
776 case '[':
777 ctx -> token = LBRACE;
778 oberon_get_char(ctx);
779 break;
780 case ']':
781 ctx -> token = RBRACE;
782 oberon_get_char(ctx);
783 break;
784 case '^':
785 ctx -> token = UPARROW;
786 oberon_get_char(ctx);
787 break;
788 case '"':
789 oberon_read_string(ctx);
790 break;
791 case '\'':
792 oberon_read_string(ctx);
793 break;
794 default:
795 oberon_error(ctx, "invalid char %c", ctx -> c);
796 break;
800 static void
801 oberon_read_token(oberon_context_t * ctx)
803 oberon_skip_space(ctx);
805 int c = ctx -> c;
806 if(isalpha(c))
808 oberon_read_ident(ctx);
810 else if(isdigit(c))
812 oberon_read_number(ctx);
814 else
816 oberon_read_symbol(ctx);
820 // =======================================================================
821 // EXPRESSION
822 // =======================================================================
824 static void oberon_expect_token(oberon_context_t * ctx, int token);
825 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
826 static void oberon_assert_token(oberon_context_t * ctx, int token);
827 static char * oberon_assert_ident(oberon_context_t * ctx);
828 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
829 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
830 static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
832 static oberon_expr_t *
833 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
835 oberon_oper_t * operator;
836 operator = malloc(sizeof *operator);
837 memset(operator, 0, sizeof *operator);
839 operator -> is_item = 0;
840 operator -> result = result;
841 operator -> read_only = 1;
842 operator -> op = op;
843 operator -> left = left;
844 operator -> right = right;
846 return (oberon_expr_t *) operator;
849 static oberon_expr_t *
850 oberon_new_item(int mode, oberon_type_t * result, int read_only)
852 oberon_item_t * item;
853 item = malloc(sizeof *item);
854 memset(item, 0, sizeof *item);
856 item -> is_item = 1;
857 item -> result = result;
858 item -> read_only = read_only;
859 item -> mode = mode;
861 return (oberon_expr_t *)item;
864 static oberon_expr_t *
865 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
867 oberon_expr_t * expr;
868 oberon_type_t * result;
870 result = a -> result;
872 if(token == MINUS)
874 if(result -> class != OBERON_TYPE_INTEGER)
876 oberon_error(ctx, "incompatible operator type");
879 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
881 else if(token == NOT)
883 if(result -> class != OBERON_TYPE_BOOLEAN)
885 oberon_error(ctx, "incompatible operator type");
888 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
890 else
892 oberon_error(ctx, "oberon_make_unary_op: wat");
895 return expr;
898 static void
899 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
901 oberon_expr_t * last;
903 *num_expr = 1;
904 if(const_expr)
906 *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
908 else
910 *first = last = oberon_expr(ctx);
912 while(ctx -> token == COMMA)
914 oberon_assert_token(ctx, COMMA);
915 oberon_expr_t * current;
917 if(const_expr)
919 current = (oberon_expr_t *) oberon_const_expr(ctx);
921 else
923 current = oberon_expr(ctx);
926 last -> next = current;
927 last = current;
928 *num_expr += 1;
932 static oberon_expr_t *
933 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
935 return oberon_new_operator(OP_CAST, pref, expr, NULL);
938 static oberon_expr_t *
939 oberno_make_record_cast(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * rec)
941 oberon_type_t * from = expr -> result;
942 oberon_type_t * to = rec;
944 printf("oberno_make_record_cast: from class %i to class %i\n", from -> class, to -> class);
946 if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
948 printf("oberno_make_record_cast: pointers\n");
949 from = from -> base;
950 to = to -> base;
953 if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
955 oberon_error(ctx, "must be record type");
958 return oberon_cast_expr(ctx, expr, rec);
961 static oberon_type_t *
962 oberon_get_equal_expr_type(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
964 oberon_type_t * result;
965 if(a -> class == OBERON_TYPE_REAL && b -> class == OBERON_TYPE_INTEGER)
967 result = a;
969 else if(b -> class == OBERON_TYPE_REAL && a -> class == OBERON_TYPE_INTEGER)
971 result = b;
973 else if(a -> class != b -> class)
975 oberon_error(ctx, "oberon_get_equal_expr_type: incompatible types");
977 else if(a -> size > b -> size)
979 result = a;
981 else
983 result = b;
986 return result;
989 static void
990 oberon_check_record_compatibility(oberon_context_t * ctx, oberon_type_t * from, oberon_type_t * to)
992 if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
994 from = from -> base;
995 to = to -> base;
998 if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
1000 oberon_error(ctx, "not a record");
1003 oberon_type_t * t = from;
1004 while(t != NULL && t != to)
1006 t = t -> base;
1009 if(t == NULL)
1011 oberon_error(ctx, "incompatible record types");
1015 static void
1016 oberon_check_dst(oberon_context_t * ctx, oberon_expr_t * dst)
1018 if(dst -> is_item == false)
1020 oberon_error(ctx, "not variable");
1023 switch(dst -> item.mode)
1025 case MODE_VAR:
1026 case MODE_CALL:
1027 case MODE_INDEX:
1028 case MODE_FIELD:
1029 case MODE_DEREF:
1030 case MODE_NEW:
1031 /* accept */
1032 break;
1033 default:
1034 oberon_error(ctx, "not variable");
1035 break;
1039 static void
1040 oberon_check_src(oberon_context_t * ctx, oberon_expr_t * src)
1042 if(src -> is_item)
1044 if(src -> item.mode == MODE_TYPE)
1046 oberon_error(ctx, "not variable");
1051 static oberon_expr_t *
1052 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
1054 // Допускается:
1055 // Если классы типов равны
1056 // Если INTEGER переводится в REAL
1057 // Есди STRING переводится в ARRAY OF CHAR
1059 oberon_check_src(ctx, expr);
1061 bool error = false;
1062 if(pref -> class != expr -> result -> class)
1064 printf("expr class %i\n", expr -> result -> class);
1065 printf("pref class %i\n", pref -> class);
1067 if(expr -> result -> class == OBERON_TYPE_STRING)
1069 if(pref -> class == OBERON_TYPE_ARRAY)
1071 if(pref -> base -> class != OBERON_TYPE_CHAR)
1073 error = true;
1076 else
1078 error = true;
1081 else if(expr -> result -> class == OBERON_TYPE_INTEGER)
1083 if(pref -> class != OBERON_TYPE_REAL)
1085 error = true;
1088 else
1090 error = true;
1094 if(error)
1096 oberon_error(ctx, "oberon_autocast_to: incompatible types");
1099 if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL)
1101 if(expr -> result -> size > pref -> size)
1103 oberon_error(ctx, "incompatible size");
1105 else
1107 expr = oberon_cast_expr(ctx, expr, pref);
1110 else if(pref -> class == OBERON_TYPE_RECORD)
1112 oberon_check_record_compatibility(ctx, expr -> result, pref);
1113 expr = oberno_make_record_cast(ctx, expr, pref);
1115 else if(pref -> class == OBERON_TYPE_POINTER)
1117 assert(pref -> base);
1118 if(expr -> result -> base -> class == OBERON_TYPE_RECORD)
1120 oberon_check_record_compatibility(ctx, expr -> result, pref);
1121 expr = oberno_make_record_cast(ctx, expr, pref);
1123 else if(expr -> result -> base != pref -> base)
1125 if(expr -> result -> base -> class != OBERON_TYPE_VOID)
1127 oberon_error(ctx, "incompatible pointer types");
1132 return expr;
1135 static void
1136 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_expr_t ** ea, oberon_expr_t ** eb)
1138 oberon_type_t * a = (*ea) -> result;
1139 oberon_type_t * b = (*eb) -> result;
1140 oberon_type_t * preq = oberon_get_equal_expr_type(ctx, a, b);
1141 *ea = oberon_autocast_to(ctx, *ea, preq);
1142 *eb = oberon_autocast_to(ctx, *eb, preq);
1145 static void
1146 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
1148 if(desig -> mode != MODE_CALL)
1150 oberon_error(ctx, "expected mode CALL");
1153 oberon_type_t * fn = desig -> parent -> result;
1154 int num_args = desig -> num_args;
1155 int num_decl = fn -> num_decl;
1157 if(num_args < num_decl)
1159 oberon_error(ctx, "too few arguments");
1161 else if(num_args > num_decl)
1163 oberon_error(ctx, "too many arguments");
1166 /* Делаем проверку на запись и делаем автокаст */
1167 oberon_expr_t * casted[num_args];
1168 oberon_expr_t * arg = desig -> args;
1169 oberon_object_t * param = fn -> decl;
1170 for(int i = 0; i < num_args; i++)
1172 if(param -> class == OBERON_CLASS_VAR_PARAM)
1174 if(arg -> read_only)
1176 oberon_error(ctx, "assign to read-only var");
1180 casted[i] = oberon_autocast_to(ctx, arg, param -> type);
1181 arg = arg -> next;
1182 param = param -> next;
1185 /* Создаём новый список выражений */
1186 if(num_args > 0)
1188 arg = casted[0];
1189 for(int i = 0; i < num_args - 1; i++)
1191 casted[i] -> next = casted[i + 1];
1193 desig -> args = arg;
1197 static oberon_expr_t *
1198 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1200 oberon_type_t * signature = item -> result;
1201 if(signature -> class != OBERON_TYPE_PROCEDURE)
1203 oberon_error(ctx, "not a procedure");
1206 oberon_expr_t * call;
1208 if(signature -> sysproc)
1210 if(signature -> genfunc == NULL)
1212 oberon_error(ctx, "not a function-procedure");
1215 call = signature -> genfunc(ctx, num_args, list_args);
1217 else
1219 if(signature -> base -> class == OBERON_TYPE_VOID)
1221 oberon_error(ctx, "attempt to call procedure in expression");
1224 call = oberon_new_item(MODE_CALL, signature -> base, true);
1225 call -> item.parent = item;
1226 call -> item.num_args = num_args;
1227 call -> item.args = list_args;
1228 oberon_autocast_call(ctx, (oberon_item_t *) call);
1231 return call;
1234 static void
1235 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1237 oberon_type_t * signature = item -> result;
1238 if(signature -> class != OBERON_TYPE_PROCEDURE)
1240 oberon_error(ctx, "not a procedure");
1243 oberon_expr_t * call;
1245 if(signature -> sysproc)
1247 if(signature -> genproc == NULL)
1249 oberon_error(ctx, "not a procedure");
1252 signature -> genproc(ctx, num_args, list_args);
1254 else
1256 if(signature -> base -> class != OBERON_TYPE_VOID)
1258 oberon_error(ctx, "attempt to call function as non-typed procedure");
1261 call = oberon_new_item(MODE_CALL, signature -> base, true);
1262 call -> item.parent = item;
1263 call -> item.num_args = num_args;
1264 call -> item.args = list_args;
1265 oberon_autocast_call(ctx, (oberon_item_t *) call);
1266 oberon_generate_call_proc(ctx, call);
1270 #define ISEXPR(x) \
1271 (((x) == PLUS) \
1272 || ((x) == MINUS) \
1273 || ((x) == IDENT) \
1274 || ((x) == INTEGER) \
1275 || ((x) == REAL) \
1276 || ((x) == CHAR) \
1277 || ((x) == STRING) \
1278 || ((x) == NIL) \
1279 || ((x) == LPAREN) \
1280 || ((x) == NOT) \
1281 || ((x) == TRUE) \
1282 || ((x) == FALSE))
1284 static oberon_expr_t *
1285 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1287 printf("oberno_make_dereferencing\n");
1288 if(expr -> result -> class != OBERON_TYPE_POINTER)
1290 oberon_error(ctx, "not a pointer");
1293 assert(expr -> is_item);
1295 oberon_expr_t * selector;
1296 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
1297 selector -> item.parent = (oberon_item_t *) expr;
1299 return selector;
1302 static oberon_expr_t *
1303 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1305 if(desig -> result -> class == OBERON_TYPE_POINTER)
1307 desig = oberno_make_dereferencing(ctx, desig);
1310 assert(desig -> is_item);
1312 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1314 oberon_error(ctx, "not array");
1317 oberon_type_t * base;
1318 base = desig -> result -> base;
1320 if(index -> result -> class != OBERON_TYPE_INTEGER)
1322 oberon_error(ctx, "index must be integer");
1325 // Статическая проверка границ массива
1326 if(desig -> result -> size != 0)
1328 if(index -> is_item)
1330 if(index -> item.mode == MODE_INTEGER)
1332 int arr_size = desig -> result -> size;
1333 int index_int = index -> item.integer;
1334 if(index_int < 0 || index_int > arr_size - 1)
1336 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1342 oberon_expr_t * selector;
1343 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1344 selector -> item.parent = (oberon_item_t *) desig;
1345 selector -> item.num_args = 1;
1346 selector -> item.args = index;
1348 return selector;
1351 static oberon_expr_t *
1352 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1354 if(expr -> result -> class == OBERON_TYPE_POINTER)
1356 expr = oberno_make_dereferencing(ctx, expr);
1359 assert(expr -> is_item);
1361 if(expr -> result -> class != OBERON_TYPE_RECORD)
1363 oberon_error(ctx, "not record");
1366 oberon_type_t * rec = expr -> result;
1368 oberon_object_t * field;
1369 field = oberon_find_object(rec -> scope, name, true);
1371 if(field -> export == 0)
1373 if(field -> module != ctx -> mod)
1375 oberon_error(ctx, "field not exported");
1379 int read_only = 0;
1380 if(field -> read_only)
1382 if(field -> module != ctx -> mod)
1384 read_only = 1;
1388 oberon_expr_t * selector;
1389 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1390 selector -> item.var = field;
1391 selector -> item.parent = (oberon_item_t *) expr;
1393 return selector;
1396 #define ISSELECTOR(x) \
1397 (((x) == LBRACE) \
1398 || ((x) == DOT) \
1399 || ((x) == UPARROW) \
1400 || ((x) == LPAREN))
1402 static oberon_object_t *
1403 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1405 char * name;
1406 oberon_object_t * x;
1408 name = oberon_assert_ident(ctx);
1409 x = oberon_find_object(ctx -> decl, name, check);
1411 if(x != NULL)
1413 if(x -> class == OBERON_CLASS_MODULE)
1415 oberon_assert_token(ctx, DOT);
1416 name = oberon_assert_ident(ctx);
1417 /* Наличие объектов в левых модулях всегда проверяется */
1418 x = oberon_find_object(x -> module -> decl, name, 1);
1420 if(x -> export == 0)
1422 oberon_error(ctx, "not exported");
1427 if(xname)
1429 *xname = name;
1432 return x;
1435 static oberon_expr_t *
1436 oberon_ident_item(oberon_context_t * ctx, char * name)
1438 bool read_only;
1439 oberon_object_t * x;
1440 oberon_expr_t * expr;
1442 x = oberon_find_object(ctx -> decl, name, true);
1444 read_only = false;
1445 if(x -> class == OBERON_CLASS_CONST || x -> class == OBERON_CLASS_PROC)
1447 read_only = true;
1450 expr = oberon_new_item(MODE_VAR, x -> type, read_only);
1451 expr -> item.var = x;
1452 return expr;
1455 static oberon_expr_t *
1456 oberon_designator(oberon_context_t * ctx)
1458 char * name;
1459 oberon_object_t * var;
1460 oberon_expr_t * expr;
1462 var = oberon_qualident(ctx, NULL, 1);
1464 int read_only = 0;
1465 if(var -> read_only)
1467 if(var -> module != ctx -> mod)
1469 read_only = 1;
1473 switch(var -> class)
1475 case OBERON_CLASS_CONST:
1476 // TODO copy value
1477 expr = (oberon_expr_t *) var -> value;
1478 break;
1479 case OBERON_CLASS_TYPE:
1480 expr = oberon_new_item(MODE_TYPE, var -> type, read_only);
1481 break;
1482 case OBERON_CLASS_VAR:
1483 case OBERON_CLASS_VAR_PARAM:
1484 case OBERON_CLASS_PARAM:
1485 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1486 break;
1487 case OBERON_CLASS_PROC:
1488 expr = oberon_new_item(MODE_VAR, var -> type, true);
1489 break;
1490 default:
1491 oberon_error(ctx, "invalid designator");
1492 break;
1494 expr -> item.var = var;
1496 while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
1498 switch(ctx -> token)
1500 case DOT:
1501 oberon_assert_token(ctx, DOT);
1502 name = oberon_assert_ident(ctx);
1503 expr = oberon_make_record_selector(ctx, expr, name);
1504 break;
1505 case LBRACE:
1506 oberon_assert_token(ctx, LBRACE);
1507 int num_indexes = 0;
1508 oberon_expr_t * indexes = NULL;
1509 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1510 oberon_assert_token(ctx, RBRACE);
1512 for(int i = 0; i < num_indexes; i++)
1514 expr = oberon_make_array_selector(ctx, expr, indexes);
1515 indexes = indexes -> next;
1517 break;
1518 case UPARROW:
1519 oberon_assert_token(ctx, UPARROW);
1520 expr = oberno_make_dereferencing(ctx, expr);
1521 break;
1522 case LPAREN:
1523 oberon_assert_token(ctx, LPAREN);
1524 oberon_object_t * objtype = oberon_qualident(ctx, NULL, 1);
1525 if(objtype -> class != OBERON_CLASS_TYPE)
1527 oberon_error(ctx, "must be type");
1529 oberon_assert_token(ctx, RPAREN);
1530 expr = oberno_make_record_cast(ctx, expr, objtype -> type);
1531 break;
1532 default:
1533 oberon_error(ctx, "oberon_designator: wat");
1534 break;
1538 return expr;
1541 static oberon_expr_t *
1542 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1544 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1545 if(ctx -> token == LPAREN)
1547 oberon_assert_token(ctx, LPAREN);
1549 int num_args = 0;
1550 oberon_expr_t * arguments = NULL;
1552 if(ISEXPR(ctx -> token))
1554 oberon_expr_list(ctx, &num_args, &arguments, 0);
1557 assert(expr -> is_item == 1);
1558 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1560 oberon_assert_token(ctx, RPAREN);
1563 return expr;
1566 static void
1567 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1569 assert(expr -> is_item);
1571 int num_args = 0;
1572 oberon_expr_t * arguments = NULL;
1574 if(ctx -> token == LPAREN)
1576 oberon_assert_token(ctx, LPAREN);
1578 if(ISEXPR(ctx -> token))
1580 oberon_expr_list(ctx, &num_args, &arguments, 0);
1583 oberon_assert_token(ctx, RPAREN);
1586 /* Вызов происходит даже без скобок */
1587 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1590 static oberon_type_t *
1591 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
1593 if(i >= -128 && i <= 127)
1595 return ctx -> byte_type;
1597 else if(i >= -32768 && i <= 32767)
1599 return ctx -> shortint_type;
1601 else if(i >= -2147483648 && i <= 2147483647)
1603 return ctx -> int_type;
1605 else
1607 return ctx -> longint_type;
1611 static oberon_expr_t *
1612 oberon_integer_item(oberon_context_t * ctx, int64_t i)
1614 oberon_expr_t * expr;
1615 oberon_type_t * result;
1616 result = oberon_get_type_of_int_value(ctx, i);
1617 expr = oberon_new_item(MODE_INTEGER, result, true);
1618 expr -> item.integer = i;
1619 return expr;
1622 static oberon_expr_t *
1623 oberon_factor(oberon_context_t * ctx)
1625 oberon_expr_t * expr;
1626 oberon_type_t * result;
1628 switch(ctx -> token)
1630 case IDENT:
1631 expr = oberon_designator(ctx);
1632 expr = oberon_opt_func_parens(ctx, expr);
1633 break;
1634 case INTEGER:
1635 expr = oberon_integer_item(ctx, ctx -> integer);
1636 oberon_assert_token(ctx, INTEGER);
1637 break;
1638 case CHAR:
1639 result = ctx -> char_type;
1640 expr = oberon_new_item(MODE_CHAR, result, true);
1641 expr -> item.integer = ctx -> integer;
1642 oberon_assert_token(ctx, CHAR);
1643 break;
1644 case STRING:
1645 result = ctx -> string_type;
1646 expr = oberon_new_item(MODE_STRING, result, true);
1647 expr -> item.string = ctx -> string;
1648 oberon_assert_token(ctx, STRING);
1649 break;
1650 case REAL:
1651 result = (ctx -> longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
1652 expr = oberon_new_item(MODE_REAL, result, 1);
1653 expr -> item.real = ctx -> real;
1654 oberon_assert_token(ctx, REAL);
1655 break;
1656 case TRUE:
1657 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
1658 expr -> item.boolean = true;
1659 oberon_assert_token(ctx, TRUE);
1660 break;
1661 case FALSE:
1662 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
1663 expr -> item.boolean = false;
1664 oberon_assert_token(ctx, FALSE);
1665 break;
1666 case LPAREN:
1667 oberon_assert_token(ctx, LPAREN);
1668 expr = oberon_expr(ctx);
1669 oberon_assert_token(ctx, RPAREN);
1670 break;
1671 case NOT:
1672 oberon_assert_token(ctx, NOT);
1673 expr = oberon_factor(ctx);
1674 expr = oberon_make_unary_op(ctx, NOT, expr);
1675 break;
1676 case NIL:
1677 oberon_assert_token(ctx, NIL);
1678 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, true);
1679 break;
1680 default:
1681 oberon_error(ctx, "invalid expression");
1684 return expr;
1687 #define ITMAKESBOOLEAN(x) \
1688 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1690 #define ITUSEONLYINTEGER(x) \
1691 ((x) >= LESS && (x) <= GEQ)
1693 #define ITUSEONLYBOOLEAN(x) \
1694 (((x) == OR) || ((x) == AND))
1696 static void
1697 oberon_autocast_to_real(oberon_context_t * ctx, oberon_expr_t ** e)
1699 oberon_expr_t * expr = *e;
1700 if(expr -> result -> class == OBERON_TYPE_INTEGER)
1702 if(expr -> result -> size <= ctx -> real_type -> size)
1704 *e = oberon_cast_expr(ctx, expr, ctx -> real_type);
1706 else
1708 *e = oberon_cast_expr(ctx, expr, ctx -> longreal_type);
1711 else if(expr -> result -> class != OBERON_TYPE_REAL)
1713 oberon_error(ctx, "required numeric type");
1717 static oberon_expr_t *
1718 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1720 oberon_expr_t * expr;
1721 oberon_type_t * result;
1723 if(ITMAKESBOOLEAN(token))
1725 if(ITUSEONLYINTEGER(token))
1727 if(a -> result -> class == OBERON_TYPE_INTEGER
1728 || b -> result -> class == OBERON_TYPE_INTEGER
1729 || a -> result -> class == OBERON_TYPE_REAL
1730 || b -> result -> class == OBERON_TYPE_REAL)
1732 // accept
1734 else
1736 oberon_error(ctx, "used only with numeric types");
1739 else if(ITUSEONLYBOOLEAN(token))
1741 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1742 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1744 oberon_error(ctx, "used only with boolean type");
1748 oberon_autocast_binary_op(ctx, &a, &b);
1749 result = ctx -> bool_type;
1751 if(token == EQUAL)
1753 expr = oberon_new_operator(OP_EQ, result, a, b);
1755 else if(token == NEQ)
1757 expr = oberon_new_operator(OP_NEQ, result, a, b);
1759 else if(token == LESS)
1761 expr = oberon_new_operator(OP_LSS, result, a, b);
1763 else if(token == LEQ)
1765 expr = oberon_new_operator(OP_LEQ, result, a, b);
1767 else if(token == GREAT)
1769 expr = oberon_new_operator(OP_GRT, result, a, b);
1771 else if(token == GEQ)
1773 expr = oberon_new_operator(OP_GEQ, result, a, b);
1775 else if(token == OR)
1777 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1779 else if(token == AND)
1781 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1783 else
1785 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1788 else if(token == SLASH)
1790 oberon_autocast_to_real(ctx, &a);
1791 oberon_autocast_to_real(ctx, &b);
1792 oberon_autocast_binary_op(ctx, &a, &b);
1793 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1795 else if(token == DIV)
1797 if(a -> result -> class != OBERON_TYPE_INTEGER
1798 || b -> result -> class != OBERON_TYPE_INTEGER)
1800 oberon_error(ctx, "operator DIV requires integer type");
1803 oberon_autocast_binary_op(ctx, &a, &b);
1804 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1806 else
1808 oberon_autocast_binary_op(ctx, &a, &b);
1810 if(token == PLUS)
1812 expr = oberon_new_operator(OP_ADD, a -> result, a, b);
1814 else if(token == MINUS)
1816 expr = oberon_new_operator(OP_SUB, a -> result, a, b);
1818 else if(token == STAR)
1820 expr = oberon_new_operator(OP_MUL, a -> result, a, b);
1822 else if(token == MOD)
1824 expr = oberon_new_operator(OP_MOD, a -> result, a, b);
1826 else
1828 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1832 return expr;
1835 #define ISMULOP(x) \
1836 ((x) >= STAR && (x) <= AND)
1838 static oberon_expr_t *
1839 oberon_term_expr(oberon_context_t * ctx)
1841 oberon_expr_t * expr;
1843 expr = oberon_factor(ctx);
1844 while(ISMULOP(ctx -> token))
1846 int token = ctx -> token;
1847 oberon_read_token(ctx);
1849 oberon_expr_t * inter = oberon_factor(ctx);
1850 expr = oberon_make_bin_op(ctx, token, expr, inter);
1853 return expr;
1856 #define ISADDOP(x) \
1857 ((x) >= PLUS && (x) <= OR)
1859 static oberon_expr_t *
1860 oberon_simple_expr(oberon_context_t * ctx)
1862 oberon_expr_t * expr;
1864 int minus = 0;
1865 if(ctx -> token == PLUS)
1867 minus = 0;
1868 oberon_assert_token(ctx, PLUS);
1870 else if(ctx -> token == MINUS)
1872 minus = 1;
1873 oberon_assert_token(ctx, MINUS);
1876 expr = oberon_term_expr(ctx);
1878 if(minus)
1880 expr = oberon_make_unary_op(ctx, MINUS, expr);
1883 while(ISADDOP(ctx -> token))
1885 int token = ctx -> token;
1886 oberon_read_token(ctx);
1888 oberon_expr_t * inter = oberon_term_expr(ctx);
1889 expr = oberon_make_bin_op(ctx, token, expr, inter);
1892 return expr;
1895 #define ISRELATION(x) \
1896 ((x) >= EQUAL && (x) <= IS)
1898 static oberon_expr_t *
1899 oberon_expr(oberon_context_t * ctx)
1901 oberon_expr_t * expr;
1903 expr = oberon_simple_expr(ctx);
1904 while(ISRELATION(ctx -> token))
1906 int token = ctx -> token;
1907 oberon_read_token(ctx);
1909 oberon_expr_t * inter = oberon_simple_expr(ctx);
1910 expr = oberon_make_bin_op(ctx, token, expr, inter);
1913 return expr;
1916 static oberon_item_t *
1917 oberon_const_expr(oberon_context_t * ctx)
1919 oberon_expr_t * expr;
1920 expr = oberon_expr(ctx);
1922 if(expr -> is_item == 0)
1924 oberon_error(ctx, "const expression are required");
1927 switch(expr -> item.mode)
1929 case MODE_INTEGER:
1930 case MODE_BOOLEAN:
1931 case MODE_NIL:
1932 case MODE_REAL:
1933 case MODE_CHAR:
1934 case MODE_STRING:
1935 case MODE_TYPE:
1936 /* accept */
1937 break;
1938 default:
1939 oberon_error(ctx, "const expression are required");
1940 break;
1943 return (oberon_item_t *) expr;
1946 // =======================================================================
1947 // PARSER
1948 // =======================================================================
1950 static void oberon_decl_seq(oberon_context_t * ctx);
1951 static void oberon_statement_seq(oberon_context_t * ctx);
1952 static void oberon_initialize_decl(oberon_context_t * ctx);
1954 static void
1955 oberon_expect_token(oberon_context_t * ctx, int token)
1957 if(ctx -> token != token)
1959 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
1963 static void
1964 oberon_assert_token(oberon_context_t * ctx, int token)
1966 oberon_expect_token(ctx, token);
1967 oberon_read_token(ctx);
1970 static char *
1971 oberon_assert_ident(oberon_context_t * ctx)
1973 oberon_expect_token(ctx, IDENT);
1974 char * ident = ctx -> string;
1975 oberon_read_token(ctx);
1976 return ident;
1979 static void
1980 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
1982 switch(ctx -> token)
1984 case STAR:
1985 oberon_assert_token(ctx, STAR);
1986 *export = 1;
1987 *read_only = 0;
1988 break;
1989 case MINUS:
1990 oberon_assert_token(ctx, MINUS);
1991 *export = 1;
1992 *read_only = 1;
1993 break;
1994 default:
1995 *export = 0;
1996 *read_only = 0;
1997 break;
2001 static oberon_object_t *
2002 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
2004 char * name;
2005 int export;
2006 int read_only;
2007 oberon_object_t * x;
2009 name = oberon_assert_ident(ctx);
2010 oberon_def(ctx, &export, &read_only);
2012 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
2013 return x;
2016 static void
2017 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
2019 *num = 1;
2020 *list = oberon_ident_def(ctx, class, check_upscope);
2021 while(ctx -> token == COMMA)
2023 oberon_assert_token(ctx, COMMA);
2024 oberon_ident_def(ctx, class, check_upscope);
2025 *num += 1;
2029 static void
2030 oberon_var_decl(oberon_context_t * ctx)
2032 int num;
2033 oberon_object_t * list;
2034 oberon_type_t * type;
2035 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2037 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
2038 oberon_assert_token(ctx, COLON);
2039 oberon_type(ctx, &type);
2041 oberon_object_t * var = list;
2042 for(int i = 0; i < num; i++)
2044 var -> type = type;
2045 var = var -> next;
2049 static oberon_object_t *
2050 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
2052 int class = OBERON_CLASS_PARAM;
2053 if(ctx -> token == VAR)
2055 oberon_read_token(ctx);
2056 class = OBERON_CLASS_VAR_PARAM;
2059 int num;
2060 oberon_object_t * list;
2061 oberon_ident_list(ctx, class, false, &num, &list);
2063 oberon_assert_token(ctx, COLON);
2065 oberon_type_t * type;
2066 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2067 oberon_type(ctx, &type);
2069 oberon_object_t * param = list;
2070 for(int i = 0; i < num; i++)
2072 param -> type = type;
2073 param = param -> next;
2076 *num_decl += num;
2077 return list;
2080 #define ISFPSECTION \
2081 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2083 static void
2084 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2086 oberon_assert_token(ctx, LPAREN);
2088 if(ISFPSECTION)
2090 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2091 while(ctx -> token == SEMICOLON)
2093 oberon_assert_token(ctx, SEMICOLON);
2094 oberon_fp_section(ctx, &signature -> num_decl);
2098 oberon_assert_token(ctx, RPAREN);
2100 if(ctx -> token == COLON)
2102 oberon_assert_token(ctx, COLON);
2104 oberon_object_t * typeobj;
2105 typeobj = oberon_qualident(ctx, NULL, 1);
2106 if(typeobj -> class != OBERON_CLASS_TYPE)
2108 oberon_error(ctx, "function result is not type");
2110 signature -> base = typeobj -> type;
2114 static void
2115 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2117 oberon_type_t * signature;
2118 signature = *type;
2119 signature -> class = OBERON_TYPE_PROCEDURE;
2120 signature -> num_decl = 0;
2121 signature -> base = ctx -> void_type;
2122 signature -> decl = NULL;
2124 if(ctx -> token == LPAREN)
2126 oberon_formal_pars(ctx, signature);
2130 static void
2131 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2133 if(a -> num_decl != b -> num_decl)
2135 oberon_error(ctx, "number parameters not matched");
2138 int num_param = a -> num_decl;
2139 oberon_object_t * param_a = a -> decl;
2140 oberon_object_t * param_b = b -> decl;
2141 for(int i = 0; i < num_param; i++)
2143 if(strcmp(param_a -> name, param_b -> name) != 0)
2145 oberon_error(ctx, "param %i name not matched", i + 1);
2148 if(param_a -> type != param_b -> type)
2150 oberon_error(ctx, "param %i type not matched", i + 1);
2153 param_a = param_a -> next;
2154 param_b = param_b -> next;
2158 static void
2159 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2161 oberon_object_t * proc = ctx -> decl -> parent;
2162 oberon_type_t * result_type = proc -> type -> base;
2164 if(result_type -> class == OBERON_TYPE_VOID)
2166 if(expr != NULL)
2168 oberon_error(ctx, "procedure has no result type");
2171 else
2173 if(expr == NULL)
2175 oberon_error(ctx, "procedure requires expression on result");
2178 expr = oberon_autocast_to(ctx, expr, result_type);
2181 proc -> has_return = 1;
2183 oberon_generate_return(ctx, expr);
2186 static void
2187 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2189 oberon_assert_token(ctx, SEMICOLON);
2191 ctx -> decl = proc -> scope;
2193 oberon_decl_seq(ctx);
2195 oberon_generate_begin_proc(ctx, proc);
2197 if(ctx -> token == BEGIN)
2199 oberon_assert_token(ctx, BEGIN);
2200 oberon_statement_seq(ctx);
2203 oberon_assert_token(ctx, END);
2204 char * name = oberon_assert_ident(ctx);
2205 if(strcmp(name, proc -> name) != 0)
2207 oberon_error(ctx, "procedure name not matched");
2210 if(proc -> type -> base -> class == OBERON_TYPE_VOID
2211 && proc -> has_return == 0)
2213 oberon_make_return(ctx, NULL);
2216 if(proc -> has_return == 0)
2218 oberon_error(ctx, "procedure requires return");
2221 oberon_generate_end_proc(ctx);
2222 oberon_close_scope(ctx -> decl);
2225 static void
2226 oberon_proc_decl(oberon_context_t * ctx)
2228 oberon_assert_token(ctx, PROCEDURE);
2230 int forward = 0;
2231 if(ctx -> token == UPARROW)
2233 oberon_assert_token(ctx, UPARROW);
2234 forward = 1;
2237 char * name;
2238 int export;
2239 int read_only;
2240 name = oberon_assert_ident(ctx);
2241 oberon_def(ctx, &export, &read_only);
2243 oberon_scope_t * proc_scope;
2244 proc_scope = oberon_open_scope(ctx);
2245 ctx -> decl -> local = 1;
2247 oberon_type_t * signature;
2248 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
2249 oberon_opt_formal_pars(ctx, &signature);
2251 oberon_initialize_decl(ctx);
2252 oberon_generator_init_type(ctx, signature);
2253 oberon_close_scope(ctx -> decl);
2255 oberon_object_t * proc;
2256 proc = oberon_find_object(ctx -> decl, name, 0);
2257 if(proc != NULL)
2259 if(proc -> class != OBERON_CLASS_PROC)
2261 oberon_error(ctx, "mult definition");
2264 if(forward == 0)
2266 if(proc -> linked)
2268 oberon_error(ctx, "mult procedure definition");
2272 if(proc -> export != export || proc -> read_only != read_only)
2274 oberon_error(ctx, "export type not matched");
2277 oberon_compare_signatures(ctx, proc -> type, signature);
2279 else
2281 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2282 proc -> type = signature;
2283 proc -> scope = proc_scope;
2284 oberon_generator_init_proc(ctx, proc);
2287 proc -> scope -> parent = proc;
2289 if(forward == 0)
2291 proc -> linked = 1;
2292 oberon_proc_decl_body(ctx, proc);
2296 static void
2297 oberon_const_decl(oberon_context_t * ctx)
2299 oberon_item_t * value;
2300 oberon_object_t * constant;
2302 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2303 oberon_assert_token(ctx, EQUAL);
2304 value = oberon_const_expr(ctx);
2305 constant -> value = value;
2308 static void
2309 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2311 if(size -> is_item == 0)
2313 oberon_error(ctx, "requires constant");
2316 if(size -> item.mode != MODE_INTEGER)
2318 oberon_error(ctx, "requires integer constant");
2321 oberon_type_t * arr;
2322 arr = *type;
2323 arr -> class = OBERON_TYPE_ARRAY;
2324 arr -> size = size -> item.integer;
2325 arr -> base = base;
2328 static void
2329 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2331 char * name;
2332 oberon_object_t * to;
2334 to = oberon_qualident(ctx, &name, 0);
2336 //name = oberon_assert_ident(ctx);
2337 //to = oberon_find_object(ctx -> decl, name, 0);
2339 if(to != NULL)
2341 if(to -> class != OBERON_CLASS_TYPE)
2343 oberon_error(ctx, "not a type");
2346 else
2348 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2349 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2352 *type = to -> type;
2355 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2357 /*
2358 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2359 */
2361 static void
2362 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2364 if(sizes == NULL)
2366 *type = base;
2367 return;
2370 oberon_type_t * dim;
2371 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
2373 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2375 oberon_make_array_type(ctx, sizes, dim, type);
2378 static void
2379 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2381 type -> class = OBERON_TYPE_ARRAY;
2382 type -> size = 0;
2383 type -> base = base;
2386 static void
2387 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2389 if(ctx -> token == IDENT)
2391 int num;
2392 oberon_object_t * list;
2393 oberon_type_t * type;
2394 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2396 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2397 oberon_assert_token(ctx, COLON);
2399 oberon_scope_t * current = ctx -> decl;
2400 ctx -> decl = modscope;
2401 oberon_type(ctx, &type);
2402 ctx -> decl = current;
2404 oberon_object_t * field = list;
2405 for(int i = 0; i < num; i++)
2407 field -> type = type;
2408 field = field -> next;
2411 rec -> num_decl += num;
2415 static void
2416 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2418 oberon_scope_t * modscope = ctx -> mod -> decl;
2419 oberon_scope_t * oldscope = ctx -> decl;
2420 ctx -> decl = modscope;
2422 if(ctx -> token == LPAREN)
2424 oberon_assert_token(ctx, LPAREN);
2426 oberon_object_t * typeobj;
2427 typeobj = oberon_qualident(ctx, NULL, true);
2429 if(typeobj -> class != OBERON_CLASS_TYPE)
2431 oberon_error(ctx, "base must be type");
2434 oberon_type_t * base = typeobj -> type;
2435 if(base -> class == OBERON_TYPE_POINTER)
2437 base = base -> base;
2440 if(base -> class != OBERON_TYPE_RECORD)
2442 oberon_error(ctx, "base must be record type");
2445 rec -> base = base;
2446 ctx -> decl = base -> scope;
2448 oberon_assert_token(ctx, RPAREN);
2450 else
2452 ctx -> decl = NULL;
2455 oberon_scope_t * this_scope;
2456 this_scope = oberon_open_scope(ctx);
2457 this_scope -> local = true;
2458 this_scope -> parent = NULL;
2459 this_scope -> parent_type = rec;
2461 oberon_field_list(ctx, rec, modscope);
2462 while(ctx -> token == SEMICOLON)
2464 oberon_assert_token(ctx, SEMICOLON);
2465 oberon_field_list(ctx, rec, modscope);
2468 rec -> scope = this_scope;
2469 rec -> decl = this_scope -> list -> next;
2470 ctx -> decl = oldscope;
2473 static void
2474 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2476 if(ctx -> token == IDENT)
2478 oberon_qualident_type(ctx, type);
2480 else if(ctx -> token == ARRAY)
2482 oberon_assert_token(ctx, ARRAY);
2484 int num_sizes = 0;
2485 oberon_expr_t * sizes;
2487 if(ISEXPR(ctx -> token))
2489 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2492 oberon_assert_token(ctx, OF);
2494 oberon_type_t * base;
2495 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2496 oberon_type(ctx, &base);
2498 if(num_sizes == 0)
2500 oberon_make_open_array(ctx, base, *type);
2502 else
2504 oberon_make_multiarray(ctx, sizes, base, type);
2507 else if(ctx -> token == RECORD)
2509 oberon_type_t * rec;
2510 rec = *type;
2511 rec -> class = OBERON_TYPE_RECORD;
2512 rec -> module = ctx -> mod;
2514 oberon_assert_token(ctx, RECORD);
2515 oberon_type_record_body(ctx, rec);
2516 oberon_assert_token(ctx, END);
2518 *type = rec;
2520 else if(ctx -> token == POINTER)
2522 oberon_assert_token(ctx, POINTER);
2523 oberon_assert_token(ctx, TO);
2525 oberon_type_t * base;
2526 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2527 oberon_type(ctx, &base);
2529 oberon_type_t * ptr;
2530 ptr = *type;
2531 ptr -> class = OBERON_TYPE_POINTER;
2532 ptr -> base = base;
2534 else if(ctx -> token == PROCEDURE)
2536 oberon_open_scope(ctx);
2537 oberon_assert_token(ctx, PROCEDURE);
2538 oberon_opt_formal_pars(ctx, type);
2539 oberon_close_scope(ctx -> decl);
2541 else
2543 oberon_error(ctx, "invalid type declaration");
2547 static void
2548 oberon_type_decl(oberon_context_t * ctx)
2550 char * name;
2551 oberon_object_t * newtype;
2552 oberon_type_t * type;
2553 int export;
2554 int read_only;
2556 name = oberon_assert_ident(ctx);
2557 oberon_def(ctx, &export, &read_only);
2559 newtype = oberon_find_object(ctx -> decl, name, 0);
2560 if(newtype == NULL)
2562 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2563 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2564 assert(newtype -> type);
2566 else
2568 if(newtype -> class != OBERON_CLASS_TYPE)
2570 oberon_error(ctx, "mult definition");
2573 if(newtype -> linked)
2575 oberon_error(ctx, "mult definition - already linked");
2578 newtype -> export = export;
2579 newtype -> read_only = read_only;
2582 oberon_assert_token(ctx, EQUAL);
2584 type = newtype -> type;
2585 oberon_type(ctx, &type);
2587 if(type -> class == OBERON_TYPE_VOID)
2589 oberon_error(ctx, "recursive alias declaration");
2592 newtype -> type = type;
2593 newtype -> linked = 1;
2596 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2597 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2599 static void
2600 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2602 if(type -> class != OBERON_TYPE_POINTER
2603 && type -> class != OBERON_TYPE_ARRAY)
2605 return;
2608 if(type -> recursive)
2610 oberon_error(ctx, "recursive pointer declaration");
2613 if(type -> class == OBERON_TYPE_POINTER
2614 && type -> base -> class == OBERON_TYPE_POINTER)
2616 oberon_error(ctx, "attempt to make pointer to pointer");
2619 type -> recursive = 1;
2621 oberon_prevent_recursive_pointer(ctx, type -> base);
2623 type -> recursive = 0;
2626 static void
2627 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2629 if(type -> class != OBERON_TYPE_RECORD)
2631 return;
2634 if(type -> recursive)
2636 oberon_error(ctx, "recursive record declaration");
2639 type -> recursive = 1;
2641 int num_fields = type -> num_decl;
2642 oberon_object_t * field = type -> decl;
2643 for(int i = 0; i < num_fields; i++)
2645 oberon_prevent_recursive_object(ctx, field);
2646 field = field -> next;
2649 type -> recursive = 0;
2651 static void
2652 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2654 if(type -> class != OBERON_TYPE_PROCEDURE)
2656 return;
2659 if(type -> recursive)
2661 oberon_error(ctx, "recursive procedure declaration");
2664 type -> recursive = 1;
2666 int num_fields = type -> num_decl;
2667 oberon_object_t * field = type -> decl;
2668 for(int i = 0; i < num_fields; i++)
2670 oberon_prevent_recursive_object(ctx, field);
2671 field = field -> next;
2674 type -> recursive = 0;
2677 static void
2678 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2680 if(type -> class != OBERON_TYPE_ARRAY)
2682 return;
2685 if(type -> recursive)
2687 oberon_error(ctx, "recursive array declaration");
2690 type -> recursive = 1;
2692 oberon_prevent_recursive_type(ctx, type -> base);
2694 type -> recursive = 0;
2697 static void
2698 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2700 if(type -> class == OBERON_TYPE_POINTER)
2702 oberon_prevent_recursive_pointer(ctx, type);
2704 else if(type -> class == OBERON_TYPE_RECORD)
2706 oberon_prevent_recursive_record(ctx, type);
2708 else if(type -> class == OBERON_TYPE_ARRAY)
2710 oberon_prevent_recursive_array(ctx, type);
2712 else if(type -> class == OBERON_TYPE_PROCEDURE)
2714 oberon_prevent_recursive_procedure(ctx, type);
2718 static void
2719 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2721 switch(x -> class)
2723 case OBERON_CLASS_VAR:
2724 case OBERON_CLASS_TYPE:
2725 case OBERON_CLASS_PARAM:
2726 case OBERON_CLASS_VAR_PARAM:
2727 case OBERON_CLASS_FIELD:
2728 oberon_prevent_recursive_type(ctx, x -> type);
2729 break;
2730 case OBERON_CLASS_CONST:
2731 case OBERON_CLASS_PROC:
2732 case OBERON_CLASS_MODULE:
2733 break;
2734 default:
2735 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2736 break;
2740 static void
2741 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2743 oberon_object_t * x = ctx -> decl -> list -> next;
2745 while(x)
2747 oberon_prevent_recursive_object(ctx, x);
2748 x = x -> next;
2752 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2753 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2755 static void
2756 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2758 if(type -> class != OBERON_TYPE_RECORD)
2760 return;
2763 int num_fields = type -> num_decl;
2764 oberon_object_t * field = type -> decl;
2765 for(int i = 0; i < num_fields; i++)
2767 if(field -> type -> class == OBERON_TYPE_POINTER)
2769 oberon_initialize_type(ctx, field -> type);
2772 oberon_initialize_object(ctx, field);
2773 field = field -> next;
2776 oberon_generator_init_record(ctx, type);
2779 static void
2780 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2782 if(type -> class == OBERON_TYPE_VOID)
2784 oberon_error(ctx, "undeclarated type");
2787 if(type -> initialized)
2789 return;
2792 type -> initialized = 1;
2794 if(type -> class == OBERON_TYPE_POINTER)
2796 oberon_initialize_type(ctx, type -> base);
2797 oberon_generator_init_type(ctx, type);
2799 else if(type -> class == OBERON_TYPE_ARRAY)
2801 if(type -> size != 0)
2803 if(type -> base -> class == OBERON_TYPE_ARRAY)
2805 if(type -> base -> size == 0)
2807 oberon_error(ctx, "open array not allowed as array element");
2812 oberon_initialize_type(ctx, type -> base);
2813 oberon_generator_init_type(ctx, type);
2815 else if(type -> class == OBERON_TYPE_RECORD)
2817 oberon_generator_init_type(ctx, type);
2818 oberon_initialize_record_fields(ctx, type);
2820 else if(type -> class == OBERON_TYPE_PROCEDURE)
2822 int num_fields = type -> num_decl;
2823 oberon_object_t * field = type -> decl;
2824 for(int i = 0; i < num_fields; i++)
2826 oberon_initialize_object(ctx, field);
2827 field = field -> next;
2828 }
2830 oberon_generator_init_type(ctx, type);
2832 else
2834 oberon_generator_init_type(ctx, type);
2838 static void
2839 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2841 if(x -> initialized)
2843 return;
2846 x -> initialized = 1;
2848 switch(x -> class)
2850 case OBERON_CLASS_TYPE:
2851 oberon_initialize_type(ctx, x -> type);
2852 break;
2853 case OBERON_CLASS_VAR:
2854 case OBERON_CLASS_FIELD:
2855 if(x -> type -> class == OBERON_TYPE_ARRAY)
2857 if(x -> type -> size == 0)
2859 oberon_error(ctx, "open array not allowed as variable or field");
2862 oberon_initialize_type(ctx, x -> type);
2863 oberon_generator_init_var(ctx, x);
2864 break;
2865 case OBERON_CLASS_PARAM:
2866 case OBERON_CLASS_VAR_PARAM:
2867 oberon_initialize_type(ctx, x -> type);
2868 oberon_generator_init_var(ctx, x);
2869 break;
2870 case OBERON_CLASS_CONST:
2871 case OBERON_CLASS_PROC:
2872 case OBERON_CLASS_MODULE:
2873 break;
2874 default:
2875 oberon_error(ctx, "oberon_initialize_object: wat");
2876 break;
2880 static void
2881 oberon_initialize_decl(oberon_context_t * ctx)
2883 oberon_object_t * x = ctx -> decl -> list;
2885 while(x -> next)
2887 oberon_initialize_object(ctx, x -> next);
2888 x = x -> next;
2889 }
2892 static void
2893 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2895 oberon_object_t * x = ctx -> decl -> list;
2897 while(x -> next)
2899 if(x -> next -> class == OBERON_CLASS_PROC)
2901 if(x -> next -> linked == 0)
2903 oberon_error(ctx, "unresolved forward declaration");
2906 x = x -> next;
2907 }
2910 static void
2911 oberon_decl_seq(oberon_context_t * ctx)
2913 if(ctx -> token == CONST)
2915 oberon_assert_token(ctx, CONST);
2916 while(ctx -> token == IDENT)
2918 oberon_const_decl(ctx);
2919 oberon_assert_token(ctx, SEMICOLON);
2923 if(ctx -> token == TYPE)
2925 oberon_assert_token(ctx, TYPE);
2926 while(ctx -> token == IDENT)
2928 oberon_type_decl(ctx);
2929 oberon_assert_token(ctx, SEMICOLON);
2933 if(ctx -> token == VAR)
2935 oberon_assert_token(ctx, VAR);
2936 while(ctx -> token == IDENT)
2938 oberon_var_decl(ctx);
2939 oberon_assert_token(ctx, SEMICOLON);
2943 oberon_prevent_recursive_decl(ctx);
2944 oberon_initialize_decl(ctx);
2946 while(ctx -> token == PROCEDURE)
2948 oberon_proc_decl(ctx);
2949 oberon_assert_token(ctx, SEMICOLON);
2952 oberon_prevent_undeclarated_procedures(ctx);
2955 static oberon_expr_t *
2956 oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type)
2958 oberon_object_t * x;
2959 oberon_expr_t * expr;
2961 x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false);
2962 x -> local = true;
2963 x -> type = type;
2964 oberon_generator_init_temp_var(ctx, x);
2966 expr = oberon_new_item(MODE_VAR, type, false);
2967 expr -> item.var = x;
2968 return expr;
2971 static void
2972 oberon_statement_seq(oberon_context_t * ctx);
2974 static void
2975 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
2977 if(dst -> read_only)
2979 oberon_error(ctx, "read-only destination");
2982 oberon_check_dst(ctx, dst);
2983 src = oberon_autocast_to(ctx, src, dst -> result);
2984 oberon_generate_assign(ctx, src, dst);
2987 static void
2988 oberon_statement(oberon_context_t * ctx)
2990 oberon_expr_t * item1;
2991 oberon_expr_t * item2;
2993 if(ctx -> token == IDENT)
2995 item1 = oberon_designator(ctx);
2996 if(ctx -> token == ASSIGN)
2998 oberon_assert_token(ctx, ASSIGN);
2999 item2 = oberon_expr(ctx);
3000 oberon_assign(ctx, item2, item1);
3002 else
3004 oberon_opt_proc_parens(ctx, item1);
3007 else if(ctx -> token == IF)
3009 gen_label_t * end;
3010 gen_label_t * els;
3011 oberon_expr_t * cond;
3013 els = oberon_generator_reserve_label(ctx);
3014 end = oberon_generator_reserve_label(ctx);
3016 oberon_assert_token(ctx, IF);
3017 cond = oberon_expr(ctx);
3018 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3020 oberon_error(ctx, "condition must be boolean");
3022 oberon_assert_token(ctx, THEN);
3023 oberon_generate_branch(ctx, cond, false, els);
3024 oberon_statement_seq(ctx);
3025 oberon_generate_goto(ctx, end);
3026 oberon_generate_label(ctx, els);
3028 while(ctx -> token == ELSIF)
3030 els = oberon_generator_reserve_label(ctx);
3032 oberon_assert_token(ctx, ELSIF);
3033 cond = oberon_expr(ctx);
3034 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3036 oberon_error(ctx, "condition must be boolean");
3038 oberon_assert_token(ctx, THEN);
3039 oberon_generate_branch(ctx, cond, false, els);
3040 oberon_statement_seq(ctx);
3041 oberon_generate_goto(ctx, end);
3042 oberon_generate_label(ctx, els);
3045 if(ctx -> token == ELSE)
3047 oberon_assert_token(ctx, ELSE);
3048 oberon_statement_seq(ctx);
3051 oberon_generate_label(ctx, end);
3052 oberon_assert_token(ctx, END);
3054 else if(ctx -> token == WHILE)
3056 gen_label_t * begin;
3057 gen_label_t * end;
3058 oberon_expr_t * cond;
3060 begin = oberon_generator_reserve_label(ctx);
3061 end = oberon_generator_reserve_label(ctx);
3063 oberon_assert_token(ctx, WHILE);
3064 oberon_generate_label(ctx, begin);
3065 cond = oberon_expr(ctx);
3066 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3068 oberon_error(ctx, "condition must be boolean");
3070 oberon_generate_branch(ctx, cond, false, end);
3072 oberon_assert_token(ctx, DO);
3073 oberon_statement_seq(ctx);
3074 oberon_generate_goto(ctx, begin);
3076 oberon_assert_token(ctx, END);
3077 oberon_generate_label(ctx, end);
3079 else if(ctx -> token == REPEAT)
3081 gen_label_t * begin;
3082 oberon_expr_t * cond;
3084 begin = oberon_generator_reserve_label(ctx);
3085 oberon_generate_label(ctx, begin);
3086 oberon_assert_token(ctx, REPEAT);
3088 oberon_statement_seq(ctx);
3090 oberon_assert_token(ctx, UNTIL);
3092 cond = oberon_expr(ctx);
3093 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3095 oberon_error(ctx, "condition must be boolean");
3098 oberon_generate_branch(ctx, cond, true, begin);
3100 else if(ctx -> token == FOR)
3102 oberon_expr_t * from;
3103 oberon_expr_t * index;
3104 oberon_expr_t * to;
3105 oberon_expr_t * bound;
3106 oberon_expr_t * by;
3107 oberon_expr_t * cond;
3108 oberon_expr_t * count;
3109 gen_label_t * begin;
3110 gen_label_t * end;
3111 char * iname;
3112 int op;
3114 begin = oberon_generator_reserve_label(ctx);
3115 end = oberon_generator_reserve_label(ctx);
3117 oberon_assert_token(ctx, FOR);
3118 iname = oberon_assert_ident(ctx);
3119 index = oberon_ident_item(ctx, iname);
3120 oberon_assert_token(ctx, ASSIGN);
3121 from = oberon_expr(ctx);
3122 oberon_assign(ctx, from, index);
3123 oberon_assert_token(ctx, TO);
3124 bound = oberon_make_temp_var_item(ctx, index -> result);
3125 to = oberon_expr(ctx);
3126 oberon_assign(ctx, to, bound);
3127 if(ctx -> token == BY)
3129 oberon_assert_token(ctx, BY);
3130 by = (oberon_expr_t *) oberon_const_expr(ctx);
3132 else
3134 by = oberon_integer_item(ctx, 1);
3137 if(by -> result -> class != OBERON_TYPE_INTEGER)
3139 oberon_error(ctx, "must be integer");
3142 if(by -> item.integer > 0)
3144 op = LEQ;
3146 else if(by -> item.integer < 0)
3148 op = GEQ;
3150 else
3152 oberon_error(ctx, "zero step not allowed");
3155 oberon_assert_token(ctx, DO);
3156 oberon_generate_label(ctx, begin);
3157 cond = oberon_make_bin_op(ctx, op, index, bound);
3158 oberon_generate_branch(ctx, cond, false, end);
3159 oberon_statement_seq(ctx);
3160 count = oberon_make_bin_op(ctx, PLUS, index, by);
3161 oberon_assign(ctx, count, index);
3162 oberon_generate_goto(ctx, begin);
3163 oberon_generate_label(ctx, end);
3164 oberon_assert_token(ctx, END);
3166 else if(ctx -> token == LOOP)
3168 gen_label_t * begin;
3169 gen_label_t * end;
3171 begin = oberon_generator_reserve_label(ctx);
3172 end = oberon_generator_reserve_label(ctx);
3174 oberon_open_scope(ctx);
3175 oberon_assert_token(ctx, LOOP);
3176 oberon_generate_label(ctx, begin);
3177 ctx -> decl -> exit_label = end;
3178 oberon_statement_seq(ctx);
3179 oberon_generate_goto(ctx, begin);
3180 oberon_generate_label(ctx, end);
3181 oberon_assert_token(ctx, END);
3182 oberon_close_scope(ctx -> decl);
3184 else if(ctx -> token == EXIT)
3186 oberon_assert_token(ctx, EXIT);
3187 if(ctx -> decl -> exit_label == NULL)
3189 oberon_error(ctx, "not in LOOP-END");
3191 oberon_generate_goto(ctx, ctx -> decl -> exit_label);
3193 else if(ctx -> token == RETURN)
3195 oberon_assert_token(ctx, RETURN);
3196 if(ISEXPR(ctx -> token))
3198 oberon_expr_t * expr;
3199 expr = oberon_expr(ctx);
3200 oberon_make_return(ctx, expr);
3202 else
3204 oberon_make_return(ctx, NULL);
3209 static void
3210 oberon_statement_seq(oberon_context_t * ctx)
3212 oberon_statement(ctx);
3213 while(ctx -> token == SEMICOLON)
3215 oberon_assert_token(ctx, SEMICOLON);
3216 oberon_statement(ctx);
3220 static void
3221 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
3223 oberon_module_t * m = ctx -> module_list;
3224 while(m && strcmp(m -> name, name) != 0)
3226 m = m -> next;
3229 if(m == NULL)
3231 const char * code;
3232 code = ctx -> import_module(name);
3233 if(code == NULL)
3235 oberon_error(ctx, "no such module");
3238 m = oberon_compile_module(ctx, code);
3239 assert(m);
3242 if(m -> ready == 0)
3244 oberon_error(ctx, "cyclic module import");
3247 oberon_object_t * ident;
3248 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3249 ident -> module = m;
3252 static void
3253 oberon_import_decl(oberon_context_t * ctx)
3255 char * alias;
3256 char * name;
3258 alias = name = oberon_assert_ident(ctx);
3259 if(ctx -> token == ASSIGN)
3261 oberon_assert_token(ctx, ASSIGN);
3262 name = oberon_assert_ident(ctx);
3265 oberon_import_module(ctx, alias, name);
3268 static void
3269 oberon_import_list(oberon_context_t * ctx)
3271 oberon_assert_token(ctx, IMPORT);
3273 oberon_import_decl(ctx);
3274 while(ctx -> token == COMMA)
3276 oberon_assert_token(ctx, COMMA);
3277 oberon_import_decl(ctx);
3280 oberon_assert_token(ctx, SEMICOLON);
3283 static void
3284 oberon_parse_module(oberon_context_t * ctx)
3286 char * name1;
3287 char * name2;
3288 oberon_read_token(ctx);
3290 oberon_assert_token(ctx, MODULE);
3291 name1 = oberon_assert_ident(ctx);
3292 oberon_assert_token(ctx, SEMICOLON);
3293 ctx -> mod -> name = name1;
3295 oberon_generator_init_module(ctx, ctx -> mod);
3297 if(ctx -> token == IMPORT)
3299 oberon_import_list(ctx);
3302 oberon_decl_seq(ctx);
3304 oberon_generate_begin_module(ctx);
3305 if(ctx -> token == BEGIN)
3307 oberon_assert_token(ctx, BEGIN);
3308 oberon_statement_seq(ctx);
3310 oberon_generate_end_module(ctx);
3312 oberon_assert_token(ctx, END);
3313 name2 = oberon_assert_ident(ctx);
3314 oberon_assert_token(ctx, DOT);
3316 if(strcmp(name1, name2) != 0)
3318 oberon_error(ctx, "module name not matched");
3321 oberon_generator_fini_module(ctx -> mod);
3324 // =======================================================================
3325 // LIBRARY
3326 // =======================================================================
3328 static void
3329 register_default_types(oberon_context_t * ctx)
3331 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
3332 oberon_generator_init_type(ctx, ctx -> void_type);
3334 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
3335 ctx -> void_ptr_type -> base = ctx -> void_type;
3336 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
3338 ctx -> string_type = oberon_new_type_string(1);
3339 oberon_generator_init_type(ctx, ctx -> string_type);
3341 ctx -> bool_type = oberon_new_type_boolean();
3342 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
3344 ctx -> byte_type = oberon_new_type_integer(1);
3345 oberon_define_type(ctx -> world_scope, "BYTE", ctx -> byte_type, 1);
3347 ctx -> shortint_type = oberon_new_type_integer(2);
3348 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> shortint_type, 1);
3350 ctx -> int_type = oberon_new_type_integer(4);
3351 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type, 1);
3353 ctx -> longint_type = oberon_new_type_integer(8);
3354 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> longint_type, 1);
3356 ctx -> real_type = oberon_new_type_real(4);
3357 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
3359 ctx -> longreal_type = oberon_new_type_real(8);
3360 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
3362 ctx -> char_type = oberon_new_type_char(1);
3363 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
3366 static void
3367 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
3369 oberon_object_t * proc;
3370 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
3371 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
3372 proc -> type -> sysproc = true;
3373 proc -> type -> genfunc = f;
3374 proc -> type -> genproc = p;
3377 static oberon_expr_t *
3378 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3380 if(num_args < 1)
3382 oberon_error(ctx, "too few arguments");
3385 if(num_args > 1)
3387 oberon_error(ctx, "too mach arguments");
3390 oberon_expr_t * arg;
3391 arg = list_args;
3393 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3395 oberon_error(ctx, "MIN accept only type");
3398 oberon_expr_t * expr;
3399 int bits = arg -> result -> size * 8;
3400 switch(arg -> result -> class)
3402 case OBERON_TYPE_INTEGER:
3403 expr = oberon_integer_item(ctx, -powl(2, bits - 1));
3404 break;
3405 default:
3406 oberon_error(ctx, "allowed only basic types");
3407 break;
3410 return expr;
3413 static oberon_expr_t *
3414 oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3416 if(num_args < 1)
3418 oberon_error(ctx, "too few arguments");
3421 if(num_args > 1)
3423 oberon_error(ctx, "too mach arguments");
3426 oberon_expr_t * arg;
3427 arg = list_args;
3429 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3431 oberon_error(ctx, "MAX accept only type");
3434 oberon_expr_t * expr;
3435 int bits = arg -> result -> size * 8;
3436 switch(arg -> result -> class)
3438 case OBERON_TYPE_INTEGER:
3439 expr = oberon_integer_item(ctx, powl(2, bits - 1) - 1);
3440 break;
3441 default:
3442 oberon_error(ctx, "allowed only basic types");
3443 break;
3446 return expr;
3449 static oberon_expr_t *
3450 oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3452 if(num_args < 1)
3454 oberon_error(ctx, "too few arguments");
3457 if(num_args > 1)
3459 oberon_error(ctx, "too mach arguments");
3462 oberon_expr_t * arg;
3463 arg = list_args;
3465 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3467 oberon_error(ctx, "SIZE accept only type");
3470 int size;
3471 oberon_expr_t * expr;
3472 oberon_type_t * type = arg -> result;
3473 switch(type -> class)
3475 case OBERON_TYPE_INTEGER:
3476 case OBERON_TYPE_BOOLEAN:
3477 case OBERON_TYPE_REAL:
3478 size = type -> size;
3479 break;
3480 default:
3481 oberon_error(ctx, "TODO SIZE");
3482 break;
3485 expr = oberon_integer_item(ctx, size);
3486 return expr;
3489 static oberon_expr_t *
3490 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3492 if(num_args < 1)
3494 oberon_error(ctx, "too few arguments");
3497 if(num_args > 1)
3499 oberon_error(ctx, "too mach arguments");
3502 oberon_expr_t * arg;
3503 arg = list_args;
3505 oberon_type_t * result_type;
3506 result_type = arg -> result;
3508 if(result_type -> class != OBERON_TYPE_INTEGER)
3510 oberon_error(ctx, "ABS accepts only integers");
3513 oberon_expr_t * expr;
3514 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
3515 return expr;
3518 static void
3519 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3521 if(num_args < 1)
3523 oberon_error(ctx, "too few arguments");
3526 oberon_expr_t * dst;
3527 dst = list_args;
3529 oberon_type_t * type;
3530 type = dst -> result;
3532 if(type -> class != OBERON_TYPE_POINTER)
3534 oberon_error(ctx, "not a pointer");
3537 type = type -> base;
3539 oberon_expr_t * src;
3540 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3541 src -> item.num_args = 0;
3542 src -> item.args = NULL;
3544 int max_args = 1;
3545 if(type -> class == OBERON_TYPE_ARRAY)
3547 if(type -> size == 0)
3549 oberon_type_t * x = type;
3550 while(x -> class == OBERON_TYPE_ARRAY)
3552 if(x -> size == 0)
3554 max_args += 1;
3556 x = x -> base;
3560 if(num_args < max_args)
3562 oberon_error(ctx, "too few arguments");
3565 if(num_args > max_args)
3567 oberon_error(ctx, "too mach arguments");
3570 int num_sizes = max_args - 1;
3571 oberon_expr_t * size_list = list_args -> next;
3573 oberon_expr_t * arg = size_list;
3574 for(int i = 0; i < max_args - 1; i++)
3576 if(arg -> result -> class != OBERON_TYPE_INTEGER)
3578 oberon_error(ctx, "size must be integer");
3580 arg = arg -> next;
3583 src -> item.num_args = num_sizes;
3584 src -> item.args = size_list;
3586 else if(type -> class != OBERON_TYPE_RECORD)
3588 oberon_error(ctx, "oberon_make_new_call: wat");
3591 if(num_args > max_args)
3593 oberon_error(ctx, "too mach arguments");
3596 oberon_assign(ctx, src, dst);
3599 oberon_context_t *
3600 oberon_create_context(ModuleImportCallback import_module)
3602 oberon_context_t * ctx = calloc(1, sizeof *ctx);
3604 oberon_scope_t * world_scope;
3605 world_scope = oberon_open_scope(ctx);
3606 ctx -> world_scope = world_scope;
3608 ctx -> import_module = import_module;
3610 oberon_generator_init_context(ctx);
3612 register_default_types(ctx);
3614 /* Functions */
3615 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
3616 oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
3617 oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
3618 oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
3620 /* Procedures */
3621 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
3623 return ctx;
3626 void
3627 oberon_destroy_context(oberon_context_t * ctx)
3629 oberon_generator_destroy_context(ctx);
3630 free(ctx);
3633 oberon_module_t *
3634 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
3636 const char * code = ctx -> code;
3637 int code_index = ctx -> code_index;
3638 char c = ctx -> c;
3639 int token = ctx -> token;
3640 char * string = ctx -> string;
3641 int integer = ctx -> integer;
3642 int real = ctx -> real;
3643 bool longmode = ctx -> longmode;
3644 oberon_scope_t * decl = ctx -> decl;
3645 oberon_module_t * mod = ctx -> mod;
3647 oberon_scope_t * module_scope;
3648 module_scope = oberon_open_scope(ctx);
3650 oberon_module_t * module;
3651 module = calloc(1, sizeof *module);
3652 module -> decl = module_scope;
3653 module -> next = ctx -> module_list;
3655 ctx -> mod = module;
3656 ctx -> module_list = module;
3658 oberon_init_scaner(ctx, newcode);
3659 oberon_parse_module(ctx);
3661 module -> ready = 1;
3663 ctx -> code = code;
3664 ctx -> code_index = code_index;
3665 ctx -> c = c;
3666 ctx -> token = token;
3667 ctx -> string = string;
3668 ctx -> integer = integer;
3669 ctx -> real = real;
3670 ctx -> longmode = longmode;
3671 ctx -> decl = decl;
3672 ctx -> mod = mod;
3674 return module;