DEADSOFTWARE

Добавлены функции CHR ENTIER LEN ORD
[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>
9 #include <float.h>
11 #include "../include/oberon.h"
13 #include "oberon-internals.h"
14 #include "oberon-type-compat.h"
15 #include "oberon-common.h"
16 #include "generator.h"
18 // =======================================================================
19 // UTILS
20 // =======================================================================
22 static void
23 oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args);
25 static oberon_type_t *
26 oberon_new_type_ptr(int class)
27 {
28 oberon_type_t * x = malloc(sizeof *x);
29 memset(x, 0, sizeof *x);
30 x -> class = class;
31 return x;
32 }
34 static oberon_type_t *
35 oberon_new_type_integer(int size)
36 {
37 oberon_type_t * x;
38 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
39 x -> size = size;
40 return x;
41 }
43 static oberon_type_t *
44 oberon_new_type_boolean()
45 {
46 oberon_type_t * x;
47 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
48 return x;
49 }
51 static oberon_type_t *
52 oberon_new_type_real(int size)
53 {
54 oberon_type_t * x;
55 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
56 x -> size = size;
57 return x;
58 }
60 static oberon_type_t *
61 oberon_new_type_char(int size)
62 {
63 oberon_type_t * x;
64 x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
65 x -> size = size;
66 return x;
67 }
69 static oberon_type_t *
70 oberon_new_type_string(int size)
71 {
72 oberon_type_t * x;
73 x = oberon_new_type_ptr(OBERON_TYPE_STRING);
74 x -> size = size;
75 return x;
76 }
78 static oberon_type_t *
79 oberon_new_type_set(int size)
80 {
81 oberon_type_t * x;
82 x = oberon_new_type_ptr(OBERON_TYPE_SET);
83 x -> size = size;
84 return x;
85 }
87 static oberon_expr_t *
88 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
89 {
90 oberon_oper_t * operator;
91 operator = malloc(sizeof *operator);
92 memset(operator, 0, sizeof *operator);
94 operator -> is_item = 0;
95 operator -> result = result;
96 operator -> read_only = 1;
97 operator -> op = op;
98 operator -> left = left;
99 operator -> right = right;
101 return (oberon_expr_t *) operator;
104 static oberon_expr_t *
105 oberon_new_item(int mode, oberon_type_t * result, int read_only)
107 oberon_item_t * item;
108 item = malloc(sizeof *item);
109 memset(item, 0, sizeof *item);
111 item -> is_item = 1;
112 item -> result = result;
113 item -> read_only = read_only;
114 item -> mode = mode;
116 return (oberon_expr_t *)item;
119 static oberon_type_t *
120 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
122 if(i >= -128 && i <= 127)
124 return ctx -> byte_type;
126 else if(i >= -32768 && i <= 32767)
128 return ctx -> shortint_type;
130 else if(i >= -2147483648 && i <= 2147483647)
132 return ctx -> int_type;
134 else
136 return ctx -> longint_type;
140 static oberon_expr_t *
141 oberon_make_integer(oberon_context_t * ctx, int64_t i)
143 oberon_expr_t * expr;
144 oberon_type_t * result;
145 result = oberon_get_type_of_int_value(ctx, i);
146 expr = oberon_new_item(MODE_INTEGER, result, true);
147 expr -> item.integer = i;
148 expr -> item.real = i;
149 return expr;
152 static oberon_expr_t *
153 oberon_make_char(oberon_context_t * ctx, int64_t i)
155 oberon_expr_t * expr;
156 expr = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
157 expr -> item.integer = i;
158 expr -> item.real = i;
159 return expr;
162 static oberon_expr_t *
163 oberon_make_real_typed(oberon_context_t * ctx, double r, oberon_type_t * result)
165 oberon_expr_t * expr;
166 expr = oberon_new_item(MODE_REAL, result, true);
167 expr -> item.integer = r;
168 expr -> item.real = r;
169 return expr;
172 static oberon_expr_t *
173 oberon_make_real(oberon_context_t * ctx, double r, bool longmode)
175 oberon_type_t * result;
176 result = (longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
177 return oberon_make_real_typed(ctx, r, result);
180 static oberon_expr_t *
181 oberon_make_boolean(oberon_context_t * ctx, bool cond)
183 oberon_expr_t * expr;
184 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
185 expr -> item.integer = cond;
186 expr -> item.real = cond;
187 return expr;
190 static oberon_expr_t *
191 oberon_make_set(oberon_context_t * ctx, int64_t i)
193 oberon_expr_t * expr;
194 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
195 expr -> item.integer = i;
196 expr -> item.real = i;
197 return expr;
200 static oberon_expr_t *
201 oberon_make_set_range(oberon_context_t * ctx, int64_t x, int64_t y)
203 oberon_expr_t * expr;
204 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
205 expr -> item.integer = (x <= y) ? ((2 << y) - (1 << x)) : (0);
206 expr -> item.real = expr -> item.integer;
207 return expr;
210 // =======================================================================
211 // TABLE
212 // =======================================================================
214 static oberon_scope_t *
215 oberon_open_scope(oberon_context_t * ctx)
217 oberon_scope_t * scope = calloc(1, sizeof *scope);
218 oberon_object_t * list = calloc(1, sizeof *list);
220 scope -> ctx = ctx;
221 scope -> list = list;
222 scope -> up = ctx -> decl;
224 if(scope -> up)
226 scope -> local = scope -> up -> local;
227 scope -> parent = scope -> up -> parent;
228 scope -> parent_type = scope -> up -> parent_type;
229 scope -> exit_label = scope -> up -> exit_label;
232 ctx -> decl = scope;
233 return scope;
236 static void
237 oberon_close_scope(oberon_scope_t * scope)
239 oberon_context_t * ctx = scope -> ctx;
240 ctx -> decl = scope -> up;
243 static oberon_object_t *
244 oberon_find_object_in_list(oberon_object_t * list, char * name)
246 oberon_object_t * x = list;
247 while(x -> next && strcmp(x -> next -> name, name) != 0)
249 x = x -> next;
251 return x -> next;
254 static oberon_object_t *
255 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
257 oberon_object_t * result = NULL;
259 oberon_scope_t * s = scope;
260 while(result == NULL && s != NULL)
262 result = oberon_find_object_in_list(s -> list, name);
263 s = s -> up;
266 if(check_it && result == NULL)
268 oberon_error(scope -> ctx, "undefined ident %s", name);
271 return result;
274 static oberon_object_t *
275 oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only)
277 oberon_object_t * newvar = malloc(sizeof *newvar);
278 memset(newvar, 0, sizeof *newvar);
279 newvar -> name = name;
280 newvar -> class = class;
281 newvar -> export = export;
282 newvar -> read_only = read_only;
283 newvar -> local = scope -> local;
284 newvar -> parent = scope -> parent;
285 newvar -> parent_type = scope -> parent_type;
286 newvar -> module = scope -> ctx -> mod;
287 return newvar;
290 static oberon_object_t *
291 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
293 if(check_upscope)
295 if(oberon_find_object(scope -> up, name, false))
297 oberon_error(scope -> ctx, "already defined");
301 oberon_object_t * x = scope -> list;
302 while(x -> next && strcmp(x -> next -> name, name) != 0)
304 x = x -> next;
307 if(x -> next)
309 oberon_error(scope -> ctx, "already defined");
312 oberon_object_t * newvar;
313 newvar = oberon_create_object(scope, name, class, export, read_only);
314 x -> next = newvar;
316 return newvar;
319 static oberon_object_t *
320 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
322 oberon_object_t * id;
323 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
324 id -> type = type;
325 oberon_generator_init_type(scope -> ctx, type);
326 return id;
329 // =======================================================================
330 // SCANER
331 // =======================================================================
333 static void
334 oberon_get_char(oberon_context_t * ctx)
336 if(ctx -> code[ctx -> code_index])
338 ctx -> code_index += 1;
339 ctx -> c = ctx -> code[ctx -> code_index];
343 static void
344 oberon_init_scaner(oberon_context_t * ctx, const char * code)
346 ctx -> code = code;
347 ctx -> code_index = 0;
348 ctx -> c = ctx -> code[ctx -> code_index];
351 static void
352 oberon_read_ident(oberon_context_t * ctx)
354 int len = 0;
355 int i = ctx -> code_index;
357 int c = ctx -> code[i];
358 while(isalnum(c))
360 i += 1;
361 len += 1;
362 c = ctx -> code[i];
365 char * ident = malloc(len + 1);
366 memcpy(ident, &ctx->code[ctx->code_index], len);
367 ident[len] = 0;
369 ctx -> code_index = i;
370 ctx -> c = ctx -> code[i];
371 ctx -> string = ident;
372 ctx -> token = IDENT;
374 if(strcmp(ident, "MODULE") == 0)
376 ctx -> token = MODULE;
378 else if(strcmp(ident, "END") == 0)
380 ctx -> token = END;
382 else if(strcmp(ident, "VAR") == 0)
384 ctx -> token = VAR;
386 else if(strcmp(ident, "BEGIN") == 0)
388 ctx -> token = BEGIN;
390 else if(strcmp(ident, "OR") == 0)
392 ctx -> token = OR;
394 else if(strcmp(ident, "DIV") == 0)
396 ctx -> token = DIV;
398 else if(strcmp(ident, "MOD") == 0)
400 ctx -> token = MOD;
402 else if(strcmp(ident, "PROCEDURE") == 0)
404 ctx -> token = PROCEDURE;
406 else if(strcmp(ident, "RETURN") == 0)
408 ctx -> token = RETURN;
410 else if(strcmp(ident, "CONST") == 0)
412 ctx -> token = CONST;
414 else if(strcmp(ident, "TYPE") == 0)
416 ctx -> token = TYPE;
418 else if(strcmp(ident, "ARRAY") == 0)
420 ctx -> token = ARRAY;
422 else if(strcmp(ident, "OF") == 0)
424 ctx -> token = OF;
426 else if(strcmp(ident, "RECORD") == 0)
428 ctx -> token = RECORD;
430 else if(strcmp(ident, "POINTER") == 0)
432 ctx -> token = POINTER;
434 else if(strcmp(ident, "TO") == 0)
436 ctx -> token = TO;
438 else if(strcmp(ident, "NIL") == 0)
440 ctx -> token = NIL;
442 else if(strcmp(ident, "IMPORT") == 0)
444 ctx -> token = IMPORT;
446 else if(strcmp(ident, "IN") == 0)
448 ctx -> token = IN;
450 else if(strcmp(ident, "IS") == 0)
452 ctx -> token = IS;
454 else if(strcmp(ident, "IF") == 0)
456 ctx -> token = IF;
458 else if(strcmp(ident, "THEN") == 0)
460 ctx -> token = THEN;
462 else if(strcmp(ident, "ELSE") == 0)
464 ctx -> token = ELSE;
466 else if(strcmp(ident, "ELSIF") == 0)
468 ctx -> token = ELSIF;
470 else if(strcmp(ident, "WHILE") == 0)
472 ctx -> token = WHILE;
474 else if(strcmp(ident, "DO") == 0)
476 ctx -> token = DO;
478 else if(strcmp(ident, "REPEAT") == 0)
480 ctx -> token = REPEAT;
482 else if(strcmp(ident, "UNTIL") == 0)
484 ctx -> token = UNTIL;
486 else if(strcmp(ident, "FOR") == 0)
488 ctx -> token = FOR;
490 else if(strcmp(ident, "BY") == 0)
492 ctx -> token = BY;
494 else if(strcmp(ident, "LOOP") == 0)
496 ctx -> token = LOOP;
498 else if(strcmp(ident, "EXIT") == 0)
500 ctx -> token = EXIT;
502 else if(strcmp(ident, "CASE") == 0)
504 ctx -> token = CASE;
506 else if(strcmp(ident, "WITH") == 0)
508 ctx -> token = WITH;
512 #define ISHEXDIGIT(x) \
513 (((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F'))
515 static void
516 oberon_read_number(oberon_context_t * ctx)
518 long integer;
519 double real;
520 char * ident;
521 int start_i;
522 int exp_i;
523 int end_i;
525 /*
526 * mode = 0 == DEC
527 * mode = 1 == HEX
528 * mode = 2 == REAL
529 * mode = 3 == LONGREAL
530 * mode = 4 == CHAR
531 */
532 int mode = 0;
533 start_i = ctx -> code_index;
535 while(isdigit(ctx -> c))
537 oberon_get_char(ctx);
540 end_i = ctx -> code_index;
542 if(ISHEXDIGIT(ctx -> c))
544 mode = 1;
545 while(ISHEXDIGIT(ctx -> c))
547 oberon_get_char(ctx);
550 end_i = ctx -> code_index;
552 if(ctx -> c == 'H')
554 mode = 1;
555 oberon_get_char(ctx);
557 else if(ctx -> c == 'X')
559 mode = 4;
560 oberon_get_char(ctx);
562 else
564 oberon_error(ctx, "invalid hex number");
567 else if(ctx -> c == '.')
569 oberon_get_char(ctx);
570 if(ctx -> c == '.')
572 /* Чит: избегаем конфликта с DOTDOT */
573 ctx -> code_index -= 1;
575 else
577 mode = 2;
579 while(isdigit(ctx -> c))
581 oberon_get_char(ctx);
584 if(ctx -> c == 'E' || ctx -> c == 'D')
586 exp_i = ctx -> code_index;
588 if(ctx -> c == 'D')
590 mode = 3;
593 oberon_get_char(ctx);
595 if(ctx -> c == '+' || ctx -> c == '-')
597 oberon_get_char(ctx);
600 while(isdigit(ctx -> c))
602 oberon_get_char(ctx);
603 }
606 end_i = ctx -> code_index;
609 if(mode == 0)
611 if(ctx -> c == 'H')
613 mode = 1;
614 oberon_get_char(ctx);
616 else if(ctx -> c == 'X')
618 mode = 4;
619 oberon_get_char(ctx);
623 int len = end_i - start_i;
624 ident = malloc(len + 1);
625 memcpy(ident, &ctx -> code[start_i], len);
626 ident[len] = 0;
628 ctx -> longmode = false;
629 if(mode == 3)
631 int i = exp_i - start_i;
632 ident[i] = 'E';
633 ctx -> longmode = true;
636 switch(mode)
638 case 0:
639 integer = atol(ident);
640 real = integer;
641 ctx -> token = INTEGER;
642 break;
643 case 1:
644 sscanf(ident, "%lx", &integer);
645 real = integer;
646 ctx -> token = INTEGER;
647 break;
648 case 2:
649 case 3:
650 sscanf(ident, "%lf", &real);
651 integer = real;
652 ctx -> token = REAL;
653 break;
654 case 4:
655 sscanf(ident, "%lx", &integer);
656 real = integer;
657 ctx -> token = CHAR;
658 break;
659 default:
660 oberon_error(ctx, "oberon_read_number: wat");
661 break;
664 ctx -> string = ident;
665 ctx -> integer = integer;
666 ctx -> real = real;
669 static void
670 oberon_skip_space(oberon_context_t * ctx)
672 while(isspace(ctx -> c))
674 oberon_get_char(ctx);
678 static void
679 oberon_read_comment(oberon_context_t * ctx)
681 int nesting = 1;
682 while(nesting >= 1)
684 if(ctx -> c == '(')
686 oberon_get_char(ctx);
687 if(ctx -> c == '*')
689 oberon_get_char(ctx);
690 nesting += 1;
693 else if(ctx -> c == '*')
695 oberon_get_char(ctx);
696 if(ctx -> c == ')')
698 oberon_get_char(ctx);
699 nesting -= 1;
702 else if(ctx -> c == 0)
704 oberon_error(ctx, "unterminated comment");
706 else
708 oberon_get_char(ctx);
713 static void oberon_read_string(oberon_context_t * ctx)
715 int c = ctx -> c;
716 oberon_get_char(ctx);
718 int start = ctx -> code_index;
720 while(ctx -> c != 0 && ctx -> c != c)
722 oberon_get_char(ctx);
725 if(ctx -> c == 0)
727 oberon_error(ctx, "unterminated string");
730 int end = ctx -> code_index;
732 oberon_get_char(ctx);
734 char * string = calloc(1, end - start + 1);
735 strncpy(string, &ctx -> code[start], end - start);
737 ctx -> token = STRING;
738 ctx -> string = string;
739 ctx -> integer = string[0];
742 static void oberon_read_token(oberon_context_t * ctx);
744 static void
745 oberon_read_symbol(oberon_context_t * ctx)
747 int c = ctx -> c;
748 switch(c)
750 case 0:
751 ctx -> token = EOF_;
752 break;
753 case ';':
754 ctx -> token = SEMICOLON;
755 oberon_get_char(ctx);
756 break;
757 case ':':
758 ctx -> token = COLON;
759 oberon_get_char(ctx);
760 if(ctx -> c == '=')
762 ctx -> token = ASSIGN;
763 oberon_get_char(ctx);
765 break;
766 case '.':
767 ctx -> token = DOT;
768 oberon_get_char(ctx);
769 if(ctx -> c == '.')
771 ctx -> token = DOTDOT;
772 oberon_get_char(ctx);
774 break;
775 case '(':
776 ctx -> token = LPAREN;
777 oberon_get_char(ctx);
778 if(ctx -> c == '*')
780 oberon_get_char(ctx);
781 oberon_read_comment(ctx);
782 oberon_read_token(ctx);
784 break;
785 case ')':
786 ctx -> token = RPAREN;
787 oberon_get_char(ctx);
788 break;
789 case '=':
790 ctx -> token = EQUAL;
791 oberon_get_char(ctx);
792 break;
793 case '#':
794 ctx -> token = NEQ;
795 oberon_get_char(ctx);
796 break;
797 case '<':
798 ctx -> token = LESS;
799 oberon_get_char(ctx);
800 if(ctx -> c == '=')
802 ctx -> token = LEQ;
803 oberon_get_char(ctx);
805 break;
806 case '>':
807 ctx -> token = GREAT;
808 oberon_get_char(ctx);
809 if(ctx -> c == '=')
811 ctx -> token = GEQ;
812 oberon_get_char(ctx);
814 break;
815 case '+':
816 ctx -> token = PLUS;
817 oberon_get_char(ctx);
818 break;
819 case '-':
820 ctx -> token = MINUS;
821 oberon_get_char(ctx);
822 break;
823 case '*':
824 ctx -> token = STAR;
825 oberon_get_char(ctx);
826 if(ctx -> c == ')')
828 oberon_get_char(ctx);
829 oberon_error(ctx, "unstarted comment");
831 break;
832 case '/':
833 ctx -> token = SLASH;
834 oberon_get_char(ctx);
835 break;
836 case '&':
837 ctx -> token = AND;
838 oberon_get_char(ctx);
839 break;
840 case '~':
841 ctx -> token = NOT;
842 oberon_get_char(ctx);
843 break;
844 case ',':
845 ctx -> token = COMMA;
846 oberon_get_char(ctx);
847 break;
848 case '[':
849 ctx -> token = LBRACK;
850 oberon_get_char(ctx);
851 break;
852 case ']':
853 ctx -> token = RBRACK;
854 oberon_get_char(ctx);
855 break;
856 case '^':
857 ctx -> token = UPARROW;
858 oberon_get_char(ctx);
859 break;
860 case '"':
861 oberon_read_string(ctx);
862 break;
863 case '\'':
864 oberon_read_string(ctx);
865 break;
866 case '{':
867 ctx -> token = LBRACE;
868 oberon_get_char(ctx);
869 break;
870 case '}':
871 ctx -> token = RBRACE;
872 oberon_get_char(ctx);
873 break;
874 case '|':
875 ctx -> token = BAR;
876 oberon_get_char(ctx);
877 break;
878 default:
879 oberon_error(ctx, "invalid char %c", ctx -> c);
880 break;
884 static void
885 oberon_read_token(oberon_context_t * ctx)
887 oberon_skip_space(ctx);
889 int c = ctx -> c;
890 if(isalpha(c))
892 oberon_read_ident(ctx);
894 else if(isdigit(c))
896 oberon_read_number(ctx);
898 else
900 oberon_read_symbol(ctx);
904 // =======================================================================
905 // EXPRESSION
906 // =======================================================================
908 static void oberon_expect_token(oberon_context_t * ctx, int token);
909 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
910 static void oberon_assert_token(oberon_context_t * ctx, int token);
911 static char * oberon_assert_ident(oberon_context_t * ctx);
912 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
913 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
914 static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
915 static bool oberon_is_const(oberon_expr_t * expr);
917 static oberon_expr_t *
918 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
920 oberon_expr_t * expr;
921 oberon_type_t * result;
923 result = a -> result;
925 if(token == MINUS)
927 if(result -> class == OBERON_TYPE_SET)
929 if(oberon_is_const(a))
931 expr = oberon_make_set(ctx, ~(a -> item.integer));
933 else
935 expr = oberon_new_operator(OP_COMPLEMENTATION, result, a, NULL);
938 else if(result -> class == OBERON_TYPE_INTEGER)
940 if(oberon_is_const(a))
942 expr = oberon_make_integer(ctx, -(a -> item.integer));
944 else
946 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
949 else if(result -> class == OBERON_TYPE_REAL)
951 if(oberon_is_const(a))
953 expr = oberon_make_real_typed(ctx, -(a -> item.real), result);
955 else
957 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
960 else
962 oberon_error(ctx, "incompatible operator type");
965 else if(token == NOT)
967 if(result -> class != OBERON_TYPE_BOOLEAN)
969 oberon_error(ctx, "incompatible operator type");
972 if(oberon_is_const(a))
974 expr = oberon_make_boolean(ctx, !(a -> item.integer));
976 else
978 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
981 else
983 oberon_error(ctx, "oberon_make_unary_op: wat");
986 return expr;
989 static void
990 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
992 oberon_expr_t * last;
994 *num_expr = 1;
995 if(const_expr)
997 *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
999 else
1001 *first = last = oberon_expr(ctx);
1003 while(ctx -> token == COMMA)
1005 oberon_assert_token(ctx, COMMA);
1006 oberon_expr_t * current;
1008 if(const_expr)
1010 current = (oberon_expr_t *) oberon_const_expr(ctx);
1012 else
1014 current = oberon_expr(ctx);
1017 last -> next = current;
1018 last = current;
1019 *num_expr += 1;
1023 static oberon_expr_t *
1024 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
1026 oberon_expr_t * cast;
1028 if((oberon_is_char_type(pref) && oberon_is_const_string(expr) && strlen(expr -> item.string) == 1))
1030 /* Автоматически преобразуем строку единичного размера в символ */
1031 cast = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
1032 cast -> item.integer = expr -> item.string[0];
1034 else
1036 cast = oberon_new_operator(OP_CAST, pref, expr, NULL);
1039 return cast;
1042 static void
1043 oberon_check_dst(oberon_context_t * ctx, oberon_expr_t * dst)
1045 if(dst -> read_only)
1047 oberon_error(ctx, "read-only destination");
1050 if(dst -> is_item == false)
1052 oberon_error(ctx, "not variable");
1055 switch(dst -> item.mode)
1057 case MODE_VAR:
1058 case MODE_CALL:
1059 case MODE_INDEX:
1060 case MODE_FIELD:
1061 case MODE_DEREF:
1062 case MODE_NEW:
1063 /* accept */
1064 break;
1065 default:
1066 oberon_error(ctx, "not variable");
1067 break;
1071 static void
1072 oberon_check_src(oberon_context_t * ctx, oberon_expr_t * src)
1074 if(src -> is_item)
1076 if(src -> item.mode == MODE_TYPE)
1078 oberon_error(ctx, "not variable");
1083 static void
1084 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
1086 if(desig -> mode != MODE_CALL)
1088 oberon_error(ctx, "expected mode CALL");
1091 oberon_type_t * fn = desig -> parent -> result;
1092 int num_args = desig -> num_args;
1093 int num_decl = fn -> num_decl;
1095 if(num_args < num_decl)
1097 oberon_error(ctx, "too few arguments");
1099 else if(num_args > num_decl)
1101 oberon_error(ctx, "too many arguments");
1104 /* Делаем проверку на запись и делаем автокаст */
1105 oberon_expr_t * casted[num_args];
1106 oberon_expr_t * arg = desig -> args;
1107 oberon_object_t * param = fn -> decl;
1108 for(int i = 0; i < num_args; i++)
1110 if(param -> class == OBERON_CLASS_VAR_PARAM)
1112 oberon_check_dst(ctx, arg);
1113 if(!oberon_is_compatible_arrays(param, arg))
1115 oberon_check_compatible_var_param(ctx, param -> type, arg -> result);
1117 casted[i] = oberon_cast_expr(ctx, arg, param -> type);
1119 else
1121 oberon_check_src(ctx, arg);
1122 if(!oberon_is_compatible_arrays(param, arg))
1124 oberon_check_assignment_compatible(ctx, arg, param -> type);
1126 casted[i] = oberon_cast_expr(ctx, arg, param -> type);
1129 arg = arg -> next;
1130 param = param -> next;
1133 /* Создаём новый список выражений */
1134 if(num_args > 0)
1136 arg = casted[0];
1137 for(int i = 0; i < num_args - 1; i++)
1139 casted[i] -> next = casted[i + 1];
1141 desig -> args = arg;
1145 static oberon_expr_t *
1146 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1148 oberon_type_t * signature = item -> result;
1149 if(signature -> class != OBERON_TYPE_PROCEDURE)
1151 oberon_error(ctx, "not a procedure");
1154 oberon_expr_t * call;
1156 if(signature -> sysproc)
1158 if(signature -> genfunc == NULL)
1160 oberon_error(ctx, "not a function-procedure");
1163 call = signature -> genfunc(ctx, num_args, list_args);
1165 else
1167 if(signature -> base -> class == OBERON_TYPE_NOTYPE)
1169 oberon_error(ctx, "attempt to call procedure in expression");
1172 call = oberon_new_item(MODE_CALL, signature -> base, true);
1173 call -> item.parent = item;
1174 call -> item.num_args = num_args;
1175 call -> item.args = list_args;
1176 oberon_autocast_call(ctx, (oberon_item_t *) call);
1179 return call;
1182 static void
1183 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1185 oberon_type_t * signature = item -> result;
1186 if(signature -> class != OBERON_TYPE_PROCEDURE)
1188 oberon_error(ctx, "not a procedure");
1191 oberon_expr_t * call;
1193 if(signature -> sysproc)
1195 if(signature -> genproc == NULL)
1197 oberon_error(ctx, "not a procedure");
1200 signature -> genproc(ctx, num_args, list_args);
1202 else
1204 if(signature -> base -> class != OBERON_TYPE_NOTYPE)
1206 oberon_error(ctx, "attempt to call function as non-typed procedure");
1209 call = oberon_new_item(MODE_CALL, signature -> base, true);
1210 call -> item.parent = item;
1211 call -> item.num_args = num_args;
1212 call -> item.args = list_args;
1213 oberon_autocast_call(ctx, (oberon_item_t *) call);
1214 oberon_generate_call_proc(ctx, call);
1218 #define ISEXPR(x) \
1219 (((x) == PLUS) \
1220 || ((x) == MINUS) \
1221 || ((x) == IDENT) \
1222 || ((x) == INTEGER) \
1223 || ((x) == REAL) \
1224 || ((x) == CHAR) \
1225 || ((x) == STRING) \
1226 || ((x) == NIL) \
1227 || ((x) == LPAREN) \
1228 || ((x) == NOT))
1230 static oberon_expr_t *
1231 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1233 if(expr -> result -> class != OBERON_TYPE_POINTER)
1235 oberon_error(ctx, "not a pointer");
1238 assert(expr -> is_item);
1240 oberon_expr_t * selector;
1241 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
1242 selector -> item.parent = (oberon_item_t *) expr;
1244 return selector;
1247 static oberon_expr_t *
1248 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1250 if(desig -> result -> class == OBERON_TYPE_POINTER)
1252 desig = oberno_make_dereferencing(ctx, desig);
1255 assert(desig -> is_item);
1257 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1259 oberon_error(ctx, "not array");
1262 oberon_type_t * base;
1263 base = desig -> result -> base;
1265 if(index -> result -> class != OBERON_TYPE_INTEGER)
1267 oberon_error(ctx, "index must be integer");
1270 // Статическая проверка границ массива
1271 if(desig -> result -> size != 0)
1273 if(index -> is_item)
1275 if(index -> item.mode == MODE_INTEGER)
1277 int arr_size = desig -> result -> size;
1278 int index_int = index -> item.integer;
1279 if(index_int < 0 || index_int > arr_size - 1)
1281 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1287 oberon_expr_t * selector;
1288 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1289 selector -> item.parent = (oberon_item_t *) desig;
1290 selector -> item.num_args = 1;
1291 selector -> item.args = index;
1293 return selector;
1296 static oberon_expr_t *
1297 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1299 if(expr -> result -> class == OBERON_TYPE_POINTER)
1301 expr = oberno_make_dereferencing(ctx, expr);
1304 assert(expr -> is_item);
1306 if(expr -> result -> class != OBERON_TYPE_RECORD)
1308 oberon_error(ctx, "not record");
1311 oberon_type_t * rec = expr -> result;
1313 oberon_object_t * field;
1314 field = oberon_find_object(rec -> scope, name, true);
1316 if(field -> export == 0)
1318 if(field -> module != ctx -> mod)
1320 oberon_error(ctx, "field not exported");
1324 int read_only = expr -> read_only;
1325 if(field -> read_only)
1327 if(field -> module != ctx -> mod)
1329 read_only = 1;
1333 oberon_expr_t * selector;
1334 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1335 selector -> item.var = field;
1336 selector -> item.parent = (oberon_item_t *) expr;
1338 return selector;
1341 #define ISSELECTOR(x) \
1342 (((x) == LBRACK) \
1343 || ((x) == DOT) \
1344 || ((x) == UPARROW) \
1345 || ((x) == LPAREN))
1347 static oberon_object_t *
1348 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1350 char * name;
1351 oberon_object_t * x;
1353 name = oberon_assert_ident(ctx);
1354 x = oberon_find_object(ctx -> decl, name, check);
1356 if(x != NULL)
1358 if(x -> class == OBERON_CLASS_MODULE)
1360 oberon_assert_token(ctx, DOT);
1361 name = oberon_assert_ident(ctx);
1362 /* Наличие объектов в левых модулях всегда проверяется */
1363 x = oberon_find_object(x -> module -> decl, name, 1);
1365 if(x -> export == 0)
1367 oberon_error(ctx, "not exported");
1372 if(xname)
1374 *xname = name;
1377 return x;
1380 static oberon_expr_t *
1381 oberon_ident_item(oberon_context_t * ctx, char * name)
1383 bool read_only;
1384 oberon_object_t * x;
1385 oberon_expr_t * expr;
1387 x = oberon_find_object(ctx -> decl, name, true);
1389 read_only = false;
1390 if(x -> class == OBERON_CLASS_CONST || x -> class == OBERON_CLASS_PROC)
1392 read_only = true;
1395 expr = oberon_new_item(MODE_VAR, x -> type, read_only);
1396 expr -> item.var = x;
1397 return expr;
1400 static oberon_expr_t *
1401 oberon_qualident_expr(oberon_context_t * ctx)
1403 oberon_object_t * var;
1404 oberon_expr_t * expr;
1406 var = oberon_qualident(ctx, NULL, 1);
1408 int read_only = 0;
1409 if(var -> read_only)
1411 if(var -> module != ctx -> mod)
1413 read_only = 1;
1417 switch(var -> class)
1419 case OBERON_CLASS_CONST:
1420 // TODO copy value
1421 expr = (oberon_expr_t *) var -> value;
1422 break;
1423 case OBERON_CLASS_TYPE:
1424 expr = oberon_new_item(MODE_TYPE, var -> type, read_only);
1425 break;
1426 case OBERON_CLASS_VAR:
1427 case OBERON_CLASS_VAR_PARAM:
1428 case OBERON_CLASS_PARAM:
1429 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1430 break;
1431 case OBERON_CLASS_PROC:
1432 expr = oberon_new_item(MODE_VAR, var -> type, true);
1433 break;
1434 default:
1435 oberon_error(ctx, "invalid designator");
1436 break;
1439 expr -> item.var = var;
1441 return expr;
1444 static oberon_expr_t *
1445 oberon_designator(oberon_context_t * ctx)
1447 char * name;
1448 oberon_expr_t * expr;
1449 oberon_object_t * objtype;
1451 expr = oberon_qualident_expr(ctx);
1453 while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
1455 switch(ctx -> token)
1457 case DOT:
1458 oberon_assert_token(ctx, DOT);
1459 name = oberon_assert_ident(ctx);
1460 expr = oberon_make_record_selector(ctx, expr, name);
1461 break;
1462 case LBRACK:
1463 oberon_assert_token(ctx, LBRACK);
1464 int num_indexes = 0;
1465 oberon_expr_t * indexes = NULL;
1466 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1467 oberon_assert_token(ctx, RBRACK);
1469 for(int i = 0; i < num_indexes; i++)
1471 expr = oberon_make_array_selector(ctx, expr, indexes);
1472 indexes = indexes -> next;
1474 break;
1475 case UPARROW:
1476 oberon_assert_token(ctx, UPARROW);
1477 expr = oberno_make_dereferencing(ctx, expr);
1478 break;
1479 case LPAREN:
1480 oberon_assert_token(ctx, LPAREN);
1481 objtype = oberon_qualident(ctx, NULL, true);
1482 oberon_assert_token(ctx, RPAREN);
1483 oberon_check_extension_of(ctx, expr -> result, objtype -> type);
1484 expr = oberon_cast_expr(ctx, expr, objtype -> type);
1485 break;
1486 default:
1487 oberon_error(ctx, "oberon_designator: wat");
1488 break;
1492 return expr;
1495 static oberon_expr_t *
1496 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1498 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1499 if(ctx -> token == LPAREN)
1501 oberon_assert_token(ctx, LPAREN);
1503 int num_args = 0;
1504 oberon_expr_t * arguments = NULL;
1506 if(ISEXPR(ctx -> token))
1508 oberon_expr_list(ctx, &num_args, &arguments, 0);
1511 assert(expr -> is_item == 1);
1512 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1514 oberon_assert_token(ctx, RPAREN);
1517 return expr;
1520 static void
1521 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1523 assert(expr -> is_item);
1525 int num_args = 0;
1526 oberon_expr_t * arguments = NULL;
1528 if(ctx -> token == LPAREN)
1530 oberon_assert_token(ctx, LPAREN);
1532 if(ISEXPR(ctx -> token))
1534 oberon_expr_list(ctx, &num_args, &arguments, 0);
1537 oberon_assert_token(ctx, RPAREN);
1540 /* Вызов происходит даже без скобок */
1541 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1544 static oberon_expr_t *
1545 oberon_element(oberon_context_t * ctx)
1547 oberon_expr_t * e1;
1548 oberon_expr_t * e2;
1550 e1 = oberon_expr(ctx);
1551 oberon_check_src(ctx, e1);
1552 if(e1 -> result -> class != OBERON_TYPE_INTEGER)
1554 oberon_error(ctx, "expected integer");
1557 e2 = NULL;
1558 if(ctx -> token == DOTDOT)
1560 oberon_assert_token(ctx, DOTDOT);
1561 e2 = oberon_expr(ctx);
1562 oberon_check_src(ctx, e2);
1563 if(e2 -> result -> class != OBERON_TYPE_INTEGER)
1565 oberon_error(ctx, "expected integer");
1569 oberon_expr_t * set;
1570 if(e2 == NULL && oberon_is_const(e1))
1572 set = oberon_make_set(ctx, e1 -> item.integer);
1574 else if(e2 != NULL && oberon_is_const(e1) && oberon_is_const(e2))
1576 set = oberon_make_set_range(ctx, e1 -> item.integer, e2 -> item.integer);
1578 else
1580 set = oberon_new_operator(OP_RANGE, ctx -> set_type, e1, e2);
1582 return set;
1585 static oberon_expr_t *
1586 oberon_make_set_union(oberon_context_t * ctx, oberon_expr_t * a, oberon_expr_t * b)
1588 if(oberon_is_const(a) && oberon_is_const(b))
1590 return oberon_make_set(ctx, (a -> item.integer | b -> item.integer));
1592 else
1594 return oberon_new_operator(OP_UNION, ctx -> set_type, a, b);
1595 }
1598 static oberon_expr_t *
1599 oberon_set(oberon_context_t * ctx)
1601 oberon_expr_t * set;
1602 oberon_expr_t * elements;
1603 set = oberon_make_set(ctx, 0);
1605 oberon_assert_token(ctx, LBRACE);
1606 if(ISEXPR(ctx -> token))
1608 elements = oberon_element(ctx);
1609 set = oberon_make_set_union(ctx, set, elements);
1610 while(ctx -> token == COMMA)
1612 oberon_assert_token(ctx, COMMA);
1613 elements = oberon_element(ctx);
1614 set = oberon_make_set_union(ctx, set, elements);
1617 oberon_assert_token(ctx, RBRACE);
1619 return set;
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_make_integer(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 expr = oberon_make_real(ctx, ctx -> real, ctx -> longmode);
1652 oberon_assert_token(ctx, REAL);
1653 break;
1654 case LBRACE:
1655 expr = oberon_set(ctx);
1656 break;
1657 case LPAREN:
1658 oberon_assert_token(ctx, LPAREN);
1659 expr = oberon_expr(ctx);
1660 oberon_assert_token(ctx, RPAREN);
1661 break;
1662 case NOT:
1663 oberon_assert_token(ctx, NOT);
1664 expr = oberon_factor(ctx);
1665 expr = oberon_make_unary_op(ctx, NOT, expr);
1666 break;
1667 case NIL:
1668 oberon_assert_token(ctx, NIL);
1669 expr = oberon_new_item(MODE_NIL, ctx -> nil_type, true);
1670 break;
1671 default:
1672 oberon_error(ctx, "invalid expression");
1675 return expr;
1678 static oberon_expr_t *
1679 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1681 oberon_expr_t * expr;
1682 oberon_type_t * result;
1684 oberon_check_compatible_bin_expr_types(ctx, token, a -> result, b -> result);
1685 oberon_check_src(ctx, a);
1686 if(token != IS)
1688 oberon_check_src(ctx, b);
1691 if(token == IN)
1693 if(oberon_is_const(a) && oberon_is_const(b))
1695 expr = oberon_make_boolean(ctx, (1 << a -> item.integer) & b -> item.integer);
1697 else
1699 expr = oberon_new_operator(OP_IN, ctx -> bool_type, a, b);
1702 else if(token == IS)
1704 oberon_check_type_expr(ctx, b);
1705 expr = oberon_new_operator(OP_IS, ctx -> bool_type, a, b);
1707 else if((token >= EQUAL && token <= GEQ) || token == OR || token == AND)
1709 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1711 if(oberon_is_const(a) && oberon_is_const(b)
1712 && (oberon_is_real_type(result) || oberon_is_integer_type(result)))
1714 if(oberon_is_real_type(result))
1716 double x = a -> item.real;
1717 double y = b -> item.real;
1718 switch(token)
1720 case EQUAL: expr = oberon_make_boolean(ctx, x == y); break;
1721 case NEQ: expr = oberon_make_boolean(ctx, x != y); break;
1722 case LESS: expr = oberon_make_boolean(ctx, x < y); break;
1723 case LEQ: expr = oberon_make_boolean(ctx, x <= y); break;
1724 case GREAT: expr = oberon_make_boolean(ctx, x > y); break;
1725 case GEQ: expr = oberon_make_boolean(ctx, x >= y); break;
1726 case OR: expr = oberon_make_boolean(ctx, x || y); break;
1727 case AND: expr = oberon_make_boolean(ctx, x && y); break;
1728 default: assert(0); break;
1731 else if(oberon_is_integer_type(result))
1733 int64_t x = a -> item.integer;
1734 int64_t y = b -> item.integer;
1735 switch(token)
1737 case EQUAL: expr = oberon_make_boolean(ctx, x == y); break;
1738 case NEQ: expr = oberon_make_boolean(ctx, x != y); break;
1739 case LESS: expr = oberon_make_boolean(ctx, x < y); break;
1740 case LEQ: expr = oberon_make_boolean(ctx, x <= y); break;
1741 case GREAT: expr = oberon_make_boolean(ctx, x > y); break;
1742 case GEQ: expr = oberon_make_boolean(ctx, x >= y); break;
1743 case OR: expr = oberon_make_boolean(ctx, x || y); break;
1744 case AND: expr = oberon_make_boolean(ctx, x && y); break;
1745 default: assert(0); break;
1748 else
1750 assert(0);
1753 else
1755 a = oberon_cast_expr(ctx, a, result);
1756 b = oberon_cast_expr(ctx, b, result);
1757 result = ctx -> bool_type;
1758 switch(token)
1760 case EQUAL: expr = oberon_new_operator(OP_EQ, result, a, b); break;
1761 case NEQ: expr = oberon_new_operator(OP_NEQ, result, a, b); break;
1762 case LESS: expr = oberon_new_operator(OP_LSS, result, a, b); break;
1763 case LEQ: expr = oberon_new_operator(OP_LEQ, result, a, b); break;
1764 case GREAT: expr = oberon_new_operator(OP_GRT, result, a, b); break;
1765 case GEQ: expr = oberon_new_operator(OP_GEQ, result, a, b); break;
1766 case OR: expr = oberon_new_operator(OP_LOGIC_OR, result, a, b); break;
1767 case AND: expr = oberon_new_operator(OP_LOGIC_AND, result, a, b); break;
1768 default: assert(0); break;
1772 else if(token == SLASH)
1774 if(oberon_is_set_type(a -> result) && oberon_is_set_type(b -> result))
1776 if(oberon_is_const(a) && oberon_is_const(b))
1778 int64_t x = a -> item.integer;
1779 int64_t y = b -> item.integer;
1780 expr = oberon_make_set(ctx, x ^ y);
1782 else
1784 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1785 a = oberon_cast_expr(ctx, a, result);
1786 b = oberon_cast_expr(ctx, b, result);
1787 expr = oberon_new_operator(OP_SYM_DIFFERENCE, result, a, b);
1790 else
1792 result = oberon_get_longer_real_type(ctx, a -> result, b -> result);
1793 if(oberon_is_const(a) && oberon_is_const(b))
1795 double x = a -> item.real;
1796 double y = b -> item.real;
1797 expr = oberon_make_real_typed(ctx, x / y, result);
1799 else
1801 a = oberon_cast_expr(ctx, a, result);
1802 b = oberon_cast_expr(ctx, b, result);
1803 expr = oberon_new_operator(OP_DIV, result, a, b);
1807 else
1809 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1811 if(oberon_is_const(a) && oberon_is_const(b))
1813 if(oberon_is_set_type(result))
1815 int64_t x = a -> item.integer;
1816 int64_t y = b -> item.integer;
1817 switch(token)
1819 case PLUS: expr = oberon_make_set(ctx, x | y); break;
1820 case MINUS: expr = oberon_make_set(ctx, x & ~y); break;
1821 case STAR: expr = oberon_make_set(ctx, x & y); break;
1822 default: assert(0); break;
1825 if(oberon_is_real_type(result))
1827 double x = a -> item.real;
1828 double y = b -> item.real;
1829 switch(token)
1831 case PLUS: expr = oberon_make_real_typed(ctx, x + y, result); break;
1832 case MINUS: expr = oberon_make_real_typed(ctx, x - y, result); break;
1833 case STAR: expr = oberon_make_real_typed(ctx, x * y, result); break;
1834 default: assert(0); break;
1837 else if(oberon_is_integer_type(result))
1839 int64_t x = a -> item.integer;
1840 int64_t y = b -> item.integer;
1841 switch(token)
1843 case PLUS: expr = oberon_make_integer(ctx, x + y); break;
1844 case MINUS: expr = oberon_make_integer(ctx, x - y); break;
1845 case STAR: expr = oberon_make_integer(ctx, x * y); break;
1846 case DIV: expr = oberon_make_integer(ctx, x / y); break;
1847 case MOD: expr = oberon_make_integer(ctx, x % y); break;
1848 default: assert(0); break;
1851 else
1853 assert(0);
1856 else
1858 a = oberon_cast_expr(ctx, a, result);
1859 b = oberon_cast_expr(ctx, b, result);
1862 if(oberon_is_set_type(result))
1864 switch(token)
1866 case PLUS:
1867 expr = oberon_new_operator(OP_UNION, result, a, b);
1868 break;
1869 case MINUS:
1870 expr = oberon_new_operator(OP_DIFFERENCE, result, a, b);
1871 break;
1872 case STAR:
1873 expr = oberon_new_operator(OP_INTERSECTION, result, a, b);
1874 break;
1875 default:
1876 assert(0);
1877 break;
1880 else if(oberon_is_number_type(result))
1882 switch(token)
1884 case PLUS:
1885 expr = oberon_new_operator(OP_ADD, result, a, b);
1886 break;
1887 case MINUS:
1888 expr = oberon_new_operator(OP_SUB, result, a, b);
1889 break;
1890 case STAR:
1891 expr = oberon_new_operator(OP_MUL, result, a, b);
1892 break;
1893 default:
1894 assert(0);
1895 break;
1898 else
1900 assert(0);
1905 return expr;
1908 #define ISMULOP(x) \
1909 ((x) >= STAR && (x) <= AND)
1911 static oberon_expr_t *
1912 oberon_term_expr(oberon_context_t * ctx)
1914 oberon_expr_t * expr;
1916 expr = oberon_factor(ctx);
1917 while(ISMULOP(ctx -> token))
1919 int token = ctx -> token;
1920 oberon_read_token(ctx);
1922 oberon_expr_t * inter = oberon_factor(ctx);
1923 expr = oberon_make_bin_op(ctx, token, expr, inter);
1926 return expr;
1929 #define ISADDOP(x) \
1930 ((x) >= PLUS && (x) <= OR)
1932 static oberon_expr_t *
1933 oberon_simple_expr(oberon_context_t * ctx)
1935 oberon_expr_t * expr;
1937 int minus = 0;
1938 if(ctx -> token == PLUS)
1940 minus = 0;
1941 oberon_assert_token(ctx, PLUS);
1943 else if(ctx -> token == MINUS)
1945 minus = 1;
1946 oberon_assert_token(ctx, MINUS);
1949 expr = oberon_term_expr(ctx);
1951 if(minus)
1953 expr = oberon_make_unary_op(ctx, MINUS, expr);
1956 while(ISADDOP(ctx -> token))
1958 int token = ctx -> token;
1959 oberon_read_token(ctx);
1961 oberon_expr_t * inter = oberon_term_expr(ctx);
1962 expr = oberon_make_bin_op(ctx, token, expr, inter);
1965 return expr;
1968 #define ISRELATION(x) \
1969 ((x) >= EQUAL && (x) <= IS)
1971 static oberon_expr_t *
1972 oberon_expr(oberon_context_t * ctx)
1974 oberon_expr_t * expr;
1976 expr = oberon_simple_expr(ctx);
1977 while(ISRELATION(ctx -> token))
1979 int token = ctx -> token;
1980 oberon_read_token(ctx);
1982 oberon_expr_t * inter = oberon_simple_expr(ctx);
1983 expr = oberon_make_bin_op(ctx, token, expr, inter);
1986 return expr;
1989 static bool
1990 oberon_is_const(oberon_expr_t * expr)
1992 if(expr -> is_item == false)
1994 return false;
1997 switch(expr -> item.mode)
1999 case MODE_INTEGER:
2000 case MODE_BOOLEAN:
2001 case MODE_NIL:
2002 case MODE_REAL:
2003 case MODE_CHAR:
2004 case MODE_STRING:
2005 case MODE_SET:
2006 case MODE_TYPE:
2007 return true;
2008 break;
2009 default:
2010 return false;
2011 break;
2014 return false;
2017 static void
2018 oberon_check_const(oberon_context_t * ctx, oberon_expr_t * expr)
2020 if(!oberon_is_const(expr))
2022 oberon_error(ctx, "const expression are required");
2026 static oberon_item_t *
2027 oberon_const_expr(oberon_context_t * ctx)
2029 oberon_expr_t * expr;
2030 expr = oberon_expr(ctx);
2031 oberon_check_const(ctx, expr);
2032 return (oberon_item_t *) expr;
2035 // =======================================================================
2036 // PARSER
2037 // =======================================================================
2039 static void oberon_decl_seq(oberon_context_t * ctx);
2040 static void oberon_statement_seq(oberon_context_t * ctx);
2041 static void oberon_initialize_decl(oberon_context_t * ctx);
2043 static void
2044 oberon_expect_token(oberon_context_t * ctx, int token)
2046 if(ctx -> token != token)
2048 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
2052 static void
2053 oberon_assert_token(oberon_context_t * ctx, int token)
2055 oberon_expect_token(ctx, token);
2056 oberon_read_token(ctx);
2059 static char *
2060 oberon_assert_ident(oberon_context_t * ctx)
2062 oberon_expect_token(ctx, IDENT);
2063 char * ident = ctx -> string;
2064 oberon_read_token(ctx);
2065 return ident;
2068 static void
2069 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
2071 switch(ctx -> token)
2073 case STAR:
2074 oberon_assert_token(ctx, STAR);
2075 *export = 1;
2076 *read_only = 0;
2077 break;
2078 case MINUS:
2079 oberon_assert_token(ctx, MINUS);
2080 *export = 1;
2081 *read_only = 1;
2082 break;
2083 default:
2084 *export = 0;
2085 *read_only = 0;
2086 break;
2090 static oberon_object_t *
2091 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
2093 char * name;
2094 int export;
2095 int read_only;
2096 oberon_object_t * x;
2098 name = oberon_assert_ident(ctx);
2099 oberon_def(ctx, &export, &read_only);
2101 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
2102 return x;
2105 static void
2106 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
2108 *num = 1;
2109 *list = oberon_ident_def(ctx, class, check_upscope);
2110 while(ctx -> token == COMMA)
2112 oberon_assert_token(ctx, COMMA);
2113 oberon_ident_def(ctx, class, check_upscope);
2114 *num += 1;
2118 static void
2119 oberon_var_decl(oberon_context_t * ctx)
2121 int num;
2122 oberon_object_t * list;
2123 oberon_type_t * type;
2124 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2126 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
2127 oberon_assert_token(ctx, COLON);
2128 oberon_type(ctx, &type);
2130 oberon_object_t * var = list;
2131 for(int i = 0; i < num; i++)
2133 var -> type = type;
2134 var = var -> next;
2138 static oberon_object_t *
2139 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
2141 int class = OBERON_CLASS_PARAM;
2142 if(ctx -> token == VAR)
2144 oberon_read_token(ctx);
2145 class = OBERON_CLASS_VAR_PARAM;
2148 int num;
2149 oberon_object_t * list;
2150 oberon_ident_list(ctx, class, false, &num, &list);
2152 oberon_assert_token(ctx, COLON);
2154 oberon_type_t * type;
2155 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2156 oberon_type(ctx, &type);
2158 oberon_object_t * param = list;
2159 for(int i = 0; i < num; i++)
2161 param -> type = type;
2162 param = param -> next;
2165 *num_decl += num;
2166 return list;
2169 #define ISFPSECTION \
2170 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2172 static void
2173 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2175 oberon_assert_token(ctx, LPAREN);
2177 if(ISFPSECTION)
2179 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2180 while(ctx -> token == SEMICOLON)
2182 oberon_assert_token(ctx, SEMICOLON);
2183 oberon_fp_section(ctx, &signature -> num_decl);
2187 oberon_assert_token(ctx, RPAREN);
2189 if(ctx -> token == COLON)
2191 oberon_assert_token(ctx, COLON);
2193 oberon_object_t * typeobj;
2194 typeobj = oberon_qualident(ctx, NULL, 1);
2195 if(typeobj -> class != OBERON_CLASS_TYPE)
2197 oberon_error(ctx, "function result is not type");
2199 if(typeobj -> type -> class == OBERON_TYPE_RECORD
2200 || typeobj -> type -> class == OBERON_TYPE_ARRAY)
2202 oberon_error(ctx, "records or arrays could not be result of function");
2204 signature -> base = typeobj -> type;
2208 static void
2209 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2211 oberon_type_t * signature;
2212 signature = *type;
2213 signature -> class = OBERON_TYPE_PROCEDURE;
2214 signature -> num_decl = 0;
2215 signature -> base = ctx -> notype_type;
2216 signature -> decl = NULL;
2218 if(ctx -> token == LPAREN)
2220 oberon_formal_pars(ctx, signature);
2224 static void
2225 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2227 if(a -> num_decl != b -> num_decl)
2229 oberon_error(ctx, "number parameters not matched");
2232 int num_param = a -> num_decl;
2233 oberon_object_t * param_a = a -> decl;
2234 oberon_object_t * param_b = b -> decl;
2235 for(int i = 0; i < num_param; i++)
2237 if(strcmp(param_a -> name, param_b -> name) != 0)
2239 oberon_error(ctx, "param %i name not matched", i + 1);
2242 if(param_a -> type != param_b -> type)
2244 oberon_error(ctx, "param %i type not matched", i + 1);
2247 param_a = param_a -> next;
2248 param_b = param_b -> next;
2252 static void
2253 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2255 oberon_object_t * proc = ctx -> decl -> parent;
2256 oberon_type_t * result_type = proc -> type -> base;
2258 if(result_type -> class == OBERON_TYPE_NOTYPE)
2260 if(expr != NULL)
2262 oberon_error(ctx, "procedure has no result type");
2265 else
2267 if(expr == NULL)
2269 oberon_error(ctx, "procedure requires expression on result");
2272 oberon_check_src(ctx, expr);
2273 oberon_check_assignment_compatible(ctx, expr, result_type);
2274 expr = oberon_cast_expr(ctx, expr, result_type);
2277 proc -> has_return = 1;
2279 oberon_generate_return(ctx, expr);
2282 static void
2283 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2285 oberon_assert_token(ctx, SEMICOLON);
2287 ctx -> decl = proc -> scope;
2289 oberon_decl_seq(ctx);
2291 oberon_generate_begin_proc(ctx, proc);
2293 if(ctx -> token == BEGIN)
2295 oberon_assert_token(ctx, BEGIN);
2296 oberon_statement_seq(ctx);
2299 oberon_assert_token(ctx, END);
2300 char * name = oberon_assert_ident(ctx);
2301 if(strcmp(name, proc -> name) != 0)
2303 oberon_error(ctx, "procedure name not matched");
2306 if(proc -> type -> base -> class == OBERON_TYPE_NOTYPE
2307 && proc -> has_return == 0)
2309 oberon_make_return(ctx, NULL);
2312 if(proc -> has_return == 0)
2314 oberon_error(ctx, "procedure requires return");
2317 oberon_generate_end_proc(ctx);
2318 oberon_close_scope(ctx -> decl);
2321 static void
2322 oberon_proc_decl(oberon_context_t * ctx)
2324 oberon_assert_token(ctx, PROCEDURE);
2326 int forward = 0;
2327 if(ctx -> token == UPARROW)
2329 oberon_assert_token(ctx, UPARROW);
2330 forward = 1;
2333 char * name;
2334 int export;
2335 int read_only;
2336 name = oberon_assert_ident(ctx);
2337 oberon_def(ctx, &export, &read_only);
2339 oberon_scope_t * proc_scope;
2340 proc_scope = oberon_open_scope(ctx);
2341 ctx -> decl -> local = 1;
2343 oberon_type_t * signature;
2344 signature = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2345 oberon_opt_formal_pars(ctx, &signature);
2347 //oberon_initialize_decl(ctx);
2348 oberon_generator_init_type(ctx, signature);
2349 oberon_close_scope(ctx -> decl);
2351 oberon_object_t * proc;
2352 proc = oberon_find_object(ctx -> decl, name, 0);
2353 if(proc == NULL)
2355 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2356 proc -> type = signature;
2357 proc -> scope = proc_scope;
2358 oberon_generator_init_proc(ctx, proc);
2360 else
2362 if(proc -> class != OBERON_CLASS_PROC)
2364 oberon_error(ctx, "mult definition");
2367 if(forward == 0)
2369 if(proc -> linked)
2371 oberon_error(ctx, "mult procedure definition");
2375 if(proc -> export != export || proc -> read_only != read_only)
2377 oberon_error(ctx, "export type not matched");
2380 oberon_compare_signatures(ctx, proc -> type, signature);
2383 proc_scope -> parent = proc;
2384 oberon_object_t * param = proc_scope -> list -> next;
2385 while(param)
2387 param -> parent = proc;
2388 param = param -> next;
2391 if(forward == 0)
2393 proc -> linked = 1;
2394 oberon_proc_decl_body(ctx, proc);
2398 static void
2399 oberon_const_decl(oberon_context_t * ctx)
2401 oberon_item_t * value;
2402 oberon_object_t * constant;
2404 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2405 oberon_assert_token(ctx, EQUAL);
2406 value = oberon_const_expr(ctx);
2407 constant -> value = value;
2410 static void
2411 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2413 if(size -> is_item == 0)
2415 oberon_error(ctx, "requires constant");
2418 if(size -> item.mode != MODE_INTEGER)
2420 oberon_error(ctx, "requires integer constant");
2423 oberon_type_t * arr;
2424 arr = *type;
2425 arr -> class = OBERON_TYPE_ARRAY;
2426 arr -> size = size -> item.integer;
2427 arr -> base = base;
2430 static void
2431 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2433 char * name;
2434 oberon_object_t * to;
2436 to = oberon_qualident(ctx, &name, 0);
2438 //name = oberon_assert_ident(ctx);
2439 //to = oberon_find_object(ctx -> decl, name, 0);
2441 if(to != NULL)
2443 if(to -> class != OBERON_CLASS_TYPE)
2445 oberon_error(ctx, "not a type");
2448 else
2450 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2451 to -> type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2454 *type = to -> type;
2457 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2459 /*
2460 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2461 */
2463 static void
2464 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2466 if(sizes == NULL)
2468 *type = base;
2469 return;
2472 oberon_type_t * dim;
2473 dim = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2475 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2477 oberon_make_array_type(ctx, sizes, dim, type);
2480 static void
2481 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2483 type -> class = OBERON_TYPE_ARRAY;
2484 type -> size = 0;
2485 type -> base = base;
2488 static void
2489 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2491 if(ctx -> token == IDENT)
2493 int num;
2494 oberon_object_t * list;
2495 oberon_type_t * type;
2496 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2498 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2499 oberon_assert_token(ctx, COLON);
2501 oberon_scope_t * current = ctx -> decl;
2502 ctx -> decl = modscope;
2503 oberon_type(ctx, &type);
2504 ctx -> decl = current;
2506 oberon_object_t * field = list;
2507 for(int i = 0; i < num; i++)
2509 field -> type = type;
2510 field = field -> next;
2513 rec -> num_decl += num;
2517 static void
2518 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2520 oberon_scope_t * modscope = ctx -> mod -> decl;
2521 oberon_scope_t * oldscope = ctx -> decl;
2522 ctx -> decl = modscope;
2524 if(ctx -> token == LPAREN)
2526 oberon_assert_token(ctx, LPAREN);
2528 oberon_object_t * typeobj;
2529 typeobj = oberon_qualident(ctx, NULL, true);
2531 if(typeobj -> class != OBERON_CLASS_TYPE)
2533 oberon_error(ctx, "base must be type");
2536 oberon_type_t * base = typeobj -> type;
2537 if(base -> class == OBERON_TYPE_POINTER)
2539 base = base -> base;
2542 if(base -> class != OBERON_TYPE_RECORD)
2544 oberon_error(ctx, "base must be record type");
2547 rec -> base = base;
2548 ctx -> decl = base -> scope;
2550 oberon_assert_token(ctx, RPAREN);
2552 else
2554 ctx -> decl = NULL;
2557 oberon_scope_t * this_scope;
2558 this_scope = oberon_open_scope(ctx);
2559 this_scope -> local = true;
2560 this_scope -> parent = NULL;
2561 this_scope -> parent_type = rec;
2563 oberon_field_list(ctx, rec, modscope);
2564 while(ctx -> token == SEMICOLON)
2566 oberon_assert_token(ctx, SEMICOLON);
2567 oberon_field_list(ctx, rec, modscope);
2570 rec -> scope = this_scope;
2571 rec -> decl = this_scope -> list -> next;
2572 ctx -> decl = oldscope;
2575 static void
2576 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2578 if(ctx -> token == IDENT)
2580 oberon_qualident_type(ctx, type);
2582 else if(ctx -> token == ARRAY)
2584 oberon_assert_token(ctx, ARRAY);
2586 int num_sizes = 0;
2587 oberon_expr_t * sizes;
2589 if(ISEXPR(ctx -> token))
2591 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2594 oberon_assert_token(ctx, OF);
2596 oberon_type_t * base;
2597 base = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2598 oberon_type(ctx, &base);
2600 if(num_sizes == 0)
2602 oberon_make_open_array(ctx, base, *type);
2604 else
2606 oberon_make_multiarray(ctx, sizes, base, type);
2609 else if(ctx -> token == RECORD)
2611 oberon_type_t * rec;
2612 rec = *type;
2613 rec -> class = OBERON_TYPE_RECORD;
2614 rec -> module = ctx -> mod;
2616 oberon_assert_token(ctx, RECORD);
2617 oberon_type_record_body(ctx, rec);
2618 oberon_assert_token(ctx, END);
2620 *type = rec;
2622 else if(ctx -> token == POINTER)
2624 oberon_assert_token(ctx, POINTER);
2625 oberon_assert_token(ctx, TO);
2627 oberon_type_t * base;
2628 base = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2629 oberon_type(ctx, &base);
2631 oberon_type_t * ptr;
2632 ptr = *type;
2633 ptr -> class = OBERON_TYPE_POINTER;
2634 ptr -> base = base;
2636 else if(ctx -> token == PROCEDURE)
2638 oberon_open_scope(ctx);
2639 oberon_assert_token(ctx, PROCEDURE);
2640 oberon_opt_formal_pars(ctx, type);
2641 oberon_close_scope(ctx -> decl);
2643 else
2645 oberon_error(ctx, "invalid type declaration");
2649 static void
2650 oberon_type_decl(oberon_context_t * ctx)
2652 char * name;
2653 oberon_object_t * newtype;
2654 oberon_type_t * type;
2655 int export;
2656 int read_only;
2658 name = oberon_assert_ident(ctx);
2659 oberon_def(ctx, &export, &read_only);
2661 newtype = oberon_find_object(ctx -> decl, name, 0);
2662 if(newtype == NULL)
2664 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2665 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2666 assert(newtype -> type);
2668 else
2670 if(newtype -> class != OBERON_CLASS_TYPE)
2672 oberon_error(ctx, "mult definition");
2675 if(newtype -> linked)
2677 oberon_error(ctx, "mult definition - already linked");
2680 newtype -> export = export;
2681 newtype -> read_only = read_only;
2684 oberon_assert_token(ctx, EQUAL);
2686 type = newtype -> type;
2687 oberon_type(ctx, &type);
2689 if(type -> class == OBERON_TYPE_NOTYPE)
2691 oberon_error(ctx, "recursive alias declaration");
2694 newtype -> type = type;
2695 newtype -> linked = 1;
2698 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2699 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2701 static void
2702 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2704 if(type -> class != OBERON_TYPE_POINTER
2705 && type -> class != OBERON_TYPE_ARRAY)
2707 return;
2710 if(type -> recursive)
2712 oberon_error(ctx, "recursive pointer declaration");
2715 if(type -> class == OBERON_TYPE_POINTER
2716 && type -> base -> class == OBERON_TYPE_POINTER)
2718 oberon_error(ctx, "attempt to make pointer to pointer");
2721 type -> recursive = 1;
2723 oberon_prevent_recursive_pointer(ctx, type -> base);
2725 type -> recursive = 0;
2728 static void
2729 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2731 if(type -> class != OBERON_TYPE_RECORD)
2733 return;
2736 if(type -> recursive)
2738 oberon_error(ctx, "recursive record declaration");
2741 type -> recursive = 1;
2743 if(type -> base)
2745 oberon_prevent_recursive_record(ctx, type -> base);
2748 int num_fields = type -> num_decl;
2749 oberon_object_t * field = type -> decl;
2750 for(int i = 0; i < num_fields; i++)
2752 oberon_prevent_recursive_object(ctx, field);
2753 field = field -> next;
2756 type -> recursive = 0;
2758 static void
2759 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2761 if(type -> class != OBERON_TYPE_PROCEDURE)
2763 return;
2766 if(type -> recursive)
2768 oberon_error(ctx, "recursive procedure declaration");
2771 type -> recursive = 1;
2773 int num_fields = type -> num_decl;
2774 oberon_object_t * field = type -> decl;
2775 for(int i = 0; i < num_fields; i++)
2777 oberon_prevent_recursive_object(ctx, field);
2778 field = field -> next;
2781 type -> recursive = 0;
2784 static void
2785 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2787 if(type -> class != OBERON_TYPE_ARRAY)
2789 return;
2792 if(type -> recursive)
2794 oberon_error(ctx, "recursive array declaration");
2797 type -> recursive = 1;
2799 oberon_prevent_recursive_type(ctx, type -> base);
2801 type -> recursive = 0;
2804 static void
2805 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2807 if(type -> class == OBERON_TYPE_POINTER)
2809 oberon_prevent_recursive_pointer(ctx, type);
2811 else if(type -> class == OBERON_TYPE_RECORD)
2813 oberon_prevent_recursive_record(ctx, type);
2815 else if(type -> class == OBERON_TYPE_ARRAY)
2817 oberon_prevent_recursive_array(ctx, type);
2819 else if(type -> class == OBERON_TYPE_PROCEDURE)
2821 oberon_prevent_recursive_procedure(ctx, type);
2825 static void
2826 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2828 switch(x -> class)
2830 case OBERON_CLASS_VAR:
2831 case OBERON_CLASS_TYPE:
2832 case OBERON_CLASS_PARAM:
2833 case OBERON_CLASS_VAR_PARAM:
2834 case OBERON_CLASS_FIELD:
2835 oberon_prevent_recursive_type(ctx, x -> type);
2836 break;
2837 case OBERON_CLASS_CONST:
2838 case OBERON_CLASS_PROC:
2839 case OBERON_CLASS_MODULE:
2840 break;
2841 default:
2842 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2843 break;
2847 static void
2848 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2850 oberon_object_t * x = ctx -> decl -> list -> next;
2852 while(x)
2854 oberon_prevent_recursive_object(ctx, x);
2855 x = x -> next;
2859 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2860 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2862 static void
2863 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2865 if(type -> class != OBERON_TYPE_RECORD)
2867 return;
2870 int num_fields = type -> num_decl;
2871 oberon_object_t * field = type -> decl;
2872 for(int i = 0; i < num_fields; i++)
2874 if(field -> type -> class == OBERON_TYPE_POINTER)
2876 oberon_initialize_type(ctx, field -> type);
2879 oberon_initialize_object(ctx, field);
2880 field = field -> next;
2883 oberon_generator_init_record(ctx, type);
2886 static void
2887 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2889 if(type -> class == OBERON_TYPE_NOTYPE)
2891 oberon_error(ctx, "undeclarated type");
2894 if(type -> initialized)
2896 return;
2899 type -> initialized = 1;
2901 if(type -> class == OBERON_TYPE_POINTER)
2903 oberon_initialize_type(ctx, type -> base);
2904 oberon_generator_init_type(ctx, type);
2906 else if(type -> class == OBERON_TYPE_ARRAY)
2908 if(type -> size != 0)
2910 if(type -> base -> class == OBERON_TYPE_ARRAY)
2912 if(type -> base -> size == 0)
2914 oberon_error(ctx, "open array not allowed as array element");
2919 oberon_initialize_type(ctx, type -> base);
2920 oberon_generator_init_type(ctx, type);
2922 else if(type -> class == OBERON_TYPE_RECORD)
2924 oberon_generator_init_type(ctx, type);
2925 oberon_initialize_record_fields(ctx, type);
2927 else if(type -> class == OBERON_TYPE_PROCEDURE)
2929 int num_fields = type -> num_decl;
2930 oberon_object_t * field = type -> decl;
2931 for(int i = 0; i < num_fields; i++)
2933 oberon_initialize_object(ctx, field);
2934 field = field -> next;
2935 }
2937 oberon_generator_init_type(ctx, type);
2939 else
2941 oberon_generator_init_type(ctx, type);
2945 static void
2946 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2948 if(x -> initialized)
2950 return;
2953 x -> initialized = 1;
2955 switch(x -> class)
2957 case OBERON_CLASS_TYPE:
2958 oberon_initialize_type(ctx, x -> type);
2959 break;
2960 case OBERON_CLASS_VAR:
2961 case OBERON_CLASS_FIELD:
2962 if(x -> type -> class == OBERON_TYPE_ARRAY)
2964 if(x -> type -> size == 0)
2966 oberon_error(ctx, "open array not allowed as variable or field");
2969 oberon_initialize_type(ctx, x -> type);
2970 oberon_generator_init_var(ctx, x);
2971 break;
2972 case OBERON_CLASS_PARAM:
2973 case OBERON_CLASS_VAR_PARAM:
2974 oberon_initialize_type(ctx, x -> type);
2975 oberon_generator_init_var(ctx, x);
2976 break;
2977 case OBERON_CLASS_CONST:
2978 case OBERON_CLASS_PROC:
2979 case OBERON_CLASS_MODULE:
2980 break;
2981 default:
2982 oberon_error(ctx, "oberon_initialize_object: wat");
2983 break;
2987 static void
2988 oberon_initialize_decl(oberon_context_t * ctx)
2990 oberon_object_t * x = ctx -> decl -> list;
2992 while(x -> next)
2994 oberon_initialize_object(ctx, x -> next);
2995 x = x -> next;
2996 }
2999 static void
3000 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
3002 oberon_object_t * x = ctx -> decl -> list;
3004 while(x -> next)
3006 if(x -> next -> class == OBERON_CLASS_PROC)
3008 if(x -> next -> linked == 0)
3010 oberon_error(ctx, "unresolved forward declaration");
3013 x = x -> next;
3014 }
3017 static void
3018 oberon_decl_seq(oberon_context_t * ctx)
3020 if(ctx -> token == CONST)
3022 oberon_assert_token(ctx, CONST);
3023 while(ctx -> token == IDENT)
3025 oberon_const_decl(ctx);
3026 oberon_assert_token(ctx, SEMICOLON);
3030 if(ctx -> token == TYPE)
3032 oberon_assert_token(ctx, TYPE);
3033 while(ctx -> token == IDENT)
3035 oberon_type_decl(ctx);
3036 oberon_assert_token(ctx, SEMICOLON);
3040 if(ctx -> token == VAR)
3042 oberon_assert_token(ctx, VAR);
3043 while(ctx -> token == IDENT)
3045 oberon_var_decl(ctx);
3046 oberon_assert_token(ctx, SEMICOLON);
3050 oberon_prevent_recursive_decl(ctx);
3051 oberon_initialize_decl(ctx);
3053 while(ctx -> token == PROCEDURE)
3055 oberon_proc_decl(ctx);
3056 oberon_assert_token(ctx, SEMICOLON);
3059 oberon_prevent_undeclarated_procedures(ctx);
3062 static oberon_expr_t *
3063 oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type)
3065 oberon_object_t * x;
3066 oberon_expr_t * expr;
3068 x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false);
3069 x -> local = true;
3070 x -> type = type;
3071 oberon_generator_init_temp_var(ctx, x);
3073 expr = oberon_new_item(MODE_VAR, type, false);
3074 expr -> item.var = x;
3075 return expr;
3078 static void
3079 oberon_statement_seq(oberon_context_t * ctx);
3081 static void
3082 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
3084 oberon_check_dst(ctx, dst);
3085 oberon_check_assignment_compatible(ctx, src, dst -> result);
3087 if(oberon_is_array_of_char_type(dst -> result)
3088 && oberon_is_string_type(src -> result))
3090 src -> next = dst;
3091 oberon_make_copy_call(ctx, 2, src);
3093 else
3095 src = oberon_cast_expr(ctx, src, dst -> result);
3096 oberon_generate_assign(ctx, src, dst);
3100 static oberon_expr_t *
3101 oberon_case_labels(oberon_context_t * ctx, oberon_expr_t * val)
3103 oberon_expr_t * e1;
3104 oberon_expr_t * e2;
3105 oberon_expr_t * cond;
3106 oberon_expr_t * cond2;
3108 e1 = (oberon_expr_t *) oberon_const_expr(ctx);
3110 e2 = NULL;
3111 if(ctx -> token == DOTDOT)
3113 oberon_assert_token(ctx, DOTDOT);
3114 e2 = (oberon_expr_t *) oberon_const_expr(ctx);
3117 if(e2 == NULL)
3119 /* val == e1 */
3120 cond = oberon_make_bin_op(ctx, EQUAL, val, e1);
3122 else
3124 /* val >= e1 && val <= e2 */
3125 cond = oberon_make_bin_op(ctx, GEQ, val, e1);
3126 cond2 = oberon_make_bin_op(ctx, LEQ, val, e2);
3127 cond = oberon_make_bin_op(ctx, AND, cond, cond2);
3130 return cond;
3133 static void
3134 oberon_case(oberon_context_t * ctx, oberon_expr_t * val, gen_label_t * end)
3136 oberon_expr_t * cond;
3137 oberon_expr_t * cond2;
3138 gen_label_t * this_end;
3140 if(ISEXPR(ctx -> token))
3142 this_end = oberon_generator_reserve_label(ctx);
3144 cond = oberon_case_labels(ctx, val);
3145 while(ctx -> token == COMMA)
3147 oberon_assert_token(ctx, COMMA);
3148 /* cond || cond2 */
3149 cond2 = oberon_case_labels(ctx, val);
3150 cond = oberon_make_bin_op(ctx, OR, cond, cond2);
3152 oberon_assert_token(ctx, COLON);
3154 oberon_generate_branch(ctx, cond, false, this_end);
3155 oberon_statement_seq(ctx);
3156 oberon_generate_goto(ctx, end);
3158 oberon_generate_label(ctx, this_end);
3162 static void
3163 oberon_case_statement(oberon_context_t * ctx)
3165 oberon_expr_t * val;
3166 oberon_expr_t * expr;
3167 gen_label_t * end;
3169 end = oberon_generator_reserve_label(ctx);
3171 oberon_assert_token(ctx, CASE);
3172 expr = oberon_expr(ctx);
3173 val = oberon_make_temp_var_item(ctx, expr -> result);
3174 oberon_assign(ctx, expr, val);
3175 oberon_assert_token(ctx, OF);
3176 oberon_case(ctx, val, end);
3177 while(ctx -> token == BAR)
3179 oberon_assert_token(ctx, BAR);
3180 oberon_case(ctx, val, end);
3183 if(ctx -> token == ELSE)
3185 oberon_assert_token(ctx, ELSE);
3186 oberon_statement_seq(ctx);
3188 else
3190 oberon_generate_trap(ctx, -1);
3193 oberon_generate_label(ctx, end);
3194 oberon_assert_token(ctx, END);
3197 static void
3198 oberon_with_guard_do(oberon_context_t * ctx, gen_label_t * end)
3200 oberon_expr_t * val;
3201 oberon_expr_t * var;
3202 oberon_expr_t * type;
3203 oberon_expr_t * cond;
3204 oberon_expr_t * cast;
3205 oberon_type_t * old_type;
3206 gen_var_t * old_var;
3207 gen_label_t * this_end;
3209 this_end = oberon_generator_reserve_label(ctx);
3211 var = oberon_qualident_expr(ctx);
3212 oberon_assert_token(ctx, COLON);
3213 type = oberon_qualident_expr(ctx);
3214 cond = oberon_make_bin_op(ctx, IS, var, type);
3216 oberon_assert_token(ctx, DO);
3217 oberon_generate_branch(ctx, cond, false, this_end);
3219 /* Сохраняем ссылку во временной переменной */
3220 val = oberon_make_temp_var_item(ctx, type -> result);
3221 //cast = oberno_make_record_cast(ctx, var, type -> result);
3222 cast = oberon_cast_expr(ctx, var, type -> result);
3223 oberon_assign(ctx, cast, val);
3224 /* Подменяем тип у оригинальной переменной */
3225 old_type = var -> item.var -> type;
3226 var -> item.var -> type = type -> result;
3227 /* Подменяем ссылку на переменную */
3228 old_var = var -> item.var -> gen_var;
3229 var -> item.var -> gen_var = val -> item.var -> gen_var;
3231 oberon_statement_seq(ctx);
3232 oberon_generate_goto(ctx, end);
3233 oberon_generate_label(ctx, this_end);
3235 /* Возвращаем исходное состояние */
3236 var -> item.var -> gen_var = old_var;
3237 var -> item.var -> type = old_type;
3240 static void
3241 oberon_with_statement(oberon_context_t * ctx)
3243 gen_label_t * end;
3244 end = oberon_generator_reserve_label(ctx);
3246 oberon_assert_token(ctx, WITH);
3247 oberon_with_guard_do(ctx, end);
3248 while(ctx -> token == BAR)
3250 oberon_assert_token(ctx, BAR);
3251 oberon_with_guard_do(ctx, end);
3254 if(ctx -> token == ELSE)
3256 oberon_assert_token(ctx, ELSE);
3257 oberon_statement_seq(ctx);
3259 else
3261 oberon_generate_trap(ctx, -2);
3264 oberon_generate_label(ctx, end);
3265 oberon_assert_token(ctx, END);
3268 static void
3269 oberon_statement(oberon_context_t * ctx)
3271 oberon_expr_t * item1;
3272 oberon_expr_t * item2;
3274 if(ctx -> token == IDENT)
3276 item1 = oberon_designator(ctx);
3277 if(ctx -> token == ASSIGN)
3279 oberon_assert_token(ctx, ASSIGN);
3280 item2 = oberon_expr(ctx);
3281 oberon_assign(ctx, item2, item1);
3283 else
3285 oberon_opt_proc_parens(ctx, item1);
3288 else if(ctx -> token == IF)
3290 gen_label_t * end;
3291 gen_label_t * els;
3292 oberon_expr_t * cond;
3294 els = oberon_generator_reserve_label(ctx);
3295 end = oberon_generator_reserve_label(ctx);
3297 oberon_assert_token(ctx, IF);
3298 cond = oberon_expr(ctx);
3299 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3301 oberon_error(ctx, "condition must be boolean");
3303 oberon_assert_token(ctx, THEN);
3304 oberon_generate_branch(ctx, cond, false, els);
3305 oberon_statement_seq(ctx);
3306 oberon_generate_goto(ctx, end);
3307 oberon_generate_label(ctx, els);
3309 while(ctx -> token == ELSIF)
3311 els = oberon_generator_reserve_label(ctx);
3313 oberon_assert_token(ctx, ELSIF);
3314 cond = oberon_expr(ctx);
3315 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3317 oberon_error(ctx, "condition must be boolean");
3319 oberon_assert_token(ctx, THEN);
3320 oberon_generate_branch(ctx, cond, false, els);
3321 oberon_statement_seq(ctx);
3322 oberon_generate_goto(ctx, end);
3323 oberon_generate_label(ctx, els);
3326 if(ctx -> token == ELSE)
3328 oberon_assert_token(ctx, ELSE);
3329 oberon_statement_seq(ctx);
3332 oberon_generate_label(ctx, end);
3333 oberon_assert_token(ctx, END);
3335 else if(ctx -> token == WHILE)
3337 gen_label_t * begin;
3338 gen_label_t * end;
3339 oberon_expr_t * cond;
3341 begin = oberon_generator_reserve_label(ctx);
3342 end = oberon_generator_reserve_label(ctx);
3344 oberon_assert_token(ctx, WHILE);
3345 oberon_generate_label(ctx, begin);
3346 cond = oberon_expr(ctx);
3347 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3349 oberon_error(ctx, "condition must be boolean");
3351 oberon_generate_branch(ctx, cond, false, end);
3353 oberon_assert_token(ctx, DO);
3354 oberon_statement_seq(ctx);
3355 oberon_generate_goto(ctx, begin);
3357 oberon_assert_token(ctx, END);
3358 oberon_generate_label(ctx, end);
3360 else if(ctx -> token == REPEAT)
3362 gen_label_t * begin;
3363 oberon_expr_t * cond;
3365 begin = oberon_generator_reserve_label(ctx);
3366 oberon_generate_label(ctx, begin);
3367 oberon_assert_token(ctx, REPEAT);
3369 oberon_statement_seq(ctx);
3371 oberon_assert_token(ctx, UNTIL);
3373 cond = oberon_expr(ctx);
3374 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3376 oberon_error(ctx, "condition must be boolean");
3379 oberon_generate_branch(ctx, cond, true, begin);
3381 else if(ctx -> token == FOR)
3383 oberon_expr_t * from;
3384 oberon_expr_t * index;
3385 oberon_expr_t * to;
3386 oberon_expr_t * bound;
3387 oberon_expr_t * by;
3388 oberon_expr_t * cond;
3389 oberon_expr_t * count;
3390 gen_label_t * begin;
3391 gen_label_t * end;
3392 char * iname;
3393 int op;
3395 begin = oberon_generator_reserve_label(ctx);
3396 end = oberon_generator_reserve_label(ctx);
3398 oberon_assert_token(ctx, FOR);
3399 iname = oberon_assert_ident(ctx);
3400 index = oberon_ident_item(ctx, iname);
3401 oberon_assert_token(ctx, ASSIGN);
3402 from = oberon_expr(ctx);
3403 oberon_assert_token(ctx, TO);
3404 bound = oberon_make_temp_var_item(ctx, index -> result);
3405 to = oberon_expr(ctx);
3406 oberon_assign(ctx, to, bound); // сначала temp
3407 oberon_assign(ctx, from, index); // потом i
3408 if(ctx -> token == BY)
3410 oberon_assert_token(ctx, BY);
3411 by = (oberon_expr_t *) oberon_const_expr(ctx);
3413 else
3415 by = oberon_make_integer(ctx, 1);
3418 if(by -> result -> class != OBERON_TYPE_INTEGER)
3420 oberon_error(ctx, "must be integer");
3423 if(by -> item.integer > 0)
3425 op = LEQ;
3427 else if(by -> item.integer < 0)
3429 op = GEQ;
3431 else
3433 oberon_error(ctx, "zero step not allowed");
3436 oberon_assert_token(ctx, DO);
3437 oberon_generate_label(ctx, begin);
3438 cond = oberon_make_bin_op(ctx, op, index, bound);
3439 oberon_generate_branch(ctx, cond, false, end);
3440 oberon_statement_seq(ctx);
3441 count = oberon_make_bin_op(ctx, PLUS, index, by);
3442 oberon_assign(ctx, count, index);
3443 oberon_generate_goto(ctx, begin);
3444 oberon_generate_label(ctx, end);
3445 oberon_assert_token(ctx, END);
3447 else if(ctx -> token == LOOP)
3449 gen_label_t * begin;
3450 gen_label_t * end;
3452 begin = oberon_generator_reserve_label(ctx);
3453 end = oberon_generator_reserve_label(ctx);
3455 oberon_open_scope(ctx);
3456 oberon_assert_token(ctx, LOOP);
3457 oberon_generate_label(ctx, begin);
3458 ctx -> decl -> exit_label = end;
3459 oberon_statement_seq(ctx);
3460 oberon_generate_goto(ctx, begin);
3461 oberon_generate_label(ctx, end);
3462 oberon_assert_token(ctx, END);
3463 oberon_close_scope(ctx -> decl);
3465 else if(ctx -> token == EXIT)
3467 oberon_assert_token(ctx, EXIT);
3468 if(ctx -> decl -> exit_label == NULL)
3470 oberon_error(ctx, "not in LOOP-END");
3472 oberon_generate_goto(ctx, ctx -> decl -> exit_label);
3474 else if(ctx -> token == CASE)
3476 oberon_case_statement(ctx);
3478 else if(ctx -> token == WITH)
3480 oberon_with_statement(ctx);
3482 else if(ctx -> token == RETURN)
3484 oberon_assert_token(ctx, RETURN);
3485 if(ISEXPR(ctx -> token))
3487 oberon_expr_t * expr;
3488 expr = oberon_expr(ctx);
3489 oberon_make_return(ctx, expr);
3491 else
3493 oberon_make_return(ctx, NULL);
3498 static void
3499 oberon_statement_seq(oberon_context_t * ctx)
3501 oberon_statement(ctx);
3502 while(ctx -> token == SEMICOLON)
3504 oberon_assert_token(ctx, SEMICOLON);
3505 oberon_statement(ctx);
3509 static void
3510 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
3512 oberon_module_t * m = ctx -> module_list;
3513 while(m && strcmp(m -> name, name) != 0)
3515 m = m -> next;
3518 if(m == NULL)
3520 const char * code;
3521 code = ctx -> import_module(name);
3522 if(code == NULL)
3524 oberon_error(ctx, "no such module");
3527 m = oberon_compile_module(ctx, code);
3528 assert(m);
3531 if(m -> ready == 0)
3533 oberon_error(ctx, "cyclic module import");
3536 oberon_object_t * ident;
3537 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3538 ident -> module = m;
3541 static void
3542 oberon_import_decl(oberon_context_t * ctx)
3544 char * alias;
3545 char * name;
3547 alias = name = oberon_assert_ident(ctx);
3548 if(ctx -> token == ASSIGN)
3550 oberon_assert_token(ctx, ASSIGN);
3551 name = oberon_assert_ident(ctx);
3554 oberon_import_module(ctx, alias, name);
3557 static void
3558 oberon_import_list(oberon_context_t * ctx)
3560 oberon_assert_token(ctx, IMPORT);
3562 oberon_import_decl(ctx);
3563 while(ctx -> token == COMMA)
3565 oberon_assert_token(ctx, COMMA);
3566 oberon_import_decl(ctx);
3569 oberon_assert_token(ctx, SEMICOLON);
3572 static void
3573 oberon_parse_module(oberon_context_t * ctx)
3575 char * name1;
3576 char * name2;
3577 oberon_read_token(ctx);
3579 oberon_assert_token(ctx, MODULE);
3580 name1 = oberon_assert_ident(ctx);
3581 oberon_assert_token(ctx, SEMICOLON);
3582 ctx -> mod -> name = name1;
3584 oberon_generator_init_module(ctx, ctx -> mod);
3586 if(ctx -> token == IMPORT)
3588 oberon_import_list(ctx);
3591 oberon_decl_seq(ctx);
3593 oberon_generate_begin_module(ctx);
3594 if(ctx -> token == BEGIN)
3596 oberon_assert_token(ctx, BEGIN);
3597 oberon_statement_seq(ctx);
3599 oberon_generate_end_module(ctx);
3601 oberon_assert_token(ctx, END);
3602 name2 = oberon_assert_ident(ctx);
3603 oberon_expect_token(ctx, DOT);
3605 if(strcmp(name1, name2) != 0)
3607 oberon_error(ctx, "module name not matched");
3610 oberon_generator_fini_module(ctx -> mod);
3613 // =======================================================================
3614 // LIBRARY
3615 // =======================================================================
3617 static void
3618 register_default_types(oberon_context_t * ctx)
3620 ctx -> notype_type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
3621 oberon_generator_init_type(ctx, ctx -> notype_type);
3623 ctx -> nil_type = oberon_new_type_ptr(OBERON_TYPE_NIL);
3624 oberon_generator_init_type(ctx, ctx -> nil_type);
3626 ctx -> string_type = oberon_new_type_string(1);
3627 oberon_generator_init_type(ctx, ctx -> string_type);
3629 ctx -> bool_type = oberon_new_type_boolean();
3630 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
3632 ctx -> char_type = oberon_new_type_char(1);
3633 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
3635 ctx -> byte_type = oberon_new_type_integer(1);
3636 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> byte_type, 1);
3638 ctx -> shortint_type = oberon_new_type_integer(2);
3639 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> shortint_type, 1);
3641 ctx -> int_type = oberon_new_type_integer(4);
3642 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> int_type, 1);
3644 ctx -> longint_type = oberon_new_type_integer(8);
3645 oberon_define_type(ctx -> world_scope, "HUGEINT", ctx -> longint_type, 1);
3647 ctx -> real_type = oberon_new_type_real(4);
3648 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
3650 ctx -> longreal_type = oberon_new_type_real(8);
3651 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
3653 ctx -> set_type = oberon_new_type_set(4);
3654 oberon_define_type(ctx -> world_scope, "SET", ctx -> set_type, 1);
3657 static void
3658 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
3660 oberon_object_t * proc;
3661 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
3662 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
3663 proc -> type -> sysproc = true;
3664 proc -> type -> genfunc = f;
3665 proc -> type -> genproc = p;
3668 static oberon_expr_t *
3669 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3671 if(num_args < 1)
3673 oberon_error(ctx, "too few arguments");
3676 if(num_args > 1)
3678 oberon_error(ctx, "too mach arguments");
3681 oberon_expr_t * arg;
3682 arg = list_args;
3684 if(!oberon_is_type_expr(arg))
3686 oberon_error(ctx, "MIN accept only type");
3689 oberon_expr_t * expr;
3690 int bits = arg -> result -> size * 8;
3691 switch(arg -> result -> class)
3693 case OBERON_TYPE_INTEGER:
3694 expr = oberon_make_integer(ctx, -powl(2, bits - 1));
3695 break;
3696 case OBERON_TYPE_BOOLEAN:
3697 expr = oberon_make_boolean(ctx, false);
3698 break;
3699 case OBERON_TYPE_CHAR:
3700 expr = oberon_make_char(ctx, 0);
3701 break;
3702 case OBERON_TYPE_REAL:
3703 expr = oberon_make_real_typed(ctx, (bits <= 32) ? (-FLT_MAX) : (-DBL_MAX), arg -> result);
3704 break;
3705 case OBERON_TYPE_SET:
3706 expr = oberon_make_integer(ctx, 0);
3707 break;
3708 default:
3709 oberon_error(ctx, "allowed only basic types");
3710 break;
3713 return expr;
3716 static oberon_expr_t *
3717 oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3719 if(num_args < 1)
3721 oberon_error(ctx, "too few arguments");
3724 if(num_args > 1)
3726 oberon_error(ctx, "too mach arguments");
3729 oberon_expr_t * arg;
3730 arg = list_args;
3732 if(!oberon_is_type_expr(arg))
3734 oberon_error(ctx, "MAX accept only type");
3737 oberon_expr_t * expr;
3738 int bits = arg -> result -> size * 8;
3739 switch(arg -> result -> class)
3741 case OBERON_TYPE_INTEGER:
3742 expr = oberon_make_integer(ctx, powl(2, bits - 1) - 1);
3743 break;
3744 case OBERON_TYPE_BOOLEAN:
3745 expr = oberon_make_boolean(ctx, true);
3746 break;
3747 case OBERON_TYPE_CHAR:
3748 expr = oberon_make_char(ctx, powl(2, bits) - 1);
3749 break;
3750 case OBERON_TYPE_REAL:
3751 expr = oberon_make_real_typed(ctx, (bits <= 32) ? (FLT_MAX) : (DBL_MAX), arg -> result);
3752 break;
3753 case OBERON_TYPE_SET:
3754 expr = oberon_make_integer(ctx, bits);
3755 break;
3756 default:
3757 oberon_error(ctx, "allowed only basic types");
3758 break;
3761 return expr;
3764 static oberon_expr_t *
3765 oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3767 if(num_args < 1)
3769 oberon_error(ctx, "too few arguments");
3772 if(num_args > 1)
3774 oberon_error(ctx, "too mach arguments");
3777 oberon_expr_t * arg;
3778 arg = list_args;
3779 if(!oberon_is_type_expr(arg))
3781 oberon_error(ctx, "SIZE accept only type");
3784 int size;
3785 oberon_expr_t * expr;
3786 oberon_type_t * type = arg -> result;
3787 switch(type -> class)
3789 case OBERON_TYPE_INTEGER:
3790 case OBERON_TYPE_BOOLEAN:
3791 case OBERON_TYPE_REAL:
3792 case OBERON_TYPE_CHAR:
3793 case OBERON_TYPE_SET:
3794 size = type -> size;
3795 break;
3796 default:
3797 oberon_error(ctx, "TODO SIZE");
3798 break;
3801 expr = oberon_make_integer(ctx, size);
3802 return expr;
3805 static oberon_expr_t *
3806 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3808 if(num_args < 1)
3810 oberon_error(ctx, "too few arguments");
3813 if(num_args > 1)
3815 oberon_error(ctx, "too mach arguments");
3818 oberon_expr_t * arg;
3819 arg = list_args;
3820 oberon_check_src(ctx, arg);
3822 if(oberon_is_number_type(arg -> result))
3824 oberon_error(ctx, "ABS accepts only numbers");
3827 oberon_expr_t * expr;
3828 if(oberon_is_const(arg))
3830 if(oberon_is_real_type(arg -> result))
3832 double x = arg -> item.real;
3833 expr = oberon_make_real(ctx, fabsl(x), arg -> result);
3835 else
3837 int64_t x = arg -> item.integer;
3838 expr = oberon_make_integer(ctx, llabs(x));
3841 else
3843 expr = oberon_new_operator(OP_ABS, arg -> result, arg, NULL);
3845 return expr;
3848 static void
3849 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3851 if(num_args < 1)
3853 oberon_error(ctx, "too few arguments");
3856 oberon_expr_t * dst;
3857 dst = list_args;
3858 oberon_check_dst(ctx, dst);
3860 oberon_type_t * type;
3861 type = dst -> result;
3863 if(type -> class != OBERON_TYPE_POINTER)
3865 oberon_error(ctx, "not a pointer");
3868 type = type -> base;
3870 oberon_expr_t * src;
3871 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3872 src -> item.num_args = 0;
3873 src -> item.args = NULL;
3875 int max_args = 1;
3876 if(type -> class == OBERON_TYPE_ARRAY)
3878 if(type -> size == 0)
3880 oberon_type_t * x = type;
3881 while(x -> class == OBERON_TYPE_ARRAY)
3883 if(x -> size == 0)
3885 max_args += 1;
3887 x = x -> base;
3891 if(num_args < max_args)
3893 oberon_error(ctx, "too few arguments");
3896 if(num_args > max_args)
3898 oberon_error(ctx, "too mach arguments");
3901 int num_sizes = max_args - 1;
3902 oberon_expr_t * size_list = list_args -> next;
3904 oberon_expr_t * arg = size_list;
3905 for(int i = 0; i < max_args - 1; i++)
3907 oberon_check_src(ctx, arg);
3908 if(arg -> result -> class != OBERON_TYPE_INTEGER)
3910 oberon_error(ctx, "size must be integer");
3912 arg = arg -> next;
3915 src -> item.num_args = num_sizes;
3916 src -> item.args = size_list;
3918 else if(type -> class != OBERON_TYPE_RECORD)
3920 oberon_error(ctx, "oberon_make_new_call: wat");
3923 if(num_args > max_args)
3925 oberon_error(ctx, "too mach arguments");
3928 oberon_assign(ctx, src, dst);
3931 static void
3932 oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3934 if(num_args < 2)
3936 oberon_error(ctx, "too few arguments");
3939 if(num_args > 2)
3941 oberon_error(ctx, "too mach arguments");
3944 oberon_expr_t * src;
3945 src = list_args;
3946 oberon_check_src(ctx, src);
3948 oberon_expr_t * dst;
3949 dst = list_args -> next;
3950 oberon_check_dst(ctx, dst);
3952 if(!oberon_is_string_type(src -> result) && !oberon_is_array_of_char_type(src -> result))
3954 oberon_error(ctx, "source must be string or array of char");
3957 if(!oberon_is_array_of_char_type(dst -> result))
3959 oberon_error(ctx, "dst must be array of char");
3962 oberon_generate_copy(ctx, src, dst);
3965 static void
3966 oberon_make_assert_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3968 if(num_args < 1)
3970 oberon_error(ctx, "too few arguments");
3973 if(num_args > 2)
3975 oberon_error(ctx, "too mach arguments");
3978 oberon_expr_t * cond;
3979 cond = list_args;
3980 oberon_check_src(ctx, cond);
3982 if(!oberon_is_boolean_type(cond -> result))
3984 oberon_error(ctx, "expected boolean");
3987 if(num_args == 1)
3989 oberon_generate_assert(ctx, cond);
3991 else
3993 oberon_expr_t * num;
3994 num = list_args -> next;
3995 oberon_check_src(ctx, num);
3997 if(!oberon_is_integer_type(num -> result))
3999 oberon_error(ctx, "expected integer");
4002 oberon_check_const(ctx, num);
4004 oberon_generate_assert_n(ctx, cond, num -> item.integer);
4008 static void
4009 oberon_make_halt_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4011 if(num_args < 1)
4013 oberon_error(ctx, "too few arguments");
4016 if(num_args > 1)
4018 oberon_error(ctx, "too mach arguments");
4021 oberon_expr_t * num;
4022 num = list_args;
4023 oberon_check_src(ctx, num);
4025 if(num -> result -> class != OBERON_TYPE_INTEGER)
4027 oberon_error(ctx, "expected integer");
4030 oberon_check_const(ctx, num);
4032 oberon_generate_halt(ctx, num -> item.integer);
4035 static oberon_expr_t *
4036 oberon_make_ash_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4038 if(num_args < 2)
4040 oberon_error(ctx, "too few arguments");
4043 if(num_args > 2)
4045 oberon_error(ctx, "too mach arguments");
4048 oberon_expr_t * arg1;
4049 arg1 = list_args;
4050 oberon_check_src(ctx, arg1);
4051 if(arg1 -> result -> class != OBERON_TYPE_INTEGER)
4053 oberon_error(ctx, "expected integer");
4056 oberon_expr_t * arg2;
4057 arg2 = list_args -> next;
4058 oberon_check_src(ctx, arg2);
4059 if(arg2 -> result -> class != OBERON_TYPE_INTEGER)
4061 oberon_error(ctx, "expected integer");
4064 oberon_expr_t * expr;
4065 if(oberon_is_const(arg1) && oberon_is_const(arg2))
4067 int64_t x = arg1 -> item.integer;
4068 int64_t y = arg2 -> item.integer;
4069 expr = oberon_make_integer(ctx, x * powl(2, y));
4071 else
4073 expr = oberon_new_operator(OP_ASH, arg1 -> result, arg1, arg2);
4076 return expr;
4079 static oberon_expr_t *
4080 oberon_make_cap_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4082 if(num_args < 1)
4084 oberon_error(ctx, "too few arguments");
4087 if(num_args > 1)
4089 oberon_error(ctx, "too mach arguments");
4092 oberon_expr_t * arg;
4093 arg = list_args;
4094 oberon_check_src(ctx, arg);
4096 if(!oberon_is_char_type(arg -> result))
4098 oberon_error(ctx, "expected char");
4101 oberon_expr_t * expr;
4102 if(oberon_is_const(arg))
4104 expr = oberon_make_char(ctx, toupper(arg -> item.integer));
4106 else
4108 expr = oberon_new_operator(OP_CAP, arg -> result, arg, NULL);
4111 return expr;
4114 static oberon_expr_t *
4115 oberon_make_chr_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4117 if(num_args < 1)
4119 oberon_error(ctx, "too few arguments");
4122 if(num_args > 1)
4124 oberon_error(ctx, "too mach arguments");
4127 oberon_expr_t * arg;
4128 arg = list_args;
4129 oberon_check_src(ctx, arg);
4131 if(!oberon_is_integer_type(arg -> result))
4133 oberon_error(ctx, "expected integer");
4136 oberon_expr_t * expr;
4137 if(oberon_is_const(arg))
4139 expr = oberon_make_char(ctx, arg -> item.integer);
4141 else
4143 expr = oberon_cast_expr(ctx, arg, ctx -> char_type);
4145 return expr;
4148 static oberon_expr_t *
4149 oberon_make_ord_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4151 if(num_args < 1)
4153 oberon_error(ctx, "too few arguments");
4156 if(num_args > 1)
4158 oberon_error(ctx, "too mach arguments");
4161 oberon_expr_t * arg;
4162 arg = list_args;
4163 oberon_check_src(ctx, arg);
4165 if(!oberon_is_char_type(arg -> result))
4167 oberon_error(ctx, "expected char");
4170 oberon_expr_t * expr;
4171 if(oberon_is_const(arg))
4173 expr = oberon_make_integer(ctx, arg -> item.integer);
4175 else
4177 expr = oberon_cast_expr(ctx, arg, ctx -> int_type);
4179 return expr;
4182 static oberon_expr_t *
4183 oberon_make_entier_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4185 if(num_args < 1)
4187 oberon_error(ctx, "too few arguments");
4190 if(num_args > 1)
4192 oberon_error(ctx, "too mach arguments");
4195 oberon_expr_t * arg;
4196 arg = list_args;
4197 oberon_check_src(ctx, arg);
4199 if(!oberon_is_real_type(arg -> result))
4201 oberon_error(ctx, "expected real");
4204 oberon_expr_t * expr;
4205 if(oberon_is_const(arg))
4207 expr = oberon_make_integer(ctx, floor(arg -> item.real));
4209 else
4211 expr = oberon_new_operator(OP_ENTIER, ctx -> int_type, arg, NULL);
4213 return expr;
4216 static oberon_expr_t *
4217 oberon_make_odd_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4219 if(num_args < 1)
4221 oberon_error(ctx, "too few arguments");
4224 if(num_args > 1)
4226 oberon_error(ctx, "too mach arguments");
4229 oberon_expr_t * arg;
4230 arg = list_args;
4231 oberon_check_src(ctx, arg);
4233 if(!oberon_is_integer_type(arg -> result))
4235 oberon_error(ctx, "expected integer");
4238 oberon_expr_t * expr;
4239 expr = oberon_make_bin_op(ctx, MOD, arg, oberon_make_integer(ctx, 2));
4240 expr = oberon_make_bin_op(ctx, EQUAL, expr, oberon_make_integer(ctx, 1));
4241 return expr;
4244 static oberon_expr_t *
4245 oberon_make_len_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4247 if(num_args < 1)
4249 oberon_error(ctx, "too few arguments");
4252 if(num_args > 2)
4254 oberon_error(ctx, "too mach arguments");
4257 oberon_expr_t * v;
4258 v = list_args;
4259 oberon_check_src(ctx, v);
4261 if(!oberon_is_array_type(v -> result))
4263 oberon_error(ctx, "expected array");
4266 int n = 0;
4267 if(num_args == 2)
4269 oberon_expr_t * num;
4270 num = list_args -> next;
4271 oberon_check_src(ctx, num);
4273 if(!oberon_is_integer_type(num -> result))
4275 oberon_error(ctx, "expected integer");
4277 oberon_check_const(ctx, num);
4279 n = num -> item.integer;
4282 int dim = 0;
4283 oberon_type_t * arr = v -> result;
4284 while(arr -> class == OBERON_TYPE_ARRAY)
4286 dim += 1;
4287 arr = arr -> base;
4290 if(n < 0 || n > dim)
4292 oberon_error(ctx, "not in range 0..%i", dim - 1);
4295 assert(v -> is_item);
4297 oberon_expr_t * expr;
4298 expr = oberon_new_item(MODE_LEN, ctx -> int_type, true);
4299 expr -> item.parent = (oberon_item_t *) v;
4300 expr -> item.integer = n;
4301 return expr;
4304 static void
4305 oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
4307 oberon_object_t * constant;
4308 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST, true, false, false);
4309 oberon_check_const(ctx, expr);
4310 constant -> value = (oberon_item_t *) expr;
4313 oberon_context_t *
4314 oberon_create_context(ModuleImportCallback import_module)
4316 oberon_context_t * ctx = calloc(1, sizeof *ctx);
4318 oberon_scope_t * world_scope;
4319 world_scope = oberon_open_scope(ctx);
4320 ctx -> world_scope = world_scope;
4322 ctx -> import_module = import_module;
4324 oberon_generator_init_context(ctx);
4326 register_default_types(ctx);
4328 /* Constants */
4329 oberon_new_const(ctx, "TRUE", oberon_make_boolean(ctx, true));
4330 oberon_new_const(ctx, "FALSE", oberon_make_boolean(ctx, false));
4332 /* Functions */
4333 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
4334 oberon_new_intrinsic(ctx, "ASH", oberon_make_ash_call, NULL);
4335 oberon_new_intrinsic(ctx, "CAP", oberon_make_cap_call, NULL);
4336 oberon_new_intrinsic(ctx, "CHR", oberon_make_chr_call, NULL);
4337 oberon_new_intrinsic(ctx, "ENTIER", oberon_make_entier_call, NULL);
4338 oberon_new_intrinsic(ctx, "LEN", oberon_make_len_call, NULL);
4339 oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
4340 oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
4341 oberon_new_intrinsic(ctx, "ODD", oberon_make_odd_call, NULL);
4342 oberon_new_intrinsic(ctx, "ORD", oberon_make_ord_call, NULL);
4343 oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
4345 /* Procedures */
4346 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
4347 oberon_new_intrinsic(ctx, "COPY", NULL, oberon_make_copy_call);
4348 oberon_new_intrinsic(ctx, "ASSERT", NULL, oberon_make_assert_call);
4349 oberon_new_intrinsic(ctx, "HALT", NULL, oberon_make_halt_call);
4351 return ctx;
4354 void
4355 oberon_destroy_context(oberon_context_t * ctx)
4357 oberon_generator_destroy_context(ctx);
4358 free(ctx);
4361 oberon_module_t *
4362 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
4364 const char * code = ctx -> code;
4365 int code_index = ctx -> code_index;
4366 char c = ctx -> c;
4367 int token = ctx -> token;
4368 char * string = ctx -> string;
4369 int integer = ctx -> integer;
4370 int real = ctx -> real;
4371 bool longmode = ctx -> longmode;
4372 oberon_scope_t * decl = ctx -> decl;
4373 oberon_module_t * mod = ctx -> mod;
4375 oberon_scope_t * module_scope;
4376 module_scope = oberon_open_scope(ctx);
4378 oberon_module_t * module;
4379 module = calloc(1, sizeof *module);
4380 module -> decl = module_scope;
4381 module -> next = ctx -> module_list;
4383 ctx -> mod = module;
4384 ctx -> module_list = module;
4386 oberon_init_scaner(ctx, newcode);
4387 oberon_parse_module(ctx);
4389 module -> ready = 1;
4391 ctx -> code = code;
4392 ctx -> code_index = code_index;
4393 ctx -> c = c;
4394 ctx -> token = token;
4395 ctx -> string = string;
4396 ctx -> integer = integer;
4397 ctx -> real = real;
4398 ctx -> longmode = longmode;
4399 ctx -> decl = decl;
4400 ctx -> mod = mod;
4402 return module;