DEADSOFTWARE

b428ecf73f6bda15c85365a8d49a5dd6aafb45ad
[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>
9 #include "../include/oberon.h"
11 #include "oberon-internals.h"
12 #include "generator.h"
14 enum {
15 EOF_ = 0,
16 IDENT,
17 MODULE,
18 SEMICOLON,
19 END,
20 DOT,
21 VAR,
22 COLON,
23 BEGIN,
24 ASSIGN,
25 INTEGER,
26 TRUE,
27 FALSE,
28 LPAREN,
29 RPAREN,
30 EQUAL,
31 NEQ,
32 LESS,
33 LEQ,
34 GREAT,
35 GEQ,
36 IN,
37 IS,
38 PLUS,
39 MINUS,
40 OR,
41 STAR,
42 SLASH,
43 DIV,
44 MOD,
45 AND,
46 NOT,
47 PROCEDURE,
48 COMMA,
49 RETURN,
50 CONST,
51 TYPE,
52 ARRAY,
53 OF,
54 LBRACE,
55 RBRACE,
56 RECORD,
57 POINTER,
58 TO,
59 UPARROW,
60 NIL,
61 IMPORT,
62 REAL,
63 CHAR,
64 STRING,
65 IF,
66 THEN,
67 ELSE,
68 ELSIF
69 };
71 // =======================================================================
72 // UTILS
73 // =======================================================================
75 static void
76 oberon_error(oberon_context_t * ctx, const char * fmt, ...)
77 {
78 va_list ptr;
79 va_start(ptr, fmt);
80 fprintf(stderr, "error: ");
81 vfprintf(stderr, fmt, ptr);
82 fprintf(stderr, "\n");
83 fprintf(stderr, " code_index = %i\n", ctx -> code_index);
84 fprintf(stderr, " c = %c\n", ctx -> c);
85 fprintf(stderr, " token = %i\n", ctx -> token);
86 va_end(ptr);
87 exit(1);
88 }
90 static oberon_type_t *
91 oberon_new_type_ptr(int class)
92 {
93 oberon_type_t * x = malloc(sizeof *x);
94 memset(x, 0, sizeof *x);
95 x -> class = class;
96 return x;
97 }
99 static oberon_type_t *
100 oberon_new_type_integer(int size)
102 oberon_type_t * x;
103 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
104 x -> size = size;
105 return x;
108 static oberon_type_t *
109 oberon_new_type_boolean()
111 oberon_type_t * x;
112 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
113 return x;
116 static oberon_type_t *
117 oberon_new_type_real(int size)
119 oberon_type_t * x;
120 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
121 x -> size = size;
122 return x;
125 static oberon_type_t *
126 oberon_new_type_char(int size)
128 oberon_type_t * x;
129 x = oberon_new_type_ptr(OBERON_TYPE_CHAR);
130 x -> size = size;
131 return x;
134 static oberon_type_t *
135 oberon_new_type_string(int size)
137 oberon_type_t * x;
138 x = oberon_new_type_ptr(OBERON_TYPE_STRING);
139 x -> size = size;
140 return x;
143 // =======================================================================
144 // TABLE
145 // =======================================================================
147 static oberon_scope_t *
148 oberon_open_scope(oberon_context_t * ctx)
150 oberon_scope_t * scope = calloc(1, sizeof *scope);
151 oberon_object_t * list = calloc(1, sizeof *list);
153 scope -> ctx = ctx;
154 scope -> list = list;
155 scope -> up = ctx -> decl;
157 if(scope -> up)
159 scope -> local = scope -> up -> local;
160 scope -> parent = scope -> up -> parent;
161 scope -> parent_type = scope -> up -> parent_type;
164 ctx -> decl = scope;
165 return scope;
168 static void
169 oberon_close_scope(oberon_scope_t * scope)
171 oberon_context_t * ctx = scope -> ctx;
172 ctx -> decl = scope -> up;
175 static oberon_object_t *
176 oberon_find_object_in_list(oberon_object_t * list, char * name)
178 oberon_object_t * x = list;
179 while(x -> next && strcmp(x -> next -> name, name) != 0)
181 x = x -> next;
183 return x -> next;
186 static oberon_object_t *
187 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
189 oberon_object_t * result = NULL;
191 oberon_scope_t * s = scope;
192 while(result == NULL && s != NULL)
194 result = oberon_find_object_in_list(s -> list, name);
195 s = s -> up;
198 if(check_it && result == NULL)
200 oberon_error(scope -> ctx, "undefined ident %s", name);
203 return result;
206 static oberon_object_t *
207 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
209 if(check_upscope)
211 if(oberon_find_object(scope -> up, name, false))
213 oberon_error(scope -> ctx, "already defined");
217 oberon_object_t * x = scope -> list;
218 while(x -> next && strcmp(x -> next -> name, name) != 0)
220 x = x -> next;
223 if(x -> next)
225 oberon_error(scope -> ctx, "already defined");
228 oberon_object_t * newvar = malloc(sizeof *newvar);
229 memset(newvar, 0, sizeof *newvar);
230 newvar -> name = name;
231 newvar -> class = class;
232 newvar -> export = export;
233 newvar -> read_only = read_only;
234 newvar -> local = scope -> local;
235 newvar -> parent = scope -> parent;
236 newvar -> parent_type = scope -> parent_type;
237 newvar -> module = scope -> ctx -> mod;
239 x -> next = newvar;
241 return newvar;
244 static oberon_object_t *
245 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
247 oberon_object_t * id;
248 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
249 id -> type = type;
250 oberon_generator_init_type(scope -> ctx, type);
251 return id;
254 // =======================================================================
255 // SCANER
256 // =======================================================================
258 static void
259 oberon_get_char(oberon_context_t * ctx)
261 if(ctx -> code[ctx -> code_index])
263 ctx -> code_index += 1;
264 ctx -> c = ctx -> code[ctx -> code_index];
268 static void
269 oberon_init_scaner(oberon_context_t * ctx, const char * code)
271 ctx -> code = code;
272 ctx -> code_index = 0;
273 ctx -> c = ctx -> code[ctx -> code_index];
276 static void
277 oberon_read_ident(oberon_context_t * ctx)
279 int len = 0;
280 int i = ctx -> code_index;
282 int c = ctx -> code[i];
283 while(isalnum(c))
285 i += 1;
286 len += 1;
287 c = ctx -> code[i];
290 char * ident = malloc(len + 1);
291 memcpy(ident, &ctx->code[ctx->code_index], len);
292 ident[len] = 0;
294 ctx -> code_index = i;
295 ctx -> c = ctx -> code[i];
296 ctx -> string = ident;
297 ctx -> token = IDENT;
299 if(strcmp(ident, "MODULE") == 0)
301 ctx -> token = MODULE;
303 else if(strcmp(ident, "END") == 0)
305 ctx -> token = END;
307 else if(strcmp(ident, "VAR") == 0)
309 ctx -> token = VAR;
311 else if(strcmp(ident, "BEGIN") == 0)
313 ctx -> token = BEGIN;
315 else if(strcmp(ident, "TRUE") == 0)
317 ctx -> token = TRUE;
319 else if(strcmp(ident, "FALSE") == 0)
321 ctx -> token = FALSE;
323 else if(strcmp(ident, "OR") == 0)
325 ctx -> token = OR;
327 else if(strcmp(ident, "DIV") == 0)
329 ctx -> token = DIV;
331 else if(strcmp(ident, "MOD") == 0)
333 ctx -> token = MOD;
335 else if(strcmp(ident, "PROCEDURE") == 0)
337 ctx -> token = PROCEDURE;
339 else if(strcmp(ident, "RETURN") == 0)
341 ctx -> token = RETURN;
343 else if(strcmp(ident, "CONST") == 0)
345 ctx -> token = CONST;
347 else if(strcmp(ident, "TYPE") == 0)
349 ctx -> token = TYPE;
351 else if(strcmp(ident, "ARRAY") == 0)
353 ctx -> token = ARRAY;
355 else if(strcmp(ident, "OF") == 0)
357 ctx -> token = OF;
359 else if(strcmp(ident, "RECORD") == 0)
361 ctx -> token = RECORD;
363 else if(strcmp(ident, "POINTER") == 0)
365 ctx -> token = POINTER;
367 else if(strcmp(ident, "TO") == 0)
369 ctx -> token = TO;
371 else if(strcmp(ident, "NIL") == 0)
373 ctx -> token = NIL;
375 else if(strcmp(ident, "IMPORT") == 0)
377 ctx -> token = IMPORT;
379 else if(strcmp(ident, "IN") == 0)
381 ctx -> token = IN;
383 else if(strcmp(ident, "IS") == 0)
385 ctx -> token = IS;
387 else if(strcmp(ident, "IF") == 0)
389 ctx -> token = IF;
391 else if(strcmp(ident, "THEN") == 0)
393 ctx -> token = THEN;
395 else if(strcmp(ident, "ELSE") == 0)
397 ctx -> token = ELSE;
399 else if(strcmp(ident, "ELSIF") == 0)
401 ctx -> token = ELSIF;
405 static void
406 oberon_read_number(oberon_context_t * ctx)
408 long integer;
409 double real;
410 char * ident;
411 int start_i;
412 int exp_i;
413 int end_i;
415 /*
416 * mode = 0 == DEC
417 * mode = 1 == HEX
418 * mode = 2 == REAL
419 * mode = 3 == LONGREAL
420 * mode = 4 == CHAR
421 */
422 int mode = 0;
423 start_i = ctx -> code_index;
425 while(isdigit(ctx -> c))
427 oberon_get_char(ctx);
430 end_i = ctx -> code_index;
432 if(isxdigit(ctx -> c))
434 mode = 1;
435 while(isxdigit(ctx -> c))
437 oberon_get_char(ctx);
440 end_i = ctx -> code_index;
442 if(ctx -> c == 'H')
444 mode = 1;
445 oberon_get_char(ctx);
447 else if(ctx -> c == 'X')
449 mode = 4;
450 oberon_get_char(ctx);
452 else
454 oberon_error(ctx, "invalid hex number");
457 else if(ctx -> c == '.')
459 mode = 2;
460 oberon_get_char(ctx);
462 while(isdigit(ctx -> c))
464 oberon_get_char(ctx);
467 if(ctx -> c == 'E' || ctx -> c == 'D')
469 exp_i = ctx -> code_index;
471 if(ctx -> c == 'D')
473 mode = 3;
476 oberon_get_char(ctx);
478 if(ctx -> c == '+' || ctx -> c == '-')
480 oberon_get_char(ctx);
483 while(isdigit(ctx -> c))
485 oberon_get_char(ctx);
490 end_i = ctx -> code_index;
493 if(mode == 0)
495 if(ctx -> c == 'H')
497 mode = 1;
498 oberon_get_char(ctx);
500 else if(ctx -> c == 'X')
502 mode = 4;
503 oberon_get_char(ctx);
507 int len = end_i - start_i;
508 ident = malloc(len + 1);
509 memcpy(ident, &ctx -> code[start_i], len);
510 ident[len] = 0;
512 ctx -> longmode = false;
513 if(mode == 3)
515 int i = exp_i - start_i;
516 ident[i] = 'E';
517 ctx -> longmode = true;
520 switch(mode)
522 case 0:
523 integer = atol(ident);
524 real = integer;
525 ctx -> token = INTEGER;
526 break;
527 case 1:
528 sscanf(ident, "%lx", &integer);
529 real = integer;
530 ctx -> token = INTEGER;
531 break;
532 case 2:
533 case 3:
534 sscanf(ident, "%lf", &real);
535 ctx -> token = REAL;
536 break;
537 case 4:
538 sscanf(ident, "%lx", &integer);
539 real = integer;
540 ctx -> token = CHAR;
541 break;
542 default:
543 oberon_error(ctx, "oberon_read_number: wat");
544 break;
547 ctx -> string = ident;
548 ctx -> integer = integer;
549 ctx -> real = real;
552 static void
553 oberon_skip_space(oberon_context_t * ctx)
555 while(isspace(ctx -> c))
557 oberon_get_char(ctx);
561 static void
562 oberon_read_comment(oberon_context_t * ctx)
564 int nesting = 1;
565 while(nesting >= 1)
567 if(ctx -> c == '(')
569 oberon_get_char(ctx);
570 if(ctx -> c == '*')
572 oberon_get_char(ctx);
573 nesting += 1;
576 else if(ctx -> c == '*')
578 oberon_get_char(ctx);
579 if(ctx -> c == ')')
581 oberon_get_char(ctx);
582 nesting -= 1;
585 else if(ctx -> c == 0)
587 oberon_error(ctx, "unterminated comment");
589 else
591 oberon_get_char(ctx);
596 static void oberon_read_string(oberon_context_t * ctx)
598 int c = ctx -> c;
599 oberon_get_char(ctx);
601 int start = ctx -> code_index;
603 while(ctx -> c != 0 && ctx -> c != c)
605 oberon_get_char(ctx);
608 if(ctx -> c == 0)
610 oberon_error(ctx, "unterminated string");
613 int end = ctx -> code_index;
615 oberon_get_char(ctx);
617 char * string = calloc(1, end - start + 1);
618 strncpy(string, &ctx -> code[start], end - start);
620 ctx -> token = STRING;
621 ctx -> string = string;
623 printf("oberon_read_string: string ((%s))\n", string);
626 static void oberon_read_token(oberon_context_t * ctx);
628 static void
629 oberon_read_symbol(oberon_context_t * ctx)
631 int c = ctx -> c;
632 switch(c)
634 case 0:
635 ctx -> token = EOF_;
636 break;
637 case ';':
638 ctx -> token = SEMICOLON;
639 oberon_get_char(ctx);
640 break;
641 case ':':
642 ctx -> token = COLON;
643 oberon_get_char(ctx);
644 if(ctx -> c == '=')
646 ctx -> token = ASSIGN;
647 oberon_get_char(ctx);
649 break;
650 case '.':
651 ctx -> token = DOT;
652 oberon_get_char(ctx);
653 break;
654 case '(':
655 ctx -> token = LPAREN;
656 oberon_get_char(ctx);
657 if(ctx -> c == '*')
659 oberon_get_char(ctx);
660 oberon_read_comment(ctx);
661 oberon_read_token(ctx);
663 break;
664 case ')':
665 ctx -> token = RPAREN;
666 oberon_get_char(ctx);
667 break;
668 case '=':
669 ctx -> token = EQUAL;
670 oberon_get_char(ctx);
671 break;
672 case '#':
673 ctx -> token = NEQ;
674 oberon_get_char(ctx);
675 break;
676 case '<':
677 ctx -> token = LESS;
678 oberon_get_char(ctx);
679 if(ctx -> c == '=')
681 ctx -> token = LEQ;
682 oberon_get_char(ctx);
684 break;
685 case '>':
686 ctx -> token = GREAT;
687 oberon_get_char(ctx);
688 if(ctx -> c == '=')
690 ctx -> token = GEQ;
691 oberon_get_char(ctx);
693 break;
694 case '+':
695 ctx -> token = PLUS;
696 oberon_get_char(ctx);
697 break;
698 case '-':
699 ctx -> token = MINUS;
700 oberon_get_char(ctx);
701 break;
702 case '*':
703 ctx -> token = STAR;
704 oberon_get_char(ctx);
705 if(ctx -> c == ')')
707 oberon_get_char(ctx);
708 oberon_error(ctx, "unstarted comment");
710 break;
711 case '/':
712 ctx -> token = SLASH;
713 oberon_get_char(ctx);
714 break;
715 case '&':
716 ctx -> token = AND;
717 oberon_get_char(ctx);
718 break;
719 case '~':
720 ctx -> token = NOT;
721 oberon_get_char(ctx);
722 break;
723 case ',':
724 ctx -> token = COMMA;
725 oberon_get_char(ctx);
726 break;
727 case '[':
728 ctx -> token = LBRACE;
729 oberon_get_char(ctx);
730 break;
731 case ']':
732 ctx -> token = RBRACE;
733 oberon_get_char(ctx);
734 break;
735 case '^':
736 ctx -> token = UPARROW;
737 oberon_get_char(ctx);
738 break;
739 case '"':
740 oberon_read_string(ctx);
741 break;
742 case '\'':
743 oberon_read_string(ctx);
744 break;
745 default:
746 oberon_error(ctx, "invalid char %c", ctx -> c);
747 break;
751 static void
752 oberon_read_token(oberon_context_t * ctx)
754 oberon_skip_space(ctx);
756 int c = ctx -> c;
757 if(isalpha(c))
759 oberon_read_ident(ctx);
761 else if(isdigit(c))
763 oberon_read_number(ctx);
765 else
767 oberon_read_symbol(ctx);
771 // =======================================================================
772 // EXPRESSION
773 // =======================================================================
775 static void oberon_expect_token(oberon_context_t * ctx, int token);
776 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
777 static void oberon_assert_token(oberon_context_t * ctx, int token);
778 static char * oberon_assert_ident(oberon_context_t * ctx);
779 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
780 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
781 static oberon_expr_t * oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr);
783 static oberon_expr_t *
784 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
786 oberon_oper_t * operator;
787 operator = malloc(sizeof *operator);
788 memset(operator, 0, sizeof *operator);
790 operator -> is_item = 0;
791 operator -> result = result;
792 operator -> read_only = 1;
793 operator -> op = op;
794 operator -> left = left;
795 operator -> right = right;
797 return (oberon_expr_t *) operator;
800 static oberon_expr_t *
801 oberon_new_item(int mode, oberon_type_t * result, int read_only)
803 oberon_item_t * item;
804 item = malloc(sizeof *item);
805 memset(item, 0, sizeof *item);
807 item -> is_item = 1;
808 item -> result = result;
809 item -> read_only = read_only;
810 item -> mode = mode;
812 return (oberon_expr_t *)item;
815 static oberon_expr_t *
816 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
818 oberon_expr_t * expr;
819 oberon_type_t * result;
821 result = a -> result;
823 if(token == MINUS)
825 if(result -> class != OBERON_TYPE_INTEGER)
827 oberon_error(ctx, "incompatible operator type");
830 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
832 else if(token == NOT)
834 if(result -> class != OBERON_TYPE_BOOLEAN)
836 oberon_error(ctx, "incompatible operator type");
839 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
841 else
843 oberon_error(ctx, "oberon_make_unary_op: wat");
846 return expr;
849 static void
850 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
852 oberon_expr_t * last;
854 *num_expr = 1;
855 if(const_expr)
857 *first = last = (oberon_expr_t *) oberon_const_expr(ctx);
859 else
861 *first = last = oberon_expr(ctx);
863 while(ctx -> token == COMMA)
865 oberon_assert_token(ctx, COMMA);
866 oberon_expr_t * current;
868 if(const_expr)
870 current = (oberon_expr_t *) oberon_const_expr(ctx);
872 else
874 current = oberon_expr(ctx);
877 last -> next = current;
878 last = current;
879 *num_expr += 1;
883 static oberon_expr_t *
884 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
886 return oberon_new_operator(OP_CAST, pref, expr, NULL);
889 static oberon_expr_t *
890 oberno_make_record_cast(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * rec)
892 oberon_type_t * from = expr -> result;
893 oberon_type_t * to = rec;
895 printf("oberno_make_record_cast: from class %i to class %i\n", from -> class, to -> class);
897 if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
899 printf("oberno_make_record_cast: pointers\n");
900 from = from -> base;
901 to = to -> base;
904 if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
906 oberon_error(ctx, "must be record type");
909 return oberon_cast_expr(ctx, expr, rec);
912 static oberon_type_t *
913 oberon_get_equal_expr_type(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
915 oberon_type_t * result;
916 if(a -> class == OBERON_TYPE_REAL && b -> class == OBERON_TYPE_INTEGER)
918 result = a;
920 else if(b -> class == OBERON_TYPE_REAL && a -> class == OBERON_TYPE_INTEGER)
922 result = b;
924 else if(a -> class != b -> class)
926 oberon_error(ctx, "oberon_get_equal_expr_type: incompatible types");
928 else if(a -> size > b -> size)
930 result = a;
932 else
934 result = b;
937 return result;
940 static void
941 oberon_check_record_compatibility(oberon_context_t * ctx, oberon_type_t * from, oberon_type_t * to)
943 if(from -> class == OBERON_TYPE_POINTER && to -> class == OBERON_TYPE_POINTER)
945 from = from -> base;
946 to = to -> base;
949 if(from -> class != OBERON_TYPE_RECORD || to -> class != OBERON_TYPE_RECORD)
951 oberon_error(ctx, "not a record");
954 oberon_type_t * t = from;
955 while(t != NULL && t != to)
957 t = t -> base;
960 if(t == NULL)
962 oberon_error(ctx, "incompatible record types");
966 static oberon_expr_t *
967 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
969 // Допускается:
970 // Если классы типов равны
971 // Если INTEGER переводится в REAL
972 // Есди STRING переводится в ARRAY OF CHAR
974 bool error = false;
975 if(pref -> class != expr -> result -> class)
977 printf("expr class %i\n", expr -> result -> class);
978 printf("pref class %i\n", pref -> class);
980 if(expr -> result -> class == OBERON_TYPE_STRING)
982 if(pref -> class == OBERON_TYPE_ARRAY)
984 if(pref -> base -> class != OBERON_TYPE_CHAR)
986 error = true;
989 else
991 error = true;
994 else if(expr -> result -> class == OBERON_TYPE_INTEGER)
996 if(pref -> class != OBERON_TYPE_REAL)
998 error = true;
1001 else
1003 error = true;
1007 if(error)
1009 oberon_error(ctx, "oberon_autocast_to: incompatible types");
1012 if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL)
1014 if(expr -> result -> size > pref -> size)
1016 oberon_error(ctx, "incompatible size");
1018 else
1020 expr = oberon_cast_expr(ctx, expr, pref);
1023 else if(pref -> class == OBERON_TYPE_RECORD)
1025 oberon_check_record_compatibility(ctx, expr -> result, pref);
1026 expr = oberno_make_record_cast(ctx, expr, pref);
1028 else if(pref -> class == OBERON_TYPE_POINTER)
1030 assert(pref -> base);
1031 if(expr -> result -> base -> class == OBERON_TYPE_RECORD)
1033 oberon_check_record_compatibility(ctx, expr -> result, pref);
1034 expr = oberno_make_record_cast(ctx, expr, pref);
1036 else if(expr -> result -> base != pref -> base)
1038 if(expr -> result -> base -> class != OBERON_TYPE_VOID)
1040 oberon_error(ctx, "incompatible pointer types");
1045 return expr;
1048 static void
1049 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_expr_t ** ea, oberon_expr_t ** eb)
1051 oberon_type_t * a = (*ea) -> result;
1052 oberon_type_t * b = (*eb) -> result;
1053 oberon_type_t * preq = oberon_get_equal_expr_type(ctx, a, b);
1054 *ea = oberon_autocast_to(ctx, *ea, preq);
1055 *eb = oberon_autocast_to(ctx, *eb, preq);
1058 static void
1059 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
1061 if(desig -> mode != MODE_CALL)
1063 oberon_error(ctx, "expected mode CALL");
1066 oberon_type_t * fn = desig -> parent -> result;
1067 int num_args = desig -> num_args;
1068 int num_decl = fn -> num_decl;
1070 if(num_args < num_decl)
1072 oberon_error(ctx, "too few arguments");
1074 else if(num_args > num_decl)
1076 oberon_error(ctx, "too many arguments");
1079 /* Делаем проверку на запись и делаем автокаст */
1080 oberon_expr_t * casted[num_args];
1081 oberon_expr_t * arg = desig -> args;
1082 oberon_object_t * param = fn -> decl;
1083 for(int i = 0; i < num_args; i++)
1085 if(param -> class == OBERON_CLASS_VAR_PARAM)
1087 if(arg -> read_only)
1089 oberon_error(ctx, "assign to read-only var");
1093 casted[i] = oberon_autocast_to(ctx, arg, param -> type);
1094 arg = arg -> next;
1095 param = param -> next;
1098 /* Создаём новый список выражений */
1099 if(num_args > 0)
1101 arg = casted[0];
1102 for(int i = 0; i < num_args - 1; i++)
1104 casted[i] -> next = casted[i + 1];
1106 desig -> args = arg;
1110 static oberon_expr_t *
1111 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1113 oberon_type_t * signature = item -> result;
1114 if(signature -> class != OBERON_TYPE_PROCEDURE)
1116 oberon_error(ctx, "not a procedure");
1119 oberon_expr_t * call;
1121 if(signature -> sysproc)
1123 if(signature -> genfunc == NULL)
1125 oberon_error(ctx, "not a function-procedure");
1128 call = signature -> genfunc(ctx, num_args, list_args);
1130 else
1132 if(signature -> base -> class == OBERON_TYPE_VOID)
1134 oberon_error(ctx, "attempt to call procedure in expression");
1137 call = oberon_new_item(MODE_CALL, signature -> base, true);
1138 call -> item.parent = item;
1139 call -> item.num_args = num_args;
1140 call -> item.args = list_args;
1141 oberon_autocast_call(ctx, (oberon_item_t *) call);
1144 return call;
1147 static void
1148 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
1150 oberon_type_t * signature = item -> result;
1151 if(signature -> class != OBERON_TYPE_PROCEDURE)
1153 oberon_error(ctx, "not a procedure");
1156 oberon_expr_t * call;
1158 if(signature -> sysproc)
1160 if(signature -> genproc == NULL)
1162 oberon_error(ctx, "not a procedure");
1165 signature -> genproc(ctx, num_args, list_args);
1167 else
1169 if(signature -> base -> class != OBERON_TYPE_VOID)
1171 oberon_error(ctx, "attempt to call function as non-typed procedure");
1174 call = oberon_new_item(MODE_CALL, signature -> base, true);
1175 call -> item.parent = item;
1176 call -> item.num_args = num_args;
1177 call -> item.args = list_args;
1178 oberon_autocast_call(ctx, (oberon_item_t *) call);
1179 oberon_generate_call_proc(ctx, call);
1183 /*
1184 static void
1185 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
1187 switch(proc -> class)
1189 case OBERON_CLASS_PROC:
1190 if(proc -> class != OBERON_CLASS_PROC)
1192 oberon_error(ctx, "not a procedure");
1194 break;
1195 case OBERON_CLASS_VAR:
1196 case OBERON_CLASS_VAR_PARAM:
1197 case OBERON_CLASS_PARAM:
1198 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
1200 oberon_error(ctx, "not a procedure");
1202 break;
1203 default:
1204 oberon_error(ctx, "not a procedure");
1205 break;
1208 if(proc -> sysproc)
1210 if(proc -> genproc == NULL)
1212 oberon_error(ctx, "requres non-typed procedure");
1215 proc -> genproc(ctx, num_args, list_args);
1217 else
1219 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
1221 oberon_error(ctx, "attempt to call function as non-typed procedure");
1224 oberon_expr_t * call;
1225 call = oberon_new_item(MODE_CALL, proc -> type -> base, 1);
1226 call -> item.var = proc;
1227 call -> item.num_args = num_args;
1228 call -> item.args = list_args;
1229 oberon_autocast_call(ctx, call);
1230 oberon_generate_call_proc(ctx, call);
1233 */
1235 #define ISEXPR(x) \
1236 (((x) == PLUS) \
1237 || ((x) == MINUS) \
1238 || ((x) == IDENT) \
1239 || ((x) == INTEGER) \
1240 || ((x) == REAL) \
1241 || ((x) == CHAR) \
1242 || ((x) == STRING) \
1243 || ((x) == NIL) \
1244 || ((x) == LPAREN) \
1245 || ((x) == NOT) \
1246 || ((x) == TRUE) \
1247 || ((x) == FALSE))
1249 static oberon_expr_t *
1250 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1252 printf("oberno_make_dereferencing\n");
1253 if(expr -> result -> class != OBERON_TYPE_POINTER)
1255 oberon_error(ctx, "not a pointer");
1258 assert(expr -> is_item);
1260 oberon_expr_t * selector;
1261 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
1262 selector -> item.parent = (oberon_item_t *) expr;
1264 return selector;
1267 static oberon_expr_t *
1268 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1270 if(desig -> result -> class == OBERON_TYPE_POINTER)
1272 desig = oberno_make_dereferencing(ctx, desig);
1275 assert(desig -> is_item);
1277 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1279 oberon_error(ctx, "not array");
1282 oberon_type_t * base;
1283 base = desig -> result -> base;
1285 if(index -> result -> class != OBERON_TYPE_INTEGER)
1287 oberon_error(ctx, "index must be integer");
1290 // Статическая проверка границ массива
1291 if(desig -> result -> size != 0)
1293 if(index -> is_item)
1295 if(index -> item.mode == MODE_INTEGER)
1297 int arr_size = desig -> result -> size;
1298 int index_int = index -> item.integer;
1299 if(index_int < 0 || index_int > arr_size - 1)
1301 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1307 oberon_expr_t * selector;
1308 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1309 selector -> item.parent = (oberon_item_t *) desig;
1310 selector -> item.num_args = 1;
1311 selector -> item.args = index;
1313 return selector;
1316 static oberon_expr_t *
1317 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1319 if(expr -> result -> class == OBERON_TYPE_POINTER)
1321 expr = oberno_make_dereferencing(ctx, expr);
1324 assert(expr -> is_item);
1326 if(expr -> result -> class != OBERON_TYPE_RECORD)
1328 oberon_error(ctx, "not record");
1331 oberon_type_t * rec = expr -> result;
1333 oberon_object_t * field;
1334 field = oberon_find_object(rec -> scope, name, true);
1336 if(field -> export == 0)
1338 if(field -> module != ctx -> mod)
1340 oberon_error(ctx, "field not exported");
1344 int read_only = 0;
1345 if(field -> read_only)
1347 if(field -> module != ctx -> mod)
1349 read_only = 1;
1353 oberon_expr_t * selector;
1354 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1355 selector -> item.var = field;
1356 selector -> item.parent = (oberon_item_t *) expr;
1358 return selector;
1361 #define ISSELECTOR(x) \
1362 (((x) == LBRACE) \
1363 || ((x) == DOT) \
1364 || ((x) == UPARROW) \
1365 || ((x) == LPAREN))
1367 static oberon_object_t *
1368 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1370 char * name;
1371 oberon_object_t * x;
1373 name = oberon_assert_ident(ctx);
1374 x = oberon_find_object(ctx -> decl, name, check);
1376 if(x != NULL)
1378 if(x -> class == OBERON_CLASS_MODULE)
1380 oberon_assert_token(ctx, DOT);
1381 name = oberon_assert_ident(ctx);
1382 /* Наличие объектов в левых модулях всегда проверяется */
1383 x = oberon_find_object(x -> module -> decl, name, 1);
1385 if(x -> export == 0)
1387 oberon_error(ctx, "not exported");
1392 if(xname)
1394 *xname = name;
1397 return x;
1400 static oberon_expr_t *
1401 oberon_designator(oberon_context_t * ctx)
1403 char * name;
1404 oberon_object_t * var;
1405 oberon_expr_t * expr;
1407 var = oberon_qualident(ctx, NULL, 1);
1409 int read_only = 0;
1410 if(var -> read_only)
1412 if(var -> module != ctx -> mod)
1414 read_only = 1;
1418 switch(var -> class)
1420 case OBERON_CLASS_CONST:
1421 // TODO copy value
1422 expr = (oberon_expr_t *) var -> value;
1423 break;
1424 case OBERON_CLASS_VAR:
1425 case OBERON_CLASS_VAR_PARAM:
1426 case OBERON_CLASS_PARAM:
1427 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1428 break;
1429 case OBERON_CLASS_PROC:
1430 expr = oberon_new_item(MODE_VAR, var -> type, 1);
1431 break;
1432 default:
1433 oberon_error(ctx, "invalid designator");
1434 break;
1436 expr -> item.var = var;
1438 while(expr -> result -> class != OBERON_TYPE_PROCEDURE && ISSELECTOR(ctx -> token))
1440 switch(ctx -> token)
1442 case DOT:
1443 oberon_assert_token(ctx, DOT);
1444 name = oberon_assert_ident(ctx);
1445 expr = oberon_make_record_selector(ctx, expr, name);
1446 break;
1447 case LBRACE:
1448 oberon_assert_token(ctx, LBRACE);
1449 int num_indexes = 0;
1450 oberon_expr_t * indexes = NULL;
1451 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1452 oberon_assert_token(ctx, RBRACE);
1454 for(int i = 0; i < num_indexes; i++)
1456 expr = oberon_make_array_selector(ctx, expr, indexes);
1457 indexes = indexes -> next;
1459 break;
1460 case UPARROW:
1461 oberon_assert_token(ctx, UPARROW);
1462 expr = oberno_make_dereferencing(ctx, expr);
1463 break;
1464 case LPAREN:
1465 oberon_assert_token(ctx, LPAREN);
1466 oberon_object_t * objtype = oberon_qualident(ctx, NULL, 1);
1467 if(objtype -> class != OBERON_CLASS_TYPE)
1469 oberon_error(ctx, "must be type");
1471 oberon_assert_token(ctx, RPAREN);
1472 expr = oberno_make_record_cast(ctx, expr, objtype -> type);
1473 break;
1474 default:
1475 oberon_error(ctx, "oberon_designator: wat");
1476 break;
1480 return expr;
1483 static oberon_expr_t *
1484 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1486 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1487 if(ctx -> token == LPAREN)
1489 oberon_assert_token(ctx, LPAREN);
1491 int num_args = 0;
1492 oberon_expr_t * arguments = NULL;
1494 if(ISEXPR(ctx -> token))
1496 oberon_expr_list(ctx, &num_args, &arguments, 0);
1499 assert(expr -> is_item == 1);
1500 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1502 oberon_assert_token(ctx, RPAREN);
1505 return expr;
1508 static void
1509 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1511 assert(expr -> is_item);
1513 int num_args = 0;
1514 oberon_expr_t * arguments = NULL;
1516 if(ctx -> token == LPAREN)
1518 oberon_assert_token(ctx, LPAREN);
1520 if(ISEXPR(ctx -> token))
1522 oberon_expr_list(ctx, &num_args, &arguments, 0);
1525 oberon_assert_token(ctx, RPAREN);
1528 /* Вызов происходит даже без скобок */
1529 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1532 static oberon_type_t *
1533 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
1535 if(i >= -128 && i <= 127)
1537 return ctx -> byte_type;
1539 else if(i >= -32768 && i <= 32767)
1541 return ctx -> shortint_type;
1543 else if(i >= -2147483648 && i <= 2147483647)
1545 return ctx -> int_type;
1547 else
1549 return ctx -> longint_type;
1553 static oberon_expr_t *
1554 oberon_factor(oberon_context_t * ctx)
1556 oberon_expr_t * expr;
1557 oberon_type_t * result;
1559 switch(ctx -> token)
1561 case IDENT:
1562 expr = oberon_designator(ctx);
1563 expr = oberon_opt_func_parens(ctx, expr);
1564 break;
1565 case INTEGER:
1566 result = oberon_get_type_of_int_value(ctx, ctx -> integer);
1567 expr = oberon_new_item(MODE_INTEGER, result, true);
1568 expr -> item.integer = ctx -> integer;
1569 oberon_assert_token(ctx, INTEGER);
1570 break;
1571 case CHAR:
1572 result = ctx -> char_type;
1573 expr = oberon_new_item(MODE_CHAR, result, true);
1574 expr -> item.integer = ctx -> integer;
1575 oberon_assert_token(ctx, CHAR);
1576 break;
1577 case STRING:
1578 result = ctx -> string_type;
1579 expr = oberon_new_item(MODE_STRING, result, true);
1580 expr -> item.string = ctx -> string;
1581 oberon_assert_token(ctx, STRING);
1582 break;
1583 case REAL:
1584 result = (ctx -> longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
1585 expr = oberon_new_item(MODE_REAL, result, 1);
1586 expr -> item.real = ctx -> real;
1587 oberon_assert_token(ctx, REAL);
1588 break;
1589 case TRUE:
1590 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
1591 expr -> item.boolean = true;
1592 oberon_assert_token(ctx, TRUE);
1593 break;
1594 case FALSE:
1595 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, true);
1596 expr -> item.boolean = false;
1597 oberon_assert_token(ctx, FALSE);
1598 break;
1599 case LPAREN:
1600 oberon_assert_token(ctx, LPAREN);
1601 expr = oberon_expr(ctx);
1602 oberon_assert_token(ctx, RPAREN);
1603 break;
1604 case NOT:
1605 oberon_assert_token(ctx, NOT);
1606 expr = oberon_factor(ctx);
1607 expr = oberon_make_unary_op(ctx, NOT, expr);
1608 break;
1609 case NIL:
1610 oberon_assert_token(ctx, NIL);
1611 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, true);
1612 break;
1613 default:
1614 oberon_error(ctx, "invalid expression");
1617 return expr;
1620 #define ITMAKESBOOLEAN(x) \
1621 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1623 #define ITUSEONLYINTEGER(x) \
1624 ((x) >= LESS && (x) <= GEQ)
1626 #define ITUSEONLYBOOLEAN(x) \
1627 (((x) == OR) || ((x) == AND))
1629 static void
1630 oberon_autocast_to_real(oberon_context_t * ctx, oberon_expr_t ** e)
1632 oberon_expr_t * expr = *e;
1633 if(expr -> result -> class == OBERON_TYPE_INTEGER)
1635 if(expr -> result -> size <= ctx -> real_type -> size)
1637 *e = oberon_cast_expr(ctx, expr, ctx -> real_type);
1639 else
1641 *e = oberon_cast_expr(ctx, expr, ctx -> longreal_type);
1644 else if(expr -> result -> class != OBERON_TYPE_REAL)
1646 oberon_error(ctx, "required numeric type");
1650 static oberon_expr_t *
1651 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1653 oberon_expr_t * expr;
1654 oberon_type_t * result;
1656 if(ITMAKESBOOLEAN(token))
1658 if(ITUSEONLYINTEGER(token))
1660 if(a -> result -> class == OBERON_TYPE_INTEGER
1661 || b -> result -> class == OBERON_TYPE_INTEGER
1662 || a -> result -> class == OBERON_TYPE_REAL
1663 || b -> result -> class == OBERON_TYPE_REAL)
1665 // accept
1667 else
1669 oberon_error(ctx, "used only with numeric types");
1672 else if(ITUSEONLYBOOLEAN(token))
1674 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1675 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1677 oberon_error(ctx, "used only with boolean type");
1681 oberon_autocast_binary_op(ctx, &a, &b);
1682 result = ctx -> bool_type;
1684 if(token == EQUAL)
1686 expr = oberon_new_operator(OP_EQ, result, a, b);
1688 else if(token == NEQ)
1690 expr = oberon_new_operator(OP_NEQ, result, a, b);
1692 else if(token == LESS)
1694 expr = oberon_new_operator(OP_LSS, result, a, b);
1696 else if(token == LEQ)
1698 expr = oberon_new_operator(OP_LEQ, result, a, b);
1700 else if(token == GREAT)
1702 expr = oberon_new_operator(OP_GRT, result, a, b);
1704 else if(token == GEQ)
1706 expr = oberon_new_operator(OP_GEQ, result, a, b);
1708 else if(token == OR)
1710 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1712 else if(token == AND)
1714 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1716 else
1718 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1721 else if(token == SLASH)
1723 oberon_autocast_to_real(ctx, &a);
1724 oberon_autocast_to_real(ctx, &b);
1725 oberon_autocast_binary_op(ctx, &a, &b);
1726 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1728 else if(token == DIV)
1730 if(a -> result -> class != OBERON_TYPE_INTEGER
1731 || b -> result -> class != OBERON_TYPE_INTEGER)
1733 oberon_error(ctx, "operator DIV requires integer type");
1736 oberon_autocast_binary_op(ctx, &a, &b);
1737 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1739 else
1741 oberon_autocast_binary_op(ctx, &a, &b);
1743 if(token == PLUS)
1745 expr = oberon_new_operator(OP_ADD, a -> result, a, b);
1747 else if(token == MINUS)
1749 expr = oberon_new_operator(OP_SUB, a -> result, a, b);
1751 else if(token == STAR)
1753 expr = oberon_new_operator(OP_MUL, a -> result, a, b);
1755 else if(token == MOD)
1757 expr = oberon_new_operator(OP_MOD, a -> result, a, b);
1759 else
1761 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1765 return expr;
1768 #define ISMULOP(x) \
1769 ((x) >= STAR && (x) <= AND)
1771 static oberon_expr_t *
1772 oberon_term_expr(oberon_context_t * ctx)
1774 oberon_expr_t * expr;
1776 expr = oberon_factor(ctx);
1777 while(ISMULOP(ctx -> token))
1779 int token = ctx -> token;
1780 oberon_read_token(ctx);
1782 oberon_expr_t * inter = oberon_factor(ctx);
1783 expr = oberon_make_bin_op(ctx, token, expr, inter);
1786 return expr;
1789 #define ISADDOP(x) \
1790 ((x) >= PLUS && (x) <= OR)
1792 static oberon_expr_t *
1793 oberon_simple_expr(oberon_context_t * ctx)
1795 oberon_expr_t * expr;
1797 int minus = 0;
1798 if(ctx -> token == PLUS)
1800 minus = 0;
1801 oberon_assert_token(ctx, PLUS);
1803 else if(ctx -> token == MINUS)
1805 minus = 1;
1806 oberon_assert_token(ctx, MINUS);
1809 expr = oberon_term_expr(ctx);
1811 if(minus)
1813 expr = oberon_make_unary_op(ctx, MINUS, expr);
1816 while(ISADDOP(ctx -> token))
1818 int token = ctx -> token;
1819 oberon_read_token(ctx);
1821 oberon_expr_t * inter = oberon_term_expr(ctx);
1822 expr = oberon_make_bin_op(ctx, token, expr, inter);
1825 return expr;
1828 #define ISRELATION(x) \
1829 ((x) >= EQUAL && (x) <= IS)
1831 static oberon_expr_t *
1832 oberon_expr(oberon_context_t * ctx)
1834 oberon_expr_t * expr;
1836 expr = oberon_simple_expr(ctx);
1837 while(ISRELATION(ctx -> token))
1839 int token = ctx -> token;
1840 oberon_read_token(ctx);
1842 oberon_expr_t * inter = oberon_simple_expr(ctx);
1843 expr = oberon_make_bin_op(ctx, token, expr, inter);
1846 return expr;
1849 static oberon_item_t *
1850 oberon_const_expr(oberon_context_t * ctx)
1852 oberon_expr_t * expr;
1853 expr = oberon_expr(ctx);
1855 if(expr -> is_item == 0)
1857 oberon_error(ctx, "const expression are required");
1860 return (oberon_item_t *) expr;
1863 // =======================================================================
1864 // PARSER
1865 // =======================================================================
1867 static void oberon_decl_seq(oberon_context_t * ctx);
1868 static void oberon_statement_seq(oberon_context_t * ctx);
1869 static void oberon_initialize_decl(oberon_context_t * ctx);
1871 static void
1872 oberon_expect_token(oberon_context_t * ctx, int token)
1874 if(ctx -> token != token)
1876 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
1880 static void
1881 oberon_assert_token(oberon_context_t * ctx, int token)
1883 oberon_expect_token(ctx, token);
1884 oberon_read_token(ctx);
1887 static char *
1888 oberon_assert_ident(oberon_context_t * ctx)
1890 oberon_expect_token(ctx, IDENT);
1891 char * ident = ctx -> string;
1892 oberon_read_token(ctx);
1893 return ident;
1896 static void
1897 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
1899 switch(ctx -> token)
1901 case STAR:
1902 oberon_assert_token(ctx, STAR);
1903 *export = 1;
1904 *read_only = 0;
1905 break;
1906 case MINUS:
1907 oberon_assert_token(ctx, MINUS);
1908 *export = 1;
1909 *read_only = 1;
1910 break;
1911 default:
1912 *export = 0;
1913 *read_only = 0;
1914 break;
1918 static oberon_object_t *
1919 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
1921 char * name;
1922 int export;
1923 int read_only;
1924 oberon_object_t * x;
1926 name = oberon_assert_ident(ctx);
1927 oberon_def(ctx, &export, &read_only);
1929 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
1930 return x;
1933 static void
1934 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
1936 *num = 1;
1937 *list = oberon_ident_def(ctx, class, check_upscope);
1938 while(ctx -> token == COMMA)
1940 oberon_assert_token(ctx, COMMA);
1941 oberon_ident_def(ctx, class, check_upscope);
1942 *num += 1;
1946 static void
1947 oberon_var_decl(oberon_context_t * ctx)
1949 int num;
1950 oberon_object_t * list;
1951 oberon_type_t * type;
1952 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1954 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
1955 oberon_assert_token(ctx, COLON);
1956 oberon_type(ctx, &type);
1958 oberon_object_t * var = list;
1959 for(int i = 0; i < num; i++)
1961 var -> type = type;
1962 var = var -> next;
1966 static oberon_object_t *
1967 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
1969 int class = OBERON_CLASS_PARAM;
1970 if(ctx -> token == VAR)
1972 oberon_read_token(ctx);
1973 class = OBERON_CLASS_VAR_PARAM;
1976 int num;
1977 oberon_object_t * list;
1978 oberon_ident_list(ctx, class, false, &num, &list);
1980 oberon_assert_token(ctx, COLON);
1982 oberon_type_t * type;
1983 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1984 oberon_type(ctx, &type);
1986 oberon_object_t * param = list;
1987 for(int i = 0; i < num; i++)
1989 param -> type = type;
1990 param = param -> next;
1993 *num_decl += num;
1994 return list;
1997 #define ISFPSECTION \
1998 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2000 static void
2001 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
2003 oberon_assert_token(ctx, LPAREN);
2005 if(ISFPSECTION)
2007 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
2008 while(ctx -> token == SEMICOLON)
2010 oberon_assert_token(ctx, SEMICOLON);
2011 oberon_fp_section(ctx, &signature -> num_decl);
2015 oberon_assert_token(ctx, RPAREN);
2017 if(ctx -> token == COLON)
2019 oberon_assert_token(ctx, COLON);
2021 oberon_object_t * typeobj;
2022 typeobj = oberon_qualident(ctx, NULL, 1);
2023 if(typeobj -> class != OBERON_CLASS_TYPE)
2025 oberon_error(ctx, "function result is not type");
2027 signature -> base = typeobj -> type;
2031 static void
2032 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
2034 oberon_type_t * signature;
2035 signature = *type;
2036 signature -> class = OBERON_TYPE_PROCEDURE;
2037 signature -> num_decl = 0;
2038 signature -> base = ctx -> void_type;
2039 signature -> decl = NULL;
2041 if(ctx -> token == LPAREN)
2043 oberon_formal_pars(ctx, signature);
2047 static void
2048 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
2050 if(a -> num_decl != b -> num_decl)
2052 oberon_error(ctx, "number parameters not matched");
2055 int num_param = a -> num_decl;
2056 oberon_object_t * param_a = a -> decl;
2057 oberon_object_t * param_b = b -> decl;
2058 for(int i = 0; i < num_param; i++)
2060 if(strcmp(param_a -> name, param_b -> name) != 0)
2062 oberon_error(ctx, "param %i name not matched", i + 1);
2065 if(param_a -> type != param_b -> type)
2067 oberon_error(ctx, "param %i type not matched", i + 1);
2070 param_a = param_a -> next;
2071 param_b = param_b -> next;
2075 static void
2076 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
2078 oberon_object_t * proc = ctx -> decl -> parent;
2079 oberon_type_t * result_type = proc -> type -> base;
2081 if(result_type -> class == OBERON_TYPE_VOID)
2083 if(expr != NULL)
2085 oberon_error(ctx, "procedure has no result type");
2088 else
2090 if(expr == NULL)
2092 oberon_error(ctx, "procedure requires expression on result");
2095 expr = oberon_autocast_to(ctx, expr, result_type);
2098 proc -> has_return = 1;
2100 oberon_generate_return(ctx, expr);
2103 static void
2104 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
2106 oberon_assert_token(ctx, SEMICOLON);
2108 ctx -> decl = proc -> scope;
2110 oberon_decl_seq(ctx);
2112 oberon_generate_begin_proc(ctx, proc);
2114 if(ctx -> token == BEGIN)
2116 oberon_assert_token(ctx, BEGIN);
2117 oberon_statement_seq(ctx);
2120 oberon_assert_token(ctx, END);
2121 char * name = oberon_assert_ident(ctx);
2122 if(strcmp(name, proc -> name) != 0)
2124 oberon_error(ctx, "procedure name not matched");
2127 if(proc -> type -> base -> class == OBERON_TYPE_VOID
2128 && proc -> has_return == 0)
2130 oberon_make_return(ctx, NULL);
2133 if(proc -> has_return == 0)
2135 oberon_error(ctx, "procedure requires return");
2138 oberon_generate_end_proc(ctx);
2139 oberon_close_scope(ctx -> decl);
2142 static void
2143 oberon_proc_decl(oberon_context_t * ctx)
2145 oberon_assert_token(ctx, PROCEDURE);
2147 int forward = 0;
2148 if(ctx -> token == UPARROW)
2150 oberon_assert_token(ctx, UPARROW);
2151 forward = 1;
2154 char * name;
2155 int export;
2156 int read_only;
2157 name = oberon_assert_ident(ctx);
2158 oberon_def(ctx, &export, &read_only);
2160 oberon_scope_t * proc_scope;
2161 proc_scope = oberon_open_scope(ctx);
2162 ctx -> decl -> local = 1;
2164 oberon_type_t * signature;
2165 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
2166 oberon_opt_formal_pars(ctx, &signature);
2168 oberon_initialize_decl(ctx);
2169 oberon_generator_init_type(ctx, signature);
2170 oberon_close_scope(ctx -> decl);
2172 oberon_object_t * proc;
2173 proc = oberon_find_object(ctx -> decl, name, 0);
2174 if(proc != NULL)
2176 if(proc -> class != OBERON_CLASS_PROC)
2178 oberon_error(ctx, "mult definition");
2181 if(forward == 0)
2183 if(proc -> linked)
2185 oberon_error(ctx, "mult procedure definition");
2189 if(proc -> export != export || proc -> read_only != read_only)
2191 oberon_error(ctx, "export type not matched");
2194 oberon_compare_signatures(ctx, proc -> type, signature);
2196 else
2198 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2199 proc -> type = signature;
2200 proc -> scope = proc_scope;
2201 oberon_generator_init_proc(ctx, proc);
2204 proc -> scope -> parent = proc;
2206 if(forward == 0)
2208 proc -> linked = 1;
2209 oberon_proc_decl_body(ctx, proc);
2213 static void
2214 oberon_const_decl(oberon_context_t * ctx)
2216 oberon_item_t * value;
2217 oberon_object_t * constant;
2219 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2220 oberon_assert_token(ctx, EQUAL);
2221 value = oberon_const_expr(ctx);
2222 constant -> value = value;
2225 static void
2226 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2228 if(size -> is_item == 0)
2230 oberon_error(ctx, "requires constant");
2233 if(size -> item.mode != MODE_INTEGER)
2235 oberon_error(ctx, "requires integer constant");
2238 oberon_type_t * arr;
2239 arr = *type;
2240 arr -> class = OBERON_TYPE_ARRAY;
2241 arr -> size = size -> item.integer;
2242 arr -> base = base;
2245 static void
2246 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2248 char * name;
2249 oberon_object_t * to;
2251 to = oberon_qualident(ctx, &name, 0);
2253 //name = oberon_assert_ident(ctx);
2254 //to = oberon_find_object(ctx -> decl, name, 0);
2256 if(to != NULL)
2258 if(to -> class != OBERON_CLASS_TYPE)
2260 oberon_error(ctx, "not a type");
2263 else
2265 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2266 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2269 *type = to -> type;
2272 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2274 /*
2275 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2276 */
2278 static void
2279 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2281 if(sizes == NULL)
2283 *type = base;
2284 return;
2287 oberon_type_t * dim;
2288 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
2290 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2292 oberon_make_array_type(ctx, sizes, dim, type);
2295 static void
2296 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2298 type -> class = OBERON_TYPE_ARRAY;
2299 type -> size = 0;
2300 type -> base = base;
2303 static void
2304 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2306 if(ctx -> token == IDENT)
2308 int num;
2309 oberon_object_t * list;
2310 oberon_type_t * type;
2311 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2313 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2314 oberon_assert_token(ctx, COLON);
2316 oberon_scope_t * current = ctx -> decl;
2317 ctx -> decl = modscope;
2318 oberon_type(ctx, &type);
2319 ctx -> decl = current;
2321 oberon_object_t * field = list;
2322 for(int i = 0; i < num; i++)
2324 field -> type = type;
2325 field = field -> next;
2328 rec -> num_decl += num;
2332 static void
2333 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2335 oberon_scope_t * modscope = ctx -> mod -> decl;
2336 oberon_scope_t * oldscope = ctx -> decl;
2337 ctx -> decl = modscope;
2339 if(ctx -> token == LPAREN)
2341 oberon_assert_token(ctx, LPAREN);
2343 oberon_object_t * typeobj;
2344 typeobj = oberon_qualident(ctx, NULL, true);
2346 if(typeobj -> class != OBERON_CLASS_TYPE)
2348 oberon_error(ctx, "base must be type");
2351 oberon_type_t * base = typeobj -> type;
2352 if(base -> class == OBERON_TYPE_POINTER)
2354 base = base -> base;
2357 if(base -> class != OBERON_TYPE_RECORD)
2359 oberon_error(ctx, "base must be record type");
2362 rec -> base = base;
2363 ctx -> decl = base -> scope;
2365 oberon_assert_token(ctx, RPAREN);
2367 else
2369 ctx -> decl = NULL;
2372 oberon_scope_t * this_scope;
2373 this_scope = oberon_open_scope(ctx);
2374 this_scope -> local = true;
2375 this_scope -> parent = NULL;
2376 this_scope -> parent_type = rec;
2378 oberon_field_list(ctx, rec, modscope);
2379 while(ctx -> token == SEMICOLON)
2381 oberon_assert_token(ctx, SEMICOLON);
2382 oberon_field_list(ctx, rec, modscope);
2385 rec -> scope = this_scope;
2386 rec -> decl = this_scope -> list -> next;
2387 ctx -> decl = oldscope;
2390 static void
2391 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2393 if(ctx -> token == IDENT)
2395 oberon_qualident_type(ctx, type);
2397 else if(ctx -> token == ARRAY)
2399 oberon_assert_token(ctx, ARRAY);
2401 int num_sizes = 0;
2402 oberon_expr_t * sizes;
2404 if(ISEXPR(ctx -> token))
2406 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2409 oberon_assert_token(ctx, OF);
2411 oberon_type_t * base;
2412 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2413 oberon_type(ctx, &base);
2415 if(num_sizes == 0)
2417 oberon_make_open_array(ctx, base, *type);
2419 else
2421 oberon_make_multiarray(ctx, sizes, base, type);
2424 else if(ctx -> token == RECORD)
2426 oberon_type_t * rec;
2427 rec = *type;
2428 rec -> class = OBERON_TYPE_RECORD;
2429 rec -> module = ctx -> mod;
2431 oberon_assert_token(ctx, RECORD);
2432 oberon_type_record_body(ctx, rec);
2433 oberon_assert_token(ctx, END);
2435 *type = rec;
2437 else if(ctx -> token == POINTER)
2439 oberon_assert_token(ctx, POINTER);
2440 oberon_assert_token(ctx, TO);
2442 oberon_type_t * base;
2443 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2444 oberon_type(ctx, &base);
2446 oberon_type_t * ptr;
2447 ptr = *type;
2448 ptr -> class = OBERON_TYPE_POINTER;
2449 ptr -> base = base;
2451 else if(ctx -> token == PROCEDURE)
2453 oberon_open_scope(ctx);
2454 oberon_assert_token(ctx, PROCEDURE);
2455 oberon_opt_formal_pars(ctx, type);
2456 oberon_close_scope(ctx -> decl);
2458 else
2460 oberon_error(ctx, "invalid type declaration");
2464 static void
2465 oberon_type_decl(oberon_context_t * ctx)
2467 char * name;
2468 oberon_object_t * newtype;
2469 oberon_type_t * type;
2470 int export;
2471 int read_only;
2473 name = oberon_assert_ident(ctx);
2474 oberon_def(ctx, &export, &read_only);
2476 newtype = oberon_find_object(ctx -> decl, name, 0);
2477 if(newtype == NULL)
2479 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2480 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2481 assert(newtype -> type);
2483 else
2485 if(newtype -> class != OBERON_CLASS_TYPE)
2487 oberon_error(ctx, "mult definition");
2490 if(newtype -> linked)
2492 oberon_error(ctx, "mult definition - already linked");
2495 newtype -> export = export;
2496 newtype -> read_only = read_only;
2499 oberon_assert_token(ctx, EQUAL);
2501 type = newtype -> type;
2502 oberon_type(ctx, &type);
2504 if(type -> class == OBERON_TYPE_VOID)
2506 oberon_error(ctx, "recursive alias declaration");
2509 newtype -> type = type;
2510 newtype -> linked = 1;
2513 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2514 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2516 static void
2517 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2519 if(type -> class != OBERON_TYPE_POINTER
2520 && type -> class != OBERON_TYPE_ARRAY)
2522 return;
2525 if(type -> recursive)
2527 oberon_error(ctx, "recursive pointer declaration");
2530 if(type -> class == OBERON_TYPE_POINTER
2531 && type -> base -> class == OBERON_TYPE_POINTER)
2533 oberon_error(ctx, "attempt to make pointer to pointer");
2536 type -> recursive = 1;
2538 oberon_prevent_recursive_pointer(ctx, type -> base);
2540 type -> recursive = 0;
2543 static void
2544 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2546 if(type -> class != OBERON_TYPE_RECORD)
2548 return;
2551 if(type -> recursive)
2553 oberon_error(ctx, "recursive record declaration");
2556 type -> recursive = 1;
2558 int num_fields = type -> num_decl;
2559 oberon_object_t * field = type -> decl;
2560 for(int i = 0; i < num_fields; i++)
2562 oberon_prevent_recursive_object(ctx, field);
2563 field = field -> next;
2566 type -> recursive = 0;
2568 static void
2569 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2571 if(type -> class != OBERON_TYPE_PROCEDURE)
2573 return;
2576 if(type -> recursive)
2578 oberon_error(ctx, "recursive procedure declaration");
2581 type -> recursive = 1;
2583 int num_fields = type -> num_decl;
2584 oberon_object_t * field = type -> decl;
2585 for(int i = 0; i < num_fields; i++)
2587 oberon_prevent_recursive_object(ctx, field);
2588 field = field -> next;
2591 type -> recursive = 0;
2594 static void
2595 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2597 if(type -> class != OBERON_TYPE_ARRAY)
2599 return;
2602 if(type -> recursive)
2604 oberon_error(ctx, "recursive array declaration");
2607 type -> recursive = 1;
2609 oberon_prevent_recursive_type(ctx, type -> base);
2611 type -> recursive = 0;
2614 static void
2615 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2617 if(type -> class == OBERON_TYPE_POINTER)
2619 oberon_prevent_recursive_pointer(ctx, type);
2621 else if(type -> class == OBERON_TYPE_RECORD)
2623 oberon_prevent_recursive_record(ctx, type);
2625 else if(type -> class == OBERON_TYPE_ARRAY)
2627 oberon_prevent_recursive_array(ctx, type);
2629 else if(type -> class == OBERON_TYPE_PROCEDURE)
2631 oberon_prevent_recursive_procedure(ctx, type);
2635 static void
2636 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2638 switch(x -> class)
2640 case OBERON_CLASS_VAR:
2641 case OBERON_CLASS_TYPE:
2642 case OBERON_CLASS_PARAM:
2643 case OBERON_CLASS_VAR_PARAM:
2644 case OBERON_CLASS_FIELD:
2645 oberon_prevent_recursive_type(ctx, x -> type);
2646 break;
2647 case OBERON_CLASS_CONST:
2648 case OBERON_CLASS_PROC:
2649 case OBERON_CLASS_MODULE:
2650 break;
2651 default:
2652 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2653 break;
2657 static void
2658 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2660 oberon_object_t * x = ctx -> decl -> list -> next;
2662 while(x)
2664 oberon_prevent_recursive_object(ctx, x);
2665 x = x -> next;
2669 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2670 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2672 static void
2673 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2675 if(type -> class != OBERON_TYPE_RECORD)
2677 return;
2680 int num_fields = type -> num_decl;
2681 oberon_object_t * field = type -> decl;
2682 for(int i = 0; i < num_fields; i++)
2684 if(field -> type -> class == OBERON_TYPE_POINTER)
2686 oberon_initialize_type(ctx, field -> type);
2689 oberon_initialize_object(ctx, field);
2690 field = field -> next;
2693 oberon_generator_init_record(ctx, type);
2696 static void
2697 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2699 if(type -> class == OBERON_TYPE_VOID)
2701 oberon_error(ctx, "undeclarated type");
2704 if(type -> initialized)
2706 return;
2709 type -> initialized = 1;
2711 if(type -> class == OBERON_TYPE_POINTER)
2713 oberon_initialize_type(ctx, type -> base);
2714 oberon_generator_init_type(ctx, type);
2716 else if(type -> class == OBERON_TYPE_ARRAY)
2718 if(type -> size != 0)
2720 if(type -> base -> class == OBERON_TYPE_ARRAY)
2722 if(type -> base -> size == 0)
2724 oberon_error(ctx, "open array not allowed as array element");
2729 oberon_initialize_type(ctx, type -> base);
2730 oberon_generator_init_type(ctx, type);
2732 else if(type -> class == OBERON_TYPE_RECORD)
2734 oberon_generator_init_type(ctx, type);
2735 oberon_initialize_record_fields(ctx, type);
2737 else if(type -> class == OBERON_TYPE_PROCEDURE)
2739 int num_fields = type -> num_decl;
2740 oberon_object_t * field = type -> decl;
2741 for(int i = 0; i < num_fields; i++)
2743 oberon_initialize_object(ctx, field);
2744 field = field -> next;
2745 }
2747 oberon_generator_init_type(ctx, type);
2749 else
2751 oberon_generator_init_type(ctx, type);
2755 static void
2756 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2758 if(x -> initialized)
2760 return;
2763 x -> initialized = 1;
2765 switch(x -> class)
2767 case OBERON_CLASS_TYPE:
2768 oberon_initialize_type(ctx, x -> type);
2769 break;
2770 case OBERON_CLASS_VAR:
2771 case OBERON_CLASS_FIELD:
2772 if(x -> type -> class == OBERON_TYPE_ARRAY)
2774 if(x -> type -> size == 0)
2776 oberon_error(ctx, "open array not allowed as variable or field");
2779 oberon_initialize_type(ctx, x -> type);
2780 oberon_generator_init_var(ctx, x);
2781 break;
2782 case OBERON_CLASS_PARAM:
2783 case OBERON_CLASS_VAR_PARAM:
2784 oberon_initialize_type(ctx, x -> type);
2785 oberon_generator_init_var(ctx, x);
2786 break;
2787 case OBERON_CLASS_CONST:
2788 case OBERON_CLASS_PROC:
2789 case OBERON_CLASS_MODULE:
2790 break;
2791 default:
2792 oberon_error(ctx, "oberon_initialize_object: wat");
2793 break;
2797 static void
2798 oberon_initialize_decl(oberon_context_t * ctx)
2800 oberon_object_t * x = ctx -> decl -> list;
2802 while(x -> next)
2804 oberon_initialize_object(ctx, x -> next);
2805 x = x -> next;
2806 }
2809 static void
2810 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2812 oberon_object_t * x = ctx -> decl -> list;
2814 while(x -> next)
2816 if(x -> next -> class == OBERON_CLASS_PROC)
2818 if(x -> next -> linked == 0)
2820 oberon_error(ctx, "unresolved forward declaration");
2823 x = x -> next;
2824 }
2827 static void
2828 oberon_decl_seq(oberon_context_t * ctx)
2830 if(ctx -> token == CONST)
2832 oberon_assert_token(ctx, CONST);
2833 while(ctx -> token == IDENT)
2835 oberon_const_decl(ctx);
2836 oberon_assert_token(ctx, SEMICOLON);
2840 if(ctx -> token == TYPE)
2842 oberon_assert_token(ctx, TYPE);
2843 while(ctx -> token == IDENT)
2845 oberon_type_decl(ctx);
2846 oberon_assert_token(ctx, SEMICOLON);
2850 if(ctx -> token == VAR)
2852 oberon_assert_token(ctx, VAR);
2853 while(ctx -> token == IDENT)
2855 oberon_var_decl(ctx);
2856 oberon_assert_token(ctx, SEMICOLON);
2860 oberon_prevent_recursive_decl(ctx);
2861 oberon_initialize_decl(ctx);
2863 while(ctx -> token == PROCEDURE)
2865 oberon_proc_decl(ctx);
2866 oberon_assert_token(ctx, SEMICOLON);
2869 oberon_prevent_undeclarated_procedures(ctx);
2872 static void
2873 oberon_statement_seq(oberon_context_t * ctx);
2875 static void
2876 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
2878 if(dst -> read_only)
2880 oberon_error(ctx, "read-only destination");
2883 src = oberon_autocast_to(ctx, src, dst -> result);
2884 oberon_generate_assign(ctx, src, dst);
2887 static void
2888 oberon_statement(oberon_context_t * ctx)
2890 oberon_expr_t * item1;
2891 oberon_expr_t * item2;
2893 if(ctx -> token == IDENT)
2895 item1 = oberon_designator(ctx);
2896 if(ctx -> token == ASSIGN)
2898 oberon_assert_token(ctx, ASSIGN);
2899 item2 = oberon_expr(ctx);
2900 oberon_assign(ctx, item2, item1);
2902 else
2904 oberon_opt_proc_parens(ctx, item1);
2907 else if(ctx -> token == IF)
2909 gen_label_t * end;
2910 gen_label_t * els;
2911 oberon_expr_t * cond;
2913 els = oberon_generator_reserve_label(ctx);
2914 end = oberon_generator_reserve_label(ctx);
2916 oberon_assert_token(ctx, IF);
2917 cond = oberon_expr(ctx);
2918 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
2920 oberon_error(ctx, "condition must be boolean");
2922 oberon_assert_token(ctx, THEN);
2923 oberon_generate_branch(ctx, cond, false, els);
2924 oberon_statement_seq(ctx);
2925 oberon_generate_goto(ctx, end);
2926 oberon_generate_label(ctx, els);
2928 while(ctx -> token == ELSIF)
2930 els = oberon_generator_reserve_label(ctx);
2932 oberon_assert_token(ctx, ELSIF);
2933 cond = oberon_expr(ctx);
2934 if(cond -> result -> class != OBERON_TYPE_BOOLEAN)
2936 oberon_error(ctx, "condition must be boolean");
2938 oberon_assert_token(ctx, THEN);
2939 oberon_generate_branch(ctx, cond, false, els);
2940 oberon_statement_seq(ctx);
2941 oberon_generate_goto(ctx, end);
2942 oberon_generate_label(ctx, els);
2945 if(ctx -> token == ELSE)
2947 oberon_assert_token(ctx, ELSE);
2948 oberon_statement_seq(ctx);
2951 oberon_generate_label(ctx, end);
2952 oberon_assert_token(ctx, END);
2954 else if(ctx -> token == RETURN)
2956 oberon_assert_token(ctx, RETURN);
2957 if(ISEXPR(ctx -> token))
2959 oberon_expr_t * expr;
2960 expr = oberon_expr(ctx);
2961 oberon_make_return(ctx, expr);
2963 else
2965 oberon_make_return(ctx, NULL);
2970 static void
2971 oberon_statement_seq(oberon_context_t * ctx)
2973 oberon_statement(ctx);
2974 while(ctx -> token == SEMICOLON)
2976 oberon_assert_token(ctx, SEMICOLON);
2977 oberon_statement(ctx);
2981 static void
2982 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
2984 oberon_module_t * m = ctx -> module_list;
2985 while(m && strcmp(m -> name, name) != 0)
2987 m = m -> next;
2990 if(m == NULL)
2992 const char * code;
2993 code = ctx -> import_module(name);
2994 if(code == NULL)
2996 oberon_error(ctx, "no such module");
2999 m = oberon_compile_module(ctx, code);
3000 assert(m);
3003 if(m -> ready == 0)
3005 oberon_error(ctx, "cyclic module import");
3008 oberon_object_t * ident;
3009 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
3010 ident -> module = m;
3013 static void
3014 oberon_import_decl(oberon_context_t * ctx)
3016 char * alias;
3017 char * name;
3019 alias = name = oberon_assert_ident(ctx);
3020 if(ctx -> token == ASSIGN)
3022 oberon_assert_token(ctx, ASSIGN);
3023 name = oberon_assert_ident(ctx);
3026 oberon_import_module(ctx, alias, name);
3029 static void
3030 oberon_import_list(oberon_context_t * ctx)
3032 oberon_assert_token(ctx, IMPORT);
3034 oberon_import_decl(ctx);
3035 while(ctx -> token == COMMA)
3037 oberon_assert_token(ctx, COMMA);
3038 oberon_import_decl(ctx);
3041 oberon_assert_token(ctx, SEMICOLON);
3044 static void
3045 oberon_parse_module(oberon_context_t * ctx)
3047 char * name1;
3048 char * name2;
3049 oberon_read_token(ctx);
3051 oberon_assert_token(ctx, MODULE);
3052 name1 = oberon_assert_ident(ctx);
3053 oberon_assert_token(ctx, SEMICOLON);
3054 ctx -> mod -> name = name1;
3056 oberon_generator_init_module(ctx, ctx -> mod);
3058 if(ctx -> token == IMPORT)
3060 oberon_import_list(ctx);
3063 oberon_decl_seq(ctx);
3065 oberon_generate_begin_module(ctx);
3066 if(ctx -> token == BEGIN)
3068 oberon_assert_token(ctx, BEGIN);
3069 oberon_statement_seq(ctx);
3071 oberon_generate_end_module(ctx);
3073 oberon_assert_token(ctx, END);
3074 name2 = oberon_assert_ident(ctx);
3075 oberon_assert_token(ctx, DOT);
3077 if(strcmp(name1, name2) != 0)
3079 oberon_error(ctx, "module name not matched");
3082 oberon_generator_fini_module(ctx -> mod);
3085 // =======================================================================
3086 // LIBRARY
3087 // =======================================================================
3089 static void
3090 register_default_types(oberon_context_t * ctx)
3092 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
3093 oberon_generator_init_type(ctx, ctx -> void_type);
3095 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
3096 ctx -> void_ptr_type -> base = ctx -> void_type;
3097 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
3099 ctx -> string_type = oberon_new_type_string(1);
3100 oberon_generator_init_type(ctx, ctx -> string_type);
3102 ctx -> bool_type = oberon_new_type_boolean();
3103 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
3105 ctx -> byte_type = oberon_new_type_integer(1);
3106 oberon_define_type(ctx -> world_scope, "BYTE", ctx -> byte_type, 1);
3108 ctx -> shortint_type = oberon_new_type_integer(2);
3109 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> shortint_type, 1);
3111 ctx -> int_type = oberon_new_type_integer(4);
3112 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type, 1);
3114 ctx -> longint_type = oberon_new_type_integer(8);
3115 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> longint_type, 1);
3117 ctx -> real_type = oberon_new_type_real(4);
3118 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
3120 ctx -> longreal_type = oberon_new_type_real(8);
3121 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
3123 ctx -> char_type = oberon_new_type_char(1);
3124 oberon_define_type(ctx -> world_scope, "CHAR", ctx -> char_type, 1);
3127 static void
3128 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
3130 oberon_object_t * proc;
3131 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
3132 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
3133 proc -> type -> sysproc = true;
3134 proc -> type -> genfunc = f;
3135 proc -> type -> genproc = p;
3138 static oberon_expr_t *
3139 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3141 if(num_args < 1)
3143 oberon_error(ctx, "too few arguments");
3146 if(num_args > 1)
3148 oberon_error(ctx, "too mach arguments");
3151 oberon_expr_t * arg;
3152 arg = list_args;
3154 oberon_type_t * result_type;
3155 result_type = arg -> result;
3157 if(result_type -> class != OBERON_TYPE_INTEGER)
3159 oberon_error(ctx, "ABS accepts only integers");
3163 oberon_expr_t * expr;
3164 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
3165 return expr;
3168 static void
3169 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
3171 if(num_args < 1)
3173 oberon_error(ctx, "too few arguments");
3176 oberon_expr_t * dst;
3177 dst = list_args;
3179 oberon_type_t * type;
3180 type = dst -> result;
3182 if(type -> class != OBERON_TYPE_POINTER)
3184 oberon_error(ctx, "not a pointer");
3187 type = type -> base;
3189 oberon_expr_t * src;
3190 src = oberon_new_item(MODE_NEW, dst -> result, 0);
3191 src -> item.num_args = 0;
3192 src -> item.args = NULL;
3194 int max_args = 1;
3195 if(type -> class == OBERON_TYPE_ARRAY)
3197 if(type -> size == 0)
3199 oberon_type_t * x = type;
3200 while(x -> class == OBERON_TYPE_ARRAY)
3202 if(x -> size == 0)
3204 max_args += 1;
3206 x = x -> base;
3210 if(num_args < max_args)
3212 oberon_error(ctx, "too few arguments");
3215 if(num_args > max_args)
3217 oberon_error(ctx, "too mach arguments");
3220 int num_sizes = max_args - 1;
3221 oberon_expr_t * size_list = list_args -> next;
3223 oberon_expr_t * arg = size_list;
3224 for(int i = 0; i < max_args - 1; i++)
3226 if(arg -> result -> class != OBERON_TYPE_INTEGER)
3228 oberon_error(ctx, "size must be integer");
3230 arg = arg -> next;
3233 src -> item.num_args = num_sizes;
3234 src -> item.args = size_list;
3236 else if(type -> class != OBERON_TYPE_RECORD)
3238 oberon_error(ctx, "oberon_make_new_call: wat");
3241 if(num_args > max_args)
3243 oberon_error(ctx, "too mach arguments");
3246 oberon_assign(ctx, src, dst);
3249 oberon_context_t *
3250 oberon_create_context(ModuleImportCallback import_module)
3252 oberon_context_t * ctx = calloc(1, sizeof *ctx);
3254 oberon_scope_t * world_scope;
3255 world_scope = oberon_open_scope(ctx);
3256 ctx -> world_scope = world_scope;
3258 ctx -> import_module = import_module;
3260 oberon_generator_init_context(ctx);
3262 register_default_types(ctx);
3263 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
3264 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
3266 return ctx;
3269 void
3270 oberon_destroy_context(oberon_context_t * ctx)
3272 oberon_generator_destroy_context(ctx);
3273 free(ctx);
3276 oberon_module_t *
3277 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
3279 const char * code = ctx -> code;
3280 int code_index = ctx -> code_index;
3281 char c = ctx -> c;
3282 int token = ctx -> token;
3283 char * string = ctx -> string;
3284 int integer = ctx -> integer;
3285 int real = ctx -> real;
3286 bool longmode = ctx -> longmode;
3287 oberon_scope_t * decl = ctx -> decl;
3288 oberon_module_t * mod = ctx -> mod;
3290 oberon_scope_t * module_scope;
3291 module_scope = oberon_open_scope(ctx);
3293 oberon_module_t * module;
3294 module = calloc(1, sizeof *module);
3295 module -> decl = module_scope;
3296 module -> next = ctx -> module_list;
3298 ctx -> mod = module;
3299 ctx -> module_list = module;
3301 oberon_init_scaner(ctx, newcode);
3302 oberon_parse_module(ctx);
3304 module -> ready = 1;
3306 ctx -> code = code;
3307 ctx -> code_index = code_index;
3308 ctx -> c = c;
3309 ctx -> token = token;
3310 ctx -> string = string;
3311 ctx -> integer = integer;
3312 ctx -> real = real;
3313 ctx -> longmode = longmode;
3314 ctx -> decl = decl;
3315 ctx -> mod = mod;
3317 return module;