DEADSOFTWARE

e88ff79159e28e93c010a73ebde045ee8a6c9132
[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(token == IS)
1725 oberon_type_t * v = a -> result;
1726 if(v -> class == OBERON_TYPE_POINTER)
1728 v = v -> base;
1729 if(v -> class != OBERON_TYPE_RECORD)
1731 oberon_error(ctx, "must be record");
1734 else if(v -> class != OBERON_TYPE_RECORD)
1736 oberon_error(ctx, "must be record");
1737 }
1739 if(b -> is_item == false || b -> item.mode != MODE_TYPE)
1741 oberon_error(ctx, "requires type");
1744 oberon_type_t * t = b -> result;
1745 if(t -> class == OBERON_TYPE_POINTER)
1747 t = t -> base;
1748 if(t -> class != OBERON_TYPE_RECORD)
1750 oberon_error(ctx, "must be record");
1753 else if(t -> class != OBERON_TYPE_RECORD)
1755 oberon_error(ctx, "must be record");
1758 result = ctx -> bool_type;
1759 expr = oberon_new_operator(OP_IS, result, a, b);
1761 else if(ITMAKESBOOLEAN(token))
1763 if(ITUSEONLYINTEGER(token))
1765 if(a -> result -> class == OBERON_TYPE_INTEGER
1766 || b -> result -> class == OBERON_TYPE_INTEGER
1767 || a -> result -> class == OBERON_TYPE_REAL
1768 || b -> result -> class == OBERON_TYPE_REAL)
1770 // accept
1772 else
1774 oberon_error(ctx, "used only with numeric types");
1777 else if(ITUSEONLYBOOLEAN(token))
1779 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1780 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1782 oberon_error(ctx, "used only with boolean type");
1786 oberon_autocast_binary_op(ctx, &a, &b);
1787 result = ctx -> bool_type;
1789 if(token == EQUAL)
1791 expr = oberon_new_operator(OP_EQ, result, a, b);
1793 else if(token == NEQ)
1795 expr = oberon_new_operator(OP_NEQ, result, a, b);
1797 else if(token == LESS)
1799 expr = oberon_new_operator(OP_LSS, result, a, b);
1801 else if(token == LEQ)
1803 expr = oberon_new_operator(OP_LEQ, result, a, b);
1805 else if(token == GREAT)
1807 expr = oberon_new_operator(OP_GRT, result, a, b);
1809 else if(token == GEQ)
1811 expr = oberon_new_operator(OP_GEQ, result, a, b);
1813 else if(token == OR)
1815 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1817 else if(token == AND)
1819 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1821 else
1823 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1826 else if(token == SLASH)
1828 oberon_autocast_to_real(ctx, &a);
1829 oberon_autocast_to_real(ctx, &b);
1830 oberon_autocast_binary_op(ctx, &a, &b);
1831 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1833 else if(token == DIV)
1835 if(a -> result -> class != OBERON_TYPE_INTEGER
1836 || b -> result -> class != OBERON_TYPE_INTEGER)
1838 oberon_error(ctx, "operator DIV requires integer type");
1841 oberon_autocast_binary_op(ctx, &a, &b);
1842 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1844 else
1846 oberon_autocast_binary_op(ctx, &a, &b);
1848 if(token == PLUS)
1850 expr = oberon_new_operator(OP_ADD, a -> result, a, b);
1852 else if(token == MINUS)
1854 expr = oberon_new_operator(OP_SUB, a -> result, a, b);
1856 else if(token == STAR)
1858 expr = oberon_new_operator(OP_MUL, a -> result, a, b);
1860 else if(token == MOD)
1862 expr = oberon_new_operator(OP_MOD, a -> result, a, b);
1864 else
1866 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1870 return expr;
1873 #define ISMULOP(x) \
1874 ((x) >= STAR && (x) <= AND)
1876 static oberon_expr_t *
1877 oberon_term_expr(oberon_context_t * ctx)
1879 oberon_expr_t * expr;
1881 expr = oberon_factor(ctx);
1882 while(ISMULOP(ctx -> token))
1884 int token = ctx -> token;
1885 oberon_read_token(ctx);
1887 oberon_expr_t * inter = oberon_factor(ctx);
1888 expr = oberon_make_bin_op(ctx, token, expr, inter);
1891 return expr;
1894 #define ISADDOP(x) \
1895 ((x) >= PLUS && (x) <= OR)
1897 static oberon_expr_t *
1898 oberon_simple_expr(oberon_context_t * ctx)
1900 oberon_expr_t * expr;
1902 int minus = 0;
1903 if(ctx -> token == PLUS)
1905 minus = 0;
1906 oberon_assert_token(ctx, PLUS);
1908 else if(ctx -> token == MINUS)
1910 minus = 1;
1911 oberon_assert_token(ctx, MINUS);
1914 expr = oberon_term_expr(ctx);
1916 if(minus)
1918 expr = oberon_make_unary_op(ctx, MINUS, expr);
1921 while(ISADDOP(ctx -> token))
1923 int token = ctx -> token;
1924 oberon_read_token(ctx);
1926 oberon_expr_t * inter = oberon_term_expr(ctx);
1927 expr = oberon_make_bin_op(ctx, token, expr, inter);
1930 return expr;
1933 #define ISRELATION(x) \
1934 ((x) >= EQUAL && (x) <= IS)
1936 static oberon_expr_t *
1937 oberon_expr(oberon_context_t * ctx)
1939 oberon_expr_t * expr;
1941 expr = oberon_simple_expr(ctx);
1942 while(ISRELATION(ctx -> token))
1944 int token = ctx -> token;
1945 oberon_read_token(ctx);
1947 oberon_expr_t * inter = oberon_simple_expr(ctx);
1948 expr = oberon_make_bin_op(ctx, token, expr, inter);
1951 return expr;
1954 static oberon_item_t *
1955 oberon_const_expr(oberon_context_t * ctx)
1957 oberon_expr_t * expr;
1958 expr = oberon_expr(ctx);
1960 if(expr -> is_item == 0)
1962 oberon_error(ctx, "const expression are required");
1965 switch(expr -> item.mode)
1967 case MODE_INTEGER:
1968 case MODE_BOOLEAN:
1969 case MODE_NIL:
1970 case MODE_REAL:
1971 case MODE_CHAR:
1972 case MODE_STRING:
1973 case MODE_TYPE:
1974 /* accept */
1975 break;
1976 default:
1977 oberon_error(ctx, "const expression are required");
1978 break;
1981 return (oberon_item_t *) expr;
1984 // =======================================================================
1985 // PARSER
1986 // =======================================================================
1988 static void oberon_decl_seq(oberon_context_t * ctx);
1989 static void oberon_statement_seq(oberon_context_t * ctx);
1990 static void oberon_initialize_decl(oberon_context_t * ctx);
1992 static void
1993 oberon_expect_token(oberon_context_t * ctx, int token)
1995 if(ctx -> token != token)
1997 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
2001 static void
2002 oberon_assert_token(oberon_context_t * ctx, int token)
2004 oberon_expect_token(ctx, token);
2005 oberon_read_token(ctx);
2008 static char *
2009 oberon_assert_ident(oberon_context_t * ctx)
2011 oberon_expect_token(ctx, IDENT);
2012 char * ident = ctx -> string;
2013 oberon_read_token(ctx);
2014 return ident;
2017 static void
2018 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
2020 switch(ctx -> token)
2022 case STAR:
2023 oberon_assert_token(ctx, STAR);
2024 *export = 1;
2025 *read_only = 0;
2026 break;
2027 case MINUS:
2028 oberon_assert_token(ctx, MINUS);
2029 *export = 1;
2030 *read_only = 1;
2031 break;
2032 default:
2033 *export = 0;
2034 *read_only = 0;
2035 break;
2039 static oberon_object_t *
2040 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
2042 char * name;
2043 int export;
2044 int read_only;
2045 oberon_object_t * x;
2047 name = oberon_assert_ident(ctx);
2048 oberon_def(ctx, &export, &read_only);
2050 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
2051 return x;
2054 static void
2055 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
2057 *num = 1;
2058 *list = oberon_ident_def(ctx, class, check_upscope);
2059 while(ctx -> token == COMMA)
2061 oberon_assert_token(ctx, COMMA);
2062 oberon_ident_def(ctx, class, check_upscope);
2063 *num += 1;
2067 static void
2068 oberon_var_decl(oberon_context_t * ctx)
2070 int num;
2071 oberon_object_t * list;
2072 oberon_type_t * type;
2073 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2075 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
2076 oberon_assert_token(ctx, COLON);
2077 oberon_type(ctx, &type);
2079 oberon_object_t * var = list;
2080 for(int i = 0; i < num; i++)
2082 var -> type = type;
2083 var = var -> next;
2087 static oberon_object_t *
2088 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
2090 int class = OBERON_CLASS_PARAM;
2091 if(ctx -> token == VAR)
2093 oberon_read_token(ctx);
2094 class = OBERON_CLASS_VAR_PARAM;
2097 int num;
2098 oberon_object_t * list;
2099 oberon_ident_list(ctx, class, false, &num, &list);
2101 oberon_assert_token(ctx, COLON);
2103 oberon_type_t * type;
2104 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2105 oberon_type(ctx, &type);
2107 oberon_object_t * param = list;
2108 for(int i = 0; i < num; i++)
2110 param -> type = type;
2111 param = param -> next;
2114 *num_decl += num;
2115 return list;
2118 #define ISFPSECTION \
2119 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2121 static void
2122 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2124 oberon_assert_token(ctx, LPAREN);
2126 if(ISFPSECTION)
2128 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2129 while(ctx -> token == SEMICOLON)
2131 oberon_assert_token(ctx, SEMICOLON);
2132 oberon_fp_section(ctx, &signature -> num_decl);
2136 oberon_assert_token(ctx, RPAREN);
2138 if(ctx -> token == COLON)
2140 oberon_assert_token(ctx, COLON);
2142 oberon_object_t * typeobj;
2143 typeobj = oberon_qualident(ctx, NULL, 1);
2144 if(typeobj -> class != OBERON_CLASS_TYPE)
2146 oberon_error(ctx, "function result is not type");
2148 signature -> base = typeobj -> type;
2152 static void
2153 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2155 oberon_type_t * signature;
2156 signature = *type;
2157 signature -> class = OBERON_TYPE_PROCEDURE;
2158 signature -> num_decl = 0;
2159 signature -> base = ctx -> void_type;
2160 signature -> decl = NULL;
2162 if(ctx -> token == LPAREN)
2164 oberon_formal_pars(ctx, signature);
2168 static void
2169 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2171 if(a -> num_decl != b -> num_decl)
2173 oberon_error(ctx, "number parameters not matched");
2176 int num_param = a -> num_decl;
2177 oberon_object_t * param_a = a -> decl;
2178 oberon_object_t * param_b = b -> decl;
2179 for(int i = 0; i < num_param; i++)
2181 if(strcmp(param_a -> name, param_b -> name) != 0)
2183 oberon_error(ctx, "param %i name not matched", i + 1);
2186 if(param_a -> type != param_b -> type)
2188 oberon_error(ctx, "param %i type not matched", i + 1);
2191 param_a = param_a -> next;
2192 param_b = param_b -> next;
2196 static void
2197 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2199 oberon_object_t * proc = ctx -> decl -> parent;
2200 oberon_type_t * result_type = proc -> type -> base;
2202 if(result_type -> class == OBERON_TYPE_VOID)
2204 if(expr != NULL)
2206 oberon_error(ctx, "procedure has no result type");
2209 else
2211 if(expr == NULL)
2213 oberon_error(ctx, "procedure requires expression on result");
2216 expr = oberon_autocast_to(ctx, expr, result_type);
2219 proc -> has_return = 1;
2221 oberon_generate_return(ctx, expr);
2224 static void
2225 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2227 oberon_assert_token(ctx, SEMICOLON);
2229 ctx -> decl = proc -> scope;
2231 oberon_decl_seq(ctx);
2233 oberon_generate_begin_proc(ctx, proc);
2235 if(ctx -> token == BEGIN)
2237 oberon_assert_token(ctx, BEGIN);
2238 oberon_statement_seq(ctx);
2241 oberon_assert_token(ctx, END);
2242 char * name = oberon_assert_ident(ctx);
2243 if(strcmp(name, proc -> name) != 0)
2245 oberon_error(ctx, "procedure name not matched");
2248 if(proc -> type -> base -> class == OBERON_TYPE_VOID
2249 && proc -> has_return == 0)
2251 oberon_make_return(ctx, NULL);
2254 if(proc -> has_return == 0)
2256 oberon_error(ctx, "procedure requires return");
2259 oberon_generate_end_proc(ctx);
2260 oberon_close_scope(ctx -> decl);
2263 static void
2264 oberon_proc_decl(oberon_context_t * ctx)
2266 oberon_assert_token(ctx, PROCEDURE);
2268 int forward = 0;
2269 if(ctx -> token == UPARROW)
2271 oberon_assert_token(ctx, UPARROW);
2272 forward = 1;
2275 char * name;
2276 int export;
2277 int read_only;
2278 name = oberon_assert_ident(ctx);
2279 oberon_def(ctx, &export, &read_only);
2281 oberon_scope_t * proc_scope;
2282 proc_scope = oberon_open_scope(ctx);
2283 ctx -> decl -> local = 1;
2285 oberon_type_t * signature;
2286 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
2287 oberon_opt_formal_pars(ctx, &signature);
2289 oberon_initialize_decl(ctx);
2290 oberon_generator_init_type(ctx, signature);
2291 oberon_close_scope(ctx -> decl);
2293 oberon_object_t * proc;
2294 proc = oberon_find_object(ctx -> decl, name, 0);
2295 if(proc != NULL)
2297 if(proc -> class != OBERON_CLASS_PROC)
2299 oberon_error(ctx, "mult definition");
2302 if(forward == 0)
2304 if(proc -> linked)
2306 oberon_error(ctx, "mult procedure definition");
2310 if(proc -> export != export || proc -> read_only != read_only)
2312 oberon_error(ctx, "export type not matched");
2315 oberon_compare_signatures(ctx, proc -> type, signature);
2317 else
2319 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2320 proc -> type = signature;
2321 proc -> scope = proc_scope;
2322 oberon_generator_init_proc(ctx, proc);
2325 proc -> scope -> parent = proc;
2327 if(forward == 0)
2329 proc -> linked = 1;
2330 oberon_proc_decl_body(ctx, proc);
2334 static void
2335 oberon_const_decl(oberon_context_t * ctx)
2337 oberon_item_t * value;
2338 oberon_object_t * constant;
2340 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2341 oberon_assert_token(ctx, EQUAL);
2342 value = oberon_const_expr(ctx);
2343 constant -> value = value;
2346 static void
2347 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2349 if(size -> is_item == 0)
2351 oberon_error(ctx, "requires constant");
2354 if(size -> item.mode != MODE_INTEGER)
2356 oberon_error(ctx, "requires integer constant");
2359 oberon_type_t * arr;
2360 arr = *type;
2361 arr -> class = OBERON_TYPE_ARRAY;
2362 arr -> size = size -> item.integer;
2363 arr -> base = base;
2366 static void
2367 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2369 char * name;
2370 oberon_object_t * to;
2372 to = oberon_qualident(ctx, &name, 0);
2374 //name = oberon_assert_ident(ctx);
2375 //to = oberon_find_object(ctx -> decl, name, 0);
2377 if(to != NULL)
2379 if(to -> class != OBERON_CLASS_TYPE)
2381 oberon_error(ctx, "not a type");
2384 else
2386 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2387 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2390 *type = to -> type;
2393 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2395 /*
2396 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2397 */
2399 static void
2400 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2402 if(sizes == NULL)
2404 *type = base;
2405 return;
2408 oberon_type_t * dim;
2409 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
2411 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2413 oberon_make_array_type(ctx, sizes, dim, type);
2416 static void
2417 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2419 type -> class = OBERON_TYPE_ARRAY;
2420 type -> size = 0;
2421 type -> base = base;
2424 static void
2425 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2427 if(ctx -> token == IDENT)
2429 int num;
2430 oberon_object_t * list;
2431 oberon_type_t * type;
2432 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2434 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2435 oberon_assert_token(ctx, COLON);
2437 oberon_scope_t * current = ctx -> decl;
2438 ctx -> decl = modscope;
2439 oberon_type(ctx, &type);
2440 ctx -> decl = current;
2442 oberon_object_t * field = list;
2443 for(int i = 0; i < num; i++)
2445 field -> type = type;
2446 field = field -> next;
2449 rec -> num_decl += num;
2453 static void
2454 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2456 oberon_scope_t * modscope = ctx -> mod -> decl;
2457 oberon_scope_t * oldscope = ctx -> decl;
2458 ctx -> decl = modscope;
2460 if(ctx -> token == LPAREN)
2462 oberon_assert_token(ctx, LPAREN);
2464 oberon_object_t * typeobj;
2465 typeobj = oberon_qualident(ctx, NULL, true);
2467 if(typeobj -> class != OBERON_CLASS_TYPE)
2469 oberon_error(ctx, "base must be type");
2472 oberon_type_t * base = typeobj -> type;
2473 if(base -> class == OBERON_TYPE_POINTER)
2475 base = base -> base;
2478 if(base -> class != OBERON_TYPE_RECORD)
2480 oberon_error(ctx, "base must be record type");
2483 rec -> base = base;
2484 ctx -> decl = base -> scope;
2486 oberon_assert_token(ctx, RPAREN);
2488 else
2490 ctx -> decl = NULL;
2493 oberon_scope_t * this_scope;
2494 this_scope = oberon_open_scope(ctx);
2495 this_scope -> local = true;
2496 this_scope -> parent = NULL;
2497 this_scope -> parent_type = rec;
2499 oberon_field_list(ctx, rec, modscope);
2500 while(ctx -> token == SEMICOLON)
2502 oberon_assert_token(ctx, SEMICOLON);
2503 oberon_field_list(ctx, rec, modscope);
2506 rec -> scope = this_scope;
2507 rec -> decl = this_scope -> list -> next;
2508 ctx -> decl = oldscope;
2511 static void
2512 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2514 if(ctx -> token == IDENT)
2516 oberon_qualident_type(ctx, type);
2518 else if(ctx -> token == ARRAY)
2520 oberon_assert_token(ctx, ARRAY);
2522 int num_sizes = 0;
2523 oberon_expr_t * sizes;
2525 if(ISEXPR(ctx -> token))
2527 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2530 oberon_assert_token(ctx, OF);
2532 oberon_type_t * base;
2533 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2534 oberon_type(ctx, &base);
2536 if(num_sizes == 0)
2538 oberon_make_open_array(ctx, base, *type);
2540 else
2542 oberon_make_multiarray(ctx, sizes, base, type);
2545 else if(ctx -> token == RECORD)
2547 oberon_type_t * rec;
2548 rec = *type;
2549 rec -> class = OBERON_TYPE_RECORD;
2550 rec -> module = ctx -> mod;
2552 oberon_assert_token(ctx, RECORD);
2553 oberon_type_record_body(ctx, rec);
2554 oberon_assert_token(ctx, END);
2556 *type = rec;
2558 else if(ctx -> token == POINTER)
2560 oberon_assert_token(ctx, POINTER);
2561 oberon_assert_token(ctx, TO);
2563 oberon_type_t * base;
2564 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2565 oberon_type(ctx, &base);
2567 oberon_type_t * ptr;
2568 ptr = *type;
2569 ptr -> class = OBERON_TYPE_POINTER;
2570 ptr -> base = base;
2572 else if(ctx -> token == PROCEDURE)
2574 oberon_open_scope(ctx);
2575 oberon_assert_token(ctx, PROCEDURE);
2576 oberon_opt_formal_pars(ctx, type);
2577 oberon_close_scope(ctx -> decl);
2579 else
2581 oberon_error(ctx, "invalid type declaration");
2585 static void
2586 oberon_type_decl(oberon_context_t * ctx)
2588 char * name;
2589 oberon_object_t * newtype;
2590 oberon_type_t * type;
2591 int export;
2592 int read_only;
2594 name = oberon_assert_ident(ctx);
2595 oberon_def(ctx, &export, &read_only);
2597 newtype = oberon_find_object(ctx -> decl, name, 0);
2598 if(newtype == NULL)
2600 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2601 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2602 assert(newtype -> type);
2604 else
2606 if(newtype -> class != OBERON_CLASS_TYPE)
2608 oberon_error(ctx, "mult definition");
2611 if(newtype -> linked)
2613 oberon_error(ctx, "mult definition - already linked");
2616 newtype -> export = export;
2617 newtype -> read_only = read_only;
2620 oberon_assert_token(ctx, EQUAL);
2622 type = newtype -> type;
2623 oberon_type(ctx, &type);
2625 if(type -> class == OBERON_TYPE_VOID)
2627 oberon_error(ctx, "recursive alias declaration");
2630 newtype -> type = type;
2631 newtype -> linked = 1;
2634 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2635 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2637 static void
2638 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2640 if(type -> class != OBERON_TYPE_POINTER
2641 && type -> class != OBERON_TYPE_ARRAY)
2643 return;
2646 if(type -> recursive)
2648 oberon_error(ctx, "recursive pointer declaration");
2651 if(type -> class == OBERON_TYPE_POINTER
2652 && type -> base -> class == OBERON_TYPE_POINTER)
2654 oberon_error(ctx, "attempt to make pointer to pointer");
2657 type -> recursive = 1;
2659 oberon_prevent_recursive_pointer(ctx, type -> base);
2661 type -> recursive = 0;
2664 static void
2665 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2667 if(type -> class != OBERON_TYPE_RECORD)
2669 return;
2672 if(type -> recursive)
2674 oberon_error(ctx, "recursive record declaration");
2677 type -> recursive = 1;
2679 int num_fields = type -> num_decl;
2680 oberon_object_t * field = type -> decl;
2681 for(int i = 0; i < num_fields; i++)
2683 oberon_prevent_recursive_object(ctx, field);
2684 field = field -> next;
2687 type -> recursive = 0;
2689 static void
2690 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2692 if(type -> class != OBERON_TYPE_PROCEDURE)
2694 return;
2697 if(type -> recursive)
2699 oberon_error(ctx, "recursive procedure declaration");
2702 type -> recursive = 1;
2704 int num_fields = type -> num_decl;
2705 oberon_object_t * field = type -> decl;
2706 for(int i = 0; i < num_fields; i++)
2708 oberon_prevent_recursive_object(ctx, field);
2709 field = field -> next;
2712 type -> recursive = 0;
2715 static void
2716 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2718 if(type -> class != OBERON_TYPE_ARRAY)
2720 return;
2723 if(type -> recursive)
2725 oberon_error(ctx, "recursive array declaration");
2728 type -> recursive = 1;
2730 oberon_prevent_recursive_type(ctx, type -> base);
2732 type -> recursive = 0;
2735 static void
2736 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2738 if(type -> class == OBERON_TYPE_POINTER)
2740 oberon_prevent_recursive_pointer(ctx, type);
2742 else if(type -> class == OBERON_TYPE_RECORD)
2744 oberon_prevent_recursive_record(ctx, type);
2746 else if(type -> class == OBERON_TYPE_ARRAY)
2748 oberon_prevent_recursive_array(ctx, type);
2750 else if(type -> class == OBERON_TYPE_PROCEDURE)
2752 oberon_prevent_recursive_procedure(ctx, type);
2756 static void
2757 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2759 switch(x -> class)
2761 case OBERON_CLASS_VAR:
2762 case OBERON_CLASS_TYPE:
2763 case OBERON_CLASS_PARAM:
2764 case OBERON_CLASS_VAR_PARAM:
2765 case OBERON_CLASS_FIELD:
2766 oberon_prevent_recursive_type(ctx, x -> type);
2767 break;
2768 case OBERON_CLASS_CONST:
2769 case OBERON_CLASS_PROC:
2770 case OBERON_CLASS_MODULE:
2771 break;
2772 default:
2773 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2774 break;
2778 static void
2779 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2781 oberon_object_t * x = ctx -> decl -> list -> next;
2783 while(x)
2785 oberon_prevent_recursive_object(ctx, x);
2786 x = x -> next;
2790 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2791 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2793 static void
2794 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2796 if(type -> class != OBERON_TYPE_RECORD)
2798 return;
2801 int num_fields = type -> num_decl;
2802 oberon_object_t * field = type -> decl;
2803 for(int i = 0; i < num_fields; i++)
2805 if(field -> type -> class == OBERON_TYPE_POINTER)
2807 oberon_initialize_type(ctx, field -> type);
2810 oberon_initialize_object(ctx, field);
2811 field = field -> next;
2814 oberon_generator_init_record(ctx, type);
2817 static void
2818 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2820 if(type -> class == OBERON_TYPE_VOID)
2822 oberon_error(ctx, "undeclarated type");
2825 if(type -> initialized)
2827 return;
2830 type -> initialized = 1;
2832 if(type -> class == OBERON_TYPE_POINTER)
2834 oberon_initialize_type(ctx, type -> base);
2835 oberon_generator_init_type(ctx, type);
2837 else if(type -> class == OBERON_TYPE_ARRAY)
2839 if(type -> size != 0)
2841 if(type -> base -> class == OBERON_TYPE_ARRAY)
2843 if(type -> base -> size == 0)
2845 oberon_error(ctx, "open array not allowed as array element");
2850 oberon_initialize_type(ctx, type -> base);
2851 oberon_generator_init_type(ctx, type);
2853 else if(type -> class == OBERON_TYPE_RECORD)
2855 oberon_generator_init_type(ctx, type);
2856 oberon_initialize_record_fields(ctx, type);
2858 else if(type -> class == OBERON_TYPE_PROCEDURE)
2860 int num_fields = type -> num_decl;
2861 oberon_object_t * field = type -> decl;
2862 for(int i = 0; i < num_fields; i++)
2864 oberon_initialize_object(ctx, field);
2865 field = field -> next;
2866 }
2868 oberon_generator_init_type(ctx, type);
2870 else
2872 oberon_generator_init_type(ctx, type);
2876 static void
2877 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2879 if(x -> initialized)
2881 return;
2884 x -> initialized = 1;
2886 switch(x -> class)
2888 case OBERON_CLASS_TYPE:
2889 oberon_initialize_type(ctx, x -> type);
2890 break;
2891 case OBERON_CLASS_VAR:
2892 case OBERON_CLASS_FIELD:
2893 if(x -> type -> class == OBERON_TYPE_ARRAY)
2895 if(x -> type -> size == 0)
2897 oberon_error(ctx, "open array not allowed as variable or field");
2900 oberon_initialize_type(ctx, x -> type);
2901 oberon_generator_init_var(ctx, x);
2902 break;
2903 case OBERON_CLASS_PARAM:
2904 case OBERON_CLASS_VAR_PARAM:
2905 oberon_initialize_type(ctx, x -> type);
2906 oberon_generator_init_var(ctx, x);
2907 break;
2908 case OBERON_CLASS_CONST:
2909 case OBERON_CLASS_PROC:
2910 case OBERON_CLASS_MODULE:
2911 break;
2912 default:
2913 oberon_error(ctx, "oberon_initialize_object: wat");
2914 break;
2918 static void
2919 oberon_initialize_decl(oberon_context_t * ctx)
2921 oberon_object_t * x = ctx -> decl -> list;
2923 while(x -> next)
2925 oberon_initialize_object(ctx, x -> next);
2926 x = x -> next;
2927 }
2930 static void
2931 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2933 oberon_object_t * x = ctx -> decl -> list;
2935 while(x -> next)
2937 if(x -> next -> class == OBERON_CLASS_PROC)
2939 if(x -> next -> linked == 0)
2941 oberon_error(ctx, "unresolved forward declaration");
2944 x = x -> next;
2945 }
2948 static void
2949 oberon_decl_seq(oberon_context_t * ctx)
2951 if(ctx -> token == CONST)
2953 oberon_assert_token(ctx, CONST);
2954 while(ctx -> token == IDENT)
2956 oberon_const_decl(ctx);
2957 oberon_assert_token(ctx, SEMICOLON);
2961 if(ctx -> token == TYPE)
2963 oberon_assert_token(ctx, TYPE);
2964 while(ctx -> token == IDENT)
2966 oberon_type_decl(ctx);
2967 oberon_assert_token(ctx, SEMICOLON);
2971 if(ctx -> token == VAR)
2973 oberon_assert_token(ctx, VAR);
2974 while(ctx -> token == IDENT)
2976 oberon_var_decl(ctx);
2977 oberon_assert_token(ctx, SEMICOLON);
2981 oberon_prevent_recursive_decl(ctx);
2982 oberon_initialize_decl(ctx);
2984 while(ctx -> token == PROCEDURE)
2986 oberon_proc_decl(ctx);
2987 oberon_assert_token(ctx, SEMICOLON);
2990 oberon_prevent_undeclarated_procedures(ctx);
2993 static oberon_expr_t *
2994 oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type)
2996 oberon_object_t * x;
2997 oberon_expr_t * expr;
2999 x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false);
3000 x -> local = true;
3001 x -> type = type;
3002 oberon_generator_init_temp_var(ctx, x);
3004 expr = oberon_new_item(MODE_VAR, type, false);
3005 expr -> item.var = x;
3006 return expr;
3009 static void
3010 oberon_statement_seq(oberon_context_t * ctx);
3012 static void
3013 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
3015 if(dst -> read_only)
3017 oberon_error(ctx, "read-only destination");
3020 oberon_check_dst(ctx, dst);
3021 src = oberon_autocast_to(ctx, src, dst -> result);
3022 oberon_generate_assign(ctx, src, dst);
3025 static void
3026 oberon_statement(oberon_context_t * ctx)
3028 oberon_expr_t * item1;
3029 oberon_expr_t * item2;
3031 if(ctx -> token == IDENT)
3033 item1 = oberon_designator(ctx);
3034 if(ctx -> token == ASSIGN)
3036 oberon_assert_token(ctx, ASSIGN);
3037 item2 = oberon_expr(ctx);
3038 oberon_assign(ctx, item2, item1);
3040 else
3042 oberon_opt_proc_parens(ctx, item1);
3045 else if(ctx -> token == IF)
3047 gen_label_t * end;
3048 gen_label_t * els;
3049 oberon_expr_t * cond;
3051 els = oberon_generator_reserve_label(ctx);
3052 end = oberon_generator_reserve_label(ctx);
3054 oberon_assert_token(ctx, IF);
3055 cond = oberon_expr(ctx);
3056 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3058 oberon_error(ctx, "condition must be boolean");
3060 oberon_assert_token(ctx, THEN);
3061 oberon_generate_branch(ctx, cond, false, els);
3062 oberon_statement_seq(ctx);
3063 oberon_generate_goto(ctx, end);
3064 oberon_generate_label(ctx, els);
3066 while(ctx -> token == ELSIF)
3068 els = oberon_generator_reserve_label(ctx);
3070 oberon_assert_token(ctx, ELSIF);
3071 cond = oberon_expr(ctx);
3072 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3074 oberon_error(ctx, "condition must be boolean");
3076 oberon_assert_token(ctx, THEN);
3077 oberon_generate_branch(ctx, cond, false, els);
3078 oberon_statement_seq(ctx);
3079 oberon_generate_goto(ctx, end);
3080 oberon_generate_label(ctx, els);
3083 if(ctx -> token == ELSE)
3085 oberon_assert_token(ctx, ELSE);
3086 oberon_statement_seq(ctx);
3089 oberon_generate_label(ctx, end);
3090 oberon_assert_token(ctx, END);
3092 else if(ctx -> token == WHILE)
3094 gen_label_t * begin;
3095 gen_label_t * end;
3096 oberon_expr_t * cond;
3098 begin = oberon_generator_reserve_label(ctx);
3099 end = oberon_generator_reserve_label(ctx);
3101 oberon_assert_token(ctx, WHILE);
3102 oberon_generate_label(ctx, begin);
3103 cond = oberon_expr(ctx);
3104 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3106 oberon_error(ctx, "condition must be boolean");
3108 oberon_generate_branch(ctx, cond, false, end);
3110 oberon_assert_token(ctx, DO);
3111 oberon_statement_seq(ctx);
3112 oberon_generate_goto(ctx, begin);
3114 oberon_assert_token(ctx, END);
3115 oberon_generate_label(ctx, end);
3117 else if(ctx -> token == REPEAT)
3119 gen_label_t * begin;
3120 oberon_expr_t * cond;
3122 begin = oberon_generator_reserve_label(ctx);
3123 oberon_generate_label(ctx, begin);
3124 oberon_assert_token(ctx, REPEAT);
3126 oberon_statement_seq(ctx);
3128 oberon_assert_token(ctx, UNTIL);
3130 cond = oberon_expr(ctx);
3131 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3133 oberon_error(ctx, "condition must be boolean");
3136 oberon_generate_branch(ctx, cond, true, begin);
3138 else if(ctx -> token == FOR)
3140 oberon_expr_t * from;
3141 oberon_expr_t * index;
3142 oberon_expr_t * to;
3143 oberon_expr_t * bound;
3144 oberon_expr_t * by;
3145 oberon_expr_t * cond;
3146 oberon_expr_t * count;
3147 gen_label_t * begin;
3148 gen_label_t * end;
3149 char * iname;
3150 int op;
3152 begin = oberon_generator_reserve_label(ctx);
3153 end = oberon_generator_reserve_label(ctx);
3155 oberon_assert_token(ctx, FOR);
3156 iname = oberon_assert_ident(ctx);
3157 index = oberon_ident_item(ctx, iname);
3158 oberon_assert_token(ctx, ASSIGN);
3159 from = oberon_expr(ctx);
3160 oberon_assign(ctx, from, index);
3161 oberon_assert_token(ctx, TO);
3162 bound = oberon_make_temp_var_item(ctx, index -> result);
3163 to = oberon_expr(ctx);
3164 oberon_assign(ctx, to, bound);
3165 if(ctx -> token == BY)
3167 oberon_assert_token(ctx, BY);
3168 by = (oberon_expr_t *) oberon_const_expr(ctx);
3170 else
3172 by = oberon_integer_item(ctx, 1);
3175 if(by -> result -> class != OBERON_TYPE_INTEGER)
3177 oberon_error(ctx, "must be integer");
3180 if(by -> item.integer > 0)
3182 op = LEQ;
3184 else if(by -> item.integer < 0)
3186 op = GEQ;
3188 else
3190 oberon_error(ctx, "zero step not allowed");
3193 oberon_assert_token(ctx, DO);
3194 oberon_generate_label(ctx, begin);
3195 cond = oberon_make_bin_op(ctx, op, index, bound);
3196 oberon_generate_branch(ctx, cond, false, end);
3197 oberon_statement_seq(ctx);
3198 count = oberon_make_bin_op(ctx, PLUS, index, by);
3199 oberon_assign(ctx, count, index);
3200 oberon_generate_goto(ctx, begin);
3201 oberon_generate_label(ctx, end);
3202 oberon_assert_token(ctx, END);
3204 else if(ctx -> token == LOOP)
3206 gen_label_t * begin;
3207 gen_label_t * end;
3209 begin = oberon_generator_reserve_label(ctx);
3210 end = oberon_generator_reserve_label(ctx);
3212 oberon_open_scope(ctx);
3213 oberon_assert_token(ctx, LOOP);
3214 oberon_generate_label(ctx, begin);
3215 ctx -> decl -> exit_label = end;
3216 oberon_statement_seq(ctx);
3217 oberon_generate_goto(ctx, begin);
3218 oberon_generate_label(ctx, end);
3219 oberon_assert_token(ctx, END);
3220 oberon_close_scope(ctx -> decl);
3222 else if(ctx -> token == EXIT)
3224 oberon_assert_token(ctx, EXIT);
3225 if(ctx -> decl -> exit_label == NULL)
3227 oberon_error(ctx, "not in LOOP-END");
3229 oberon_generate_goto(ctx, ctx -> decl -> exit_label);
3231 else if(ctx -> token == RETURN)
3233 oberon_assert_token(ctx, RETURN);
3234 if(ISEXPR(ctx -> token))
3236 oberon_expr_t * expr;
3237 expr = oberon_expr(ctx);
3238 oberon_make_return(ctx, expr);
3240 else
3242 oberon_make_return(ctx, NULL);
3247 static void
3248 oberon_statement_seq(oberon_context_t * ctx)
3250 oberon_statement(ctx);
3251 while(ctx -> token == SEMICOLON)
3253 oberon_assert_token(ctx, SEMICOLON);
3254 oberon_statement(ctx);
3258 static void
3259 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
3261 oberon_module_t * m = ctx -> module_list;
3262 while(m && strcmp(m -> name, name) != 0)
3264 m = m -> next;
3267 if(m == NULL)
3269 const char * code;
3270 code = ctx -> import_module(name);
3271 if(code == NULL)
3273 oberon_error(ctx, "no such module");
3276 m = oberon_compile_module(ctx, code);
3277 assert(m);
3280 if(m -> ready == 0)
3282 oberon_error(ctx, "cyclic module import");
3285 oberon_object_t * ident;
3286 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3287 ident -> module = m;
3290 static void
3291 oberon_import_decl(oberon_context_t * ctx)
3293 char * alias;
3294 char * name;
3296 alias = name = oberon_assert_ident(ctx);
3297 if(ctx -> token == ASSIGN)
3299 oberon_assert_token(ctx, ASSIGN);
3300 name = oberon_assert_ident(ctx);
3303 oberon_import_module(ctx, alias, name);
3306 static void
3307 oberon_import_list(oberon_context_t * ctx)
3309 oberon_assert_token(ctx, IMPORT);
3311 oberon_import_decl(ctx);
3312 while(ctx -> token == COMMA)
3314 oberon_assert_token(ctx, COMMA);
3315 oberon_import_decl(ctx);
3318 oberon_assert_token(ctx, SEMICOLON);
3321 static void
3322 oberon_parse_module(oberon_context_t * ctx)
3324 char * name1;
3325 char * name2;
3326 oberon_read_token(ctx);
3328 oberon_assert_token(ctx, MODULE);
3329 name1 = oberon_assert_ident(ctx);
3330 oberon_assert_token(ctx, SEMICOLON);
3331 ctx -> mod -> name = name1;
3333 oberon_generator_init_module(ctx, ctx -> mod);
3335 if(ctx -> token == IMPORT)
3337 oberon_import_list(ctx);
3340 oberon_decl_seq(ctx);
3342 oberon_generate_begin_module(ctx);
3343 if(ctx -> token == BEGIN)
3345 oberon_assert_token(ctx, BEGIN);
3346 oberon_statement_seq(ctx);
3348 oberon_generate_end_module(ctx);
3350 oberon_assert_token(ctx, END);
3351 name2 = oberon_assert_ident(ctx);
3352 oberon_assert_token(ctx, DOT);
3354 if(strcmp(name1, name2) != 0)
3356 oberon_error(ctx, "module name not matched");
3359 oberon_generator_fini_module(ctx -> mod);
3362 // =======================================================================
3363 // LIBRARY
3364 // =======================================================================
3366 static void
3367 register_default_types(oberon_context_t * ctx)
3369 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
3370 oberon_generator_init_type(ctx, ctx -> void_type);
3372 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
3373 ctx -> void_ptr_type -> base = ctx -> void_type;
3374 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
3376 ctx -> string_type = oberon_new_type_string(1);
3377 oberon_generator_init_type(ctx, ctx -> string_type);
3379 ctx -> bool_type = oberon_new_type_boolean();
3380 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
3382 ctx -> byte_type = oberon_new_type_integer(1);
3383 oberon_define_type(ctx -> world_scope, "BYTE", ctx -> byte_type, 1);
3385 ctx -> shortint_type = oberon_new_type_integer(2);
3386 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> shortint_type, 1);
3388 ctx -> int_type = oberon_new_type_integer(4);
3389 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type, 1);
3391 ctx -> longint_type = oberon_new_type_integer(8);
3392 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> longint_type, 1);
3394 ctx -> real_type = oberon_new_type_real(4);
3395 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
3397 ctx -> longreal_type = oberon_new_type_real(8);
3398 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
3400 ctx -> char_type = oberon_new_type_char(1);
3401 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
3404 static void
3405 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
3407 oberon_object_t * proc;
3408 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
3409 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
3410 proc -> type -> sysproc = true;
3411 proc -> type -> genfunc = f;
3412 proc -> type -> genproc = p;
3415 static oberon_expr_t *
3416 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3418 if(num_args < 1)
3420 oberon_error(ctx, "too few arguments");
3423 if(num_args > 1)
3425 oberon_error(ctx, "too mach arguments");
3428 oberon_expr_t * arg;
3429 arg = list_args;
3431 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3433 oberon_error(ctx, "MIN accept only type");
3436 oberon_expr_t * expr;
3437 int bits = arg -> result -> size * 8;
3438 switch(arg -> result -> class)
3440 case OBERON_TYPE_INTEGER:
3441 expr = oberon_integer_item(ctx, -powl(2, bits - 1));
3442 break;
3443 default:
3444 oberon_error(ctx, "allowed only basic types");
3445 break;
3448 return expr;
3451 static oberon_expr_t *
3452 oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3454 if(num_args < 1)
3456 oberon_error(ctx, "too few arguments");
3459 if(num_args > 1)
3461 oberon_error(ctx, "too mach arguments");
3464 oberon_expr_t * arg;
3465 arg = list_args;
3467 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3469 oberon_error(ctx, "MAX accept only type");
3472 oberon_expr_t * expr;
3473 int bits = arg -> result -> size * 8;
3474 switch(arg -> result -> class)
3476 case OBERON_TYPE_INTEGER:
3477 expr = oberon_integer_item(ctx, powl(2, bits - 1) - 1);
3478 break;
3479 default:
3480 oberon_error(ctx, "allowed only basic types");
3481 break;
3484 return expr;
3487 static oberon_expr_t *
3488 oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3490 if(num_args < 1)
3492 oberon_error(ctx, "too few arguments");
3495 if(num_args > 1)
3497 oberon_error(ctx, "too mach arguments");
3500 oberon_expr_t * arg;
3501 arg = list_args;
3503 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3505 oberon_error(ctx, "SIZE accept only type");
3508 int size;
3509 oberon_expr_t * expr;
3510 oberon_type_t * type = arg -> result;
3511 switch(type -> class)
3513 case OBERON_TYPE_INTEGER:
3514 case OBERON_TYPE_BOOLEAN:
3515 case OBERON_TYPE_REAL:
3516 size = type -> size;
3517 break;
3518 default:
3519 oberon_error(ctx, "TODO SIZE");
3520 break;
3523 expr = oberon_integer_item(ctx, size);
3524 return expr;
3527 static oberon_expr_t *
3528 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3530 if(num_args < 1)
3532 oberon_error(ctx, "too few arguments");
3535 if(num_args > 1)
3537 oberon_error(ctx, "too mach arguments");
3540 oberon_expr_t * arg;
3541 arg = list_args;
3542 oberon_check_src(ctx, arg);
3544 oberon_type_t * result_type;
3545 result_type = arg -> result;
3547 if(result_type -> class != OBERON_TYPE_INTEGER)
3549 oberon_error(ctx, "ABS accepts only integers");
3552 oberon_expr_t * expr;
3553 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
3554 return expr;
3557 static void
3558 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3560 if(num_args < 1)
3562 oberon_error(ctx, "too few arguments");
3566 oberon_expr_t * dst;
3567 dst = list_args;
3568 oberon_check_dst(ctx, dst);
3570 oberon_type_t * type;
3571 type = dst -> result;
3573 if(type -> class != OBERON_TYPE_POINTER)
3575 oberon_error(ctx, "not a pointer");
3578 type = type -> base;
3580 oberon_expr_t * src;
3581 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3582 src -> item.num_args = 0;
3583 src -> item.args = NULL;
3585 int max_args = 1;
3586 if(type -> class == OBERON_TYPE_ARRAY)
3588 if(type -> size == 0)
3590 oberon_type_t * x = type;
3591 while(x -> class == OBERON_TYPE_ARRAY)
3593 if(x -> size == 0)
3595 max_args += 1;
3597 x = x -> base;
3601 if(num_args < max_args)
3603 oberon_error(ctx, "too few arguments");
3606 if(num_args > max_args)
3608 oberon_error(ctx, "too mach arguments");
3611 int num_sizes = max_args - 1;
3612 oberon_expr_t * size_list = list_args -> next;
3614 oberon_expr_t * arg = size_list;
3615 for(int i = 0; i < max_args - 1; i++)
3617 oberon_check_src(ctx, arg);
3618 if(arg -> result -> class != OBERON_TYPE_INTEGER)
3620 oberon_error(ctx, "size must be integer");
3622 arg = arg -> next;
3625 src -> item.num_args = num_sizes;
3626 src -> item.args = size_list;
3628 else if(type -> class != OBERON_TYPE_RECORD)
3630 oberon_error(ctx, "oberon_make_new_call: wat");
3633 if(num_args > max_args)
3635 oberon_error(ctx, "too mach arguments");
3638 oberon_assign(ctx, src, dst);
3641 oberon_context_t *
3642 oberon_create_context(ModuleImportCallback import_module)
3644 oberon_context_t * ctx = calloc(1, sizeof *ctx);
3646 oberon_scope_t * world_scope;
3647 world_scope = oberon_open_scope(ctx);
3648 ctx -> world_scope = world_scope;
3650 ctx -> import_module = import_module;
3652 oberon_generator_init_context(ctx);
3654 register_default_types(ctx);
3656 /* Functions */
3657 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
3658 oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
3659 oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
3660 oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
3662 /* Procedures */
3663 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
3665 return ctx;
3668 void
3669 oberon_destroy_context(oberon_context_t * ctx)
3671 oberon_generator_destroy_context(ctx);
3672 free(ctx);
3675 oberon_module_t *
3676 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
3678 const char * code = ctx -> code;
3679 int code_index = ctx -> code_index;
3680 char c = ctx -> c;
3681 int token = ctx -> token;
3682 char * string = ctx -> string;
3683 int integer = ctx -> integer;
3684 int real = ctx -> real;
3685 bool longmode = ctx -> longmode;
3686 oberon_scope_t * decl = ctx -> decl;
3687 oberon_module_t * mod = ctx -> mod;
3689 oberon_scope_t * module_scope;
3690 module_scope = oberon_open_scope(ctx);
3692 oberon_module_t * module;
3693 module = calloc(1, sizeof *module);
3694 module -> decl = module_scope;
3695 module -> next = ctx -> module_list;
3697 ctx -> mod = module;
3698 ctx -> module_list = module;
3700 oberon_init_scaner(ctx, newcode);
3701 oberon_parse_module(ctx);
3703 module -> ready = 1;
3705 ctx -> code = code;
3706 ctx -> code_index = code_index;
3707 ctx -> c = c;
3708 ctx -> token = token;
3709 ctx -> string = string;
3710 ctx -> integer = integer;
3711 ctx -> real = real;
3712 ctx -> longmode = longmode;
3713 ctx -> decl = decl;
3714 ctx -> mod = mod;
3716 return module;