DEADSOFTWARE

Добавлена свёртка констант
[dsw-obn.git] / src / oberon.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdbool.h>
8 #include <math.h>
10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "oberon-type-compat.h"
14 #include "oberon-common.h"
15 #include "generator.h"
17 // =======================================================================
18 // UTILS
19 // =======================================================================
21 static void
22 oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args);
24 static oberon_type_t *
25 oberon_new_type_ptr(int class)
26 {
27 oberon_type_t * x = malloc(sizeof *x);
28 memset(x, 0, sizeof *x);
29 x -> class = class;
30 return x;
31 }
33 static oberon_type_t *
34 oberon_new_type_integer(int size)
35 {
36 oberon_type_t * x;
37 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
38 x -> size = size;
39 return x;
40 }
42 static oberon_type_t *
43 oberon_new_type_boolean()
44 {
45 oberon_type_t * x;
46 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
47 return x;
48 }
50 static oberon_type_t *
51 oberon_new_type_real(int size)
52 {
53 oberon_type_t * x;
54 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
55 x -> size = size;
56 return x;
57 }
59 static oberon_type_t *
60 oberon_new_type_char(int size)
61 {
62 oberon_type_t * x;
63 x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
64 x -> size = size;
65 return x;
66 }
68 static oberon_type_t *
69 oberon_new_type_string(int size)
70 {
71 oberon_type_t * x;
72 x = oberon_new_type_ptr(OBERON_TYPE_STRING);
73 x -> size = size;
74 return x;
75 }
77 static oberon_type_t *
78 oberon_new_type_set(int size)
79 {
80 oberon_type_t * x;
81 x = oberon_new_type_ptr(OBERON_TYPE_SET);
82 x -> size = size;
83 return x;
84 }
86 static oberon_expr_t *
87 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
88 {
89 oberon_oper_t * operator;
90 operator = malloc(sizeof *operator);
91 memset(operator, 0, sizeof *operator);
93 operator -> is_item = 0;
94 operator -> result = result;
95 operator -> read_only = 1;
96 operator -> op = op;
97 operator -> left = left;
98 operator -> right = right;
100 return (oberon_expr_t *) operator;
103 static oberon_expr_t *
104 oberon_new_item(int mode, oberon_type_t * result, int read_only)
106 oberon_item_t * item;
107 item = malloc(sizeof *item);
108 memset(item, 0, sizeof *item);
110 item -> is_item = 1;
111 item -> result = result;
112 item -> read_only = read_only;
113 item -> mode = mode;
115 return (oberon_expr_t *)item;
118 static oberon_type_t *
119 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
121 if(i >= -128 && i <= 127)
123 return ctx -> byte_type;
125 else if(i >= -32768 && i <= 32767)
127 return ctx -> shortint_type;
129 else if(i >= -2147483648 && i <= 2147483647)
131 return ctx -> int_type;
133 else
135 return ctx -> longint_type;
139 static oberon_expr_t *
140 oberon_make_integer(oberon_context_t * ctx, int64_t i)
142 oberon_expr_t * expr;
143 oberon_type_t * result;
144 result = oberon_get_type_of_int_value(ctx, i);
145 expr = oberon_new_item(MODE_INTEGER, result, true);
146 expr -> item.integer = i;
147 expr -> item.real = i;
148 return expr;
151 static oberon_expr_t *
152 oberon_make_real_typed(oberon_context_t * ctx, double r, oberon_type_t * result)
154 oberon_expr_t * expr;
155 expr = oberon_new_item(MODE_REAL, result, true);
156 expr -> item.integer = r;
157 expr -> item.real = r;
158 return expr;
161 static oberon_expr_t *
162 oberon_make_real(oberon_context_t * ctx, double r, bool longmode)
164 oberon_type_t * result;
165 result = (longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
166 return oberon_make_real_typed(ctx, r, result);
169 static oberon_expr_t *
170 oberon_make_boolean(oberon_context_t * ctx, bool cond)
172 oberon_expr_t * expr;
173 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
174 expr -> item.integer = cond;
175 expr -> item.real = cond;
176 return expr;
179 static oberon_expr_t *
180 oberon_make_set(oberon_context_t * ctx, int64_t i)
182 oberon_expr_t * expr;
183 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
184 expr -> item.integer = i;
185 expr -> item.real = i;
186 return expr;
189 static oberon_expr_t *
190 oberon_make_set_range(oberon_context_t * ctx, int64_t x, int64_t y)
192 oberon_expr_t * expr;
193 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
194 expr -> item.integer = (x <= y) ? ((2 << y) - (1 << x)) : (0);
195 expr -> item.real = expr -> item.integer;
196 return expr;
199 // =======================================================================
200 // TABLE
201 // =======================================================================
203 static oberon_scope_t *
204 oberon_open_scope(oberon_context_t * ctx)
206 oberon_scope_t * scope = calloc(1, sizeof *scope);
207 oberon_object_t * list = calloc(1, sizeof *list);
209 scope -> ctx = ctx;
210 scope -> list = list;
211 scope -> up = ctx -> decl;
213 if(scope -> up)
215 scope -> local = scope -> up -> local;
216 scope -> parent = scope -> up -> parent;
217 scope -> parent_type = scope -> up -> parent_type;
218 scope -> exit_label = scope -> up -> exit_label;
221 ctx -> decl = scope;
222 return scope;
225 static void
226 oberon_close_scope(oberon_scope_t * scope)
228 oberon_context_t * ctx = scope -> ctx;
229 ctx -> decl = scope -> up;
232 static oberon_object_t *
233 oberon_find_object_in_list(oberon_object_t * list, char * name)
235 oberon_object_t * x = list;
236 while(x -> next && strcmp(x -> next -> name, name) != 0)
238 x = x -> next;
240 return x -> next;
243 static oberon_object_t *
244 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
246 oberon_object_t * result = NULL;
248 oberon_scope_t * s = scope;
249 while(result == NULL && s != NULL)
251 result = oberon_find_object_in_list(s -> list, name);
252 s = s -> up;
255 if(check_it && result == NULL)
257 oberon_error(scope -> ctx, "undefined ident %s", name);
260 return result;
263 static oberon_object_t *
264 oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only)
266 oberon_object_t * newvar = malloc(sizeof *newvar);
267 memset(newvar, 0, sizeof *newvar);
268 newvar -> name = name;
269 newvar -> class = class;
270 newvar -> export = export;
271 newvar -> read_only = read_only;
272 newvar -> local = scope -> local;
273 newvar -> parent = scope -> parent;
274 newvar -> parent_type = scope -> parent_type;
275 newvar -> module = scope -> ctx -> mod;
276 return newvar;
279 static oberon_object_t *
280 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
282 if(check_upscope)
284 if(oberon_find_object(scope -> up, name, false))
286 oberon_error(scope -> ctx, "already defined");
290 oberon_object_t * x = scope -> list;
291 while(x -> next && strcmp(x -> next -> name, name) != 0)
293 x = x -> next;
296 if(x -> next)
298 oberon_error(scope -> ctx, "already defined");
301 oberon_object_t * newvar;
302 newvar = oberon_create_object(scope, name, class, export, read_only);
303 x -> next = newvar;
305 return newvar;
308 static oberon_object_t *
309 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
311 oberon_object_t * id;
312 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
313 id -> type = type;
314 oberon_generator_init_type(scope -> ctx, type);
315 return id;
318 // =======================================================================
319 // SCANER
320 // =======================================================================
322 static void
323 oberon_get_char(oberon_context_t * ctx)
325 if(ctx -> code[ctx -> code_index])
327 ctx -> code_index += 1;
328 ctx -> c = ctx -> code[ctx -> code_index];
332 static void
333 oberon_init_scaner(oberon_context_t * ctx, const char * code)
335 ctx -> code = code;
336 ctx -> code_index = 0;
337 ctx -> c = ctx -> code[ctx -> code_index];
340 static void
341 oberon_read_ident(oberon_context_t * ctx)
343 int len = 0;
344 int i = ctx -> code_index;
346 int c = ctx -> code[i];
347 while(isalnum(c))
349 i += 1;
350 len += 1;
351 c = ctx -> code[i];
354 char * ident = malloc(len + 1);
355 memcpy(ident, &ctx->code[ctx->code_index], len);
356 ident[len] = 0;
358 ctx -> code_index = i;
359 ctx -> c = ctx -> code[i];
360 ctx -> string = ident;
361 ctx -> token = IDENT;
363 if(strcmp(ident, "MODULE") == 0)
365 ctx -> token = MODULE;
367 else if(strcmp(ident, "END") == 0)
369 ctx -> token = END;
371 else if(strcmp(ident, "VAR") == 0)
373 ctx -> token = VAR;
375 else if(strcmp(ident, "BEGIN") == 0)
377 ctx -> token = BEGIN;
379 else if(strcmp(ident, "OR") == 0)
381 ctx -> token = OR;
383 else if(strcmp(ident, "DIV") == 0)
385 ctx -> token = DIV;
387 else if(strcmp(ident, "MOD") == 0)
389 ctx -> token = MOD;
391 else if(strcmp(ident, "PROCEDURE") == 0)
393 ctx -> token = PROCEDURE;
395 else if(strcmp(ident, "RETURN") == 0)
397 ctx -> token = RETURN;
399 else if(strcmp(ident, "CONST") == 0)
401 ctx -> token = CONST;
403 else if(strcmp(ident, "TYPE") == 0)
405 ctx -> token = TYPE;
407 else if(strcmp(ident, "ARRAY") == 0)
409 ctx -> token = ARRAY;
411 else if(strcmp(ident, "OF") == 0)
413 ctx -> token = OF;
415 else if(strcmp(ident, "RECORD") == 0)
417 ctx -> token = RECORD;
419 else if(strcmp(ident, "POINTER") == 0)
421 ctx -> token = POINTER;
423 else if(strcmp(ident, "TO") == 0)
425 ctx -> token = TO;
427 else if(strcmp(ident, "NIL") == 0)
429 ctx -> token = NIL;
431 else if(strcmp(ident, "IMPORT") == 0)
433 ctx -> token = IMPORT;
435 else if(strcmp(ident, "IN") == 0)
437 ctx -> token = IN;
439 else if(strcmp(ident, "IS") == 0)
441 ctx -> token = IS;
443 else if(strcmp(ident, "IF") == 0)
445 ctx -> token = IF;
447 else if(strcmp(ident, "THEN") == 0)
449 ctx -> token = THEN;
451 else if(strcmp(ident, "ELSE") == 0)
453 ctx -> token = ELSE;
455 else if(strcmp(ident, "ELSIF") == 0)
457 ctx -> token = ELSIF;
459 else if(strcmp(ident, "WHILE") == 0)
461 ctx -> token = WHILE;
463 else if(strcmp(ident, "DO") == 0)
465 ctx -> token = DO;
467 else if(strcmp(ident, "REPEAT") == 0)
469 ctx -> token = REPEAT;
471 else if(strcmp(ident, "UNTIL") == 0)
473 ctx -> token = UNTIL;
475 else if(strcmp(ident, "FOR") == 0)
477 ctx -> token = FOR;
479 else if(strcmp(ident, "BY") == 0)
481 ctx -> token = BY;
483 else if(strcmp(ident, "LOOP") == 0)
485 ctx -> token = LOOP;
487 else if(strcmp(ident, "EXIT") == 0)
489 ctx -> token = EXIT;
491 else if(strcmp(ident, "CASE") == 0)
493 ctx -> token = CASE;
495 else if(strcmp(ident, "WITH") == 0)
497 ctx -> token = WITH;
501 #define ISHEXDIGIT(x) \
502 (((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F'))
504 static void
505 oberon_read_number(oberon_context_t * ctx)
507 long integer;
508 double real;
509 char * ident;
510 int start_i;
511 int exp_i;
512 int end_i;
514 /*
515 * mode = 0 == DEC
516 * mode = 1 == HEX
517 * mode = 2 == REAL
518 * mode = 3 == LONGREAL
519 * mode = 4 == CHAR
520 */
521 int mode = 0;
522 start_i = ctx -> code_index;
524 while(isdigit(ctx -> c))
526 oberon_get_char(ctx);
529 end_i = ctx -> code_index;
531 if(ISHEXDIGIT(ctx -> c))
533 mode = 1;
534 while(ISHEXDIGIT(ctx -> c))
536 oberon_get_char(ctx);
539 end_i = ctx -> code_index;
541 if(ctx -> c == 'H')
543 mode = 1;
544 oberon_get_char(ctx);
546 else if(ctx -> c == 'X')
548 mode = 4;
549 oberon_get_char(ctx);
551 else
553 oberon_error(ctx, "invalid hex number");
556 else if(ctx -> c == '.')
558 oberon_get_char(ctx);
559 if(ctx -> c == '.')
561 /* Чит: избегаем конфликта с DOTDOT */
562 ctx -> code_index -= 1;
564 else
566 mode = 2;
568 while(isdigit(ctx -> c))
570 oberon_get_char(ctx);
573 if(ctx -> c == 'E' || ctx -> c == 'D')
575 exp_i = ctx -> code_index;
577 if(ctx -> c == 'D')
579 mode = 3;
582 oberon_get_char(ctx);
584 if(ctx -> c == '+' || ctx -> c == '-')
586 oberon_get_char(ctx);
589 while(isdigit(ctx -> c))
591 oberon_get_char(ctx);
592 }
595 end_i = ctx -> code_index;
598 if(mode == 0)
600 if(ctx -> c == 'H')
602 mode = 1;
603 oberon_get_char(ctx);
605 else if(ctx -> c == 'X')
607 mode = 4;
608 oberon_get_char(ctx);
612 int len = end_i - start_i;
613 ident = malloc(len + 1);
614 memcpy(ident, &ctx -> code[start_i], len);
615 ident[len] = 0;
617 ctx -> longmode = false;
618 if(mode == 3)
620 int i = exp_i - start_i;
621 ident[i] = 'E';
622 ctx -> longmode = true;
625 switch(mode)
627 case 0:
628 integer = atol(ident);
629 real = integer;
630 ctx -> token = INTEGER;
631 break;
632 case 1:
633 sscanf(ident, "%lx", &integer);
634 real = integer;
635 ctx -> token = INTEGER;
636 break;
637 case 2:
638 case 3:
639 sscanf(ident, "%lf", &real);
640 integer = real;
641 ctx -> token = REAL;
642 break;
643 case 4:
644 sscanf(ident, "%lx", &integer);
645 real = integer;
646 ctx -> token = CHAR;
647 break;
648 default:
649 oberon_error(ctx, "oberon_read_number: wat");
650 break;
653 ctx -> string = ident;
654 ctx -> integer = integer;
655 ctx -> real = real;
658 static void
659 oberon_skip_space(oberon_context_t * ctx)
661 while(isspace(ctx -> c))
663 oberon_get_char(ctx);
667 static void
668 oberon_read_comment(oberon_context_t * ctx)
670 int nesting = 1;
671 while(nesting >= 1)
673 if(ctx -> c == '(')
675 oberon_get_char(ctx);
676 if(ctx -> c == '*')
678 oberon_get_char(ctx);
679 nesting += 1;
682 else if(ctx -> c == '*')
684 oberon_get_char(ctx);
685 if(ctx -> c == ')')
687 oberon_get_char(ctx);
688 nesting -= 1;
691 else if(ctx -> c == 0)
693 oberon_error(ctx, "unterminated comment");
695 else
697 oberon_get_char(ctx);
702 static void oberon_read_string(oberon_context_t * ctx)
704 int c = ctx -> c;
705 oberon_get_char(ctx);
707 int start = ctx -> code_index;
709 while(ctx -> c != 0 && ctx -> c != c)
711 oberon_get_char(ctx);
714 if(ctx -> c == 0)
716 oberon_error(ctx, "unterminated string");
719 int end = ctx -> code_index;
721 oberon_get_char(ctx);
723 char * string = calloc(1, end - start + 1);
724 strncpy(string, &ctx -> code[start], end - start);
726 ctx -> token = STRING;
727 ctx -> string = string;
728 ctx -> integer = string[0];
731 static void oberon_read_token(oberon_context_t * ctx);
733 static void
734 oberon_read_symbol(oberon_context_t * ctx)
736 int c = ctx -> c;
737 switch(c)
739 case 0:
740 ctx -> token = EOF_;
741 break;
742 case ';':
743 ctx -> token = SEMICOLON;
744 oberon_get_char(ctx);
745 break;
746 case ':':
747 ctx -> token = COLON;
748 oberon_get_char(ctx);
749 if(ctx -> c == '=')
751 ctx -> token = ASSIGN;
752 oberon_get_char(ctx);
754 break;
755 case '.':
756 ctx -> token = DOT;
757 oberon_get_char(ctx);
758 if(ctx -> c == '.')
760 ctx -> token = DOTDOT;
761 oberon_get_char(ctx);
763 break;
764 case '(':
765 ctx -> token = LPAREN;
766 oberon_get_char(ctx);
767 if(ctx -> c == '*')
769 oberon_get_char(ctx);
770 oberon_read_comment(ctx);
771 oberon_read_token(ctx);
773 break;
774 case ')':
775 ctx -> token = RPAREN;
776 oberon_get_char(ctx);
777 break;
778 case '=':
779 ctx -> token = EQUAL;
780 oberon_get_char(ctx);
781 break;
782 case '#':
783 ctx -> token = NEQ;
784 oberon_get_char(ctx);
785 break;
786 case '<':
787 ctx -> token = LESS;
788 oberon_get_char(ctx);
789 if(ctx -> c == '=')
791 ctx -> token = LEQ;
792 oberon_get_char(ctx);
794 break;
795 case '>':
796 ctx -> token = GREAT;
797 oberon_get_char(ctx);
798 if(ctx -> c == '=')
800 ctx -> token = GEQ;
801 oberon_get_char(ctx);
803 break;
804 case '+':
805 ctx -> token = PLUS;
806 oberon_get_char(ctx);
807 break;
808 case '-':
809 ctx -> token = MINUS;
810 oberon_get_char(ctx);
811 break;
812 case '*':
813 ctx -> token = STAR;
814 oberon_get_char(ctx);
815 if(ctx -> c == ')')
817 oberon_get_char(ctx);
818 oberon_error(ctx, "unstarted comment");
820 break;
821 case '/':
822 ctx -> token = SLASH;
823 oberon_get_char(ctx);
824 break;
825 case '&':
826 ctx -> token = AND;
827 oberon_get_char(ctx);
828 break;
829 case '~':
830 ctx -> token = NOT;
831 oberon_get_char(ctx);
832 break;
833 case ',':
834 ctx -> token = COMMA;
835 oberon_get_char(ctx);
836 break;
837 case '[':
838 ctx -> token = LBRACK;
839 oberon_get_char(ctx);
840 break;
841 case ']':
842 ctx -> token = RBRACK;
843 oberon_get_char(ctx);
844 break;
845 case '^':
846 ctx -> token = UPARROW;
847 oberon_get_char(ctx);
848 break;
849 case '"':
850 oberon_read_string(ctx);
851 break;
852 case '\'':
853 oberon_read_string(ctx);
854 break;
855 case '{':
856 ctx -> token = LBRACE;
857 oberon_get_char(ctx);
858 break;
859 case '}':
860 ctx -> token = RBRACE;
861 oberon_get_char(ctx);
862 break;
863 case '|':
864 ctx -> token = BAR;
865 oberon_get_char(ctx);
866 break;
867 default:
868 oberon_error(ctx, "invalid char %c", ctx -> c);
869 break;
873 static void
874 oberon_read_token(oberon_context_t * ctx)
876 oberon_skip_space(ctx);
878 int c = ctx -> c;
879 if(isalpha(c))
881 oberon_read_ident(ctx);
883 else if(isdigit(c))
885 oberon_read_number(ctx);
887 else
889 oberon_read_symbol(ctx);
893 // =======================================================================
894 // EXPRESSION
895 // =======================================================================
897 static void oberon_expect_token(oberon_context_t * ctx, int token);
898 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
899 static void oberon_assert_token(oberon_context_t * ctx, int token);
900 static char * oberon_assert_ident(oberon_context_t * ctx);
901 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
902 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
903 static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
904 static bool oberon_is_const(oberon_expr_t * expr);
906 static oberon_expr_t *
907 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
909 oberon_expr_t * expr;
910 oberon_type_t * result;
912 result = a -> result;
914 if(token == MINUS)
916 if(result -> class == OBERON_TYPE_SET)
918 if(oberon_is_const(a))
920 expr = oberon_make_set(ctx, ~(a -> item.integer));
922 else
924 expr = oberon_new_operator(OP_COMPLEMENTATION, result, a, NULL);
927 else if(result -> class == OBERON_TYPE_INTEGER)
929 if(oberon_is_const(a))
931 expr = oberon_make_integer(ctx, -(a -> item.integer));
933 else
935 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
938 else if(result -> class == OBERON_TYPE_REAL)
940 if(oberon_is_const(a))
942 expr = oberon_make_real_typed(ctx, -(a -> item.real), result);
944 else
946 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
949 else
951 oberon_error(ctx, "incompatible operator type");
954 else if(token == NOT)
956 if(result -> class != OBERON_TYPE_BOOLEAN)
958 oberon_error(ctx, "incompatible operator type");
961 if(oberon_is_const(a))
963 expr = oberon_make_boolean(ctx, !(a -> item.integer));
965 else
967 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
970 else
972 oberon_error(ctx, "oberon_make_unary_op: wat");
975 return expr;
978 static void
979 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
981 oberon_expr_t * last;
983 *num_expr = 1;
984 if(const_expr)
986 *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
988 else
990 *first = last = oberon_expr(ctx);
992 while(ctx -> token == COMMA)
994 oberon_assert_token(ctx, COMMA);
995 oberon_expr_t * current;
997 if(const_expr)
999 current = (oberon_expr_t *) oberon_const_expr(ctx);
1001 else
1003 current = oberon_expr(ctx);
1006 last -> next = current;
1007 last = current;
1008 *num_expr += 1;
1012 static oberon_expr_t *
1013 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
1015 oberon_expr_t * cast;
1017 if((oberon_is_char_type(pref) && oberon_is_const_string(expr) && strlen(expr -> item.string) == 1))
1019 /* Автоматически преобразуем строку единичного размера в символ */
1020 cast = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
1021 cast -> item.integer = expr -> item.string[0];
1023 else
1025 cast = oberon_new_operator(OP_CAST, pref, expr, NULL);
1028 return cast;
1031 static void
1032 oberon_check_dst(oberon_context_t * ctx, oberon_expr_t * dst)
1034 if(dst -> read_only)
1036 oberon_error(ctx, "read-only destination");
1039 if(dst -> is_item == false)
1041 oberon_error(ctx, "not variable");
1044 switch(dst -> item.mode)
1046 case MODE_VAR:
1047 case MODE_CALL:
1048 case MODE_INDEX:
1049 case MODE_FIELD:
1050 case MODE_DEREF:
1051 case MODE_NEW:
1052 /* accept */
1053 break;
1054 default:
1055 oberon_error(ctx, "not variable");
1056 break;
1060 static void
1061 oberon_check_src(oberon_context_t * ctx, oberon_expr_t * src)
1063 if(src -> is_item)
1065 if(src -> item.mode == MODE_TYPE)
1067 oberon_error(ctx, "not variable");
1072 static void
1073 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
1075 if(desig -> mode != MODE_CALL)
1077 oberon_error(ctx, "expected mode CALL");
1080 oberon_type_t * fn = desig -> parent -> result;
1081 int num_args = desig -> num_args;
1082 int num_decl = fn -> num_decl;
1084 if(num_args < num_decl)
1086 oberon_error(ctx, "too few arguments");
1088 else if(num_args > num_decl)
1090 oberon_error(ctx, "too many arguments");
1093 /* Делаем проверку на запись и делаем автокаст */
1094 oberon_expr_t * casted[num_args];
1095 oberon_expr_t * arg = desig -> args;
1096 oberon_object_t * param = fn -> decl;
1097 for(int i = 0; i < num_args; i++)
1099 if(param -> class == OBERON_CLASS_VAR_PARAM)
1101 oberon_check_dst(ctx, arg);
1102 if(!oberon_is_compatible_arrays(param, arg))
1104 oberon_check_compatible_var_param(ctx, param -> type, arg -> result);
1106 casted[i] = oberon_cast_expr(ctx, arg, param -> type);
1108 else
1110 oberon_check_src(ctx, arg);
1111 if(!oberon_is_compatible_arrays(param, arg))
1113 oberon_check_assignment_compatible(ctx, arg, param -> type);
1115 casted[i] = oberon_cast_expr(ctx, arg, param -> type);
1118 arg = arg -> next;
1119 param = param -> next;
1122 /* Создаём новый список выражений */
1123 if(num_args > 0)
1125 arg = casted[0];
1126 for(int i = 0; i < num_args - 1; i++)
1128 casted[i] -> next = casted[i + 1];
1130 desig -> args = arg;
1134 static oberon_expr_t *
1135 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1137 oberon_type_t * signature = item -> result;
1138 if(signature -> class != OBERON_TYPE_PROCEDURE)
1140 oberon_error(ctx, "not a procedure");
1143 oberon_expr_t * call;
1145 if(signature -> sysproc)
1147 if(signature -> genfunc == NULL)
1149 oberon_error(ctx, "not a function-procedure");
1152 call = signature -> genfunc(ctx, num_args, list_args);
1154 else
1156 if(signature -> base -> class == OBERON_TYPE_NOTYPE)
1158 oberon_error(ctx, "attempt to call procedure in expression");
1161 call = oberon_new_item(MODE_CALL, signature -> base, true);
1162 call -> item.parent = item;
1163 call -> item.num_args = num_args;
1164 call -> item.args = list_args;
1165 oberon_autocast_call(ctx, (oberon_item_t *) call);
1168 return call;
1171 static void
1172 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1174 oberon_type_t * signature = item -> result;
1175 if(signature -> class != OBERON_TYPE_PROCEDURE)
1177 oberon_error(ctx, "not a procedure");
1180 oberon_expr_t * call;
1182 if(signature -> sysproc)
1184 if(signature -> genproc == NULL)
1186 oberon_error(ctx, "not a procedure");
1189 signature -> genproc(ctx, num_args, list_args);
1191 else
1193 if(signature -> base -> class != OBERON_TYPE_NOTYPE)
1195 oberon_error(ctx, "attempt to call function as non-typed procedure");
1198 call = oberon_new_item(MODE_CALL, signature -> base, true);
1199 call -> item.parent = item;
1200 call -> item.num_args = num_args;
1201 call -> item.args = list_args;
1202 oberon_autocast_call(ctx, (oberon_item_t *) call);
1203 oberon_generate_call_proc(ctx, call);
1207 #define ISEXPR(x) \
1208 (((x) == PLUS) \
1209 || ((x) == MINUS) \
1210 || ((x) == IDENT) \
1211 || ((x) == INTEGER) \
1212 || ((x) == REAL) \
1213 || ((x) == CHAR) \
1214 || ((x) == STRING) \
1215 || ((x) == NIL) \
1216 || ((x) == LPAREN) \
1217 || ((x) == NOT))
1219 static oberon_expr_t *
1220 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1222 if(expr -> result -> class != OBERON_TYPE_POINTER)
1224 oberon_error(ctx, "not a pointer");
1227 assert(expr -> is_item);
1229 oberon_expr_t * selector;
1230 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
1231 selector -> item.parent = (oberon_item_t *) expr;
1233 return selector;
1236 static oberon_expr_t *
1237 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1239 if(desig -> result -> class == OBERON_TYPE_POINTER)
1241 desig = oberno_make_dereferencing(ctx, desig);
1244 assert(desig -> is_item);
1246 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1248 oberon_error(ctx, "not array");
1251 oberon_type_t * base;
1252 base = desig -> result -> base;
1254 if(index -> result -> class != OBERON_TYPE_INTEGER)
1256 oberon_error(ctx, "index must be integer");
1259 // Статическая проверка границ массива
1260 if(desig -> result -> size != 0)
1262 if(index -> is_item)
1264 if(index -> item.mode == MODE_INTEGER)
1266 int arr_size = desig -> result -> size;
1267 int index_int = index -> item.integer;
1268 if(index_int < 0 || index_int > arr_size - 1)
1270 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1276 oberon_expr_t * selector;
1277 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1278 selector -> item.parent = (oberon_item_t *) desig;
1279 selector -> item.num_args = 1;
1280 selector -> item.args = index;
1282 return selector;
1285 static oberon_expr_t *
1286 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1288 if(expr -> result -> class == OBERON_TYPE_POINTER)
1290 expr = oberno_make_dereferencing(ctx, expr);
1293 assert(expr -> is_item);
1295 if(expr -> result -> class != OBERON_TYPE_RECORD)
1297 oberon_error(ctx, "not record");
1300 oberon_type_t * rec = expr -> result;
1302 oberon_object_t * field;
1303 field = oberon_find_object(rec -> scope, name, true);
1305 if(field -> export == 0)
1307 if(field -> module != ctx -> mod)
1309 oberon_error(ctx, "field not exported");
1313 int read_only = expr -> read_only;
1314 if(field -> read_only)
1316 if(field -> module != ctx -> mod)
1318 read_only = 1;
1322 oberon_expr_t * selector;
1323 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1324 selector -> item.var = field;
1325 selector -> item.parent = (oberon_item_t *) expr;
1327 return selector;
1330 #define ISSELECTOR(x) \
1331 (((x) == LBRACK) \
1332 || ((x) == DOT) \
1333 || ((x) == UPARROW) \
1334 || ((x) == LPAREN))
1336 static oberon_object_t *
1337 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1339 char * name;
1340 oberon_object_t * x;
1342 name = oberon_assert_ident(ctx);
1343 x = oberon_find_object(ctx -> decl, name, check);
1345 if(x != NULL)
1347 if(x -> class == OBERON_CLASS_MODULE)
1349 oberon_assert_token(ctx, DOT);
1350 name = oberon_assert_ident(ctx);
1351 /* Наличие объектов в левых модулях всегда проверяется */
1352 x = oberon_find_object(x -> module -> decl, name, 1);
1354 if(x -> export == 0)
1356 oberon_error(ctx, "not exported");
1361 if(xname)
1363 *xname = name;
1366 return x;
1369 static oberon_expr_t *
1370 oberon_ident_item(oberon_context_t * ctx, char * name)
1372 bool read_only;
1373 oberon_object_t * x;
1374 oberon_expr_t * expr;
1376 x = oberon_find_object(ctx -> decl, name, true);
1378 read_only = false;
1379 if(x -> class == OBERON_CLASS_CONST || x -> class == OBERON_CLASS_PROC)
1381 read_only = true;
1384 expr = oberon_new_item(MODE_VAR, x -> type, read_only);
1385 expr -> item.var = x;
1386 return expr;
1389 static oberon_expr_t *
1390 oberon_qualident_expr(oberon_context_t * ctx)
1392 oberon_object_t * var;
1393 oberon_expr_t * expr;
1395 var = oberon_qualident(ctx, NULL, 1);
1397 int read_only = 0;
1398 if(var -> read_only)
1400 if(var -> module != ctx -> mod)
1402 read_only = 1;
1406 switch(var -> class)
1408 case OBERON_CLASS_CONST:
1409 // TODO copy value
1410 expr = (oberon_expr_t *) var -> value;
1411 break;
1412 case OBERON_CLASS_TYPE:
1413 expr = oberon_new_item(MODE_TYPE, var -> type, read_only);
1414 break;
1415 case OBERON_CLASS_VAR:
1416 case OBERON_CLASS_VAR_PARAM:
1417 case OBERON_CLASS_PARAM:
1418 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1419 break;
1420 case OBERON_CLASS_PROC:
1421 expr = oberon_new_item(MODE_VAR, var -> type, true);
1422 break;
1423 default:
1424 oberon_error(ctx, "invalid designator");
1425 break;
1428 expr -> item.var = var;
1430 return expr;
1433 static oberon_expr_t *
1434 oberon_designator(oberon_context_t * ctx)
1436 char * name;
1437 oberon_expr_t * expr;
1438 oberon_object_t * objtype;
1440 expr = oberon_qualident_expr(ctx);
1442 while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
1444 switch(ctx -> token)
1446 case DOT:
1447 oberon_assert_token(ctx, DOT);
1448 name = oberon_assert_ident(ctx);
1449 expr = oberon_make_record_selector(ctx, expr, name);
1450 break;
1451 case LBRACK:
1452 oberon_assert_token(ctx, LBRACK);
1453 int num_indexes = 0;
1454 oberon_expr_t * indexes = NULL;
1455 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1456 oberon_assert_token(ctx, RBRACK);
1458 for(int i = 0; i < num_indexes; i++)
1460 expr = oberon_make_array_selector(ctx, expr, indexes);
1461 indexes = indexes -> next;
1463 break;
1464 case UPARROW:
1465 oberon_assert_token(ctx, UPARROW);
1466 expr = oberno_make_dereferencing(ctx, expr);
1467 break;
1468 case LPAREN:
1469 oberon_assert_token(ctx, LPAREN);
1470 objtype = oberon_qualident(ctx, NULL, true);
1471 oberon_assert_token(ctx, RPAREN);
1472 oberon_check_extension_of(ctx, expr -> result, objtype -> type);
1473 expr = oberon_cast_expr(ctx, expr, objtype -> type);
1474 break;
1475 default:
1476 oberon_error(ctx, "oberon_designator: wat");
1477 break;
1481 return expr;
1484 static oberon_expr_t *
1485 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1487 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1488 if(ctx -> token == LPAREN)
1490 oberon_assert_token(ctx, LPAREN);
1492 int num_args = 0;
1493 oberon_expr_t * arguments = NULL;
1495 if(ISEXPR(ctx -> token))
1497 oberon_expr_list(ctx, &num_args, &arguments, 0);
1500 assert(expr -> is_item == 1);
1501 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1503 oberon_assert_token(ctx, RPAREN);
1506 return expr;
1509 static void
1510 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1512 assert(expr -> is_item);
1514 int num_args = 0;
1515 oberon_expr_t * arguments = NULL;
1517 if(ctx -> token == LPAREN)
1519 oberon_assert_token(ctx, LPAREN);
1521 if(ISEXPR(ctx -> token))
1523 oberon_expr_list(ctx, &num_args, &arguments, 0);
1526 oberon_assert_token(ctx, RPAREN);
1529 /* Вызов происходит даже без скобок */
1530 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1533 static oberon_expr_t *
1534 oberon_element(oberon_context_t * ctx)
1536 oberon_expr_t * e1;
1537 oberon_expr_t * e2;
1539 e1 = oberon_expr(ctx);
1540 oberon_check_src(ctx, e1);
1541 if(e1 -> result -> class != OBERON_TYPE_INTEGER)
1543 oberon_error(ctx, "expected integer");
1546 e2 = NULL;
1547 if(ctx -> token == DOTDOT)
1549 oberon_assert_token(ctx, DOTDOT);
1550 e2 = oberon_expr(ctx);
1551 oberon_check_src(ctx, e2);
1552 if(e2 -> result -> class != OBERON_TYPE_INTEGER)
1554 oberon_error(ctx, "expected integer");
1558 oberon_expr_t * set;
1559 if(e2 == NULL && oberon_is_const(e1))
1561 set = oberon_make_set(ctx, e1 -> item.integer);
1563 else if(e2 != NULL && oberon_is_const(e1) && oberon_is_const(e2))
1565 set = oberon_make_set_range(ctx, e1 -> item.integer, e2 -> item.integer);
1567 else
1569 set = oberon_new_operator(OP_RANGE, ctx -> set_type, e1, e2);
1571 return set;
1574 static oberon_expr_t *
1575 oberon_make_set_union(oberon_context_t * ctx, oberon_expr_t * a, oberon_expr_t * b)
1577 if(oberon_is_const(a) && oberon_is_const(b))
1579 return oberon_make_set(ctx, (a -> item.integer | b -> item.integer));
1581 else
1583 return oberon_new_operator(OP_UNION, ctx -> set_type, a, b);
1584 }
1587 static oberon_expr_t *
1588 oberon_set(oberon_context_t * ctx)
1590 oberon_expr_t * set;
1591 oberon_expr_t * elements;
1592 set = oberon_make_set(ctx, 0);
1594 oberon_assert_token(ctx, LBRACE);
1595 if(ISEXPR(ctx -> token))
1597 elements = oberon_element(ctx);
1598 set = oberon_make_set_union(ctx, set, elements);
1599 while(ctx -> token == COMMA)
1601 oberon_assert_token(ctx, COMMA);
1602 elements = oberon_element(ctx);
1603 set = oberon_make_set_union(ctx, set, elements);
1606 oberon_assert_token(ctx, RBRACE);
1608 return set;
1611 static oberon_expr_t *
1612 oberon_factor(oberon_context_t * ctx)
1614 oberon_expr_t * expr;
1615 oberon_type_t * result;
1617 switch(ctx -> token)
1619 case IDENT:
1620 expr = oberon_designator(ctx);
1621 expr = oberon_opt_func_parens(ctx, expr);
1622 break;
1623 case INTEGER:
1624 expr = oberon_make_integer(ctx, ctx -> integer);
1625 oberon_assert_token(ctx, INTEGER);
1626 break;
1627 case CHAR:
1628 result = ctx -> char_type;
1629 expr = oberon_new_item(MODE_CHAR, result, true);
1630 expr -> item.integer = ctx -> integer;
1631 oberon_assert_token(ctx, CHAR);
1632 break;
1633 case STRING:
1634 result = ctx -> string_type;
1635 expr = oberon_new_item(MODE_STRING, result, true);
1636 expr -> item.string = ctx -> string;
1637 oberon_assert_token(ctx, STRING);
1638 break;
1639 case REAL:
1640 expr = oberon_make_real(ctx, ctx -> real, ctx -> longmode);
1641 oberon_assert_token(ctx, REAL);
1642 break;
1643 case LBRACE:
1644 expr = oberon_set(ctx);
1645 break;
1646 case LPAREN:
1647 oberon_assert_token(ctx, LPAREN);
1648 expr = oberon_expr(ctx);
1649 oberon_assert_token(ctx, RPAREN);
1650 break;
1651 case NOT:
1652 oberon_assert_token(ctx, NOT);
1653 expr = oberon_factor(ctx);
1654 expr = oberon_make_unary_op(ctx, NOT, expr);
1655 break;
1656 case NIL:
1657 oberon_assert_token(ctx, NIL);
1658 expr = oberon_new_item(MODE_NIL, ctx -> nil_type, true);
1659 break;
1660 default:
1661 oberon_error(ctx, "invalid expression");
1664 return expr;
1667 static oberon_expr_t *
1668 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1670 oberon_expr_t * expr;
1671 oberon_type_t * result;
1673 oberon_check_compatible_bin_expr_types(ctx, token, a -> result, b -> result);
1674 oberon_check_src(ctx, a);
1675 if(token != IS)
1677 oberon_check_src(ctx, b);
1680 if(token == IN)
1682 if(oberon_is_const(a) && oberon_is_const(b))
1684 expr = oberon_make_boolean(ctx, (1 << a -> item.integer) & b -> item.integer);
1686 else
1688 expr = oberon_new_operator(OP_IN, ctx -> bool_type, a, b);
1691 else if(token == IS)
1693 oberon_check_type_expr(ctx, b);
1694 expr = oberon_new_operator(OP_IS, ctx -> bool_type, a, b);
1696 else if((token >= EQUAL && token <= GEQ) || token == OR || token == AND)
1698 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1700 if(oberon_is_const(a) && oberon_is_const(b)
1701 && (oberon_is_real_type(result) || oberon_is_integer_type(result)))
1703 if(oberon_is_real_type(result))
1705 double x = a -> item.real;
1706 double y = b -> item.real;
1707 switch(token)
1709 case EQUAL: expr = oberon_make_boolean(ctx, x == y); break;
1710 case NEQ: expr = oberon_make_boolean(ctx, x != y); break;
1711 case LESS: expr = oberon_make_boolean(ctx, x < y); break;
1712 case LEQ: expr = oberon_make_boolean(ctx, x <= y); break;
1713 case GREAT: expr = oberon_make_boolean(ctx, x > y); break;
1714 case GEQ: expr = oberon_make_boolean(ctx, x >= y); break;
1715 case OR: expr = oberon_make_boolean(ctx, x || y); break;
1716 case AND: expr = oberon_make_boolean(ctx, x && y); break;
1717 default: assert(0); break;
1720 else if(oberon_is_integer_type(result))
1722 int64_t x = a -> item.integer;
1723 int64_t y = b -> item.integer;
1724 switch(token)
1726 case EQUAL: expr = oberon_make_boolean(ctx, x == y); break;
1727 case NEQ: expr = oberon_make_boolean(ctx, x != y); break;
1728 case LESS: expr = oberon_make_boolean(ctx, x < y); break;
1729 case LEQ: expr = oberon_make_boolean(ctx, x <= y); break;
1730 case GREAT: expr = oberon_make_boolean(ctx, x > y); break;
1731 case GEQ: expr = oberon_make_boolean(ctx, x >= y); break;
1732 case OR: expr = oberon_make_boolean(ctx, x || y); break;
1733 case AND: expr = oberon_make_boolean(ctx, x && y); break;
1734 default: assert(0); break;
1737 else
1739 assert(0);
1742 else
1744 a = oberon_cast_expr(ctx, a, result);
1745 b = oberon_cast_expr(ctx, b, result);
1746 result = ctx -> bool_type;
1747 switch(token)
1749 case EQUAL: expr = oberon_new_operator(OP_EQ, result, a, b); break;
1750 case NEQ: expr = oberon_new_operator(OP_NEQ, result, a, b); break;
1751 case LESS: expr = oberon_new_operator(OP_LSS, result, a, b); break;
1752 case LEQ: expr = oberon_new_operator(OP_LEQ, result, a, b); break;
1753 case GREAT: expr = oberon_new_operator(OP_GRT, result, a, b); break;
1754 case GEQ: expr = oberon_new_operator(OP_GEQ, result, a, b); break;
1755 case OR: expr = oberon_new_operator(OP_LOGIC_OR, result, a, b); break;
1756 case AND: expr = oberon_new_operator(OP_LOGIC_AND, result, a, b); break;
1757 default: assert(0); break;
1761 else if(token == SLASH)
1763 if(oberon_is_set_type(a -> result) && oberon_is_set_type(b -> result))
1765 if(oberon_is_const(a) && oberon_is_const(b))
1767 int64_t x = a -> item.integer;
1768 int64_t y = b -> item.integer;
1769 expr = oberon_make_set(ctx, x ^ y);
1771 else
1773 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1774 a = oberon_cast_expr(ctx, a, result);
1775 b = oberon_cast_expr(ctx, b, result);
1776 expr = oberon_new_operator(OP_SYM_DIFFERENCE, result, a, b);
1779 else
1781 result = oberon_get_longer_real_type(ctx, a -> result, b -> result);
1782 if(oberon_is_const(a) && oberon_is_const(b))
1784 double x = a -> item.real;
1785 double y = b -> item.real;
1786 expr = oberon_make_real_typed(ctx, x / y, result);
1788 else
1790 a = oberon_cast_expr(ctx, a, result);
1791 b = oberon_cast_expr(ctx, b, result);
1792 expr = oberon_new_operator(OP_DIV, result, a, b);
1796 else
1798 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1800 if(oberon_is_const(a) && oberon_is_const(b))
1802 if(oberon_is_set_type(result))
1804 int64_t x = a -> item.integer;
1805 int64_t y = b -> item.integer;
1806 switch(token)
1808 case PLUS: expr = oberon_make_set(ctx, x | y); break;
1809 case MINUS: expr = oberon_make_set(ctx, x & ~y); break;
1810 case STAR: expr = oberon_make_set(ctx, x & y); break;
1811 default: assert(0); break;
1814 if(oberon_is_real_type(result))
1816 double x = a -> item.real;
1817 double y = b -> item.real;
1818 switch(token)
1820 case PLUS: expr = oberon_make_real_typed(ctx, x + y, result); break;
1821 case MINUS: expr = oberon_make_real_typed(ctx, x - y, result); break;
1822 case STAR: expr = oberon_make_real_typed(ctx, x * y, result); break;
1823 default: assert(0); break;
1826 else if(oberon_is_integer_type(result))
1828 int64_t x = a -> item.integer;
1829 int64_t y = b -> item.integer;
1830 switch(token)
1832 case PLUS: expr = oberon_make_integer(ctx, x + y); break;
1833 case MINUS: expr = oberon_make_integer(ctx, x - y); break;
1834 case STAR: expr = oberon_make_integer(ctx, x * y); break;
1835 case DIV: expr = oberon_make_integer(ctx, x / y); break;
1836 case MOD: expr = oberon_make_integer(ctx, x % y); break;
1837 default: assert(0); break;
1840 else
1842 assert(0);
1845 else
1847 a = oberon_cast_expr(ctx, a, result);
1848 b = oberon_cast_expr(ctx, b, result);
1851 if(oberon_is_set_type(result))
1853 switch(token)
1855 case PLUS:
1856 expr = oberon_new_operator(OP_UNION, result, a, b);
1857 break;
1858 case MINUS:
1859 expr = oberon_new_operator(OP_DIFFERENCE, result, a, b);
1860 break;
1861 case STAR:
1862 expr = oberon_new_operator(OP_INTERSECTION, result, a, b);
1863 break;
1864 default:
1865 assert(0);
1866 break;
1869 else if(oberon_is_number_type(result))
1871 switch(token)
1873 case PLUS:
1874 expr = oberon_new_operator(OP_ADD, result, a, b);
1875 break;
1876 case MINUS:
1877 expr = oberon_new_operator(OP_SUB, result, a, b);
1878 break;
1879 case STAR:
1880 expr = oberon_new_operator(OP_MUL, result, a, b);
1881 break;
1882 default:
1883 assert(0);
1884 break;
1887 else
1889 assert(0);
1894 return expr;
1897 #define ISMULOP(x) \
1898 ((x) >= STAR && (x) <= AND)
1900 static oberon_expr_t *
1901 oberon_term_expr(oberon_context_t * ctx)
1903 oberon_expr_t * expr;
1905 expr = oberon_factor(ctx);
1906 while(ISMULOP(ctx -> token))
1908 int token = ctx -> token;
1909 oberon_read_token(ctx);
1911 oberon_expr_t * inter = oberon_factor(ctx);
1912 expr = oberon_make_bin_op(ctx, token, expr, inter);
1915 return expr;
1918 #define ISADDOP(x) \
1919 ((x) >= PLUS && (x) <= OR)
1921 static oberon_expr_t *
1922 oberon_simple_expr(oberon_context_t * ctx)
1924 oberon_expr_t * expr;
1926 int minus = 0;
1927 if(ctx -> token == PLUS)
1929 minus = 0;
1930 oberon_assert_token(ctx, PLUS);
1932 else if(ctx -> token == MINUS)
1934 minus = 1;
1935 oberon_assert_token(ctx, MINUS);
1938 expr = oberon_term_expr(ctx);
1940 if(minus)
1942 expr = oberon_make_unary_op(ctx, MINUS, expr);
1945 while(ISADDOP(ctx -> token))
1947 int token = ctx -> token;
1948 oberon_read_token(ctx);
1950 oberon_expr_t * inter = oberon_term_expr(ctx);
1951 expr = oberon_make_bin_op(ctx, token, expr, inter);
1954 return expr;
1957 #define ISRELATION(x) \
1958 ((x) >= EQUAL && (x) <= IS)
1960 static oberon_expr_t *
1961 oberon_expr(oberon_context_t * ctx)
1963 oberon_expr_t * expr;
1965 expr = oberon_simple_expr(ctx);
1966 while(ISRELATION(ctx -> token))
1968 int token = ctx -> token;
1969 oberon_read_token(ctx);
1971 oberon_expr_t * inter = oberon_simple_expr(ctx);
1972 expr = oberon_make_bin_op(ctx, token, expr, inter);
1975 return expr;
1978 static bool
1979 oberon_is_const(oberon_expr_t * expr)
1981 if(expr -> is_item == false)
1983 return false;
1986 switch(expr -> item.mode)
1988 case MODE_INTEGER:
1989 case MODE_BOOLEAN:
1990 case MODE_NIL:
1991 case MODE_REAL:
1992 case MODE_CHAR:
1993 case MODE_STRING:
1994 case MODE_SET:
1995 case MODE_TYPE:
1996 return true;
1997 break;
1998 default:
1999 return false;
2000 break;
2003 return false;
2006 static void
2007 oberon_check_const(oberon_context_t * ctx, oberon_expr_t * expr)
2009 if(!oberon_is_const(expr))
2011 oberon_error(ctx, "const expression are required");
2015 static oberon_item_t *
2016 oberon_const_expr(oberon_context_t * ctx)
2018 oberon_expr_t * expr;
2019 expr = oberon_expr(ctx);
2020 oberon_check_const(ctx, expr);
2021 return (oberon_item_t *) expr;
2024 // =======================================================================
2025 // PARSER
2026 // =======================================================================
2028 static void oberon_decl_seq(oberon_context_t * ctx);
2029 static void oberon_statement_seq(oberon_context_t * ctx);
2030 static void oberon_initialize_decl(oberon_context_t * ctx);
2032 static void
2033 oberon_expect_token(oberon_context_t * ctx, int token)
2035 if(ctx -> token != token)
2037 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
2041 static void
2042 oberon_assert_token(oberon_context_t * ctx, int token)
2044 oberon_expect_token(ctx, token);
2045 oberon_read_token(ctx);
2048 static char *
2049 oberon_assert_ident(oberon_context_t * ctx)
2051 oberon_expect_token(ctx, IDENT);
2052 char * ident = ctx -> string;
2053 oberon_read_token(ctx);
2054 return ident;
2057 static void
2058 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
2060 switch(ctx -> token)
2062 case STAR:
2063 oberon_assert_token(ctx, STAR);
2064 *export = 1;
2065 *read_only = 0;
2066 break;
2067 case MINUS:
2068 oberon_assert_token(ctx, MINUS);
2069 *export = 1;
2070 *read_only = 1;
2071 break;
2072 default:
2073 *export = 0;
2074 *read_only = 0;
2075 break;
2079 static oberon_object_t *
2080 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
2082 char * name;
2083 int export;
2084 int read_only;
2085 oberon_object_t * x;
2087 name = oberon_assert_ident(ctx);
2088 oberon_def(ctx, &export, &read_only);
2090 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
2091 return x;
2094 static void
2095 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
2097 *num = 1;
2098 *list = oberon_ident_def(ctx, class, check_upscope);
2099 while(ctx -> token == COMMA)
2101 oberon_assert_token(ctx, COMMA);
2102 oberon_ident_def(ctx, class, check_upscope);
2103 *num += 1;
2107 static void
2108 oberon_var_decl(oberon_context_t * ctx)
2110 int num;
2111 oberon_object_t * list;
2112 oberon_type_t * type;
2113 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2115 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
2116 oberon_assert_token(ctx, COLON);
2117 oberon_type(ctx, &type);
2119 oberon_object_t * var = list;
2120 for(int i = 0; i < num; i++)
2122 var -> type = type;
2123 var = var -> next;
2127 static oberon_object_t *
2128 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
2130 int class = OBERON_CLASS_PARAM;
2131 if(ctx -> token == VAR)
2133 oberon_read_token(ctx);
2134 class = OBERON_CLASS_VAR_PARAM;
2137 int num;
2138 oberon_object_t * list;
2139 oberon_ident_list(ctx, class, false, &num, &list);
2141 oberon_assert_token(ctx, COLON);
2143 oberon_type_t * type;
2144 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2145 oberon_type(ctx, &type);
2147 oberon_object_t * param = list;
2148 for(int i = 0; i < num; i++)
2150 param -> type = type;
2151 param = param -> next;
2154 *num_decl += num;
2155 return list;
2158 #define ISFPSECTION \
2159 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2161 static void
2162 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2164 oberon_assert_token(ctx, LPAREN);
2166 if(ISFPSECTION)
2168 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2169 while(ctx -> token == SEMICOLON)
2171 oberon_assert_token(ctx, SEMICOLON);
2172 oberon_fp_section(ctx, &signature -> num_decl);
2176 oberon_assert_token(ctx, RPAREN);
2178 if(ctx -> token == COLON)
2180 oberon_assert_token(ctx, COLON);
2182 oberon_object_t * typeobj;
2183 typeobj = oberon_qualident(ctx, NULL, 1);
2184 if(typeobj -> class != OBERON_CLASS_TYPE)
2186 oberon_error(ctx, "function result is not type");
2188 if(typeobj -> type -> class == OBERON_TYPE_RECORD
2189 || typeobj -> type -> class == OBERON_TYPE_ARRAY)
2191 oberon_error(ctx, "records or arrays could not be result of function");
2193 signature -> base = typeobj -> type;
2197 static void
2198 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2200 oberon_type_t * signature;
2201 signature = *type;
2202 signature -> class = OBERON_TYPE_PROCEDURE;
2203 signature -> num_decl = 0;
2204 signature -> base = ctx -> notype_type;
2205 signature -> decl = NULL;
2207 if(ctx -> token == LPAREN)
2209 oberon_formal_pars(ctx, signature);
2213 static void
2214 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2216 if(a -> num_decl != b -> num_decl)
2218 oberon_error(ctx, "number parameters not matched");
2221 int num_param = a -> num_decl;
2222 oberon_object_t * param_a = a -> decl;
2223 oberon_object_t * param_b = b -> decl;
2224 for(int i = 0; i < num_param; i++)
2226 if(strcmp(param_a -> name, param_b -> name) != 0)
2228 oberon_error(ctx, "param %i name not matched", i + 1);
2231 if(param_a -> type != param_b -> type)
2233 oberon_error(ctx, "param %i type not matched", i + 1);
2236 param_a = param_a -> next;
2237 param_b = param_b -> next;
2241 static void
2242 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2244 oberon_object_t * proc = ctx -> decl -> parent;
2245 oberon_type_t * result_type = proc -> type -> base;
2247 if(result_type -> class == OBERON_TYPE_NOTYPE)
2249 if(expr != NULL)
2251 oberon_error(ctx, "procedure has no result type");
2254 else
2256 if(expr == NULL)
2258 oberon_error(ctx, "procedure requires expression on result");
2261 oberon_check_src(ctx, expr);
2262 oberon_check_assignment_compatible(ctx, expr, result_type);
2263 expr = oberon_cast_expr(ctx, expr, result_type);
2266 proc -> has_return = 1;
2268 oberon_generate_return(ctx, expr);
2271 static void
2272 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2274 oberon_assert_token(ctx, SEMICOLON);
2276 ctx -> decl = proc -> scope;
2278 oberon_decl_seq(ctx);
2280 oberon_generate_begin_proc(ctx, proc);
2282 if(ctx -> token == BEGIN)
2284 oberon_assert_token(ctx, BEGIN);
2285 oberon_statement_seq(ctx);
2288 oberon_assert_token(ctx, END);
2289 char * name = oberon_assert_ident(ctx);
2290 if(strcmp(name, proc -> name) != 0)
2292 oberon_error(ctx, "procedure name not matched");
2295 if(proc -> type -> base -> class == OBERON_TYPE_NOTYPE
2296 && proc -> has_return == 0)
2298 oberon_make_return(ctx, NULL);
2301 if(proc -> has_return == 0)
2303 oberon_error(ctx, "procedure requires return");
2306 oberon_generate_end_proc(ctx);
2307 oberon_close_scope(ctx -> decl);
2310 static void
2311 oberon_proc_decl(oberon_context_t * ctx)
2313 oberon_assert_token(ctx, PROCEDURE);
2315 int forward = 0;
2316 if(ctx -> token == UPARROW)
2318 oberon_assert_token(ctx, UPARROW);
2319 forward = 1;
2322 char * name;
2323 int export;
2324 int read_only;
2325 name = oberon_assert_ident(ctx);
2326 oberon_def(ctx, &export, &read_only);
2328 oberon_scope_t * proc_scope;
2329 proc_scope = oberon_open_scope(ctx);
2330 ctx -> decl -> local = 1;
2332 oberon_type_t * signature;
2333 signature = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2334 oberon_opt_formal_pars(ctx, &signature);
2336 //oberon_initialize_decl(ctx);
2337 oberon_generator_init_type(ctx, signature);
2338 oberon_close_scope(ctx -> decl);
2340 oberon_object_t * proc;
2341 proc = oberon_find_object(ctx -> decl, name, 0);
2342 if(proc == NULL)
2344 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2345 proc -> type = signature;
2346 proc -> scope = proc_scope;
2347 oberon_generator_init_proc(ctx, proc);
2349 else
2351 if(proc -> class != OBERON_CLASS_PROC)
2353 oberon_error(ctx, "mult definition");
2356 if(forward == 0)
2358 if(proc -> linked)
2360 oberon_error(ctx, "mult procedure definition");
2364 if(proc -> export != export || proc -> read_only != read_only)
2366 oberon_error(ctx, "export type not matched");
2369 oberon_compare_signatures(ctx, proc -> type, signature);
2372 proc_scope -> parent = proc;
2373 oberon_object_t * param = proc_scope -> list -> next;
2374 while(param)
2376 param -> parent = proc;
2377 param = param -> next;
2380 if(forward == 0)
2382 proc -> linked = 1;
2383 oberon_proc_decl_body(ctx, proc);
2387 static void
2388 oberon_const_decl(oberon_context_t * ctx)
2390 oberon_item_t * value;
2391 oberon_object_t * constant;
2393 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2394 oberon_assert_token(ctx, EQUAL);
2395 value = oberon_const_expr(ctx);
2396 constant -> value = value;
2399 static void
2400 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2402 if(size -> is_item == 0)
2404 oberon_error(ctx, "requires constant");
2407 if(size -> item.mode != MODE_INTEGER)
2409 oberon_error(ctx, "requires integer constant");
2412 oberon_type_t * arr;
2413 arr = *type;
2414 arr -> class = OBERON_TYPE_ARRAY;
2415 arr -> size = size -> item.integer;
2416 arr -> base = base;
2419 static void
2420 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2422 char * name;
2423 oberon_object_t * to;
2425 to = oberon_qualident(ctx, &name, 0);
2427 //name = oberon_assert_ident(ctx);
2428 //to = oberon_find_object(ctx -> decl, name, 0);
2430 if(to != NULL)
2432 if(to -> class != OBERON_CLASS_TYPE)
2434 oberon_error(ctx, "not a type");
2437 else
2439 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2440 to -> type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2443 *type = to -> type;
2446 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2448 /*
2449 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2450 */
2452 static void
2453 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2455 if(sizes == NULL)
2457 *type = base;
2458 return;
2461 oberon_type_t * dim;
2462 dim = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2464 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2466 oberon_make_array_type(ctx, sizes, dim, type);
2469 static void
2470 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2472 type -> class = OBERON_TYPE_ARRAY;
2473 type -> size = 0;
2474 type -> base = base;
2477 static void
2478 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2480 if(ctx -> token == IDENT)
2482 int num;
2483 oberon_object_t * list;
2484 oberon_type_t * type;
2485 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2487 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2488 oberon_assert_token(ctx, COLON);
2490 oberon_scope_t * current = ctx -> decl;
2491 ctx -> decl = modscope;
2492 oberon_type(ctx, &type);
2493 ctx -> decl = current;
2495 oberon_object_t * field = list;
2496 for(int i = 0; i < num; i++)
2498 field -> type = type;
2499 field = field -> next;
2502 rec -> num_decl += num;
2506 static void
2507 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2509 oberon_scope_t * modscope = ctx -> mod -> decl;
2510 oberon_scope_t * oldscope = ctx -> decl;
2511 ctx -> decl = modscope;
2513 if(ctx -> token == LPAREN)
2515 oberon_assert_token(ctx, LPAREN);
2517 oberon_object_t * typeobj;
2518 typeobj = oberon_qualident(ctx, NULL, true);
2520 if(typeobj -> class != OBERON_CLASS_TYPE)
2522 oberon_error(ctx, "base must be type");
2525 oberon_type_t * base = typeobj -> type;
2526 if(base -> class == OBERON_TYPE_POINTER)
2528 base = base -> base;
2531 if(base -> class != OBERON_TYPE_RECORD)
2533 oberon_error(ctx, "base must be record type");
2536 rec -> base = base;
2537 ctx -> decl = base -> scope;
2539 oberon_assert_token(ctx, RPAREN);
2541 else
2543 ctx -> decl = NULL;
2546 oberon_scope_t * this_scope;
2547 this_scope = oberon_open_scope(ctx);
2548 this_scope -> local = true;
2549 this_scope -> parent = NULL;
2550 this_scope -> parent_type = rec;
2552 oberon_field_list(ctx, rec, modscope);
2553 while(ctx -> token == SEMICOLON)
2555 oberon_assert_token(ctx, SEMICOLON);
2556 oberon_field_list(ctx, rec, modscope);
2559 rec -> scope = this_scope;
2560 rec -> decl = this_scope -> list -> next;
2561 ctx -> decl = oldscope;
2564 static void
2565 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2567 if(ctx -> token == IDENT)
2569 oberon_qualident_type(ctx, type);
2571 else if(ctx -> token == ARRAY)
2573 oberon_assert_token(ctx, ARRAY);
2575 int num_sizes = 0;
2576 oberon_expr_t * sizes;
2578 if(ISEXPR(ctx -> token))
2580 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2583 oberon_assert_token(ctx, OF);
2585 oberon_type_t * base;
2586 base = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2587 oberon_type(ctx, &base);
2589 if(num_sizes == 0)
2591 oberon_make_open_array(ctx, base, *type);
2593 else
2595 oberon_make_multiarray(ctx, sizes, base, type);
2598 else if(ctx -> token == RECORD)
2600 oberon_type_t * rec;
2601 rec = *type;
2602 rec -> class = OBERON_TYPE_RECORD;
2603 rec -> module = ctx -> mod;
2605 oberon_assert_token(ctx, RECORD);
2606 oberon_type_record_body(ctx, rec);
2607 oberon_assert_token(ctx, END);
2609 *type = rec;
2611 else if(ctx -> token == POINTER)
2613 oberon_assert_token(ctx, POINTER);
2614 oberon_assert_token(ctx, TO);
2616 oberon_type_t * base;
2617 base = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2618 oberon_type(ctx, &base);
2620 oberon_type_t * ptr;
2621 ptr = *type;
2622 ptr -> class = OBERON_TYPE_POINTER;
2623 ptr -> base = base;
2625 else if(ctx -> token == PROCEDURE)
2627 oberon_open_scope(ctx);
2628 oberon_assert_token(ctx, PROCEDURE);
2629 oberon_opt_formal_pars(ctx, type);
2630 oberon_close_scope(ctx -> decl);
2632 else
2634 oberon_error(ctx, "invalid type declaration");
2638 static void
2639 oberon_type_decl(oberon_context_t * ctx)
2641 char * name;
2642 oberon_object_t * newtype;
2643 oberon_type_t * type;
2644 int export;
2645 int read_only;
2647 name = oberon_assert_ident(ctx);
2648 oberon_def(ctx, &export, &read_only);
2650 newtype = oberon_find_object(ctx -> decl, name, 0);
2651 if(newtype == NULL)
2653 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2654 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2655 assert(newtype -> type);
2657 else
2659 if(newtype -> class != OBERON_CLASS_TYPE)
2661 oberon_error(ctx, "mult definition");
2664 if(newtype -> linked)
2666 oberon_error(ctx, "mult definition - already linked");
2669 newtype -> export = export;
2670 newtype -> read_only = read_only;
2673 oberon_assert_token(ctx, EQUAL);
2675 type = newtype -> type;
2676 oberon_type(ctx, &type);
2678 if(type -> class == OBERON_TYPE_NOTYPE)
2680 oberon_error(ctx, "recursive alias declaration");
2683 newtype -> type = type;
2684 newtype -> linked = 1;
2687 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2688 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2690 static void
2691 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2693 if(type -> class != OBERON_TYPE_POINTER
2694 && type -> class != OBERON_TYPE_ARRAY)
2696 return;
2699 if(type -> recursive)
2701 oberon_error(ctx, "recursive pointer declaration");
2704 if(type -> class == OBERON_TYPE_POINTER
2705 && type -> base -> class == OBERON_TYPE_POINTER)
2707 oberon_error(ctx, "attempt to make pointer to pointer");
2710 type -> recursive = 1;
2712 oberon_prevent_recursive_pointer(ctx, type -> base);
2714 type -> recursive = 0;
2717 static void
2718 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2720 if(type -> class != OBERON_TYPE_RECORD)
2722 return;
2725 if(type -> recursive)
2727 oberon_error(ctx, "recursive record declaration");
2730 type -> recursive = 1;
2732 if(type -> base)
2734 oberon_prevent_recursive_record(ctx, type -> base);
2737 int num_fields = type -> num_decl;
2738 oberon_object_t * field = type -> decl;
2739 for(int i = 0; i < num_fields; i++)
2741 oberon_prevent_recursive_object(ctx, field);
2742 field = field -> next;
2745 type -> recursive = 0;
2747 static void
2748 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2750 if(type -> class != OBERON_TYPE_PROCEDURE)
2752 return;
2755 if(type -> recursive)
2757 oberon_error(ctx, "recursive procedure declaration");
2760 type -> recursive = 1;
2762 int num_fields = type -> num_decl;
2763 oberon_object_t * field = type -> decl;
2764 for(int i = 0; i < num_fields; i++)
2766 oberon_prevent_recursive_object(ctx, field);
2767 field = field -> next;
2770 type -> recursive = 0;
2773 static void
2774 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2776 if(type -> class != OBERON_TYPE_ARRAY)
2778 return;
2781 if(type -> recursive)
2783 oberon_error(ctx, "recursive array declaration");
2786 type -> recursive = 1;
2788 oberon_prevent_recursive_type(ctx, type -> base);
2790 type -> recursive = 0;
2793 static void
2794 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2796 if(type -> class == OBERON_TYPE_POINTER)
2798 oberon_prevent_recursive_pointer(ctx, type);
2800 else if(type -> class == OBERON_TYPE_RECORD)
2802 oberon_prevent_recursive_record(ctx, type);
2804 else if(type -> class == OBERON_TYPE_ARRAY)
2806 oberon_prevent_recursive_array(ctx, type);
2808 else if(type -> class == OBERON_TYPE_PROCEDURE)
2810 oberon_prevent_recursive_procedure(ctx, type);
2814 static void
2815 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2817 switch(x -> class)
2819 case OBERON_CLASS_VAR:
2820 case OBERON_CLASS_TYPE:
2821 case OBERON_CLASS_PARAM:
2822 case OBERON_CLASS_VAR_PARAM:
2823 case OBERON_CLASS_FIELD:
2824 oberon_prevent_recursive_type(ctx, x -> type);
2825 break;
2826 case OBERON_CLASS_CONST:
2827 case OBERON_CLASS_PROC:
2828 case OBERON_CLASS_MODULE:
2829 break;
2830 default:
2831 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2832 break;
2836 static void
2837 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2839 oberon_object_t * x = ctx -> decl -> list -> next;
2841 while(x)
2843 oberon_prevent_recursive_object(ctx, x);
2844 x = x -> next;
2848 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2849 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2851 static void
2852 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2854 if(type -> class != OBERON_TYPE_RECORD)
2856 return;
2859 int num_fields = type -> num_decl;
2860 oberon_object_t * field = type -> decl;
2861 for(int i = 0; i < num_fields; i++)
2863 if(field -> type -> class == OBERON_TYPE_POINTER)
2865 oberon_initialize_type(ctx, field -> type);
2868 oberon_initialize_object(ctx, field);
2869 field = field -> next;
2872 oberon_generator_init_record(ctx, type);
2875 static void
2876 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2878 if(type -> class == OBERON_TYPE_NOTYPE)
2880 oberon_error(ctx, "undeclarated type");
2883 if(type -> initialized)
2885 return;
2888 type -> initialized = 1;
2890 if(type -> class == OBERON_TYPE_POINTER)
2892 oberon_initialize_type(ctx, type -> base);
2893 oberon_generator_init_type(ctx, type);
2895 else if(type -> class == OBERON_TYPE_ARRAY)
2897 if(type -> size != 0)
2899 if(type -> base -> class == OBERON_TYPE_ARRAY)
2901 if(type -> base -> size == 0)
2903 oberon_error(ctx, "open array not allowed as array element");
2908 oberon_initialize_type(ctx, type -> base);
2909 oberon_generator_init_type(ctx, type);
2911 else if(type -> class == OBERON_TYPE_RECORD)
2913 oberon_generator_init_type(ctx, type);
2914 oberon_initialize_record_fields(ctx, type);
2916 else if(type -> class == OBERON_TYPE_PROCEDURE)
2918 int num_fields = type -> num_decl;
2919 oberon_object_t * field = type -> decl;
2920 for(int i = 0; i < num_fields; i++)
2922 oberon_initialize_object(ctx, field);
2923 field = field -> next;
2924 }
2926 oberon_generator_init_type(ctx, type);
2928 else
2930 oberon_generator_init_type(ctx, type);
2934 static void
2935 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2937 if(x -> initialized)
2939 return;
2942 x -> initialized = 1;
2944 switch(x -> class)
2946 case OBERON_CLASS_TYPE:
2947 oberon_initialize_type(ctx, x -> type);
2948 break;
2949 case OBERON_CLASS_VAR:
2950 case OBERON_CLASS_FIELD:
2951 if(x -> type -> class == OBERON_TYPE_ARRAY)
2953 if(x -> type -> size == 0)
2955 oberon_error(ctx, "open array not allowed as variable or field");
2958 oberon_initialize_type(ctx, x -> type);
2959 oberon_generator_init_var(ctx, x);
2960 break;
2961 case OBERON_CLASS_PARAM:
2962 case OBERON_CLASS_VAR_PARAM:
2963 oberon_initialize_type(ctx, x -> type);
2964 oberon_generator_init_var(ctx, x);
2965 break;
2966 case OBERON_CLASS_CONST:
2967 case OBERON_CLASS_PROC:
2968 case OBERON_CLASS_MODULE:
2969 break;
2970 default:
2971 oberon_error(ctx, "oberon_initialize_object: wat");
2972 break;
2976 static void
2977 oberon_initialize_decl(oberon_context_t * ctx)
2979 oberon_object_t * x = ctx -> decl -> list;
2981 while(x -> next)
2983 oberon_initialize_object(ctx, x -> next);
2984 x = x -> next;
2985 }
2988 static void
2989 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2991 oberon_object_t * x = ctx -> decl -> list;
2993 while(x -> next)
2995 if(x -> next -> class == OBERON_CLASS_PROC)
2997 if(x -> next -> linked == 0)
2999 oberon_error(ctx, "unresolved forward declaration");
3002 x = x -> next;
3003 }
3006 static void
3007 oberon_decl_seq(oberon_context_t * ctx)
3009 if(ctx -> token == CONST)
3011 oberon_assert_token(ctx, CONST);
3012 while(ctx -> token == IDENT)
3014 oberon_const_decl(ctx);
3015 oberon_assert_token(ctx, SEMICOLON);
3019 if(ctx -> token == TYPE)
3021 oberon_assert_token(ctx, TYPE);
3022 while(ctx -> token == IDENT)
3024 oberon_type_decl(ctx);
3025 oberon_assert_token(ctx, SEMICOLON);
3029 if(ctx -> token == VAR)
3031 oberon_assert_token(ctx, VAR);
3032 while(ctx -> token == IDENT)
3034 oberon_var_decl(ctx);
3035 oberon_assert_token(ctx, SEMICOLON);
3039 oberon_prevent_recursive_decl(ctx);
3040 oberon_initialize_decl(ctx);
3042 while(ctx -> token == PROCEDURE)
3044 oberon_proc_decl(ctx);
3045 oberon_assert_token(ctx, SEMICOLON);
3048 oberon_prevent_undeclarated_procedures(ctx);
3051 static oberon_expr_t *
3052 oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type)
3054 oberon_object_t * x;
3055 oberon_expr_t * expr;
3057 x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false);
3058 x -> local = true;
3059 x -> type = type;
3060 oberon_generator_init_temp_var(ctx, x);
3062 expr = oberon_new_item(MODE_VAR, type, false);
3063 expr -> item.var = x;
3064 return expr;
3067 static void
3068 oberon_statement_seq(oberon_context_t * ctx);
3070 static void
3071 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
3073 oberon_check_dst(ctx, dst);
3074 oberon_check_assignment_compatible(ctx, src, dst -> result);
3076 if(oberon_is_string_type(src -> result))
3078 src -> next = dst;
3079 oberon_make_copy_call(ctx, 2, src);
3081 else
3083 src = oberon_cast_expr(ctx, src, dst -> result);
3084 oberon_generate_assign(ctx, src, dst);
3088 static oberon_expr_t *
3089 oberon_case_labels(oberon_context_t * ctx, oberon_expr_t * val)
3091 oberon_expr_t * e1;
3092 oberon_expr_t * e2;
3093 oberon_expr_t * cond;
3094 oberon_expr_t * cond2;
3096 e1 = (oberon_expr_t *) oberon_const_expr(ctx);
3098 e2 = NULL;
3099 if(ctx -> token == DOTDOT)
3101 oberon_assert_token(ctx, DOTDOT);
3102 e2 = (oberon_expr_t *) oberon_const_expr(ctx);
3105 if(e2 == NULL)
3107 /* val == e1 */
3108 cond = oberon_make_bin_op(ctx, EQUAL, val, e1);
3110 else
3112 /* val >= e1 && val <= e2 */
3113 cond = oberon_make_bin_op(ctx, GEQ, val, e1);
3114 cond2 = oberon_make_bin_op(ctx, LEQ, val, e2);
3115 cond = oberon_make_bin_op(ctx, AND, cond, cond2);
3118 return cond;
3121 static void
3122 oberon_case(oberon_context_t * ctx, oberon_expr_t * val, gen_label_t * end)
3124 oberon_expr_t * cond;
3125 oberon_expr_t * cond2;
3126 gen_label_t * this_end;
3128 if(ISEXPR(ctx -> token))
3130 this_end = oberon_generator_reserve_label(ctx);
3132 cond = oberon_case_labels(ctx, val);
3133 while(ctx -> token == COMMA)
3135 oberon_assert_token(ctx, COMMA);
3136 /* cond || cond2 */
3137 cond2 = oberon_case_labels(ctx, val);
3138 cond = oberon_make_bin_op(ctx, OR, cond, cond2);
3140 oberon_assert_token(ctx, COLON);
3142 oberon_generate_branch(ctx, cond, false, this_end);
3143 oberon_statement_seq(ctx);
3144 oberon_generate_goto(ctx, end);
3146 oberon_generate_label(ctx, this_end);
3150 static void
3151 oberon_case_statement(oberon_context_t * ctx)
3153 oberon_expr_t * val;
3154 oberon_expr_t * expr;
3155 gen_label_t * end;
3157 end = oberon_generator_reserve_label(ctx);
3159 oberon_assert_token(ctx, CASE);
3160 expr = oberon_expr(ctx);
3161 val = oberon_make_temp_var_item(ctx, expr -> result);
3162 oberon_assign(ctx, expr, val);
3163 oberon_assert_token(ctx, OF);
3164 oberon_case(ctx, val, end);
3165 while(ctx -> token == BAR)
3167 oberon_assert_token(ctx, BAR);
3168 oberon_case(ctx, val, end);
3171 if(ctx -> token == ELSE)
3173 oberon_assert_token(ctx, ELSE);
3174 oberon_statement_seq(ctx);
3176 else
3178 oberon_generate_trap(ctx, -1);
3181 oberon_generate_label(ctx, end);
3182 oberon_assert_token(ctx, END);
3185 static void
3186 oberon_with_guard_do(oberon_context_t * ctx, gen_label_t * end)
3188 oberon_expr_t * val;
3189 oberon_expr_t * var;
3190 oberon_expr_t * type;
3191 oberon_expr_t * cond;
3192 oberon_expr_t * cast;
3193 oberon_type_t * old_type;
3194 gen_var_t * old_var;
3195 gen_label_t * this_end;
3197 this_end = oberon_generator_reserve_label(ctx);
3199 var = oberon_qualident_expr(ctx);
3200 oberon_assert_token(ctx, COLON);
3201 type = oberon_qualident_expr(ctx);
3202 cond = oberon_make_bin_op(ctx, IS, var, type);
3204 oberon_assert_token(ctx, DO);
3205 oberon_generate_branch(ctx, cond, false, this_end);
3207 /* Сохраняем ссылку во временной переменной */
3208 val = oberon_make_temp_var_item(ctx, type -> result);
3209 //cast = oberno_make_record_cast(ctx, var, type -> result);
3210 cast = oberon_cast_expr(ctx, var, type -> result);
3211 oberon_assign(ctx, cast, val);
3212 /* Подменяем тип у оригинальной переменной */
3213 old_type = var -> item.var -> type;
3214 var -> item.var -> type = type -> result;
3215 /* Подменяем ссылку на переменную */
3216 old_var = var -> item.var -> gen_var;
3217 var -> item.var -> gen_var = val -> item.var -> gen_var;
3219 oberon_statement_seq(ctx);
3220 oberon_generate_goto(ctx, end);
3221 oberon_generate_label(ctx, this_end);
3223 /* Возвращаем исходное состояние */
3224 var -> item.var -> gen_var = old_var;
3225 var -> item.var -> type = old_type;
3228 static void
3229 oberon_with_statement(oberon_context_t * ctx)
3231 gen_label_t * end;
3232 end = oberon_generator_reserve_label(ctx);
3234 oberon_assert_token(ctx, WITH);
3235 oberon_with_guard_do(ctx, end);
3236 while(ctx -> token == BAR)
3238 oberon_assert_token(ctx, BAR);
3239 oberon_with_guard_do(ctx, end);
3242 if(ctx -> token == ELSE)
3244 oberon_assert_token(ctx, ELSE);
3245 oberon_statement_seq(ctx);
3247 else
3249 oberon_generate_trap(ctx, -2);
3252 oberon_generate_label(ctx, end);
3253 oberon_assert_token(ctx, END);
3256 static void
3257 oberon_statement(oberon_context_t * ctx)
3259 oberon_expr_t * item1;
3260 oberon_expr_t * item2;
3262 if(ctx -> token == IDENT)
3264 item1 = oberon_designator(ctx);
3265 if(ctx -> token == ASSIGN)
3267 oberon_assert_token(ctx, ASSIGN);
3268 item2 = oberon_expr(ctx);
3269 oberon_assign(ctx, item2, item1);
3271 else
3273 oberon_opt_proc_parens(ctx, item1);
3276 else if(ctx -> token == IF)
3278 gen_label_t * end;
3279 gen_label_t * els;
3280 oberon_expr_t * cond;
3282 els = oberon_generator_reserve_label(ctx);
3283 end = oberon_generator_reserve_label(ctx);
3285 oberon_assert_token(ctx, IF);
3286 cond = oberon_expr(ctx);
3287 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3289 oberon_error(ctx, "condition must be boolean");
3291 oberon_assert_token(ctx, THEN);
3292 oberon_generate_branch(ctx, cond, false, els);
3293 oberon_statement_seq(ctx);
3294 oberon_generate_goto(ctx, end);
3295 oberon_generate_label(ctx, els);
3297 while(ctx -> token == ELSIF)
3299 els = oberon_generator_reserve_label(ctx);
3301 oberon_assert_token(ctx, ELSIF);
3302 cond = oberon_expr(ctx);
3303 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3305 oberon_error(ctx, "condition must be boolean");
3307 oberon_assert_token(ctx, THEN);
3308 oberon_generate_branch(ctx, cond, false, els);
3309 oberon_statement_seq(ctx);
3310 oberon_generate_goto(ctx, end);
3311 oberon_generate_label(ctx, els);
3314 if(ctx -> token == ELSE)
3316 oberon_assert_token(ctx, ELSE);
3317 oberon_statement_seq(ctx);
3320 oberon_generate_label(ctx, end);
3321 oberon_assert_token(ctx, END);
3323 else if(ctx -> token == WHILE)
3325 gen_label_t * begin;
3326 gen_label_t * end;
3327 oberon_expr_t * cond;
3329 begin = oberon_generator_reserve_label(ctx);
3330 end = oberon_generator_reserve_label(ctx);
3332 oberon_assert_token(ctx, WHILE);
3333 oberon_generate_label(ctx, begin);
3334 cond = oberon_expr(ctx);
3335 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3337 oberon_error(ctx, "condition must be boolean");
3339 oberon_generate_branch(ctx, cond, false, end);
3341 oberon_assert_token(ctx, DO);
3342 oberon_statement_seq(ctx);
3343 oberon_generate_goto(ctx, begin);
3345 oberon_assert_token(ctx, END);
3346 oberon_generate_label(ctx, end);
3348 else if(ctx -> token == REPEAT)
3350 gen_label_t * begin;
3351 oberon_expr_t * cond;
3353 begin = oberon_generator_reserve_label(ctx);
3354 oberon_generate_label(ctx, begin);
3355 oberon_assert_token(ctx, REPEAT);
3357 oberon_statement_seq(ctx);
3359 oberon_assert_token(ctx, UNTIL);
3361 cond = oberon_expr(ctx);
3362 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3364 oberon_error(ctx, "condition must be boolean");
3367 oberon_generate_branch(ctx, cond, true, begin);
3369 else if(ctx -> token == FOR)
3371 oberon_expr_t * from;
3372 oberon_expr_t * index;
3373 oberon_expr_t * to;
3374 oberon_expr_t * bound;
3375 oberon_expr_t * by;
3376 oberon_expr_t * cond;
3377 oberon_expr_t * count;
3378 gen_label_t * begin;
3379 gen_label_t * end;
3380 char * iname;
3381 int op;
3383 begin = oberon_generator_reserve_label(ctx);
3384 end = oberon_generator_reserve_label(ctx);
3386 oberon_assert_token(ctx, FOR);
3387 iname = oberon_assert_ident(ctx);
3388 index = oberon_ident_item(ctx, iname);
3389 oberon_assert_token(ctx, ASSIGN);
3390 from = oberon_expr(ctx);
3391 oberon_assert_token(ctx, TO);
3392 bound = oberon_make_temp_var_item(ctx, index -> result);
3393 to = oberon_expr(ctx);
3394 oberon_assign(ctx, to, bound); // сначала temp
3395 oberon_assign(ctx, from, index); // потом i
3396 if(ctx -> token == BY)
3398 oberon_assert_token(ctx, BY);
3399 by = (oberon_expr_t *) oberon_const_expr(ctx);
3401 else
3403 by = oberon_make_integer(ctx, 1);
3406 if(by -> result -> class != OBERON_TYPE_INTEGER)
3408 oberon_error(ctx, "must be integer");
3411 if(by -> item.integer > 0)
3413 op = LEQ;
3415 else if(by -> item.integer < 0)
3417 op = GEQ;
3419 else
3421 oberon_error(ctx, "zero step not allowed");
3424 oberon_assert_token(ctx, DO);
3425 oberon_generate_label(ctx, begin);
3426 cond = oberon_make_bin_op(ctx, op, index, bound);
3427 oberon_generate_branch(ctx, cond, false, end);
3428 oberon_statement_seq(ctx);
3429 count = oberon_make_bin_op(ctx, PLUS, index, by);
3430 oberon_assign(ctx, count, index);
3431 oberon_generate_goto(ctx, begin);
3432 oberon_generate_label(ctx, end);
3433 oberon_assert_token(ctx, END);
3435 else if(ctx -> token == LOOP)
3437 gen_label_t * begin;
3438 gen_label_t * end;
3440 begin = oberon_generator_reserve_label(ctx);
3441 end = oberon_generator_reserve_label(ctx);
3443 oberon_open_scope(ctx);
3444 oberon_assert_token(ctx, LOOP);
3445 oberon_generate_label(ctx, begin);
3446 ctx -> decl -> exit_label = end;
3447 oberon_statement_seq(ctx);
3448 oberon_generate_goto(ctx, begin);
3449 oberon_generate_label(ctx, end);
3450 oberon_assert_token(ctx, END);
3451 oberon_close_scope(ctx -> decl);
3453 else if(ctx -> token == EXIT)
3455 oberon_assert_token(ctx, EXIT);
3456 if(ctx -> decl -> exit_label == NULL)
3458 oberon_error(ctx, "not in LOOP-END");
3460 oberon_generate_goto(ctx, ctx -> decl -> exit_label);
3462 else if(ctx -> token == CASE)
3464 oberon_case_statement(ctx);
3466 else if(ctx -> token == WITH)
3468 oberon_with_statement(ctx);
3470 else if(ctx -> token == RETURN)
3472 oberon_assert_token(ctx, RETURN);
3473 if(ISEXPR(ctx -> token))
3475 oberon_expr_t * expr;
3476 expr = oberon_expr(ctx);
3477 oberon_make_return(ctx, expr);
3479 else
3481 oberon_make_return(ctx, NULL);
3486 static void
3487 oberon_statement_seq(oberon_context_t * ctx)
3489 oberon_statement(ctx);
3490 while(ctx -> token == SEMICOLON)
3492 oberon_assert_token(ctx, SEMICOLON);
3493 oberon_statement(ctx);
3497 static void
3498 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
3500 oberon_module_t * m = ctx -> module_list;
3501 while(m && strcmp(m -> name, name) != 0)
3503 m = m -> next;
3506 if(m == NULL)
3508 const char * code;
3509 code = ctx -> import_module(name);
3510 if(code == NULL)
3512 oberon_error(ctx, "no such module");
3515 m = oberon_compile_module(ctx, code);
3516 assert(m);
3519 if(m -> ready == 0)
3521 oberon_error(ctx, "cyclic module import");
3524 oberon_object_t * ident;
3525 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3526 ident -> module = m;
3529 static void
3530 oberon_import_decl(oberon_context_t * ctx)
3532 char * alias;
3533 char * name;
3535 alias = name = oberon_assert_ident(ctx);
3536 if(ctx -> token == ASSIGN)
3538 oberon_assert_token(ctx, ASSIGN);
3539 name = oberon_assert_ident(ctx);
3542 oberon_import_module(ctx, alias, name);
3545 static void
3546 oberon_import_list(oberon_context_t * ctx)
3548 oberon_assert_token(ctx, IMPORT);
3550 oberon_import_decl(ctx);
3551 while(ctx -> token == COMMA)
3553 oberon_assert_token(ctx, COMMA);
3554 oberon_import_decl(ctx);
3557 oberon_assert_token(ctx, SEMICOLON);
3560 static void
3561 oberon_parse_module(oberon_context_t * ctx)
3563 char * name1;
3564 char * name2;
3565 oberon_read_token(ctx);
3567 oberon_assert_token(ctx, MODULE);
3568 name1 = oberon_assert_ident(ctx);
3569 oberon_assert_token(ctx, SEMICOLON);
3570 ctx -> mod -> name = name1;
3572 oberon_generator_init_module(ctx, ctx -> mod);
3574 if(ctx -> token == IMPORT)
3576 oberon_import_list(ctx);
3579 oberon_decl_seq(ctx);
3581 oberon_generate_begin_module(ctx);
3582 if(ctx -> token == BEGIN)
3584 oberon_assert_token(ctx, BEGIN);
3585 oberon_statement_seq(ctx);
3587 oberon_generate_end_module(ctx);
3589 oberon_assert_token(ctx, END);
3590 name2 = oberon_assert_ident(ctx);
3591 oberon_expect_token(ctx, DOT);
3593 if(strcmp(name1, name2) != 0)
3595 oberon_error(ctx, "module name not matched");
3598 oberon_generator_fini_module(ctx -> mod);
3601 // =======================================================================
3602 // LIBRARY
3603 // =======================================================================
3605 static void
3606 register_default_types(oberon_context_t * ctx)
3608 ctx -> notype_type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
3609 oberon_generator_init_type(ctx, ctx -> notype_type);
3611 ctx -> nil_type = oberon_new_type_ptr(OBERON_TYPE_NIL);
3612 oberon_generator_init_type(ctx, ctx -> nil_type);
3614 ctx -> string_type = oberon_new_type_string(1);
3615 oberon_generator_init_type(ctx, ctx -> string_type);
3617 ctx -> bool_type = oberon_new_type_boolean();
3618 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
3620 ctx -> char_type = oberon_new_type_char(1);
3621 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
3623 ctx -> byte_type = oberon_new_type_integer(1);
3624 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> byte_type, 1);
3626 ctx -> shortint_type = oberon_new_type_integer(2);
3627 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> shortint_type, 1);
3629 ctx -> int_type = oberon_new_type_integer(4);
3630 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> int_type, 1);
3632 ctx -> longint_type = oberon_new_type_integer(8);
3633 oberon_define_type(ctx -> world_scope, "HUGEINT", ctx -> longint_type, 1);
3635 ctx -> real_type = oberon_new_type_real(4);
3636 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
3638 ctx -> longreal_type = oberon_new_type_real(8);
3639 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
3641 ctx -> set_type = oberon_new_type_set(4);
3642 oberon_define_type(ctx -> world_scope, "SET", ctx -> set_type, 1);
3645 static void
3646 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
3648 oberon_object_t * proc;
3649 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
3650 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
3651 proc -> type -> sysproc = true;
3652 proc -> type -> genfunc = f;
3653 proc -> type -> genproc = p;
3656 static oberon_expr_t *
3657 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3659 if(num_args < 1)
3661 oberon_error(ctx, "too few arguments");
3664 if(num_args > 1)
3666 oberon_error(ctx, "too mach arguments");
3669 oberon_expr_t * arg;
3670 arg = list_args;
3672 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3674 oberon_error(ctx, "MIN accept only type");
3677 oberon_expr_t * expr;
3678 int bits = arg -> result -> size * 8;
3679 switch(arg -> result -> class)
3681 case OBERON_TYPE_INTEGER:
3682 expr = oberon_make_integer(ctx, -powl(2, bits - 1));
3683 break;
3684 case OBERON_TYPE_SET:
3685 expr = oberon_make_integer(ctx, 0);
3686 break;
3687 default:
3688 oberon_error(ctx, "allowed only basic types");
3689 break;
3692 return expr;
3695 static oberon_expr_t *
3696 oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3698 if(num_args < 1)
3700 oberon_error(ctx, "too few arguments");
3703 if(num_args > 1)
3705 oberon_error(ctx, "too mach arguments");
3708 oberon_expr_t * arg;
3709 arg = list_args;
3711 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3713 oberon_error(ctx, "MAX accept only type");
3716 oberon_expr_t * expr;
3717 int bits = arg -> result -> size * 8;
3718 switch(arg -> result -> class)
3720 case OBERON_TYPE_INTEGER:
3721 expr = oberon_make_integer(ctx, powl(2, bits - 1) - 1);
3722 break;
3723 case OBERON_TYPE_SET:
3724 expr = oberon_make_integer(ctx, bits);
3725 break;
3726 default:
3727 oberon_error(ctx, "allowed only basic types");
3728 break;
3731 return expr;
3734 static oberon_expr_t *
3735 oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3737 if(num_args < 1)
3739 oberon_error(ctx, "too few arguments");
3742 if(num_args > 1)
3744 oberon_error(ctx, "too mach arguments");
3747 oberon_expr_t * arg;
3748 arg = list_args;
3750 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3752 oberon_error(ctx, "SIZE accept only type");
3755 int size;
3756 oberon_expr_t * expr;
3757 oberon_type_t * type = arg -> result;
3758 switch(type -> class)
3760 case OBERON_TYPE_INTEGER:
3761 case OBERON_TYPE_BOOLEAN:
3762 case OBERON_TYPE_REAL:
3763 case OBERON_TYPE_CHAR:
3764 case OBERON_TYPE_SET:
3765 size = type -> size;
3766 break;
3767 default:
3768 oberon_error(ctx, "TODO SIZE");
3769 break;
3772 expr = oberon_make_integer(ctx, size);
3773 return expr;
3776 static oberon_expr_t *
3777 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3779 if(num_args < 1)
3781 oberon_error(ctx, "too few arguments");
3784 if(num_args > 1)
3786 oberon_error(ctx, "too mach arguments");
3789 oberon_expr_t * arg;
3790 arg = list_args;
3791 oberon_check_src(ctx, arg);
3793 oberon_type_t * result_type;
3794 result_type = arg -> result;
3796 if(result_type -> class != OBERON_TYPE_INTEGER)
3798 oberon_error(ctx, "ABS accepts only integers");
3801 oberon_expr_t * expr;
3802 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
3803 return expr;
3806 static void
3807 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3809 if(num_args < 1)
3811 oberon_error(ctx, "too few arguments");
3814 oberon_expr_t * dst;
3815 dst = list_args;
3816 oberon_check_dst(ctx, dst);
3818 oberon_type_t * type;
3819 type = dst -> result;
3821 if(type -> class != OBERON_TYPE_POINTER)
3823 oberon_error(ctx, "not a pointer");
3826 type = type -> base;
3828 oberon_expr_t * src;
3829 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3830 src -> item.num_args = 0;
3831 src -> item.args = NULL;
3833 int max_args = 1;
3834 if(type -> class == OBERON_TYPE_ARRAY)
3836 if(type -> size == 0)
3838 oberon_type_t * x = type;
3839 while(x -> class == OBERON_TYPE_ARRAY)
3841 if(x -> size == 0)
3843 max_args += 1;
3845 x = x -> base;
3849 if(num_args < max_args)
3851 oberon_error(ctx, "too few arguments");
3854 if(num_args > max_args)
3856 oberon_error(ctx, "too mach arguments");
3859 int num_sizes = max_args - 1;
3860 oberon_expr_t * size_list = list_args -> next;
3862 oberon_expr_t * arg = size_list;
3863 for(int i = 0; i < max_args - 1; i++)
3865 oberon_check_src(ctx, arg);
3866 if(arg -> result -> class != OBERON_TYPE_INTEGER)
3868 oberon_error(ctx, "size must be integer");
3870 arg = arg -> next;
3873 src -> item.num_args = num_sizes;
3874 src -> item.args = size_list;
3876 else if(type -> class != OBERON_TYPE_RECORD)
3878 oberon_error(ctx, "oberon_make_new_call: wat");
3881 if(num_args > max_args)
3883 oberon_error(ctx, "too mach arguments");
3886 oberon_assign(ctx, src, dst);
3889 static void
3890 oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3892 if(num_args < 2)
3894 oberon_error(ctx, "too few arguments");
3897 if(num_args > 2)
3899 oberon_error(ctx, "too mach arguments");
3902 oberon_expr_t * src;
3903 src = list_args;
3904 oberon_check_src(ctx, src);
3906 oberon_expr_t * dst;
3907 dst = list_args -> next;
3908 oberon_check_dst(ctx, dst);
3910 if(!oberon_is_string_type(src -> result) && !oberon_is_array_of_char_type(src -> result))
3912 oberon_error(ctx, "source must be string or array of char");
3915 if(!oberon_is_array_of_char_type(dst -> result))
3917 oberon_error(ctx, "dst must be array of char");
3920 oberon_generate_copy(ctx, src, dst);
3923 static void
3924 oberon_make_assert_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3926 if(num_args < 1)
3928 oberon_error(ctx, "too few arguments");
3931 if(num_args > 2)
3933 oberon_error(ctx, "too mach arguments");
3936 oberon_expr_t * cond;
3937 cond = list_args;
3938 oberon_check_src(ctx, cond);
3940 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3942 oberon_error(ctx, "expected boolean");
3945 if(num_args == 1)
3947 oberon_generate_assert(ctx, cond);
3949 else
3951 oberon_expr_t * num;
3952 num = list_args -> next;
3953 oberon_check_src(ctx, num);
3955 if(num -> result -> class != OBERON_TYPE_INTEGER)
3957 oberon_error(ctx, "expected integer");
3960 oberon_check_const(ctx, num);
3962 oberon_generate_assert_n(ctx, cond, num -> item.integer);
3966 static void
3967 oberon_make_halt_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3969 if(num_args < 1)
3971 oberon_error(ctx, "too few arguments");
3974 if(num_args > 1)
3976 oberon_error(ctx, "too mach arguments");
3979 oberon_expr_t * num;
3980 num = list_args;
3981 oberon_check_src(ctx, num);
3983 if(num -> result -> class != OBERON_TYPE_INTEGER)
3985 oberon_error(ctx, "expected integer");
3988 oberon_check_const(ctx, num);
3990 oberon_generate_halt(ctx, num -> item.integer);
3993 static void
3994 oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
3996 oberon_object_t * constant;
3997 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST, true, false, false);
3998 oberon_check_const(ctx, expr);
3999 constant -> value = (oberon_item_t *) expr;
4002 oberon_context_t *
4003 oberon_create_context(ModuleImportCallback import_module)
4005 oberon_context_t * ctx = calloc(1, sizeof *ctx);
4007 oberon_scope_t * world_scope;
4008 world_scope = oberon_open_scope(ctx);
4009 ctx -> world_scope = world_scope;
4011 ctx -> import_module = import_module;
4013 oberon_generator_init_context(ctx);
4015 register_default_types(ctx);
4017 /* Constants */
4018 oberon_new_const(ctx, "TRUE", oberon_make_boolean(ctx, true));
4019 oberon_new_const(ctx, "FALSE", oberon_make_boolean(ctx, false));
4021 /* Functions */
4022 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
4023 oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
4024 oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
4025 oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
4027 /* Procedures */
4028 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
4029 oberon_new_intrinsic(ctx, "COPY", NULL, oberon_make_copy_call);
4030 oberon_new_intrinsic(ctx, "ASSERT", NULL, oberon_make_assert_call);
4031 oberon_new_intrinsic(ctx, "HALT", NULL, oberon_make_halt_call);
4033 return ctx;
4036 void
4037 oberon_destroy_context(oberon_context_t * ctx)
4039 oberon_generator_destroy_context(ctx);
4040 free(ctx);
4043 oberon_module_t *
4044 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
4046 const char * code = ctx -> code;
4047 int code_index = ctx -> code_index;
4048 char c = ctx -> c;
4049 int token = ctx -> token;
4050 char * string = ctx -> string;
4051 int integer = ctx -> integer;
4052 int real = ctx -> real;
4053 bool longmode = ctx -> longmode;
4054 oberon_scope_t * decl = ctx -> decl;
4055 oberon_module_t * mod = ctx -> mod;
4057 oberon_scope_t * module_scope;
4058 module_scope = oberon_open_scope(ctx);
4060 oberon_module_t * module;
4061 module = calloc(1, sizeof *module);
4062 module -> decl = module_scope;
4063 module -> next = ctx -> module_list;
4065 ctx -> mod = module;
4066 ctx -> module_list = module;
4068 oberon_init_scaner(ctx, newcode);
4069 oberon_parse_module(ctx);
4071 module -> ready = 1;
4073 ctx -> code = code;
4074 ctx -> code_index = code_index;
4075 ctx -> c = c;
4076 ctx -> token = token;
4077 ctx -> string = string;
4078 ctx -> integer = integer;
4079 ctx -> real = real;
4080 ctx -> longmode = longmode;
4081 ctx -> decl = decl;
4082 ctx -> mod = mod;
4084 return module;