DEADSOFTWARE

TRUE и FALSE теперь определены как константы
[dsw-obn.git] / src / oberon.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdbool.h>
8 #include <math.h>
10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
15 enum {
16 EOF_ = 0,
17 IDENT,
18 MODULE,
19 SEMICOLON,
20 END,
21 DOT,
22 VAR,
23 COLON,
24 BEGIN,
25 ASSIGN,
26 INTEGER,
27 LPAREN,
28 RPAREN,
29 EQUAL,
30 NEQ,
31 LESS,
32 LEQ,
33 GREAT,
34 GEQ,
35 IN,
36 IS,
37 PLUS,
38 MINUS,
39 OR,
40 STAR,
41 SLASH,
42 DIV,
43 MOD,
44 AND,
45 NOT,
46 PROCEDURE,
47 COMMA,
48 RETURN,
49 CONST,
50 TYPE,
51 ARRAY,
52 OF,
53 LBRACK,
54 RBRACK,
55 RECORD,
56 POINTER,
57 TO,
58 UPARROW,
59 NIL,
60 IMPORT,
61 REAL,
62 CHAR,
63 STRING,
64 IF,
65 THEN,
66 ELSE,
67 ELSIF,
68 WHILE,
69 DO,
70 REPEAT,
71 UNTIL,
72 FOR,
73 BY,
74 LOOP,
75 EXIT,
76 LBRACE,
77 RBRACE,
78 DOTDOT,
79 CASE,
80 BAR,
81 WITH
82 };
84 // =======================================================================
85 // UTILS
86 // =======================================================================
88 static void
89 oberon_error(oberon_context_t * ctx, const char * fmt, ...)
90 {
91 va_list ptr;
92 va_start(ptr, fmt);
93 fprintf(stderr, "error: ");
94 vfprintf(stderr, fmt, ptr);
95 fprintf(stderr, "\n");
96 fprintf(stderr, " code_index = %i\n", ctx -> code_index);
97 fprintf(stderr, " c = %c\n", ctx -> c);
98 fprintf(stderr, " token = %i\n", ctx -> token);
99 va_end(ptr);
100 exit(1);
103 static oberon_type_t *
104 oberon_new_type_ptr(int class)
106 oberon_type_t * x = malloc(sizeof *x);
107 memset(x, 0, sizeof *x);
108 x -> class = class;
109 return x;
112 static oberon_type_t *
113 oberon_new_type_integer(int size)
115 oberon_type_t * x;
116 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
117 x -> size = size;
118 return x;
121 static oberon_type_t *
122 oberon_new_type_boolean()
124 oberon_type_t * x;
125 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
126 return x;
129 static oberon_type_t *
130 oberon_new_type_real(int size)
132 oberon_type_t * x;
133 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
134 x -> size = size;
135 return x;
138 static oberon_type_t *
139 oberon_new_type_char(int size)
141 oberon_type_t * x;
142 x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
143 x -> size = size;
144 return x;
147 static oberon_type_t *
148 oberon_new_type_string(int size)
150 oberon_type_t * x;
151 x = oberon_new_type_ptr(OBERON_TYPE_STRING);
152 x -> size = size;
153 return x;
156 static oberon_type_t *
157 oberon_new_type_set(int size)
159 oberon_type_t * x;
160 x = oberon_new_type_ptr(OBERON_TYPE_SET);
161 x -> size = size;
162 return x;
165 // =======================================================================
166 // TABLE
167 // =======================================================================
169 static oberon_scope_t *
170 oberon_open_scope(oberon_context_t * ctx)
172 oberon_scope_t * scope = calloc(1, sizeof *scope);
173 oberon_object_t * list = calloc(1, sizeof *list);
175 scope -> ctx = ctx;
176 scope -> list = list;
177 scope -> up = ctx -> decl;
179 if(scope -> up)
181 scope -> local = scope -> up -> local;
182 scope -> parent = scope -> up -> parent;
183 scope -> parent_type = scope -> up -> parent_type;
184 scope -> exit_label = scope -> up -> exit_label;
187 ctx -> decl = scope;
188 return scope;
191 static void
192 oberon_close_scope(oberon_scope_t * scope)
194 oberon_context_t * ctx = scope -> ctx;
195 ctx -> decl = scope -> up;
198 static oberon_object_t *
199 oberon_find_object_in_list(oberon_object_t * list, char * name)
201 oberon_object_t * x = list;
202 while(x -> next && strcmp(x -> next -> name, name) != 0)
204 x = x -> next;
206 return x -> next;
209 static oberon_object_t *
210 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
212 oberon_object_t * result = NULL;
214 oberon_scope_t * s = scope;
215 while(result == NULL && s != NULL)
217 result = oberon_find_object_in_list(s -> list, name);
218 s = s -> up;
221 if(check_it && result == NULL)
223 oberon_error(scope -> ctx, "undefined ident %s", name);
226 return result;
229 static oberon_object_t *
230 oberon_create_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only)
232 oberon_object_t * newvar = malloc(sizeof *newvar);
233 memset(newvar, 0, sizeof *newvar);
234 newvar -> name = name;
235 newvar -> class = class;
236 newvar -> export = export;
237 newvar -> read_only = read_only;
238 newvar -> local = scope -> local;
239 newvar -> parent = scope -> parent;
240 newvar -> parent_type = scope -> parent_type;
241 newvar -> module = scope -> ctx -> mod;
242 return newvar;
245 static oberon_object_t *
246 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
248 if(check_upscope)
250 if(oberon_find_object(scope -> up, name, false))
252 oberon_error(scope -> ctx, "already defined");
256 oberon_object_t * x = scope -> list;
257 while(x -> next && strcmp(x -> next -> name, name) != 0)
259 x = x -> next;
262 if(x -> next)
264 oberon_error(scope -> ctx, "already defined");
267 oberon_object_t * newvar;
268 newvar = oberon_create_object(scope, name, class, export, read_only);
269 x -> next = newvar;
271 return newvar;
274 static oberon_object_t *
275 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
277 oberon_object_t * id;
278 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
279 id -> type = type;
280 oberon_generator_init_type(scope -> ctx, type);
281 return id;
284 // =======================================================================
285 // SCANER
286 // =======================================================================
288 static void
289 oberon_get_char(oberon_context_t * ctx)
291 if(ctx -> code[ctx -> code_index])
293 ctx -> code_index += 1;
294 ctx -> c = ctx -> code[ctx -> code_index];
298 static void
299 oberon_init_scaner(oberon_context_t * ctx, const char * code)
301 ctx -> code = code;
302 ctx -> code_index = 0;
303 ctx -> c = ctx -> code[ctx -> code_index];
306 static void
307 oberon_read_ident(oberon_context_t * ctx)
309 int len = 0;
310 int i = ctx -> code_index;
312 int c = ctx -> code[i];
313 while(isalnum(c))
315 i += 1;
316 len += 1;
317 c = ctx -> code[i];
320 char * ident = malloc(len + 1);
321 memcpy(ident, &ctx->code[ctx->code_index], len);
322 ident[len] = 0;
324 ctx -> code_index = i;
325 ctx -> c = ctx -> code[i];
326 ctx -> string = ident;
327 ctx -> token = IDENT;
329 if(strcmp(ident, "MODULE") == 0)
331 ctx -> token = MODULE;
333 else if(strcmp(ident, "END") == 0)
335 ctx -> token = END;
337 else if(strcmp(ident, "VAR") == 0)
339 ctx -> token = VAR;
341 else if(strcmp(ident, "BEGIN") == 0)
343 ctx -> token = BEGIN;
345 else if(strcmp(ident, "OR") == 0)
347 ctx -> token = OR;
349 else if(strcmp(ident, "DIV") == 0)
351 ctx -> token = DIV;
353 else if(strcmp(ident, "MOD") == 0)
355 ctx -> token = MOD;
357 else if(strcmp(ident, "PROCEDURE") == 0)
359 ctx -> token = PROCEDURE;
361 else if(strcmp(ident, "RETURN") == 0)
363 ctx -> token = RETURN;
365 else if(strcmp(ident, "CONST") == 0)
367 ctx -> token = CONST;
369 else if(strcmp(ident, "TYPE") == 0)
371 ctx -> token = TYPE;
373 else if(strcmp(ident, "ARRAY") == 0)
375 ctx -> token = ARRAY;
377 else if(strcmp(ident, "OF") == 0)
379 ctx -> token = OF;
381 else if(strcmp(ident, "RECORD") == 0)
383 ctx -> token = RECORD;
385 else if(strcmp(ident, "POINTER") == 0)
387 ctx -> token = POINTER;
389 else if(strcmp(ident, "TO") == 0)
391 ctx -> token = TO;
393 else if(strcmp(ident, "NIL") == 0)
395 ctx -> token = NIL;
397 else if(strcmp(ident, "IMPORT") == 0)
399 ctx -> token = IMPORT;
401 else if(strcmp(ident, "IN") == 0)
403 ctx -> token = IN;
405 else if(strcmp(ident, "IS") == 0)
407 ctx -> token = IS;
409 else if(strcmp(ident, "IF") == 0)
411 ctx -> token = IF;
413 else if(strcmp(ident, "THEN") == 0)
415 ctx -> token = THEN;
417 else if(strcmp(ident, "ELSE") == 0)
419 ctx -> token = ELSE;
421 else if(strcmp(ident, "ELSIF") == 0)
423 ctx -> token = ELSIF;
425 else if(strcmp(ident, "WHILE") == 0)
427 ctx -> token = WHILE;
429 else if(strcmp(ident, "DO") == 0)
431 ctx -> token = DO;
433 else if(strcmp(ident, "REPEAT") == 0)
435 ctx -> token = REPEAT;
437 else if(strcmp(ident, "UNTIL") == 0)
439 ctx -> token = UNTIL;
441 else if(strcmp(ident, "FOR") == 0)
443 ctx -> token = FOR;
445 else if(strcmp(ident, "BY") == 0)
447 ctx -> token = BY;
449 else if(strcmp(ident, "LOOP") == 0)
451 ctx -> token = LOOP;
453 else if(strcmp(ident, "EXIT") == 0)
455 ctx -> token = EXIT;
457 else if(strcmp(ident, "CASE") == 0)
459 ctx -> token = CASE;
461 else if(strcmp(ident, "WITH") == 0)
463 ctx -> token = WITH;
467 #define ISHEXDIGIT(x) \
468 (((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F'))
470 static void
471 oberon_read_number(oberon_context_t * ctx)
473 long integer;
474 double real;
475 char * ident;
476 int start_i;
477 int exp_i;
478 int end_i;
480 /*
481 * mode = 0 == DEC
482 * mode = 1 == HEX
483 * mode = 2 == REAL
484 * mode = 3 == LONGREAL
485 * mode = 4 == CHAR
486 */
487 int mode = 0;
488 start_i = ctx -> code_index;
490 while(isdigit(ctx -> c))
492 oberon_get_char(ctx);
495 end_i = ctx -> code_index;
497 if(ISHEXDIGIT(ctx -> c))
499 mode = 1;
500 while(ISHEXDIGIT(ctx -> c))
502 oberon_get_char(ctx);
505 end_i = ctx -> code_index;
507 if(ctx -> c == 'H')
509 mode = 1;
510 oberon_get_char(ctx);
512 else if(ctx -> c == 'X')
514 mode = 4;
515 oberon_get_char(ctx);
517 else
519 oberon_error(ctx, "invalid hex number");
522 else if(ctx -> c == '.')
524 oberon_get_char(ctx);
525 if(ctx -> c == '.')
527 /* Чит: избегаем конфликта с DOTDOT */
528 ctx -> code_index -= 1;
530 else
532 mode = 2;
534 while(isdigit(ctx -> c))
536 oberon_get_char(ctx);
539 if(ctx -> c == 'E' || ctx -> c == 'D')
541 exp_i = ctx -> code_index;
543 if(ctx -> c == 'D')
545 mode = 3;
548 oberon_get_char(ctx);
550 if(ctx -> c == '+' || ctx -> c == '-')
552 oberon_get_char(ctx);
555 while(isdigit(ctx -> c))
557 oberon_get_char(ctx);
558 }
561 end_i = ctx -> code_index;
564 if(mode == 0)
566 if(ctx -> c == 'H')
568 mode = 1;
569 oberon_get_char(ctx);
571 else if(ctx -> c == 'X')
573 mode = 4;
574 oberon_get_char(ctx);
578 int len = end_i - start_i;
579 ident = malloc(len + 1);
580 memcpy(ident, &ctx -> code[start_i], len);
581 ident[len] = 0;
583 ctx -> longmode = false;
584 if(mode == 3)
586 int i = exp_i - start_i;
587 ident[i] = 'E';
588 ctx -> longmode = true;
591 switch(mode)
593 case 0:
594 integer = atol(ident);
595 real = integer;
596 ctx -> token = INTEGER;
597 break;
598 case 1:
599 sscanf(ident, "%lx", &integer);
600 real = integer;
601 ctx -> token = INTEGER;
602 break;
603 case 2:
604 case 3:
605 sscanf(ident, "%lf", &real);
606 ctx -> token = REAL;
607 break;
608 case 4:
609 sscanf(ident, "%lx", &integer);
610 real = integer;
611 ctx -> token = CHAR;
612 break;
613 default:
614 oberon_error(ctx, "oberon_read_number: wat");
615 break;
618 ctx -> string = ident;
619 ctx -> integer = integer;
620 ctx -> real = real;
623 static void
624 oberon_skip_space(oberon_context_t * ctx)
626 while(isspace(ctx -> c))
628 oberon_get_char(ctx);
632 static void
633 oberon_read_comment(oberon_context_t * ctx)
635 int nesting = 1;
636 while(nesting >= 1)
638 if(ctx -> c == '(')
640 oberon_get_char(ctx);
641 if(ctx -> c == '*')
643 oberon_get_char(ctx);
644 nesting += 1;
647 else if(ctx -> c == '*')
649 oberon_get_char(ctx);
650 if(ctx -> c == ')')
652 oberon_get_char(ctx);
653 nesting -= 1;
656 else if(ctx -> c == 0)
658 oberon_error(ctx, "unterminated comment");
660 else
662 oberon_get_char(ctx);
667 static void oberon_read_string(oberon_context_t * ctx)
669 int c = ctx -> c;
670 oberon_get_char(ctx);
672 int start = ctx -> code_index;
674 while(ctx -> c != 0 && ctx -> c != c)
676 oberon_get_char(ctx);
679 if(ctx -> c == 0)
681 oberon_error(ctx, "unterminated string");
684 int end = ctx -> code_index;
686 oberon_get_char(ctx);
688 char * string = calloc(1, end - start + 1);
689 strncpy(string, &ctx -> code[start], end - start);
691 ctx -> token = STRING;
692 ctx -> string = string;
694 printf("oberon_read_string: string ((%s))\n", string);
697 static void oberon_read_token(oberon_context_t * ctx);
699 static void
700 oberon_read_symbol(oberon_context_t * ctx)
702 int c = ctx -> c;
703 switch(c)
705 case 0:
706 ctx -> token = EOF_;
707 break;
708 case ';':
709 ctx -> token = SEMICOLON;
710 oberon_get_char(ctx);
711 break;
712 case ':':
713 ctx -> token = COLON;
714 oberon_get_char(ctx);
715 if(ctx -> c == '=')
717 ctx -> token = ASSIGN;
718 oberon_get_char(ctx);
720 break;
721 case '.':
722 ctx -> token = DOT;
723 oberon_get_char(ctx);
724 if(ctx -> c == '.')
726 ctx -> token = DOTDOT;
727 oberon_get_char(ctx);
729 break;
730 case '(':
731 ctx -> token = LPAREN;
732 oberon_get_char(ctx);
733 if(ctx -> c == '*')
735 oberon_get_char(ctx);
736 oberon_read_comment(ctx);
737 oberon_read_token(ctx);
739 break;
740 case ')':
741 ctx -> token = RPAREN;
742 oberon_get_char(ctx);
743 break;
744 case '=':
745 ctx -> token = EQUAL;
746 oberon_get_char(ctx);
747 break;
748 case '#':
749 ctx -> token = NEQ;
750 oberon_get_char(ctx);
751 break;
752 case '<':
753 ctx -> token = LESS;
754 oberon_get_char(ctx);
755 if(ctx -> c == '=')
757 ctx -> token = LEQ;
758 oberon_get_char(ctx);
760 break;
761 case '>':
762 ctx -> token = GREAT;
763 oberon_get_char(ctx);
764 if(ctx -> c == '=')
766 ctx -> token = GEQ;
767 oberon_get_char(ctx);
769 break;
770 case '+':
771 ctx -> token = PLUS;
772 oberon_get_char(ctx);
773 break;
774 case '-':
775 ctx -> token = MINUS;
776 oberon_get_char(ctx);
777 break;
778 case '*':
779 ctx -> token = STAR;
780 oberon_get_char(ctx);
781 if(ctx -> c == ')')
783 oberon_get_char(ctx);
784 oberon_error(ctx, "unstarted comment");
786 break;
787 case '/':
788 ctx -> token = SLASH;
789 oberon_get_char(ctx);
790 break;
791 case '&':
792 ctx -> token = AND;
793 oberon_get_char(ctx);
794 break;
795 case '~':
796 ctx -> token = NOT;
797 oberon_get_char(ctx);
798 break;
799 case ',':
800 ctx -> token = COMMA;
801 oberon_get_char(ctx);
802 break;
803 case '[':
804 ctx -> token = LBRACK;
805 oberon_get_char(ctx);
806 break;
807 case ']':
808 ctx -> token = RBRACK;
809 oberon_get_char(ctx);
810 break;
811 case '^':
812 ctx -> token = UPARROW;
813 oberon_get_char(ctx);
814 break;
815 case '"':
816 oberon_read_string(ctx);
817 break;
818 case '\'':
819 oberon_read_string(ctx);
820 break;
821 case '{':
822 ctx -> token = LBRACE;
823 oberon_get_char(ctx);
824 break;
825 case '}':
826 ctx -> token = RBRACE;
827 oberon_get_char(ctx);
828 break;
829 case '|':
830 ctx -> token = BAR;
831 oberon_get_char(ctx);
832 break;
833 default:
834 oberon_error(ctx, "invalid char %c", ctx -> c);
835 break;
839 static void
840 oberon_read_token(oberon_context_t * ctx)
842 oberon_skip_space(ctx);
844 int c = ctx -> c;
845 if(isalpha(c))
847 oberon_read_ident(ctx);
849 else if(isdigit(c))
851 oberon_read_number(ctx);
853 else
855 oberon_read_symbol(ctx);
859 // =======================================================================
860 // EXPRESSION
861 // =======================================================================
863 static void oberon_expect_token(oberon_context_t * ctx, int token);
864 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
865 static void oberon_assert_token(oberon_context_t * ctx, int token);
866 static char * oberon_assert_ident(oberon_context_t * ctx);
867 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
868 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
869 static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
871 static oberon_expr_t *
872 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
874 oberon_oper_t * operator;
875 operator = malloc(sizeof *operator);
876 memset(operator, 0, sizeof *operator);
878 operator -> is_item = 0;
879 operator -> result = result;
880 operator -> read_only = 1;
881 operator -> op = op;
882 operator -> left = left;
883 operator -> right = right;
885 return (oberon_expr_t *) operator;
888 static oberon_expr_t *
889 oberon_new_item(int mode, oberon_type_t * result, int read_only)
891 oberon_item_t * item;
892 item = malloc(sizeof *item);
893 memset(item, 0, sizeof *item);
895 item -> is_item = 1;
896 item -> result = result;
897 item -> read_only = read_only;
898 item -> mode = mode;
900 return (oberon_expr_t *)item;
903 static oberon_expr_t *
904 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
906 oberon_expr_t * expr;
907 oberon_type_t * result;
909 result = a -> result;
911 if(token == MINUS)
913 if(result -> class == OBERON_TYPE_SET)
915 expr = oberon_new_operator(OP_COMPLEMENTATION, result, a, NULL);
917 else if(result -> class == OBERON_TYPE_INTEGER)
919 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
921 else
923 oberon_error(ctx, "incompatible operator type");
926 else if(token == NOT)
928 if(result -> class != OBERON_TYPE_BOOLEAN)
930 oberon_error(ctx, "incompatible operator type");
933 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
935 else
937 oberon_error(ctx, "oberon_make_unary_op: wat");
940 return expr;
943 static void
944 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
946 oberon_expr_t * last;
948 *num_expr = 1;
949 if(const_expr)
951 *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
953 else
955 *first = last = oberon_expr(ctx);
957 while(ctx -> token == COMMA)
959 oberon_assert_token(ctx, COMMA);
960 oberon_expr_t * current;
962 if(const_expr)
964 current = (oberon_expr_t *) oberon_const_expr(ctx);
966 else
968 current = oberon_expr(ctx);
971 last -> next = current;
972 last = current;
973 *num_expr += 1;
977 static oberon_expr_t *
978 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
980 return oberon_new_operator(OP_CAST, pref, expr, NULL);
983 static oberon_expr_t *
984 oberno_make_record_cast(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * rec)
986 oberon_type_t * from = expr -> result;
987 oberon_type_t * to = rec;
989 printf("oberno_make_record_cast: from class %i to class %i\n", from -> class, to -> class);
991 if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
993 printf("oberno_make_record_cast: pointers\n");
994 from = from -> base;
995 to = to -> base;
998 if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
1000 oberon_error(ctx, "must be record type");
1003 return oberon_cast_expr(ctx, expr, rec);
1006 static oberon_type_t *
1007 oberon_get_equal_expr_type(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
1009 oberon_type_t * result;
1010 if(a -> class == OBERON_TYPE_REAL && b -> class == OBERON_TYPE_INTEGER)
1012 result = a;
1014 else if(b -> class == OBERON_TYPE_REAL && a -> class == OBERON_TYPE_INTEGER)
1016 result = b;
1018 else if(a -> class != b -> class)
1020 oberon_error(ctx, "oberon_get_equal_expr_type: incompatible types");
1022 else if(a -> size > b -> size)
1024 result = a;
1026 else
1028 result = b;
1031 return result;
1034 static void
1035 oberon_check_record_compatibility(oberon_context_t * ctx, oberon_type_t * from, oberon_type_t * to)
1037 if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
1039 from = from -> base;
1040 to = to -> base;
1043 if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
1045 oberon_error(ctx, "not a record");
1048 oberon_type_t * t = from;
1049 while(t != NULL && t != to)
1051 t = t -> base;
1054 if(t == NULL)
1056 oberon_error(ctx, "incompatible record types");
1060 static void
1061 oberon_check_dst(oberon_context_t * ctx, oberon_expr_t * dst)
1063 if(dst -> is_item == false)
1065 oberon_error(ctx, "not variable");
1068 switch(dst -> item.mode)
1070 case MODE_VAR:
1071 case MODE_CALL:
1072 case MODE_INDEX:
1073 case MODE_FIELD:
1074 case MODE_DEREF:
1075 case MODE_NEW:
1076 /* accept */
1077 break;
1078 default:
1079 oberon_error(ctx, "not variable");
1080 break;
1084 static void
1085 oberon_check_src(oberon_context_t * ctx, oberon_expr_t * src)
1087 if(src -> is_item)
1089 if(src -> item.mode == MODE_TYPE)
1091 oberon_error(ctx, "not variable");
1096 static oberon_expr_t *
1097 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
1099 // Допускается:
1100 // Если классы типов равны
1101 // Если INTEGER переводится в REAL
1102 // Есди STRING переводится в CHAR
1103 // Есди STRING переводится в ARRAY OF CHAR
1105 oberon_check_src(ctx, expr);
1107 bool error = false;
1108 if(pref -> class != expr -> result -> class)
1110 printf("expr class %i\n", expr -> result -> class);
1111 printf("pref class %i\n", pref -> class);
1113 if(expr -> result -> class == OBERON_TYPE_STRING)
1115 if(pref -> class == OBERON_TYPE_CHAR)
1117 if(expr -> is_item && expr -> item.mode == MODE_STRING)
1119 if(strlen(expr -> item.string) != 1)
1121 error = true;
1124 else
1126 error = true;
1129 else if(pref -> class == OBERON_TYPE_ARRAY)
1131 if(pref -> base -> class != OBERON_TYPE_CHAR)
1133 error = true;
1136 else
1138 error = true;
1141 else if(expr -> result -> class == OBERON_TYPE_INTEGER)
1143 if(pref -> class != OBERON_TYPE_REAL)
1145 error = true;
1148 else
1150 error = true;
1154 if(error)
1156 oberon_error(ctx, "oberon_autocast_to: incompatible types");
1159 if(pref -> class == OBERON_TYPE_CHAR)
1161 if(expr -> result -> class == OBERON_TYPE_STRING)
1163 int c = expr -> item.string[0];
1164 expr = oberon_new_item(MODE_CHAR, ctx -> char_type, true);
1165 expr -> item.integer = c;
1168 else if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL)
1170 if(expr -> result -> size > pref -> size)
1172 oberon_error(ctx, "incompatible size");
1174 else
1176 expr = oberon_cast_expr(ctx, expr, pref);
1179 else if(pref -> class == OBERON_TYPE_RECORD)
1181 oberon_check_record_compatibility(ctx, expr -> result, pref);
1182 expr = oberno_make_record_cast(ctx, expr, pref);
1184 else if(pref -> class == OBERON_TYPE_POINTER)
1186 assert(pref -> base);
1187 if(expr -> result -> base -> class == OBERON_TYPE_RECORD)
1189 oberon_check_record_compatibility(ctx, expr -> result, pref);
1190 expr = oberno_make_record_cast(ctx, expr, pref);
1192 else if(expr -> result -> base != pref -> base)
1194 if(expr -> result -> base -> class != OBERON_TYPE_VOID)
1196 oberon_error(ctx, "incompatible pointer types");
1201 return expr;
1204 static void
1205 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_expr_t ** ea, oberon_expr_t ** eb)
1207 oberon_type_t * a = (*ea) -> result;
1208 oberon_type_t * b = (*eb) -> result;
1209 oberon_type_t * preq = oberon_get_equal_expr_type(ctx, a, b);
1210 *ea = oberon_autocast_to(ctx, *ea, preq);
1211 *eb = oberon_autocast_to(ctx, *eb, preq);
1214 static void
1215 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
1217 if(desig -> mode != MODE_CALL)
1219 oberon_error(ctx, "expected mode CALL");
1222 oberon_type_t * fn = desig -> parent -> result;
1223 int num_args = desig -> num_args;
1224 int num_decl = fn -> num_decl;
1226 if(num_args < num_decl)
1228 oberon_error(ctx, "too few arguments");
1230 else if(num_args > num_decl)
1232 oberon_error(ctx, "too many arguments");
1235 /* Делаем проверку на запись и делаем автокаст */
1236 oberon_expr_t * casted[num_args];
1237 oberon_expr_t * arg = desig -> args;
1238 oberon_object_t * param = fn -> decl;
1239 for(int i = 0; i < num_args; i++)
1241 if(param -> class == OBERON_CLASS_VAR_PARAM)
1243 if(arg -> result != param -> type)
1245 oberon_error(ctx, "incompatible type");
1247 if(arg -> read_only)
1249 oberon_error(ctx, "assign to read-only var");
1251 casted[i] = arg;
1253 else
1255 casted[i] = oberon_autocast_to(ctx, arg, param -> type);
1258 arg = arg -> next;
1259 param = param -> next;
1262 /* Создаём новый список выражений */
1263 if(num_args > 0)
1265 arg = casted[0];
1266 for(int i = 0; i < num_args - 1; i++)
1268 casted[i] -> next = casted[i + 1];
1270 desig -> args = arg;
1274 static oberon_expr_t *
1275 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1277 oberon_type_t * signature = item -> result;
1278 if(signature -> class != OBERON_TYPE_PROCEDURE)
1280 oberon_error(ctx, "not a procedure");
1283 oberon_expr_t * call;
1285 if(signature -> sysproc)
1287 if(signature -> genfunc == NULL)
1289 oberon_error(ctx, "not a function-procedure");
1292 call = signature -> genfunc(ctx, num_args, list_args);
1294 else
1296 if(signature -> base -> class == OBERON_TYPE_VOID)
1298 oberon_error(ctx, "attempt to call procedure in expression");
1301 call = oberon_new_item(MODE_CALL, signature -> base, true);
1302 call -> item.parent = item;
1303 call -> item.num_args = num_args;
1304 call -> item.args = list_args;
1305 oberon_autocast_call(ctx, (oberon_item_t *) call);
1308 return call;
1311 static void
1312 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1314 oberon_type_t * signature = item -> result;
1315 if(signature -> class != OBERON_TYPE_PROCEDURE)
1317 oberon_error(ctx, "not a procedure");
1320 oberon_expr_t * call;
1322 if(signature -> sysproc)
1324 if(signature -> genproc == NULL)
1326 oberon_error(ctx, "not a procedure");
1329 signature -> genproc(ctx, num_args, list_args);
1331 else
1333 if(signature -> base -> class != OBERON_TYPE_VOID)
1335 oberon_error(ctx, "attempt to call function as non-typed procedure");
1338 call = oberon_new_item(MODE_CALL, signature -> base, true);
1339 call -> item.parent = item;
1340 call -> item.num_args = num_args;
1341 call -> item.args = list_args;
1342 oberon_autocast_call(ctx, (oberon_item_t *) call);
1343 oberon_generate_call_proc(ctx, call);
1347 #define ISEXPR(x) \
1348 (((x) == PLUS) \
1349 || ((x) == MINUS) \
1350 || ((x) == IDENT) \
1351 || ((x) == INTEGER) \
1352 || ((x) == REAL) \
1353 || ((x) == CHAR) \
1354 || ((x) == STRING) \
1355 || ((x) == NIL) \
1356 || ((x) == LPAREN) \
1357 || ((x) == NOT))
1359 static oberon_expr_t *
1360 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1362 printf("oberno_make_dereferencing\n");
1363 if(expr -> result -> class != OBERON_TYPE_POINTER)
1365 oberon_error(ctx, "not a pointer");
1368 assert(expr -> is_item);
1370 oberon_expr_t * selector;
1371 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
1372 selector -> item.parent = (oberon_item_t *) expr;
1374 return selector;
1377 static oberon_expr_t *
1378 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1380 if(desig -> result -> class == OBERON_TYPE_POINTER)
1382 desig = oberno_make_dereferencing(ctx, desig);
1385 assert(desig -> is_item);
1387 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1389 oberon_error(ctx, "not array");
1392 oberon_type_t * base;
1393 base = desig -> result -> base;
1395 if(index -> result -> class != OBERON_TYPE_INTEGER)
1397 oberon_error(ctx, "index must be integer");
1400 // Статическая проверка границ массива
1401 if(desig -> result -> size != 0)
1403 if(index -> is_item)
1405 if(index -> item.mode == MODE_INTEGER)
1407 int arr_size = desig -> result -> size;
1408 int index_int = index -> item.integer;
1409 if(index_int < 0 || index_int > arr_size - 1)
1411 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1417 oberon_expr_t * selector;
1418 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1419 selector -> item.parent = (oberon_item_t *) desig;
1420 selector -> item.num_args = 1;
1421 selector -> item.args = index;
1423 return selector;
1426 static oberon_expr_t *
1427 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1429 if(expr -> result -> class == OBERON_TYPE_POINTER)
1431 expr = oberno_make_dereferencing(ctx, expr);
1434 assert(expr -> is_item);
1436 if(expr -> result -> class != OBERON_TYPE_RECORD)
1438 oberon_error(ctx, "not record");
1441 oberon_type_t * rec = expr -> result;
1443 oberon_object_t * field;
1444 field = oberon_find_object(rec -> scope, name, true);
1446 if(field -> export == 0)
1448 if(field -> module != ctx -> mod)
1450 oberon_error(ctx, "field not exported");
1454 int read_only = 0;
1455 if(field -> read_only)
1457 if(field -> module != ctx -> mod)
1459 read_only = 1;
1463 oberon_expr_t * selector;
1464 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1465 selector -> item.var = field;
1466 selector -> item.parent = (oberon_item_t *) expr;
1468 return selector;
1471 #define ISSELECTOR(x) \
1472 (((x) == LBRACK) \
1473 || ((x) == DOT) \
1474 || ((x) == UPARROW) \
1475 || ((x) == LPAREN))
1477 static oberon_object_t *
1478 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1480 char * name;
1481 oberon_object_t * x;
1483 name = oberon_assert_ident(ctx);
1484 x = oberon_find_object(ctx -> decl, name, check);
1486 if(x != NULL)
1488 if(x -> class == OBERON_CLASS_MODULE)
1490 oberon_assert_token(ctx, DOT);
1491 name = oberon_assert_ident(ctx);
1492 /* Наличие объектов в левых модулях всегда проверяется */
1493 x = oberon_find_object(x -> module -> decl, name, 1);
1495 if(x -> export == 0)
1497 oberon_error(ctx, "not exported");
1502 if(xname)
1504 *xname = name;
1507 return x;
1510 static oberon_expr_t *
1511 oberon_ident_item(oberon_context_t * ctx, char * name)
1513 bool read_only;
1514 oberon_object_t * x;
1515 oberon_expr_t * expr;
1517 x = oberon_find_object(ctx -> decl, name, true);
1519 read_only = false;
1520 if(x -> class == OBERON_CLASS_CONST || x -> class == OBERON_CLASS_PROC)
1522 read_only = true;
1525 expr = oberon_new_item(MODE_VAR, x -> type, read_only);
1526 expr -> item.var = x;
1527 return expr;
1530 static oberon_expr_t *
1531 oberon_qualident_expr(oberon_context_t * ctx)
1533 oberon_object_t * var;
1534 oberon_expr_t * expr;
1536 var = oberon_qualident(ctx, NULL, 1);
1538 int read_only = 0;
1539 if(var -> read_only)
1541 if(var -> module != ctx -> mod)
1543 read_only = 1;
1547 switch(var -> class)
1549 case OBERON_CLASS_CONST:
1550 // TODO copy value
1551 expr = (oberon_expr_t *) var -> value;
1552 break;
1553 case OBERON_CLASS_TYPE:
1554 expr = oberon_new_item(MODE_TYPE, var -> type, read_only);
1555 break;
1556 case OBERON_CLASS_VAR:
1557 case OBERON_CLASS_VAR_PARAM:
1558 case OBERON_CLASS_PARAM:
1559 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1560 break;
1561 case OBERON_CLASS_PROC:
1562 expr = oberon_new_item(MODE_VAR, var -> type, true);
1563 break;
1564 default:
1565 oberon_error(ctx, "invalid designator");
1566 break;
1569 expr -> item.var = var;
1571 return expr;
1574 static oberon_expr_t *
1575 oberon_designator(oberon_context_t * ctx)
1577 char * name;
1578 oberon_expr_t * expr;
1580 expr = oberon_qualident_expr(ctx);
1582 while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
1584 switch(ctx -> token)
1586 case DOT:
1587 oberon_assert_token(ctx, DOT);
1588 name = oberon_assert_ident(ctx);
1589 expr = oberon_make_record_selector(ctx, expr, name);
1590 break;
1591 case LBRACK:
1592 oberon_assert_token(ctx, LBRACK);
1593 int num_indexes = 0;
1594 oberon_expr_t * indexes = NULL;
1595 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1596 oberon_assert_token(ctx, RBRACK);
1598 for(int i = 0; i < num_indexes; i++)
1600 expr = oberon_make_array_selector(ctx, expr, indexes);
1601 indexes = indexes -> next;
1603 break;
1604 case UPARROW:
1605 oberon_assert_token(ctx, UPARROW);
1606 expr = oberno_make_dereferencing(ctx, expr);
1607 break;
1608 case LPAREN:
1609 oberon_assert_token(ctx, LPAREN);
1610 oberon_object_t * objtype = oberon_qualident(ctx, NULL, 1);
1611 if(objtype -> class != OBERON_CLASS_TYPE)
1613 oberon_error(ctx, "must be type");
1615 oberon_assert_token(ctx, RPAREN);
1616 expr = oberno_make_record_cast(ctx, expr, objtype -> type);
1617 break;
1618 default:
1619 oberon_error(ctx, "oberon_designator: wat");
1620 break;
1624 return expr;
1627 static oberon_expr_t *
1628 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1630 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1631 if(ctx -> token == LPAREN)
1633 oberon_assert_token(ctx, LPAREN);
1635 int num_args = 0;
1636 oberon_expr_t * arguments = NULL;
1638 if(ISEXPR(ctx -> token))
1640 oberon_expr_list(ctx, &num_args, &arguments, 0);
1643 assert(expr -> is_item == 1);
1644 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1646 oberon_assert_token(ctx, RPAREN);
1649 return expr;
1652 static void
1653 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1655 assert(expr -> is_item);
1657 int num_args = 0;
1658 oberon_expr_t * arguments = NULL;
1660 if(ctx -> token == LPAREN)
1662 oberon_assert_token(ctx, LPAREN);
1664 if(ISEXPR(ctx -> token))
1666 oberon_expr_list(ctx, &num_args, &arguments, 0);
1669 oberon_assert_token(ctx, RPAREN);
1672 /* Вызов происходит даже без скобок */
1673 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1676 static oberon_type_t *
1677 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
1679 if(i >= -128 && i <= 127)
1681 return ctx -> byte_type;
1683 else if(i >= -32768 && i <= 32767)
1685 return ctx -> shortint_type;
1687 else if(i >= -2147483648 && i <= 2147483647)
1689 return ctx -> int_type;
1691 else
1693 return ctx -> longint_type;
1697 static oberon_expr_t *
1698 oberon_integer_item(oberon_context_t * ctx, int64_t i)
1700 oberon_expr_t * expr;
1701 oberon_type_t * result;
1702 result = oberon_get_type_of_int_value(ctx, i);
1703 expr = oberon_new_item(MODE_INTEGER, result, true);
1704 expr -> item.integer = i;
1705 return expr;
1708 static oberon_expr_t *
1709 oberon_element(oberon_context_t * ctx)
1711 oberon_expr_t * e1;
1712 oberon_expr_t * e2;
1714 e1 = oberon_expr(ctx);
1715 if(e1 -> result -> class != OBERON_TYPE_INTEGER)
1717 oberon_error(ctx, "expected integer");
1720 e2 = NULL;
1721 if(ctx -> token == DOTDOT)
1723 oberon_assert_token(ctx, DOTDOT);
1724 e2 = oberon_expr(ctx);
1725 if(e2 -> result -> class != OBERON_TYPE_INTEGER)
1727 oberon_error(ctx, "expected integer");
1731 oberon_expr_t * set;
1732 set = oberon_new_operator(OP_RANGE, ctx -> set_type, e1, e2);
1733 return set;
1736 static oberon_expr_t *
1737 oberon_set(oberon_context_t * ctx)
1739 oberon_expr_t * set;
1740 oberon_expr_t * elements;
1741 set = oberon_new_item(MODE_SET, ctx -> set_type, true);
1742 set -> item.integer = 0;
1744 oberon_assert_token(ctx, LBRACE);
1745 if(ISEXPR(ctx -> token))
1747 elements = oberon_element(ctx);
1748 set = oberon_new_operator(OP_UNION, ctx -> set_type, set, elements);
1749 while(ctx -> token == COMMA)
1751 oberon_assert_token(ctx, COMMA);
1752 elements = oberon_element(ctx);
1753 set = oberon_new_operator(OP_UNION, ctx -> set_type, set, elements);
1756 oberon_assert_token(ctx, RBRACE);
1758 return set;
1761 static oberon_expr_t *
1762 oberon_make_boolean(oberon_context_t * ctx, bool cond)
1764 oberon_expr_t * expr;
1765 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
1766 expr -> item.integer = cond;
1767 return expr;
1770 static oberon_expr_t *
1771 oberon_factor(oberon_context_t * ctx)
1773 oberon_expr_t * expr;
1774 oberon_type_t * result;
1776 switch(ctx -> token)
1778 case IDENT:
1779 expr = oberon_designator(ctx);
1780 expr = oberon_opt_func_parens(ctx, expr);
1781 break;
1782 case INTEGER:
1783 expr = oberon_integer_item(ctx, ctx -> integer);
1784 oberon_assert_token(ctx, INTEGER);
1785 break;
1786 case CHAR:
1787 result = ctx -> char_type;
1788 expr = oberon_new_item(MODE_CHAR, result, true);
1789 expr -> item.integer = ctx -> integer;
1790 oberon_assert_token(ctx, CHAR);
1791 break;
1792 case STRING:
1793 result = ctx -> string_type;
1794 expr = oberon_new_item(MODE_STRING, result, true);
1795 expr -> item.string = ctx -> string;
1796 oberon_assert_token(ctx, STRING);
1797 break;
1798 case REAL:
1799 result = (ctx -> longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
1800 expr = oberon_new_item(MODE_REAL, result, 1);
1801 expr -> item.real = ctx -> real;
1802 oberon_assert_token(ctx, REAL);
1803 break;
1804 case LBRACE:
1805 expr = oberon_set(ctx);
1806 break;
1807 case LPAREN:
1808 oberon_assert_token(ctx, LPAREN);
1809 expr = oberon_expr(ctx);
1810 oberon_assert_token(ctx, RPAREN);
1811 break;
1812 case NOT:
1813 oberon_assert_token(ctx, NOT);
1814 expr = oberon_factor(ctx);
1815 expr = oberon_make_unary_op(ctx, NOT, expr);
1816 break;
1817 case NIL:
1818 oberon_assert_token(ctx, NIL);
1819 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, true);
1820 break;
1821 default:
1822 oberon_error(ctx, "invalid expression");
1825 return expr;
1828 #define ITMAKESBOOLEAN(x) \
1829 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1831 #define ITUSEONLYINTEGER(x) \
1832 ((x) >= LESS && (x) <= GEQ)
1834 #define ITUSEONLYBOOLEAN(x) \
1835 (((x) == OR) || ((x) == AND))
1837 static void
1838 oberon_autocast_to_real(oberon_context_t * ctx, oberon_expr_t ** e)
1840 oberon_expr_t * expr = *e;
1841 if(expr -> result -> class == OBERON_TYPE_INTEGER)
1843 if(expr -> result -> size <= ctx -> real_type -> size)
1845 *e = oberon_cast_expr(ctx, expr, ctx -> real_type);
1847 else
1849 *e = oberon_cast_expr(ctx, expr, ctx -> longreal_type);
1852 else if(expr -> result -> class != OBERON_TYPE_REAL)
1854 oberon_error(ctx, "required numeric type");
1858 static oberon_expr_t *
1859 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1861 oberon_expr_t * expr;
1862 oberon_type_t * result;
1864 bool error = false;
1865 if(token == IN)
1867 if(a -> result -> class != OBERON_TYPE_INTEGER)
1869 oberon_error(ctx, "must be integer");
1872 if(b -> result -> class != OBERON_TYPE_SET)
1874 oberon_error(ctx, "must be set");
1877 result = ctx -> bool_type;
1878 expr = oberon_new_operator(OP_IN, result, a, b);
1880 else if(token == IS)
1882 oberon_type_t * v = a -> result;
1883 if(v -> class == OBERON_TYPE_POINTER)
1885 v = v -> base;
1886 if(v -> class != OBERON_TYPE_RECORD)
1888 oberon_error(ctx, "must be record");
1891 else if(v -> class != OBERON_TYPE_RECORD)
1893 oberon_error(ctx, "must be record");
1894 }
1896 if(b -> is_item == false || b -> item.mode != MODE_TYPE)
1898 oberon_error(ctx, "requires type");
1901 oberon_type_t * t = b -> result;
1902 if(t -> class == OBERON_TYPE_POINTER)
1904 t = t -> base;
1905 if(t -> class != OBERON_TYPE_RECORD)
1907 oberon_error(ctx, "must be record");
1910 else if(t -> class != OBERON_TYPE_RECORD)
1912 oberon_error(ctx, "must be record");
1915 result = ctx -> bool_type;
1916 expr = oberon_new_operator(OP_IS, result, a, b);
1918 else if(ITMAKESBOOLEAN(token))
1920 if(ITUSEONLYINTEGER(token))
1922 if(a -> result -> class == OBERON_TYPE_INTEGER
1923 || b -> result -> class == OBERON_TYPE_INTEGER
1924 || a -> result -> class == OBERON_TYPE_REAL
1925 || b -> result -> class == OBERON_TYPE_REAL)
1927 // accept
1929 else
1931 oberon_error(ctx, "used only with numeric types");
1934 else if(ITUSEONLYBOOLEAN(token))
1936 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1937 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1939 oberon_error(ctx, "used only with boolean type");
1943 oberon_autocast_binary_op(ctx, &a, &b);
1944 result = ctx -> bool_type;
1946 if(token == EQUAL)
1948 expr = oberon_new_operator(OP_EQ, result, a, b);
1950 else if(token == NEQ)
1952 expr = oberon_new_operator(OP_NEQ, result, a, b);
1954 else if(token == LESS)
1956 expr = oberon_new_operator(OP_LSS, result, a, b);
1958 else if(token == LEQ)
1960 expr = oberon_new_operator(OP_LEQ, result, a, b);
1962 else if(token == GREAT)
1964 expr = oberon_new_operator(OP_GRT, result, a, b);
1966 else if(token == GEQ)
1968 expr = oberon_new_operator(OP_GEQ, result, a, b);
1970 else if(token == OR)
1972 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1974 else if(token == AND)
1976 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1978 else
1980 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1983 else if(token == SLASH)
1985 if(a -> result -> class == OBERON_TYPE_SET
1986 || b -> result -> class == OBERON_TYPE_SET)
1988 oberon_autocast_binary_op(ctx, &a, &b);
1989 result = a -> result;
1990 expr = oberon_new_operator(OP_SYM_DIFFERENCE, result, a, b);
1992 else
1994 oberon_autocast_to_real(ctx, &a);
1995 oberon_autocast_to_real(ctx, &b);
1996 oberon_autocast_binary_op(ctx, &a, &b);
1997 result = a -> result;
1998 expr = oberon_new_operator(OP_DIV, result, a, b);
2001 else if(token == DIV)
2003 if(a -> result -> class != OBERON_TYPE_INTEGER
2004 || b -> result -> class != OBERON_TYPE_INTEGER)
2006 oberon_error(ctx, "operator DIV requires integer type");
2009 oberon_autocast_binary_op(ctx, &a, &b);
2010 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
2012 else
2014 oberon_autocast_binary_op(ctx, &a, &b);
2015 result = a -> result;
2016 if(result -> class == OBERON_TYPE_SET)
2018 switch(token)
2020 case PLUS:
2021 expr = oberon_new_operator(OP_UNION, result, a, b);
2022 break;
2023 case MINUS:
2024 expr = oberon_new_operator(OP_DIFFERENCE, result, a, b);
2025 break;
2026 case STAR:
2027 expr = oberon_new_operator(OP_INTERSECTION, result, a, b);
2028 break;
2029 default:
2030 error = true;
2031 break;
2034 else if(result -> class == OBERON_TYPE_INTEGER
2035 || result -> class == OBERON_TYPE_REAL)
2037 switch(token)
2039 case PLUS:
2040 expr = oberon_new_operator(OP_ADD, result, a, b);
2041 break;
2042 case MINUS:
2043 expr = oberon_new_operator(OP_SUB, result, a, b);
2044 break;
2045 case STAR:
2046 expr = oberon_new_operator(OP_MUL, result, a, b);
2047 break;
2048 case MOD:
2049 expr = oberon_new_operator(OP_MOD, result, a, b);
2050 break;
2051 default:
2052 error = true;
2053 break;
2056 else
2058 error = true;
2062 if(error)
2064 oberon_error(ctx, "invalid operation");
2067 return expr;
2070 #define ISMULOP(x) \
2071 ((x) >= STAR && (x) <= AND)
2073 static oberon_expr_t *
2074 oberon_term_expr(oberon_context_t * ctx)
2076 oberon_expr_t * expr;
2078 expr = oberon_factor(ctx);
2079 while(ISMULOP(ctx -> token))
2081 int token = ctx -> token;
2082 oberon_read_token(ctx);
2084 oberon_expr_t * inter = oberon_factor(ctx);
2085 expr = oberon_make_bin_op(ctx, token, expr, inter);
2088 return expr;
2091 #define ISADDOP(x) \
2092 ((x) >= PLUS && (x) <= OR)
2094 static oberon_expr_t *
2095 oberon_simple_expr(oberon_context_t * ctx)
2097 oberon_expr_t * expr;
2099 int minus = 0;
2100 if(ctx -> token == PLUS)
2102 minus = 0;
2103 oberon_assert_token(ctx, PLUS);
2105 else if(ctx -> token == MINUS)
2107 minus = 1;
2108 oberon_assert_token(ctx, MINUS);
2111 expr = oberon_term_expr(ctx);
2113 if(minus)
2115 expr = oberon_make_unary_op(ctx, MINUS, expr);
2118 while(ISADDOP(ctx -> token))
2120 int token = ctx -> token;
2121 oberon_read_token(ctx);
2123 oberon_expr_t * inter = oberon_term_expr(ctx);
2124 expr = oberon_make_bin_op(ctx, token, expr, inter);
2127 return expr;
2130 #define ISRELATION(x) \
2131 ((x) >= EQUAL && (x) <= IS)
2133 static oberon_expr_t *
2134 oberon_expr(oberon_context_t * ctx)
2136 oberon_expr_t * expr;
2138 expr = oberon_simple_expr(ctx);
2139 while(ISRELATION(ctx -> token))
2141 int token = ctx -> token;
2142 oberon_read_token(ctx);
2144 oberon_expr_t * inter = oberon_simple_expr(ctx);
2145 expr = oberon_make_bin_op(ctx, token, expr, inter);
2148 return expr;
2151 static void
2152 oberon_check_const(oberon_context_t * ctx, oberon_expr_t * expr)
2154 if(expr -> is_item == 0)
2156 oberon_error(ctx, "const expression are required");
2159 switch(expr -> item.mode)
2161 case MODE_INTEGER:
2162 case MODE_BOOLEAN:
2163 case MODE_NIL:
2164 case MODE_REAL:
2165 case MODE_CHAR:
2166 case MODE_STRING:
2167 case MODE_TYPE:
2168 /* accept */
2169 break;
2170 default:
2171 oberon_error(ctx, "const expression are required");
2172 break;
2176 static oberon_item_t *
2177 oberon_const_expr(oberon_context_t * ctx)
2179 oberon_expr_t * expr;
2180 expr = oberon_expr(ctx);
2181 oberon_check_const(ctx, expr);
2182 return (oberon_item_t *) expr;
2185 // =======================================================================
2186 // PARSER
2187 // =======================================================================
2189 static void oberon_decl_seq(oberon_context_t * ctx);
2190 static void oberon_statement_seq(oberon_context_t * ctx);
2191 static void oberon_initialize_decl(oberon_context_t * ctx);
2193 static void
2194 oberon_expect_token(oberon_context_t * ctx, int token)
2196 if(ctx -> token != token)
2198 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
2202 static void
2203 oberon_assert_token(oberon_context_t * ctx, int token)
2205 oberon_expect_token(ctx, token);
2206 oberon_read_token(ctx);
2209 static char *
2210 oberon_assert_ident(oberon_context_t * ctx)
2212 oberon_expect_token(ctx, IDENT);
2213 char * ident = ctx -> string;
2214 oberon_read_token(ctx);
2215 return ident;
2218 static void
2219 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
2221 switch(ctx -> token)
2223 case STAR:
2224 oberon_assert_token(ctx, STAR);
2225 *export = 1;
2226 *read_only = 0;
2227 break;
2228 case MINUS:
2229 oberon_assert_token(ctx, MINUS);
2230 *export = 1;
2231 *read_only = 1;
2232 break;
2233 default:
2234 *export = 0;
2235 *read_only = 0;
2236 break;
2240 static oberon_object_t *
2241 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
2243 char * name;
2244 int export;
2245 int read_only;
2246 oberon_object_t * x;
2248 name = oberon_assert_ident(ctx);
2249 oberon_def(ctx, &export, &read_only);
2251 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
2252 return x;
2255 static void
2256 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
2258 *num = 1;
2259 *list = oberon_ident_def(ctx, class, check_upscope);
2260 while(ctx -> token == COMMA)
2262 oberon_assert_token(ctx, COMMA);
2263 oberon_ident_def(ctx, class, check_upscope);
2264 *num += 1;
2268 static void
2269 oberon_var_decl(oberon_context_t * ctx)
2271 int num;
2272 oberon_object_t * list;
2273 oberon_type_t * type;
2274 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2276 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
2277 oberon_assert_token(ctx, COLON);
2278 oberon_type(ctx, &type);
2280 oberon_object_t * var = list;
2281 for(int i = 0; i < num; i++)
2283 var -> type = type;
2284 var = var -> next;
2288 static oberon_object_t *
2289 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
2291 int class = OBERON_CLASS_PARAM;
2292 if(ctx -> token == VAR)
2294 oberon_read_token(ctx);
2295 class = OBERON_CLASS_VAR_PARAM;
2298 int num;
2299 oberon_object_t * list;
2300 oberon_ident_list(ctx, class, false, &num, &list);
2302 oberon_assert_token(ctx, COLON);
2304 oberon_type_t * type;
2305 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2306 oberon_type(ctx, &type);
2308 oberon_object_t * param = list;
2309 for(int i = 0; i < num; i++)
2311 param -> type = type;
2312 param = param -> next;
2315 *num_decl += num;
2316 return list;
2319 #define ISFPSECTION \
2320 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2322 static void
2323 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2325 oberon_assert_token(ctx, LPAREN);
2327 if(ISFPSECTION)
2329 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2330 while(ctx -> token == SEMICOLON)
2332 oberon_assert_token(ctx, SEMICOLON);
2333 oberon_fp_section(ctx, &signature -> num_decl);
2337 oberon_assert_token(ctx, RPAREN);
2339 if(ctx -> token == COLON)
2341 oberon_assert_token(ctx, COLON);
2343 oberon_object_t * typeobj;
2344 typeobj = oberon_qualident(ctx, NULL, 1);
2345 if(typeobj -> class != OBERON_CLASS_TYPE)
2347 oberon_error(ctx, "function result is not type");
2349 signature -> base = typeobj -> type;
2353 static void
2354 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2356 oberon_type_t * signature;
2357 signature = *type;
2358 signature -> class = OBERON_TYPE_PROCEDURE;
2359 signature -> num_decl = 0;
2360 signature -> base = ctx -> void_type;
2361 signature -> decl = NULL;
2363 if(ctx -> token == LPAREN)
2365 oberon_formal_pars(ctx, signature);
2369 static void
2370 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2372 if(a -> num_decl != b -> num_decl)
2374 oberon_error(ctx, "number parameters not matched");
2377 int num_param = a -> num_decl;
2378 oberon_object_t * param_a = a -> decl;
2379 oberon_object_t * param_b = b -> decl;
2380 for(int i = 0; i < num_param; i++)
2382 if(strcmp(param_a -> name, param_b -> name) != 0)
2384 oberon_error(ctx, "param %i name not matched", i + 1);
2387 if(param_a -> type != param_b -> type)
2389 oberon_error(ctx, "param %i type not matched", i + 1);
2392 param_a = param_a -> next;
2393 param_b = param_b -> next;
2397 static void
2398 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2400 oberon_object_t * proc = ctx -> decl -> parent;
2401 oberon_type_t * result_type = proc -> type -> base;
2403 if(result_type -> class == OBERON_TYPE_VOID)
2405 if(expr != NULL)
2407 oberon_error(ctx, "procedure has no result type");
2410 else
2412 if(expr == NULL)
2414 oberon_error(ctx, "procedure requires expression on result");
2417 expr = oberon_autocast_to(ctx, expr, result_type);
2420 proc -> has_return = 1;
2422 oberon_generate_return(ctx, expr);
2425 static void
2426 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2428 oberon_assert_token(ctx, SEMICOLON);
2430 ctx -> decl = proc -> scope;
2432 oberon_decl_seq(ctx);
2434 oberon_generate_begin_proc(ctx, proc);
2436 if(ctx -> token == BEGIN)
2438 oberon_assert_token(ctx, BEGIN);
2439 oberon_statement_seq(ctx);
2442 oberon_assert_token(ctx, END);
2443 char * name = oberon_assert_ident(ctx);
2444 if(strcmp(name, proc -> name) != 0)
2446 oberon_error(ctx, "procedure name not matched");
2449 if(proc -> type -> base -> class == OBERON_TYPE_VOID
2450 && proc -> has_return == 0)
2452 oberon_make_return(ctx, NULL);
2455 if(proc -> has_return == 0)
2457 oberon_error(ctx, "procedure requires return");
2460 oberon_generate_end_proc(ctx);
2461 oberon_close_scope(ctx -> decl);
2464 static void
2465 oberon_proc_decl(oberon_context_t * ctx)
2467 oberon_assert_token(ctx, PROCEDURE);
2469 int forward = 0;
2470 if(ctx -> token == UPARROW)
2472 oberon_assert_token(ctx, UPARROW);
2473 forward = 1;
2476 char * name;
2477 int export;
2478 int read_only;
2479 name = oberon_assert_ident(ctx);
2480 oberon_def(ctx, &export, &read_only);
2482 oberon_scope_t * proc_scope;
2483 proc_scope = oberon_open_scope(ctx);
2484 ctx -> decl -> local = 1;
2486 oberon_type_t * signature;
2487 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
2488 oberon_opt_formal_pars(ctx, &signature);
2490 //oberon_initialize_decl(ctx);
2491 oberon_generator_init_type(ctx, signature);
2492 oberon_close_scope(ctx -> decl);
2494 oberon_object_t * proc;
2495 proc = oberon_find_object(ctx -> decl, name, 0);
2496 if(proc == NULL)
2498 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2499 proc -> type = signature;
2500 proc -> scope = proc_scope;
2501 oberon_generator_init_proc(ctx, proc);
2503 else
2505 if(proc -> class != OBERON_CLASS_PROC)
2507 oberon_error(ctx, "mult definition");
2510 if(forward == 0)
2512 if(proc -> linked)
2514 oberon_error(ctx, "mult procedure definition");
2518 if(proc -> export != export || proc -> read_only != read_only)
2520 oberon_error(ctx, "export type not matched");
2523 oberon_compare_signatures(ctx, proc -> type, signature);
2526 proc_scope -> parent = proc;
2527 oberon_object_t * param = proc_scope -> list -> next;
2528 while(param)
2530 param -> parent = proc;
2531 param = param -> next;
2534 if(forward == 0)
2536 proc -> linked = 1;
2537 oberon_proc_decl_body(ctx, proc);
2541 static void
2542 oberon_const_decl(oberon_context_t * ctx)
2544 oberon_item_t * value;
2545 oberon_object_t * constant;
2547 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2548 oberon_assert_token(ctx, EQUAL);
2549 value = oberon_const_expr(ctx);
2550 constant -> value = value;
2553 static void
2554 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2556 if(size -> is_item == 0)
2558 oberon_error(ctx, "requires constant");
2561 if(size -> item.mode != MODE_INTEGER)
2563 oberon_error(ctx, "requires integer constant");
2566 oberon_type_t * arr;
2567 arr = *type;
2568 arr -> class = OBERON_TYPE_ARRAY;
2569 arr -> size = size -> item.integer;
2570 arr -> base = base;
2573 static void
2574 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2576 char * name;
2577 oberon_object_t * to;
2579 to = oberon_qualident(ctx, &name, 0);
2581 //name = oberon_assert_ident(ctx);
2582 //to = oberon_find_object(ctx -> decl, name, 0);
2584 if(to != NULL)
2586 if(to -> class != OBERON_CLASS_TYPE)
2588 oberon_error(ctx, "not a type");
2591 else
2593 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2594 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2597 *type = to -> type;
2600 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2602 /*
2603 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2604 */
2606 static void
2607 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2609 if(sizes == NULL)
2611 *type = base;
2612 return;
2615 oberon_type_t * dim;
2616 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
2618 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2620 oberon_make_array_type(ctx, sizes, dim, type);
2623 static void
2624 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2626 type -> class = OBERON_TYPE_ARRAY;
2627 type -> size = 0;
2628 type -> base = base;
2631 static void
2632 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2634 if(ctx -> token == IDENT)
2636 int num;
2637 oberon_object_t * list;
2638 oberon_type_t * type;
2639 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2641 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2642 oberon_assert_token(ctx, COLON);
2644 oberon_scope_t * current = ctx -> decl;
2645 ctx -> decl = modscope;
2646 oberon_type(ctx, &type);
2647 ctx -> decl = current;
2649 oberon_object_t * field = list;
2650 for(int i = 0; i < num; i++)
2652 field -> type = type;
2653 field = field -> next;
2656 rec -> num_decl += num;
2660 static void
2661 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2663 oberon_scope_t * modscope = ctx -> mod -> decl;
2664 oberon_scope_t * oldscope = ctx -> decl;
2665 ctx -> decl = modscope;
2667 if(ctx -> token == LPAREN)
2669 oberon_assert_token(ctx, LPAREN);
2671 oberon_object_t * typeobj;
2672 typeobj = oberon_qualident(ctx, NULL, true);
2674 if(typeobj -> class != OBERON_CLASS_TYPE)
2676 oberon_error(ctx, "base must be type");
2679 oberon_type_t * base = typeobj -> type;
2680 if(base -> class == OBERON_TYPE_POINTER)
2682 base = base -> base;
2685 if(base -> class != OBERON_TYPE_RECORD)
2687 oberon_error(ctx, "base must be record type");
2690 rec -> base = base;
2691 ctx -> decl = base -> scope;
2693 oberon_assert_token(ctx, RPAREN);
2695 else
2697 ctx -> decl = NULL;
2700 oberon_scope_t * this_scope;
2701 this_scope = oberon_open_scope(ctx);
2702 this_scope -> local = true;
2703 this_scope -> parent = NULL;
2704 this_scope -> parent_type = rec;
2706 oberon_field_list(ctx, rec, modscope);
2707 while(ctx -> token == SEMICOLON)
2709 oberon_assert_token(ctx, SEMICOLON);
2710 oberon_field_list(ctx, rec, modscope);
2713 rec -> scope = this_scope;
2714 rec -> decl = this_scope -> list -> next;
2715 ctx -> decl = oldscope;
2718 static void
2719 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2721 if(ctx -> token == IDENT)
2723 oberon_qualident_type(ctx, type);
2725 else if(ctx -> token == ARRAY)
2727 oberon_assert_token(ctx, ARRAY);
2729 int num_sizes = 0;
2730 oberon_expr_t * sizes;
2732 if(ISEXPR(ctx -> token))
2734 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2737 oberon_assert_token(ctx, OF);
2739 oberon_type_t * base;
2740 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2741 oberon_type(ctx, &base);
2743 if(num_sizes == 0)
2745 oberon_make_open_array(ctx, base, *type);
2747 else
2749 oberon_make_multiarray(ctx, sizes, base, type);
2752 else if(ctx -> token == RECORD)
2754 oberon_type_t * rec;
2755 rec = *type;
2756 rec -> class = OBERON_TYPE_RECORD;
2757 rec -> module = ctx -> mod;
2759 oberon_assert_token(ctx, RECORD);
2760 oberon_type_record_body(ctx, rec);
2761 oberon_assert_token(ctx, END);
2763 *type = rec;
2765 else if(ctx -> token == POINTER)
2767 oberon_assert_token(ctx, POINTER);
2768 oberon_assert_token(ctx, TO);
2770 oberon_type_t * base;
2771 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2772 oberon_type(ctx, &base);
2774 oberon_type_t * ptr;
2775 ptr = *type;
2776 ptr -> class = OBERON_TYPE_POINTER;
2777 ptr -> base = base;
2779 else if(ctx -> token == PROCEDURE)
2781 oberon_open_scope(ctx);
2782 oberon_assert_token(ctx, PROCEDURE);
2783 oberon_opt_formal_pars(ctx, type);
2784 oberon_close_scope(ctx -> decl);
2786 else
2788 oberon_error(ctx, "invalid type declaration");
2792 static void
2793 oberon_type_decl(oberon_context_t * ctx)
2795 char * name;
2796 oberon_object_t * newtype;
2797 oberon_type_t * type;
2798 int export;
2799 int read_only;
2801 name = oberon_assert_ident(ctx);
2802 oberon_def(ctx, &export, &read_only);
2804 newtype = oberon_find_object(ctx -> decl, name, 0);
2805 if(newtype == NULL)
2807 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2808 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2809 assert(newtype -> type);
2811 else
2813 if(newtype -> class != OBERON_CLASS_TYPE)
2815 oberon_error(ctx, "mult definition");
2818 if(newtype -> linked)
2820 oberon_error(ctx, "mult definition - already linked");
2823 newtype -> export = export;
2824 newtype -> read_only = read_only;
2827 oberon_assert_token(ctx, EQUAL);
2829 type = newtype -> type;
2830 oberon_type(ctx, &type);
2832 if(type -> class == OBERON_TYPE_VOID)
2834 oberon_error(ctx, "recursive alias declaration");
2837 newtype -> type = type;
2838 newtype -> linked = 1;
2841 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2842 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2844 static void
2845 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2847 if(type -> class != OBERON_TYPE_POINTER
2848 && type -> class != OBERON_TYPE_ARRAY)
2850 return;
2853 if(type -> recursive)
2855 oberon_error(ctx, "recursive pointer declaration");
2858 if(type -> class == OBERON_TYPE_POINTER
2859 && type -> base -> class == OBERON_TYPE_POINTER)
2861 oberon_error(ctx, "attempt to make pointer to pointer");
2864 type -> recursive = 1;
2866 oberon_prevent_recursive_pointer(ctx, type -> base);
2868 type -> recursive = 0;
2871 static void
2872 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2874 if(type -> class != OBERON_TYPE_RECORD)
2876 return;
2879 if(type -> recursive)
2881 oberon_error(ctx, "recursive record declaration");
2884 type -> recursive = 1;
2886 int num_fields = type -> num_decl;
2887 oberon_object_t * field = type -> decl;
2888 for(int i = 0; i < num_fields; i++)
2890 oberon_prevent_recursive_object(ctx, field);
2891 field = field -> next;
2894 type -> recursive = 0;
2896 static void
2897 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2899 if(type -> class != OBERON_TYPE_PROCEDURE)
2901 return;
2904 if(type -> recursive)
2906 oberon_error(ctx, "recursive procedure declaration");
2909 type -> recursive = 1;
2911 int num_fields = type -> num_decl;
2912 oberon_object_t * field = type -> decl;
2913 for(int i = 0; i < num_fields; i++)
2915 oberon_prevent_recursive_object(ctx, field);
2916 field = field -> next;
2919 type -> recursive = 0;
2922 static void
2923 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2925 if(type -> class != OBERON_TYPE_ARRAY)
2927 return;
2930 if(type -> recursive)
2932 oberon_error(ctx, "recursive array declaration");
2935 type -> recursive = 1;
2937 oberon_prevent_recursive_type(ctx, type -> base);
2939 type -> recursive = 0;
2942 static void
2943 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2945 if(type -> class == OBERON_TYPE_POINTER)
2947 oberon_prevent_recursive_pointer(ctx, type);
2949 else if(type -> class == OBERON_TYPE_RECORD)
2951 oberon_prevent_recursive_record(ctx, type);
2953 else if(type -> class == OBERON_TYPE_ARRAY)
2955 oberon_prevent_recursive_array(ctx, type);
2957 else if(type -> class == OBERON_TYPE_PROCEDURE)
2959 oberon_prevent_recursive_procedure(ctx, type);
2963 static void
2964 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2966 switch(x -> class)
2968 case OBERON_CLASS_VAR:
2969 case OBERON_CLASS_TYPE:
2970 case OBERON_CLASS_PARAM:
2971 case OBERON_CLASS_VAR_PARAM:
2972 case OBERON_CLASS_FIELD:
2973 oberon_prevent_recursive_type(ctx, x -> type);
2974 break;
2975 case OBERON_CLASS_CONST:
2976 case OBERON_CLASS_PROC:
2977 case OBERON_CLASS_MODULE:
2978 break;
2979 default:
2980 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2981 break;
2985 static void
2986 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2988 oberon_object_t * x = ctx -> decl -> list -> next;
2990 while(x)
2992 oberon_prevent_recursive_object(ctx, x);
2993 x = x -> next;
2997 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2998 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
3000 static void
3001 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
3003 if(type -> class != OBERON_TYPE_RECORD)
3005 return;
3008 int num_fields = type -> num_decl;
3009 oberon_object_t * field = type -> decl;
3010 for(int i = 0; i < num_fields; i++)
3012 if(field -> type -> class == OBERON_TYPE_POINTER)
3014 oberon_initialize_type(ctx, field -> type);
3017 oberon_initialize_object(ctx, field);
3018 field = field -> next;
3021 oberon_generator_init_record(ctx, type);
3024 static void
3025 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
3027 if(type -> class == OBERON_TYPE_VOID)
3029 oberon_error(ctx, "undeclarated type");
3032 if(type -> initialized)
3034 return;
3037 type -> initialized = 1;
3039 if(type -> class == OBERON_TYPE_POINTER)
3041 oberon_initialize_type(ctx, type -> base);
3042 oberon_generator_init_type(ctx, type);
3044 else if(type -> class == OBERON_TYPE_ARRAY)
3046 if(type -> size != 0)
3048 if(type -> base -> class == OBERON_TYPE_ARRAY)
3050 if(type -> base -> size == 0)
3052 oberon_error(ctx, "open array not allowed as array element");
3057 oberon_initialize_type(ctx, type -> base);
3058 oberon_generator_init_type(ctx, type);
3060 else if(type -> class == OBERON_TYPE_RECORD)
3062 oberon_generator_init_type(ctx, type);
3063 oberon_initialize_record_fields(ctx, type);
3065 else if(type -> class == OBERON_TYPE_PROCEDURE)
3067 int num_fields = type -> num_decl;
3068 oberon_object_t * field = type -> decl;
3069 for(int i = 0; i < num_fields; i++)
3071 oberon_initialize_object(ctx, field);
3072 field = field -> next;
3073 }
3075 oberon_generator_init_type(ctx, type);
3077 else
3079 oberon_generator_init_type(ctx, type);
3083 static void
3084 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
3086 if(x -> initialized)
3088 return;
3091 x -> initialized = 1;
3093 switch(x -> class)
3095 case OBERON_CLASS_TYPE:
3096 oberon_initialize_type(ctx, x -> type);
3097 break;
3098 case OBERON_CLASS_VAR:
3099 case OBERON_CLASS_FIELD:
3100 if(x -> type -> class == OBERON_TYPE_ARRAY)
3102 if(x -> type -> size == 0)
3104 oberon_error(ctx, "open array not allowed as variable or field");
3107 oberon_initialize_type(ctx, x -> type);
3108 oberon_generator_init_var(ctx, x);
3109 break;
3110 case OBERON_CLASS_PARAM:
3111 case OBERON_CLASS_VAR_PARAM:
3112 oberon_initialize_type(ctx, x -> type);
3113 oberon_generator_init_var(ctx, x);
3114 break;
3115 case OBERON_CLASS_CONST:
3116 case OBERON_CLASS_PROC:
3117 case OBERON_CLASS_MODULE:
3118 break;
3119 default:
3120 oberon_error(ctx, "oberon_initialize_object: wat");
3121 break;
3125 static void
3126 oberon_initialize_decl(oberon_context_t * ctx)
3128 oberon_object_t * x = ctx -> decl -> list;
3130 while(x -> next)
3132 oberon_initialize_object(ctx, x -> next);
3133 x = x -> next;
3134 }
3137 static void
3138 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
3140 oberon_object_t * x = ctx -> decl -> list;
3142 while(x -> next)
3144 if(x -> next -> class == OBERON_CLASS_PROC)
3146 if(x -> next -> linked == 0)
3148 oberon_error(ctx, "unresolved forward declaration");
3151 x = x -> next;
3152 }
3155 static void
3156 oberon_decl_seq(oberon_context_t * ctx)
3158 if(ctx -> token == CONST)
3160 oberon_assert_token(ctx, CONST);
3161 while(ctx -> token == IDENT)
3163 oberon_const_decl(ctx);
3164 oberon_assert_token(ctx, SEMICOLON);
3168 if(ctx -> token == TYPE)
3170 oberon_assert_token(ctx, TYPE);
3171 while(ctx -> token == IDENT)
3173 oberon_type_decl(ctx);
3174 oberon_assert_token(ctx, SEMICOLON);
3178 if(ctx -> token == VAR)
3180 oberon_assert_token(ctx, VAR);
3181 while(ctx -> token == IDENT)
3183 oberon_var_decl(ctx);
3184 oberon_assert_token(ctx, SEMICOLON);
3188 oberon_prevent_recursive_decl(ctx);
3189 oberon_initialize_decl(ctx);
3191 while(ctx -> token == PROCEDURE)
3193 oberon_proc_decl(ctx);
3194 oberon_assert_token(ctx, SEMICOLON);
3197 oberon_prevent_undeclarated_procedures(ctx);
3200 static oberon_expr_t *
3201 oberon_make_temp_var_item(oberon_context_t * ctx, oberon_type_t * type)
3203 oberon_object_t * x;
3204 oberon_expr_t * expr;
3206 x = oberon_create_object(ctx -> decl, "TEMP", OBERON_CLASS_VAR, false, false);
3207 x -> local = true;
3208 x -> type = type;
3209 oberon_generator_init_temp_var(ctx, x);
3211 expr = oberon_new_item(MODE_VAR, type, false);
3212 expr -> item.var = x;
3213 return expr;
3216 static void
3217 oberon_statement_seq(oberon_context_t * ctx);
3219 static void
3220 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
3222 if(dst -> read_only)
3224 oberon_error(ctx, "read-only destination");
3227 oberon_check_dst(ctx, dst);
3228 src = oberon_autocast_to(ctx, src, dst -> result);
3229 oberon_generate_assign(ctx, src, dst);
3232 static oberon_expr_t *
3233 oberon_case_labels(oberon_context_t * ctx, oberon_expr_t * val)
3235 oberon_expr_t * e1;
3236 oberon_expr_t * e2;
3237 oberon_expr_t * cond;
3238 oberon_expr_t * cond2;
3240 e1 = (oberon_expr_t *) oberon_const_expr(ctx);
3241 oberon_autocast_to(ctx, e1, val -> result);
3243 e2 = NULL;
3244 if(ctx -> token == DOTDOT)
3246 oberon_assert_token(ctx, DOTDOT);
3247 e2 = (oberon_expr_t *) oberon_const_expr(ctx);
3248 oberon_autocast_to(ctx, e2, val -> result);
3251 if(e2 == NULL)
3253 /* val == e1 */
3254 cond = oberon_make_bin_op(ctx, EQUAL, val, e1);
3256 else
3258 /* val >= e1 && val <= e2 */
3259 cond = oberon_make_bin_op(ctx, GEQ, val, e1);
3260 cond2 = oberon_make_bin_op(ctx, LEQ, val, e2);
3261 cond = oberon_make_bin_op(ctx, AND, cond, cond2);
3264 return cond;
3267 static void
3268 oberon_case(oberon_context_t * ctx, oberon_expr_t * val, gen_label_t * end)
3270 oberon_expr_t * cond;
3271 oberon_expr_t * cond2;
3272 gen_label_t * this_end;
3274 if(ISEXPR(ctx -> token))
3276 this_end = oberon_generator_reserve_label(ctx);
3278 cond = oberon_case_labels(ctx, val);
3279 while(ctx -> token == COMMA)
3281 oberon_assert_token(ctx, COMMA);
3282 /* cond || cond2 */
3283 cond2 = oberon_case_labels(ctx, val);
3284 cond = oberon_make_bin_op(ctx, OR, cond, cond2);
3286 oberon_assert_token(ctx, COLON);
3288 oberon_generate_branch(ctx, cond, false, this_end);
3289 oberon_statement_seq(ctx);
3290 oberon_generate_goto(ctx, end);
3292 oberon_generate_label(ctx, this_end);
3296 static void
3297 oberon_case_statement(oberon_context_t * ctx)
3299 oberon_expr_t * val;
3300 oberon_expr_t * expr;
3301 gen_label_t * end;
3303 end = oberon_generator_reserve_label(ctx);
3305 oberon_assert_token(ctx, CASE);
3306 expr = oberon_expr(ctx);
3307 val = oberon_make_temp_var_item(ctx, expr -> result);
3308 oberon_assign(ctx, expr, val);
3309 oberon_assert_token(ctx, OF);
3310 oberon_case(ctx, val, end);
3311 while(ctx -> token == BAR)
3313 oberon_assert_token(ctx, BAR);
3314 oberon_case(ctx, val, end);
3317 if(ctx -> token == ELSE)
3319 oberon_assert_token(ctx, ELSE);
3320 oberon_statement_seq(ctx);
3323 oberon_generate_label(ctx, end);
3324 oberon_assert_token(ctx, END);
3327 static void
3328 oberon_with_guard_do(oberon_context_t * ctx, gen_label_t * end)
3330 oberon_expr_t * val;
3331 oberon_expr_t * var;
3332 oberon_expr_t * type;
3333 oberon_expr_t * cond;
3334 oberon_expr_t * cast;
3335 oberon_type_t * old_type;
3336 gen_var_t * old_var;
3337 gen_label_t * this_end;
3339 this_end = oberon_generator_reserve_label(ctx);
3341 var = oberon_qualident_expr(ctx);
3342 oberon_assert_token(ctx, COLON);
3343 type = oberon_qualident_expr(ctx);
3344 cond = oberon_make_bin_op(ctx, IS, var, type);
3346 oberon_assert_token(ctx, DO);
3347 oberon_generate_branch(ctx, cond, false, this_end);
3349 /* Сохраняем ссылку во временной переменной */
3350 val = oberon_make_temp_var_item(ctx, type -> result);
3351 cast = oberno_make_record_cast(ctx, var, type -> result);
3352 oberon_assign(ctx, cast, val);
3353 /* Подменяем тип у оригинальной переменной */
3354 old_type = var -> item.var -> type;
3355 var -> item.var -> type = type -> result;
3356 /* Подменяем ссылку на переменную */
3357 old_var = var -> item.var -> gen_var;
3358 var -> item.var -> gen_var = val -> item.var -> gen_var;
3360 oberon_statement_seq(ctx);
3361 oberon_generate_goto(ctx, end);
3362 oberon_generate_label(ctx, this_end);
3364 /* Возвращаем исходное состояние */
3365 var -> item.var -> gen_var = old_var;
3366 var -> item.var -> type = old_type;
3369 static void
3370 oberon_with_statement(oberon_context_t * ctx)
3372 gen_label_t * end;
3373 end = oberon_generator_reserve_label(ctx);
3375 oberon_assert_token(ctx, WITH);
3376 oberon_with_guard_do(ctx, end);
3377 while(ctx -> token == BAR)
3379 oberon_assert_token(ctx, BAR);
3380 oberon_with_guard_do(ctx, end);
3383 if(ctx -> token == ELSE)
3385 oberon_assert_token(ctx, ELSE);
3386 oberon_statement_seq(ctx);
3389 oberon_generate_label(ctx, end);
3390 oberon_assert_token(ctx, END);
3393 static void
3394 oberon_statement(oberon_context_t * ctx)
3396 oberon_expr_t * item1;
3397 oberon_expr_t * item2;
3399 if(ctx -> token == IDENT)
3401 item1 = oberon_designator(ctx);
3402 if(ctx -> token == ASSIGN)
3404 oberon_assert_token(ctx, ASSIGN);
3405 item2 = oberon_expr(ctx);
3406 oberon_assign(ctx, item2, item1);
3408 else
3410 oberon_opt_proc_parens(ctx, item1);
3413 else if(ctx -> token == IF)
3415 gen_label_t * end;
3416 gen_label_t * els;
3417 oberon_expr_t * cond;
3419 els = oberon_generator_reserve_label(ctx);
3420 end = oberon_generator_reserve_label(ctx);
3422 oberon_assert_token(ctx, IF);
3423 cond = oberon_expr(ctx);
3424 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3426 oberon_error(ctx, "condition must be boolean");
3428 oberon_assert_token(ctx, THEN);
3429 oberon_generate_branch(ctx, cond, false, els);
3430 oberon_statement_seq(ctx);
3431 oberon_generate_goto(ctx, end);
3432 oberon_generate_label(ctx, els);
3434 while(ctx -> token == ELSIF)
3436 els = oberon_generator_reserve_label(ctx);
3438 oberon_assert_token(ctx, ELSIF);
3439 cond = oberon_expr(ctx);
3440 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3442 oberon_error(ctx, "condition must be boolean");
3444 oberon_assert_token(ctx, THEN);
3445 oberon_generate_branch(ctx, cond, false, els);
3446 oberon_statement_seq(ctx);
3447 oberon_generate_goto(ctx, end);
3448 oberon_generate_label(ctx, els);
3451 if(ctx -> token == ELSE)
3453 oberon_assert_token(ctx, ELSE);
3454 oberon_statement_seq(ctx);
3457 oberon_generate_label(ctx, end);
3458 oberon_assert_token(ctx, END);
3460 else if(ctx -> token == WHILE)
3462 gen_label_t * begin;
3463 gen_label_t * end;
3464 oberon_expr_t * cond;
3466 begin = oberon_generator_reserve_label(ctx);
3467 end = oberon_generator_reserve_label(ctx);
3469 oberon_assert_token(ctx, WHILE);
3470 oberon_generate_label(ctx, begin);
3471 cond = oberon_expr(ctx);
3472 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3474 oberon_error(ctx, "condition must be boolean");
3476 oberon_generate_branch(ctx, cond, false, end);
3478 oberon_assert_token(ctx, DO);
3479 oberon_statement_seq(ctx);
3480 oberon_generate_goto(ctx, begin);
3482 oberon_assert_token(ctx, END);
3483 oberon_generate_label(ctx, end);
3485 else if(ctx -> token == REPEAT)
3487 gen_label_t * begin;
3488 oberon_expr_t * cond;
3490 begin = oberon_generator_reserve_label(ctx);
3491 oberon_generate_label(ctx, begin);
3492 oberon_assert_token(ctx, REPEAT);
3494 oberon_statement_seq(ctx);
3496 oberon_assert_token(ctx, UNTIL);
3498 cond = oberon_expr(ctx);
3499 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
3501 oberon_error(ctx, "condition must be boolean");
3504 oberon_generate_branch(ctx, cond, true, begin);
3506 else if(ctx -> token == FOR)
3508 oberon_expr_t * from;
3509 oberon_expr_t * index;
3510 oberon_expr_t * to;
3511 oberon_expr_t * bound;
3512 oberon_expr_t * by;
3513 oberon_expr_t * cond;
3514 oberon_expr_t * count;
3515 gen_label_t * begin;
3516 gen_label_t * end;
3517 char * iname;
3518 int op;
3520 begin = oberon_generator_reserve_label(ctx);
3521 end = oberon_generator_reserve_label(ctx);
3523 oberon_assert_token(ctx, FOR);
3524 iname = oberon_assert_ident(ctx);
3525 index = oberon_ident_item(ctx, iname);
3526 oberon_assert_token(ctx, ASSIGN);
3527 from = oberon_expr(ctx);
3528 oberon_assign(ctx, from, index);
3529 oberon_assert_token(ctx, TO);
3530 bound = oberon_make_temp_var_item(ctx, index -> result);
3531 to = oberon_expr(ctx);
3532 oberon_assign(ctx, to, bound);
3533 if(ctx -> token == BY)
3535 oberon_assert_token(ctx, BY);
3536 by = (oberon_expr_t *) oberon_const_expr(ctx);
3538 else
3540 by = oberon_integer_item(ctx, 1);
3543 if(by -> result -> class != OBERON_TYPE_INTEGER)
3545 oberon_error(ctx, "must be integer");
3548 if(by -> item.integer > 0)
3550 op = LEQ;
3552 else if(by -> item.integer < 0)
3554 op = GEQ;
3556 else
3558 oberon_error(ctx, "zero step not allowed");
3561 oberon_assert_token(ctx, DO);
3562 oberon_generate_label(ctx, begin);
3563 cond = oberon_make_bin_op(ctx, op, index, bound);
3564 oberon_generate_branch(ctx, cond, false, end);
3565 oberon_statement_seq(ctx);
3566 count = oberon_make_bin_op(ctx, PLUS, index, by);
3567 oberon_assign(ctx, count, index);
3568 oberon_generate_goto(ctx, begin);
3569 oberon_generate_label(ctx, end);
3570 oberon_assert_token(ctx, END);
3572 else if(ctx -> token == LOOP)
3574 gen_label_t * begin;
3575 gen_label_t * end;
3577 begin = oberon_generator_reserve_label(ctx);
3578 end = oberon_generator_reserve_label(ctx);
3580 oberon_open_scope(ctx);
3581 oberon_assert_token(ctx, LOOP);
3582 oberon_generate_label(ctx, begin);
3583 ctx -> decl -> exit_label = end;
3584 oberon_statement_seq(ctx);
3585 oberon_generate_goto(ctx, begin);
3586 oberon_generate_label(ctx, end);
3587 oberon_assert_token(ctx, END);
3588 oberon_close_scope(ctx -> decl);
3590 else if(ctx -> token == EXIT)
3592 oberon_assert_token(ctx, EXIT);
3593 if(ctx -> decl -> exit_label == NULL)
3595 oberon_error(ctx, "not in LOOP-END");
3597 oberon_generate_goto(ctx, ctx -> decl -> exit_label);
3599 else if(ctx -> token == CASE)
3601 oberon_case_statement(ctx);
3603 else if(ctx -> token == WITH)
3605 oberon_with_statement(ctx);
3607 else if(ctx -> token == RETURN)
3609 oberon_assert_token(ctx, RETURN);
3610 if(ISEXPR(ctx -> token))
3612 oberon_expr_t * expr;
3613 expr = oberon_expr(ctx);
3614 oberon_make_return(ctx, expr);
3616 else
3618 oberon_make_return(ctx, NULL);
3623 static void
3624 oberon_statement_seq(oberon_context_t * ctx)
3626 oberon_statement(ctx);
3627 while(ctx -> token == SEMICOLON)
3629 oberon_assert_token(ctx, SEMICOLON);
3630 oberon_statement(ctx);
3634 static void
3635 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
3637 oberon_module_t * m = ctx -> module_list;
3638 while(m && strcmp(m -> name, name) != 0)
3640 m = m -> next;
3643 if(m == NULL)
3645 const char * code;
3646 code = ctx -> import_module(name);
3647 if(code == NULL)
3649 oberon_error(ctx, "no such module");
3652 m = oberon_compile_module(ctx, code);
3653 assert(m);
3656 if(m -> ready == 0)
3658 oberon_error(ctx, "cyclic module import");
3661 oberon_object_t * ident;
3662 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3663 ident -> module = m;
3666 static void
3667 oberon_import_decl(oberon_context_t * ctx)
3669 char * alias;
3670 char * name;
3672 alias = name = oberon_assert_ident(ctx);
3673 if(ctx -> token == ASSIGN)
3675 oberon_assert_token(ctx, ASSIGN);
3676 name = oberon_assert_ident(ctx);
3679 oberon_import_module(ctx, alias, name);
3682 static void
3683 oberon_import_list(oberon_context_t * ctx)
3685 oberon_assert_token(ctx, IMPORT);
3687 oberon_import_decl(ctx);
3688 while(ctx -> token == COMMA)
3690 oberon_assert_token(ctx, COMMA);
3691 oberon_import_decl(ctx);
3694 oberon_assert_token(ctx, SEMICOLON);
3697 static void
3698 oberon_parse_module(oberon_context_t * ctx)
3700 char * name1;
3701 char * name2;
3702 oberon_read_token(ctx);
3704 oberon_assert_token(ctx, MODULE);
3705 name1 = oberon_assert_ident(ctx);
3706 oberon_assert_token(ctx, SEMICOLON);
3707 ctx -> mod -> name = name1;
3709 oberon_generator_init_module(ctx, ctx -> mod);
3711 if(ctx -> token == IMPORT)
3713 oberon_import_list(ctx);
3716 oberon_decl_seq(ctx);
3718 oberon_generate_begin_module(ctx);
3719 if(ctx -> token == BEGIN)
3721 oberon_assert_token(ctx, BEGIN);
3722 oberon_statement_seq(ctx);
3724 oberon_generate_end_module(ctx);
3726 oberon_assert_token(ctx, END);
3727 name2 = oberon_assert_ident(ctx);
3728 oberon_expect_token(ctx, DOT);
3730 if(strcmp(name1, name2) != 0)
3732 oberon_error(ctx, "module name not matched");
3735 oberon_generator_fini_module(ctx -> mod);
3738 // =======================================================================
3739 // LIBRARY
3740 // =======================================================================
3742 static void
3743 register_default_types(oberon_context_t * ctx)
3745 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
3746 oberon_generator_init_type(ctx, ctx -> void_type);
3748 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
3749 ctx -> void_ptr_type -> base = ctx -> void_type;
3750 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
3752 ctx -> string_type = oberon_new_type_string(1);
3753 oberon_generator_init_type(ctx, ctx -> string_type);
3755 ctx -> bool_type = oberon_new_type_boolean();
3756 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
3758 ctx -> byte_type = oberon_new_type_integer(1);
3759 oberon_define_type(ctx -> world_scope, "BYTE", ctx -> byte_type, 1);
3761 ctx -> shortint_type = oberon_new_type_integer(2);
3762 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> shortint_type, 1);
3764 ctx -> int_type = oberon_new_type_integer(4);
3765 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type, 1);
3767 ctx -> longint_type = oberon_new_type_integer(8);
3768 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> longint_type, 1);
3770 ctx -> real_type = oberon_new_type_real(4);
3771 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
3773 ctx -> longreal_type = oberon_new_type_real(8);
3774 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
3776 ctx -> char_type = oberon_new_type_char(1);
3777 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
3779 ctx -> set_type = oberon_new_type_set(4);
3780 oberon_define_type(ctx -> world_scope, "SET", ctx -> set_type, 1);
3783 static void
3784 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
3786 oberon_object_t * proc;
3787 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
3788 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
3789 proc -> type -> sysproc = true;
3790 proc -> type -> genfunc = f;
3791 proc -> type -> genproc = p;
3794 static oberon_expr_t *
3795 oberon_make_min_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3797 if(num_args < 1)
3799 oberon_error(ctx, "too few arguments");
3802 if(num_args > 1)
3804 oberon_error(ctx, "too mach arguments");
3807 oberon_expr_t * arg;
3808 arg = list_args;
3810 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3812 oberon_error(ctx, "MIN accept only type");
3815 oberon_expr_t * expr;
3816 int bits = arg -> result -> size * 8;
3817 switch(arg -> result -> class)
3819 case OBERON_TYPE_INTEGER:
3820 expr = oberon_integer_item(ctx, -powl(2, bits - 1));
3821 break;
3822 case OBERON_TYPE_SET:
3823 expr = oberon_integer_item(ctx, 0);
3824 break;
3825 default:
3826 oberon_error(ctx, "allowed only basic types");
3827 break;
3830 return expr;
3833 static oberon_expr_t *
3834 oberon_make_max_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3836 if(num_args < 1)
3838 oberon_error(ctx, "too few arguments");
3841 if(num_args > 1)
3843 oberon_error(ctx, "too mach arguments");
3846 oberon_expr_t * arg;
3847 arg = list_args;
3849 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3851 oberon_error(ctx, "MAX accept only type");
3854 oberon_expr_t * expr;
3855 int bits = arg -> result -> size * 8;
3856 switch(arg -> result -> class)
3858 case OBERON_TYPE_INTEGER:
3859 expr = oberon_integer_item(ctx, powl(2, bits - 1) - 1);
3860 break;
3861 case OBERON_TYPE_SET:
3862 expr = oberon_integer_item(ctx, bits);
3863 break;
3864 default:
3865 oberon_error(ctx, "allowed only basic types");
3866 break;
3869 return expr;
3872 static oberon_expr_t *
3873 oberon_make_size_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3875 if(num_args < 1)
3877 oberon_error(ctx, "too few arguments");
3880 if(num_args > 1)
3882 oberon_error(ctx, "too mach arguments");
3885 oberon_expr_t * arg;
3886 arg = list_args;
3888 if(!arg -> is_item || arg -> item.mode != MODE_TYPE)
3890 oberon_error(ctx, "SIZE accept only type");
3893 int size;
3894 oberon_expr_t * expr;
3895 oberon_type_t * type = arg -> result;
3896 switch(type -> class)
3898 case OBERON_TYPE_INTEGER:
3899 case OBERON_TYPE_BOOLEAN:
3900 case OBERON_TYPE_REAL:
3901 case OBERON_TYPE_CHAR:
3902 case OBERON_TYPE_SET:
3903 size = type -> size;
3904 break;
3905 default:
3906 oberon_error(ctx, "TODO SIZE");
3907 break;
3910 expr = oberon_integer_item(ctx, size);
3911 return expr;
3914 static oberon_expr_t *
3915 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3917 if(num_args < 1)
3919 oberon_error(ctx, "too few arguments");
3922 if(num_args > 1)
3924 oberon_error(ctx, "too mach arguments");
3927 oberon_expr_t * arg;
3928 arg = list_args;
3929 oberon_check_src(ctx, arg);
3931 oberon_type_t * result_type;
3932 result_type = arg -> result;
3934 if(result_type -> class != OBERON_TYPE_INTEGER)
3936 oberon_error(ctx, "ABS accepts only integers");
3939 oberon_expr_t * expr;
3940 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
3941 return expr;
3944 static void
3945 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3947 if(num_args < 1)
3949 oberon_error(ctx, "too few arguments");
3953 oberon_expr_t * dst;
3954 dst = list_args;
3955 oberon_check_dst(ctx, dst);
3957 oberon_type_t * type;
3958 type = dst -> result;
3960 if(type -> class != OBERON_TYPE_POINTER)
3962 oberon_error(ctx, "not a pointer");
3965 type = type -> base;
3967 oberon_expr_t * src;
3968 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3969 src -> item.num_args = 0;
3970 src -> item.args = NULL;
3972 int max_args = 1;
3973 if(type -> class == OBERON_TYPE_ARRAY)
3975 if(type -> size == 0)
3977 oberon_type_t * x = type;
3978 while(x -> class == OBERON_TYPE_ARRAY)
3980 if(x -> size == 0)
3982 max_args += 1;
3984 x = x -> base;
3988 if(num_args < max_args)
3990 oberon_error(ctx, "too few arguments");
3993 if(num_args > max_args)
3995 oberon_error(ctx, "too mach arguments");
3998 int num_sizes = max_args - 1;
3999 oberon_expr_t * size_list = list_args -> next;
4001 oberon_expr_t * arg = size_list;
4002 for(int i = 0; i < max_args - 1; i++)
4004 oberon_check_src(ctx, arg);
4005 if(arg -> result -> class != OBERON_TYPE_INTEGER)
4007 oberon_error(ctx, "size must be integer");
4009 arg = arg -> next;
4012 src -> item.num_args = num_sizes;
4013 src -> item.args = size_list;
4015 else if(type -> class != OBERON_TYPE_RECORD)
4017 oberon_error(ctx, "oberon_make_new_call: wat");
4020 if(num_args > max_args)
4022 oberon_error(ctx, "too mach arguments");
4025 oberon_assign(ctx, src, dst);
4028 static void
4029 oberon_new_const(oberon_context_t * ctx, char * name, oberon_expr_t * expr)
4031 oberon_object_t * constant;
4032 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST, true, false, false);
4033 oberon_check_const(ctx, expr);
4034 constant -> value = (oberon_item_t *) expr;
4037 oberon_context_t *
4038 oberon_create_context(ModuleImportCallback import_module)
4040 oberon_context_t * ctx = calloc(1, sizeof *ctx);
4042 oberon_scope_t * world_scope;
4043 world_scope = oberon_open_scope(ctx);
4044 ctx -> world_scope = world_scope;
4046 ctx -> import_module = import_module;
4048 oberon_generator_init_context(ctx);
4050 register_default_types(ctx);
4052 /* Constants */
4053 oberon_new_const(ctx, "TRUE", oberon_make_boolean(ctx, true));
4054 oberon_new_const(ctx, "FALSE", oberon_make_boolean(ctx, false));
4056 /* Functions */
4057 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
4058 oberon_new_intrinsic(ctx, "MIN", oberon_make_min_call, NULL);
4059 oberon_new_intrinsic(ctx, "MAX", oberon_make_max_call, NULL);
4060 oberon_new_intrinsic(ctx, "SIZE", oberon_make_size_call, NULL);
4062 /* Procedures */
4063 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
4065 return ctx;
4068 void
4069 oberon_destroy_context(oberon_context_t * ctx)
4071 oberon_generator_destroy_context(ctx);
4072 free(ctx);
4075 oberon_module_t *
4076 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
4078 const char * code = ctx -> code;
4079 int code_index = ctx -> code_index;
4080 char c = ctx -> c;
4081 int token = ctx -> token;
4082 char * string = ctx -> string;
4083 int integer = ctx -> integer;
4084 int real = ctx -> real;
4085 bool longmode = ctx -> longmode;
4086 oberon_scope_t * decl = ctx -> decl;
4087 oberon_module_t * mod = ctx -> mod;
4089 oberon_scope_t * module_scope;
4090 module_scope = oberon_open_scope(ctx);
4092 oberon_module_t * module;
4093 module = calloc(1, sizeof *module);
4094 module -> decl = module_scope;
4095 module -> next = ctx -> module_list;
4097 ctx -> mod = module;
4098 ctx -> module_list = module;
4100 oberon_init_scaner(ctx, newcode);
4101 oberon_parse_module(ctx);
4103 module -> ready = 1;
4105 ctx -> code = code;
4106 ctx -> code_index = code_index;
4107 ctx -> c = c;
4108 ctx -> token = token;
4109 ctx -> string = string;
4110 ctx -> integer = integer;
4111 ctx -> real = real;
4112 ctx -> longmode = longmode;
4113 ctx -> decl = decl;
4114 ctx -> mod = mod;
4116 return module;