DEADSOFTWARE

0fff8d7ba4c791846d662f1a6e728c6b2fbfe883
[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 <gc.h>
13 #include "../include/oberon.h"
15 #include "oberon-internals.h"
16 #include "oberon-type-compat.h"
17 #include "oberon-common.h"
18 #include "generator.h"
20 // =======================================================================
21 // UTILS
22 // =======================================================================
24 static void
25 oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args);
27 static oberon_type_t *
28 oberon_new_type_ptr(int class)
29 {
30 oberon_type_t * x = GC_MALLOC(sizeof *x);
31 memset(x, 0, sizeof *x);
32 x -> class = class;
33 return x;
34 }
36 static oberon_type_t *
37 oberon_new_type_integer(int size)
38 {
39 oberon_type_t * x;
40 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
41 x -> size = size;
42 return x;
43 }
45 static oberon_type_t *
46 oberon_new_type_boolean()
47 {
48 oberon_type_t * x;
49 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
50 return x;
51 }
53 static oberon_type_t *
54 oberon_new_type_real(int size)
55 {
56 oberon_type_t * x;
57 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
58 x -> size = size;
59 return x;
60 }
62 static oberon_type_t *
63 oberon_new_type_char(int size)
64 {
65 oberon_type_t * x;
66 x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
67 x -> size = size;
68 return x;
69 }
71 static oberon_type_t *
72 oberon_new_type_string(int size)
73 {
74 oberon_type_t * x;
75 x = oberon_new_type_ptr(OBERON_TYPE_STRING);
76 x -> size = size;
77 return x;
78 }
80 static oberon_type_t *
81 oberon_new_type_set(int size)
82 {
83 oberon_type_t * x;
84 x = oberon_new_type_ptr(OBERON_TYPE_SET);
85 x -> size = size;
86 return x;
87 }
89 static oberon_expr_t *
90 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
91 {
92 oberon_oper_t * operator;
93 operator = GC_MALLOC(sizeof *operator);
94 memset(operator, 0, sizeof *operator);
96 operator -> is_item = 0;
97 operator -> result = result;
98 operator -> read_only = 1;
99 operator -> op = op;
100 operator -> left = left;
101 operator -> right = right;
103 return (oberon_expr_t *) operator;
106 static oberon_expr_t *
107 oberon_new_item(int mode, oberon_type_t * result, int read_only)
109 oberon_item_t * item;
110 item = GC_MALLOC(sizeof *item);
111 memset(item, 0, sizeof *item);
113 item -> is_item = 1;
114 item -> result = result;
115 item -> read_only = read_only;
116 item -> mode = mode;
118 return (oberon_expr_t *)item;
121 static oberon_type_t *
122 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
124 if(i >= -128 && i <= 127)
126 return ctx -> byte_type;
128 else if(i >= -32768 && i <= 32767)
130 return ctx -> shortint_type;
132 else if(i >= -2147483648 && i <= 2147483647)
134 return ctx -> int_type;
136 else
138 return ctx -> longint_type;
142 static oberon_expr_t *
143 oberon_make_integer(oberon_context_t * ctx, int64_t i)
145 oberon_expr_t * expr;
146 oberon_type_t * result;
147 result = oberon_get_type_of_int_value(ctx, i);
148 expr = oberon_new_item(MODE_INTEGER, result, true);
149 expr -> item.integer = i;
150 expr -> item.real = i;
151 return expr;
154 static oberon_expr_t *
155 oberon_make_char(oberon_context_t * ctx, int64_t i)
157 oberon_expr_t * expr;
158 expr = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
159 expr -> item.integer = i;
160 expr -> item.real = i;
161 return expr;
164 static oberon_expr_t *
165 oberon_make_real_typed(oberon_context_t * ctx, double r, oberon_type_t * result)
167 oberon_expr_t * expr;
168 expr = oberon_new_item(MODE_REAL, result, true);
169 expr -> item.integer = r;
170 expr -> item.real = r;
171 return expr;
174 static oberon_expr_t *
175 oberon_make_real(oberon_context_t * ctx, double r, bool longmode)
177 oberon_type_t * result;
178 result = (longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
179 return oberon_make_real_typed(ctx, r, result);
182 static oberon_expr_t *
183 oberon_make_boolean(oberon_context_t * ctx, bool cond)
185 oberon_expr_t * expr;
186 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
187 expr -> item.integer = cond;
188 expr -> item.real = cond;
189 return expr;
192 static oberon_expr_t *
193 oberon_make_set(oberon_context_t * ctx, int64_t i)
195 oberon_expr_t * expr;
196 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
197 expr -> item.integer = i;
198 expr -> item.real = i;
199 return expr;
202 static oberon_expr_t *
203 oberon_make_set_index(oberon_context_t * ctx, int64_t i)
205 oberon_expr_t * expr;
206 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
207 expr -> item.integer = 1 << i;
208 expr -> item.real = 1 << i;
209 return expr;
212 static oberon_expr_t *
213 oberon_make_set_range(oberon_context_t * ctx, int64_t x, int64_t y)
215 oberon_expr_t * expr;
216 expr = oberon_new_item(MODE_SET, ctx -> set_type, true);
217 expr -> item.integer = (x <= y) ? ((2 << y) - (1 << x)) : (0);
218 expr -> item.real = expr -> item.integer;
219 return expr;
222 // =======================================================================
223 // TABLE
224 // =======================================================================
226 static oberon_scope_t *
227 oberon_open_scope(oberon_context_t * ctx)
229 oberon_scope_t * scope = GC_MALLOC(sizeof *scope);
230 memset(scope, 0, sizeof *scope);
232 oberon_object_t * list = GC_MALLOC(sizeof *list);
233 memset(list, 0, sizeof *list);
235 scope -> ctx = ctx;
236 scope -> list = list;
237 scope -> up = ctx -> decl;
239 if(scope -> up)
241 scope -> local = scope -> up -> local;
242 scope -> parent = scope -> up -> parent;
243 scope -> parent_type = scope -> up -> parent_type;
244 scope -> exit_label = scope -> up -> exit_label;
247 ctx -> decl = scope;
248 return scope;
251 static void
252 oberon_close_scope(oberon_scope_t * scope)
254 oberon_context_t * ctx = scope -> ctx;
255 ctx -> decl = scope -> up;
258 static oberon_object_t *
259 oberon_find_object_in_list(oberon_object_t * list, char * name)
261 oberon_object_t * x = list;
262 while(x -> next && strcmp(x -> next -> name, name) != 0)
264 x = x -> next;
266 return x -> next;
269 static oberon_object_t *
270 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
272 oberon_object_t * result = NULL;
274 oberon_scope_t * s = scope;
275 while(result == NULL && s != NULL)
277 result = oberon_find_object_in_list(s -> list, name);
278 s = s -> up;
281 if(check_it && result == NULL)
283 oberon_error(scope -> ctx, "undefined ident %s", name);
286 return result;
289 static oberon_object_t *
290 oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only)
292 oberon_object_t * newvar = GC_MALLOC(sizeof *newvar);
293 memset(newvar, 0, sizeof *newvar);
294 newvar -> name = name;
295 newvar -> class = class;
296 newvar -> export = export;
297 newvar -> read_only = read_only;
298 newvar -> local = scope -> local;
299 newvar -> parent = scope -> parent;
300 newvar -> parent_type = scope -> parent_type;
301 newvar -> module = scope -> ctx -> mod;
302 return newvar;
305 static oberon_object_t *
306 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
308 if(check_upscope)
310 if(oberon_find_object(scope -> up, name, false))
312 oberon_error(scope -> ctx, "already defined");
316 oberon_object_t * x = scope -> list;
317 while(x -> next && strcmp(x -> next -> name, name) != 0)
319 x = x -> next;
322 if(x -> next)
324 oberon_error(scope -> ctx, "already defined");
327 oberon_object_t * newvar;
328 newvar = oberon_create_object(scope, name, class, export, read_only);
329 x -> next = newvar;
331 return newvar;
334 static oberon_object_t *
335 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
337 oberon_object_t * id;
338 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
339 id -> type = type;
340 oberon_generator_init_type(scope -> ctx, type);
341 return id;
344 // =======================================================================
345 // SCANER
346 // =======================================================================
348 static void
349 oberon_get_char(oberon_context_t * ctx)
351 if(ctx -> code[ctx -> code_index])
353 ctx -> code_index += 1;
354 ctx -> c = ctx -> code[ctx -> code_index];
358 static void
359 oberon_init_scaner(oberon_context_t * ctx, const char * code)
361 ctx -> code = code;
362 ctx -> code_index = 0;
363 ctx -> c = ctx -> code[ctx -> code_index];
366 static void
367 oberon_read_ident(oberon_context_t * ctx)
369 int len = 0;
370 int i = ctx -> code_index;
372 int c = ctx -> code[i];
373 while(isalnum(c))
375 i += 1;
376 len += 1;
377 c = ctx -> code[i];
380 char * ident = GC_MALLOC(len + 1);
381 memcpy(ident, &ctx->code[ctx->code_index], len);
382 ident[len] = 0;
384 ctx -> code_index = i;
385 ctx -> c = ctx -> code[i];
386 ctx -> string = ident;
387 ctx -> token = IDENT;
389 if(strcmp(ident, "MODULE") == 0)
391 ctx -> token = MODULE;
393 else if(strcmp(ident, "END") == 0)
395 ctx -> token = END;
397 else if(strcmp(ident, "VAR") == 0)
399 ctx -> token = VAR;
401 else if(strcmp(ident, "BEGIN") == 0)
403 ctx -> token = BEGIN;
405 else if(strcmp(ident, "OR") == 0)
407 ctx -> token = OR;
409 else if(strcmp(ident, "DIV") == 0)
411 ctx -> token = DIV;
413 else if(strcmp(ident, "MOD") == 0)
415 ctx -> token = MOD;
417 else if(strcmp(ident, "PROCEDURE") == 0)
419 ctx -> token = PROCEDURE;
421 else if(strcmp(ident, "RETURN") == 0)
423 ctx -> token = RETURN;
425 else if(strcmp(ident, "CONST") == 0)
427 ctx -> token = CONST;
429 else if(strcmp(ident, "TYPE") == 0)
431 ctx -> token = TYPE;
433 else if(strcmp(ident, "ARRAY") == 0)
435 ctx -> token = ARRAY;
437 else if(strcmp(ident, "OF") == 0)
439 ctx -> token = OF;
441 else if(strcmp(ident, "RECORD") == 0)
443 ctx -> token = RECORD;
445 else if(strcmp(ident, "POINTER") == 0)
447 ctx -> token = POINTER;
449 else if(strcmp(ident, "TO") == 0)
451 ctx -> token = TO;
453 else if(strcmp(ident, "NIL") == 0)
455 ctx -> token = NIL;
457 else if(strcmp(ident, "IMPORT") == 0)
459 ctx -> token = IMPORT;
461 else if(strcmp(ident, "IN") == 0)
463 ctx -> token = IN;
465 else if(strcmp(ident, "IS") == 0)
467 ctx -> token = IS;
469 else if(strcmp(ident, "IF") == 0)
471 ctx -> token = IF;
473 else if(strcmp(ident, "THEN") == 0)
475 ctx -> token = THEN;
477 else if(strcmp(ident, "ELSE") == 0)
479 ctx -> token = ELSE;
481 else if(strcmp(ident, "ELSIF") == 0)
483 ctx -> token = ELSIF;
485 else if(strcmp(ident, "WHILE") == 0)
487 ctx -> token = WHILE;
489 else if(strcmp(ident, "DO") == 0)
491 ctx -> token = DO;
493 else if(strcmp(ident, "REPEAT") == 0)
495 ctx -> token = REPEAT;
497 else if(strcmp(ident, "UNTIL") == 0)
499 ctx -> token = UNTIL;
501 else if(strcmp(ident, "FOR") == 0)
503 ctx -> token = FOR;
505 else if(strcmp(ident, "BY") == 0)
507 ctx -> token = BY;
509 else if(strcmp(ident, "LOOP") == 0)
511 ctx -> token = LOOP;
513 else if(strcmp(ident, "EXIT") == 0)
515 ctx -> token = EXIT;
517 else if(strcmp(ident, "CASE") == 0)
519 ctx -> token = CASE;
521 else if(strcmp(ident, "WITH") == 0)
523 ctx -> token = WITH;
527 #define ISHEXDIGIT(x) \
528 (((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F'))
530 static void
531 oberon_read_number(oberon_context_t * ctx)
533 long integer;
534 double real;
535 char * ident;
536 int start_i;
537 int exp_i;
538 int end_i;
540 /*
541 * mode = 0 == DEC
542 * mode = 1 == HEX
543 * mode = 2 == REAL
544 * mode = 3 == LONGREAL
545 * mode = 4 == CHAR
546 */
547 int mode = 0;
548 start_i = ctx -> code_index;
550 while(isdigit(ctx -> c))
552 oberon_get_char(ctx);
555 end_i = ctx -> code_index;
557 if(ISHEXDIGIT(ctx -> c))
559 mode = 1;
560 while(ISHEXDIGIT(ctx -> c))
562 oberon_get_char(ctx);
565 end_i = ctx -> code_index;
567 if(ctx -> c == 'H')
569 mode = 1;
570 oberon_get_char(ctx);
572 else if(ctx -> c == 'X')
574 mode = 4;
575 oberon_get_char(ctx);
577 else
579 oberon_error(ctx, "invalid hex number");
582 else if(ctx -> c == '.')
584 oberon_get_char(ctx);
585 if(ctx -> c == '.')
587 /* Чит: избегаем конфликта с DOTDOT */
588 ctx -> code_index -= 1;
590 else
592 mode = 2;
594 while(isdigit(ctx -> c))
596 oberon_get_char(ctx);
599 if(ctx -> c == 'E' || ctx -> c == 'D')
601 exp_i = ctx -> code_index;
603 if(ctx -> c == 'D')
605 mode = 3;
608 oberon_get_char(ctx);
610 if(ctx -> c == '+' || ctx -> c == '-')
612 oberon_get_char(ctx);
615 while(isdigit(ctx -> c))
617 oberon_get_char(ctx);
618 }
621 end_i = ctx -> code_index;
624 if(mode == 0)
626 if(ctx -> c == 'H')
628 mode = 1;
629 oberon_get_char(ctx);
631 else if(ctx -> c == 'X')
633 mode = 4;
634 oberon_get_char(ctx);
638 int len = end_i - start_i;
639 ident = GC_MALLOC(len + 1);
640 memcpy(ident, &ctx -> code[start_i], len);
641 ident[len] = 0;
643 ctx -> longmode = false;
644 if(mode == 3)
646 int i = exp_i - start_i;
647 ident[i] = 'E';
648 ctx -> longmode = true;
651 switch(mode)
653 case 0:
654 integer = atol(ident);
655 real = integer;
656 ctx -> token = INTEGER;
657 break;
658 case 1:
659 sscanf(ident, "%lx", &integer);
660 real = integer;
661 ctx -> token = INTEGER;
662 break;
663 case 2:
664 case 3:
665 sscanf(ident, "%lf", &real);
666 integer = real;
667 ctx -> token = REAL;
668 break;
669 case 4:
670 sscanf(ident, "%lx", &integer);
671 real = integer;
672 ctx -> token = CHAR;
673 break;
674 default:
675 oberon_error(ctx, "oberon_read_number: wat");
676 break;
679 ctx -> string = ident;
680 ctx -> integer = integer;
681 ctx -> real = real;
684 static void
685 oberon_skip_space(oberon_context_t * ctx)
687 while(isspace(ctx -> c))
689 oberon_get_char(ctx);
693 static void
694 oberon_read_comment(oberon_context_t * ctx)
696 int nesting = 1;
697 while(nesting >= 1)
699 if(ctx -> c == '(')
701 oberon_get_char(ctx);
702 if(ctx -> c == '*')
704 oberon_get_char(ctx);
705 nesting += 1;
708 else if(ctx -> c == '*')
710 oberon_get_char(ctx);
711 if(ctx -> c == ')')
713 oberon_get_char(ctx);
714 nesting -= 1;
717 else if(ctx -> c == 0)
719 oberon_error(ctx, "unterminated comment");
721 else
723 oberon_get_char(ctx);
728 static void oberon_read_string(oberon_context_t * ctx)
730 int c = ctx -> c;
731 oberon_get_char(ctx);
733 int start = ctx -> code_index;
735 while(ctx -> c != 0 && ctx -> c != c)
737 oberon_get_char(ctx);
740 if(ctx -> c == 0)
742 oberon_error(ctx, "unterminated string");
745 int end = ctx -> code_index;
747 oberon_get_char(ctx);
749 char * string = GC_MALLOC(end - start + 1);
750 strncpy(string, &ctx -> code[start], end - start);
751 string[end] = 0;
753 ctx -> token = STRING;
754 ctx -> string = string;
755 ctx -> integer = string[0];
758 static void oberon_read_token(oberon_context_t * ctx);
760 static void
761 oberon_read_symbol(oberon_context_t * ctx)
763 int c = ctx -> c;
764 switch(c)
766 case 0:
767 ctx -> token = EOF_;
768 break;
769 case ';':
770 ctx -> token = SEMICOLON;
771 oberon_get_char(ctx);
772 break;
773 case ':':
774 ctx -> token = COLON;
775 oberon_get_char(ctx);
776 if(ctx -> c == '=')
778 ctx -> token = ASSIGN;
779 oberon_get_char(ctx);
781 break;
782 case '.':
783 ctx -> token = DOT;
784 oberon_get_char(ctx);
785 if(ctx -> c == '.')
787 ctx -> token = DOTDOT;
788 oberon_get_char(ctx);
790 break;
791 case '(':
792 ctx -> token = LPAREN;
793 oberon_get_char(ctx);
794 if(ctx -> c == '*')
796 oberon_get_char(ctx);
797 oberon_read_comment(ctx);
798 oberon_read_token(ctx);
800 break;
801 case ')':
802 ctx -> token = RPAREN;
803 oberon_get_char(ctx);
804 break;
805 case '=':
806 ctx -> token = EQUAL;
807 oberon_get_char(ctx);
808 break;
809 case '#':
810 ctx -> token = NEQ;
811 oberon_get_char(ctx);
812 break;
813 case '<':
814 ctx -> token = LESS;
815 oberon_get_char(ctx);
816 if(ctx -> c == '=')
818 ctx -> token = LEQ;
819 oberon_get_char(ctx);
821 break;
822 case '>':
823 ctx -> token = GREAT;
824 oberon_get_char(ctx);
825 if(ctx -> c == '=')
827 ctx -> token = GEQ;
828 oberon_get_char(ctx);
830 break;
831 case '+':
832 ctx -> token = PLUS;
833 oberon_get_char(ctx);
834 break;
835 case '-':
836 ctx -> token = MINUS;
837 oberon_get_char(ctx);
838 break;
839 case '*':
840 ctx -> token = STAR;
841 oberon_get_char(ctx);
842 if(ctx -> c == ')')
844 oberon_get_char(ctx);
845 oberon_error(ctx, "unstarted comment");
847 break;
848 case '/':
849 ctx -> token = SLASH;
850 oberon_get_char(ctx);
851 break;
852 case '&':
853 ctx -> token = AND;
854 oberon_get_char(ctx);
855 break;
856 case '~':
857 ctx -> token = NOT;
858 oberon_get_char(ctx);
859 break;
860 case ',':
861 ctx -> token = COMMA;
862 oberon_get_char(ctx);
863 break;
864 case '[':
865 ctx -> token = LBRACK;
866 oberon_get_char(ctx);
867 break;
868 case ']':
869 ctx -> token = RBRACK;
870 oberon_get_char(ctx);
871 break;
872 case '^':
873 ctx -> token = UPARROW;
874 oberon_get_char(ctx);
875 break;
876 case '"':
877 oberon_read_string(ctx);
878 break;
879 case '\'':
880 oberon_read_string(ctx);
881 break;
882 case '{':
883 ctx -> token = LBRACE;
884 oberon_get_char(ctx);
885 break;
886 case '}':
887 ctx -> token = RBRACE;
888 oberon_get_char(ctx);
889 break;
890 case '|':
891 ctx -> token = BAR;
892 oberon_get_char(ctx);
893 break;
894 default:
895 oberon_error(ctx, "invalid char %c", ctx -> c);
896 break;
900 static void
901 oberon_read_token(oberon_context_t * ctx)
903 oberon_skip_space(ctx);
905 int c = ctx -> c;
906 if(isalpha(c))
908 oberon_read_ident(ctx);
910 else if(isdigit(c))
912 oberon_read_number(ctx);
914 else
916 oberon_read_symbol(ctx);
920 // =======================================================================
921 // EXPRESSION
922 // =======================================================================
924 static void oberon_expect_token(oberon_context_t * ctx, int token);
925 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
926 static void oberon_assert_token(oberon_context_t * ctx, int token);
927 static char * oberon_assert_ident(oberon_context_t * ctx);
928 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
929 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
930 static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
931 static bool oberon_is_const(oberon_expr_t * expr);
933 static oberon_expr_t *
934 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
936 oberon_expr_t * expr;
937 oberon_type_t * result;
939 result = a -> result;
941 if(token == MINUS)
943 if(result -> class == OBERON_TYPE_SET)
945 if(oberon_is_const(a))
947 expr = oberon_make_set(ctx, ~(a -> item.integer));
949 else
951 expr = oberon_new_operator(OP_COMPLEMENTATION, result, a, NULL);
954 else if(result -> class == OBERON_TYPE_INTEGER)
956 if(oberon_is_const(a))
958 expr = oberon_make_integer(ctx, -(a -> item.integer));
960 else
962 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
965 else if(result -> class == OBERON_TYPE_REAL)
967 if(oberon_is_const(a))
969 expr = oberon_make_real_typed(ctx, -(a -> item.real), result);
971 else
973 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
976 else
978 oberon_error(ctx, "incompatible operator type");
981 else if(token == NOT)
983 if(result -> class != OBERON_TYPE_BOOLEAN)
985 oberon_error(ctx, "incompatible operator type");
988 if(oberon_is_const(a))
990 expr = oberon_make_boolean(ctx, !(a -> item.integer));
992 else
994 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
997 else
999 oberon_error(ctx, "oberon_make_unary_op: wat");
1002 return expr;
1005 static void
1006 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
1008 oberon_expr_t * last;
1010 *num_expr = 1;
1011 if(const_expr)
1013 *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
1015 else
1017 *first = last = oberon_expr(ctx);
1019 while(ctx -> token == COMMA)
1021 oberon_assert_token(ctx, COMMA);
1022 oberon_expr_t * current;
1024 if(const_expr)
1026 current = (oberon_expr_t *) oberon_const_expr(ctx);
1028 else
1030 current = oberon_expr(ctx);
1033 last -> next = current;
1034 last = current;
1035 *num_expr += 1;
1039 static oberon_expr_t *
1040 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
1042 oberon_expr_t * cast;
1044 if((oberon_is_char_type(pref) && oberon_is_const_string(expr) && strlen(expr -> item.string) == 1))
1046 /* Автоматически преобразуем строку единичного размера в символ */
1047 cast = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
1048 cast -> item.integer = expr -> item.string[0];
1050 else
1052 cast = oberon_new_operator(OP_CAST, pref, expr, NULL);
1055 return cast;
1058 static void
1059 oberon_check_dst(oberon_context_t * ctx, oberon_expr_t * dst)
1061 if(dst -> read_only)
1063 oberon_error(ctx, "read-only destination");
1066 if(dst -> is_item == false)
1068 oberon_error(ctx, "not variable");
1071 switch(dst -> item.mode)
1073 case MODE_VAR:
1074 case MODE_CALL:
1075 case MODE_INDEX:
1076 case MODE_FIELD:
1077 case MODE_DEREF:
1078 case MODE_NEW:
1079 /* accept */
1080 break;
1081 default:
1082 oberon_error(ctx, "not variable");
1083 break;
1087 static void
1088 oberon_check_src(oberon_context_t * ctx, oberon_expr_t * src)
1090 if(src -> is_item)
1092 if(src -> item.mode == MODE_TYPE)
1094 oberon_error(ctx, "not variable");
1099 static void
1100 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
1102 if(desig -> mode != MODE_CALL)
1104 oberon_error(ctx, "expected mode CALL");
1107 oberon_type_t * fn = desig -> parent -> result;
1108 int num_args = desig -> num_args;
1109 int num_decl = fn -> num_decl;
1111 if(num_args < num_decl)
1113 oberon_error(ctx, "too few arguments");
1115 else if(num_args > num_decl)
1117 oberon_error(ctx, "too many arguments");
1120 /* Делаем проверку на запись и делаем автокаст */
1121 oberon_expr_t * casted[num_args];
1122 oberon_expr_t * arg = desig -> args;
1123 oberon_object_t * param = fn -> decl;
1124 for(int i = 0; i < num_args; i++)
1126 if(param -> class == OBERON_CLASS_VAR_PARAM)
1128 oberon_check_dst(ctx, arg);
1129 if(!oberon_is_compatible_arrays(param, arg))
1131 oberon_check_compatible_var_param(ctx, param -> type, arg -> result);
1133 casted[i] = oberon_cast_expr(ctx, arg, param -> type);
1135 else
1137 oberon_check_src(ctx, arg);
1138 if(!oberon_is_compatible_arrays(param, arg))
1140 oberon_check_assignment_compatible(ctx, arg, param -> type);
1142 casted[i] = oberon_cast_expr(ctx, arg, param -> type);
1145 arg = arg -> next;
1146 param = param -> next;
1149 /* Создаём новый список выражений */
1150 if(num_args > 0)
1152 arg = casted[0];
1153 for(int i = 0; i < num_args - 1; i++)
1155 casted[i] -> next = casted[i + 1];
1157 desig -> args = arg;
1161 static oberon_expr_t *
1162 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1164 oberon_type_t * signature = item -> result;
1165 if(signature -> class != OBERON_TYPE_PROCEDURE)
1167 oberon_error(ctx, "not a procedure");
1170 oberon_expr_t * call;
1172 if(signature -> sysproc)
1174 if(signature -> genfunc == NULL)
1176 oberon_error(ctx, "not a function-procedure");
1179 call = signature -> genfunc(ctx, num_args, list_args);
1181 else
1183 if(signature -> base -> class == OBERON_TYPE_NOTYPE)
1185 oberon_error(ctx, "attempt to call procedure in expression");
1188 call = oberon_new_item(MODE_CALL, signature -> base, true);
1189 call -> item.parent = item;
1190 call -> item.num_args = num_args;
1191 call -> item.args = list_args;
1192 oberon_autocast_call(ctx, (oberon_item_t *) call);
1195 return call;
1198 static void
1199 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1201 oberon_type_t * signature = item -> result;
1202 if(signature -> class != OBERON_TYPE_PROCEDURE)
1204 oberon_error(ctx, "not a procedure");
1207 oberon_expr_t * call;
1209 if(signature -> sysproc)
1211 if(signature -> genproc == NULL)
1213 oberon_error(ctx, "not a procedure");
1216 signature -> genproc(ctx, num_args, list_args);
1218 else
1220 if(signature -> base -> class != OBERON_TYPE_NOTYPE)
1222 oberon_error(ctx, "attempt to call function as non-typed procedure");
1225 call = oberon_new_item(MODE_CALL, signature -> base, true);
1226 call -> item.parent = item;
1227 call -> item.num_args = num_args;
1228 call -> item.args = list_args;
1229 oberon_autocast_call(ctx, (oberon_item_t *) call);
1230 oberon_generate_call_proc(ctx, call);
1234 #define ISEXPR(x) \
1235 (((x) == PLUS) \
1236 || ((x) == MINUS) \
1237 || ((x) == IDENT) \
1238 || ((x) == INTEGER) \
1239 || ((x) == REAL) \
1240 || ((x) == CHAR) \
1241 || ((x) == STRING) \
1242 || ((x) == NIL) \
1243 || ((x) == LPAREN) \
1244 || ((x) == NOT))
1246 static oberon_expr_t *
1247 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1249 if(expr -> result -> class != OBERON_TYPE_POINTER)
1251 oberon_error(ctx, "not a pointer");
1254 assert(expr -> is_item);
1256 oberon_expr_t * selector;
1257 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, false);
1258 selector -> item.parent = (oberon_item_t *) expr;
1260 return selector;
1263 static oberon_expr_t *
1264 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1266 if(desig -> result -> class == OBERON_TYPE_POINTER)
1268 desig = oberno_make_dereferencing(ctx, desig);
1271 assert(desig -> is_item);
1273 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1275 oberon_error(ctx, "not array");
1278 oberon_type_t * base;
1279 base = desig -> result -> base;
1281 if(index -> result -> class != OBERON_TYPE_INTEGER)
1283 oberon_error(ctx, "index must be integer");
1286 // Статическая проверка границ массива
1287 if(desig -> result -> size != 0)
1289 if(index -> is_item)
1291 if(index -> item.mode == MODE_INTEGER)
1293 int arr_size = desig -> result -> size;
1294 int index_int = index -> item.integer;
1295 if(index_int < 0 || index_int > arr_size - 1)
1297 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1303 oberon_expr_t * selector;
1304 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1305 selector -> item.parent = (oberon_item_t *) desig;
1306 selector -> item.num_args = 1;
1307 selector -> item.args = index;
1309 return selector;
1312 static oberon_expr_t *
1313 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1315 if(expr -> result -> class == OBERON_TYPE_POINTER)
1317 expr = oberno_make_dereferencing(ctx, expr);
1320 assert(expr -> is_item);
1322 if(expr -> result -> class != OBERON_TYPE_RECORD)
1324 oberon_error(ctx, "not record");
1327 oberon_type_t * rec = expr -> result;
1329 oberon_object_t * field;
1330 field = oberon_find_object(rec -> scope, name, true);
1332 if(field -> export == 0)
1334 if(field -> module != ctx -> mod)
1336 oberon_error(ctx, "field not exported");
1340 int read_only = expr -> read_only;
1341 if(field -> read_only)
1343 if(field -> module != ctx -> mod)
1345 read_only = 1;
1349 oberon_expr_t * selector;
1350 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1351 selector -> item.var = field;
1352 selector -> item.parent = (oberon_item_t *) expr;
1354 return selector;
1357 #define ISSELECTOR(x) \
1358 (((x) == LBRACK) \
1359 || ((x) == DOT) \
1360 || ((x) == UPARROW) \
1361 || ((x) == LPAREN))
1363 static oberon_object_t *
1364 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1366 char * name;
1367 oberon_object_t * x;
1369 name = oberon_assert_ident(ctx);
1370 x = oberon_find_object(ctx -> decl, name, check);
1372 if(x != NULL)
1374 if(x -> class == OBERON_CLASS_MODULE)
1376 oberon_assert_token(ctx, DOT);
1377 name = oberon_assert_ident(ctx);
1378 /* Наличие объектов в левых модулях всегда проверяется */
1379 x = oberon_find_object(x -> module -> decl, name, 1);
1381 if(x -> export == 0)
1383 oberon_error(ctx, "not exported");
1388 if(xname)
1390 *xname = name;
1393 return x;
1396 static oberon_expr_t *
1397 oberon_ident_item(oberon_context_t * ctx, char * name)
1399 bool read_only;
1400 oberon_object_t * x;
1401 oberon_expr_t * expr;
1403 x = oberon_find_object(ctx -> decl, name, true);
1405 read_only = false;
1406 if(x -> class == OBERON_CLASS_CONST || x -> class == OBERON_CLASS_PROC)
1408 read_only = true;
1411 expr = oberon_new_item(MODE_VAR, x -> type, read_only);
1412 expr -> item.var = x;
1413 return expr;
1416 static oberon_expr_t *
1417 oberon_qualident_expr(oberon_context_t * ctx)
1419 oberon_object_t * var;
1420 oberon_expr_t * expr;
1422 var = oberon_qualident(ctx, NULL, 1);
1424 int read_only = 0;
1425 if(var -> read_only)
1427 if(var -> module != ctx -> mod)
1429 read_only = 1;
1433 switch(var -> class)
1435 case OBERON_CLASS_CONST:
1436 // TODO copy value
1437 expr = (oberon_expr_t *) var -> value;
1438 break;
1439 case OBERON_CLASS_TYPE:
1440 expr = oberon_new_item(MODE_TYPE, var -> type, read_only);
1441 break;
1442 case OBERON_CLASS_VAR:
1443 case OBERON_CLASS_VAR_PARAM:
1444 case OBERON_CLASS_PARAM:
1445 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1446 break;
1447 case OBERON_CLASS_PROC:
1448 expr = oberon_new_item(MODE_VAR, var -> type, true);
1449 break;
1450 default:
1451 oberon_error(ctx, "invalid designator");
1452 break;
1455 expr -> item.var = var;
1457 return expr;
1460 static oberon_expr_t *
1461 oberon_designator(oberon_context_t * ctx)
1463 char * name;
1464 oberon_expr_t * expr;
1465 oberon_object_t * objtype;
1467 expr = oberon_qualident_expr(ctx);
1469 while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
1471 switch(ctx -> token)
1473 case DOT:
1474 oberon_assert_token(ctx, DOT);
1475 name = oberon_assert_ident(ctx);
1476 expr = oberon_make_record_selector(ctx, expr, name);
1477 break;
1478 case LBRACK:
1479 oberon_assert_token(ctx, LBRACK);
1480 int num_indexes = 0;
1481 oberon_expr_t * indexes = NULL;
1482 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1483 oberon_assert_token(ctx, RBRACK);
1485 for(int i = 0; i < num_indexes; i++)
1487 expr = oberon_make_array_selector(ctx, expr, indexes);
1488 indexes = indexes -> next;
1490 break;
1491 case UPARROW:
1492 oberon_assert_token(ctx, UPARROW);
1493 expr = oberno_make_dereferencing(ctx, expr);
1494 break;
1495 case LPAREN:
1496 oberon_assert_token(ctx, LPAREN);
1497 objtype = oberon_qualident(ctx, NULL, true);
1498 oberon_assert_token(ctx, RPAREN);
1499 oberon_check_extension_of(ctx, expr -> result, objtype -> type);
1500 expr = oberon_cast_expr(ctx, expr, objtype -> type);
1501 break;
1502 default:
1503 oberon_error(ctx, "oberon_designator: wat");
1504 break;
1508 return expr;
1511 static oberon_expr_t *
1512 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1514 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1515 if(ctx -> token == LPAREN)
1517 oberon_assert_token(ctx, LPAREN);
1519 int num_args = 0;
1520 oberon_expr_t * arguments = NULL;
1522 if(ISEXPR(ctx -> token))
1524 oberon_expr_list(ctx, &num_args, &arguments, 0);
1527 assert(expr -> is_item == 1);
1528 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1530 oberon_assert_token(ctx, RPAREN);
1533 return expr;
1536 static void
1537 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1539 assert(expr -> is_item);
1541 int num_args = 0;
1542 oberon_expr_t * arguments = NULL;
1544 if(ctx -> token == LPAREN)
1546 oberon_assert_token(ctx, LPAREN);
1548 if(ISEXPR(ctx -> token))
1550 oberon_expr_list(ctx, &num_args, &arguments, 0);
1553 oberon_assert_token(ctx, RPAREN);
1556 /* Вызов происходит даже без скобок */
1557 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1560 static oberon_expr_t *
1561 oberon_element(oberon_context_t * ctx)
1563 oberon_expr_t * e1;
1564 oberon_expr_t * e2;
1566 e1 = oberon_expr(ctx);
1567 oberon_check_src(ctx, e1);
1568 if(e1 -> result -> class != OBERON_TYPE_INTEGER)
1570 oberon_error(ctx, "expected integer");
1573 e2 = NULL;
1574 if(ctx -> token == DOTDOT)
1576 oberon_assert_token(ctx, DOTDOT);
1577 e2 = oberon_expr(ctx);
1578 oberon_check_src(ctx, e2);
1579 if(e2 -> result -> class != OBERON_TYPE_INTEGER)
1581 oberon_error(ctx, "expected integer");
1585 oberon_expr_t * set;
1586 if(e2 == NULL && oberon_is_const(e1))
1588 set = oberon_make_set_index(ctx, e1 -> item.integer);
1590 else if(e2 != NULL && oberon_is_const(e1) && oberon_is_const(e2))
1592 set = oberon_make_set_range(ctx, e1 -> item.integer, e2 -> item.integer);
1594 else
1596 set = oberon_new_operator(OP_RANGE, ctx -> set_type, e1, e2);
1598 return set;
1601 static oberon_expr_t *
1602 oberon_make_set_union(oberon_context_t * ctx, oberon_expr_t * a, oberon_expr_t * b)
1604 if(oberon_is_const(a) && oberon_is_const(b))
1606 return oberon_make_set(ctx, (a -> item.integer | b -> item.integer));
1608 else
1610 return oberon_new_operator(OP_UNION, ctx -> set_type, a, b);
1611 }
1614 static oberon_expr_t *
1615 oberon_set(oberon_context_t * ctx)
1617 oberon_expr_t * set;
1618 oberon_expr_t * elements;
1619 set = oberon_make_set(ctx, 0);
1621 oberon_assert_token(ctx, LBRACE);
1622 if(ISEXPR(ctx -> token))
1624 elements = oberon_element(ctx);
1625 set = oberon_make_set_union(ctx, set, elements);
1626 while(ctx -> token == COMMA)
1628 oberon_assert_token(ctx, COMMA);
1629 elements = oberon_element(ctx);
1630 set = oberon_make_set_union(ctx, set, elements);
1633 oberon_assert_token(ctx, RBRACE);
1635 return set;
1638 static oberon_expr_t *
1639 oberon_factor(oberon_context_t * ctx)
1641 oberon_expr_t * expr;
1642 oberon_type_t * result;
1644 switch(ctx -> token)
1646 case IDENT:
1647 expr = oberon_designator(ctx);
1648 expr = oberon_opt_func_parens(ctx, expr);
1649 break;
1650 case INTEGER:
1651 expr = oberon_make_integer(ctx, ctx -> integer);
1652 oberon_assert_token(ctx, INTEGER);
1653 break;
1654 case CHAR:
1655 result = ctx -> char_type;
1656 expr = oberon_new_item(MODE_CHAR, result, true);
1657 expr -> item.integer = ctx -> integer;
1658 oberon_assert_token(ctx, CHAR);
1659 break;
1660 case STRING:
1661 result = ctx -> string_type;
1662 expr = oberon_new_item(MODE_STRING, result, true);
1663 expr -> item.string = ctx -> string;
1664 oberon_assert_token(ctx, STRING);
1665 break;
1666 case REAL:
1667 expr = oberon_make_real(ctx, ctx -> real, ctx -> longmode);
1668 oberon_assert_token(ctx, REAL);
1669 break;
1670 case LBRACE:
1671 expr = oberon_set(ctx);
1672 break;
1673 case LPAREN:
1674 oberon_assert_token(ctx, LPAREN);
1675 expr = oberon_expr(ctx);
1676 oberon_assert_token(ctx, RPAREN);
1677 break;
1678 case NOT:
1679 oberon_assert_token(ctx, NOT);
1680 expr = oberon_factor(ctx);
1681 expr = oberon_make_unary_op(ctx, NOT, expr);
1682 break;
1683 case NIL:
1684 oberon_assert_token(ctx, NIL);
1685 expr = oberon_new_item(MODE_NIL, ctx -> nil_type, true);
1686 break;
1687 default:
1688 oberon_error(ctx, "invalid expression");
1691 return expr;
1694 static oberon_expr_t *
1695 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1697 oberon_expr_t * expr;
1698 oberon_type_t * result;
1700 oberon_check_compatible_bin_expr_types(ctx, token, a -> result, b -> result);
1701 oberon_check_src(ctx, a);
1702 if(token != IS)
1704 oberon_check_src(ctx, b);
1707 if(token == IN)
1709 if(oberon_is_const(a) && oberon_is_const(b))
1711 expr = oberon_make_boolean(ctx, (1 << a -> item.integer) & b -> item.integer);
1713 else
1715 expr = oberon_new_operator(OP_IN, ctx -> bool_type, a, b);
1718 else if(token == IS)
1720 oberon_check_type_expr(ctx, b);
1721 expr = oberon_new_operator(OP_IS, ctx -> bool_type, a, b);
1723 else if((token >= EQUAL && token <= GEQ) || token == OR || token == AND)
1725 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1727 if(oberon_is_const(a) && oberon_is_const(b)
1728 && (oberon_is_real_type(result) || oberon_is_integer_type(result)))
1730 if(oberon_is_real_type(result))
1732 double x = a -> item.real;
1733 double y = b -> item.real;
1734 switch(token)
1736 case EQUAL: expr = oberon_make_boolean(ctx, x == y); break;
1737 case NEQ: expr = oberon_make_boolean(ctx, x != y); break;
1738 case LESS: expr = oberon_make_boolean(ctx, x < y); break;
1739 case LEQ: expr = oberon_make_boolean(ctx, x <= y); break;
1740 case GREAT: expr = oberon_make_boolean(ctx, x > y); break;
1741 case GEQ: expr = oberon_make_boolean(ctx, x >= y); break;
1742 case OR: expr = oberon_make_boolean(ctx, x || y); break;
1743 case AND: expr = oberon_make_boolean(ctx, x && y); break;
1744 default: assert(0); break;
1747 else if(oberon_is_integer_type(result))
1749 int64_t x = a -> item.integer;
1750 int64_t y = b -> item.integer;
1751 switch(token)
1753 case EQUAL: expr = oberon_make_boolean(ctx, x == y); break;
1754 case NEQ: expr = oberon_make_boolean(ctx, x != y); break;
1755 case LESS: expr = oberon_make_boolean(ctx, x < y); break;
1756 case LEQ: expr = oberon_make_boolean(ctx, x <= y); break;
1757 case GREAT: expr = oberon_make_boolean(ctx, x > y); break;
1758 case GEQ: expr = oberon_make_boolean(ctx, x >= y); break;
1759 case OR: expr = oberon_make_boolean(ctx, x || y); break;
1760 case AND: expr = oberon_make_boolean(ctx, x && y); break;
1761 default: assert(0); break;
1764 else
1766 assert(0);
1769 else
1771 a = oberon_cast_expr(ctx, a, result);
1772 b = oberon_cast_expr(ctx, b, result);
1773 result = ctx -> bool_type;
1774 switch(token)
1776 case EQUAL: expr = oberon_new_operator(OP_EQ, result, a, b); break;
1777 case NEQ: expr = oberon_new_operator(OP_NEQ, result, a, b); break;
1778 case LESS: expr = oberon_new_operator(OP_LSS, result, a, b); break;
1779 case LEQ: expr = oberon_new_operator(OP_LEQ, result, a, b); break;
1780 case GREAT: expr = oberon_new_operator(OP_GRT, result, a, b); break;
1781 case GEQ: expr = oberon_new_operator(OP_GEQ, result, a, b); break;
1782 case OR: expr = oberon_new_operator(OP_LOGIC_OR, result, a, b); break;
1783 case AND: expr = oberon_new_operator(OP_LOGIC_AND, result, a, b); break;
1784 default: assert(0); break;
1788 else if(token == SLASH)
1790 if(oberon_is_set_type(a -> result) && oberon_is_set_type(b -> result))
1792 if(oberon_is_const(a) && oberon_is_const(b))
1794 int64_t x = a -> item.integer;
1795 int64_t y = b -> item.integer;
1796 expr = oberon_make_set(ctx, x ^ y);
1798 else
1800 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1801 a = oberon_cast_expr(ctx, a, result);
1802 b = oberon_cast_expr(ctx, b, result);
1803 expr = oberon_new_operator(OP_SYM_DIFFERENCE, result, a, b);
1806 else
1808 result = oberon_get_longer_real_type(ctx, a -> result, b -> result);
1809 if(oberon_is_const(a) && oberon_is_const(b))
1811 double x = a -> item.real;
1812 double y = b -> item.real;
1813 expr = oberon_make_real_typed(ctx, x / y, result);
1815 else
1817 a = oberon_cast_expr(ctx, a, result);
1818 b = oberon_cast_expr(ctx, b, result);
1819 expr = oberon_new_operator(OP_DIV, result, a, b);
1823 else
1825 result = oberon_get_longer_type(ctx, a -> result, b -> result);
1827 if(oberon_is_const(a) && oberon_is_const(b))
1829 if(oberon_is_set_type(result))
1831 int64_t x = a -> item.integer;
1832 int64_t y = b -> item.integer;
1833 switch(token)
1835 case PLUS: expr = oberon_make_set(ctx, x | y); break;
1836 case MINUS: expr = oberon_make_set(ctx, x & ~y); break;
1837 case STAR: expr = oberon_make_set(ctx, x & y); break;
1838 default: assert(0); break;
1841 if(oberon_is_real_type(result))
1843 double x = a -> item.real;
1844 double y = b -> item.real;
1845 switch(token)
1847 case PLUS: expr = oberon_make_real_typed(ctx, x + y, result); break;
1848 case MINUS: expr = oberon_make_real_typed(ctx, x - y, result); break;
1849 case STAR: expr = oberon_make_real_typed(ctx, x * y, result); break;
1850 default: assert(0); break;
1853 else if(oberon_is_integer_type(result))
1855 int64_t x = a -> item.integer;
1856 int64_t y = b -> item.integer;
1857 switch(token)
1859 case PLUS: expr = oberon_make_integer(ctx, x + y); break;
1860 case MINUS: expr = oberon_make_integer(ctx, x - y); break;
1861 case STAR: expr = oberon_make_integer(ctx, x * y); break;
1862 case DIV: expr = oberon_make_integer(ctx, x / y); break;
1863 case MOD: expr = oberon_make_integer(ctx, x % y); break;
1864 default: assert(0); break;
1867 else
1869 assert(0);
1872 else
1874 a = oberon_cast_expr(ctx, a, result);
1875 b = oberon_cast_expr(ctx, b, result);
1878 if(oberon_is_set_type(result))
1880 switch(token)
1882 case PLUS:
1883 expr = oberon_new_operator(OP_UNION, result, a, b);
1884 break;
1885 case MINUS:
1886 expr = oberon_new_operator(OP_DIFFERENCE, result, a, b);
1887 break;
1888 case STAR:
1889 expr = oberon_new_operator(OP_INTERSECTION, result, a, b);
1890 break;
1891 default:
1892 assert(0);
1893 break;
1896 else if(oberon_is_number_type(result))
1898 switch(token)
1900 case PLUS:
1901 expr = oberon_new_operator(OP_ADD, result, a, b);
1902 break;
1903 case MINUS:
1904 expr = oberon_new_operator(OP_SUB, result, a, b);
1905 break;
1906 case STAR:
1907 expr = oberon_new_operator(OP_MUL, result, a, b);
1908 break;
1909 default:
1910 assert(0);
1911 break;
1914 else
1916 assert(0);
1921 return expr;
1924 #define ISMULOP(x) \
1925 ((x) >= STAR && (x) <= AND)
1927 static oberon_expr_t *
1928 oberon_term_expr(oberon_context_t * ctx)
1930 oberon_expr_t * expr;
1932 expr = oberon_factor(ctx);
1933 while(ISMULOP(ctx -> token))
1935 int token = ctx -> token;
1936 oberon_read_token(ctx);
1938 oberon_expr_t * inter = oberon_factor(ctx);
1939 expr = oberon_make_bin_op(ctx, token, expr, inter);
1942 return expr;
1945 #define ISADDOP(x) \
1946 ((x) >= PLUS && (x) <= OR)
1948 static oberon_expr_t *
1949 oberon_simple_expr(oberon_context_t * ctx)
1951 oberon_expr_t * expr;
1953 int minus = 0;
1954 if(ctx -> token == PLUS)
1956 minus = 0;
1957 oberon_assert_token(ctx, PLUS);
1959 else if(ctx -> token == MINUS)
1961 minus = 1;
1962 oberon_assert_token(ctx, MINUS);
1965 expr = oberon_term_expr(ctx);
1967 while(ISADDOP(ctx -> token))
1969 int token = ctx -> token;
1970 oberon_read_token(ctx);
1972 oberon_expr_t * inter = oberon_term_expr(ctx);
1973 expr = oberon_make_bin_op(ctx, token, expr, inter);
1976 if(minus)
1978 expr = oberon_make_unary_op(ctx, MINUS, expr);
1981 return expr;
1984 #define ISRELATION(x) \
1985 ((x) >= EQUAL && (x) <= IS)
1987 static oberon_expr_t *
1988 oberon_expr(oberon_context_t * ctx)
1990 oberon_expr_t * expr;
1992 expr = oberon_simple_expr(ctx);
1993 while(ISRELATION(ctx -> token))
1995 int token = ctx -> token;
1996 oberon_read_token(ctx);
1998 oberon_expr_t * inter = oberon_simple_expr(ctx);
1999 expr = oberon_make_bin_op(ctx, token, expr, inter);
2002 return expr;
2005 static bool
2006 oberon_is_const(oberon_expr_t * expr)
2008 if(expr -> is_item == false)
2010 return false;
2013 switch(expr -> item.mode)
2015 case MODE_INTEGER:
2016 case MODE_BOOLEAN:
2017 case MODE_NIL:
2018 case MODE_REAL:
2019 case MODE_CHAR:
2020 case MODE_STRING:
2021 case MODE_SET:
2022 case MODE_TYPE:
2023 return true;
2024 break;
2025 default:
2026 return false;
2027 break;
2030 return false;
2033 static void
2034 oberon_check_const(oberon_context_t * ctx, oberon_expr_t * expr)
2036 if(!oberon_is_const(expr))
2038 oberon_error(ctx, "const expression are required");
2042 static oberon_item_t *
2043 oberon_const_expr(oberon_context_t * ctx)
2045 oberon_expr_t * expr;
2046 expr = oberon_expr(ctx);
2047 oberon_check_const(ctx, expr);
2048 return (oberon_item_t *) expr;
2051 // =======================================================================
2052 // PARSER
2053 // =======================================================================
2055 static void oberon_decl_seq(oberon_context_t * ctx);
2056 static void oberon_statement_seq(oberon_context_t * ctx);
2057 static void oberon_initialize_decl(oberon_context_t * ctx);
2059 static void
2060 oberon_expect_token(oberon_context_t * ctx, int token)
2062 if(ctx -> token != token)
2064 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
2068 static void
2069 oberon_assert_token(oberon_context_t * ctx, int token)
2071 oberon_expect_token(ctx, token);
2072 oberon_read_token(ctx);
2075 static char *
2076 oberon_assert_ident(oberon_context_t * ctx)
2078 oberon_expect_token(ctx, IDENT);
2079 char * ident = ctx -> string;
2080 oberon_read_token(ctx);
2081 return ident;
2084 static void
2085 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
2087 switch(ctx -> token)
2089 case STAR:
2090 oberon_assert_token(ctx, STAR);
2091 *export = 1;
2092 *read_only = 0;
2093 break;
2094 case MINUS:
2095 oberon_assert_token(ctx, MINUS);
2096 *export = 1;
2097 *read_only = 1;
2098 break;
2099 default:
2100 *export = 0;
2101 *read_only = 0;
2102 break;
2106 static oberon_object_t *
2107 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
2109 char * name;
2110 int export;
2111 int read_only;
2112 oberon_object_t * x;
2114 name = oberon_assert_ident(ctx);
2115 oberon_def(ctx, &export, &read_only);
2117 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
2118 return x;
2121 static void
2122 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
2124 *num = 1;
2125 *list = oberon_ident_def(ctx, class, check_upscope);
2126 while(ctx -> token == COMMA)
2128 oberon_assert_token(ctx, COMMA);
2129 oberon_ident_def(ctx, class, check_upscope);
2130 *num += 1;
2134 static void
2135 oberon_var_decl(oberon_context_t * ctx)
2137 int num;
2138 oberon_object_t * list;
2139 oberon_type_t * type;
2140 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2142 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
2143 oberon_assert_token(ctx, COLON);
2144 oberon_type(ctx, &type);
2146 oberon_object_t * var = list;
2147 for(int i = 0; i < num; i++)
2149 var -> type = type;
2150 var = var -> next;
2154 static oberon_object_t *
2155 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
2157 int class = OBERON_CLASS_PARAM;
2158 if(ctx -> token == VAR)
2160 oberon_read_token(ctx);
2161 class = OBERON_CLASS_VAR_PARAM;
2164 int num;
2165 oberon_object_t * list;
2166 oberon_ident_list(ctx, class, false, &num, &list);
2168 oberon_assert_token(ctx, COLON);
2170 oberon_type_t * type;
2171 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2172 oberon_type(ctx, &type);
2174 oberon_object_t * param = list;
2175 for(int i = 0; i < num; i++)
2177 param -> type = type;
2178 param = param -> next;
2181 *num_decl += num;
2182 return list;
2185 #define ISFPSECTION \
2186 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2188 static void
2189 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2191 oberon_assert_token(ctx, LPAREN);
2193 if(ISFPSECTION)
2195 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2196 while(ctx -> token == SEMICOLON)
2198 oberon_assert_token(ctx, SEMICOLON);
2199 oberon_fp_section(ctx, &signature -> num_decl);
2203 oberon_assert_token(ctx, RPAREN);
2205 if(ctx -> token == COLON)
2207 oberon_assert_token(ctx, COLON);
2209 oberon_object_t * typeobj;
2210 typeobj = oberon_qualident(ctx, NULL, 1);
2211 if(typeobj -> class != OBERON_CLASS_TYPE)
2213 oberon_error(ctx, "function result is not type");
2215 if(typeobj -> type -> class == OBERON_TYPE_RECORD
2216 || typeobj -> type -> class == OBERON_TYPE_ARRAY)
2218 oberon_error(ctx, "records or arrays could not be result of function");
2220 signature -> base = typeobj -> type;
2224 static void
2225 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2227 oberon_type_t * signature;
2228 signature = *type;
2229 signature -> class = OBERON_TYPE_PROCEDURE;
2230 signature -> num_decl = 0;
2231 signature -> base = ctx -> notype_type;
2232 signature -> decl = NULL;
2234 if(ctx -> token == LPAREN)
2236 oberon_formal_pars(ctx, signature);
2240 static void
2241 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2243 if(a -> num_decl != b -> num_decl)
2245 oberon_error(ctx, "number parameters not matched");
2248 int num_param = a -> num_decl;
2249 oberon_object_t * param_a = a -> decl;
2250 oberon_object_t * param_b = b -> decl;
2251 for(int i = 0; i < num_param; i++)
2253 if(strcmp(param_a -> name, param_b -> name) != 0)
2255 oberon_error(ctx, "param %i name not matched", i + 1);
2258 if(param_a -> type != param_b -> type)
2260 oberon_error(ctx, "param %i type not matched", i + 1);
2263 param_a = param_a -> next;
2264 param_b = param_b -> next;
2268 static void
2269 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2271 oberon_object_t * proc = ctx -> decl -> parent;
2272 oberon_type_t * result_type = proc -> type -> base;
2274 if(result_type -> class == OBERON_TYPE_NOTYPE)
2276 if(expr != NULL)
2278 oberon_error(ctx, "procedure has no result type");
2281 else
2283 if(expr == NULL)
2285 oberon_error(ctx, "procedure requires expression on result");
2288 oberon_check_src(ctx, expr);
2289 oberon_check_assignment_compatible(ctx, expr, result_type);
2290 expr = oberon_cast_expr(ctx, expr, result_type);
2293 proc -> has_return = 1;
2295 oberon_generate_return(ctx, expr);
2298 static void
2299 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2301 oberon_assert_token(ctx, SEMICOLON);
2303 ctx -> decl = proc -> scope;
2305 oberon_decl_seq(ctx);
2307 oberon_generate_begin_proc(ctx, proc);
2309 if(ctx -> token == BEGIN)
2311 oberon_assert_token(ctx, BEGIN);
2312 oberon_statement_seq(ctx);
2315 oberon_assert_token(ctx, END);
2316 char * name = oberon_assert_ident(ctx);
2317 if(strcmp(name, proc -> name) != 0)
2319 oberon_error(ctx, "procedure name not matched");
2322 if(proc -> type -> base -> class == OBERON_TYPE_NOTYPE
2323 && proc -> has_return == 0)
2325 oberon_make_return(ctx, NULL);
2328 if(proc -> has_return == 0)
2330 oberon_error(ctx, "procedure requires return");
2333 oberon_generate_end_proc(ctx);
2334 oberon_close_scope(ctx -> decl);
2337 static void
2338 oberon_proc_decl(oberon_context_t * ctx)
2340 oberon_assert_token(ctx, PROCEDURE);
2342 int forward = 0;
2343 if(ctx -> token == UPARROW)
2345 oberon_assert_token(ctx, UPARROW);
2346 forward = 1;
2349 char * name;
2350 int export;
2351 int read_only;
2352 name = oberon_assert_ident(ctx);
2353 oberon_def(ctx, &export, &read_only);
2355 oberon_scope_t * proc_scope;
2356 proc_scope = oberon_open_scope(ctx);
2357 ctx -> decl -> local = 1;
2359 oberon_type_t * signature;
2360 signature = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2361 oberon_opt_formal_pars(ctx, &signature);
2363 //oberon_initialize_decl(ctx);
2364 oberon_generator_init_type(ctx, signature);
2365 oberon_close_scope(ctx -> decl);
2367 oberon_object_t * proc;
2368 proc = oberon_find_object(ctx -> decl, name, 0);
2369 if(proc == NULL)
2371 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2372 proc -> type = signature;
2373 proc -> scope = proc_scope;
2374 oberon_generator_init_proc(ctx, proc);
2376 else
2378 if(proc -> class != OBERON_CLASS_PROC)
2380 oberon_error(ctx, "mult definition");
2383 if(forward == 0)
2385 if(proc -> linked)
2387 oberon_error(ctx, "mult procedure definition");
2391 if(proc -> export != export || proc -> read_only != read_only)
2393 oberon_error(ctx, "export type not matched");
2396 oberon_compare_signatures(ctx, proc -> type, signature);
2399 proc_scope -> parent = proc;
2400 oberon_object_t * param = proc_scope -> list -> next;
2401 while(param)
2403 param -> parent = proc;
2404 param = param -> next;
2407 if(forward == 0)
2409 proc -> linked = 1;
2410 oberon_proc_decl_body(ctx, proc);
2414 static void
2415 oberon_const_decl(oberon_context_t * ctx)
2417 oberon_item_t * value;
2418 oberon_object_t * constant;
2420 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2421 oberon_assert_token(ctx, EQUAL);
2422 value = oberon_const_expr(ctx);
2423 constant -> value = value;
2426 static void
2427 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2429 if(size -> is_item == 0)
2431 oberon_error(ctx, "requires constant");
2434 if(size -> item.mode != MODE_INTEGER)
2436 oberon_error(ctx, "requires integer constant");
2439 oberon_type_t * arr;
2440 arr = *type;
2441 arr -> class = OBERON_TYPE_ARRAY;
2442 arr -> size = size -> item.integer;
2443 arr -> base = base;
2446 static void
2447 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2449 char * name;
2450 oberon_object_t * to;
2452 to = oberon_qualident(ctx, &name, 0);
2454 //name = oberon_assert_ident(ctx);
2455 //to = oberon_find_object(ctx -> decl, name, 0);
2457 if(to != NULL)
2459 if(to -> class != OBERON_CLASS_TYPE)
2461 oberon_error(ctx, "not a type");
2464 else
2466 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2467 to -> type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2470 *type = to -> type;
2473 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2475 /*
2476 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2477 */
2479 static void
2480 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2482 if(sizes == NULL)
2484 *type = base;
2485 return;
2488 oberon_type_t * dim;
2489 dim = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2491 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2493 oberon_make_array_type(ctx, sizes, dim, type);
2496 static void
2497 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2499 type -> class = OBERON_TYPE_ARRAY;
2500 type -> size = 0;
2501 type -> base = base;
2504 static void
2505 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2507 if(ctx -> token == IDENT)
2509 int num;
2510 oberon_object_t * list;
2511 oberon_type_t * type;
2512 type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2514 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2515 oberon_assert_token(ctx, COLON);
2517 oberon_scope_t * current = ctx -> decl;
2518 ctx -> decl = modscope;
2519 oberon_type(ctx, &type);
2520 ctx -> decl = current;
2522 oberon_object_t * field = list;
2523 for(int i = 0; i < num; i++)
2525 field -> type = type;
2526 field = field -> next;
2529 rec -> num_decl += num;
2533 static void
2534 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2536 oberon_scope_t * modscope = ctx -> mod -> decl;
2537 oberon_scope_t * oldscope = ctx -> decl;
2538 ctx -> decl = modscope;
2540 if(ctx -> token == LPAREN)
2542 oberon_assert_token(ctx, LPAREN);
2544 oberon_object_t * typeobj;
2545 typeobj = oberon_qualident(ctx, NULL, true);
2547 if(typeobj -> class != OBERON_CLASS_TYPE)
2549 oberon_error(ctx, "base must be type");
2552 oberon_type_t * base = typeobj -> type;
2553 if(base -> class == OBERON_TYPE_POINTER)
2555 base = base -> base;
2558 if(base -> class != OBERON_TYPE_RECORD)
2560 oberon_error(ctx, "base must be record type");
2563 rec -> base = base;
2564 ctx -> decl = base -> scope;
2566 oberon_assert_token(ctx, RPAREN);
2568 else
2570 ctx -> decl = NULL;
2573 oberon_scope_t * this_scope;
2574 this_scope = oberon_open_scope(ctx);
2575 this_scope -> local = true;
2576 this_scope -> parent = NULL;
2577 this_scope -> parent_type = rec;
2579 oberon_field_list(ctx, rec, modscope);
2580 while(ctx -> token == SEMICOLON)
2582 oberon_assert_token(ctx, SEMICOLON);
2583 oberon_field_list(ctx, rec, modscope);
2586 rec -> scope = this_scope;
2587 rec -> decl = this_scope -> list -> next;
2588 ctx -> decl = oldscope;
2591 static void
2592 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2594 if(ctx -> token == IDENT)
2596 oberon_qualident_type(ctx, type);
2598 else if(ctx -> token == ARRAY)
2600 oberon_assert_token(ctx, ARRAY);
2602 int num_sizes = 0;
2603 oberon_expr_t * sizes;
2605 if(ISEXPR(ctx -> token))
2607 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2610 oberon_assert_token(ctx, OF);
2612 oberon_type_t * base;
2613 base = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2614 oberon_type(ctx, &base);
2616 if(num_sizes == 0)
2618 oberon_make_open_array(ctx, base, *type);
2620 else
2622 oberon_make_multiarray(ctx, sizes, base, type);
2625 else if(ctx -> token == RECORD)
2627 oberon_type_t * rec;
2628 rec = *type;
2629 rec -> class = OBERON_TYPE_RECORD;
2630 rec -> module = ctx -> mod;
2632 oberon_assert_token(ctx, RECORD);
2633 oberon_type_record_body(ctx, rec);
2634 oberon_assert_token(ctx, END);
2636 *type = rec;
2638 else if(ctx -> token == POINTER)
2640 oberon_assert_token(ctx, POINTER);
2641 oberon_assert_token(ctx, TO);
2643 oberon_type_t * base;
2644 base = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2645 oberon_type(ctx, &base);
2647 oberon_type_t * ptr;
2648 ptr = *type;
2649 ptr -> class = OBERON_TYPE_POINTER;
2650 ptr -> base = base;
2652 else if(ctx -> token == PROCEDURE)
2654 oberon_open_scope(ctx);
2655 oberon_assert_token(ctx, PROCEDURE);
2656 oberon_opt_formal_pars(ctx, type);
2657 oberon_close_scope(ctx -> decl);
2659 else
2661 oberon_error(ctx, "invalid type declaration");
2665 static void
2666 oberon_type_decl(oberon_context_t * ctx)
2668 char * name;
2669 oberon_object_t * newtype;
2670 oberon_type_t * type;
2671 int export;
2672 int read_only;
2674 name = oberon_assert_ident(ctx);
2675 oberon_def(ctx, &export, &read_only);
2677 newtype = oberon_find_object(ctx -> decl, name, 0);
2678 if(newtype == NULL)
2680 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2681 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
2682 assert(newtype -> type);
2684 else
2686 if(newtype -> class != OBERON_CLASS_TYPE)
2688 oberon_error(ctx, "mult definition");
2691 if(newtype -> linked)
2693 oberon_error(ctx, "mult definition - already linked");
2696 newtype -> export = export;
2697 newtype -> read_only = read_only;
2700 oberon_assert_token(ctx, EQUAL);
2702 type = newtype -> type;
2703 oberon_type(ctx, &type);
2705 if(type -> class == OBERON_TYPE_NOTYPE)
2707 oberon_error(ctx, "recursive alias declaration");
2710 newtype -> type = type;
2711 newtype -> linked = 1;
2714 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2715 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2717 static void
2718 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2720 if(type -> class != OBERON_TYPE_POINTER
2721 && type -> class != OBERON_TYPE_ARRAY)
2723 return;
2726 if(type -> recursive)
2728 oberon_error(ctx, "recursive pointer declaration");
2731 if(type -> class == OBERON_TYPE_POINTER
2732 && type -> base -> class == OBERON_TYPE_POINTER)
2734 oberon_error(ctx, "attempt to make pointer to pointer");
2737 type -> recursive = 1;
2739 oberon_prevent_recursive_pointer(ctx, type -> base);
2741 type -> recursive = 0;
2744 static void
2745 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2747 if(type -> class != OBERON_TYPE_RECORD)
2749 return;
2752 if(type -> recursive)
2754 oberon_error(ctx, "recursive record declaration");
2757 type -> recursive = 1;
2759 if(type -> base)
2761 oberon_prevent_recursive_record(ctx, type -> base);
2764 int num_fields = type -> num_decl;
2765 oberon_object_t * field = type -> decl;
2766 for(int i = 0; i < num_fields; i++)
2768 oberon_prevent_recursive_object(ctx, field);
2769 field = field -> next;
2772 type -> recursive = 0;
2774 static void
2775 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2777 if(type -> class != OBERON_TYPE_PROCEDURE)
2779 return;
2782 if(type -> recursive)
2784 oberon_error(ctx, "recursive procedure declaration");
2787 type -> recursive = 1;
2789 int num_fields = type -> num_decl;
2790 oberon_object_t * field = type -> decl;
2791 for(int i = 0; i < num_fields; i++)
2793 oberon_prevent_recursive_object(ctx, field);
2794 field = field -> next;
2797 type -> recursive = 0;
2800 static void
2801 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2803 if(type -> class != OBERON_TYPE_ARRAY)
2805 return;
2808 if(type -> recursive)
2810 oberon_error(ctx, "recursive array declaration");
2813 type -> recursive = 1;
2815 oberon_prevent_recursive_type(ctx, type -> base);
2817 type -> recursive = 0;
2820 static void
2821 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2823 if(type -> class == OBERON_TYPE_POINTER)
2825 oberon_prevent_recursive_pointer(ctx, type);
2827 else if(type -> class == OBERON_TYPE_RECORD)
2829 oberon_prevent_recursive_record(ctx, type);
2831 else if(type -> class == OBERON_TYPE_ARRAY)
2833 oberon_prevent_recursive_array(ctx, type);
2835 else if(type -> class == OBERON_TYPE_PROCEDURE)
2837 oberon_prevent_recursive_procedure(ctx, type);
2841 static void
2842 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2844 switch(x -> class)
2846 case OBERON_CLASS_VAR:
2847 case OBERON_CLASS_TYPE:
2848 case OBERON_CLASS_PARAM:
2849 case OBERON_CLASS_VAR_PARAM:
2850 case OBERON_CLASS_FIELD:
2851 oberon_prevent_recursive_type(ctx, x -> type);
2852 break;
2853 case OBERON_CLASS_CONST:
2854 case OBERON_CLASS_PROC:
2855 case OBERON_CLASS_MODULE:
2856 break;
2857 default:
2858 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2859 break;
2863 static void
2864 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2866 oberon_object_t * x = ctx -> decl -> list -> next;
2868 while(x)
2870 oberon_prevent_recursive_object(ctx, x);
2871 x = x -> next;
2875 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2876 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2878 static void
2879 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2881 if(type -> class != OBERON_TYPE_RECORD)
2883 return;
2886 int num_fields = type -> num_decl;
2887 oberon_object_t * field = type -> decl;
2888 for(int i = 0; i < num_fields; i++)
2890 if(field -> type -> class == OBERON_TYPE_POINTER)
2892 oberon_initialize_type(ctx, field -> type);
2895 oberon_initialize_object(ctx, field);
2896 field = field -> next;
2899 oberon_generator_init_record(ctx, type);
2902 static void
2903 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2905 if(type -> class == OBERON_TYPE_NOTYPE)
2907 oberon_error(ctx, "undeclarated type");
2910 if(type -> initialized)
2912 return;
2915 type -> initialized = 1;
2917 if(type -> class == OBERON_TYPE_POINTER)
2919 oberon_initialize_type(ctx, type -> base);
2920 oberon_generator_init_type(ctx, type);
2922 else if(type -> class == OBERON_TYPE_ARRAY)
2924 if(type -> size != 0)
2926 if(type -> base -> class == OBERON_TYPE_ARRAY)
2928 if(type -> base -> size == 0)
2930 oberon_error(ctx, "open array not allowed as array element");
2935 oberon_initialize_type(ctx, type -> base);
2936 oberon_generator_init_type(ctx, type);
2938 else if(type -> class == OBERON_TYPE_RECORD)
2940 oberon_generator_init_type(ctx, type);
2941 oberon_initialize_record_fields(ctx, type);
2943 else if(type -> class == OBERON_TYPE_PROCEDURE)
2945 int num_fields = type -> num_decl;
2946 oberon_object_t * field = type -> decl;
2947 for(int i = 0; i < num_fields; i++)
2949 //oberon_initialize_object(ctx, field);
2950 oberon_initialize_type(ctx, field -> type);
2951 field = field -> next;
2954 oberon_generator_init_type(ctx, type);
2956 else
2958 oberon_generator_init_type(ctx, type);
2962 static void
2963 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2965 if(x -> initialized)
2967 return;
2970 x -> initialized = 1;
2972 switch(x -> class)
2974 case OBERON_CLASS_TYPE:
2975 oberon_initialize_type(ctx, x -> type);
2976 break;
2977 case OBERON_CLASS_VAR:
2978 case OBERON_CLASS_FIELD:
2979 if(x -> type -> class == OBERON_TYPE_ARRAY)
2981 if(x -> type -> size == 0)
2983 oberon_error(ctx, "open array not allowed as variable or field");
2986 oberon_initialize_type(ctx, x -> type);
2987 oberon_generator_init_var(ctx, x);
2988 break;
2989 case OBERON_CLASS_PARAM:
2990 case OBERON_CLASS_VAR_PARAM:
2991 oberon_initialize_type(ctx, x -> type);
2992 oberon_generator_init_var(ctx, x);
2993 break;
2994 case OBERON_CLASS_CONST:
2995 case OBERON_CLASS_PROC:
2996 case OBERON_CLASS_MODULE:
2997 break;
2998 default:
2999 oberon_error(ctx, "oberon_initialize_object: wat");
3000 break;
3004 static void
3005 oberon_initialize_decl(oberon_context_t * ctx)
3007 oberon_object_t * x = ctx -> decl -> list;
3009 while(x -> next)
3011 oberon_initialize_object(ctx, x -> next);
3012 x = x -> next;
3013 }
3016 static void
3017 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
3019 oberon_object_t * x = ctx -> decl -> list;
3021 while(x -> next)
3023 if(x -> next -> class == OBERON_CLASS_PROC)
3025 if(x -> next -> linked == 0)
3027 oberon_error(ctx, "unresolved forward declaration");
3030 x = x -> next;
3031 }
3034 static void
3035 oberon_decl_seq(oberon_context_t * ctx)
3037 if(ctx -> token == CONST)
3039 oberon_assert_token(ctx, CONST);
3040 while(ctx -> token == IDENT)
3042 oberon_const_decl(ctx);
3043 oberon_assert_token(ctx, SEMICOLON);
3047 if(ctx -> token == TYPE)
3049 oberon_assert_token(ctx, TYPE);
3050 while(ctx -> token == IDENT)
3052 oberon_type_decl(ctx);
3053 oberon_assert_token(ctx, SEMICOLON);
3057 if(ctx -> token == VAR)
3059 oberon_assert_token(ctx, VAR);
3060 while(ctx -> token == IDENT)
3062 oberon_var_decl(ctx);
3063 oberon_assert_token(ctx, SEMICOLON);
3067 oberon_prevent_recursive_decl(ctx);
3068 oberon_initialize_decl(ctx);
3070 while(ctx -> token == PROCEDURE)
3072 oberon_proc_decl(ctx);
3073 oberon_assert_token(ctx, SEMICOLON);
3076 oberon_prevent_undeclarated_procedures(ctx);
3079 static oberon_expr_t *
3080 oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type)
3082 oberon_object_t * x;
3083 oberon_expr_t * expr;
3085 x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false);
3086 x -> local = true;
3087 x -> type = type;
3088 oberon_generator_init_temp_var(ctx, x);
3090 expr = oberon_new_item(MODE_VAR, type, false);
3091 expr -> item.var = x;
3092 return expr;
3095 static void
3096 oberon_statement_seq(oberon_context_t * ctx);
3098 static void
3099 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
3101 oberon_check_dst(ctx, dst);
3102 oberon_check_assignment_compatible(ctx, src, dst -> result);
3104 if(oberon_is_array_of_char_type(dst -> result)
3105 && oberon_is_string_type(src -> result))
3107 src -> next = dst;
3108 oberon_make_copy_call(ctx, 2, src);
3110 else
3112 src = oberon_cast_expr(ctx, src, dst -> result);
3113 oberon_generate_assign(ctx, src, dst);
3117 static oberon_expr_t *
3118 oberon_case_labels(oberon_context_t * ctx, oberon_expr_t * val)
3120 oberon_expr_t * e1;
3121 oberon_expr_t * e2;
3122 oberon_expr_t * cond;
3123 oberon_expr_t * cond2;
3125 e1 = (oberon_expr_t *) oberon_const_expr(ctx);
3127 e2 = NULL;
3128 if(ctx -> token == DOTDOT)
3130 oberon_assert_token(ctx, DOTDOT);
3131 e2 = (oberon_expr_t *) oberon_const_expr(ctx);
3134 if(e2 == NULL)
3136 /* val == e1 */
3137 cond = oberon_make_bin_op(ctx, EQUAL, val, e1);
3139 else
3141 /* val >= e1 && val <= e2 */
3142 cond = oberon_make_bin_op(ctx, GEQ, val, e1);
3143 cond2 = oberon_make_bin_op(ctx, LEQ, val, e2);
3144 cond = oberon_make_bin_op(ctx, AND, cond, cond2);
3147 return cond;
3150 static void
3151 oberon_case(oberon_context_t * ctx, oberon_expr_t * val, gen_label_t * end)
3153 oberon_expr_t * cond;
3154 oberon_expr_t * cond2;
3155 gen_label_t * this_end;
3157 if(ISEXPR(ctx -> token))
3159 this_end = oberon_generator_reserve_label(ctx);
3161 cond = oberon_case_labels(ctx, val);
3162 while(ctx -> token == COMMA)
3164 oberon_assert_token(ctx, COMMA);
3165 /* cond || cond2 */
3166 cond2 = oberon_case_labels(ctx, val);
3167 cond = oberon_make_bin_op(ctx, OR, cond, cond2);
3169 oberon_assert_token(ctx, COLON);
3171 oberon_generate_branch(ctx, cond, false, this_end);
3172 oberon_statement_seq(ctx);
3173 oberon_generate_goto(ctx, end);
3175 oberon_generate_label(ctx, this_end);
3179 static void
3180 oberon_case_statement(oberon_context_t * ctx)
3182 oberon_expr_t * val;
3183 oberon_expr_t * expr;
3184 gen_label_t * end;
3186 end = oberon_generator_reserve_label(ctx);
3188 oberon_assert_token(ctx, CASE);
3189 expr = oberon_expr(ctx);
3190 val = oberon_make_temp_var_item(ctx, expr -> result);
3191 oberon_assign(ctx, expr, val);
3192 oberon_assert_token(ctx, OF);
3193 oberon_case(ctx, val, end);
3194 while(ctx -> token == BAR)
3196 oberon_assert_token(ctx, BAR);
3197 oberon_case(ctx, val, end);
3200 if(ctx -> token == ELSE)
3202 oberon_assert_token(ctx, ELSE);
3203 oberon_statement_seq(ctx);
3205 else
3207 oberon_generate_trap(ctx, -1);
3210 oberon_generate_label(ctx, end);
3211 oberon_assert_token(ctx, END);
3214 static void
3215 oberon_with_guard_do(oberon_context_t * ctx, gen_label_t * end)
3217 oberon_expr_t * val;
3218 oberon_expr_t * var;
3219 oberon_expr_t * type;
3220 oberon_expr_t * cond;
3221 oberon_expr_t * cast;
3222 oberon_type_t * old_type;
3223 gen_var_t * old_var;
3224 gen_label_t * this_end;
3226 this_end = oberon_generator_reserve_label(ctx);
3228 var = oberon_qualident_expr(ctx);
3229 oberon_assert_token(ctx, COLON);
3230 type = oberon_qualident_expr(ctx);
3231 cond = oberon_make_bin_op(ctx, IS, var, type);
3233 oberon_assert_token(ctx, DO);
3234 oberon_generate_branch(ctx, cond, false, this_end);
3236 /* Сохраняем ссылку во временной переменной */
3237 val = oberon_make_temp_var_item(ctx, type -> result);
3238 //cast = oberno_make_record_cast(ctx, var, type -> result);
3239 cast = oberon_cast_expr(ctx, var, type -> result);
3240 oberon_assign(ctx, cast, val);
3241 /* Подменяем тип у оригинальной переменной */
3242 old_type = var -> item.var -> type;
3243 var -> item.var -> type = type -> result;
3244 /* Подменяем ссылку на переменную */
3245 old_var = var -> item.var -> gen_var;
3246 var -> item.var -> gen_var = val -> item.var -> gen_var;
3248 oberon_statement_seq(ctx);
3249 oberon_generate_goto(ctx, end);
3250 oberon_generate_label(ctx, this_end);
3252 /* Возвращаем исходное состояние */
3253 var -> item.var -> gen_var = old_var;
3254 var -> item.var -> type = old_type;
3257 static void
3258 oberon_with_statement(oberon_context_t * ctx)
3260 gen_label_t * end;
3261 end = oberon_generator_reserve_label(ctx);
3263 oberon_assert_token(ctx, WITH);
3264 oberon_with_guard_do(ctx, end);
3265 while(ctx -> token == BAR)
3267 oberon_assert_token(ctx, BAR);
3268 oberon_with_guard_do(ctx, end);
3271 if(ctx -> token == ELSE)
3273 oberon_assert_token(ctx, ELSE);
3274 oberon_statement_seq(ctx);
3276 else
3278 oberon_generate_trap(ctx, -2);
3281 oberon_generate_label(ctx, end);
3282 oberon_assert_token(ctx, END);
3285 static void
3286 oberon_statement(oberon_context_t * ctx)
3288 oberon_expr_t * item1;
3289 oberon_expr_t * item2;
3291 if(ctx -> token == IDENT)
3293 item1 = oberon_designator(ctx);
3294 if(ctx -> token == ASSIGN)
3296 oberon_assert_token(ctx, ASSIGN);
3297 item2 = oberon_expr(ctx);
3298 oberon_assign(ctx, item2, item1);
3300 else
3302 oberon_opt_proc_parens(ctx, item1);
3305 else if(ctx -> token == IF)
3307 gen_label_t * end;
3308 gen_label_t * els;
3309 oberon_expr_t * cond;
3311 els = oberon_generator_reserve_label(ctx);
3312 end = oberon_generator_reserve_label(ctx);
3314 oberon_assert_token(ctx, IF);
3315 cond = oberon_expr(ctx);
3316 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3318 oberon_error(ctx, "condition must be boolean");
3320 oberon_assert_token(ctx, THEN);
3321 oberon_generate_branch(ctx, cond, false, els);
3322 oberon_statement_seq(ctx);
3323 oberon_generate_goto(ctx, end);
3324 oberon_generate_label(ctx, els);
3326 while(ctx -> token == ELSIF)
3328 els = oberon_generator_reserve_label(ctx);
3330 oberon_assert_token(ctx, ELSIF);
3331 cond = oberon_expr(ctx);
3332 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3334 oberon_error(ctx, "condition must be boolean");
3336 oberon_assert_token(ctx, THEN);
3337 oberon_generate_branch(ctx, cond, false, els);
3338 oberon_statement_seq(ctx);
3339 oberon_generate_goto(ctx, end);
3340 oberon_generate_label(ctx, els);
3343 if(ctx -> token == ELSE)
3345 oberon_assert_token(ctx, ELSE);
3346 oberon_statement_seq(ctx);
3349 oberon_generate_label(ctx, end);
3350 oberon_assert_token(ctx, END);
3352 else if(ctx -> token == WHILE)
3354 gen_label_t * begin;
3355 gen_label_t * end;
3356 oberon_expr_t * cond;
3358 begin = oberon_generator_reserve_label(ctx);
3359 end = oberon_generator_reserve_label(ctx);
3361 oberon_assert_token(ctx, WHILE);
3362 oberon_generate_label(ctx, begin);
3363 cond = oberon_expr(ctx);
3364 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3366 oberon_error(ctx, "condition must be boolean");
3368 oberon_generate_branch(ctx, cond, false, end);
3370 oberon_assert_token(ctx, DO);
3371 oberon_statement_seq(ctx);
3372 oberon_generate_goto(ctx, begin);
3374 oberon_assert_token(ctx, END);
3375 oberon_generate_label(ctx, end);
3377 else if(ctx -> token == REPEAT)
3379 gen_label_t * begin;
3380 oberon_expr_t * cond;
3382 begin = oberon_generator_reserve_label(ctx);
3383 oberon_generate_label(ctx, begin);
3384 oberon_assert_token(ctx, REPEAT);
3386 oberon_statement_seq(ctx);
3388 oberon_assert_token(ctx, UNTIL);
3390 cond = oberon_expr(ctx);
3391 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3393 oberon_error(ctx, "condition must be boolean");
3396 oberon_generate_branch(ctx, cond, true, begin);
3398 else if(ctx -> token == FOR)
3400 oberon_expr_t * from;
3401 oberon_expr_t * index;
3402 oberon_expr_t * to;
3403 oberon_expr_t * bound;
3404 oberon_expr_t * by;
3405 oberon_expr_t * cond;
3406 oberon_expr_t * count;
3407 gen_label_t * begin;
3408 gen_label_t * end;
3409 char * iname;
3410 int op;
3412 begin = oberon_generator_reserve_label(ctx);
3413 end = oberon_generator_reserve_label(ctx);
3415 oberon_assert_token(ctx, FOR);
3416 iname = oberon_assert_ident(ctx);
3417 index = oberon_ident_item(ctx, iname);
3418 oberon_assert_token(ctx, ASSIGN);
3419 from = oberon_expr(ctx);
3420 oberon_assert_token(ctx, TO);
3421 bound = oberon_make_temp_var_item(ctx, index -> result);
3422 to = oberon_expr(ctx);
3423 oberon_assign(ctx, to, bound); // сначала temp
3424 oberon_assign(ctx, from, index); // потом i
3425 if(ctx -> token == BY)
3427 oberon_assert_token(ctx, BY);
3428 by = (oberon_expr_t *) oberon_const_expr(ctx);
3430 else
3432 by = oberon_make_integer(ctx, 1);
3435 if(by -> result -> class != OBERON_TYPE_INTEGER)
3437 oberon_error(ctx, "must be integer");
3440 if(by -> item.integer > 0)
3442 op = LEQ;
3444 else if(by -> item.integer < 0)
3446 op = GEQ;
3448 else
3450 oberon_error(ctx, "zero step not allowed");
3453 oberon_assert_token(ctx, DO);
3454 oberon_generate_label(ctx, begin);
3455 cond = oberon_make_bin_op(ctx, op, index, bound);
3456 oberon_generate_branch(ctx, cond, false, end);
3457 oberon_statement_seq(ctx);
3458 count = oberon_make_bin_op(ctx, PLUS, index, by);
3459 oberon_assign(ctx, count, index);
3460 oberon_generate_goto(ctx, begin);
3461 oberon_generate_label(ctx, end);
3462 oberon_assert_token(ctx, END);
3464 else if(ctx -> token == LOOP)
3466 gen_label_t * begin;
3467 gen_label_t * end;
3469 begin = oberon_generator_reserve_label(ctx);
3470 end = oberon_generator_reserve_label(ctx);
3472 oberon_open_scope(ctx);
3473 oberon_assert_token(ctx, LOOP);
3474 oberon_generate_label(ctx, begin);
3475 ctx -> decl -> exit_label = end;
3476 oberon_statement_seq(ctx);
3477 oberon_generate_goto(ctx, begin);
3478 oberon_generate_label(ctx, end);
3479 oberon_assert_token(ctx, END);
3480 oberon_close_scope(ctx -> decl);
3482 else if(ctx -> token == EXIT)
3484 oberon_assert_token(ctx, EXIT);
3485 if(ctx -> decl -> exit_label == NULL)
3487 oberon_error(ctx, "not in LOOP-END");
3489 oberon_generate_goto(ctx, ctx -> decl -> exit_label);
3491 else if(ctx -> token == CASE)
3493 oberon_case_statement(ctx);
3495 else if(ctx -> token == WITH)
3497 oberon_with_statement(ctx);
3499 else if(ctx -> token == RETURN)
3501 oberon_assert_token(ctx, RETURN);
3502 if(ISEXPR(ctx -> token))
3504 oberon_expr_t * expr;
3505 expr = oberon_expr(ctx);
3506 oberon_make_return(ctx, expr);
3508 else
3510 oberon_make_return(ctx, NULL);
3515 static void
3516 oberon_statement_seq(oberon_context_t * ctx)
3518 oberon_statement(ctx);
3519 while(ctx -> token == SEMICOLON)
3521 oberon_assert_token(ctx, SEMICOLON);
3522 oberon_statement(ctx);
3526 static void
3527 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
3529 oberon_module_t * m = ctx -> module_list;
3530 while(m && strcmp(m -> name, name) != 0)
3532 m = m -> next;
3535 if(m == NULL)
3537 const char * code;
3538 code = ctx -> import_module(name);
3539 if(code == NULL)
3541 oberon_error(ctx, "no such module");
3544 m = oberon_compile_module(ctx, code);
3545 assert(m);
3548 if(m -> ready == 0)
3550 oberon_error(ctx, "cyclic module import");
3553 oberon_object_t * ident;
3554 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3555 ident -> module = m;
3558 static void
3559 oberon_import_decl(oberon_context_t * ctx)
3561 char * alias;
3562 char * name;
3564 alias = name = oberon_assert_ident(ctx);
3565 if(ctx -> token == ASSIGN)
3567 oberon_assert_token(ctx, ASSIGN);
3568 name = oberon_assert_ident(ctx);
3571 oberon_import_module(ctx, alias, name);
3574 static void
3575 oberon_import_list(oberon_context_t * ctx)
3577 oberon_assert_token(ctx, IMPORT);
3579 oberon_import_decl(ctx);
3580 while(ctx -> token == COMMA)
3582 oberon_assert_token(ctx, COMMA);
3583 oberon_import_decl(ctx);
3586 oberon_assert_token(ctx, SEMICOLON);
3589 static void
3590 oberon_parse_module(oberon_context_t * ctx)
3592 char * name1;
3593 char * name2;
3594 oberon_read_token(ctx);
3596 oberon_assert_token(ctx, MODULE);
3597 name1 = oberon_assert_ident(ctx);
3598 oberon_assert_token(ctx, SEMICOLON);
3599 ctx -> mod -> name = name1;
3601 oberon_generator_init_module(ctx, ctx -> mod);
3603 if(ctx -> token == IMPORT)
3605 oberon_import_list(ctx);
3608 oberon_decl_seq(ctx);
3610 oberon_generate_begin_module(ctx);
3611 if(ctx -> token == BEGIN)
3613 oberon_assert_token(ctx, BEGIN);
3614 oberon_statement_seq(ctx);
3616 oberon_generate_end_module(ctx);
3618 oberon_assert_token(ctx, END);
3619 name2 = oberon_assert_ident(ctx);
3620 oberon_expect_token(ctx, DOT);
3622 if(strcmp(name1, name2) != 0)
3624 oberon_error(ctx, "module name not matched");
3627 oberon_generator_fini_module(ctx -> mod);
3630 // =======================================================================
3631 // LIBRARY
3632 // =======================================================================
3634 static oberon_expr_t *
3635 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3637 if(num_args < 1)
3639 oberon_error(ctx, "too few arguments");
3642 if(num_args > 1)
3644 oberon_error(ctx, "too mach arguments");
3647 oberon_expr_t * arg;
3648 arg = list_args;
3650 if(!oberon_is_type_expr(arg))
3652 oberon_error(ctx, "MIN accept only type");
3655 oberon_expr_t * expr;
3656 int bits = arg -> result -> size * 8;
3657 switch(arg -> result -> class)
3659 case OBERON_TYPE_INTEGER:
3660 expr = oberon_make_integer(ctx, -powl(2, bits - 1));
3661 break;
3662 case OBERON_TYPE_BOOLEAN:
3663 expr = oberon_make_boolean(ctx, false);
3664 break;
3665 case OBERON_TYPE_CHAR:
3666 expr = oberon_make_char(ctx, 0);
3667 break;
3668 case OBERON_TYPE_REAL:
3669 expr = oberon_make_real_typed(ctx, (bits <= 32) ? (-FLT_MAX) : (-DBL_MAX), arg -> result);
3670 break;
3671 case OBERON_TYPE_SET:
3672 expr = oberon_make_integer(ctx, 0);
3673 break;
3674 default:
3675 oberon_error(ctx, "allowed only basic types");
3676 break;
3679 return expr;
3682 static oberon_expr_t *
3683 oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3685 if(num_args < 1)
3687 oberon_error(ctx, "too few arguments");
3690 if(num_args > 1)
3692 oberon_error(ctx, "too mach arguments");
3695 oberon_expr_t * arg;
3696 arg = list_args;
3698 if(!oberon_is_type_expr(arg))
3700 oberon_error(ctx, "MAX accept only type");
3703 oberon_expr_t * expr;
3704 int bits = arg -> result -> size * 8;
3705 switch(arg -> result -> class)
3707 case OBERON_TYPE_INTEGER:
3708 expr = oberon_make_integer(ctx, powl(2, bits - 1) - 1);
3709 break;
3710 case OBERON_TYPE_BOOLEAN:
3711 expr = oberon_make_boolean(ctx, true);
3712 break;
3713 case OBERON_TYPE_CHAR:
3714 expr = oberon_make_char(ctx, powl(2, bits) - 1);
3715 break;
3716 case OBERON_TYPE_REAL:
3717 expr = oberon_make_real_typed(ctx, (bits <= 32) ? (FLT_MAX) : (DBL_MAX), arg -> result);
3718 break;
3719 case OBERON_TYPE_SET:
3720 expr = oberon_make_integer(ctx, bits);
3721 break;
3722 default:
3723 oberon_error(ctx, "allowed only basic types");
3724 break;
3727 return expr;
3730 static oberon_expr_t *
3731 oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3733 if(num_args < 1)
3735 oberon_error(ctx, "too few arguments");
3738 if(num_args > 1)
3740 oberon_error(ctx, "too mach arguments");
3743 oberon_expr_t * arg;
3744 arg = list_args;
3745 if(!oberon_is_type_expr(arg))
3747 oberon_error(ctx, "SIZE accept only type");
3750 int size;
3751 oberon_expr_t * expr;
3752 oberon_type_t * type = arg -> result;
3753 switch(type -> class)
3755 case OBERON_TYPE_INTEGER:
3756 case OBERON_TYPE_BOOLEAN:
3757 case OBERON_TYPE_REAL:
3758 case OBERON_TYPE_CHAR:
3759 case OBERON_TYPE_SET:
3760 size = type -> size;
3761 break;
3762 default:
3763 oberon_error(ctx, "TODO SIZE");
3764 break;
3767 expr = oberon_make_integer(ctx, size);
3768 return expr;
3771 static oberon_expr_t *
3772 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3774 if(num_args < 1)
3776 oberon_error(ctx, "too few arguments");
3779 if(num_args > 1)
3781 oberon_error(ctx, "too mach arguments");
3784 oberon_expr_t * arg;
3785 arg = list_args;
3786 oberon_check_src(ctx, arg);
3788 if(oberon_is_number_type(arg -> result))
3790 oberon_error(ctx, "ABS accepts only numbers");
3793 oberon_expr_t * expr;
3794 if(oberon_is_const(arg))
3796 if(oberon_is_real_type(arg -> result))
3798 double x = arg -> item.real;
3799 expr = oberon_make_real(ctx, fabsl(x), arg -> result);
3801 else
3803 int64_t x = arg -> item.integer;
3804 expr = oberon_make_integer(ctx, llabs(x));
3807 else
3809 expr = oberon_new_operator(OP_ABS, arg -> result, arg, NULL);
3811 return expr;
3814 static void
3815 oberon_make_inc_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3817 if(num_args < 1)
3819 oberon_error(ctx, "too few arguments");
3822 if(num_args > 1)
3824 oberon_error(ctx, "too mach arguments");
3827 oberon_expr_t * dst;
3828 dst = list_args;
3829 oberon_check_dst(ctx, dst);
3831 if(!oberon_is_integer_type(dst -> result))
3833 oberon_error(ctx, "expect integer");
3836 oberon_expr_t * expr;
3837 expr = oberon_make_bin_op(ctx, PLUS, dst, oberon_make_integer(ctx, 1));
3838 oberon_assign(ctx, expr, dst);
3841 static void
3842 oberon_make_incl_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3844 if(num_args < 2)
3846 oberon_error(ctx, "too few arguments");
3849 if(num_args > 2)
3851 oberon_error(ctx, "too mach arguments");
3854 oberon_expr_t * dst;
3855 dst = list_args;
3856 oberon_check_dst(ctx, dst);
3858 if(!oberon_is_set_type(dst -> result))
3860 oberon_error(ctx, "expect integer");
3863 oberon_expr_t * x;
3864 x = list_args -> next;
3865 oberon_check_src(ctx, x);
3867 if(!oberon_is_integer_type(x -> result))
3869 oberon_error(ctx, "expect integer");
3872 oberon_expr_t * expr;
3873 expr = oberon_make_bin_op(ctx, PLUS, dst, oberon_new_operator(OP_RANGE, dst -> result, x, NULL));
3874 oberon_assign(ctx, expr, dst);
3877 static void
3878 oberon_make_excl_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3880 if(num_args < 2)
3882 oberon_error(ctx, "too few arguments");
3885 if(num_args > 2)
3887 oberon_error(ctx, "too mach arguments");
3890 oberon_expr_t * dst;
3891 dst = list_args;
3892 oberon_check_dst(ctx, dst);
3894 if(!oberon_is_set_type(dst -> result))
3896 oberon_error(ctx, "expect integer");
3899 oberon_expr_t * x;
3900 x = list_args -> next;
3901 oberon_check_src(ctx, x);
3903 if(!oberon_is_integer_type(x -> result))
3905 oberon_error(ctx, "expect integer");
3908 oberon_expr_t * expr;
3909 expr = oberon_make_bin_op(ctx, MINUS, dst, oberon_new_operator(OP_RANGE, dst -> result, x, NULL));
3910 oberon_assign(ctx, expr, dst);
3913 static void
3914 oberon_make_dec_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3916 if(num_args < 1)
3918 oberon_error(ctx, "too few arguments");
3921 if(num_args > 1)
3923 oberon_error(ctx, "too mach arguments");
3926 oberon_expr_t * dst;
3927 dst = list_args;
3928 oberon_check_dst(ctx, dst);
3930 if(!oberon_is_integer_type(dst -> result))
3932 oberon_error(ctx, "expect integer");
3935 oberon_expr_t * expr;
3936 expr = oberon_make_bin_op(ctx, MINUS, dst, oberon_make_integer(ctx, 1));
3937 oberon_assign(ctx, expr, dst);
3940 static void
3941 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3943 if(num_args < 1)
3945 oberon_error(ctx, "too few arguments");
3948 oberon_expr_t * dst;
3949 dst = list_args;
3950 oberon_check_dst(ctx, dst);
3952 oberon_type_t * type;
3953 type = dst -> result;
3955 if(type -> class != OBERON_TYPE_POINTER)
3957 oberon_error(ctx, "not a pointer");
3960 type = type -> base;
3962 oberon_expr_t * src;
3963 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3964 src -> item.num_args = 0;
3965 src -> item.args = NULL;
3967 int max_args = 1;
3968 if(type -> class == OBERON_TYPE_ARRAY)
3970 if(type -> size == 0)
3972 oberon_type_t * x = type;
3973 while(x -> class == OBERON_TYPE_ARRAY)
3975 if(x -> size == 0)
3977 max_args += 1;
3979 x = x -> base;
3983 if(num_args < max_args)
3985 oberon_error(ctx, "too few arguments");
3988 if(num_args > max_args)
3990 oberon_error(ctx, "too mach arguments");
3993 int num_sizes = max_args - 1;
3994 oberon_expr_t * size_list = list_args -> next;
3996 oberon_expr_t * arg = size_list;
3997 for(int i = 0; i < max_args - 1; i++)
3999 oberon_check_src(ctx, arg);
4000 if(arg -> result -> class != OBERON_TYPE_INTEGER)
4002 oberon_error(ctx, "size must be integer");
4004 arg = arg -> next;
4007 src -> item.num_args = num_sizes;
4008 src -> item.args = size_list;
4010 else if(type -> class != OBERON_TYPE_RECORD)
4012 oberon_error(ctx, "oberon_make_new_call: wat");
4015 if(num_args > max_args)
4017 oberon_error(ctx, "too mach arguments");
4020 oberon_assign(ctx, src, dst);
4023 static void
4024 oberon_make_copy_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4026 if(num_args < 2)
4028 oberon_error(ctx, "too few arguments");
4031 if(num_args > 2)
4033 oberon_error(ctx, "too mach arguments");
4036 oberon_expr_t * src;
4037 src = list_args;
4038 oberon_check_src(ctx, src);
4040 oberon_expr_t * dst;
4041 dst = list_args -> next;
4042 oberon_check_dst(ctx, dst);
4044 if(!oberon_is_string_type(src -> result) && !oberon_is_array_of_char_type(src -> result))
4046 oberon_error(ctx, "source must be string or array of char");
4049 if(!oberon_is_array_of_char_type(dst -> result))
4051 oberon_error(ctx, "dst must be array of char");
4054 oberon_generate_copy(ctx, src, dst);
4057 static void
4058 oberon_make_assert_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4060 if(num_args < 1)
4062 oberon_error(ctx, "too few arguments");
4065 if(num_args > 2)
4067 oberon_error(ctx, "too mach arguments");
4070 oberon_expr_t * cond;
4071 cond = list_args;
4072 oberon_check_src(ctx, cond);
4074 if(!oberon_is_boolean_type(cond -> result))
4076 oberon_error(ctx, "expected boolean");
4079 if(num_args == 1)
4081 oberon_generate_assert(ctx, cond);
4083 else
4085 oberon_expr_t * num;
4086 num = list_args -> next;
4087 oberon_check_src(ctx, num);
4089 if(!oberon_is_integer_type(num -> result))
4091 oberon_error(ctx, "expected integer");
4094 oberon_check_const(ctx, num);
4096 oberon_generate_assert_n(ctx, cond, num -> item.integer);
4100 static void
4101 oberon_make_halt_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4103 if(num_args < 1)
4105 oberon_error(ctx, "too few arguments");
4108 if(num_args > 1)
4110 oberon_error(ctx, "too mach arguments");
4113 oberon_expr_t * num;
4114 num = list_args;
4115 oberon_check_src(ctx, num);
4117 if(num -> result -> class != OBERON_TYPE_INTEGER)
4119 oberon_error(ctx, "expected integer");
4122 oberon_check_const(ctx, num);
4124 oberon_generate_halt(ctx, num -> item.integer);
4127 static oberon_expr_t *
4128 oberon_make_ash_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4130 if(num_args < 2)
4132 oberon_error(ctx, "too few arguments");
4135 if(num_args > 2)
4137 oberon_error(ctx, "too mach arguments");
4140 oberon_expr_t * arg1;
4141 arg1 = list_args;
4142 oberon_check_src(ctx, arg1);
4143 if(arg1 -> result -> class != OBERON_TYPE_INTEGER)
4145 oberon_error(ctx, "expected integer");
4148 oberon_expr_t * arg2;
4149 arg2 = list_args -> next;
4150 oberon_check_src(ctx, arg2);
4151 if(arg2 -> result -> class != OBERON_TYPE_INTEGER)
4153 oberon_error(ctx, "expected integer");
4156 oberon_expr_t * expr;
4157 if(oberon_is_const(arg1) && oberon_is_const(arg2))
4159 int64_t x = arg1 -> item.integer;
4160 int64_t y = arg2 -> item.integer;
4161 expr = oberon_make_integer(ctx, x * powl(2, y));
4163 else
4165 expr = oberon_new_operator(OP_ASH, arg1 -> result, arg1, arg2);
4168 return expr;
4171 static oberon_expr_t *
4172 oberon_make_cap_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4174 if(num_args < 1)
4176 oberon_error(ctx, "too few arguments");
4179 if(num_args > 1)
4181 oberon_error(ctx, "too mach arguments");
4184 oberon_expr_t * arg;
4185 arg = list_args;
4186 oberon_check_src(ctx, arg);
4188 if(!oberon_is_char_type(arg -> result))
4190 oberon_error(ctx, "expected char");
4193 oberon_expr_t * expr;
4194 if(oberon_is_const(arg))
4196 expr = oberon_make_char(ctx, toupper(arg -> item.integer));
4198 else
4200 expr = oberon_new_operator(OP_CAP, arg -> result, arg, NULL);
4203 return expr;
4206 static oberon_expr_t *
4207 oberon_make_chr_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4209 if(num_args < 1)
4211 oberon_error(ctx, "too few arguments");
4214 if(num_args > 1)
4216 oberon_error(ctx, "too mach arguments");
4219 oberon_expr_t * arg;
4220 arg = list_args;
4221 oberon_check_src(ctx, arg);
4223 if(!oberon_is_integer_type(arg -> result))
4225 oberon_error(ctx, "expected integer");
4228 oberon_expr_t * expr;
4229 if(oberon_is_const(arg))
4231 expr = oberon_make_char(ctx, arg -> item.integer);
4233 else
4235 expr = oberon_cast_expr(ctx, arg, ctx -> char_type);
4237 return expr;
4240 static oberon_expr_t *
4241 oberon_make_ord_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4243 if(num_args < 1)
4245 oberon_error(ctx, "too few arguments");
4248 if(num_args > 1)
4250 oberon_error(ctx, "too mach arguments");
4253 oberon_expr_t * arg;
4254 arg = list_args;
4255 oberon_check_src(ctx, arg);
4257 if(!oberon_is_char_type(arg -> result))
4259 oberon_error(ctx, "expected char");
4262 oberon_expr_t * expr;
4263 if(oberon_is_const(arg))
4265 expr = oberon_make_integer(ctx, arg -> item.integer);
4267 else
4269 expr = oberon_cast_expr(ctx, arg, ctx -> int_type);
4271 return expr;
4274 static oberon_expr_t *
4275 oberon_make_entier_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4277 if(num_args < 1)
4279 oberon_error(ctx, "too few arguments");
4282 if(num_args > 1)
4284 oberon_error(ctx, "too mach arguments");
4287 oberon_expr_t * arg;
4288 arg = list_args;
4289 oberon_check_src(ctx, arg);
4291 if(!oberon_is_real_type(arg -> result))
4293 oberon_error(ctx, "expected real");
4296 oberon_expr_t * expr;
4297 if(oberon_is_const(arg))
4299 expr = oberon_make_integer(ctx, floor(arg -> item.real));
4301 else
4303 expr = oberon_new_operator(OP_ENTIER, ctx -> int_type, arg, NULL);
4305 return expr;
4308 static oberon_expr_t *
4309 oberon_make_odd_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4311 if(num_args < 1)
4313 oberon_error(ctx, "too few arguments");
4316 if(num_args > 1)
4318 oberon_error(ctx, "too mach arguments");
4321 oberon_expr_t * arg;
4322 arg = list_args;
4323 oberon_check_src(ctx, arg);
4325 if(!oberon_is_integer_type(arg -> result))
4327 oberon_error(ctx, "expected integer");
4330 oberon_expr_t * expr;
4331 expr = oberon_make_bin_op(ctx, MOD, arg, oberon_make_integer(ctx, 2));
4332 expr = oberon_make_bin_op(ctx, EQUAL, expr, oberon_make_integer(ctx, 1));
4333 return expr;
4336 static oberon_expr_t *
4337 oberon_make_short_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4339 if(num_args < 1)
4341 oberon_error(ctx, "too few arguments");
4344 if(num_args > 1)
4346 oberon_error(ctx, "too mach arguments");
4349 oberon_expr_t * arg;
4350 arg = list_args;
4351 oberon_check_src(ctx, arg);
4353 if(arg -> result -> shorter == NULL)
4355 oberon_error(ctx, "already shorter");
4358 oberon_expr_t * expr;
4359 expr = oberon_cast_expr(ctx, arg, arg -> result -> shorter);
4360 return expr;
4363 static oberon_expr_t *
4364 oberon_make_long_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4366 if(num_args < 1)
4368 oberon_error(ctx, "too few arguments");
4371 if(num_args > 1)
4373 oberon_error(ctx, "too mach arguments");
4376 oberon_expr_t * arg;
4377 arg = list_args;
4378 oberon_check_src(ctx, arg);
4380 if(arg -> result -> longer == NULL)
4382 oberon_error(ctx, "already longer");
4385 oberon_expr_t * expr;
4386 expr = oberon_cast_expr(ctx, arg, arg -> result -> longer);
4387 return expr;
4390 static oberon_expr_t *
4391 oberon_make_len_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
4393 if(num_args < 1)
4395 oberon_error(ctx, "too few arguments");
4398 if(num_args > 2)
4400 oberon_error(ctx, "too mach arguments");
4403 oberon_expr_t * v;
4404 v = list_args;
4405 oberon_check_src(ctx, v);
4407 if(!oberon_is_array_type(v -> result))
4409 oberon_error(ctx, "expected array");
4412 int n = 0;
4413 if(num_args == 2)
4415 oberon_expr_t * num;
4416 num = list_args -> next;
4417 oberon_check_src(ctx, num);
4419 if(!oberon_is_integer_type(num -> result))
4421 oberon_error(ctx, "expected integer");
4423 oberon_check_const(ctx, num);
4425 n = num -> item.integer;
4428 int dim = 0;
4429 oberon_type_t * arr = v -> result;
4430 while(arr -> class == OBERON_TYPE_ARRAY)
4432 dim += 1;
4433 arr = arr -> base;
4436 if(n < 0 || n > dim)
4438 oberon_error(ctx, "not in range 0..%i", dim - 1);
4441 assert(v -> is_item);
4443 oberon_expr_t * expr;
4444 expr = oberon_new_item(MODE_LEN, ctx -> int_type, true);
4445 expr -> item.parent = (oberon_item_t *) v;
4446 expr -> item.integer = n;
4447 return expr;
4450 static void
4451 oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
4453 oberon_object_t * constant;
4454 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST, true, false, false);
4455 oberon_check_const(ctx, expr);
4456 constant -> value = (oberon_item_t *) expr;
4459 static void
4460 register_default_types(oberon_context_t * ctx)
4462 ctx -> notype_type = oberon_new_type_ptr(OBERON_TYPE_NOTYPE);
4463 oberon_generator_init_type(ctx, ctx -> notype_type);
4465 ctx -> nil_type = oberon_new_type_ptr(OBERON_TYPE_NIL);
4466 oberon_generator_init_type(ctx, ctx -> nil_type);
4468 ctx -> string_type = oberon_new_type_string(1);
4469 oberon_generator_init_type(ctx, ctx -> string_type);
4471 ctx -> bool_type = oberon_new_type_boolean();
4472 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
4474 ctx -> char_type = oberon_new_type_char(1);
4475 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
4477 ctx -> byte_type = oberon_new_type_integer(1);
4478 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> byte_type, 1);
4480 ctx -> shortint_type = oberon_new_type_integer(2);
4481 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> shortint_type, 1);
4483 ctx -> int_type = oberon_new_type_integer(4);
4484 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> int_type, 1);
4486 ctx -> longint_type = oberon_new_type_integer(8);
4487 oberon_define_type(ctx -> world_scope, "HUGEINT", ctx -> longint_type, 1);
4489 ctx -> real_type = oberon_new_type_real(4);
4490 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
4492 ctx -> longreal_type = oberon_new_type_real(8);
4493 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
4495 ctx -> set_type = oberon_new_type_set(4);
4496 oberon_define_type(ctx -> world_scope, "SET", ctx -> set_type, 1);
4500 ctx -> byte_type -> shorter = NULL;
4501 ctx -> byte_type -> longer = ctx -> shortint_type;
4503 ctx -> shortint_type -> shorter = ctx -> byte_type;
4504 ctx -> shortint_type -> longer = ctx -> int_type;
4506 ctx -> int_type -> shorter = ctx -> shortint_type;
4507 ctx -> int_type -> longer = ctx -> longint_type;
4509 ctx -> longint_type -> shorter = ctx -> int_type;
4510 ctx -> longint_type -> longer = NULL;
4512 ctx -> real_type -> shorter = NULL;
4513 ctx -> real_type -> longer = ctx -> longreal_type;
4515 ctx -> longreal_type -> shorter = ctx -> real_type;
4516 ctx -> longreal_type -> longer = NULL;
4519 static void
4520 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
4522 oberon_object_t * proc;
4523 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
4524 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
4525 proc -> type -> sysproc = true;
4526 proc -> type -> genfunc = f;
4527 proc -> type -> genproc = p;
4530 oberon_context_t *
4531 oberon_create_context(ModuleImportCallback import_module)
4533 oberon_context_t * ctx = GC_MALLOC(sizeof *ctx);
4534 memset(ctx, 0, sizeof *ctx);
4536 oberon_scope_t * world_scope;
4537 world_scope = oberon_open_scope(ctx);
4538 ctx -> world_scope = world_scope;
4540 ctx -> import_module = import_module;
4542 oberon_generator_init_context(ctx);
4544 register_default_types(ctx);
4546 /* Constants */
4547 oberon_new_const(ctx, "TRUE", oberon_make_boolean(ctx, true));
4548 oberon_new_const(ctx, "FALSE", oberon_make_boolean(ctx, false));
4550 /* Functions */
4551 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
4552 oberon_new_intrinsic(ctx, "ASH", oberon_make_ash_call, NULL);
4553 oberon_new_intrinsic(ctx, "CAP", oberon_make_cap_call, NULL);
4554 oberon_new_intrinsic(ctx, "CHR", oberon_make_chr_call, NULL);
4555 oberon_new_intrinsic(ctx, "ENTIER", oberon_make_entier_call, NULL);
4556 oberon_new_intrinsic(ctx, "LEN", oberon_make_len_call, NULL);
4557 oberon_new_intrinsic(ctx, "LONG", oberon_make_long_call, NULL);
4558 oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
4559 oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
4560 oberon_new_intrinsic(ctx, "ODD", oberon_make_odd_call, NULL);
4561 oberon_new_intrinsic(ctx, "ORD", oberon_make_ord_call, NULL);
4562 oberon_new_intrinsic(ctx, "SHORT", oberon_make_short_call, NULL);
4563 oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
4565 /* Procedures */
4566 oberon_new_intrinsic(ctx, "ASSERT", NULL, oberon_make_assert_call);
4567 oberon_new_intrinsic(ctx, "COPY", NULL, oberon_make_copy_call);
4568 oberon_new_intrinsic(ctx, "DEC", NULL, oberon_make_dec_call);
4569 oberon_new_intrinsic(ctx, "EXCL", NULL, oberon_make_excl_call);
4570 oberon_new_intrinsic(ctx, "HALT", NULL, oberon_make_halt_call);
4571 oberon_new_intrinsic(ctx, "INC", NULL, oberon_make_inc_call);
4572 oberon_new_intrinsic(ctx, "INCL", NULL, oberon_make_incl_call);
4573 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
4575 return ctx;
4578 void
4579 oberon_destroy_context(oberon_context_t * ctx)
4581 oberon_generator_destroy_context(ctx);
4584 oberon_module_t *
4585 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
4587 const char * code = ctx -> code;
4588 int code_index = ctx -> code_index;
4589 char c = ctx -> c;
4590 int token = ctx -> token;
4591 char * string = ctx -> string;
4592 int integer = ctx -> integer;
4593 int real = ctx -> real;
4594 bool longmode = ctx -> longmode;
4595 oberon_scope_t * decl = ctx -> decl;
4596 oberon_module_t * mod = ctx -> mod;
4598 oberon_scope_t * module_scope;
4599 module_scope = oberon_open_scope(ctx);
4601 oberon_module_t * module;
4602 module = GC_MALLOC(sizeof *module);
4603 memset(module, 0, sizeof *module);
4604 module -> decl = module_scope;
4605 module -> next = ctx -> module_list;
4607 ctx -> mod = module;
4608 ctx -> module_list = module;
4610 oberon_init_scaner(ctx, newcode);
4611 oberon_parse_module(ctx);
4613 module -> ready = 1;
4615 ctx -> code = code;
4616 ctx -> code_index = code_index;
4617 ctx -> c = c;
4618 ctx -> token = token;
4619 ctx -> string = string;
4620 ctx -> integer = integer;
4621 ctx -> real = real;
4622 ctx -> longmode = longmode;
4623 ctx -> decl = decl;
4624 ctx -> mod = mod;
4626 return module;