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 ctx -> decl = scope;
122 return scope;
125 static void
126 oberon_close_scope(oberon_scope_t * scope)
128 oberon_context_t * ctx = scope -> ctx;
129 ctx -> decl = scope -> up;
132 static oberon_object_t *
133 oberon_define_object(oberon_scope_t * scope, char * name, int class)
135 oberon_object_t * x = scope -> list;
136 while(x -> next && strcmp(x -> next -> name, name) != 0)
138 x = x -> next;
141 if(x -> next)
143 oberon_error(scope -> ctx, "already defined");
146 oberon_object_t * newvar = malloc(sizeof *newvar);
147 memset(newvar, 0, sizeof *newvar);
148 newvar -> name = name;
149 newvar -> class = class;
151 x -> next = newvar;
153 return newvar;
156 static void
157 oberon_define_field(oberon_context_t * ctx, oberon_type_t * rec, char * name, oberon_type_t * type)
159 oberon_object_t * x = rec -> decl;
160 while(x -> next && strcmp(x -> next -> name, name) != 0)
162 x = x -> next;
165 if(x -> next)
167 oberon_error(ctx, "multiple definition");
170 oberon_object_t * field = malloc(sizeof *field);
171 memset(field, 0, sizeof *field);
172 field -> name = name;
173 field -> class = OBERON_CLASS_FIELD;
174 field -> type = type;
176 rec -> num_decl += 1;
177 x -> next = field;
180 static oberon_object_t *
181 oberon_find_object_in_list(oberon_object_t * list, char * name)
183 oberon_object_t * x = list;
184 while(x -> next && strcmp(x -> next -> name, name) != 0)
186 x = x -> next;
188 return x -> next;
191 static oberon_object_t *
192 oberon_find_object(oberon_scope_t * scope, char * name, int check_it)
194 oberon_object_t * result = NULL;
196 oberon_scope_t * s = scope;
197 while(result == NULL && s != NULL)
199 result = oberon_find_object_in_list(s -> list, name);
200 s = s -> up;
203 if(check_it && result == NULL)
205 oberon_error(scope -> ctx, "undefined ident %s", name);
208 return result;
211 static oberon_object_t *
212 oberon_find_field(oberon_context_t * ctx, oberon_type_t * rec, char * name)
214 oberon_object_t * x = rec -> decl;
215 for(int i = 0; i < rec -> num_decl; i++)
217 if(strcmp(x -> name, name) == 0)
219 return x;
221 x = x -> next;
224 oberon_error(ctx, "field not defined");
226 return NULL;
229 static oberon_object_t *
230 oberon_define_type(oberon_scope_t * scope, char * name, oberon_type_t * type)
232 oberon_object_t * id;
233 id = oberon_define_object(scope, name, OBERON_CLASS_TYPE);
234 id -> type = type;
235 oberon_generator_init_type(scope -> ctx, type);
236 return id;
239 /*
240 static oberon_type_t *
241 oberon_find_type(oberon_scope_t * scope, char * name)
243 oberon_object_t * x = oberon_find_object(scope, name);
244 if(x -> class != OBERON_CLASS_TYPE)
246 oberon_error(scope -> ctx, "%s not a type", name);
249 return x -> type;
251 */
253 static oberon_object_t *
254 oberon_define_var(oberon_scope_t * scope, int class, char * name, oberon_type_t * type)
256 oberon_object_t * var;
257 var = oberon_define_object(scope, name, class);
258 var -> type = type;
259 return var;
262 /*
263 static oberon_object_t *
264 oberon_find_var(oberon_scope_t * scope, char * name)
266 oberon_object_t * x = oberon_find_object(scope, name);
268 if(x -> class != OBERON_CLASS_VAR)
270 oberon_error(scope -> ctx, "%s not a var", name);
273 return x;
275 */
277 static oberon_object_t *
278 oberon_define_proc(oberon_scope_t * scope, char * name, oberon_type_t * signature)
280 oberon_object_t * proc;
281 proc = oberon_define_object(scope, name, OBERON_CLASS_PROC);
282 proc -> type = signature;
283 return proc;
286 // =======================================================================
287 // SCANER
288 // =======================================================================
290 static void
291 oberon_get_char(oberon_context_t * ctx)
293 ctx -> code_index += 1;
294 ctx -> c = ctx -> code[ctx -> code_index];
297 static void
298 oberon_init_scaner(oberon_context_t * ctx, const char * code)
300 ctx -> code = code;
301 ctx -> code_index = 0;
302 ctx -> c = ctx -> code[ctx -> code_index];
305 static void
306 oberon_read_ident(oberon_context_t * ctx)
308 int len = 0;
309 int i = ctx -> code_index;
311 int c = ctx -> code[i];
312 while(isalnum(c))
314 i += 1;
315 len += 1;
316 c = ctx -> code[i];
319 char * ident = malloc(len + 1);
320 memcpy(ident, &ctx->code[ctx->code_index], len);
321 ident[len] = 0;
323 ctx -> code_index = i;
324 ctx -> c = ctx -> code[i];
325 ctx -> string = ident;
326 ctx -> token = IDENT;
328 if(strcmp(ident, "MODULE") == 0)
330 ctx -> token = MODULE;
332 else if(strcmp(ident, "END") == 0)
334 ctx -> token = END;
336 else if(strcmp(ident, "VAR") == 0)
338 ctx -> token = VAR;
340 else if(strcmp(ident, "BEGIN") == 0)
342 ctx -> token = BEGIN;
344 else if(strcmp(ident, "TRUE") == 0)
346 ctx -> token = TRUE;
348 else if(strcmp(ident, "FALSE") == 0)
350 ctx -> token = FALSE;
352 else if(strcmp(ident, "OR") == 0)
354 ctx -> token = OR;
356 else if(strcmp(ident, "DIV") == 0)
358 ctx -> token = DIV;
360 else if(strcmp(ident, "MOD") == 0)
362 ctx -> token = MOD;
364 else if(strcmp(ident, "PROCEDURE") == 0)
366 ctx -> token = PROCEDURE;
368 else if(strcmp(ident, "RETURN") == 0)
370 ctx -> token = RETURN;
372 else if(strcmp(ident, "CONST") == 0)
374 ctx -> token = CONST;
376 else if(strcmp(ident, "TYPE") == 0)
378 ctx -> token = TYPE;
380 else if(strcmp(ident, "ARRAY") == 0)
382 ctx -> token = ARRAY;
384 else if(strcmp(ident, "OF") == 0)
386 ctx -> token = OF;
388 else if(strcmp(ident, "RECORD") == 0)
390 ctx -> token = RECORD;
392 else if(strcmp(ident, "POINTER") == 0)
394 ctx -> token = POINTER;
396 else if(strcmp(ident, "TO") == 0)
398 ctx -> token = TO;
400 else if(strcmp(ident, "NIL") == 0)
402 ctx -> token = NIL;
406 static void
407 oberon_read_integer(oberon_context_t * ctx)
409 int len = 0;
410 int i = ctx -> code_index;
412 int c = ctx -> code[i];
413 while(isdigit(c))
415 i += 1;
416 len += 1;
417 c = ctx -> code[i];
420 char * ident = malloc(len + 2);
421 memcpy(ident, &ctx->code[ctx->code_index], len);
422 ident[len + 1] = 0;
424 ctx -> code_index = i;
425 ctx -> c = ctx -> code[i];
426 ctx -> string = ident;
427 ctx -> integer = atoi(ident);
428 ctx -> token = INTEGER;
431 static void
432 oberon_skip_space(oberon_context_t * ctx)
434 while(isspace(ctx -> c))
436 oberon_get_char(ctx);
440 static void
441 oberon_read_symbol(oberon_context_t * ctx)
443 int c = ctx -> c;
444 switch(c)
446 case 0:
447 ctx -> token = EOF_;
448 break;
449 case ';':
450 ctx -> token = SEMICOLON;
451 oberon_get_char(ctx);
452 break;
453 case ':':
454 ctx -> token = COLON;
455 oberon_get_char(ctx);
456 if(ctx -> c == '=')
458 ctx -> token = ASSIGN;
459 oberon_get_char(ctx);
461 break;
462 case '.':
463 ctx -> token = DOT;
464 oberon_get_char(ctx);
465 break;
466 case '(':
467 ctx -> token = LPAREN;
468 oberon_get_char(ctx);
469 break;
470 case ')':
471 ctx -> token = RPAREN;
472 oberon_get_char(ctx);
473 break;
474 case '=':
475 ctx -> token = EQUAL;
476 oberon_get_char(ctx);
477 break;
478 case '#':
479 ctx -> token = NEQ;
480 oberon_get_char(ctx);
481 break;
482 case '<':
483 ctx -> token = LESS;
484 oberon_get_char(ctx);
485 if(ctx -> c == '=')
487 ctx -> token = LEQ;
488 oberon_get_char(ctx);
490 break;
491 case '>':
492 ctx -> token = GREAT;
493 oberon_get_char(ctx);
494 if(ctx -> c == '=')
496 ctx -> token = GEQ;
497 oberon_get_char(ctx);
499 break;
500 case '+':
501 ctx -> token = PLUS;
502 oberon_get_char(ctx);
503 break;
504 case '-':
505 ctx -> token = MINUS;
506 oberon_get_char(ctx);
507 break;
508 case '*':
509 ctx -> token = STAR;
510 oberon_get_char(ctx);
511 break;
512 case '/':
513 ctx -> token = SLASH;
514 oberon_get_char(ctx);
515 break;
516 case '&':
517 ctx -> token = AND;
518 oberon_get_char(ctx);
519 break;
520 case '~':
521 ctx -> token = NOT;
522 oberon_get_char(ctx);
523 break;
524 case ',':
525 ctx -> token = COMMA;
526 oberon_get_char(ctx);
527 break;
528 case '[':
529 ctx -> token = LBRACE;
530 oberon_get_char(ctx);
531 break;
532 case ']':
533 ctx -> token = RBRACE;
534 oberon_get_char(ctx);
535 break;
536 case '^':
537 ctx -> token = UPARROW;
538 oberon_get_char(ctx);
539 break;
540 default:
541 oberon_error(ctx, "invalid char");
542 break;
546 static void
547 oberon_read_token(oberon_context_t * ctx)
549 oberon_skip_space(ctx);
551 int c = ctx -> c;
552 if(isalpha(c))
554 oberon_read_ident(ctx);
556 else if(isdigit(c))
558 oberon_read_integer(ctx);
560 else
562 oberon_read_symbol(ctx);
566 // =======================================================================
567 // EXPRESSION
568 // =======================================================================
570 static void oberon_expect_token(oberon_context_t * ctx, int token);
571 static oberon_expr_t * oberon_expr(oberon_context_t * ctx);
572 static void oberon_assert_token(oberon_context_t * ctx, int token);
573 static char * oberon_assert_ident(oberon_context_t * ctx);
574 static void oberon_type(oberon_context_t * ctx, oberon_type_t ** type);
575 static oberon_item_t * oberon_const_expr(oberon_context_t * ctx);
577 static oberon_expr_t *
578 oberon_new_operator(int op, oberon_type_t * result, oberon_expr_t * left, oberon_expr_t * right)
580 oberon_oper_t * operator;
581 operator = malloc(sizeof *operator);
582 memset(operator, 0, sizeof *operator);
584 operator -> is_item = 0;
585 operator -> result = result;
586 operator -> op = op;
587 operator -> left = left;
588 operator -> right = right;
590 return (oberon_expr_t *) operator;
593 static oberon_expr_t *
594 oberon_new_item(int mode, oberon_type_t * result)
596 oberon_item_t * item;
597 item = malloc(sizeof *item);
598 memset(item, 0, sizeof *item);
600 item -> is_item = 1;
601 item -> result = result;
602 item -> mode = mode;
604 return (oberon_expr_t *)item;
607 static oberon_expr_t *
608 oberon_make_unary_op(oberon_context_t * ctx, int token, oberon_expr_t * a)
610 oberon_expr_t * expr;
611 oberon_type_t * result;
613 result = a -> result;
615 if(token == MINUS)
617 if(result -> class != OBERON_TYPE_INTEGER)
619 oberon_error(ctx, "incompatible operator type");
622 expr = oberon_new_operator(OP_UNARY_MINUS, result, a, NULL);
624 else if(token == NOT)
626 if(result -> class != OBERON_TYPE_BOOLEAN)
628 oberon_error(ctx, "incompatible operator type");
631 expr = oberon_new_operator(OP_LOGIC_NOT, result, a, NULL);
633 else
635 oberon_error(ctx, "oberon_make_unary_op: wat");
638 return expr;
641 static void
642 oberon_expr_list(oberon_context_t * ctx, int * num_expr, oberon_expr_t ** first, int const_expr)
644 oberon_expr_t * last;
646 *num_expr = 1;
647 *first = last = oberon_expr(ctx);
648 while(ctx -> token == COMMA)
650 oberon_assert_token(ctx, COMMA);
651 oberon_expr_t * current;
653 if(const_expr)
655 current = (oberon_expr_t *) oberon_const_expr(ctx);
657 else
659 current = oberon_expr(ctx);
662 last -> next = current;
663 last = current;
664 *num_expr += 1;
668 static oberon_expr_t *
669 oberon_autocast_to(oberon_context_t * ctx, oberon_expr_t * expr, oberon_type_t * pref)
671 if(pref -> class != expr -> result -> class)
673 oberon_error(ctx, "incompatible types");
676 if(pref -> class == OBERON_TYPE_INTEGER)
678 if(expr -> result -> class > pref -> class)
680 oberon_error(ctx, "incompatible size");
683 else if(pref -> class == OBERON_TYPE_RECORD)
685 if(expr -> result != pref)
687 printf("oberon_autocast_to: rec %p != %p\n", expr -> result, pref);
688 oberon_error(ctx, "incompatible record types");
691 else if(pref -> class == OBERON_TYPE_POINTER)
693 if(expr -> result -> base != pref -> base)
695 if(expr -> result -> base -> class != OBERON_TYPE_VOID)
697 oberon_error(ctx, "incompatible pointer types");
702 // TODO cast
704 return expr;
707 static void
708 oberon_autocast_call(oberon_context_t * ctx, oberon_expr_t * desig)
710 if(desig -> is_item == 0)
712 oberon_error(ctx, "expected item");
715 if(desig -> item.mode != MODE_CALL)
717 oberon_error(ctx, "expected mode CALL");
720 if(desig -> item.var -> class != OBERON_CLASS_PROC)
722 oberon_error(ctx, "only procedures can be called");
725 oberon_type_t * fn = desig -> item.var -> type;
726 int num_args = desig -> item.num_args;
727 int num_decl = fn -> num_decl;
729 if(num_args < num_decl)
731 oberon_error(ctx, "too few arguments");
733 else if(num_args > num_decl)
735 oberon_error(ctx, "too many arguments");
738 oberon_expr_t * arg = desig -> item.args;
739 oberon_object_t * param = fn -> decl;
740 for(int i = 0; i < num_args; i++)
742 oberon_autocast_to(ctx, arg, param -> type);
743 arg = arg -> next;
744 param = param -> next;
748 #define ISEXPR(x) \
749 (((x) == PLUS) \
750 || ((x) == MINUS) \
751 || ((x) == IDENT) \
752 || ((x) == INTEGER) \
753 || ((x) == LPAREN) \
754 || ((x) == NOT) \
755 || ((x) == TRUE) \
756 || ((x) == FALSE))
758 static oberon_expr_t *
759 oberno_make_dereferencing(oberon_context_t * ctx, oberon_expr_t * expr)
761 if(expr -> result -> class != OBERON_TYPE_POINTER)
763 oberon_error(ctx, "not a pointer");
766 assert(expr -> is_item);
768 oberon_expr_t * selector;
769 selector = oberon_new_item(MODE_DEREF, expr -> result -> base);
770 selector -> item.parent = (oberon_item_t *) expr;
772 return selector;
775 static oberon_expr_t *
776 oberon_make_array_selector(oberon_context_t * ctx, oberon_expr_t * desig, oberon_expr_t * index)
778 if(desig -> result -> class == OBERON_TYPE_POINTER)
780 desig = oberno_make_dereferencing(ctx, desig);
783 assert(desig -> is_item);
785 if(desig -> result -> class != OBERON_TYPE_ARRAY)
787 oberon_error(ctx, "not array");
790 oberon_type_t * base;
791 base = desig -> result -> base;
793 // TODO check ranges
795 printf("oberon_make_array_selector: index class %i\n", index -> result -> class);
796 if(index -> result -> class != OBERON_TYPE_INTEGER)
798 oberon_error(ctx, "index must be integer");
801 if(index -> is_item)
803 if(index -> item.mode == MODE_INTEGER)
805 int arr_size = desig -> result -> size;
806 int index_int = index -> item.integer;
807 if(index_int < 0 || index_int > arr_size - 1)
809 oberon_error(ctx, "not in range (dimension size 0..%i)", arr_size - 1);
814 oberon_expr_t * selector;
815 selector = oberon_new_item(MODE_INDEX, base);
816 selector -> item.parent = (oberon_item_t *) desig;
817 selector -> item.num_args = 1;
818 selector -> item.args = index;
820 return selector;
823 static oberon_expr_t *
824 oberon_make_record_selector(oberon_context_t * ctx, oberon_expr_t * expr, char * name)
826 if(expr -> result -> class == OBERON_TYPE_POINTER)
828 expr = oberno_make_dereferencing(ctx, expr);
831 assert(expr -> is_item == 1);
833 if(expr -> result -> class != OBERON_TYPE_RECORD)
835 oberon_error(ctx, "not record");
838 oberon_type_t * rec = expr -> result;
840 oberon_object_t * field;
841 field = oberon_find_field(ctx, rec, name);
843 oberon_expr_t * selector;
844 selector = oberon_new_item(MODE_FIELD, field -> type);
845 selector -> item.var = field;
846 selector -> item.parent = (oberon_item_t *) expr;
848 return selector;
851 #define ISSELECTOR(x) \
852 (((x) == LBRACE) \
853 || ((x) == DOT) \
854 || ((x) == UPARROW))
856 static oberon_expr_t *
857 oberon_designator(oberon_context_t * ctx)
859 char * name;
860 oberon_object_t * var;
861 oberon_expr_t * expr;
863 name = oberon_assert_ident(ctx);
864 var = oberon_find_object(ctx -> decl, name, 1);
866 switch(var -> class)
868 case OBERON_CLASS_CONST:
869 // TODO copy value
870 expr = (oberon_expr_t *) var -> value;
871 break;
872 case OBERON_CLASS_VAR:
873 case OBERON_CLASS_VAR_PARAM:
874 case OBERON_CLASS_PARAM:
875 expr = oberon_new_item(MODE_VAR, var -> type);
876 break;
877 case OBERON_CLASS_PROC:
878 expr = oberon_new_item(MODE_CALL, var -> type);
879 break;
880 default:
881 oberon_error(ctx, "invalid designator");
882 break;
884 expr -> item.var = var;
886 while(ISSELECTOR(ctx -> token))
888 switch(ctx -> token)
890 case DOT:
891 oberon_assert_token(ctx, DOT);
892 name = oberon_assert_ident(ctx);
893 expr = oberon_make_record_selector(ctx, expr, name);
894 break;
895 case LBRACE:
896 oberon_assert_token(ctx, LBRACE);
897 int num_indexes = 0;
898 oberon_expr_t * indexes = NULL;
899 oberon_expr_list(ctx, &num_indexes, &indexes, 0);
900 oberon_assert_token(ctx, RBRACE);
902 for(int i = 0; i < num_indexes; i++)
904 expr = oberon_make_array_selector(ctx, expr, indexes);
905 indexes = indexes -> next;
907 break;
908 case UPARROW:
909 oberon_assert_token(ctx, UPARROW);
910 expr = oberno_make_dereferencing(ctx, expr);
911 break;
912 default:
913 oberon_error(ctx, "oberon_designator: wat");
914 break;
917 return expr;
920 static oberon_expr_t *
921 oberon_opt_proc_parens(oberon_context_t * ctx, oberon_expr_t * expr)
923 assert(expr -> is_item == 1);
925 if(ctx -> token == LPAREN)
927 if(expr -> result -> class != OBERON_TYPE_PROCEDURE)
929 oberon_error(ctx, "not a procedure");
932 oberon_assert_token(ctx, LPAREN);
934 int num_args = 0;
935 oberon_expr_t * arguments = NULL;
937 if(ISEXPR(ctx -> token))
939 oberon_expr_list(ctx, &num_args, &arguments, 0);
942 expr -> result = expr -> item.var -> type -> base;
943 expr -> item.mode = MODE_CALL;
944 expr -> item.num_args = num_args;
945 expr -> item.args = arguments;
946 oberon_assert_token(ctx, RPAREN);
948 oberon_autocast_call(ctx, expr);
951 return expr;
954 static oberon_expr_t *
955 oberon_factor(oberon_context_t * ctx)
957 oberon_expr_t * expr;
959 switch(ctx -> token)
961 case IDENT:
962 expr = oberon_designator(ctx);
963 expr = oberon_opt_proc_parens(ctx, expr);
964 break;
965 case INTEGER:
966 expr = oberon_new_item(MODE_INTEGER, ctx -> int_type);
967 expr -> item.integer = ctx -> integer;
968 oberon_assert_token(ctx, INTEGER);
969 break;
970 case TRUE:
971 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type);
972 expr -> item.boolean = 1;
973 oberon_assert_token(ctx, TRUE);
974 break;
975 case FALSE:
976 expr = oberon_new_item(MODE_BOOLEAN, ctx -> bool_type);
977 expr -> item.boolean = 0;
978 oberon_assert_token(ctx, FALSE);
979 break;
980 case LPAREN:
981 oberon_assert_token(ctx, LPAREN);
982 expr = oberon_expr(ctx);
983 oberon_assert_token(ctx, RPAREN);
984 break;
985 case NOT:
986 oberon_assert_token(ctx, NOT);
987 expr = oberon_factor(ctx);
988 expr = oberon_make_unary_op(ctx, NOT, expr);
989 break;
990 case NIL:
991 oberon_assert_token(ctx, NIL);
992 expr = oberon_new_item(MODE_NIL, ctx -> void_ptr_type);
993 break;
994 default:
995 oberon_error(ctx, "invalid expression");
998 return expr;
1001 /*
1002 * oberon_autocast_binary_op автоматически переобразовывеат тип по след. правилам:
1003 * 1. Классы обоих типов должны быть одинаковы
1004 * 2. В качестве результата должен быть выбран больший тип.
1005 * 3. Если размер результат не должен быть меньше чем базовый int
1006 */
1008 static void
1009 oberon_autocast_binary_op(oberon_context_t * ctx, oberon_type_t * a, oberon_type_t * b, oberon_type_t ** result)
1011 if((a -> class) != (b -> class))
1013 oberon_error(ctx, "incompatible types");
1016 if((a -> size) > (b -> size))
1018 *result = a;
1020 else
1022 *result = b;
1025 if(((*result) -> class) == OBERON_TYPE_INTEGER)
1027 if(((*result) -> size) < (ctx -> int_type -> size))
1029 *result = ctx -> int_type;
1033 /* TODO: cast types */
1036 #define ITMAKESBOOLEAN(x) \
1037 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1039 #define ITUSEONLYINTEGER(x) \
1040 ((x) >= LESS && (x) <= GEQ)
1042 #define ITUSEONLYBOOLEAN(x) \
1043 (((x) == OR) || ((x) == AND))
1045 static oberon_expr_t *
1046 oberon_make_bin_op(oberon_context_t * ctx, int token, oberon_expr_t * a, oberon_expr_t * b)
1048 oberon_expr_t * expr;
1049 oberon_type_t * result;
1051 if(ITMAKESBOOLEAN(token))
1053 if(ITUSEONLYINTEGER(token))
1055 if(a -> result -> class != OBERON_TYPE_INTEGER
1056 || b -> result -> class != OBERON_TYPE_INTEGER)
1058 oberon_error(ctx, "used only with integer types");
1061 else if(ITUSEONLYBOOLEAN(token))
1063 if(a -> result -> class != OBERON_TYPE_BOOLEAN
1064 || b -> result -> class != OBERON_TYPE_BOOLEAN)
1066 oberon_error(ctx, "used only with boolean type");
1070 result = ctx -> bool_type;
1072 if(token == EQUAL)
1074 expr = oberon_new_operator(OP_EQ, result, a, b);
1076 else if(token == NEQ)
1078 expr = oberon_new_operator(OP_NEQ, result, a, b);
1080 else if(token == LESS)
1082 expr = oberon_new_operator(OP_LSS, result, a, b);
1084 else if(token == LEQ)
1086 expr = oberon_new_operator(OP_LEQ, result, a, b);
1088 else if(token == GREAT)
1090 expr = oberon_new_operator(OP_GRT, result, a, b);
1092 else if(token == GEQ)
1094 expr = oberon_new_operator(OP_GEQ, result, a, b);
1096 else if(token == OR)
1098 expr = oberon_new_operator(OP_LOGIC_OR, result, a, b);
1100 else if(token == AND)
1102 expr = oberon_new_operator(OP_LOGIC_AND, result, a, b);
1104 else
1106 oberon_error(ctx, "oberon_make_bin_op: bool wat");
1109 else
1111 oberon_autocast_binary_op(ctx, a -> result, b -> result, &result);
1113 if(token == PLUS)
1115 expr = oberon_new_operator(OP_ADD, result, a, b);
1117 else if(token == MINUS)
1119 expr = oberon_new_operator(OP_SUB, result, a, b);
1121 else if(token == STAR)
1123 expr = oberon_new_operator(OP_MUL, result, a, b);
1125 else if(token == SLASH)
1127 expr = oberon_new_operator(OP_DIV, result, a, b);
1129 else if(token == DIV)
1131 expr = oberon_new_operator(OP_DIV, result, a, b);
1133 else if(token == MOD)
1135 expr = oberon_new_operator(OP_MOD, result, a, b);
1137 else
1139 oberon_error(ctx, "oberon_make_bin_op: bin wat");
1143 return expr;
1146 #define ISMULOP(x) \
1147 ((x) >= STAR && (x) <= AND)
1149 static oberon_expr_t *
1150 oberon_term_expr(oberon_context_t * ctx)
1152 oberon_expr_t * expr;
1154 expr = oberon_factor(ctx);
1155 while(ISMULOP(ctx -> token))
1157 int token = ctx -> token;
1158 oberon_read_token(ctx);
1160 oberon_expr_t * inter = oberon_factor(ctx);
1161 expr = oberon_make_bin_op(ctx, token, expr, inter);
1164 return expr;
1167 #define ISADDOP(x) \
1168 ((x) >= PLUS && (x) <= OR)
1170 static oberon_expr_t *
1171 oberon_simple_expr(oberon_context_t * ctx)
1173 oberon_expr_t * expr;
1175 int minus = 0;
1176 if(ctx -> token == PLUS)
1178 minus = 0;
1179 oberon_assert_token(ctx, PLUS);
1181 else if(ctx -> token == MINUS)
1183 minus = 1;
1184 oberon_assert_token(ctx, MINUS);
1187 expr = oberon_term_expr(ctx);
1188 while(ISADDOP(ctx -> token))
1190 int token = ctx -> token;
1191 oberon_read_token(ctx);
1193 oberon_expr_t * inter = oberon_term_expr(ctx);
1194 expr = oberon_make_bin_op(ctx, token, expr, inter);
1197 if(minus)
1199 expr = oberon_make_unary_op(ctx, MINUS, expr);
1202 return expr;
1205 #define ISRELATION(x) \
1206 ((x) >= EQUAL && (x) <= GEQ)
1208 static oberon_expr_t *
1209 oberon_expr(oberon_context_t * ctx)
1211 oberon_expr_t * expr;
1213 expr = oberon_simple_expr(ctx);
1214 while(ISRELATION(ctx -> token))
1216 int token = ctx -> token;
1217 oberon_read_token(ctx);
1219 oberon_expr_t * inter = oberon_simple_expr(ctx);
1220 expr = oberon_make_bin_op(ctx, token, expr, inter);
1223 return expr;
1226 static oberon_item_t *
1227 oberon_const_expr(oberon_context_t * ctx)
1229 oberon_expr_t * expr;
1230 expr = oberon_expr(ctx);
1232 if(expr -> is_item == 0)
1234 oberon_error(ctx, "const expression are required");
1237 return (oberon_item_t *) expr;
1240 // =======================================================================
1241 // PARSER
1242 // =======================================================================
1244 static void oberon_statement_seq(oberon_context_t * ctx);
1246 static void
1247 oberon_expect_token(oberon_context_t * ctx, int token)
1249 if(ctx -> token != token)
1251 oberon_error(ctx, "unexpected token %i (%i)", ctx -> token, token);
1255 static void
1256 oberon_assert_token(oberon_context_t * ctx, int token)
1258 oberon_expect_token(ctx, token);
1259 oberon_read_token(ctx);
1262 static char *
1263 oberon_assert_ident(oberon_context_t * ctx)
1265 oberon_expect_token(ctx, IDENT);
1266 char * ident = ctx -> string;
1267 oberon_read_token(ctx);
1268 return ident;
1271 static void
1272 oberon_var_decl(oberon_context_t * ctx)
1274 char * name;
1275 oberon_type_t * type;
1276 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1278 name = oberon_assert_ident(ctx);
1279 oberon_assert_token(ctx, COLON);
1280 oberon_type(ctx, &type);
1281 oberon_define_var(ctx -> decl, OBERON_CLASS_VAR, name, type);
1284 static oberon_object_t *
1285 oberon_make_param(oberon_context_t * ctx, int token, char * name, oberon_type_t * type)
1287 oberon_object_t * param;
1289 if(token == VAR)
1291 param = oberon_define_var(ctx -> decl, OBERON_CLASS_VAR_PARAM, name, type);
1293 else if(token == IDENT)
1295 param = oberon_define_var(ctx -> decl, OBERON_CLASS_PARAM, name, type);
1297 else
1299 oberon_error(ctx, "oberon_make_param: wat");
1302 return param;
1305 static oberon_object_t *
1306 oberon_fp_section(oberon_context_t * ctx, int * num_decl)
1308 int modifer_token = ctx -> token;
1309 if(ctx -> token == VAR)
1311 oberon_read_token(ctx);
1314 char * name;
1315 name = oberon_assert_ident(ctx);
1317 oberon_assert_token(ctx, COLON);
1319 oberon_type_t * type;
1320 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1321 oberon_type(ctx, &type);
1323 oberon_object_t * first;
1324 first = oberon_make_param(ctx, modifer_token, name, type);
1326 *num_decl += 1;
1327 return first;
1330 #define ISFPSECTION \
1331 ((ctx -> token == VAR) || (ctx -> token == IDENT))
1333 static void
1334 oberon_formal_pars(oberon_context_t * ctx, oberon_type_t * signature)
1336 oberon_assert_token(ctx, LPAREN);
1338 if(ISFPSECTION)
1340 signature -> decl = oberon_fp_section(ctx, &signature -> num_decl);
1341 while(ctx -> token == SEMICOLON)
1343 oberon_assert_token(ctx, SEMICOLON);
1344 oberon_fp_section(ctx, &signature -> num_decl);
1348 oberon_assert_token(ctx, RPAREN);
1350 if(ctx -> token == COLON)
1352 oberon_assert_token(ctx, COLON);
1353 oberon_type(ctx, &signature -> base);
1357 static void
1358 oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type)
1360 oberon_type_t * signature;
1361 signature = *type;
1362 signature -> class = OBERON_TYPE_PROCEDURE;
1363 signature -> num_decl = 0;
1364 signature -> base = ctx -> void_type;
1365 signature -> decl = NULL;
1367 if(ctx -> token == LPAREN)
1369 oberon_formal_pars(ctx, signature);
1373 static void
1374 oberon_make_return(oberon_context_t * ctx, oberon_expr_t * expr)
1376 if(ctx -> result_type -> class == OBERON_TYPE_VOID)
1378 if(expr != NULL)
1380 oberon_error(ctx, "procedure has no result type");
1383 else
1385 if(expr == NULL)
1387 oberon_error(ctx, "procedure requires expression on result");
1390 oberon_autocast_to(ctx, expr, ctx -> result_type);
1393 ctx -> has_return = 1;
1395 oberon_generate_return(ctx, expr);
1398 static void
1399 oberon_proc_decl(oberon_context_t * ctx)
1401 oberon_assert_token(ctx, PROCEDURE);
1403 char * name;
1404 name = oberon_assert_ident(ctx);
1406 oberon_scope_t * this_proc_def_scope = ctx -> decl;
1407 oberon_open_scope(ctx);
1409 oberon_type_t * signature;
1410 signature = oberon_new_type_ptr(OBERON_TYPE_VOID);
1411 oberon_opt_formal_pars(ctx, &signature);
1413 oberon_object_t * proc;
1414 proc = oberon_define_proc(this_proc_def_scope, name, signature);
1416 ctx -> result_type = signature -> base;
1417 ctx -> has_return = 0;
1419 oberon_assert_token(ctx, SEMICOLON);
1421 oberon_generate_begin_proc(ctx, proc);
1423 // TODO declarations
1425 if(ctx -> token == BEGIN)
1427 oberon_assert_token(ctx, BEGIN);
1428 oberon_statement_seq(ctx);
1431 oberon_assert_token(ctx, END);
1432 char * name2 = oberon_assert_ident(ctx);
1433 if(strcmp(name2, name) != 0)
1435 oberon_error(ctx, "procedure name not matched");
1438 if(signature -> base -> class == OBERON_TYPE_VOID)
1440 oberon_make_return(ctx, NULL);
1443 if(ctx -> has_return == 0)
1445 oberon_error(ctx, "procedure requires return");
1447 ctx -> result_type = NULL;
1449 oberon_generate_end_proc(ctx);
1450 oberon_close_scope(ctx -> decl);
1453 static void
1454 oberon_const_decl(oberon_context_t * ctx)
1456 char * name;
1457 oberon_item_t * value;
1458 oberon_object_t * constant;
1460 name = oberon_assert_ident(ctx);
1461 oberon_assert_token(ctx, EQUAL);
1462 value = oberon_const_expr(ctx);
1464 constant = oberon_define_object(ctx -> decl, name, OBERON_CLASS_CONST);
1465 constant -> value = value;
1468 static void
1469 oberon_make_array_type(oberon_context_t * ctx, oberon_expr_t * size, oberon_type_t * base, oberon_type_t ** type)
1471 if(size -> is_item == 0)
1473 oberon_error(ctx, "requires constant");
1476 if(size -> item.mode != MODE_INTEGER)
1478 oberon_error(ctx, "requires integer constant");
1481 oberon_type_t * arr;
1482 arr = *type;
1483 arr -> class = OBERON_TYPE_ARRAY;
1484 arr -> size = size -> item.integer;
1485 arr -> base = base;
1488 static void
1489 oberon_field_list(oberon_context_t * ctx, oberon_type_t * rec)
1491 if(ctx -> token == IDENT)
1493 char * name;
1494 oberon_type_t * type;
1495 type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1497 name = oberon_assert_ident(ctx);
1498 oberon_assert_token(ctx, COLON);
1499 oberon_type(ctx, &type);
1500 oberon_define_field(ctx, rec, name, type);
1504 static void
1505 oberon_qualident_type(oberon_context_t * ctx, oberon_type_t ** type)
1507 char * name;
1508 oberon_object_t * to;
1510 name = oberon_assert_ident(ctx);
1511 to = oberon_find_object(ctx -> decl, name, 0);
1513 if(to != NULL)
1515 if(to -> class != OBERON_CLASS_TYPE)
1517 oberon_error(ctx, "not a type");
1520 else
1522 to = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE);
1523 to -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1526 *type = to -> type;
1529 static void oberon_opt_formal_pars(oberon_context_t * ctx, oberon_type_t ** type);
1531 /*
1532 * Правило граматики "type". Указатель type должен указывать на существующий объект!
1533 */
1535 static void
1536 oberon_make_multiarray(oberon_context_t * ctx, oberon_expr_t * sizes, oberon_type_t * base, oberon_type_t ** type)
1538 if(sizes == NULL)
1540 *type = base;
1541 return;
1544 oberon_type_t * dim;
1545 dim = oberon_new_type_ptr(OBERON_TYPE_VOID);
1547 oberon_make_multiarray(ctx, sizes -> next, base, &dim);
1549 oberon_make_array_type(ctx, sizes, dim, type);
1552 static void
1553 oberon_type(oberon_context_t * ctx, oberon_type_t ** type)
1555 if(ctx -> token == IDENT)
1557 oberon_qualident_type(ctx, type);
1559 else if(ctx -> token == ARRAY)
1561 oberon_assert_token(ctx, ARRAY);
1563 int num_sizes = 0;
1564 oberon_expr_t * sizes;
1565 oberon_expr_list(ctx, &num_sizes, &sizes, 1);
1567 oberon_assert_token(ctx, OF);
1569 oberon_type_t * base;
1570 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
1571 oberon_type(ctx, &base);
1573 oberon_make_multiarray(ctx, sizes, base, type);
1575 else if(ctx -> token == RECORD)
1577 oberon_type_t * rec;
1578 rec = *type;
1579 rec -> class = OBERON_TYPE_RECORD;
1580 oberon_object_t * list = malloc(sizeof *list);
1581 memset(list, 0, sizeof *list);
1582 rec -> num_decl = 0;
1583 rec -> base = NULL;
1584 rec -> decl = list;
1586 oberon_assert_token(ctx, RECORD);
1587 oberon_field_list(ctx, rec);
1588 while(ctx -> token == SEMICOLON)
1590 oberon_assert_token(ctx, SEMICOLON);
1591 oberon_field_list(ctx, rec);
1593 oberon_assert_token(ctx, END);
1595 rec -> decl = rec -> decl -> next;
1596 *type = rec;
1598 else if(ctx -> token == POINTER)
1600 oberon_assert_token(ctx, POINTER);
1601 oberon_assert_token(ctx, TO);
1603 oberon_type_t * base;
1604 base = oberon_new_type_ptr(OBERON_TYPE_VOID);
1605 oberon_type(ctx, &base);
1607 oberon_type_t * ptr;
1608 ptr = *type;
1609 ptr -> class = OBERON_TYPE_POINTER;
1610 ptr -> base = base;
1612 else if(ctx -> token == PROCEDURE)
1614 oberon_open_scope(ctx);
1615 oberon_assert_token(ctx, PROCEDURE);
1616 oberon_opt_formal_pars(ctx, type);
1617 oberon_close_scope(ctx -> decl);
1619 else
1621 oberon_error(ctx, "invalid type declaration");
1625 static void
1626 oberon_type_decl(oberon_context_t * ctx)
1628 char * name;
1629 oberon_object_t * newtype;
1630 oberon_type_t * type;
1632 name = oberon_assert_ident(ctx);
1634 newtype = oberon_find_object(ctx -> decl, name, 0);
1635 if(newtype == NULL)
1637 newtype = oberon_define_object(ctx -> decl, name, OBERON_CLASS_TYPE);
1638 newtype -> type = oberon_new_type_ptr(OBERON_TYPE_VOID);
1639 assert(newtype -> type);
1641 else
1643 if(newtype -> class != OBERON_CLASS_TYPE)
1645 oberon_error(ctx, "mult definition");
1648 if(newtype -> linked)
1650 oberon_error(ctx, "mult definition - already linked");
1654 oberon_assert_token(ctx, EQUAL);
1656 type = newtype -> type;
1657 oberon_type(ctx, &type);
1659 if(type -> class == OBERON_TYPE_VOID)
1661 oberon_error(ctx, "recursive alias declaration");
1664 newtype -> type = type;
1665 newtype -> linked = 1;
1668 static void oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x);
1669 static void oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type);
1671 static void
1672 oberon_prevent_recursive_pointer(oberon_context_t * ctx, oberon_type_t * type)
1674 if(type -> class != OBERON_TYPE_POINTER
1675 && type -> class != OBERON_TYPE_ARRAY)
1677 return;
1680 if(type -> recursive)
1682 oberon_error(ctx, "recursive pointer declaration");
1685 if(type -> base -> class == OBERON_TYPE_POINTER)
1687 oberon_error(ctx, "attempt to make pointer to pointer");
1690 type -> recursive = 1;
1692 oberon_prevent_recursive_pointer(ctx, type -> base);
1694 type -> recursive = 0;
1697 static void
1698 oberon_prevent_recursive_record(oberon_context_t * ctx, oberon_type_t * type)
1700 if(type -> class != OBERON_TYPE_RECORD)
1702 return;
1705 if(type -> recursive)
1707 oberon_error(ctx, "recursive record declaration");
1710 type -> recursive = 1;
1712 int num_fields = type -> num_decl;
1713 oberon_object_t * field = type -> decl;
1714 for(int i = 0; i < num_fields; i++)
1716 oberon_prevent_recursive_object(ctx, field);
1717 field = field -> next;
1720 type -> recursive = 0;
1722 static void
1723 oberon_prevent_recursive_procedure(oberon_context_t * ctx, oberon_type_t * type)
1725 if(type -> class != OBERON_TYPE_PROCEDURE)
1727 return;
1730 if(type -> recursive)
1732 oberon_error(ctx, "recursive procedure declaration");
1735 type -> recursive = 1;
1737 int num_fields = type -> num_decl;
1738 oberon_object_t * field = type -> decl;
1739 for(int i = 0; i < num_fields; i++)
1741 oberon_prevent_recursive_object(ctx, field);
1742 field = field -> next;
1745 type -> recursive = 0;
1748 static void
1749 oberon_prevent_recursive_array(oberon_context_t * ctx, oberon_type_t * type)
1751 if(type -> class != OBERON_TYPE_ARRAY)
1753 return;
1756 if(type -> recursive)
1758 oberon_error(ctx, "recursive array declaration");
1761 type -> recursive = 1;
1763 oberon_prevent_recursive_type(ctx, type -> base);
1765 type -> recursive = 0;
1768 static void
1769 oberon_prevent_recursive_type(oberon_context_t * ctx, oberon_type_t * type)
1771 if(type -> class == OBERON_TYPE_POINTER)
1773 oberon_prevent_recursive_pointer(ctx, type);
1775 else if(type -> class == OBERON_TYPE_RECORD)
1777 oberon_prevent_recursive_record(ctx, type);
1779 else if(type -> class == OBERON_TYPE_ARRAY)
1781 oberon_prevent_recursive_array(ctx, type);
1783 else if(type -> class == OBERON_TYPE_PROCEDURE)
1785 oberon_prevent_recursive_procedure(ctx, type);
1789 static void
1790 oberon_prevent_recursive_object(oberon_context_t * ctx, oberon_object_t * x)
1792 switch(x -> class)
1794 case OBERON_CLASS_VAR:
1795 case OBERON_CLASS_TYPE:
1796 case OBERON_CLASS_PARAM:
1797 case OBERON_CLASS_VAR_PARAM:
1798 case OBERON_CLASS_FIELD:
1799 oberon_prevent_recursive_type(ctx, x -> type);
1800 break;
1801 case OBERON_CLASS_CONST:
1802 case OBERON_CLASS_PROC:
1803 break;
1804 default:
1805 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
1806 break;
1810 static void
1811 oberon_prevent_recursive_decl(oberon_context_t * ctx)
1813 oberon_object_t * x = ctx -> decl -> list -> next;
1815 while(x)
1817 oberon_prevent_recursive_object(ctx, x);
1818 x = x -> next;
1822 static void oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x);
1823 static void oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type);
1825 static void
1826 oberon_initialize_record_fields(oberon_context_t * ctx, oberon_type_t * type)
1828 if(type -> class != OBERON_TYPE_RECORD)
1830 return;
1833 int num_fields = type -> num_decl;
1834 oberon_object_t * field = type -> decl;
1835 for(int i = 0; i < num_fields; i++)
1837 if(field -> type -> class == OBERON_TYPE_POINTER)
1839 oberon_initialize_type(ctx, field -> type);
1842 oberon_initialize_object(ctx, field);
1843 field = field -> next;
1846 oberon_generator_init_record(ctx, type);
1849 static void
1850 oberon_initialize_type(oberon_context_t * ctx, oberon_type_t * type)
1852 if(type -> class == OBERON_TYPE_VOID)
1854 oberon_error(ctx, "undeclarated type");
1857 if(type -> initialized)
1859 return;
1862 type -> initialized = 1;
1864 if(type -> class == OBERON_TYPE_POINTER)
1866 oberon_initialize_type(ctx, type -> base);
1867 oberon_generator_init_type(ctx, type);
1869 else if(type -> class == OBERON_TYPE_ARRAY)
1871 oberon_initialize_type(ctx, type -> base);
1872 oberon_generator_init_type(ctx, type);
1874 else if(type -> class == OBERON_TYPE_RECORD)
1876 oberon_generator_init_type(ctx, type);
1877 oberon_initialize_record_fields(ctx, type);
1879 else if(type -> class == OBERON_TYPE_PROCEDURE)
1881 int num_fields = type -> num_decl;
1882 oberon_object_t * field = type -> decl;
1883 for(int i = 0; i < num_fields; i++)
1885 oberon_initialize_object(ctx, field);
1886 field = field -> next;
1887 }
1889 oberon_generator_init_type(ctx, type);
1891 else
1893 oberon_generator_init_type(ctx, type);
1897 static void
1898 oberon_initialize_object(oberon_context_t * ctx, oberon_object_t * x)
1900 printf("oberon_initialize_object: name %s class %i\n", x -> name, x -> class);
1901 switch(x -> class)
1903 case OBERON_CLASS_TYPE:
1904 oberon_initialize_type(ctx, x -> type);
1905 break;
1906 case OBERON_CLASS_VAR:
1907 case OBERON_CLASS_PARAM:
1908 case OBERON_CLASS_VAR_PARAM:
1909 case OBERON_CLASS_FIELD:
1910 oberon_initialize_type(ctx, x -> type);
1911 oberon_generator_init_var(ctx, x);
1912 break;
1913 case OBERON_CLASS_CONST:
1914 case OBERON_CLASS_PROC:
1915 break;
1916 default:
1917 oberon_error(ctx, "oberon_prevent_recursive_object: wat");
1918 break;
1922 static void
1923 oberon_initialize_decl(oberon_context_t * ctx)
1925 oberon_object_t * x = ctx -> decl -> list;
1927 while(x -> next)
1929 oberon_initialize_object(ctx, x -> next);
1930 x = x -> next;
1931 }
1934 static void
1935 oberon_decl_seq(oberon_context_t * ctx)
1937 if(ctx -> token == CONST)
1939 oberon_assert_token(ctx, CONST);
1940 while(ctx -> token == IDENT)
1942 oberon_const_decl(ctx);
1943 oberon_assert_token(ctx, SEMICOLON);
1947 if(ctx -> token == TYPE)
1949 oberon_assert_token(ctx, TYPE);
1950 while(ctx -> token == IDENT)
1952 oberon_type_decl(ctx);
1953 oberon_assert_token(ctx, SEMICOLON);
1957 if(ctx -> token == VAR)
1959 oberon_assert_token(ctx, VAR);
1960 while(ctx -> token == IDENT)
1962 oberon_var_decl(ctx);
1963 oberon_assert_token(ctx, SEMICOLON);
1967 oberon_prevent_recursive_decl(ctx);
1968 oberon_initialize_decl(ctx);
1970 while(ctx -> token == PROCEDURE)
1972 oberon_proc_decl(ctx);
1973 oberon_assert_token(ctx, SEMICOLON);
1977 static void
1978 oberon_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
1980 oberon_autocast_to(ctx, src, dst -> result);
1981 oberon_generate_assign(ctx, src, dst);
1984 static void
1985 oberon_make_call(oberon_context_t * ctx, oberon_expr_t * desig)
1987 oberon_autocast_call(ctx, desig);
1988 oberon_generate_call_proc(ctx, desig);
1991 static void
1992 oberon_statement(oberon_context_t * ctx)
1994 oberon_expr_t * item1;
1995 oberon_expr_t * item2;
1997 if(ctx -> token == IDENT)
1999 item1 = oberon_designator(ctx);
2000 if(ctx -> token == ASSIGN)
2002 oberon_assert_token(ctx, ASSIGN);
2003 item2 = oberon_expr(ctx);
2004 oberon_assign(ctx, item2, item1);
2006 else
2008 item1 = oberon_opt_proc_parens(ctx, item1);
2009 oberon_make_call(ctx, item1);
2012 else if(ctx -> token == RETURN)
2014 oberon_assert_token(ctx, RETURN);
2015 if(ISEXPR(ctx -> token))
2017 oberon_expr_t * expr;
2018 expr = oberon_expr(ctx);
2019 oberon_make_return(ctx, expr);
2021 else
2023 oberon_make_return(ctx, NULL);
2028 static void
2029 oberon_statement_seq(oberon_context_t * ctx)
2031 oberon_statement(ctx);
2032 while(ctx -> token == SEMICOLON)
2034 oberon_assert_token(ctx, SEMICOLON);
2035 oberon_statement(ctx);
2039 static void
2040 oberon_parse_module(oberon_context_t * ctx)
2042 char *name1, *name2;
2043 oberon_read_token(ctx);
2045 oberon_assert_token(ctx, MODULE);
2046 name1 = oberon_assert_ident(ctx);
2047 oberon_assert_token(ctx, SEMICOLON);
2048 ctx -> mod -> name = name1;
2050 oberon_decl_seq(ctx);
2052 if(ctx -> token == BEGIN)
2054 oberon_assert_token(ctx, BEGIN);
2055 oberon_generate_begin_module(ctx);
2056 oberon_statement_seq(ctx);
2057 oberon_generate_end_module(ctx);
2060 oberon_assert_token(ctx, END);
2061 name2 = oberon_assert_ident(ctx);
2062 oberon_assert_token(ctx, DOT);
2064 if(strcmp(name1, name2) != 0)
2066 oberon_error(ctx, "module name not matched");
2070 // =======================================================================
2071 // LIBRARY
2072 // =======================================================================
2074 static void
2075 register_default_types(oberon_context_t * ctx)
2077 ctx -> void_type = oberon_new_type_ptr(OBERON_TYPE_VOID);
2078 oberon_generator_init_type(ctx, ctx -> void_type);
2080 ctx -> void_ptr_type = oberon_new_type_ptr(OBERON_TYPE_POINTER);
2081 ctx -> void_ptr_type -> base = ctx -> void_type;
2082 oberon_generator_init_type(ctx, ctx -> void_ptr_type);
2084 ctx -> int_type = oberon_new_type_integer(sizeof(int));
2085 oberon_define_type(ctx -> world_scope, "INTEGER", ctx -> int_type);
2087 ctx -> bool_type = oberon_new_type_boolean(sizeof(int));
2088 oberon_define_type(ctx -> world_scope, "BOOLEAN", ctx -> bool_type);
2091 oberon_context_t *
2092 oberon_create_context()
2094 oberon_context_t * ctx = malloc(sizeof *ctx);
2095 memset(ctx, 0, sizeof *ctx);
2097 oberon_scope_t * world_scope;
2098 world_scope = oberon_open_scope(ctx);
2099 ctx -> world_scope = world_scope;
2101 oberon_generator_init_context(ctx);
2103 register_default_types(ctx);
2105 return ctx;
2108 void
2109 oberon_destroy_context(oberon_context_t * ctx)
2111 oberon_generator_destroy_context(ctx);
2112 free(ctx);
2115 oberon_module_t *
2116 oberon_compile_module(oberon_context_t * ctx, const char * code)
2118 oberon_module_t * mod = malloc(sizeof *mod);
2119 memset(mod, 0, sizeof *mod);
2120 ctx -> mod = mod;
2122 oberon_scope_t * module_scope;
2123 module_scope = oberon_open_scope(ctx);
2124 mod -> decl = module_scope;
2126 oberon_init_scaner(ctx, code);
2127 oberon_parse_module(ctx);
2129 oberon_generate_code(ctx);
2131 ctx -> mod = NULL;
2132 return mod;