DEADSOFTWARE

Теперь возможен вызов процедур-переменных из полей записей
[dsw-obn.git] / src / oberon.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdbool.h>
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 };
65 // =======================================================================
66 // UTILS
67 // =======================================================================
69 static void
70 oberon_error(oberon_context_t * ctx, const char * fmt, ...)
71 {
72 va_list ptr;
73 va_start(ptr, fmt);
74 fprintf(stderr, "error: ");
75 vfprintf(stderr, fmt, ptr);
76 fprintf(stderr, "\n");
77 fprintf(stderr, " code_index = %i\n", ctx -> code_index);
78 fprintf(stderr, " c = %c\n", ctx -> c);
79 fprintf(stderr, " token = %i\n", ctx -> token);
80 va_end(ptr);
81 exit(1);
82 }
84 static oberon_type_t *
85 oberon_new_type_ptr(int class)
86 {
87 oberon_type_t * x = malloc(sizeof *x);
88 memset(x, 0, sizeof *x);
89 x -> class = class;
90 return x;
91 }
93 static oberon_type_t *
94 oberon_new_type_integer(int size)
95 {
96 oberon_type_t * x;
97 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
98 x -> size = size;
99 return x;
102 static oberon_type_t *
103 oberon_new_type_boolean()
105 oberon_type_t * x;
106 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
107 return x;
110 static oberon_type_t *
111 oberon_new_type_real(int size)
113 oberon_type_t * x;
114 x = oberon_new_type_ptr(OBERON_TYPE_REAL);
115 x -> size = size;
116 return x;
119 // =======================================================================
120 // TABLE
121 // =======================================================================
123 static oberon_scope_t *
124 oberon_open_scope(oberon_context_t * ctx)
126 oberon_scope_t * scope = calloc(1, sizeof *scope);
127 oberon_object_t * list = calloc(1, sizeof *list);
129 scope -> ctx = ctx;
130 scope -> list = list;
131 scope -> up = ctx -> decl;
133 if(scope -> up)
135 scope -> local = scope -> up -> local;
136 scope -> parent = scope -> up -> parent;
137 scope -> parent_type = scope -> up -> parent_type;
140 ctx -> decl = scope;
141 return scope;
144 static void
145 oberon_close_scope(oberon_scope_t * scope)
147 oberon_context_t * ctx = scope -> ctx;
148 ctx -> decl = scope -> up;
151 static oberon_object_t *
152 oberon_find_object_in_list(oberon_object_t * list, char * name)
154 oberon_object_t * x = list;
155 while(x -> next && strcmp(x -> next -> name, name) != 0)
157 x = x -> next;
159 return x -> next;
162 static oberon_object_t *
163 oberon_find_object(oberon_scope_t * scope, char * name, bool check_it)
165 oberon_object_t * result = NULL;
167 oberon_scope_t * s = scope;
168 while(result == NULL && s != NULL)
170 result = oberon_find_object_in_list(s -> list, name);
171 s = s -> up;
174 if(check_it && result == NULL)
176 oberon_error(scope -> ctx, "undefined ident %s", name);
179 return result;
182 static oberon_object_t *
183 oberon_define_object(oberon_scope_t * scope, char * name, int class, bool export, bool read_only, bool check_upscope)
185 if(check_upscope)
187 if(oberon_find_object(scope -> up, name, false))
189 oberon_error(scope -> ctx, "already defined");
193 oberon_object_t * x = scope -> list;
194 while(x -> next && strcmp(x -> next -> name, name) != 0)
196 x = x -> next;
199 if(x -> next)
201 oberon_error(scope -> ctx, "already defined");
204 oberon_object_t * newvar = malloc(sizeof *newvar);
205 memset(newvar, 0, sizeof *newvar);
206 newvar -> name = name;
207 newvar -> class = class;
208 newvar -> export = export;
209 newvar -> read_only = read_only;
210 newvar -> local = scope -> local;
211 newvar -> parent = scope -> parent;
212 newvar -> parent_type = scope -> parent_type;
213 newvar -> module = scope -> ctx -> mod;
215 x -> next = newvar;
217 return newvar;
220 static oberon_object_t *
221 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type, int export)
223 oberon_object_t * id;
224 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE, export, false, false);
225 id -> type = type;
226 oberon_generator_init_type(scope -> ctx, type);
227 return id;
230 // =======================================================================
231 // SCANER
232 // =======================================================================
234 static void
235 oberon_get_char(oberon_context_t * ctx)
237 if(ctx -> code[ctx -> code_index])
239 ctx -> code_index += 1;
240 ctx -> c = ctx -> code[ctx -> code_index];
244 static void
245 oberon_init_scaner(oberon_context_t * ctx, const char * code)
247 ctx -> code = code;
248 ctx -> code_index = 0;
249 ctx -> c = ctx -> code[ctx -> code_index];
252 static void
253 oberon_read_ident(oberon_context_t * ctx)
255 int len = 0;
256 int i = ctx -> code_index;
258 int c = ctx -> code[i];
259 while(isalnum(c))
261 i += 1;
262 len += 1;
263 c = ctx -> code[i];
266 char * ident = malloc(len + 1);
267 memcpy(ident, &ctx->code[ctx->code_index], len);
268 ident[len] = 0;
270 ctx -> code_index = i;
271 ctx -> c = ctx -> code[i];
272 ctx -> string = ident;
273 ctx -> token = IDENT;
275 if(strcmp(ident, "MODULE") == 0)
277 ctx -> token = MODULE;
279 else if(strcmp(ident, "END") == 0)
281 ctx -> token = END;
283 else if(strcmp(ident, "VAR") == 0)
285 ctx -> token = VAR;
287 else if(strcmp(ident, "BEGIN") == 0)
289 ctx -> token = BEGIN;
291 else if(strcmp(ident, "TRUE") == 0)
293 ctx -> token = TRUE;
295 else if(strcmp(ident, "FALSE") == 0)
297 ctx -> token = FALSE;
299 else if(strcmp(ident, "OR") == 0)
301 ctx -> token = OR;
303 else if(strcmp(ident, "DIV") == 0)
305 ctx -> token = DIV;
307 else if(strcmp(ident, "MOD") == 0)
309 ctx -> token = MOD;
311 else if(strcmp(ident, "PROCEDURE") == 0)
313 ctx -> token = PROCEDURE;
315 else if(strcmp(ident, "RETURN") == 0)
317 ctx -> token = RETURN;
319 else if(strcmp(ident, "CONST") == 0)
321 ctx -> token = CONST;
323 else if(strcmp(ident, "TYPE") == 0)
325 ctx -> token = TYPE;
327 else if(strcmp(ident, "ARRAY") == 0)
329 ctx -> token = ARRAY;
331 else if(strcmp(ident, "OF") == 0)
333 ctx -> token = OF;
335 else if(strcmp(ident, "RECORD") == 0)
337 ctx -> token = RECORD;
339 else if(strcmp(ident, "POINTER") == 0)
341 ctx -> token = POINTER;
343 else if(strcmp(ident, "TO") == 0)
345 ctx -> token = TO;
347 else if(strcmp(ident, "NIL") == 0)
349 ctx -> token = NIL;
351 else if(strcmp(ident, "IMPORT") == 0)
353 ctx -> token = IMPORT;
355 else if(strcmp(ident, "IN") == 0)
357 ctx -> token = IN;
359 else if(strcmp(ident, "IS") == 0)
361 ctx -> token = IS;
365 static void
366 oberon_read_number(oberon_context_t * ctx)
368 long integer;
369 double real;
370 char * ident;
371 int start_i;
372 int exp_i;
373 int end_i;
375 /*
376 * mode = 0 == DEC
377 * mode = 1 == HEX
378 * mode = 2 == REAL
379 * mode = 3 == LONGREAL
380 */
381 int mode = 0;
382 start_i = ctx -> code_index;
384 while(isdigit(ctx -> c))
386 oberon_get_char(ctx);
389 end_i = ctx -> code_index;
391 if(isxdigit(ctx -> c))
393 mode = 1;
394 while(isxdigit(ctx -> c))
396 oberon_get_char(ctx);
399 end_i = ctx -> code_index;
401 if(ctx -> c != 'H')
403 oberon_error(ctx, "invalid hex number");
405 oberon_get_char(ctx);
407 else if(ctx -> c == '.')
409 mode = 2;
410 oberon_get_char(ctx);
412 while(isdigit(ctx -> c))
414 oberon_get_char(ctx);
417 if(ctx -> c == 'E' || ctx -> c == 'D')
419 exp_i = ctx -> code_index;
421 if(ctx -> c == 'D')
423 mode = 3;
426 oberon_get_char(ctx);
428 if(ctx -> c == '+' || ctx -> c == '-')
430 oberon_get_char(ctx);
433 while(isdigit(ctx -> c))
435 oberon_get_char(ctx);
440 end_i = ctx -> code_index;
443 int len = end_i - start_i;
444 ident = malloc(len + 1);
445 memcpy(ident, &ctx -> code[start_i], len);
446 ident[len] = 0;
448 ctx -> longmode = false;
449 if(mode == 3)
451 int i = exp_i - start_i;
452 ident[i] = 'E';
453 ctx -> longmode = true;
456 switch(mode)
458 case 0:
459 integer = atol(ident);
460 real = integer;
461 ctx -> token = INTEGER;
462 break;
463 case 1:
464 sscanf(ident, "%lx", &integer);
465 real = integer;
466 ctx -> token = INTEGER;
467 break;
468 case 2:
469 case 3:
470 sscanf(ident, "%lf", &real);
471 ctx -> token = REAL;
472 break;
473 default:
474 oberon_error(ctx, "oberon_read_number: wat");
475 break;
478 ctx -> string = ident;
479 ctx -> integer = integer;
480 ctx -> real = real;
483 static void
484 oberon_skip_space(oberon_context_t * ctx)
486 while(isspace(ctx -> c))
488 oberon_get_char(ctx);
492 static void
493 oberon_read_comment(oberon_context_t * ctx)
495 int nesting = 1;
496 while(nesting >= 1)
498 if(ctx -> c == '(')
500 oberon_get_char(ctx);
501 if(ctx -> c == '*')
503 oberon_get_char(ctx);
504 nesting += 1;
507 else if(ctx -> c == '*')
509 oberon_get_char(ctx);
510 if(ctx -> c == ')')
512 oberon_get_char(ctx);
513 nesting -= 1;
516 else if(ctx -> c == 0)
518 oberon_error(ctx, "unterminated comment");
520 else
522 oberon_get_char(ctx);
527 static void oberon_read_token(oberon_context_t * ctx);
529 static void
530 oberon_read_symbol(oberon_context_t * ctx)
532 int c = ctx -> c;
533 switch(c)
535 case 0:
536 ctx -> token = EOF_;
537 break;
538 case ';':
539 ctx -> token = SEMICOLON;
540 oberon_get_char(ctx);
541 break;
542 case ':':
543 ctx -> token = COLON;
544 oberon_get_char(ctx);
545 if(ctx -> c == '=')
547 ctx -> token = ASSIGN;
548 oberon_get_char(ctx);
550 break;
551 case '.':
552 ctx -> token = DOT;
553 oberon_get_char(ctx);
554 break;
555 case '(':
556 ctx -> token = LPAREN;
557 oberon_get_char(ctx);
558 if(ctx -> c == '*')
560 oberon_get_char(ctx);
561 oberon_read_comment(ctx);
562 oberon_read_token(ctx);
564 break;
565 case ')':
566 ctx -> token = RPAREN;
567 oberon_get_char(ctx);
568 break;
569 case '=':
570 ctx -> token = EQUAL;
571 oberon_get_char(ctx);
572 break;
573 case '#':
574 ctx -> token = NEQ;
575 oberon_get_char(ctx);
576 break;
577 case '<':
578 ctx -> token = LESS;
579 oberon_get_char(ctx);
580 if(ctx -> c == '=')
582 ctx -> token = LEQ;
583 oberon_get_char(ctx);
585 break;
586 case '>':
587 ctx -> token = GREAT;
588 oberon_get_char(ctx);
589 if(ctx -> c == '=')
591 ctx -> token = GEQ;
592 oberon_get_char(ctx);
594 break;
595 case '+':
596 ctx -> token = PLUS;
597 oberon_get_char(ctx);
598 break;
599 case '-':
600 ctx -> token = MINUS;
601 oberon_get_char(ctx);
602 break;
603 case '*':
604 ctx -> token = STAR;
605 oberon_get_char(ctx);
606 if(ctx -> c == ')')
608 oberon_get_char(ctx);
609 oberon_error(ctx, "unstarted comment");
611 break;
612 case '/':
613 ctx -> token = SLASH;
614 oberon_get_char(ctx);
615 break;
616 case '&':
617 ctx -> token = AND;
618 oberon_get_char(ctx);
619 break;
620 case '~':
621 ctx -> token = NOT;
622 oberon_get_char(ctx);
623 break;
624 case ',':
625 ctx -> token = COMMA;
626 oberon_get_char(ctx);
627 break;
628 case '[':
629 ctx -> token = LBRACE;
630 oberon_get_char(ctx);
631 break;
632 case ']':
633 ctx -> token = RBRACE;
634 oberon_get_char(ctx);
635 break;
636 case '^':
637 ctx -> token = UPARROW;
638 oberon_get_char(ctx);
639 break;
640 default:
641 oberon_error(ctx, "invalid char %c", ctx -> c);
642 break;
646 static void
647 oberon_read_token(oberon_context_t * ctx)
649 oberon_skip_space(ctx);
651 int c = ctx -> c;
652 if(isalpha(c))
654 oberon_read_ident(ctx);
656 else if(isdigit(c))
658 oberon_read_number(ctx);
660 else
662 oberon_read_symbol(ctx);
666 // =======================================================================
667 // EXPRESSION
668 // =======================================================================
670 static void oberon_expect_token(oberon_context_t * ctx, int token);
671 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
672 static void oberon_assert_token(oberon_context_t * ctx, int token);
673 static char * oberon_assert_ident(oberon_context_t * ctx);
674 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
675 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
677 static oberon_expr_t *
678 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
680 oberon_oper_t * operator;
681 operator = malloc(sizeof *operator);
682 memset(operator, 0, sizeof *operator);
684 operator -> is_item = 0;
685 operator -> result = result;
686 operator -> read_only = 1;
687 operator -> op = op;
688 operator -> left = left;
689 operator -> right = right;
691 return (oberon_expr_t *) operator;
694 static oberon_expr_t *
695 oberon_new_item(int mode, oberon_type_t * result, int read_only)
697 oberon_item_t * item;
698 item = malloc(sizeof *item);
699 memset(item, 0, sizeof *item);
701 item -> is_item = 1;
702 item -> result = result;
703 item -> read_only = read_only;
704 item -> mode = mode;
706 return (oberon_expr_t *)item;
709 static oberon_expr_t *
710 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
712 oberon_expr_t * expr;
713 oberon_type_t * result;
715 result = a -> result;
717 if(token == MINUS)
719 if(result -> class != OBERON_TYPE_INTEGER)
721 oberon_error(ctx, "incompatible operator type");
724 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
726 else if(token == NOT)
728 if(result -> class != OBERON_TYPE_BOOLEAN)
730 oberon_error(ctx, "incompatible operator type");
733 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
735 else
737 oberon_error(ctx, "oberon_make_unary_op: wat");
740 return expr;
743 static void
744 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
746 oberon_expr_t * last;
748 *num_expr = 1;
749 *first = last = oberon_expr(ctx);
750 while(ctx -> token == COMMA)
752 oberon_assert_token(ctx, COMMA);
753 oberon_expr_t * current;
755 if(const_expr)
757 current = (oberon_expr_t *) oberon_const_expr(ctx);
759 else
761 current = oberon_expr(ctx);
764 last -> next = current;
765 last = current;
766 *num_expr += 1;
770 static oberon_expr_t *
771 oberon_cast_expr(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
773 return oberon_new_operator(OP_CAST, pref, expr, NULL);
776 static oberon_expr_t *
777 oberno_make_record_cast(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * rec)
779 if(expr -> result -> class != OBERON_TYPE_RECORD
780 || rec -> class != OBERON_TYPE_RECORD)
782 oberon_error(ctx, "must be record type");
785 return oberon_cast_expr(ctx, expr, rec);
788 static oberon_type_t *
789 oberon_get_equal_expr_type(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
791 oberon_type_t * result;
792 if(a -> class == OBERON_TYPE_REAL && b -> class == OBERON_TYPE_INTEGER)
794 result = a;
796 else if(b -> class == OBERON_TYPE_REAL && a -> class == OBERON_TYPE_INTEGER)
798 result = b;
800 else if(a -> class != b -> class)
802 oberon_error(ctx, "oberon_get_equal_expr_type: incompatible types");
804 else if(a -> size > b -> size)
806 result = a;
808 else
810 result = b;
813 return result;
816 static oberon_expr_t *
817 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
819 if(pref -> class != expr -> result -> class)
821 if(pref -> class == OBERON_TYPE_POINTER)
823 if(expr -> result -> class == OBERON_TYPE_POINTER)
825 // accept
827 else
829 oberon_error(ctx, "incompatible types");
832 else if(pref -> class == OBERON_TYPE_REAL)
834 if(expr -> result -> class == OBERON_TYPE_INTEGER)
836 // accept
838 else
840 oberon_error(ctx, "incompatible types");
843 else
845 oberon_error(ctx, "incompatible types");
849 if(pref -> class == OBERON_TYPE_INTEGER || pref -> class == OBERON_TYPE_REAL)
851 if(expr -> result -> size > pref -> size)
853 oberon_error(ctx, "incompatible size");
855 else
857 expr = oberon_cast_expr(ctx, expr, pref);
860 else if(pref -> class == OBERON_TYPE_RECORD)
862 oberon_type_t * t = expr -> result;
863 while(t != NULL && t != pref)
865 t = t -> base;
867 if(t == NULL)
869 printf("oberon_autocast_to: rec %p != %p\n", expr -> result, pref);
870 oberon_error(ctx, "incompatible record types");
872 if(expr -> result != pref)
874 expr = oberno_make_record_cast(ctx, expr, pref);
877 else if(pref -> class == OBERON_TYPE_POINTER)
879 if(expr -> result -> base != pref -> base)
881 if(expr -> result -> base -> class != OBERON_TYPE_VOID)
883 oberon_error(ctx, "incompatible pointer types");
888 return expr;
891 static void
892 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_expr_t ** ea, oberon_expr_t ** eb)
894 oberon_type_t * a = (*ea) -> result;
895 oberon_type_t * b = (*eb) -> result;
896 oberon_type_t * preq = oberon_get_equal_expr_type(ctx, a, b);
897 *ea = oberon_autocast_to(ctx, *ea, preq);
898 *eb = oberon_autocast_to(ctx, *eb, preq);
901 static void
902 oberon_autocast_call(oberon_context_t * ctx, oberon_item_t * desig)
904 if(desig -> mode != MODE_CALL)
906 oberon_error(ctx, "expected mode CALL");
909 oberon_type_t * fn = desig -> parent -> result;
910 int num_args = desig -> num_args;
911 int num_decl = fn -> num_decl;
913 if(num_args < num_decl)
915 oberon_error(ctx, "too few arguments");
917 else if(num_args > num_decl)
919 oberon_error(ctx, "too many arguments");
922 /* Делаем проверку на запись и делаем автокаст */
923 oberon_expr_t * casted[num_args];
924 oberon_expr_t * arg = desig -> args;
925 oberon_object_t * param = fn -> decl;
926 for(int i = 0; i < num_args; i++)
928 if(param -> class == OBERON_CLASS_VAR_PARAM)
930 if(arg -> read_only)
932 oberon_error(ctx, "assign to read-only var");
936 casted[i] = oberon_autocast_to(ctx, arg, param -> type);
937 arg = arg -> next;
938 param = param -> next;
941 /* Создаём новый список выражений */
942 if(num_args > 0)
944 arg = casted[0];
945 for(int i = 0; i < num_args - 1; i++)
947 casted[i] -> next = casted[i + 1];
949 desig -> args = arg;
953 static oberon_expr_t *
954 oberon_make_call_func(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
956 oberon_type_t * signature = item -> result;
957 if(signature -> class != OBERON_TYPE_PROCEDURE)
959 oberon_error(ctx, "not a procedure");
962 oberon_expr_t * call;
964 if(signature -> sysproc)
966 if(signature -> genfunc == NULL)
968 oberon_error(ctx, "not a function-procedure");
971 call = signature -> genfunc(ctx, num_args, list_args);
973 else
975 if(signature -> base -> class == OBERON_TYPE_VOID)
977 oberon_error(ctx, "attempt to call procedure in expression");
980 call = oberon_new_item(MODE_CALL, signature -> base, true);
981 call -> item.parent = item;
982 call -> item.num_args = num_args;
983 call -> item.args = list_args;
984 oberon_autocast_call(ctx, (oberon_item_t *) call);
987 return call;
990 static void
991 oberon_make_call_proc(oberon_context_t * ctx, oberon_item_t * item, int num_args, oberon_expr_t * list_args)
993 oberon_type_t * signature = item -> result;
994 if(signature -> class != OBERON_TYPE_PROCEDURE)
996 oberon_error(ctx, "not a procedure");
999 oberon_expr_t * call;
1001 if(signature -> sysproc)
1003 if(signature -> genproc == NULL)
1005 oberon_error(ctx, "not a procedure");
1008 signature -> genproc(ctx, num_args, list_args);
1010 else
1012 if(signature -> base -> class != OBERON_TYPE_VOID)
1014 oberon_error(ctx, "attempt to call function as non-typed procedure");
1017 call = oberon_new_item(MODE_CALL, signature -> base, true);
1018 call -> item.parent = item;
1019 call -> item.num_args = num_args;
1020 call -> item.args = list_args;
1021 oberon_autocast_call(ctx, (oberon_item_t *) call);
1022 oberon_generate_call_proc(ctx, call);
1026 /*
1027 static void
1028 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
1030 switch(proc -> class)
1032 case OBERON_CLASS_PROC:
1033 if(proc -> class != OBERON_CLASS_PROC)
1035 oberon_error(ctx, "not a procedure");
1037 break;
1038 case OBERON_CLASS_VAR:
1039 case OBERON_CLASS_VAR_PARAM:
1040 case OBERON_CLASS_PARAM:
1041 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
1043 oberon_error(ctx, "not a procedure");
1045 break;
1046 default:
1047 oberon_error(ctx, "not a procedure");
1048 break;
1051 if(proc -> sysproc)
1053 if(proc -> genproc == NULL)
1055 oberon_error(ctx, "requres non-typed procedure");
1058 proc -> genproc(ctx, num_args, list_args);
1060 else
1062 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
1064 oberon_error(ctx, "attempt to call function as non-typed procedure");
1067 oberon_expr_t * call;
1068 call = oberon_new_item(MODE_CALL, proc -> type -> base, 1);
1069 call -> item.var = proc;
1070 call -> item.num_args = num_args;
1071 call -> item.args = list_args;
1072 oberon_autocast_call(ctx, call);
1073 oberon_generate_call_proc(ctx, call);
1076 */
1078 #define ISEXPR(x) \
1079 (((x) == PLUS) \
1080 || ((x) == MINUS) \
1081 || ((x) == IDENT) \
1082 || ((x) == INTEGER) \
1083 || ((x) == LPAREN) \
1084 || ((x) == NOT) \
1085 || ((x) == TRUE) \
1086 || ((x) == FALSE))
1088 static oberon_expr_t *
1089 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
1091 if(expr -> result -> class != OBERON_TYPE_POINTER)
1093 oberon_error(ctx, "not a pointer");
1096 assert(expr -> is_item);
1098 oberon_expr_t * selector;
1099 selector = oberon_new_item(MODE_DEREF, expr -> result -> base, expr -> read_only);
1100 selector -> item.parent = (oberon_item_t *) expr;
1102 return selector;
1105 static oberon_expr_t *
1106 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
1108 if(desig -> result -> class == OBERON_TYPE_POINTER)
1110 desig = oberno_make_dereferencing(ctx, desig);
1113 assert(desig -> is_item);
1115 if(desig -> result -> class != OBERON_TYPE_ARRAY)
1117 oberon_error(ctx, "not array");
1120 oberon_type_t * base;
1121 base = desig -> result -> base;
1123 if(index -> result -> class != OBERON_TYPE_INTEGER)
1125 oberon_error(ctx, "index must be integer");
1128 // Статическая проверка границ массива
1129 if(desig -> result -> size != 0)
1131 if(index -> is_item)
1133 if(index -> item.mode == MODE_INTEGER)
1135 int arr_size = desig -> result -> size;
1136 int index_int = index -> item.integer;
1137 if(index_int < 0 || index_int > arr_size - 1)
1139 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
1145 oberon_expr_t * selector;
1146 selector = oberon_new_item(MODE_INDEX, base, desig -> read_only);
1147 selector -> item.parent = (oberon_item_t *) desig;
1148 selector -> item.num_args = 1;
1149 selector -> item.args = index;
1151 return selector;
1154 static oberon_expr_t *
1155 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
1157 if(expr -> result -> class == OBERON_TYPE_POINTER)
1159 expr = oberno_make_dereferencing(ctx, expr);
1162 assert(expr -> is_item);
1164 if(expr -> result -> class != OBERON_TYPE_RECORD)
1166 oberon_error(ctx, "not record");
1169 oberon_type_t * rec = expr -> result;
1171 oberon_object_t * field;
1172 field = oberon_find_object(rec -> scope, name, true);
1174 if(field -> export == 0)
1176 if(field -> module != ctx -> mod)
1178 oberon_error(ctx, "field not exported");
1182 int read_only = 0;
1183 if(field -> read_only)
1185 if(field -> module != ctx -> mod)
1187 read_only = 1;
1191 oberon_expr_t * selector;
1192 selector = oberon_new_item(MODE_FIELD, field -> type, read_only);
1193 selector -> item.var = field;
1194 selector -> item.parent = (oberon_item_t *) expr;
1196 return selector;
1199 #define ISSELECTOR(x) \
1200 (((x) == LBRACE) \
1201 || ((x) == DOT) \
1202 || ((x) == UPARROW) \
1203 || ((x) == LPAREN))
1205 static oberon_object_t *
1206 oberon_qualident(oberon_context_t * ctx, char ** xname, int check)
1208 char * name;
1209 oberon_object_t * x;
1211 name = oberon_assert_ident(ctx);
1212 x = oberon_find_object(ctx -> decl, name, check);
1214 if(x != NULL)
1216 if(x -> class == OBERON_CLASS_MODULE)
1218 oberon_assert_token(ctx, DOT);
1219 name = oberon_assert_ident(ctx);
1220 /* Наличие объектов в левых модулях всегда проверяется */
1221 x = oberon_find_object(x -> module -> decl, name, 1);
1223 if(x -> export == 0)
1225 oberon_error(ctx, "not exported");
1230 if(xname)
1232 *xname = name;
1235 return x;
1238 static oberon_expr_t *
1239 oberon_designator(oberon_context_t * ctx)
1241 char * name;
1242 oberon_object_t * var;
1243 oberon_expr_t * expr;
1245 var = oberon_qualident(ctx, NULL, 1);
1247 int read_only = 0;
1248 if(var -> read_only)
1250 if(var -> module != ctx -> mod)
1252 read_only = 1;
1256 switch(var -> class)
1258 case OBERON_CLASS_CONST:
1259 // TODO copy value
1260 expr = (oberon_expr_t *) var -> value;
1261 break;
1262 case OBERON_CLASS_VAR:
1263 case OBERON_CLASS_VAR_PARAM:
1264 case OBERON_CLASS_PARAM:
1265 expr = oberon_new_item(MODE_VAR, var -> type, read_only);
1266 break;
1267 case OBERON_CLASS_PROC:
1268 expr = oberon_new_item(MODE_VAR, var -> type, 1);
1269 break;
1270 default:
1271 oberon_error(ctx, "invalid designator");
1272 break;
1274 expr -> item.var = var;
1276 bool brk = false;
1277 while(brk == false && ISSELECTOR(ctx -> token))
1279 switch(ctx -> token)
1281 case DOT:
1282 oberon_assert_token(ctx, DOT);
1283 name = oberon_assert_ident(ctx);
1284 expr = oberon_make_record_selector(ctx, expr, name);
1285 break;
1286 case LBRACE:
1287 oberon_assert_token(ctx, LBRACE);
1288 int num_indexes = 0;
1289 oberon_expr_t * indexes = NULL;
1290 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1291 oberon_assert_token(ctx, RBRACE);
1293 for(int i = 0; i < num_indexes; i++)
1295 expr = oberon_make_array_selector(ctx, expr, indexes);
1296 indexes = indexes -> next;
1298 break;
1299 case UPARROW:
1300 oberon_assert_token(ctx, UPARROW);
1301 expr = oberno_make_dereferencing(ctx, expr);
1302 break;
1303 case LPAREN:
1304 if(expr -> result -> class == OBERON_TYPE_PROCEDURE)
1306 brk = true;
1307 break;
1309 oberon_assert_token(ctx, LPAREN);
1310 oberon_object_t * objtype = oberon_qualident(ctx, NULL, 1);
1311 if(objtype -> class != OBERON_CLASS_TYPE)
1313 oberon_error(ctx, "must be type");
1315 oberon_assert_token(ctx, RPAREN);
1316 expr = oberno_make_record_cast(ctx, expr, objtype -> type);
1317 break;
1318 default:
1319 oberon_error(ctx, "oberon_designator: wat");
1320 break;
1323 return expr;
1326 static oberon_expr_t *
1327 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1329 assert(expr -> is_item == 1);
1331 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1332 if(ctx -> token == LPAREN)
1334 oberon_assert_token(ctx, LPAREN);
1336 int num_args = 0;
1337 oberon_expr_t * arguments = NULL;
1339 if(ISEXPR(ctx -> token))
1341 oberon_expr_list(ctx, &num_args, &arguments, 0);
1344 expr = oberon_make_call_func(ctx, (oberon_item_t *) expr, num_args, arguments);
1346 oberon_assert_token(ctx, RPAREN);
1349 return expr;
1352 static void
1353 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1355 assert(expr -> is_item == 1);
1357 int num_args = 0;
1358 oberon_expr_t * arguments = NULL;
1360 if(ctx -> token == LPAREN)
1362 oberon_assert_token(ctx, LPAREN);
1364 if(ISEXPR(ctx -> token))
1366 oberon_expr_list(ctx, &num_args, &arguments, 0);
1369 oberon_assert_token(ctx, RPAREN);
1372 /* Вызов происходит даже без скобок */
1373 oberon_make_call_proc(ctx, (oberon_item_t *) expr, num_args, arguments);
1376 static oberon_type_t *
1377 oberon_get_type_of_int_value(oberon_context_t * ctx, int64_t i)
1379 if(i >= -128 && i <= 127)
1381 return ctx -> byte_type;
1383 else if(i >= -32768 && i <= 32767)
1385 return ctx -> shortint_type;
1387 else if(i >= -2147483648 && i <= 2147483647)
1389 return ctx -> int_type;
1391 else
1393 return ctx -> longint_type;
1397 static oberon_expr_t *
1398 oberon_factor(oberon_context_t * ctx)
1400 oberon_expr_t * expr;
1401 oberon_type_t * result;
1403 switch(ctx -> token)
1405 case IDENT:
1406 expr = oberon_designator(ctx);
1407 expr = oberon_opt_func_parens(ctx, expr);
1408 break;
1409 case INTEGER:
1410 result = oberon_get_type_of_int_value(ctx, ctx -> integer);
1411 expr = oberon_new_item(MODE_INTEGER, result, 1);
1412 expr -> item.integer = ctx -> integer;
1413 oberon_assert_token(ctx, INTEGER);
1414 break;
1415 case REAL:
1416 result = (ctx -> longmode) ? (ctx -> longreal_type) : (ctx -> real_type);
1417 expr = oberon_new_item(MODE_REAL, result, 1);
1418 expr -> item.real = ctx -> real;
1419 oberon_assert_token(ctx, REAL);
1420 break;
1421 case TRUE:
1422 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, 1);
1423 expr -> item.boolean = true;
1424 oberon_assert_token(ctx, TRUE);
1425 break;
1426 case FALSE:
1427 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type, 1);
1428 expr -> item.boolean = false;
1429 oberon_assert_token(ctx, FALSE);
1430 break;
1431 case LPAREN:
1432 oberon_assert_token(ctx, LPAREN);
1433 expr = oberon_expr(ctx);
1434 oberon_assert_token(ctx, RPAREN);
1435 break;
1436 case NOT:
1437 oberon_assert_token(ctx, NOT);
1438 expr = oberon_factor(ctx);
1439 expr = oberon_make_unary_op(ctx, NOT, expr);
1440 break;
1441 case NIL:
1442 oberon_assert_token(ctx, NIL);
1443 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type, 1);
1444 break;
1445 default:
1446 oberon_error(ctx, "invalid expression");
1449 return expr;
1452 #define ITMAKESBOOLEAN(x) \
1453 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1455 #define ITUSEONLYINTEGER(x) \
1456 ((x) >= LESS && (x) <= GEQ)
1458 #define ITUSEONLYBOOLEAN(x) \
1459 (((x) == OR) || ((x) == AND))
1461 static void
1462 oberon_autocast_to_real(oberon_context_t * ctx, oberon_expr_t ** e)
1464 oberon_expr_t * expr = *e;
1465 if(expr -> result -> class == OBERON_TYPE_INTEGER)
1467 if(expr -> result -> size <= ctx -> real_type -> size)
1469 *e = oberon_cast_expr(ctx, expr, ctx -> real_type);
1471 else
1473 *e = oberon_cast_expr(ctx, expr, ctx -> longreal_type);
1476 else if(expr -> result -> class != OBERON_TYPE_REAL)
1478 oberon_error(ctx, "required numeric type");
1482 static oberon_expr_t *
1483 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1485 oberon_expr_t * expr;
1486 oberon_type_t * result;
1488 if(ITMAKESBOOLEAN(token))
1490 if(ITUSEONLYINTEGER(token))
1492 if(a -> result -> class == OBERON_TYPE_INTEGER
1493 || b -> result -> class == OBERON_TYPE_INTEGER
1494 || a -> result -> class == OBERON_TYPE_REAL
1495 || b -> result -> class == OBERON_TYPE_REAL)
1497 oberon_error(ctx, "used only with numeric types");
1500 else if(ITUSEONLYBOOLEAN(token))
1502 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1503 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1505 oberon_error(ctx, "used only with boolean type");
1509 oberon_autocast_binary_op(ctx, &a, &b);
1510 result = ctx -> bool_type;
1512 if(token == EQUAL)
1514 expr = oberon_new_operator(OP_EQ, result, a, b);
1516 else if(token == NEQ)
1518 expr = oberon_new_operator(OP_NEQ, result, a, b);
1520 else if(token == LESS)
1522 expr = oberon_new_operator(OP_LSS, result, a, b);
1524 else if(token == LEQ)
1526 expr = oberon_new_operator(OP_LEQ, result, a, b);
1528 else if(token == GREAT)
1530 expr = oberon_new_operator(OP_GRT, result, a, b);
1532 else if(token == GEQ)
1534 expr = oberon_new_operator(OP_GEQ, result, a, b);
1536 else if(token == OR)
1538 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1540 else if(token == AND)
1542 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1544 else
1546 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1549 else if(token == SLASH)
1551 oberon_autocast_to_real(ctx, &a);
1552 oberon_autocast_to_real(ctx, &b);
1553 oberon_autocast_binary_op(ctx, &a, &b);
1554 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1556 else if(token == DIV)
1558 if(a -> result -> class != OBERON_TYPE_INTEGER
1559 || b -> result -> class != OBERON_TYPE_INTEGER)
1561 oberon_error(ctx, "operator DIV requires integer type");
1564 oberon_autocast_binary_op(ctx, &a, &b);
1565 expr = oberon_new_operator(OP_DIV, a -> result, a, b);
1567 else
1569 oberon_autocast_binary_op(ctx, &a, &b);
1571 if(token == PLUS)
1573 expr = oberon_new_operator(OP_ADD, a -> result, a, b);
1575 else if(token == MINUS)
1577 expr = oberon_new_operator(OP_SUB, a -> result, a, b);
1579 else if(token == STAR)
1581 expr = oberon_new_operator(OP_MUL, a -> result, a, b);
1583 else if(token == MOD)
1585 expr = oberon_new_operator(OP_MOD, a -> result, a, b);
1587 else
1589 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1593 return expr;
1596 #define ISMULOP(x) \
1597 ((x) >= STAR && (x) <= AND)
1599 static oberon_expr_t *
1600 oberon_term_expr(oberon_context_t * ctx)
1602 oberon_expr_t * expr;
1604 expr = oberon_factor(ctx);
1605 while(ISMULOP(ctx -> token))
1607 int token = ctx -> token;
1608 oberon_read_token(ctx);
1610 oberon_expr_t * inter = oberon_factor(ctx);
1611 expr = oberon_make_bin_op(ctx, token, expr, inter);
1614 return expr;
1617 #define ISADDOP(x) \
1618 ((x) >= PLUS && (x) <= OR)
1620 static oberon_expr_t *
1621 oberon_simple_expr(oberon_context_t * ctx)
1623 oberon_expr_t * expr;
1625 int minus = 0;
1626 if(ctx -> token == PLUS)
1628 minus = 0;
1629 oberon_assert_token(ctx, PLUS);
1631 else if(ctx -> token == MINUS)
1633 minus = 1;
1634 oberon_assert_token(ctx, MINUS);
1637 expr = oberon_term_expr(ctx);
1639 if(minus)
1641 expr = oberon_make_unary_op(ctx, MINUS, expr);
1644 while(ISADDOP(ctx -> token))
1646 int token = ctx -> token;
1647 oberon_read_token(ctx);
1649 oberon_expr_t * inter = oberon_term_expr(ctx);
1650 expr = oberon_make_bin_op(ctx, token, expr, inter);
1653 return expr;
1656 #define ISRELATION(x) \
1657 ((x) >= EQUAL && (x) <= IS)
1659 static oberon_expr_t *
1660 oberon_expr(oberon_context_t * ctx)
1662 oberon_expr_t * expr;
1664 expr = oberon_simple_expr(ctx);
1665 while(ISRELATION(ctx -> token))
1667 int token = ctx -> token;
1668 oberon_read_token(ctx);
1670 oberon_expr_t * inter = oberon_simple_expr(ctx);
1671 expr = oberon_make_bin_op(ctx, token, expr, inter);
1674 return expr;
1677 static oberon_item_t *
1678 oberon_const_expr(oberon_context_t * ctx)
1680 oberon_expr_t * expr;
1681 expr = oberon_expr(ctx);
1683 if(expr -> is_item == 0)
1685 oberon_error(ctx, "const expression are required");
1688 return (oberon_item_t *) expr;
1691 // =======================================================================
1692 // PARSER
1693 // =======================================================================
1695 static void oberon_decl_seq(oberon_context_t * ctx);
1696 static void oberon_statement_seq(oberon_context_t * ctx);
1697 static void oberon_initialize_decl(oberon_context_t * ctx);
1699 static void
1700 oberon_expect_token(oberon_context_t * ctx, int token)
1702 if(ctx -> token != token)
1704 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
1708 static void
1709 oberon_assert_token(oberon_context_t * ctx, int token)
1711 oberon_expect_token(ctx, token);
1712 oberon_read_token(ctx);
1715 static char *
1716 oberon_assert_ident(oberon_context_t * ctx)
1718 oberon_expect_token(ctx, IDENT);
1719 char * ident = ctx -> string;
1720 oberon_read_token(ctx);
1721 return ident;
1724 static void
1725 oberon_def(oberon_context_t * ctx, int * export, int * read_only)
1727 switch(ctx -> token)
1729 case STAR:
1730 oberon_assert_token(ctx, STAR);
1731 *export = 1;
1732 *read_only = 0;
1733 break;
1734 case MINUS:
1735 oberon_assert_token(ctx, MINUS);
1736 *export = 1;
1737 *read_only = 1;
1738 break;
1739 default:
1740 *export = 0;
1741 *read_only = 0;
1742 break;
1746 static oberon_object_t *
1747 oberon_ident_def(oberon_context_t * ctx, int class, bool check_upscope)
1749 char * name;
1750 int export;
1751 int read_only;
1752 oberon_object_t * x;
1754 name = oberon_assert_ident(ctx);
1755 oberon_def(ctx, &export, &read_only);
1757 x = oberon_define_object(ctx -> decl, name, class, export, read_only, check_upscope);
1758 return x;
1761 static void
1762 oberon_ident_list(oberon_context_t * ctx, int class, bool check_upscope, int * num, oberon_object_t ** list)
1764 *num = 1;
1765 *list = oberon_ident_def(ctx, class, check_upscope);
1766 while(ctx -> token == COMMA)
1768 oberon_assert_token(ctx, COMMA);
1769 oberon_ident_def(ctx, class, check_upscope);
1770 *num += 1;
1774 static void
1775 oberon_var_decl(oberon_context_t * ctx)
1777 int num;
1778 oberon_object_t * list;
1779 oberon_type_t * type;
1780 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1782 oberon_ident_list(ctx, OBERON_CLASS_VAR, false, &num, &list);
1783 oberon_assert_token(ctx, COLON);
1784 oberon_type(ctx, &type);
1786 oberon_object_t * var = list;
1787 for(int i = 0; i < num; i++)
1789 var -> type = type;
1790 var = var -> next;
1794 static oberon_object_t *
1795 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
1797 int class = OBERON_CLASS_PARAM;
1798 if(ctx -> token == VAR)
1800 oberon_read_token(ctx);
1801 class = OBERON_CLASS_VAR_PARAM;
1804 int num;
1805 oberon_object_t * list;
1806 oberon_ident_list(ctx, class, false, &num, &list);
1808 oberon_assert_token(ctx, COLON);
1810 oberon_type_t * type;
1811 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1812 oberon_type(ctx, &type);
1814 oberon_object_t * param = list;
1815 for(int i = 0; i < num; i++)
1817 param -> type = type;
1818 param = param -> next;
1821 *num_decl += num;
1822 return list;
1825 #define ISFPSECTION \
1826 ((ctx -> token == VAR) || (ctx -> token == IDENT))
1828 static void
1829 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
1831 oberon_assert_token(ctx, LPAREN);
1833 if(ISFPSECTION)
1835 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
1836 while(ctx -> token == SEMICOLON)
1838 oberon_assert_token(ctx, SEMICOLON);
1839 oberon_fp_section(ctx, &signature -> num_decl);
1843 oberon_assert_token(ctx, RPAREN);
1845 if(ctx -> token == COLON)
1847 oberon_assert_token(ctx, COLON);
1849 oberon_object_t * typeobj;
1850 typeobj = oberon_qualident(ctx, NULL, 1);
1851 if(typeobj -> class != OBERON_CLASS_TYPE)
1853 oberon_error(ctx, "function result is not type");
1855 signature -> base = typeobj -> type;
1859 static void
1860 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
1862 oberon_type_t * signature;
1863 signature = *type;
1864 signature -> class = OBERON_TYPE_PROCEDURE;
1865 signature -> num_decl = 0;
1866 signature -> base = ctx -> void_type;
1867 signature -> decl = NULL;
1869 if(ctx -> token == LPAREN)
1871 oberon_formal_pars(ctx, signature);
1875 static void
1876 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
1878 if(a -> num_decl != b -> num_decl)
1880 oberon_error(ctx, "number parameters not matched");
1883 int num_param = a -> num_decl;
1884 oberon_object_t * param_a = a -> decl;
1885 oberon_object_t * param_b = b -> decl;
1886 for(int i = 0; i < num_param; i++)
1888 if(strcmp(param_a -> name, param_b -> name) != 0)
1890 oberon_error(ctx, "param %i name not matched", i + 1);
1893 if(param_a -> type != param_b -> type)
1895 oberon_error(ctx, "param %i type not matched", i + 1);
1898 param_a = param_a -> next;
1899 param_b = param_b -> next;
1903 static void
1904 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
1906 oberon_object_t * proc = ctx -> decl -> parent;
1907 oberon_type_t * result_type = proc -> type -> base;
1909 if(result_type -> class == OBERON_TYPE_VOID)
1911 if(expr != NULL)
1913 oberon_error(ctx, "procedure has no result type");
1916 else
1918 if(expr == NULL)
1920 oberon_error(ctx, "procedure requires expression on result");
1923 expr = oberon_autocast_to(ctx, expr, result_type);
1926 proc -> has_return = 1;
1928 oberon_generate_return(ctx, expr);
1931 static void
1932 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
1934 oberon_assert_token(ctx, SEMICOLON);
1936 ctx -> decl = proc -> scope;
1938 oberon_decl_seq(ctx);
1940 oberon_generate_begin_proc(ctx, proc);
1942 if(ctx -> token == BEGIN)
1944 oberon_assert_token(ctx, BEGIN);
1945 oberon_statement_seq(ctx);
1948 oberon_assert_token(ctx, END);
1949 char * name = oberon_assert_ident(ctx);
1950 if(strcmp(name, proc -> name) != 0)
1952 oberon_error(ctx, "procedure name not matched");
1955 if(proc -> type -> base -> class == OBERON_TYPE_VOID
1956 && proc -> has_return == 0)
1958 oberon_make_return(ctx, NULL);
1961 if(proc -> has_return == 0)
1963 oberon_error(ctx, "procedure requires return");
1966 oberon_generate_end_proc(ctx);
1967 oberon_close_scope(ctx -> decl);
1970 static void
1971 oberon_proc_decl(oberon_context_t * ctx)
1973 oberon_assert_token(ctx, PROCEDURE);
1975 int forward = 0;
1976 if(ctx -> token == UPARROW)
1978 oberon_assert_token(ctx, UPARROW);
1979 forward = 1;
1982 char * name;
1983 int export;
1984 int read_only;
1985 name = oberon_assert_ident(ctx);
1986 oberon_def(ctx, &export, &read_only);
1988 oberon_scope_t * proc_scope;
1989 proc_scope = oberon_open_scope(ctx);
1990 ctx -> decl -> local = 1;
1992 oberon_type_t * signature;
1993 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
1994 oberon_opt_formal_pars(ctx, &signature);
1996 oberon_initialize_decl(ctx);
1997 oberon_generator_init_type(ctx, signature);
1998 oberon_close_scope(ctx -> decl);
2000 oberon_object_t * proc;
2001 proc = oberon_find_object(ctx -> decl, name, 0);
2002 if(proc != NULL)
2004 if(proc -> class != OBERON_CLASS_PROC)
2006 oberon_error(ctx, "mult definition");
2009 if(forward == 0)
2011 if(proc -> linked)
2013 oberon_error(ctx, "mult procedure definition");
2017 if(proc -> export != export || proc -> read_only != read_only)
2019 oberon_error(ctx, "export type not matched");
2022 oberon_compare_signatures(ctx, proc -> type, signature);
2024 else
2026 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false);
2027 proc -> type = signature;
2028 proc -> scope = proc_scope;
2029 oberon_generator_init_proc(ctx, proc);
2032 proc -> scope -> parent = proc;
2034 if(forward == 0)
2036 proc -> linked = 1;
2037 oberon_proc_decl_body(ctx, proc);
2041 static void
2042 oberon_const_decl(oberon_context_t * ctx)
2044 oberon_item_t * value;
2045 oberon_object_t * constant;
2047 constant = oberon_ident_def(ctx, OBERON_CLASS_CONST, false);
2048 oberon_assert_token(ctx, EQUAL);
2049 value = oberon_const_expr(ctx);
2050 constant -> value = value;
2053 static void
2054 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
2056 if(size -> is_item == 0)
2058 oberon_error(ctx, "requires constant");
2061 if(size -> item.mode != MODE_INTEGER)
2063 oberon_error(ctx, "requires integer constant");
2066 oberon_type_t * arr;
2067 arr = *type;
2068 arr -> class = OBERON_TYPE_ARRAY;
2069 arr -> size = size -> item.integer;
2070 arr -> base = base;
2073 static void
2074 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
2076 char * name;
2077 oberon_object_t * to;
2079 to = oberon_qualident(ctx, &name, 0);
2081 //name = oberon_assert_ident(ctx);
2082 //to = oberon_find_object(ctx -> decl, name, 0);
2084 if(to != NULL)
2086 if(to -> class != OBERON_CLASS_TYPE)
2088 oberon_error(ctx, "not a type");
2091 else
2093 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, false, false, false);
2094 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2097 *type = to -> type;
2100 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
2102 /*
2103 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2104 */
2106 static void
2107 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
2109 if(sizes == NULL)
2111 *type = base;
2112 return;
2115 oberon_type_t * dim;
2116 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
2118 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
2120 oberon_make_array_type(ctx, sizes, dim, type);
2123 static void
2124 oberon_make_open_array(oberon_context_t * ctx, oberon_type_t * base, oberon_type_t * type)
2126 type -> class = OBERON_TYPE_ARRAY;
2127 type -> size = 0;
2128 type -> base = base;
2131 static void
2132 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec, oberon_scope_t * modscope)
2134 if(ctx -> token == IDENT)
2136 int num;
2137 oberon_object_t * list;
2138 oberon_type_t * type;
2139 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2141 oberon_ident_list(ctx, OBERON_CLASS_FIELD, true, &num, &list);
2142 oberon_assert_token(ctx, COLON);
2144 oberon_scope_t * current = ctx -> decl;
2145 ctx -> decl = modscope;
2146 oberon_type(ctx, &type);
2147 ctx -> decl = current;
2149 oberon_object_t * field = list;
2150 for(int i = 0; i < num; i++)
2152 field -> type = type;
2153 field = field -> next;
2156 rec -> num_decl += num;
2160 static void
2161 oberon_type_record_body(oberon_context_t * ctx, oberon_type_t * rec)
2163 oberon_scope_t * modscope = ctx -> mod -> decl;
2164 oberon_scope_t * oldscope = ctx -> decl;
2165 ctx -> decl = modscope;
2167 if(ctx -> token == LPAREN)
2169 oberon_assert_token(ctx, LPAREN);
2171 oberon_object_t * typeobj;
2172 typeobj = oberon_qualident(ctx, NULL, true);
2174 if(typeobj -> class != OBERON_CLASS_TYPE)
2176 oberon_error(ctx, "base must be type");
2179 if(typeobj -> type -> class != OBERON_TYPE_RECORD)
2181 oberon_error(ctx, "base must be record type");
2184 rec -> base = typeobj -> type;
2185 ctx -> decl = rec -> base -> scope;
2187 oberon_assert_token(ctx, RPAREN);
2189 else
2191 ctx -> decl = NULL;
2194 oberon_scope_t * this_scope;
2195 this_scope = oberon_open_scope(ctx);
2196 this_scope -> local = true;
2197 this_scope -> parent = NULL;
2198 this_scope -> parent_type = rec;
2200 oberon_field_list(ctx, rec, modscope);
2201 while(ctx -> token == SEMICOLON)
2203 oberon_assert_token(ctx, SEMICOLON);
2204 oberon_field_list(ctx, rec, modscope);
2207 rec -> scope = this_scope;
2208 rec -> decl = this_scope -> list -> next;
2209 ctx -> decl = oldscope;
2212 static void
2213 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
2215 if(ctx -> token == IDENT)
2217 oberon_qualident_type(ctx, type);
2219 else if(ctx -> token == ARRAY)
2221 oberon_assert_token(ctx, ARRAY);
2223 int num_sizes = 0;
2224 oberon_expr_t * sizes;
2226 if(ISEXPR(ctx -> token))
2228 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
2231 oberon_assert_token(ctx, OF);
2233 oberon_type_t * base;
2234 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2235 oberon_type(ctx, &base);
2237 if(num_sizes == 0)
2239 oberon_make_open_array(ctx, base, *type);
2241 else
2243 oberon_make_multiarray(ctx, sizes, base, type);
2246 else if(ctx -> token == RECORD)
2248 oberon_type_t * rec;
2249 rec = *type;
2250 rec -> class = OBERON_TYPE_RECORD;
2251 rec -> module = ctx -> mod;
2253 oberon_assert_token(ctx, RECORD);
2254 oberon_type_record_body(ctx, rec);
2255 oberon_assert_token(ctx, END);
2257 *type = rec;
2259 else if(ctx -> token == POINTER)
2261 oberon_assert_token(ctx, POINTER);
2262 oberon_assert_token(ctx, TO);
2264 oberon_type_t * base;
2265 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
2266 oberon_type(ctx, &base);
2268 oberon_type_t * ptr;
2269 ptr = *type;
2270 ptr -> class = OBERON_TYPE_POINTER;
2271 ptr -> base = base;
2273 else if(ctx -> token == PROCEDURE)
2275 oberon_open_scope(ctx);
2276 oberon_assert_token(ctx, PROCEDURE);
2277 oberon_opt_formal_pars(ctx, type);
2278 oberon_close_scope(ctx -> decl);
2280 else
2282 oberon_error(ctx, "invalid type declaration");
2286 static void
2287 oberon_type_decl(oberon_context_t * ctx)
2289 char * name;
2290 oberon_object_t * newtype;
2291 oberon_type_t * type;
2292 int export;
2293 int read_only;
2295 name = oberon_assert_ident(ctx);
2296 oberon_def(ctx, &export, &read_only);
2298 newtype = oberon_find_object(ctx -> decl, name, 0);
2299 if(newtype == NULL)
2301 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE, export, read_only, false);
2302 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2303 assert(newtype -> type);
2305 else
2307 if(newtype -> class != OBERON_CLASS_TYPE)
2309 oberon_error(ctx, "mult definition");
2312 if(newtype -> linked)
2314 oberon_error(ctx, "mult definition - already linked");
2317 newtype -> export = export;
2318 newtype -> read_only = read_only;
2321 oberon_assert_token(ctx, EQUAL);
2323 type = newtype -> type;
2324 oberon_type(ctx, &type);
2326 if(type -> class == OBERON_TYPE_VOID)
2328 oberon_error(ctx, "recursive alias declaration");
2331 newtype -> type = type;
2332 newtype -> linked = 1;
2335 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
2336 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
2338 static void
2339 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
2341 if(type -> class != OBERON_TYPE_POINTER
2342 && type -> class != OBERON_TYPE_ARRAY)
2344 return;
2347 if(type -> recursive)
2349 oberon_error(ctx, "recursive pointer declaration");
2352 if(type -> class == OBERON_TYPE_POINTER
2353 && type -> base -> class == OBERON_TYPE_POINTER)
2355 oberon_error(ctx, "attempt to make pointer to pointer");
2358 type -> recursive = 1;
2360 oberon_prevent_recursive_pointer(ctx, type -> base);
2362 type -> recursive = 0;
2365 static void
2366 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
2368 if(type -> class != OBERON_TYPE_RECORD)
2370 return;
2373 if(type -> recursive)
2375 oberon_error(ctx, "recursive record declaration");
2378 type -> recursive = 1;
2380 int num_fields = type -> num_decl;
2381 oberon_object_t * field = type -> decl;
2382 for(int i = 0; i < num_fields; i++)
2384 oberon_prevent_recursive_object(ctx, field);
2385 field = field -> next;
2388 type -> recursive = 0;
2390 static void
2391 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
2393 if(type -> class != OBERON_TYPE_PROCEDURE)
2395 return;
2398 if(type -> recursive)
2400 oberon_error(ctx, "recursive procedure declaration");
2403 type -> recursive = 1;
2405 int num_fields = type -> num_decl;
2406 oberon_object_t * field = type -> decl;
2407 for(int i = 0; i < num_fields; i++)
2409 oberon_prevent_recursive_object(ctx, field);
2410 field = field -> next;
2413 type -> recursive = 0;
2416 static void
2417 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
2419 if(type -> class != OBERON_TYPE_ARRAY)
2421 return;
2424 if(type -> recursive)
2426 oberon_error(ctx, "recursive array declaration");
2429 type -> recursive = 1;
2431 oberon_prevent_recursive_type(ctx, type -> base);
2433 type -> recursive = 0;
2436 static void
2437 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
2439 if(type -> class == OBERON_TYPE_POINTER)
2441 oberon_prevent_recursive_pointer(ctx, type);
2443 else if(type -> class == OBERON_TYPE_RECORD)
2445 oberon_prevent_recursive_record(ctx, type);
2447 else if(type -> class == OBERON_TYPE_ARRAY)
2449 oberon_prevent_recursive_array(ctx, type);
2451 else if(type -> class == OBERON_TYPE_PROCEDURE)
2453 oberon_prevent_recursive_procedure(ctx, type);
2457 static void
2458 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2460 switch(x -> class)
2462 case OBERON_CLASS_VAR:
2463 case OBERON_CLASS_TYPE:
2464 case OBERON_CLASS_PARAM:
2465 case OBERON_CLASS_VAR_PARAM:
2466 case OBERON_CLASS_FIELD:
2467 oberon_prevent_recursive_type(ctx, x -> type);
2468 break;
2469 case OBERON_CLASS_CONST:
2470 case OBERON_CLASS_PROC:
2471 case OBERON_CLASS_MODULE:
2472 break;
2473 default:
2474 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2475 break;
2479 static void
2480 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2482 oberon_object_t * x = ctx -> decl -> list -> next;
2484 while(x)
2486 oberon_prevent_recursive_object(ctx, x);
2487 x = x -> next;
2491 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2492 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2494 static void
2495 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2497 if(type -> class != OBERON_TYPE_RECORD)
2499 return;
2502 int num_fields = type -> num_decl;
2503 oberon_object_t * field = type -> decl;
2504 for(int i = 0; i < num_fields; i++)
2506 if(field -> type -> class == OBERON_TYPE_POINTER)
2508 oberon_initialize_type(ctx, field -> type);
2511 oberon_initialize_object(ctx, field);
2512 field = field -> next;
2515 oberon_generator_init_record(ctx, type);
2518 static void
2519 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2521 if(type -> class == OBERON_TYPE_VOID)
2523 oberon_error(ctx, "undeclarated type");
2526 if(type -> initialized)
2528 return;
2531 type -> initialized = 1;
2533 if(type -> class == OBERON_TYPE_POINTER)
2535 oberon_initialize_type(ctx, type -> base);
2536 oberon_generator_init_type(ctx, type);
2538 else if(type -> class == OBERON_TYPE_ARRAY)
2540 if(type -> size != 0)
2542 if(type -> base -> class == OBERON_TYPE_ARRAY)
2544 if(type -> base -> size == 0)
2546 oberon_error(ctx, "open array not allowed as array element");
2551 oberon_initialize_type(ctx, type -> base);
2552 oberon_generator_init_type(ctx, type);
2554 else if(type -> class == OBERON_TYPE_RECORD)
2556 oberon_generator_init_type(ctx, type);
2557 oberon_initialize_record_fields(ctx, type);
2559 else if(type -> class == OBERON_TYPE_PROCEDURE)
2561 int num_fields = type -> num_decl;
2562 oberon_object_t * field = type -> decl;
2563 for(int i = 0; i < num_fields; i++)
2565 oberon_initialize_object(ctx, field);
2566 field = field -> next;
2567 }
2569 oberon_generator_init_type(ctx, type);
2571 else
2573 oberon_generator_init_type(ctx, type);
2577 static void
2578 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2580 if(x -> initialized)
2582 return;
2585 x -> initialized = 1;
2587 switch(x -> class)
2589 case OBERON_CLASS_TYPE:
2590 oberon_initialize_type(ctx, x -> type);
2591 break;
2592 case OBERON_CLASS_VAR:
2593 case OBERON_CLASS_FIELD:
2594 if(x -> type -> class == OBERON_TYPE_ARRAY)
2596 if(x -> type -> size == 0)
2598 oberon_error(ctx, "open array not allowed as variable or field");
2601 oberon_initialize_type(ctx, x -> type);
2602 oberon_generator_init_var(ctx, x);
2603 break;
2604 case OBERON_CLASS_PARAM:
2605 case OBERON_CLASS_VAR_PARAM:
2606 oberon_initialize_type(ctx, x -> type);
2607 oberon_generator_init_var(ctx, x);
2608 break;
2609 case OBERON_CLASS_CONST:
2610 case OBERON_CLASS_PROC:
2611 case OBERON_CLASS_MODULE:
2612 break;
2613 default:
2614 oberon_error(ctx, "oberon_initialize_object: wat");
2615 break;
2619 static void
2620 oberon_initialize_decl(oberon_context_t * ctx)
2622 oberon_object_t * x = ctx -> decl -> list;
2624 while(x -> next)
2626 oberon_initialize_object(ctx, x -> next);
2627 x = x -> next;
2628 }
2631 static void
2632 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2634 oberon_object_t * x = ctx -> decl -> list;
2636 while(x -> next)
2638 if(x -> next -> class == OBERON_CLASS_PROC)
2640 if(x -> next -> linked == 0)
2642 oberon_error(ctx, "unresolved forward declaration");
2645 x = x -> next;
2646 }
2649 static void
2650 oberon_decl_seq(oberon_context_t * ctx)
2652 if(ctx -> token == CONST)
2654 oberon_assert_token(ctx, CONST);
2655 while(ctx -> token == IDENT)
2657 oberon_const_decl(ctx);
2658 oberon_assert_token(ctx, SEMICOLON);
2662 if(ctx -> token == TYPE)
2664 oberon_assert_token(ctx, TYPE);
2665 while(ctx -> token == IDENT)
2667 oberon_type_decl(ctx);
2668 oberon_assert_token(ctx, SEMICOLON);
2672 if(ctx -> token == VAR)
2674 oberon_assert_token(ctx, VAR);
2675 while(ctx -> token == IDENT)
2677 oberon_var_decl(ctx);
2678 oberon_assert_token(ctx, SEMICOLON);
2682 oberon_prevent_recursive_decl(ctx);
2683 oberon_initialize_decl(ctx);
2685 while(ctx -> token == PROCEDURE)
2687 oberon_proc_decl(ctx);
2688 oberon_assert_token(ctx, SEMICOLON);
2691 oberon_prevent_undeclarated_procedures(ctx);
2694 static void
2695 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
2697 if(dst -> read_only)
2699 oberon_error(ctx, "read-only destination");
2702 src = oberon_autocast_to(ctx, src, dst -> result);
2703 oberon_generate_assign(ctx, src, dst);
2706 static void
2707 oberon_statement(oberon_context_t * ctx)
2709 oberon_expr_t * item1;
2710 oberon_expr_t * item2;
2712 if(ctx -> token == IDENT)
2714 item1 = oberon_designator(ctx);
2715 if(ctx -> token == ASSIGN)
2717 oberon_assert_token(ctx, ASSIGN);
2718 item2 = oberon_expr(ctx);
2719 oberon_assign(ctx, item2, item1);
2721 else
2723 oberon_opt_proc_parens(ctx, item1);
2726 else if(ctx -> token == RETURN)
2728 oberon_assert_token(ctx, RETURN);
2729 if(ISEXPR(ctx -> token))
2731 oberon_expr_t * expr;
2732 expr = oberon_expr(ctx);
2733 oberon_make_return(ctx, expr);
2735 else
2737 oberon_make_return(ctx, NULL);
2742 static void
2743 oberon_statement_seq(oberon_context_t * ctx)
2745 oberon_statement(ctx);
2746 while(ctx -> token == SEMICOLON)
2748 oberon_assert_token(ctx, SEMICOLON);
2749 oberon_statement(ctx);
2753 static void
2754 oberon_import_module(oberon_context_t * ctx, char * alias, char * name)
2756 oberon_module_t * m = ctx -> module_list;
2757 while(m && strcmp(m -> name, name) != 0)
2759 m = m -> next;
2762 if(m == NULL)
2764 const char * code;
2765 code = ctx -> import_module(name);
2766 if(code == NULL)
2768 oberon_error(ctx, "no such module");
2771 m = oberon_compile_module(ctx, code);
2772 assert(m);
2775 if(m -> ready == 0)
2777 oberon_error(ctx, "cyclic module import");
2780 oberon_object_t * ident;
2781 ident = oberon_define_object(ctx -> decl, alias, OBERON_CLASS_MODULE, false, false, false);
2782 ident -> module = m;
2785 static void
2786 oberon_import_decl(oberon_context_t * ctx)
2788 char * alias;
2789 char * name;
2791 alias = name = oberon_assert_ident(ctx);
2792 if(ctx -> token == ASSIGN)
2794 oberon_assert_token(ctx, ASSIGN);
2795 name = oberon_assert_ident(ctx);
2798 oberon_import_module(ctx, alias, name);
2801 static void
2802 oberon_import_list(oberon_context_t * ctx)
2804 oberon_assert_token(ctx, IMPORT);
2806 oberon_import_decl(ctx);
2807 while(ctx -> token == COMMA)
2809 oberon_assert_token(ctx, COMMA);
2810 oberon_import_decl(ctx);
2813 oberon_assert_token(ctx, SEMICOLON);
2816 static void
2817 oberon_parse_module(oberon_context_t * ctx)
2819 char * name1;
2820 char * name2;
2821 oberon_read_token(ctx);
2823 oberon_assert_token(ctx, MODULE);
2824 name1 = oberon_assert_ident(ctx);
2825 oberon_assert_token(ctx, SEMICOLON);
2826 ctx -> mod -> name = name1;
2828 oberon_generator_init_module(ctx, ctx -> mod);
2830 if(ctx -> token == IMPORT)
2832 oberon_import_list(ctx);
2835 oberon_decl_seq(ctx);
2837 oberon_generate_begin_module(ctx);
2838 if(ctx -> token == BEGIN)
2840 oberon_assert_token(ctx, BEGIN);
2841 oberon_statement_seq(ctx);
2843 oberon_generate_end_module(ctx);
2845 oberon_assert_token(ctx, END);
2846 name2 = oberon_assert_ident(ctx);
2847 oberon_assert_token(ctx, DOT);
2849 if(strcmp(name1, name2) != 0)
2851 oberon_error(ctx, "module name not matched");
2854 oberon_generator_fini_module(ctx -> mod);
2857 // =======================================================================
2858 // LIBRARY
2859 // =======================================================================
2861 static void
2862 register_default_types(oberon_context_t * ctx)
2864 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2865 oberon_generator_init_type(ctx, ctx -> void_type);
2867 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
2868 ctx -> void_ptr_type -> base = ctx -> void_type;
2869 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
2871 ctx -> bool_type = oberon_new_type_boolean();
2872 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type, 1);
2874 ctx -> byte_type = oberon_new_type_integer(1);
2875 oberon_define_type(ctx -> world_scope, "BYTE", ctx -> byte_type, 1);
2877 ctx -> shortint_type = oberon_new_type_integer(2);
2878 oberon_define_type(ctx -> world_scope, "SHORTINT", ctx -> shortint_type, 1);
2880 ctx -> int_type = oberon_new_type_integer(4);
2881 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type, 1);
2883 ctx -> longint_type = oberon_new_type_integer(8);
2884 oberon_define_type(ctx -> world_scope, "LONGINT", ctx -> longint_type, 1);
2886 ctx -> real_type = oberon_new_type_real(4);
2887 oberon_define_type(ctx -> world_scope, "REAL", ctx -> real_type, 1);
2889 ctx -> longreal_type = oberon_new_type_real(8);
2890 oberon_define_type(ctx -> world_scope, "LONGREAL", ctx -> longreal_type, 1);
2893 static void
2894 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
2896 oberon_object_t * proc;
2897 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, true, false, false);
2898 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
2899 proc -> type -> sysproc = true;
2900 proc -> type -> genfunc = f;
2901 proc -> type -> genproc = p;
2904 static oberon_expr_t *
2905 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
2907 if(num_args < 1)
2909 oberon_error(ctx, "too few arguments");
2912 if(num_args > 1)
2914 oberon_error(ctx, "too mach arguments");
2917 oberon_expr_t * arg;
2918 arg = list_args;
2920 oberon_type_t * result_type;
2921 result_type = arg -> result;
2923 if(result_type -> class != OBERON_TYPE_INTEGER)
2925 oberon_error(ctx, "ABS accepts only integers");
2929 oberon_expr_t * expr;
2930 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
2931 return expr;
2934 static void
2935 oberon_make_new_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
2937 if(num_args < 1)
2939 oberon_error(ctx, "too few arguments");
2942 oberon_expr_t * dst;
2943 dst = list_args;
2945 oberon_type_t * type;
2946 type = dst -> result;
2948 if(type -> class != OBERON_TYPE_POINTER)
2950 oberon_error(ctx, "not a pointer");
2953 type = type -> base;
2955 oberon_expr_t * src;
2956 src = oberon_new_item(MODE_NEW, dst -> result, 0);
2957 src -> item.num_args = 0;
2958 src -> item.args = NULL;
2960 int max_args = 1;
2961 if(type -> class == OBERON_TYPE_ARRAY)
2963 if(type -> size == 0)
2965 oberon_type_t * x = type;
2966 while(x -> class == OBERON_TYPE_ARRAY)
2968 if(x -> size == 0)
2970 max_args += 1;
2972 x = x -> base;
2976 if(num_args < max_args)
2978 oberon_error(ctx, "too few arguments");
2981 if(num_args > max_args)
2983 oberon_error(ctx, "too mach arguments");
2986 int num_sizes = max_args - 1;
2987 oberon_expr_t * size_list = list_args -> next;
2989 oberon_expr_t * arg = size_list;
2990 for(int i = 0; i < max_args - 1; i++)
2992 if(arg -> result -> class != OBERON_TYPE_INTEGER)
2994 oberon_error(ctx, "size must be integer");
2996 arg = arg -> next;
2999 src -> item.num_args = num_sizes;
3000 src -> item.args = size_list;
3002 else if(type -> class != OBERON_TYPE_RECORD)
3004 oberon_error(ctx, "oberon_make_new_call: wat");
3007 if(num_args > max_args)
3009 oberon_error(ctx, "too mach arguments");
3012 oberon_assign(ctx, src, dst);
3015 oberon_context_t *
3016 oberon_create_context(ModuleImportCallback import_module)
3018 oberon_context_t * ctx = calloc(1, sizeof *ctx);
3020 oberon_scope_t * world_scope;
3021 world_scope = oberon_open_scope(ctx);
3022 ctx -> world_scope = world_scope;
3024 ctx -> import_module = import_module;
3026 oberon_generator_init_context(ctx);
3028 register_default_types(ctx);
3029 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
3030 oberon_new_intrinsic(ctx, "NEW", NULL, oberon_make_new_call);
3032 return ctx;
3035 void
3036 oberon_destroy_context(oberon_context_t * ctx)
3038 oberon_generator_destroy_context(ctx);
3039 free(ctx);
3042 oberon_module_t *
3043 oberon_compile_module(oberon_context_t * ctx, const char * newcode)
3045 const char * code = ctx -> code;
3046 int code_index = ctx -> code_index;
3047 char c = ctx -> c;
3048 int token = ctx -> token;
3049 char * string = ctx -> string;
3050 int integer = ctx -> integer;
3051 int real = ctx -> real;
3052 bool longmode = ctx -> longmode;
3053 oberon_scope_t * decl = ctx -> decl;
3054 oberon_module_t * mod = ctx -> mod;
3056 oberon_scope_t * module_scope;
3057 module_scope = oberon_open_scope(ctx);
3059 oberon_module_t * module;
3060 module = calloc(1, sizeof *module);
3061 module -> decl = module_scope;
3062 module -> next = ctx -> module_list;
3064 ctx -> mod = module;
3065 ctx -> module_list = module;
3067 oberon_init_scaner(ctx, newcode);
3068 oberon_parse_module(ctx);
3070 module -> ready = 1;
3072 ctx -> code = code;
3073 ctx -> code_index = code_index;
3074 ctx -> c = c;
3075 ctx -> token = token;
3076 ctx -> string = string;
3077 ctx -> integer = integer;
3078 ctx -> real = real;
3079 ctx -> longmode = longmode;
3080 ctx -> decl = decl;
3081 ctx -> mod = mod;
3083 return module;