DEADSOFTWARE

Поправлены повторные опережающие объявления процедур, добавлено именование генерируем...
[dsw-obn.git] / 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>
8 #include "oberon.h"
9 #include "generator.h"
11 enum {
12 EOF_ = 0,
13 IDENT,
14 MODULE,
15 SEMICOLON,
16 END,
17 DOT,
18 VAR,
19 COLON,
20 BEGIN,
21 ASSIGN,
22 INTEGER,
23 TRUE,
24 FALSE,
25 LPAREN,
26 RPAREN,
27 EQUAL,
28 NEQ,
29 LESS,
30 LEQ,
31 GREAT,
32 GEQ,
33 PLUS,
34 MINUS,
35 OR,
36 STAR,
37 SLASH,
38 DIV,
39 MOD,
40 AND,
41 NOT,
42 PROCEDURE,
43 COMMA,
44 RETURN,
45 CONST,
46 TYPE,
47 ARRAY,
48 OF,
49 LBRACE,
50 RBRACE,
51 RECORD,
52 POINTER,
53 TO,
54 UPARROW,
55 NIL
56 };
58 // =======================================================================
59 // UTILS
60 // =======================================================================
62 void
63 oberon_error(oberon_context_t * ctx, const char * fmt, ...)
64 {
65 va_list ptr;
66 va_start(ptr, fmt);
67 fprintf(stderr, "error: ");
68 vfprintf(stderr, fmt, ptr);
69 fprintf(stderr, "\n");
70 fprintf(stderr, " code_index = %i\n", ctx -> code_index);
71 fprintf(stderr, " c = %c\n", ctx -> c);
72 fprintf(stderr, " token = %i\n", ctx -> token);
73 va_end(ptr);
74 exit(1);
75 }
77 static oberon_type_t *
78 oberon_new_type_ptr(int class)
79 {
80 oberon_type_t * x = malloc(sizeof *x);
81 memset(x, 0, sizeof *x);
82 x -> class = class;
83 return x;
84 }
86 static oberon_type_t *
87 oberon_new_type_integer(int size)
88 {
89 oberon_type_t * x;
90 x = oberon_new_type_ptr(OBERON_TYPE_INTEGER);
91 x -> size = size;
92 return x;
93 }
95 static oberon_type_t *
96 oberon_new_type_boolean(int size)
97 {
98 oberon_type_t * x;
99 x = oberon_new_type_ptr(OBERON_TYPE_BOOLEAN);
100 x -> size = size;
101 return x;
104 // =======================================================================
105 // TABLE
106 // =======================================================================
108 static oberon_scope_t *
109 oberon_open_scope(oberon_context_t * ctx)
111 oberon_scope_t * scope = malloc(sizeof *scope);
112 memset(scope, 0, sizeof *scope);
114 oberon_object_t * list = malloc(sizeof *list);
115 memset(list, 0, sizeof *list);
117 scope -> ctx = ctx;
118 scope -> list = list;
119 scope -> up = ctx -> decl;
121 if(scope -> up)
123 scope -> parent = scope -> up -> parent;
124 scope -> local = scope -> up -> local;
127 ctx -> decl = scope;
128 return scope;
131 static void
132 oberon_close_scope(oberon_scope_t * scope)
134 oberon_context_t * ctx = scope -> ctx;
135 ctx -> decl = scope -> up;
138 static oberon_object_t *
139 oberon_define_object(oberon_scope_t * scope, char * name, int class)
141 oberon_object_t * x = scope -> list;
142 while(x -> next && strcmp(x -> next -> name, name) != 0)
144 x = x -> next;
147 if(x -> next)
149 oberon_error(scope -> ctx, "already defined");
152 oberon_object_t * newvar = malloc(sizeof *newvar);
153 memset(newvar, 0, sizeof *newvar);
154 newvar -> name = name;
155 newvar -> class = class;
156 newvar -> local = scope -> local;
157 newvar -> parent = scope -> parent;
159 x -> next = newvar;
161 return newvar;
164 static void
165 oberon_define_field(oberon_context_t * ctx, oberon_type_t * rec, char * name, oberon_type_t * type)
167 // TODO check base fields
169 oberon_object_t * x = rec -> decl;
170 while(x -> next && strcmp(x -> next -> name, name) != 0)
172 x = x -> next;
175 if(x -> next)
177 oberon_error(ctx, "multiple definition");
180 oberon_object_t * field = malloc(sizeof *field);
181 memset(field, 0, sizeof *field);
182 field -> name = name;
183 field -> class = OBERON_CLASS_FIELD;
184 field -> type = type;
185 field -> local = 1;
186 field -> parent = NULL;
188 rec -> num_decl += 1;
189 x -> next = field;
192 static oberon_object_t *
193 oberon_find_object_in_list(oberon_object_t * list, char * name)
195 oberon_object_t * x = list;
196 while(x -> next && strcmp(x -> next -> name, name) != 0)
198 x = x -> next;
200 return x -> next;
203 static oberon_object_t *
204 oberon_find_object(oberon_scope_t * scope, char * name, int check_it)
206 oberon_object_t * result = NULL;
208 oberon_scope_t * s = scope;
209 while(result == NULL && s != NULL)
211 result = oberon_find_object_in_list(s -> list, name);
212 s = s -> up;
215 if(check_it && result == NULL)
217 oberon_error(scope -> ctx, "undefined ident %s", name);
220 return result;
223 static oberon_object_t *
224 oberon_find_field(oberon_context_t * ctx, oberon_type_t * rec, char * name)
226 oberon_object_t * x = rec -> decl;
227 for(int i = 0; i < rec -> num_decl; i++)
229 if(strcmp(x -> name, name) == 0)
231 return x;
233 x = x -> next;
236 oberon_error(ctx, "field not defined");
238 return NULL;
241 static oberon_object_t *
242 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type)
244 oberon_object_t * id;
245 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE);
246 id -> type = type;
247 oberon_generator_init_type(scope -> ctx, type);
248 return id;
251 /*
252 static oberon_type_t *
253 oberon_find_type(oberon_scope_t * scope, char * name)
255 oberon_object_t * x = oberon_find_object(scope, name);
256 if(x -> class != OBERON_CLASS_TYPE)
258 oberon_error(scope -> ctx, "%s not a type", name);
261 return x -> type;
263 */
265 static oberon_object_t *
266 oberon_define_var(oberon_scope_t * scope, int class, char * name, oberon_type_t * type)
268 oberon_object_t * var;
269 var = oberon_define_object(scope, name, class);
270 var -> type = type;
271 return var;
274 /*
275 static oberon_object_t *
276 oberon_find_var(oberon_scope_t * scope, char * name)
278 oberon_object_t * x = oberon_find_object(scope, name);
280 if(x -> class != OBERON_CLASS_VAR)
282 oberon_error(scope -> ctx, "%s not a var", name);
285 return x;
287 */
289 /*
290 static oberon_object_t *
291 oberon_define_proc(oberon_scope_t * scope, char * name, oberon_type_t * signature)
293 oberon_object_t * proc;
294 proc = oberon_define_object(scope, name, OBERON_CLASS_PROC);
295 proc -> type = signature;
296 return proc;
298 */
300 // =======================================================================
301 // SCANER
302 // =======================================================================
304 static void
305 oberon_get_char(oberon_context_t * ctx)
307 ctx -> code_index += 1;
308 ctx -> c = ctx -> code[ctx -> code_index];
311 static void
312 oberon_init_scaner(oberon_context_t * ctx, const char * code)
314 ctx -> code = code;
315 ctx -> code_index = 0;
316 ctx -> c = ctx -> code[ctx -> code_index];
319 static void
320 oberon_read_ident(oberon_context_t * ctx)
322 int len = 0;
323 int i = ctx -> code_index;
325 int c = ctx -> code[i];
326 while(isalnum(c))
328 i += 1;
329 len += 1;
330 c = ctx -> code[i];
333 char * ident = malloc(len + 1);
334 memcpy(ident, &ctx->code[ctx->code_index], len);
335 ident[len] = 0;
337 ctx -> code_index = i;
338 ctx -> c = ctx -> code[i];
339 ctx -> string = ident;
340 ctx -> token = IDENT;
342 if(strcmp(ident, "MODULE") == 0)
344 ctx -> token = MODULE;
346 else if(strcmp(ident, "END") == 0)
348 ctx -> token = END;
350 else if(strcmp(ident, "VAR") == 0)
352 ctx -> token = VAR;
354 else if(strcmp(ident, "BEGIN") == 0)
356 ctx -> token = BEGIN;
358 else if(strcmp(ident, "TRUE") == 0)
360 ctx -> token = TRUE;
362 else if(strcmp(ident, "FALSE") == 0)
364 ctx -> token = FALSE;
366 else if(strcmp(ident, "OR") == 0)
368 ctx -> token = OR;
370 else if(strcmp(ident, "DIV") == 0)
372 ctx -> token = DIV;
374 else if(strcmp(ident, "MOD") == 0)
376 ctx -> token = MOD;
378 else if(strcmp(ident, "PROCEDURE") == 0)
380 ctx -> token = PROCEDURE;
382 else if(strcmp(ident, "RETURN") == 0)
384 ctx -> token = RETURN;
386 else if(strcmp(ident, "CONST") == 0)
388 ctx -> token = CONST;
390 else if(strcmp(ident, "TYPE") == 0)
392 ctx -> token = TYPE;
394 else if(strcmp(ident, "ARRAY") == 0)
396 ctx -> token = ARRAY;
398 else if(strcmp(ident, "OF") == 0)
400 ctx -> token = OF;
402 else if(strcmp(ident, "RECORD") == 0)
404 ctx -> token = RECORD;
406 else if(strcmp(ident, "POINTER") == 0)
408 ctx -> token = POINTER;
410 else if(strcmp(ident, "TO") == 0)
412 ctx -> token = TO;
414 else if(strcmp(ident, "NIL") == 0)
416 ctx -> token = NIL;
420 static void
421 oberon_read_integer(oberon_context_t * ctx)
423 int len = 0;
424 int i = ctx -> code_index;
426 int c = ctx -> code[i];
427 while(isdigit(c))
429 i += 1;
430 len += 1;
431 c = ctx -> code[i];
434 char * ident = malloc(len + 2);
435 memcpy(ident, &ctx->code[ctx->code_index], len);
436 ident[len + 1] = 0;
438 ctx -> code_index = i;
439 ctx -> c = ctx -> code[i];
440 ctx -> string = ident;
441 ctx -> integer = atoi(ident);
442 ctx -> token = INTEGER;
445 static void
446 oberon_skip_space(oberon_context_t * ctx)
448 while(isspace(ctx -> c))
450 oberon_get_char(ctx);
454 static void
455 oberon_read_symbol(oberon_context_t * ctx)
457 int c = ctx -> c;
458 switch(c)
460 case 0:
461 ctx -> token = EOF_;
462 break;
463 case ';':
464 ctx -> token = SEMICOLON;
465 oberon_get_char(ctx);
466 break;
467 case ':':
468 ctx -> token = COLON;
469 oberon_get_char(ctx);
470 if(ctx -> c == '=')
472 ctx -> token = ASSIGN;
473 oberon_get_char(ctx);
475 break;
476 case '.':
477 ctx -> token = DOT;
478 oberon_get_char(ctx);
479 break;
480 case '(':
481 ctx -> token = LPAREN;
482 oberon_get_char(ctx);
483 break;
484 case ')':
485 ctx -> token = RPAREN;
486 oberon_get_char(ctx);
487 break;
488 case '=':
489 ctx -> token = EQUAL;
490 oberon_get_char(ctx);
491 break;
492 case '#':
493 ctx -> token = NEQ;
494 oberon_get_char(ctx);
495 break;
496 case '<':
497 ctx -> token = LESS;
498 oberon_get_char(ctx);
499 if(ctx -> c == '=')
501 ctx -> token = LEQ;
502 oberon_get_char(ctx);
504 break;
505 case '>':
506 ctx -> token = GREAT;
507 oberon_get_char(ctx);
508 if(ctx -> c == '=')
510 ctx -> token = GEQ;
511 oberon_get_char(ctx);
513 break;
514 case '+':
515 ctx -> token = PLUS;
516 oberon_get_char(ctx);
517 break;
518 case '-':
519 ctx -> token = MINUS;
520 oberon_get_char(ctx);
521 break;
522 case '*':
523 ctx -> token = STAR;
524 oberon_get_char(ctx);
525 break;
526 case '/':
527 ctx -> token = SLASH;
528 oberon_get_char(ctx);
529 break;
530 case '&':
531 ctx -> token = AND;
532 oberon_get_char(ctx);
533 break;
534 case '~':
535 ctx -> token = NOT;
536 oberon_get_char(ctx);
537 break;
538 case ',':
539 ctx -> token = COMMA;
540 oberon_get_char(ctx);
541 break;
542 case '[':
543 ctx -> token = LBRACE;
544 oberon_get_char(ctx);
545 break;
546 case ']':
547 ctx -> token = RBRACE;
548 oberon_get_char(ctx);
549 break;
550 case '^':
551 ctx -> token = UPARROW;
552 oberon_get_char(ctx);
553 break;
554 default:
555 oberon_error(ctx, "invalid char");
556 break;
560 static void
561 oberon_read_token(oberon_context_t * ctx)
563 oberon_skip_space(ctx);
565 int c = ctx -> c;
566 if(isalpha(c))
568 oberon_read_ident(ctx);
570 else if(isdigit(c))
572 oberon_read_integer(ctx);
574 else
576 oberon_read_symbol(ctx);
580 // =======================================================================
581 // EXPRESSION
582 // =======================================================================
584 static void oberon_expect_token(oberon_context_t * ctx, int token);
585 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
586 static void oberon_assert_token(oberon_context_t * ctx, int token);
587 static char * oberon_assert_ident(oberon_context_t * ctx);
588 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
589 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
591 static oberon_expr_t *
592 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
594 oberon_oper_t * operator;
595 operator = malloc(sizeof *operator);
596 memset(operator, 0, sizeof *operator);
598 operator -> is_item = 0;
599 operator -> result = result;
600 operator -> op = op;
601 operator -> left = left;
602 operator -> right = right;
604 return (oberon_expr_t *) operator;
607 static oberon_expr_t *
608 oberon_new_item(int mode, oberon_type_t * result)
610 oberon_item_t * item;
611 item = malloc(sizeof *item);
612 memset(item, 0, sizeof *item);
614 item -> is_item = 1;
615 item -> result = result;
616 item -> mode = mode;
618 return (oberon_expr_t *)item;
621 static oberon_expr_t *
622 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
624 oberon_expr_t * expr;
625 oberon_type_t * result;
627 result = a -> result;
629 if(token == MINUS)
631 if(result -> class != OBERON_TYPE_INTEGER)
633 oberon_error(ctx, "incompatible operator type");
636 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
638 else if(token == NOT)
640 if(result -> class != OBERON_TYPE_BOOLEAN)
642 oberon_error(ctx, "incompatible operator type");
645 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
647 else
649 oberon_error(ctx, "oberon_make_unary_op: wat");
652 return expr;
655 static void
656 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
658 oberon_expr_t * last;
660 *num_expr = 1;
661 *first = last = oberon_expr(ctx);
662 while(ctx -> token == COMMA)
664 oberon_assert_token(ctx, COMMA);
665 oberon_expr_t * current;
667 if(const_expr)
669 current = (oberon_expr_t *) oberon_const_expr(ctx);
671 else
673 current = oberon_expr(ctx);
676 last -> next = current;
677 last = current;
678 *num_expr += 1;
682 static oberon_expr_t *
683 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
685 if(pref -> class != expr -> result -> class)
687 oberon_error(ctx, "incompatible types");
690 if(pref -> class == OBERON_TYPE_INTEGER)
692 if(expr -> result -> class > pref -> class)
694 oberon_error(ctx, "incompatible size");
697 else if(pref -> class == OBERON_TYPE_RECORD)
699 if(expr -> result != pref)
701 printf("oberon_autocast_to: rec %p != %p\n", expr -> result, pref);
702 oberon_error(ctx, "incompatible record types");
705 else if(pref -> class == OBERON_TYPE_POINTER)
707 if(expr -> result -> base != pref -> base)
709 if(expr -> result -> base -> class != OBERON_TYPE_VOID)
711 oberon_error(ctx, "incompatible pointer types");
716 // TODO cast
718 return expr;
721 static void
722 oberon_autocast_call(oberon_context_t * ctx, oberon_expr_t * desig)
724 if(desig -> is_item == 0)
726 oberon_error(ctx, "expected item");
729 if(desig -> item.mode != MODE_CALL)
731 oberon_error(ctx, "expected mode CALL");
734 if(desig -> item.var -> class != OBERON_CLASS_PROC)
736 oberon_error(ctx, "only procedures can be called");
739 oberon_type_t * fn = desig -> item.var -> type;
740 int num_args = desig -> item.num_args;
741 int num_decl = fn -> num_decl;
743 if(num_args < num_decl)
745 oberon_error(ctx, "too few arguments");
747 else if(num_args > num_decl)
749 oberon_error(ctx, "too many arguments");
752 oberon_expr_t * arg = desig -> item.args;
753 oberon_object_t * param = fn -> decl;
754 for(int i = 0; i < num_args; i++)
756 if(param -> class == OBERON_CLASS_VAR_PARAM)
758 if(arg -> is_item)
760 switch(arg -> item.mode)
762 case MODE_VAR:
763 case MODE_INDEX:
764 case MODE_FIELD:
765 // Допустимо разыменование?
766 //case MODE_DEREF:
767 break;
768 default:
769 oberon_error(ctx, "var-parameter accept only variables");
770 break;
774 oberon_autocast_to(ctx, arg, param -> type);
775 arg = arg -> next;
776 param = param -> next;
780 #define ISEXPR(x) \
781 (((x) == PLUS) \
782 || ((x) == MINUS) \
783 || ((x) == IDENT) \
784 || ((x) == INTEGER) \
785 || ((x) == LPAREN) \
786 || ((x) == NOT) \
787 || ((x) == TRUE) \
788 || ((x) == FALSE))
790 static oberon_expr_t *
791 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
793 if(expr -> result -> class != OBERON_TYPE_POINTER)
795 oberon_error(ctx, "not a pointer");
798 assert(expr -> is_item);
800 oberon_expr_t * selector;
801 selector = oberon_new_item(MODE_DEREF, expr -> result -> base);
802 selector -> item.parent = (oberon_item_t *) expr;
804 return selector;
807 static oberon_expr_t *
808 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
810 if(desig -> result -> class == OBERON_TYPE_POINTER)
812 desig = oberno_make_dereferencing(ctx, desig);
815 assert(desig -> is_item);
817 if(desig -> result -> class != OBERON_TYPE_ARRAY)
819 oberon_error(ctx, "not array");
822 oberon_type_t * base;
823 base = desig -> result -> base;
825 if(index -> result -> class != OBERON_TYPE_INTEGER)
827 oberon_error(ctx, "index must be integer");
830 // Статическая проверка границ массива
831 if(index -> is_item)
833 if(index -> item.mode == MODE_INTEGER)
835 int arr_size = desig -> result -> size;
836 int index_int = index -> item.integer;
837 if(index_int < 0 || index_int > arr_size - 1)
839 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
844 oberon_expr_t * selector;
845 selector = oberon_new_item(MODE_INDEX, base);
846 selector -> item.parent = (oberon_item_t *) desig;
847 selector -> item.num_args = 1;
848 selector -> item.args = index;
850 return selector;
853 static oberon_expr_t *
854 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
856 if(expr -> result -> class == OBERON_TYPE_POINTER)
858 expr = oberno_make_dereferencing(ctx, expr);
861 assert(expr -> is_item == 1);
863 if(expr -> result -> class != OBERON_TYPE_RECORD)
865 oberon_error(ctx, "not record");
868 oberon_type_t * rec = expr -> result;
870 oberon_object_t * field;
871 field = oberon_find_field(ctx, rec, name);
873 oberon_expr_t * selector;
874 selector = oberon_new_item(MODE_FIELD, field -> type);
875 selector -> item.var = field;
876 selector -> item.parent = (oberon_item_t *) expr;
878 return selector;
881 #define ISSELECTOR(x) \
882 (((x) == LBRACE) \
883 || ((x) == DOT) \
884 || ((x) == UPARROW))
886 static oberon_expr_t *
887 oberon_designator(oberon_context_t * ctx)
889 char * name;
890 oberon_object_t * var;
891 oberon_expr_t * expr;
893 name = oberon_assert_ident(ctx);
894 var = oberon_find_object(ctx -> decl, name, 1);
896 switch(var -> class)
898 case OBERON_CLASS_CONST:
899 // TODO copy value
900 expr = (oberon_expr_t *) var -> value;
901 break;
902 case OBERON_CLASS_VAR:
903 case OBERON_CLASS_VAR_PARAM:
904 case OBERON_CLASS_PARAM:
905 expr = oberon_new_item(MODE_VAR, var -> type);
906 break;
907 case OBERON_CLASS_PROC:
908 expr = oberon_new_item(MODE_CALL, var -> type);
909 break;
910 default:
911 oberon_error(ctx, "invalid designator");
912 break;
914 expr -> item.var = var;
916 while(ISSELECTOR(ctx -> token))
918 switch(ctx -> token)
920 case DOT:
921 oberon_assert_token(ctx, DOT);
922 name = oberon_assert_ident(ctx);
923 expr = oberon_make_record_selector(ctx, expr, name);
924 break;
925 case LBRACE:
926 oberon_assert_token(ctx, LBRACE);
927 int num_indexes = 0;
928 oberon_expr_t * indexes = NULL;
929 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
930 oberon_assert_token(ctx, RBRACE);
932 for(int i = 0; i < num_indexes; i++)
934 expr = oberon_make_array_selector(ctx, expr, indexes);
935 indexes = indexes -> next;
937 break;
938 case UPARROW:
939 oberon_assert_token(ctx, UPARROW);
940 expr = oberno_make_dereferencing(ctx, expr);
941 break;
942 default:
943 oberon_error(ctx, "oberon_designator: wat");
944 break;
947 return expr;
950 static oberon_expr_t *
951 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
953 assert(expr -> is_item == 1);
955 if(ctx -> token == LPAREN)
957 if(expr -> result -> class != OBERON_TYPE_PROCEDURE)
959 oberon_error(ctx, "not a procedure");
962 oberon_assert_token(ctx, LPAREN);
964 int num_args = 0;
965 oberon_expr_t * arguments = NULL;
967 if(ISEXPR(ctx -> token))
969 oberon_expr_list(ctx, &num_args, &arguments, 0);
972 expr -> result = expr -> item.var -> type -> base;
973 expr -> item.mode = MODE_CALL;
974 expr -> item.num_args = num_args;
975 expr -> item.args = arguments;
976 oberon_assert_token(ctx, RPAREN);
978 oberon_autocast_call(ctx, expr);
981 return expr;
984 static oberon_expr_t *
985 oberon_factor(oberon_context_t * ctx)
987 oberon_expr_t * expr;
989 switch(ctx -> token)
991 case IDENT:
992 expr = oberon_designator(ctx);
993 expr = oberon_opt_proc_parens(ctx, expr);
994 break;
995 case INTEGER:
996 expr = oberon_new_item(MODE_INTEGER, ctx -> int_type);
997 expr -> item.integer = ctx -> integer;
998 oberon_assert_token(ctx, INTEGER);
999 break;
1000 case TRUE:
1001 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type);
1002 expr -> item.boolean = 1;
1003 oberon_assert_token(ctx, TRUE);
1004 break;
1005 case FALSE:
1006 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type);
1007 expr -> item.boolean = 0;
1008 oberon_assert_token(ctx, FALSE);
1009 break;
1010 case LPAREN:
1011 oberon_assert_token(ctx, LPAREN);
1012 expr = oberon_expr(ctx);
1013 oberon_assert_token(ctx, RPAREN);
1014 break;
1015 case NOT:
1016 oberon_assert_token(ctx, NOT);
1017 expr = oberon_factor(ctx);
1018 expr = oberon_make_unary_op(ctx, NOT, expr);
1019 break;
1020 case NIL:
1021 oberon_assert_token(ctx, NIL);
1022 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type);
1023 break;
1024 default:
1025 oberon_error(ctx, "invalid expression");
1028 return expr;
1031 /*
1032 * oberon_autocast_binary_op автоматически переобразовывеат тип по след. правилам:
1033 * 1. Классы обоих типов должны быть одинаковы
1034 * 2. В качестве результата должен быть выбран больший тип.
1035 * 3. Если размер результат не должен быть меньше чем базовый int
1036 */
1038 static void
1039 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b, oberon_type_t ** result)
1041 if((a -> class) != (b -> class))
1043 oberon_error(ctx, "incompatible types");
1046 if((a -> size) > (b -> size))
1048 *result = a;
1050 else
1052 *result = b;
1055 if(((*result) -> class) == OBERON_TYPE_INTEGER)
1057 if(((*result) -> size) < (ctx -> int_type -> size))
1059 *result = ctx -> int_type;
1063 /* TODO: cast types */
1066 #define ITMAKESBOOLEAN(x) \
1067 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1069 #define ITUSEONLYINTEGER(x) \
1070 ((x) >= LESS && (x) <= GEQ)
1072 #define ITUSEONLYBOOLEAN(x) \
1073 (((x) == OR) || ((x) == AND))
1075 static oberon_expr_t *
1076 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1078 oberon_expr_t * expr;
1079 oberon_type_t * result;
1081 if(ITMAKESBOOLEAN(token))
1083 if(ITUSEONLYINTEGER(token))
1085 if(a -> result -> class != OBERON_TYPE_INTEGER
1086 || b -> result -> class != OBERON_TYPE_INTEGER)
1088 oberon_error(ctx, "used only with integer types");
1091 else if(ITUSEONLYBOOLEAN(token))
1093 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1094 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1096 oberon_error(ctx, "used only with boolean type");
1100 result = ctx -> bool_type;
1102 if(token == EQUAL)
1104 expr = oberon_new_operator(OP_EQ, result, a, b);
1106 else if(token == NEQ)
1108 expr = oberon_new_operator(OP_NEQ, result, a, b);
1110 else if(token == LESS)
1112 expr = oberon_new_operator(OP_LSS, result, a, b);
1114 else if(token == LEQ)
1116 expr = oberon_new_operator(OP_LEQ, result, a, b);
1118 else if(token == GREAT)
1120 expr = oberon_new_operator(OP_GRT, result, a, b);
1122 else if(token == GEQ)
1124 expr = oberon_new_operator(OP_GEQ, result, a, b);
1126 else if(token == OR)
1128 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1130 else if(token == AND)
1132 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1134 else
1136 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1139 else
1141 oberon_autocast_binary_op(ctx, a -> result, b -> result, &result);
1143 if(token == PLUS)
1145 expr = oberon_new_operator(OP_ADD, result, a, b);
1147 else if(token == MINUS)
1149 expr = oberon_new_operator(OP_SUB, result, a, b);
1151 else if(token == STAR)
1153 expr = oberon_new_operator(OP_MUL, result, a, b);
1155 else if(token == SLASH)
1157 expr = oberon_new_operator(OP_DIV, result, a, b);
1159 else if(token == DIV)
1161 expr = oberon_new_operator(OP_DIV, result, a, b);
1163 else if(token == MOD)
1165 expr = oberon_new_operator(OP_MOD, result, a, b);
1167 else
1169 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1173 return expr;
1176 #define ISMULOP(x) \
1177 ((x) >= STAR && (x) <= AND)
1179 static oberon_expr_t *
1180 oberon_term_expr(oberon_context_t * ctx)
1182 oberon_expr_t * expr;
1184 expr = oberon_factor(ctx);
1185 while(ISMULOP(ctx -> token))
1187 int token = ctx -> token;
1188 oberon_read_token(ctx);
1190 oberon_expr_t * inter = oberon_factor(ctx);
1191 expr = oberon_make_bin_op(ctx, token, expr, inter);
1194 return expr;
1197 #define ISADDOP(x) \
1198 ((x) >= PLUS && (x) <= OR)
1200 static oberon_expr_t *
1201 oberon_simple_expr(oberon_context_t * ctx)
1203 oberon_expr_t * expr;
1205 int minus = 0;
1206 if(ctx -> token == PLUS)
1208 minus = 0;
1209 oberon_assert_token(ctx, PLUS);
1211 else if(ctx -> token == MINUS)
1213 minus = 1;
1214 oberon_assert_token(ctx, MINUS);
1217 expr = oberon_term_expr(ctx);
1218 while(ISADDOP(ctx -> token))
1220 int token = ctx -> token;
1221 oberon_read_token(ctx);
1223 oberon_expr_t * inter = oberon_term_expr(ctx);
1224 expr = oberon_make_bin_op(ctx, token, expr, inter);
1227 if(minus)
1229 expr = oberon_make_unary_op(ctx, MINUS, expr);
1232 return expr;
1235 #define ISRELATION(x) \
1236 ((x) >= EQUAL && (x) <= GEQ)
1238 static oberon_expr_t *
1239 oberon_expr(oberon_context_t * ctx)
1241 oberon_expr_t * expr;
1243 expr = oberon_simple_expr(ctx);
1244 while(ISRELATION(ctx -> token))
1246 int token = ctx -> token;
1247 oberon_read_token(ctx);
1249 oberon_expr_t * inter = oberon_simple_expr(ctx);
1250 expr = oberon_make_bin_op(ctx, token, expr, inter);
1253 return expr;
1256 static oberon_item_t *
1257 oberon_const_expr(oberon_context_t * ctx)
1259 oberon_expr_t * expr;
1260 expr = oberon_expr(ctx);
1262 if(expr -> is_item == 0)
1264 oberon_error(ctx, "const expression are required");
1267 return (oberon_item_t *) expr;
1270 // =======================================================================
1271 // PARSER
1272 // =======================================================================
1274 static void oberon_decl_seq(oberon_context_t * ctx);
1275 static void oberon_statement_seq(oberon_context_t * ctx);
1276 static void oberon_initialize_decl(oberon_context_t * ctx);
1278 static void
1279 oberon_expect_token(oberon_context_t * ctx, int token)
1281 if(ctx -> token != token)
1283 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
1287 static void
1288 oberon_assert_token(oberon_context_t * ctx, int token)
1290 oberon_expect_token(ctx, token);
1291 oberon_read_token(ctx);
1294 static char *
1295 oberon_assert_ident(oberon_context_t * ctx)
1297 oberon_expect_token(ctx, IDENT);
1298 char * ident = ctx -> string;
1299 oberon_read_token(ctx);
1300 return ident;
1303 static void
1304 oberon_var_decl(oberon_context_t * ctx)
1306 char * name;
1307 oberon_type_t * type;
1308 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1310 name = oberon_assert_ident(ctx);
1311 oberon_assert_token(ctx, COLON);
1312 oberon_type(ctx, &type);
1313 oberon_define_var(ctx -> decl, OBERON_CLASS_VAR, name, type);
1316 static oberon_object_t *
1317 oberon_make_param(oberon_context_t * ctx, int token, char * name, oberon_type_t * type)
1319 oberon_object_t * param;
1321 if(token == VAR)
1323 param = oberon_define_var(ctx -> decl, OBERON_CLASS_VAR_PARAM, name, type);
1325 else if(token == IDENT)
1327 param = oberon_define_var(ctx -> decl, OBERON_CLASS_PARAM, name, type);
1329 else
1331 oberon_error(ctx, "oberon_make_param: wat");
1334 return param;
1337 static oberon_object_t *
1338 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
1340 int modifer_token = ctx -> token;
1341 if(ctx -> token == VAR)
1343 oberon_read_token(ctx);
1346 char * name;
1347 name = oberon_assert_ident(ctx);
1349 oberon_assert_token(ctx, COLON);
1351 oberon_type_t * type;
1352 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1353 oberon_type(ctx, &type);
1355 oberon_object_t * first;
1356 first = oberon_make_param(ctx, modifer_token, name, type);
1358 *num_decl += 1;
1359 return first;
1362 #define ISFPSECTION \
1363 ((ctx -> token == VAR) || (ctx -> token == IDENT))
1365 static void
1366 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
1368 oberon_assert_token(ctx, LPAREN);
1370 if(ISFPSECTION)
1372 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
1373 while(ctx -> token == SEMICOLON)
1375 oberon_assert_token(ctx, SEMICOLON);
1376 oberon_fp_section(ctx, &signature -> num_decl);
1380 oberon_assert_token(ctx, RPAREN);
1382 if(ctx -> token == COLON)
1384 oberon_assert_token(ctx, COLON);
1385 // TODO get by qualident
1386 oberon_type(ctx, &signature -> base);
1390 static void
1391 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
1393 oberon_type_t * signature;
1394 signature = *type;
1395 signature -> class = OBERON_TYPE_PROCEDURE;
1396 signature -> num_decl = 0;
1397 signature -> base = ctx -> void_type;
1398 signature -> decl = NULL;
1400 if(ctx -> token == LPAREN)
1402 oberon_formal_pars(ctx, signature);
1406 static void
1407 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
1409 if(a -> num_decl != b -> num_decl)
1411 oberon_error(ctx, "number parameters not matched");
1414 int num_param = a -> num_decl;
1415 oberon_object_t * param_a = a -> decl;
1416 oberon_object_t * param_b = b -> decl;
1417 for(int i = 0; i < num_param; i++)
1419 if(strcmp(param_a -> name, param_b -> name) != 0)
1421 oberon_error(ctx, "param %i name not matched", i + 1);
1424 if(param_a -> type != param_b -> type)
1426 oberon_error(ctx, "param %i type not matched", i + 1);
1429 param_a = param_a -> next;
1430 param_b = param_b -> next;
1434 static void
1435 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
1437 oberon_object_t * proc = ctx -> decl -> parent;
1438 oberon_type_t * result_type = proc -> type -> base;
1440 if(result_type -> class == OBERON_TYPE_VOID)
1442 if(expr != NULL)
1444 oberon_error(ctx, "procedure has no result type");
1447 else
1449 if(expr == NULL)
1451 oberon_error(ctx, "procedure requires expression on result");
1454 oberon_autocast_to(ctx, expr, result_type);
1457 proc -> has_return = 1;
1459 oberon_generate_return(ctx, expr);
1462 static void
1463 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
1465 oberon_assert_token(ctx, SEMICOLON);
1467 ctx -> decl = proc -> scope;
1469 oberon_decl_seq(ctx);
1471 oberon_generate_begin_proc(ctx, proc);
1473 if(ctx -> token == BEGIN)
1475 oberon_assert_token(ctx, BEGIN);
1476 oberon_statement_seq(ctx);
1479 oberon_assert_token(ctx, END);
1480 char * name = oberon_assert_ident(ctx);
1481 if(strcmp(name, proc -> name) != 0)
1483 oberon_error(ctx, "procedure name not matched");
1486 if(proc -> type -> base -> class == OBERON_TYPE_VOID
1487 && proc -> has_return == 0)
1489 oberon_make_return(ctx, NULL);
1492 if(proc -> has_return == 0)
1494 oberon_error(ctx, "procedure requires return");
1497 oberon_generate_end_proc(ctx);
1498 oberon_close_scope(ctx -> decl);
1501 static void
1502 oberon_proc_decl(oberon_context_t * ctx)
1504 oberon_assert_token(ctx, PROCEDURE);
1506 int forward = 0;
1507 if(ctx -> token == UPARROW)
1509 oberon_assert_token(ctx, UPARROW);
1510 forward = 1;
1513 char * name;
1514 name = oberon_assert_ident(ctx);
1516 oberon_scope_t * proc_scope;
1517 proc_scope = oberon_open_scope(ctx);
1518 ctx -> decl -> local = 1;
1520 oberon_type_t * signature;
1521 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
1522 oberon_opt_formal_pars(ctx, &signature);
1524 oberon_initialize_decl(ctx);
1525 oberon_generator_init_type(ctx, signature);
1526 oberon_close_scope(ctx -> decl);
1528 oberon_object_t * proc;
1529 proc = oberon_find_object(ctx -> decl, name, 0);
1530 if(proc != NULL)
1532 if(proc -> class != OBERON_CLASS_PROC)
1534 oberon_error(ctx, "mult definition");
1537 if(forward == 0)
1539 if(proc -> linked)
1541 oberon_error(ctx, "mult procedure definition");
1545 oberon_compare_signatures(ctx, proc -> type, signature);
1547 else
1549 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC);
1550 proc -> type = signature;
1551 proc -> scope = proc_scope;
1552 oberon_generator_init_proc(ctx, proc);
1555 proc -> scope -> parent = proc;
1557 if(forward == 0)
1559 proc -> linked = 1;
1560 oberon_proc_decl_body(ctx, proc);
1564 static void
1565 oberon_const_decl(oberon_context_t * ctx)
1567 char * name;
1568 oberon_item_t * value;
1569 oberon_object_t * constant;
1571 name = oberon_assert_ident(ctx);
1572 oberon_assert_token(ctx, EQUAL);
1573 value = oberon_const_expr(ctx);
1575 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST);
1576 constant -> value = value;
1579 static void
1580 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
1582 if(size -> is_item == 0)
1584 oberon_error(ctx, "requires constant");
1587 if(size -> item.mode != MODE_INTEGER)
1589 oberon_error(ctx, "requires integer constant");
1592 oberon_type_t * arr;
1593 arr = *type;
1594 arr -> class = OBERON_TYPE_ARRAY;
1595 arr -> size = size -> item.integer;
1596 arr -> base = base;
1599 static void
1600 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec)
1602 if(ctx -> token == IDENT)
1604 char * name;
1605 oberon_type_t * type;
1606 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1608 name = oberon_assert_ident(ctx);
1609 oberon_assert_token(ctx, COLON);
1610 oberon_type(ctx, &type);
1611 oberon_define_field(ctx, rec, name, type);
1615 static void
1616 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
1618 char * name;
1619 oberon_object_t * to;
1621 name = oberon_assert_ident(ctx);
1622 to = oberon_find_object(ctx -> decl, name, 0);
1624 if(to != NULL)
1626 if(to -> class != OBERON_CLASS_TYPE)
1628 oberon_error(ctx, "not a type");
1631 else
1633 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE);
1634 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1637 *type = to -> type;
1640 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
1642 /*
1643 * Правило граматики "type". Указатель type должен указывать на существующий объект!
1644 */
1646 static void
1647 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
1649 if(sizes == NULL)
1651 *type = base;
1652 return;
1655 oberon_type_t * dim;
1656 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
1658 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
1660 oberon_make_array_type(ctx, sizes, dim, type);
1663 static void
1664 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
1666 if(ctx -> token == IDENT)
1668 oberon_qualident_type(ctx, type);
1670 else if(ctx -> token == ARRAY)
1672 oberon_assert_token(ctx, ARRAY);
1674 int num_sizes = 0;
1675 oberon_expr_t * sizes;
1676 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
1678 oberon_assert_token(ctx, OF);
1680 oberon_type_t * base;
1681 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
1682 oberon_type(ctx, &base);
1684 oberon_make_multiarray(ctx, sizes, base, type);
1686 else if(ctx -> token == RECORD)
1688 oberon_type_t * rec;
1689 rec = *type;
1690 rec -> class = OBERON_TYPE_RECORD;
1691 oberon_object_t * list = malloc(sizeof *list);
1692 memset(list, 0, sizeof *list);
1693 rec -> num_decl = 0;
1694 rec -> base = NULL;
1695 rec -> decl = list;
1697 oberon_assert_token(ctx, RECORD);
1698 oberon_field_list(ctx, rec);
1699 while(ctx -> token == SEMICOLON)
1701 oberon_assert_token(ctx, SEMICOLON);
1702 oberon_field_list(ctx, rec);
1704 oberon_assert_token(ctx, END);
1706 rec -> decl = rec -> decl -> next;
1707 *type = rec;
1709 else if(ctx -> token == POINTER)
1711 oberon_assert_token(ctx, POINTER);
1712 oberon_assert_token(ctx, TO);
1714 oberon_type_t * base;
1715 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
1716 oberon_type(ctx, &base);
1718 oberon_type_t * ptr;
1719 ptr = *type;
1720 ptr -> class = OBERON_TYPE_POINTER;
1721 ptr -> base = base;
1723 else if(ctx -> token == PROCEDURE)
1725 oberon_open_scope(ctx);
1726 oberon_assert_token(ctx, PROCEDURE);
1727 oberon_opt_formal_pars(ctx, type);
1728 oberon_close_scope(ctx -> decl);
1730 else
1732 oberon_error(ctx, "invalid type declaration");
1736 static void
1737 oberon_type_decl(oberon_context_t * ctx)
1739 char * name;
1740 oberon_object_t * newtype;
1741 oberon_type_t * type;
1743 name = oberon_assert_ident(ctx);
1745 newtype = oberon_find_object(ctx -> decl, name, 0);
1746 if(newtype == NULL)
1748 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE);
1749 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1750 assert(newtype -> type);
1752 else
1754 if(newtype -> class != OBERON_CLASS_TYPE)
1756 oberon_error(ctx, "mult definition");
1759 if(newtype -> linked)
1761 oberon_error(ctx, "mult definition - already linked");
1765 oberon_assert_token(ctx, EQUAL);
1767 type = newtype -> type;
1768 oberon_type(ctx, &type);
1770 if(type -> class == OBERON_TYPE_VOID)
1772 oberon_error(ctx, "recursive alias declaration");
1775 newtype -> type = type;
1776 newtype -> linked = 1;
1779 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
1780 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
1782 static void
1783 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
1785 if(type -> class != OBERON_TYPE_POINTER
1786 && type -> class != OBERON_TYPE_ARRAY)
1788 return;
1791 if(type -> recursive)
1793 oberon_error(ctx, "recursive pointer declaration");
1796 if(type -> base -> class == OBERON_TYPE_POINTER)
1798 oberon_error(ctx, "attempt to make pointer to pointer");
1801 type -> recursive = 1;
1803 oberon_prevent_recursive_pointer(ctx, type -> base);
1805 type -> recursive = 0;
1808 static void
1809 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
1811 if(type -> class != OBERON_TYPE_RECORD)
1813 return;
1816 if(type -> recursive)
1818 oberon_error(ctx, "recursive record declaration");
1821 type -> recursive = 1;
1823 int num_fields = type -> num_decl;
1824 oberon_object_t * field = type -> decl;
1825 for(int i = 0; i < num_fields; i++)
1827 oberon_prevent_recursive_object(ctx, field);
1828 field = field -> next;
1831 type -> recursive = 0;
1833 static void
1834 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
1836 if(type -> class != OBERON_TYPE_PROCEDURE)
1838 return;
1841 if(type -> recursive)
1843 oberon_error(ctx, "recursive procedure declaration");
1846 type -> recursive = 1;
1848 int num_fields = type -> num_decl;
1849 oberon_object_t * field = type -> decl;
1850 for(int i = 0; i < num_fields; i++)
1852 oberon_prevent_recursive_object(ctx, field);
1853 field = field -> next;
1856 type -> recursive = 0;
1859 static void
1860 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
1862 if(type -> class != OBERON_TYPE_ARRAY)
1864 return;
1867 if(type -> recursive)
1869 oberon_error(ctx, "recursive array declaration");
1872 type -> recursive = 1;
1874 oberon_prevent_recursive_type(ctx, type -> base);
1876 type -> recursive = 0;
1879 static void
1880 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
1882 if(type -> class == OBERON_TYPE_POINTER)
1884 oberon_prevent_recursive_pointer(ctx, type);
1886 else if(type -> class == OBERON_TYPE_RECORD)
1888 oberon_prevent_recursive_record(ctx, type);
1890 else if(type -> class == OBERON_TYPE_ARRAY)
1892 oberon_prevent_recursive_array(ctx, type);
1894 else if(type -> class == OBERON_TYPE_PROCEDURE)
1896 oberon_prevent_recursive_procedure(ctx, type);
1900 static void
1901 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
1903 switch(x -> class)
1905 case OBERON_CLASS_VAR:
1906 case OBERON_CLASS_TYPE:
1907 case OBERON_CLASS_PARAM:
1908 case OBERON_CLASS_VAR_PARAM:
1909 case OBERON_CLASS_FIELD:
1910 oberon_prevent_recursive_type(ctx, x -> type);
1911 break;
1912 case OBERON_CLASS_CONST:
1913 case OBERON_CLASS_PROC:
1914 break;
1915 default:
1916 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
1917 break;
1921 static void
1922 oberon_prevent_recursive_decl(oberon_context_t * ctx)
1924 oberon_object_t * x = ctx -> decl -> list -> next;
1926 while(x)
1928 oberon_prevent_recursive_object(ctx, x);
1929 x = x -> next;
1933 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
1934 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
1936 static void
1937 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
1939 if(type -> class != OBERON_TYPE_RECORD)
1941 return;
1944 int num_fields = type -> num_decl;
1945 oberon_object_t * field = type -> decl;
1946 for(int i = 0; i < num_fields; i++)
1948 if(field -> type -> class == OBERON_TYPE_POINTER)
1950 oberon_initialize_type(ctx, field -> type);
1953 oberon_initialize_object(ctx, field);
1954 field = field -> next;
1957 oberon_generator_init_record(ctx, type);
1960 static void
1961 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
1963 if(type -> class == OBERON_TYPE_VOID)
1965 oberon_error(ctx, "undeclarated type");
1968 if(type -> initialized)
1970 return;
1973 type -> initialized = 1;
1975 if(type -> class == OBERON_TYPE_POINTER)
1977 oberon_initialize_type(ctx, type -> base);
1978 oberon_generator_init_type(ctx, type);
1980 else if(type -> class == OBERON_TYPE_ARRAY)
1982 oberon_initialize_type(ctx, type -> base);
1983 oberon_generator_init_type(ctx, type);
1985 else if(type -> class == OBERON_TYPE_RECORD)
1987 oberon_generator_init_type(ctx, type);
1988 oberon_initialize_record_fields(ctx, type);
1990 else if(type -> class == OBERON_TYPE_PROCEDURE)
1992 int num_fields = type -> num_decl;
1993 oberon_object_t * field = type -> decl;
1994 for(int i = 0; i < num_fields; i++)
1996 oberon_initialize_object(ctx, field);
1997 field = field -> next;
1998 }
2000 oberon_generator_init_type(ctx, type);
2002 else
2004 oberon_generator_init_type(ctx, type);
2008 static void
2009 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2011 if(x -> initialized)
2013 return;
2016 x -> initialized = 1;
2018 switch(x -> class)
2020 case OBERON_CLASS_TYPE:
2021 oberon_initialize_type(ctx, x -> type);
2022 break;
2023 case OBERON_CLASS_VAR:
2024 case OBERON_CLASS_PARAM:
2025 case OBERON_CLASS_VAR_PARAM:
2026 case OBERON_CLASS_FIELD:
2027 oberon_initialize_type(ctx, x -> type);
2028 oberon_generator_init_var(ctx, x);
2029 break;
2030 case OBERON_CLASS_CONST:
2031 case OBERON_CLASS_PROC:
2032 break;
2033 default:
2034 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2035 break;
2039 static void
2040 oberon_initialize_decl(oberon_context_t * ctx)
2042 oberon_object_t * x = ctx -> decl -> list;
2044 while(x -> next)
2046 oberon_initialize_object(ctx, x -> next);
2047 x = x -> next;
2048 }
2051 static void
2052 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2054 oberon_object_t * x = ctx -> decl -> list;
2056 while(x -> next)
2058 if(x -> next -> class == OBERON_CLASS_PROC)
2060 if(x -> next -> linked == 0)
2062 oberon_error(ctx, "unresolved forward declaration");
2065 x = x -> next;
2066 }
2069 static void
2070 oberon_decl_seq(oberon_context_t * ctx)
2072 if(ctx -> token == CONST)
2074 oberon_assert_token(ctx, CONST);
2075 while(ctx -> token == IDENT)
2077 oberon_const_decl(ctx);
2078 oberon_assert_token(ctx, SEMICOLON);
2082 if(ctx -> token == TYPE)
2084 oberon_assert_token(ctx, TYPE);
2085 while(ctx -> token == IDENT)
2087 oberon_type_decl(ctx);
2088 oberon_assert_token(ctx, SEMICOLON);
2092 if(ctx -> token == VAR)
2094 oberon_assert_token(ctx, VAR);
2095 while(ctx -> token == IDENT)
2097 oberon_var_decl(ctx);
2098 oberon_assert_token(ctx, SEMICOLON);
2102 oberon_prevent_recursive_decl(ctx);
2103 oberon_initialize_decl(ctx);
2105 while(ctx -> token == PROCEDURE)
2107 oberon_proc_decl(ctx);
2108 oberon_assert_token(ctx, SEMICOLON);
2111 oberon_prevent_undeclarated_procedures(ctx);
2114 static void
2115 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
2117 oberon_autocast_to(ctx, src, dst -> result);
2118 oberon_generate_assign(ctx, src, dst);
2121 static void
2122 oberon_make_call(oberon_context_t * ctx, oberon_expr_t * desig)
2124 if(desig -> result -> class != OBERON_TYPE_VOID)
2126 if(desig -> result -> class != OBERON_TYPE_PROCEDURE)
2128 oberon_error(ctx, "procedure with result");
2132 oberon_autocast_call(ctx, desig);
2133 oberon_generate_call_proc(ctx, desig);
2136 static void
2137 oberon_statement(oberon_context_t * ctx)
2139 oberon_expr_t * item1;
2140 oberon_expr_t * item2;
2142 if(ctx -> token == IDENT)
2144 item1 = oberon_designator(ctx);
2145 if(ctx -> token == ASSIGN)
2147 oberon_assert_token(ctx, ASSIGN);
2148 item2 = oberon_expr(ctx);
2149 oberon_assign(ctx, item2, item1);
2151 else
2153 item1 = oberon_opt_proc_parens(ctx, item1);
2154 oberon_make_call(ctx, item1);
2157 else if(ctx -> token == RETURN)
2159 oberon_assert_token(ctx, RETURN);
2160 if(ISEXPR(ctx -> token))
2162 oberon_expr_t * expr;
2163 expr = oberon_expr(ctx);
2164 oberon_make_return(ctx, expr);
2166 else
2168 oberon_make_return(ctx, NULL);
2173 static void
2174 oberon_statement_seq(oberon_context_t * ctx)
2176 oberon_statement(ctx);
2177 while(ctx -> token == SEMICOLON)
2179 oberon_assert_token(ctx, SEMICOLON);
2180 oberon_statement(ctx);
2184 static void
2185 oberon_parse_module(oberon_context_t * ctx)
2187 char *name1, *name2;
2188 oberon_read_token(ctx);
2190 oberon_assert_token(ctx, MODULE);
2191 name1 = oberon_assert_ident(ctx);
2192 oberon_assert_token(ctx, SEMICOLON);
2193 ctx -> mod -> name = name1;
2195 oberon_decl_seq(ctx);
2197 if(ctx -> token == BEGIN)
2199 oberon_assert_token(ctx, BEGIN);
2200 oberon_generate_begin_module(ctx);
2201 oberon_statement_seq(ctx);
2202 oberon_generate_end_module(ctx);
2205 oberon_assert_token(ctx, END);
2206 name2 = oberon_assert_ident(ctx);
2207 oberon_assert_token(ctx, DOT);
2209 if(strcmp(name1, name2) != 0)
2211 oberon_error(ctx, "module name not matched");
2215 // =======================================================================
2216 // LIBRARY
2217 // =======================================================================
2219 static void
2220 register_default_types(oberon_context_t * ctx)
2222 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2223 oberon_generator_init_type(ctx, ctx -> void_type);
2225 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
2226 ctx -> void_ptr_type -> base = ctx -> void_type;
2227 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
2229 ctx -> int_type = oberon_new_type_integer(sizeof(int));
2230 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type);
2232 ctx -> bool_type = oberon_new_type_boolean(sizeof(int));
2233 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type);
2236 oberon_context_t *
2237 oberon_create_context()
2239 oberon_context_t * ctx = malloc(sizeof *ctx);
2240 memset(ctx, 0, sizeof *ctx);
2242 oberon_scope_t * world_scope;
2243 world_scope = oberon_open_scope(ctx);
2244 ctx -> world_scope = world_scope;
2246 oberon_generator_init_context(ctx);
2248 register_default_types(ctx);
2250 return ctx;
2253 void
2254 oberon_destroy_context(oberon_context_t * ctx)
2256 oberon_generator_destroy_context(ctx);
2257 free(ctx);
2260 oberon_module_t *
2261 oberon_compile_module(oberon_context_t * ctx, const char * code)
2263 oberon_module_t * mod = malloc(sizeof *mod);
2264 memset(mod, 0, sizeof *mod);
2265 ctx -> mod = mod;
2267 oberon_scope_t * module_scope;
2268 module_scope = oberon_open_scope(ctx);
2269 mod -> decl = module_scope;
2271 oberon_init_scaner(ctx, code);
2272 oberon_parse_module(ctx);
2274 oberon_generate_code(ctx);
2276 ctx -> mod = NULL;
2277 return mod;