DEADSOFTWARE

28a351a6fdbfd4e315d4b2959b40a3b440a85f1e
[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 -> type -> class != OBERON_TYPE_PROCEDURE)
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 static oberon_expr_t *
781 oberon_make_call_func(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
783 switch(proc -> class)
785 case OBERON_CLASS_PROC:
786 if(proc -> class != OBERON_CLASS_PROC)
788 oberon_error(ctx, "not a procedure");
790 break;
791 case OBERON_CLASS_VAR:
792 case OBERON_CLASS_VAR_PARAM:
793 case OBERON_CLASS_PARAM:
794 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
796 oberon_error(ctx, "not a procedure");
798 break;
799 default:
800 oberon_error(ctx, "not a procedure");
801 break;
804 oberon_expr_t * call;
806 if(proc -> sysproc)
808 if(proc -> genfunc == NULL)
810 oberon_error(ctx, "not a function-procedure");
813 call = proc -> genfunc(ctx, num_args, list_args);
815 else
817 if(proc -> type -> base -> class == OBERON_TYPE_VOID)
819 oberon_error(ctx, "attempt to call procedure in expression");
822 call = oberon_new_item(MODE_CALL, proc -> type -> base);
823 call -> item.var = proc;
824 call -> item.num_args = num_args;
825 call -> item.args = list_args;
826 oberon_autocast_call(ctx, call);
829 return call;
832 static void
833 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
835 switch(proc -> class)
837 case OBERON_CLASS_PROC:
838 if(proc -> class != OBERON_CLASS_PROC)
840 oberon_error(ctx, "not a procedure");
842 break;
843 case OBERON_CLASS_VAR:
844 case OBERON_CLASS_VAR_PARAM:
845 case OBERON_CLASS_PARAM:
846 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
848 oberon_error(ctx, "not a procedure");
850 break;
851 default:
852 oberon_error(ctx, "not a procedure");
853 break;
856 if(proc -> sysproc)
858 if(proc -> genproc == NULL)
860 oberon_error(ctx, "requres non-typed procedure");
863 proc -> genproc(ctx, num_args, list_args);
865 else
867 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
869 oberon_error(ctx, "attempt to call function as non-typed procedure");
872 oberon_expr_t * call;
873 call = oberon_new_item(MODE_CALL, proc -> type -> base);
874 call -> item.var = proc;
875 call -> item.num_args = num_args;
876 call -> item.args = list_args;
877 oberon_autocast_call(ctx, call);
878 oberon_generate_call_proc(ctx, call);
882 #define ISEXPR(x) \
883 (((x) == PLUS) \
884 || ((x) == MINUS) \
885 || ((x) == IDENT) \
886 || ((x) == INTEGER) \
887 || ((x) == LPAREN) \
888 || ((x) == NOT) \
889 || ((x) == TRUE) \
890 || ((x) == FALSE))
892 static oberon_expr_t *
893 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
895 if(expr -> result -> class != OBERON_TYPE_POINTER)
897 oberon_error(ctx, "not a pointer");
900 assert(expr -> is_item);
902 oberon_expr_t * selector;
903 selector = oberon_new_item(MODE_DEREF, expr -> result -> base);
904 selector -> item.parent = (oberon_item_t *) expr;
906 return selector;
909 static oberon_expr_t *
910 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
912 if(desig -> result -> class == OBERON_TYPE_POINTER)
914 desig = oberno_make_dereferencing(ctx, desig);
917 assert(desig -> is_item);
919 if(desig -> result -> class != OBERON_TYPE_ARRAY)
921 oberon_error(ctx, "not array");
924 oberon_type_t * base;
925 base = desig -> result -> base;
927 if(index -> result -> class != OBERON_TYPE_INTEGER)
929 oberon_error(ctx, "index must be integer");
932 // Статическая проверка границ массива
933 if(index -> is_item)
935 if(index -> item.mode == MODE_INTEGER)
937 int arr_size = desig -> result -> size;
938 int index_int = index -> item.integer;
939 if(index_int < 0 || index_int > arr_size - 1)
941 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
946 oberon_expr_t * selector;
947 selector = oberon_new_item(MODE_INDEX, base);
948 selector -> item.parent = (oberon_item_t *) desig;
949 selector -> item.num_args = 1;
950 selector -> item.args = index;
952 return selector;
955 static oberon_expr_t *
956 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
958 if(expr -> result -> class == OBERON_TYPE_POINTER)
960 expr = oberno_make_dereferencing(ctx, expr);
963 assert(expr -> is_item == 1);
965 if(expr -> result -> class != OBERON_TYPE_RECORD)
967 oberon_error(ctx, "not record");
970 oberon_type_t * rec = expr -> result;
972 oberon_object_t * field;
973 field = oberon_find_field(ctx, rec, name);
975 oberon_expr_t * selector;
976 selector = oberon_new_item(MODE_FIELD, field -> type);
977 selector -> item.var = field;
978 selector -> item.parent = (oberon_item_t *) expr;
980 return selector;
983 #define ISSELECTOR(x) \
984 (((x) == LBRACE) \
985 || ((x) == DOT) \
986 || ((x) == UPARROW))
988 static oberon_expr_t *
989 oberon_designator(oberon_context_t * ctx)
991 char * name;
992 oberon_object_t * var;
993 oberon_expr_t * expr;
995 name = oberon_assert_ident(ctx);
996 var = oberon_find_object(ctx -> decl, name, 1);
998 switch(var -> class)
1000 case OBERON_CLASS_CONST:
1001 // TODO copy value
1002 expr = (oberon_expr_t *) var -> value;
1003 break;
1004 case OBERON_CLASS_VAR:
1005 case OBERON_CLASS_VAR_PARAM:
1006 case OBERON_CLASS_PARAM:
1007 case OBERON_CLASS_PROC:
1008 expr = oberon_new_item(MODE_VAR, var -> type);
1009 break;
1010 default:
1011 oberon_error(ctx, "invalid designator");
1012 break;
1014 expr -> item.var = var;
1016 while(ISSELECTOR(ctx -> token))
1018 switch(ctx -> token)
1020 case DOT:
1021 oberon_assert_token(ctx, DOT);
1022 name = oberon_assert_ident(ctx);
1023 expr = oberon_make_record_selector(ctx, expr, name);
1024 break;
1025 case LBRACE:
1026 oberon_assert_token(ctx, LBRACE);
1027 int num_indexes = 0;
1028 oberon_expr_t * indexes = NULL;
1029 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
1030 oberon_assert_token(ctx, RBRACE);
1032 for(int i = 0; i < num_indexes; i++)
1034 expr = oberon_make_array_selector(ctx, expr, indexes);
1035 indexes = indexes -> next;
1037 break;
1038 case UPARROW:
1039 oberon_assert_token(ctx, UPARROW);
1040 expr = oberno_make_dereferencing(ctx, expr);
1041 break;
1042 default:
1043 oberon_error(ctx, "oberon_designator: wat");
1044 break;
1047 return expr;
1050 static oberon_expr_t *
1051 oberon_opt_func_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1053 assert(expr -> is_item == 1);
1055 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1056 if(ctx -> token == LPAREN)
1058 oberon_assert_token(ctx, LPAREN);
1060 int num_args = 0;
1061 oberon_expr_t * arguments = NULL;
1063 if(ISEXPR(ctx -> token))
1065 oberon_expr_list(ctx, &num_args, &arguments, 0);
1068 expr = oberon_make_call_func(ctx, expr -> item.var, num_args, arguments);
1070 oberon_assert_token(ctx, RPAREN);
1073 return expr;
1076 static void
1077 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
1079 assert(expr -> is_item == 1);
1081 int num_args = 0;
1082 oberon_expr_t * arguments = NULL;
1084 if(ctx -> token == LPAREN)
1086 oberon_assert_token(ctx, LPAREN);
1088 if(ISEXPR(ctx -> token))
1090 oberon_expr_list(ctx, &num_args, &arguments, 0);
1093 oberon_assert_token(ctx, RPAREN);
1096 /* Вызов происходит даже без скобок */
1097 oberon_make_call_proc(ctx, expr -> item.var, num_args, arguments);
1100 static oberon_expr_t *
1101 oberon_factor(oberon_context_t * ctx)
1103 oberon_expr_t * expr;
1105 switch(ctx -> token)
1107 case IDENT:
1108 expr = oberon_designator(ctx);
1109 expr = oberon_opt_func_parens(ctx, expr);
1110 break;
1111 case INTEGER:
1112 expr = oberon_new_item(MODE_INTEGER, ctx -> int_type);
1113 expr -> item.integer = ctx -> integer;
1114 oberon_assert_token(ctx, INTEGER);
1115 break;
1116 case TRUE:
1117 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type);
1118 expr -> item.boolean = 1;
1119 oberon_assert_token(ctx, TRUE);
1120 break;
1121 case FALSE:
1122 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type);
1123 expr -> item.boolean = 0;
1124 oberon_assert_token(ctx, FALSE);
1125 break;
1126 case LPAREN:
1127 oberon_assert_token(ctx, LPAREN);
1128 expr = oberon_expr(ctx);
1129 oberon_assert_token(ctx, RPAREN);
1130 break;
1131 case NOT:
1132 oberon_assert_token(ctx, NOT);
1133 expr = oberon_factor(ctx);
1134 expr = oberon_make_unary_op(ctx, NOT, expr);
1135 break;
1136 case NIL:
1137 oberon_assert_token(ctx, NIL);
1138 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type);
1139 break;
1140 default:
1141 oberon_error(ctx, "invalid expression");
1144 return expr;
1147 /*
1148 * oberon_autocast_binary_op автоматически переобразовывеат тип по след. правилам:
1149 * 1. Классы обоих типов должны быть одинаковы
1150 * 2. В качестве результата должен быть выбран больший тип.
1151 * 3. Если размер результат не должен быть меньше чем базовый int
1152 */
1154 static void
1155 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b, oberon_type_t ** result)
1157 if((a -> class) != (b -> class))
1159 oberon_error(ctx, "incompatible types");
1162 if((a -> size) > (b -> size))
1164 *result = a;
1166 else
1168 *result = b;
1171 if(((*result) -> class) == OBERON_TYPE_INTEGER)
1173 if(((*result) -> size) < (ctx -> int_type -> size))
1175 *result = ctx -> int_type;
1179 /* TODO: cast types */
1182 #define ITMAKESBOOLEAN(x) \
1183 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1185 #define ITUSEONLYINTEGER(x) \
1186 ((x) >= LESS && (x) <= GEQ)
1188 #define ITUSEONLYBOOLEAN(x) \
1189 (((x) == OR) || ((x) == AND))
1191 static oberon_expr_t *
1192 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1194 oberon_expr_t * expr;
1195 oberon_type_t * result;
1197 if(ITMAKESBOOLEAN(token))
1199 if(ITUSEONLYINTEGER(token))
1201 if(a -> result -> class != OBERON_TYPE_INTEGER
1202 || b -> result -> class != OBERON_TYPE_INTEGER)
1204 oberon_error(ctx, "used only with integer types");
1207 else if(ITUSEONLYBOOLEAN(token))
1209 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1210 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1212 oberon_error(ctx, "used only with boolean type");
1216 result = ctx -> bool_type;
1218 if(token == EQUAL)
1220 expr = oberon_new_operator(OP_EQ, result, a, b);
1222 else if(token == NEQ)
1224 expr = oberon_new_operator(OP_NEQ, result, a, b);
1226 else if(token == LESS)
1228 expr = oberon_new_operator(OP_LSS, result, a, b);
1230 else if(token == LEQ)
1232 expr = oberon_new_operator(OP_LEQ, result, a, b);
1234 else if(token == GREAT)
1236 expr = oberon_new_operator(OP_GRT, result, a, b);
1238 else if(token == GEQ)
1240 expr = oberon_new_operator(OP_GEQ, result, a, b);
1242 else if(token == OR)
1244 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1246 else if(token == AND)
1248 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1250 else
1252 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1255 else
1257 oberon_autocast_binary_op(ctx, a -> result, b -> result, &result);
1259 if(token == PLUS)
1261 expr = oberon_new_operator(OP_ADD, result, a, b);
1263 else if(token == MINUS)
1265 expr = oberon_new_operator(OP_SUB, result, a, b);
1267 else if(token == STAR)
1269 expr = oberon_new_operator(OP_MUL, result, a, b);
1271 else if(token == SLASH)
1273 expr = oberon_new_operator(OP_DIV, result, a, b);
1275 else if(token == DIV)
1277 expr = oberon_new_operator(OP_DIV, result, a, b);
1279 else if(token == MOD)
1281 expr = oberon_new_operator(OP_MOD, result, a, b);
1283 else
1285 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1289 return expr;
1292 #define ISMULOP(x) \
1293 ((x) >= STAR && (x) <= AND)
1295 static oberon_expr_t *
1296 oberon_term_expr(oberon_context_t * ctx)
1298 oberon_expr_t * expr;
1300 expr = oberon_factor(ctx);
1301 while(ISMULOP(ctx -> token))
1303 int token = ctx -> token;
1304 oberon_read_token(ctx);
1306 oberon_expr_t * inter = oberon_factor(ctx);
1307 expr = oberon_make_bin_op(ctx, token, expr, inter);
1310 return expr;
1313 #define ISADDOP(x) \
1314 ((x) >= PLUS && (x) <= OR)
1316 static oberon_expr_t *
1317 oberon_simple_expr(oberon_context_t * ctx)
1319 oberon_expr_t * expr;
1321 int minus = 0;
1322 if(ctx -> token == PLUS)
1324 minus = 0;
1325 oberon_assert_token(ctx, PLUS);
1327 else if(ctx -> token == MINUS)
1329 minus = 1;
1330 oberon_assert_token(ctx, MINUS);
1333 expr = oberon_term_expr(ctx);
1334 while(ISADDOP(ctx -> token))
1336 int token = ctx -> token;
1337 oberon_read_token(ctx);
1339 oberon_expr_t * inter = oberon_term_expr(ctx);
1340 expr = oberon_make_bin_op(ctx, token, expr, inter);
1343 if(minus)
1345 expr = oberon_make_unary_op(ctx, MINUS, expr);
1348 return expr;
1351 #define ISRELATION(x) \
1352 ((x) >= EQUAL && (x) <= GEQ)
1354 static oberon_expr_t *
1355 oberon_expr(oberon_context_t * ctx)
1357 oberon_expr_t * expr;
1359 expr = oberon_simple_expr(ctx);
1360 while(ISRELATION(ctx -> token))
1362 int token = ctx -> token;
1363 oberon_read_token(ctx);
1365 oberon_expr_t * inter = oberon_simple_expr(ctx);
1366 expr = oberon_make_bin_op(ctx, token, expr, inter);
1369 return expr;
1372 static oberon_item_t *
1373 oberon_const_expr(oberon_context_t * ctx)
1375 oberon_expr_t * expr;
1376 expr = oberon_expr(ctx);
1378 if(expr -> is_item == 0)
1380 oberon_error(ctx, "const expression are required");
1383 return (oberon_item_t *) expr;
1386 // =======================================================================
1387 // PARSER
1388 // =======================================================================
1390 static void oberon_decl_seq(oberon_context_t * ctx);
1391 static void oberon_statement_seq(oberon_context_t * ctx);
1392 static void oberon_initialize_decl(oberon_context_t * ctx);
1394 static void
1395 oberon_expect_token(oberon_context_t * ctx, int token)
1397 if(ctx -> token != token)
1399 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
1403 static void
1404 oberon_assert_token(oberon_context_t * ctx, int token)
1406 oberon_expect_token(ctx, token);
1407 oberon_read_token(ctx);
1410 static char *
1411 oberon_assert_ident(oberon_context_t * ctx)
1413 oberon_expect_token(ctx, IDENT);
1414 char * ident = ctx -> string;
1415 oberon_read_token(ctx);
1416 return ident;
1419 static void
1420 oberon_var_decl(oberon_context_t * ctx)
1422 char * name;
1423 oberon_type_t * type;
1424 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1426 name = oberon_assert_ident(ctx);
1427 oberon_assert_token(ctx, COLON);
1428 oberon_type(ctx, &type);
1429 oberon_define_var(ctx -> decl, OBERON_CLASS_VAR, name, type);
1432 static oberon_object_t *
1433 oberon_make_param(oberon_context_t * ctx, int token, char * name, oberon_type_t * type)
1435 oberon_object_t * param;
1437 if(token == VAR)
1439 param = oberon_define_var(ctx -> decl, OBERON_CLASS_VAR_PARAM, name, type);
1441 else if(token == IDENT)
1443 param = oberon_define_var(ctx -> decl, OBERON_CLASS_PARAM, name, type);
1445 else
1447 oberon_error(ctx, "oberon_make_param: wat");
1450 return param;
1453 static oberon_object_t *
1454 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
1456 int modifer_token = ctx -> token;
1457 if(ctx -> token == VAR)
1459 oberon_read_token(ctx);
1462 char * name;
1463 name = oberon_assert_ident(ctx);
1465 oberon_assert_token(ctx, COLON);
1467 oberon_type_t * type;
1468 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1469 oberon_type(ctx, &type);
1471 oberon_object_t * first;
1472 first = oberon_make_param(ctx, modifer_token, name, type);
1474 *num_decl += 1;
1475 return first;
1478 #define ISFPSECTION \
1479 ((ctx -> token == VAR) || (ctx -> token == IDENT))
1481 static void
1482 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
1484 oberon_assert_token(ctx, LPAREN);
1486 if(ISFPSECTION)
1488 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
1489 while(ctx -> token == SEMICOLON)
1491 oberon_assert_token(ctx, SEMICOLON);
1492 oberon_fp_section(ctx, &signature -> num_decl);
1496 oberon_assert_token(ctx, RPAREN);
1498 if(ctx -> token == COLON)
1500 oberon_assert_token(ctx, COLON);
1501 // TODO get by qualident
1502 oberon_type(ctx, &signature -> base);
1506 static void
1507 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
1509 oberon_type_t * signature;
1510 signature = *type;
1511 signature -> class = OBERON_TYPE_PROCEDURE;
1512 signature -> num_decl = 0;
1513 signature -> base = ctx -> void_type;
1514 signature -> decl = NULL;
1516 if(ctx -> token == LPAREN)
1518 oberon_formal_pars(ctx, signature);
1522 static void
1523 oberon_compare_signatures(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b)
1525 if(a -> num_decl != b -> num_decl)
1527 oberon_error(ctx, "number parameters not matched");
1530 int num_param = a -> num_decl;
1531 oberon_object_t * param_a = a -> decl;
1532 oberon_object_t * param_b = b -> decl;
1533 for(int i = 0; i < num_param; i++)
1535 if(strcmp(param_a -> name, param_b -> name) != 0)
1537 oberon_error(ctx, "param %i name not matched", i + 1);
1540 if(param_a -> type != param_b -> type)
1542 oberon_error(ctx, "param %i type not matched", i + 1);
1545 param_a = param_a -> next;
1546 param_b = param_b -> next;
1550 static void
1551 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
1553 oberon_object_t * proc = ctx -> decl -> parent;
1554 oberon_type_t * result_type = proc -> type -> base;
1556 if(result_type -> class == OBERON_TYPE_VOID)
1558 if(expr != NULL)
1560 oberon_error(ctx, "procedure has no result type");
1563 else
1565 if(expr == NULL)
1567 oberon_error(ctx, "procedure requires expression on result");
1570 oberon_autocast_to(ctx, expr, result_type);
1573 proc -> has_return = 1;
1575 oberon_generate_return(ctx, expr);
1578 static void
1579 oberon_proc_decl_body(oberon_context_t * ctx, oberon_object_t * proc)
1581 oberon_assert_token(ctx, SEMICOLON);
1583 ctx -> decl = proc -> scope;
1585 oberon_decl_seq(ctx);
1587 oberon_generate_begin_proc(ctx, proc);
1589 if(ctx -> token == BEGIN)
1591 oberon_assert_token(ctx, BEGIN);
1592 oberon_statement_seq(ctx);
1595 oberon_assert_token(ctx, END);
1596 char * name = oberon_assert_ident(ctx);
1597 if(strcmp(name, proc -> name) != 0)
1599 oberon_error(ctx, "procedure name not matched");
1602 if(proc -> type -> base -> class == OBERON_TYPE_VOID
1603 && proc -> has_return == 0)
1605 oberon_make_return(ctx, NULL);
1608 if(proc -> has_return == 0)
1610 oberon_error(ctx, "procedure requires return");
1613 oberon_generate_end_proc(ctx);
1614 oberon_close_scope(ctx -> decl);
1617 static void
1618 oberon_proc_decl(oberon_context_t * ctx)
1620 oberon_assert_token(ctx, PROCEDURE);
1622 int forward = 0;
1623 if(ctx -> token == UPARROW)
1625 oberon_assert_token(ctx, UPARROW);
1626 forward = 1;
1629 char * name;
1630 name = oberon_assert_ident(ctx);
1632 oberon_scope_t * proc_scope;
1633 proc_scope = oberon_open_scope(ctx);
1634 ctx -> decl -> local = 1;
1636 oberon_type_t * signature;
1637 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
1638 oberon_opt_formal_pars(ctx, &signature);
1640 oberon_initialize_decl(ctx);
1641 oberon_generator_init_type(ctx, signature);
1642 oberon_close_scope(ctx -> decl);
1644 oberon_object_t * proc;
1645 proc = oberon_find_object(ctx -> decl, name, 0);
1646 if(proc != NULL)
1648 if(proc -> class != OBERON_CLASS_PROC)
1650 oberon_error(ctx, "mult definition");
1653 if(forward == 0)
1655 if(proc -> linked)
1657 oberon_error(ctx, "mult procedure definition");
1661 oberon_compare_signatures(ctx, proc -> type, signature);
1663 else
1665 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC);
1666 proc -> type = signature;
1667 proc -> scope = proc_scope;
1668 oberon_generator_init_proc(ctx, proc);
1671 proc -> scope -> parent = proc;
1673 if(forward == 0)
1675 proc -> linked = 1;
1676 oberon_proc_decl_body(ctx, proc);
1680 static void
1681 oberon_const_decl(oberon_context_t * ctx)
1683 char * name;
1684 oberon_item_t * value;
1685 oberon_object_t * constant;
1687 name = oberon_assert_ident(ctx);
1688 oberon_assert_token(ctx, EQUAL);
1689 value = oberon_const_expr(ctx);
1691 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST);
1692 constant -> value = value;
1695 static void
1696 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
1698 if(size -> is_item == 0)
1700 oberon_error(ctx, "requires constant");
1703 if(size -> item.mode != MODE_INTEGER)
1705 oberon_error(ctx, "requires integer constant");
1708 oberon_type_t * arr;
1709 arr = *type;
1710 arr -> class = OBERON_TYPE_ARRAY;
1711 arr -> size = size -> item.integer;
1712 arr -> base = base;
1715 static void
1716 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec)
1718 if(ctx -> token == IDENT)
1720 char * name;
1721 oberon_type_t * type;
1722 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1724 name = oberon_assert_ident(ctx);
1725 oberon_assert_token(ctx, COLON);
1726 oberon_type(ctx, &type);
1727 oberon_define_field(ctx, rec, name, type);
1731 static void
1732 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
1734 char * name;
1735 oberon_object_t * to;
1737 name = oberon_assert_ident(ctx);
1738 to = oberon_find_object(ctx -> decl, name, 0);
1740 if(to != NULL)
1742 if(to -> class != OBERON_CLASS_TYPE)
1744 oberon_error(ctx, "not a type");
1747 else
1749 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE);
1750 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1753 *type = to -> type;
1756 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
1758 /*
1759 * Правило граматики "type". Указатель type должен указывать на существующий объект!
1760 */
1762 static void
1763 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
1765 if(sizes == NULL)
1767 *type = base;
1768 return;
1771 oberon_type_t * dim;
1772 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
1774 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
1776 oberon_make_array_type(ctx, sizes, dim, type);
1779 static void
1780 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
1782 if(ctx -> token == IDENT)
1784 oberon_qualident_type(ctx, type);
1786 else if(ctx -> token == ARRAY)
1788 oberon_assert_token(ctx, ARRAY);
1790 int num_sizes = 0;
1791 oberon_expr_t * sizes;
1792 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
1794 oberon_assert_token(ctx, OF);
1796 oberon_type_t * base;
1797 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
1798 oberon_type(ctx, &base);
1800 oberon_make_multiarray(ctx, sizes, base, type);
1802 else if(ctx -> token == RECORD)
1804 oberon_type_t * rec;
1805 rec = *type;
1806 rec -> class = OBERON_TYPE_RECORD;
1807 oberon_object_t * list = malloc(sizeof *list);
1808 memset(list, 0, sizeof *list);
1809 rec -> num_decl = 0;
1810 rec -> base = NULL;
1811 rec -> decl = list;
1813 oberon_assert_token(ctx, RECORD);
1814 oberon_field_list(ctx, rec);
1815 while(ctx -> token == SEMICOLON)
1817 oberon_assert_token(ctx, SEMICOLON);
1818 oberon_field_list(ctx, rec);
1820 oberon_assert_token(ctx, END);
1822 rec -> decl = rec -> decl -> next;
1823 *type = rec;
1825 else if(ctx -> token == POINTER)
1827 oberon_assert_token(ctx, POINTER);
1828 oberon_assert_token(ctx, TO);
1830 oberon_type_t * base;
1831 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
1832 oberon_type(ctx, &base);
1834 oberon_type_t * ptr;
1835 ptr = *type;
1836 ptr -> class = OBERON_TYPE_POINTER;
1837 ptr -> base = base;
1839 else if(ctx -> token == PROCEDURE)
1841 oberon_open_scope(ctx);
1842 oberon_assert_token(ctx, PROCEDURE);
1843 oberon_opt_formal_pars(ctx, type);
1844 oberon_close_scope(ctx -> decl);
1846 else
1848 oberon_error(ctx, "invalid type declaration");
1852 static void
1853 oberon_type_decl(oberon_context_t * ctx)
1855 char * name;
1856 oberon_object_t * newtype;
1857 oberon_type_t * type;
1859 name = oberon_assert_ident(ctx);
1861 newtype = oberon_find_object(ctx -> decl, name, 0);
1862 if(newtype == NULL)
1864 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE);
1865 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1866 assert(newtype -> type);
1868 else
1870 if(newtype -> class != OBERON_CLASS_TYPE)
1872 oberon_error(ctx, "mult definition");
1875 if(newtype -> linked)
1877 oberon_error(ctx, "mult definition - already linked");
1881 oberon_assert_token(ctx, EQUAL);
1883 type = newtype -> type;
1884 oberon_type(ctx, &type);
1886 if(type -> class == OBERON_TYPE_VOID)
1888 oberon_error(ctx, "recursive alias declaration");
1891 newtype -> type = type;
1892 newtype -> linked = 1;
1895 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
1896 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
1898 static void
1899 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
1901 if(type -> class != OBERON_TYPE_POINTER
1902 && type -> class != OBERON_TYPE_ARRAY)
1904 return;
1907 if(type -> recursive)
1909 oberon_error(ctx, "recursive pointer declaration");
1912 if(type -> base -> class == OBERON_TYPE_POINTER)
1914 oberon_error(ctx, "attempt to make pointer to pointer");
1917 type -> recursive = 1;
1919 oberon_prevent_recursive_pointer(ctx, type -> base);
1921 type -> recursive = 0;
1924 static void
1925 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
1927 if(type -> class != OBERON_TYPE_RECORD)
1929 return;
1932 if(type -> recursive)
1934 oberon_error(ctx, "recursive record declaration");
1937 type -> recursive = 1;
1939 int num_fields = type -> num_decl;
1940 oberon_object_t * field = type -> decl;
1941 for(int i = 0; i < num_fields; i++)
1943 oberon_prevent_recursive_object(ctx, field);
1944 field = field -> next;
1947 type -> recursive = 0;
1949 static void
1950 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
1952 if(type -> class != OBERON_TYPE_PROCEDURE)
1954 return;
1957 if(type -> recursive)
1959 oberon_error(ctx, "recursive procedure declaration");
1962 type -> recursive = 1;
1964 int num_fields = type -> num_decl;
1965 oberon_object_t * field = type -> decl;
1966 for(int i = 0; i < num_fields; i++)
1968 oberon_prevent_recursive_object(ctx, field);
1969 field = field -> next;
1972 type -> recursive = 0;
1975 static void
1976 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
1978 if(type -> class != OBERON_TYPE_ARRAY)
1980 return;
1983 if(type -> recursive)
1985 oberon_error(ctx, "recursive array declaration");
1988 type -> recursive = 1;
1990 oberon_prevent_recursive_type(ctx, type -> base);
1992 type -> recursive = 0;
1995 static void
1996 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
1998 if(type -> class == OBERON_TYPE_POINTER)
2000 oberon_prevent_recursive_pointer(ctx, type);
2002 else if(type -> class == OBERON_TYPE_RECORD)
2004 oberon_prevent_recursive_record(ctx, type);
2006 else if(type -> class == OBERON_TYPE_ARRAY)
2008 oberon_prevent_recursive_array(ctx, type);
2010 else if(type -> class == OBERON_TYPE_PROCEDURE)
2012 oberon_prevent_recursive_procedure(ctx, type);
2016 static void
2017 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
2019 switch(x -> class)
2021 case OBERON_CLASS_VAR:
2022 case OBERON_CLASS_TYPE:
2023 case OBERON_CLASS_PARAM:
2024 case OBERON_CLASS_VAR_PARAM:
2025 case OBERON_CLASS_FIELD:
2026 oberon_prevent_recursive_type(ctx, x -> type);
2027 break;
2028 case OBERON_CLASS_CONST:
2029 case OBERON_CLASS_PROC:
2030 break;
2031 default:
2032 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2033 break;
2037 static void
2038 oberon_prevent_recursive_decl(oberon_context_t * ctx)
2040 oberon_object_t * x = ctx -> decl -> list -> next;
2042 while(x)
2044 oberon_prevent_recursive_object(ctx, x);
2045 x = x -> next;
2049 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
2050 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
2052 static void
2053 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
2055 if(type -> class != OBERON_TYPE_RECORD)
2057 return;
2060 int num_fields = type -> num_decl;
2061 oberon_object_t * field = type -> decl;
2062 for(int i = 0; i < num_fields; i++)
2064 if(field -> type -> class == OBERON_TYPE_POINTER)
2066 oberon_initialize_type(ctx, field -> type);
2069 oberon_initialize_object(ctx, field);
2070 field = field -> next;
2073 oberon_generator_init_record(ctx, type);
2076 static void
2077 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
2079 if(type -> class == OBERON_TYPE_VOID)
2081 oberon_error(ctx, "undeclarated type");
2084 if(type -> initialized)
2086 return;
2089 type -> initialized = 1;
2091 if(type -> class == OBERON_TYPE_POINTER)
2093 oberon_initialize_type(ctx, type -> base);
2094 oberon_generator_init_type(ctx, type);
2096 else if(type -> class == OBERON_TYPE_ARRAY)
2098 oberon_initialize_type(ctx, type -> base);
2099 oberon_generator_init_type(ctx, type);
2101 else if(type -> class == OBERON_TYPE_RECORD)
2103 oberon_generator_init_type(ctx, type);
2104 oberon_initialize_record_fields(ctx, type);
2106 else if(type -> class == OBERON_TYPE_PROCEDURE)
2108 int num_fields = type -> num_decl;
2109 oberon_object_t * field = type -> decl;
2110 for(int i = 0; i < num_fields; i++)
2112 oberon_initialize_object(ctx, field);
2113 field = field -> next;
2114 }
2116 oberon_generator_init_type(ctx, type);
2118 else
2120 oberon_generator_init_type(ctx, type);
2124 static void
2125 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
2127 if(x -> initialized)
2129 return;
2132 x -> initialized = 1;
2134 switch(x -> class)
2136 case OBERON_CLASS_TYPE:
2137 oberon_initialize_type(ctx, x -> type);
2138 break;
2139 case OBERON_CLASS_VAR:
2140 case OBERON_CLASS_PARAM:
2141 case OBERON_CLASS_VAR_PARAM:
2142 case OBERON_CLASS_FIELD:
2143 oberon_initialize_type(ctx, x -> type);
2144 oberon_generator_init_var(ctx, x);
2145 break;
2146 case OBERON_CLASS_CONST:
2147 case OBERON_CLASS_PROC:
2148 break;
2149 default:
2150 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
2151 break;
2155 static void
2156 oberon_initialize_decl(oberon_context_t * ctx)
2158 oberon_object_t * x = ctx -> decl -> list;
2160 while(x -> next)
2162 oberon_initialize_object(ctx, x -> next);
2163 x = x -> next;
2164 }
2167 static void
2168 oberon_prevent_undeclarated_procedures(oberon_context_t * ctx)
2170 oberon_object_t * x = ctx -> decl -> list;
2172 while(x -> next)
2174 if(x -> next -> class == OBERON_CLASS_PROC)
2176 if(x -> next -> linked == 0)
2178 oberon_error(ctx, "unresolved forward declaration");
2181 x = x -> next;
2182 }
2185 static void
2186 oberon_decl_seq(oberon_context_t * ctx)
2188 if(ctx -> token == CONST)
2190 oberon_assert_token(ctx, CONST);
2191 while(ctx -> token == IDENT)
2193 oberon_const_decl(ctx);
2194 oberon_assert_token(ctx, SEMICOLON);
2198 if(ctx -> token == TYPE)
2200 oberon_assert_token(ctx, TYPE);
2201 while(ctx -> token == IDENT)
2203 oberon_type_decl(ctx);
2204 oberon_assert_token(ctx, SEMICOLON);
2208 if(ctx -> token == VAR)
2210 oberon_assert_token(ctx, VAR);
2211 while(ctx -> token == IDENT)
2213 oberon_var_decl(ctx);
2214 oberon_assert_token(ctx, SEMICOLON);
2218 oberon_prevent_recursive_decl(ctx);
2219 oberon_initialize_decl(ctx);
2221 while(ctx -> token == PROCEDURE)
2223 oberon_proc_decl(ctx);
2224 oberon_assert_token(ctx, SEMICOLON);
2227 oberon_prevent_undeclarated_procedures(ctx);
2230 static void
2231 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
2233 oberon_autocast_to(ctx, src, dst -> result);
2234 oberon_generate_assign(ctx, src, dst);
2237 static void
2238 oberon_statement(oberon_context_t * ctx)
2240 oberon_expr_t * item1;
2241 oberon_expr_t * item2;
2243 if(ctx -> token == IDENT)
2245 item1 = oberon_designator(ctx);
2246 if(ctx -> token == ASSIGN)
2248 oberon_assert_token(ctx, ASSIGN);
2249 item2 = oberon_expr(ctx);
2250 oberon_assign(ctx, item2, item1);
2252 else
2254 oberon_opt_proc_parens(ctx, item1);
2257 else if(ctx -> token == RETURN)
2259 oberon_assert_token(ctx, RETURN);
2260 if(ISEXPR(ctx -> token))
2262 oberon_expr_t * expr;
2263 expr = oberon_expr(ctx);
2264 oberon_make_return(ctx, expr);
2266 else
2268 oberon_make_return(ctx, NULL);
2273 static void
2274 oberon_statement_seq(oberon_context_t * ctx)
2276 oberon_statement(ctx);
2277 while(ctx -> token == SEMICOLON)
2279 oberon_assert_token(ctx, SEMICOLON);
2280 oberon_statement(ctx);
2284 static void
2285 oberon_parse_module(oberon_context_t * ctx)
2287 char *name1, *name2;
2288 oberon_read_token(ctx);
2290 oberon_assert_token(ctx, MODULE);
2291 name1 = oberon_assert_ident(ctx);
2292 oberon_assert_token(ctx, SEMICOLON);
2293 ctx -> mod -> name = name1;
2295 oberon_decl_seq(ctx);
2297 if(ctx -> token == BEGIN)
2299 oberon_assert_token(ctx, BEGIN);
2300 oberon_generate_begin_module(ctx);
2301 oberon_statement_seq(ctx);
2302 oberon_generate_end_module(ctx);
2305 oberon_assert_token(ctx, END);
2306 name2 = oberon_assert_ident(ctx);
2307 oberon_assert_token(ctx, DOT);
2309 if(strcmp(name1, name2) != 0)
2311 oberon_error(ctx, "module name not matched");
2315 // =======================================================================
2316 // LIBRARY
2317 // =======================================================================
2319 static void
2320 register_default_types(oberon_context_t * ctx)
2322 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2323 oberon_generator_init_type(ctx, ctx -> void_type);
2325 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
2326 ctx -> void_ptr_type -> base = ctx -> void_type;
2327 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
2329 ctx -> int_type = oberon_new_type_integer(sizeof(int));
2330 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type);
2332 ctx -> bool_type = oberon_new_type_boolean(sizeof(int));
2333 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type);
2336 static void
2337 oberon_new_intrinsic(oberon_context_t * ctx, char * name, GenerateFuncCallback f, GenerateProcCallback p)
2339 oberon_object_t * proc;
2340 proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC);
2341 proc -> sysproc = 1;
2342 proc -> genfunc = f;
2343 proc -> genproc = p;
2344 proc -> type = oberon_new_type_ptr(OBERON_TYPE_PROCEDURE);
2347 static oberon_expr_t *
2348 oberon_make_abs_call(oberon_context_t * ctx, int num_args, oberon_expr_t * list_args)
2350 if(num_args < 1)
2352 oberon_error(ctx, "too few arguments");
2355 if(num_args > 1)
2357 oberon_error(ctx, "too mach arguments");
2360 oberon_expr_t * arg;
2361 arg = list_args;
2363 oberon_type_t * result_type;
2364 result_type = arg -> result;
2366 if(result_type -> class != OBERON_TYPE_INTEGER)
2368 oberon_error(ctx, "ABS accepts only integers");
2372 oberon_expr_t * expr;
2373 expr = oberon_new_operator(OP_ABS, result_type, arg, NULL);
2374 return expr;
2377 oberon_context_t *
2378 oberon_create_context()
2380 oberon_context_t * ctx = malloc(sizeof *ctx);
2381 memset(ctx, 0, sizeof *ctx);
2383 oberon_scope_t * world_scope;
2384 world_scope = oberon_open_scope(ctx);
2385 ctx -> world_scope = world_scope;
2387 oberon_generator_init_context(ctx);
2389 register_default_types(ctx);
2390 oberon_new_intrinsic(ctx, "ABS", oberon_make_abs_call, NULL);
2392 return ctx;
2395 void
2396 oberon_destroy_context(oberon_context_t * ctx)
2398 oberon_generator_destroy_context(ctx);
2399 free(ctx);
2402 oberon_module_t *
2403 oberon_compile_module(oberon_context_t * ctx, const char * code)
2405 oberon_module_t * mod = malloc(sizeof *mod);
2406 memset(mod, 0, sizeof *mod);
2407 ctx -> mod = mod;
2409 oberon_scope_t * module_scope;
2410 module_scope = oberon_open_scope(ctx);
2411 mod -> decl = module_scope;
2413 oberon_init_scaner(ctx, code);
2414 oberon_parse_module(ctx);
2416 oberon_generate_code(ctx);
2418 ctx -> mod = NULL;
2419 return mod;