9 #include "../include/oberon.h"
11 #include "oberon-internals.h"
12 #include "generator.h"
63 // =======================================================================
65 // =======================================================================
68 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
72 fprintf(stderr
, "error: ");
73 vfprintf(stderr
, fmt
, ptr
);
74 fprintf(stderr
, "\n");
75 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
76 fprintf(stderr
, " c = %c\n", ctx
-> c
);
77 fprintf(stderr
, " token = %i\n", ctx
-> token
);
82 static oberon_type_t
*
83 oberon_new_type_ptr(int class)
85 oberon_type_t
* x
= malloc(sizeof *x
);
86 memset(x
, 0, sizeof *x
);
91 static oberon_type_t
*
92 oberon_new_type_integer(int size
)
95 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
100 static oberon_type_t
*
101 oberon_new_type_boolean(int size
)
104 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
109 static oberon_type_t
*
110 oberon_new_type_real(int size
)
113 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
118 // =======================================================================
120 // =======================================================================
122 static oberon_scope_t
*
123 oberon_open_scope(oberon_context_t
* ctx
)
125 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
126 oberon_object_t
* list
= calloc(1, sizeof *list
);
129 scope
-> list
= list
;
130 scope
-> up
= ctx
-> decl
;
134 scope
-> parent
= scope
-> up
-> parent
;
135 scope
-> local
= scope
-> up
-> local
;
143 oberon_close_scope(oberon_scope_t
* scope
)
145 oberon_context_t
* ctx
= scope
-> ctx
;
146 ctx
-> decl
= scope
-> up
;
149 static oberon_object_t
*
150 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, int export
, int read_only
)
152 oberon_object_t
* x
= scope
-> list
;
153 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
160 oberon_error(scope
-> ctx
, "already defined");
163 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
164 memset(newvar
, 0, sizeof *newvar
);
165 newvar
-> name
= name
;
166 newvar
-> class = class;
167 newvar
-> export
= export
;
168 newvar
-> read_only
= read_only
;
169 newvar
-> local
= scope
-> local
;
170 newvar
-> parent
= scope
-> parent
;
171 newvar
-> module
= scope
-> ctx
-> mod
;
178 static oberon_object_t
*
179 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
181 oberon_object_t
* x
= list
;
182 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
189 static oberon_object_t
*
190 oberon_find_object(oberon_scope_t
* scope
, char * name
, int check_it
)
192 oberon_object_t
* result
= NULL
;
194 oberon_scope_t
* s
= scope
;
195 while(result
== NULL
&& s
!= NULL
)
197 result
= oberon_find_object_in_list(s
-> list
, name
);
201 if(check_it
&& result
== NULL
)
203 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
209 static oberon_object_t
*
210 oberon_find_field(oberon_context_t
* ctx
, oberon_type_t
* rec
, char * name
)
212 oberon_object_t
* x
= rec
-> decl
;
213 for(int i
= 0; i
< rec
-> num_decl
; i
++)
215 if(strcmp(x
-> name
, name
) == 0)
222 oberon_error(ctx
, "field not defined");
227 static oberon_object_t
*
228 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
230 oberon_object_t
* id
;
231 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, 0);
233 oberon_generator_init_type(scope
-> ctx
, type
);
237 // =======================================================================
239 // =======================================================================
242 oberon_get_char(oberon_context_t
* ctx
)
244 if(ctx
-> code
[ctx
-> code_index
])
246 ctx
-> code_index
+= 1;
247 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
252 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
255 ctx
-> code_index
= 0;
256 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
260 oberon_read_ident(oberon_context_t
* ctx
)
263 int i
= ctx
-> code_index
;
265 int c
= ctx
-> code
[i
];
273 char * ident
= malloc(len
+ 1);
274 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
277 ctx
-> code_index
= i
;
278 ctx
-> c
= ctx
-> code
[i
];
279 ctx
-> string
= ident
;
280 ctx
-> token
= IDENT
;
282 if(strcmp(ident
, "MODULE") == 0)
284 ctx
-> token
= MODULE
;
286 else if(strcmp(ident
, "END") == 0)
290 else if(strcmp(ident
, "VAR") == 0)
294 else if(strcmp(ident
, "BEGIN") == 0)
296 ctx
-> token
= BEGIN
;
298 else if(strcmp(ident
, "TRUE") == 0)
302 else if(strcmp(ident
, "FALSE") == 0)
304 ctx
-> token
= FALSE
;
306 else if(strcmp(ident
, "OR") == 0)
310 else if(strcmp(ident
, "DIV") == 0)
314 else if(strcmp(ident
, "MOD") == 0)
318 else if(strcmp(ident
, "PROCEDURE") == 0)
320 ctx
-> token
= PROCEDURE
;
322 else if(strcmp(ident
, "RETURN") == 0)
324 ctx
-> token
= RETURN
;
326 else if(strcmp(ident
, "CONST") == 0)
328 ctx
-> token
= CONST
;
330 else if(strcmp(ident
, "TYPE") == 0)
334 else if(strcmp(ident
, "ARRAY") == 0)
336 ctx
-> token
= ARRAY
;
338 else if(strcmp(ident
, "OF") == 0)
342 else if(strcmp(ident
, "RECORD") == 0)
344 ctx
-> token
= RECORD
;
346 else if(strcmp(ident
, "POINTER") == 0)
348 ctx
-> token
= POINTER
;
350 else if(strcmp(ident
, "TO") == 0)
354 else if(strcmp(ident
, "NIL") == 0)
358 else if(strcmp(ident
, "IMPORT") == 0)
360 ctx
-> token
= IMPORT
;
365 oberon_read_number(oberon_context_t
* ctx
)
378 * mode = 3 == LONGREAL
381 start_i
= ctx
-> code_index
;
383 while(isdigit(ctx
-> c
))
385 oberon_get_char(ctx
);
388 end_i
= ctx
-> code_index
;
390 if(isxdigit(ctx
-> c
))
393 while(isxdigit(ctx
-> c
))
395 oberon_get_char(ctx
);
398 end_i
= ctx
-> code_index
;
402 oberon_error(ctx
, "invalid hex number");
404 oberon_get_char(ctx
);
406 else if(ctx
-> c
== '.')
409 oberon_get_char(ctx
);
411 while(isdigit(ctx
-> c
))
413 oberon_get_char(ctx
);
416 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
418 exp_i
= ctx
-> code_index
;
425 oberon_get_char(ctx
);
427 if(ctx
-> c
== '+' || ctx
-> c
== '-')
429 oberon_get_char(ctx
);
432 while(isdigit(ctx
-> c
))
434 oberon_get_char(ctx
);
439 end_i
= ctx
-> code_index
;
442 int len
= end_i
- start_i
;
443 ident
= malloc(len
+ 1);
444 memcpy(ident
, &ctx
-> code
[start_i
], len
);
449 int i
= exp_i
- start_i
;
456 integer
= atol(ident
);
458 ctx
-> token
= INTEGER
;
461 sscanf(ident
, "%lx", &integer
);
463 ctx
-> token
= INTEGER
;
467 sscanf(ident
, "%lf", &real
);
471 oberon_error(ctx
, "oberon_read_number: wat");
475 ctx
-> string
= ident
;
476 ctx
-> integer
= integer
;
481 oberon_skip_space(oberon_context_t
* ctx
)
483 while(isspace(ctx
-> c
))
485 oberon_get_char(ctx
);
490 oberon_read_comment(oberon_context_t
* ctx
)
497 oberon_get_char(ctx
);
500 oberon_get_char(ctx
);
504 else if(ctx
-> c
== '*')
506 oberon_get_char(ctx
);
509 oberon_get_char(ctx
);
513 else if(ctx
-> c
== 0)
515 oberon_error(ctx
, "unterminated comment");
519 oberon_get_char(ctx
);
524 static void oberon_read_token(oberon_context_t
* ctx
);
527 oberon_read_symbol(oberon_context_t
* ctx
)
536 ctx
-> token
= SEMICOLON
;
537 oberon_get_char(ctx
);
540 ctx
-> token
= COLON
;
541 oberon_get_char(ctx
);
544 ctx
-> token
= ASSIGN
;
545 oberon_get_char(ctx
);
550 oberon_get_char(ctx
);
553 ctx
-> token
= LPAREN
;
554 oberon_get_char(ctx
);
557 oberon_get_char(ctx
);
558 oberon_read_comment(ctx
);
559 oberon_read_token(ctx
);
563 ctx
-> token
= RPAREN
;
564 oberon_get_char(ctx
);
567 ctx
-> token
= EQUAL
;
568 oberon_get_char(ctx
);
572 oberon_get_char(ctx
);
576 oberon_get_char(ctx
);
580 oberon_get_char(ctx
);
584 ctx
-> token
= GREAT
;
585 oberon_get_char(ctx
);
589 oberon_get_char(ctx
);
594 oberon_get_char(ctx
);
597 ctx
-> token
= MINUS
;
598 oberon_get_char(ctx
);
602 oberon_get_char(ctx
);
605 oberon_get_char(ctx
);
606 oberon_error(ctx
, "unstarted comment");
610 ctx
-> token
= SLASH
;
611 oberon_get_char(ctx
);
615 oberon_get_char(ctx
);
619 oberon_get_char(ctx
);
622 ctx
-> token
= COMMA
;
623 oberon_get_char(ctx
);
626 ctx
-> token
= LBRACE
;
627 oberon_get_char(ctx
);
630 ctx
-> token
= RBRACE
;
631 oberon_get_char(ctx
);
634 ctx
-> token
= UPARROW
;
635 oberon_get_char(ctx
);
638 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
644 oberon_read_token(oberon_context_t
* ctx
)
646 oberon_skip_space(ctx
);
651 oberon_read_ident(ctx
);
655 oberon_read_number(ctx
);
659 oberon_read_symbol(ctx
);
663 // =======================================================================
665 // =======================================================================
667 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
668 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
669 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
670 static char * oberon_assert_ident(oberon_context_t
* ctx
);
671 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
672 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
674 static oberon_expr_t
*
675 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
677 oberon_oper_t
* operator;
678 operator = malloc(sizeof *operator);
679 memset(operator, 0, sizeof *operator);
681 operator -> is_item
= 0;
682 operator -> result
= result
;
683 operator -> read_only
= 1;
685 operator -> left
= left
;
686 operator -> right
= right
;
688 return (oberon_expr_t
*) operator;
691 static oberon_expr_t
*
692 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
694 oberon_item_t
* item
;
695 item
= malloc(sizeof *item
);
696 memset(item
, 0, sizeof *item
);
699 item
-> result
= result
;
700 item
-> read_only
= read_only
;
703 return (oberon_expr_t
*)item
;
706 static oberon_expr_t
*
707 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
709 oberon_expr_t
* expr
;
710 oberon_type_t
* result
;
712 result
= a
-> result
;
716 if(result
-> class != OBERON_TYPE_INTEGER
)
718 oberon_error(ctx
, "incompatible operator type");
721 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
723 else if(token
== NOT
)
725 if(result
-> class != OBERON_TYPE_BOOLEAN
)
727 oberon_error(ctx
, "incompatible operator type");
730 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
734 oberon_error(ctx
, "oberon_make_unary_op: wat");
741 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
743 oberon_expr_t
* last
;
746 *first
= last
= oberon_expr(ctx
);
747 while(ctx
-> token
== COMMA
)
749 oberon_assert_token(ctx
, COMMA
);
750 oberon_expr_t
* current
;
754 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
758 current
= oberon_expr(ctx
);
761 last
-> next
= current
;
767 static oberon_expr_t
*
768 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
770 if(pref
-> class != expr
-> result
-> class)
772 if(pref
-> class != OBERON_TYPE_PROCEDURE
)
774 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
776 oberon_error(ctx
, "incompatible types");
781 if(pref
-> class == OBERON_TYPE_INTEGER
)
783 if(expr
-> result
-> class > pref
-> class)
785 oberon_error(ctx
, "incompatible size");
788 else if(pref
-> class == OBERON_TYPE_RECORD
)
790 if(expr
-> result
!= pref
)
792 printf("oberon_autocast_to: rec %p != %p\n", expr
-> result
, pref
);
793 oberon_error(ctx
, "incompatible record types");
796 else if(pref
-> class == OBERON_TYPE_POINTER
)
798 if(expr
-> result
-> base
!= pref
-> base
)
800 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
802 oberon_error(ctx
, "incompatible pointer types");
813 oberon_autocast_call(oberon_context_t
* ctx
, oberon_expr_t
* desig
)
815 if(desig
-> is_item
== 0)
817 oberon_error(ctx
, "expected item");
820 if(desig
-> item
.mode
!= MODE_CALL
)
822 oberon_error(ctx
, "expected mode CALL");
825 if(desig
-> item
.var
-> type
-> class != OBERON_TYPE_PROCEDURE
)
827 oberon_error(ctx
, "only procedures can be called");
830 oberon_type_t
* fn
= desig
-> item
.var
-> type
;
831 int num_args
= desig
-> item
.num_args
;
832 int num_decl
= fn
-> num_decl
;
834 if(num_args
< num_decl
)
836 oberon_error(ctx
, "too few arguments");
838 else if(num_args
> num_decl
)
840 oberon_error(ctx
, "too many arguments");
843 oberon_expr_t
* arg
= desig
-> item
.args
;
844 oberon_object_t
* param
= fn
-> decl
;
845 for(int i
= 0; i
< num_args
; i
++)
847 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
851 oberon_error(ctx
, "assign to read-only var");
856 // switch(arg -> item.mode)
861 // // Допустимо разыменование?
862 // //case MODE_DEREF:
865 // oberon_error(ctx, "var-parameter accept only variables");
870 oberon_autocast_to(ctx
, arg
, param
-> type
);
872 param
= param
-> next
;
876 static oberon_expr_t
*
877 oberon_make_call_func(oberon_context_t
* ctx
, oberon_object_t
* proc
, int num_args
, oberon_expr_t
* list_args
)
879 switch(proc
-> class)
881 case OBERON_CLASS_PROC
:
882 if(proc
-> class != OBERON_CLASS_PROC
)
884 oberon_error(ctx
, "not a procedure");
887 case OBERON_CLASS_VAR
:
888 case OBERON_CLASS_VAR_PARAM
:
889 case OBERON_CLASS_PARAM
:
890 if(proc
-> type
-> class != OBERON_TYPE_PROCEDURE
)
892 oberon_error(ctx
, "not a procedure");
896 oberon_error(ctx
, "not a procedure");
900 oberon_expr_t
* call
;
904 if(proc
-> genfunc
== NULL
)
906 oberon_error(ctx
, "not a function-procedure");
909 call
= proc
-> genfunc(ctx
, num_args
, list_args
);
913 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
)
915 oberon_error(ctx
, "attempt to call procedure in expression");
918 call
= oberon_new_item(MODE_CALL
, proc
-> type
-> base
, 1);
919 call
-> item
.var
= proc
;
920 call
-> item
.num_args
= num_args
;
921 call
-> item
.args
= list_args
;
922 oberon_autocast_call(ctx
, call
);
929 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
, int num_args
, oberon_expr_t
* list_args
)
931 switch(proc
-> class)
933 case OBERON_CLASS_PROC
:
934 if(proc
-> class != OBERON_CLASS_PROC
)
936 oberon_error(ctx
, "not a procedure");
939 case OBERON_CLASS_VAR
:
940 case OBERON_CLASS_VAR_PARAM
:
941 case OBERON_CLASS_PARAM
:
942 if(proc
-> type
-> class != OBERON_TYPE_PROCEDURE
)
944 oberon_error(ctx
, "not a procedure");
948 oberon_error(ctx
, "not a procedure");
954 if(proc
-> genproc
== NULL
)
956 oberon_error(ctx
, "requres non-typed procedure");
959 proc
-> genproc(ctx
, num_args
, list_args
);
963 if(proc
-> type
-> base
-> class != OBERON_TYPE_VOID
)
965 oberon_error(ctx
, "attempt to call function as non-typed procedure");
968 oberon_expr_t
* call
;
969 call
= oberon_new_item(MODE_CALL
, proc
-> type
-> base
, 1);
970 call
-> item
.var
= proc
;
971 call
-> item
.num_args
= num_args
;
972 call
-> item
.args
= list_args
;
973 oberon_autocast_call(ctx
, call
);
974 oberon_generate_call_proc(ctx
, call
);
982 || ((x) == INTEGER) \
988 static oberon_expr_t
*
989 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
991 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
993 oberon_error(ctx
, "not a pointer");
996 assert(expr
-> is_item
);
998 oberon_expr_t
* selector
;
999 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1000 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1005 static oberon_expr_t
*
1006 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1008 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1010 desig
= oberno_make_dereferencing(ctx
, desig
);
1013 assert(desig
-> is_item
);
1015 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1017 oberon_error(ctx
, "not array");
1020 oberon_type_t
* base
;
1021 base
= desig
-> result
-> base
;
1023 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1025 oberon_error(ctx
, "index must be integer");
1028 // Статическая проверка границ массива
1029 if(desig
-> result
-> size
!= 0)
1031 if(index
-> is_item
)
1033 if(index
-> item
.mode
== MODE_INTEGER
)
1035 int arr_size
= desig
-> result
-> size
;
1036 int index_int
= index
-> item
.integer
;
1037 if(index_int
< 0 || index_int
> arr_size
- 1)
1039 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1045 oberon_expr_t
* selector
;
1046 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1047 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1048 selector
-> item
.num_args
= 1;
1049 selector
-> item
.args
= index
;
1054 static oberon_expr_t
*
1055 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1057 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1059 expr
= oberno_make_dereferencing(ctx
, expr
);
1062 assert(expr
-> is_item
== 1);
1064 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1066 oberon_error(ctx
, "not record");
1069 oberon_type_t
* rec
= expr
-> result
;
1071 oberon_object_t
* field
;
1072 field
= oberon_find_field(ctx
, rec
, name
);
1074 if(field
-> export
== 0)
1076 if(field
-> module
!= ctx
-> mod
)
1078 oberon_error(ctx
, "field not exported");
1083 if(field
-> read_only
)
1085 if(field
-> module
!= ctx
-> mod
)
1091 oberon_expr_t
* selector
;
1092 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1093 selector
-> item
.var
= field
;
1094 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1099 #define ISSELECTOR(x) \
1102 || ((x) == UPARROW))
1104 static oberon_object_t
*
1105 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1108 oberon_object_t
* x
;
1110 name
= oberon_assert_ident(ctx
);
1111 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1115 if(x
-> class == OBERON_CLASS_MODULE
)
1117 oberon_assert_token(ctx
, DOT
);
1118 name
= oberon_assert_ident(ctx
);
1119 /* Наличие объектов в левых модулях всегда проверяется */
1120 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1122 if(x
-> export
== 0)
1124 oberon_error(ctx
, "not exported");
1137 static oberon_expr_t
*
1138 oberon_designator(oberon_context_t
* ctx
)
1141 oberon_object_t
* var
;
1142 oberon_expr_t
* expr
;
1144 var
= oberon_qualident(ctx
, NULL
, 1);
1147 if(var
-> read_only
)
1149 if(var
-> module
!= ctx
-> mod
)
1155 switch(var
-> class)
1157 case OBERON_CLASS_CONST
:
1159 expr
= (oberon_expr_t
*) var
-> value
;
1161 case OBERON_CLASS_VAR
:
1162 case OBERON_CLASS_VAR_PARAM
:
1163 case OBERON_CLASS_PARAM
:
1164 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1166 case OBERON_CLASS_PROC
:
1167 expr
= oberon_new_item(MODE_VAR
, var
-> type
, 1);
1170 oberon_error(ctx
, "invalid designator");
1173 expr
-> item
.var
= var
;
1175 while(ISSELECTOR(ctx
-> token
))
1177 switch(ctx
-> token
)
1180 oberon_assert_token(ctx
, DOT
);
1181 name
= oberon_assert_ident(ctx
);
1182 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1185 oberon_assert_token(ctx
, LBRACE
);
1186 int num_indexes
= 0;
1187 oberon_expr_t
* indexes
= NULL
;
1188 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1189 oberon_assert_token(ctx
, RBRACE
);
1191 for(int i
= 0; i
< num_indexes
; i
++)
1193 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1194 indexes
= indexes
-> next
;
1198 oberon_assert_token(ctx
, UPARROW
);
1199 expr
= oberno_make_dereferencing(ctx
, expr
);
1202 oberon_error(ctx
, "oberon_designator: wat");
1209 static oberon_expr_t
*
1210 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1212 assert(expr
-> is_item
== 1);
1214 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1215 if(ctx
-> token
== LPAREN
)
1217 oberon_assert_token(ctx
, LPAREN
);
1220 oberon_expr_t
* arguments
= NULL
;
1222 if(ISEXPR(ctx
-> token
))
1224 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1227 expr
= oberon_make_call_func(ctx
, expr
-> item
.var
, num_args
, arguments
);
1229 oberon_assert_token(ctx
, RPAREN
);
1236 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1238 assert(expr
-> is_item
== 1);
1241 oberon_expr_t
* arguments
= NULL
;
1243 if(ctx
-> token
== LPAREN
)
1245 oberon_assert_token(ctx
, LPAREN
);
1247 if(ISEXPR(ctx
-> token
))
1249 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1252 oberon_assert_token(ctx
, RPAREN
);
1255 /* Вызов происходит даже без скобок */
1256 oberon_make_call_proc(ctx
, expr
-> item
.var
, num_args
, arguments
);
1259 static oberon_expr_t
*
1260 oberon_factor(oberon_context_t
* ctx
)
1262 oberon_expr_t
* expr
;
1264 switch(ctx
-> token
)
1267 expr
= oberon_designator(ctx
);
1268 expr
= oberon_opt_func_parens(ctx
, expr
);
1271 expr
= oberon_new_item(MODE_INTEGER
, ctx
-> int_type
, 1);
1272 expr
-> item
.integer
= ctx
-> integer
;
1273 oberon_assert_token(ctx
, INTEGER
);
1276 expr
= oberon_new_item(MODE_REAL
, ctx
-> real_type
, 1);
1277 expr
-> item
.real
= ctx
-> real
;
1278 oberon_assert_token(ctx
, REAL
);
1281 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, 1);
1282 expr
-> item
.boolean
= 1;
1283 oberon_assert_token(ctx
, TRUE
);
1286 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, 1);
1287 expr
-> item
.boolean
= 0;
1288 oberon_assert_token(ctx
, FALSE
);
1291 oberon_assert_token(ctx
, LPAREN
);
1292 expr
= oberon_expr(ctx
);
1293 oberon_assert_token(ctx
, RPAREN
);
1296 oberon_assert_token(ctx
, NOT
);
1297 expr
= oberon_factor(ctx
);
1298 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1301 oberon_assert_token(ctx
, NIL
);
1302 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, 1);
1305 oberon_error(ctx
, "invalid expression");
1312 * oberon_autocast_binary_op автоматически переобразовывеат тип по след. правилам:
1313 * 1. Классы обоих типов должны быть одинаковы
1314 * 2. В качестве результата должен быть выбран больший тип.
1315 * 3. Если размер результат не должен быть меньше чем базовый int
1319 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
, oberon_type_t
** result
)
1321 if((a
-> class) != (b
-> class))
1323 oberon_error(ctx
, "incompatible types");
1326 if((a
-> size
) > (b
-> size
))
1335 if(((*result
) -> class) == OBERON_TYPE_INTEGER
)
1337 if(((*result
) -> size
) < (ctx
-> int_type
-> size
))
1339 *result
= ctx
-> int_type
;
1343 /* TODO: cast types */
1346 #define ITMAKESBOOLEAN(x) \
1347 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1349 #define ITUSEONLYINTEGER(x) \
1350 ((x) >= LESS && (x) <= GEQ)
1352 #define ITUSEONLYBOOLEAN(x) \
1353 (((x) == OR) || ((x) == AND))
1355 static oberon_expr_t
*
1356 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1358 oberon_expr_t
* expr
;
1359 oberon_type_t
* result
;
1361 if(ITMAKESBOOLEAN(token
))
1363 if(ITUSEONLYINTEGER(token
))
1365 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1366 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1368 oberon_error(ctx
, "used only with integer types");
1371 else if(ITUSEONLYBOOLEAN(token
))
1373 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1374 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1376 oberon_error(ctx
, "used only with boolean type");
1380 result
= ctx
-> bool_type
;
1384 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1386 else if(token
== NEQ
)
1388 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1390 else if(token
== LESS
)
1392 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1394 else if(token
== LEQ
)
1396 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1398 else if(token
== GREAT
)
1400 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1402 else if(token
== GEQ
)
1404 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1406 else if(token
== OR
)
1408 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1410 else if(token
== AND
)
1412 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1416 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1419 else if(token
== SLASH
)
1421 if(a
-> result
-> class != OBERON_TYPE_REAL
)
1423 if(a
-> result
-> class == OBERON_TYPE_INTEGER
)
1425 oberon_error(ctx
, "TODO cast int -> real");
1429 oberon_error(ctx
, "operator / requires numeric type");
1433 if(b
-> result
-> class != OBERON_TYPE_REAL
)
1435 if(b
-> result
-> class == OBERON_TYPE_INTEGER
)
1437 oberon_error(ctx
, "TODO cast int -> real");
1441 oberon_error(ctx
, "operator / requires numeric type");
1445 oberon_autocast_binary_op(ctx
, a
-> result
, b
-> result
, &result
);
1446 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
1448 else if(token
== DIV
)
1450 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1451 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1453 oberon_error(ctx
, "operator DIV requires integer type");
1456 oberon_autocast_binary_op(ctx
, a
-> result
, b
-> result
, &result
);
1457 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
1461 oberon_autocast_binary_op(ctx
, a
-> result
, b
-> result
, &result
);
1465 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
1467 else if(token
== MINUS
)
1469 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
1471 else if(token
== STAR
)
1473 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
1475 else if(token
== MOD
)
1477 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
1481 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1488 #define ISMULOP(x) \
1489 ((x) >= STAR && (x) <= AND)
1491 static oberon_expr_t
*
1492 oberon_term_expr(oberon_context_t
* ctx
)
1494 oberon_expr_t
* expr
;
1496 expr
= oberon_factor(ctx
);
1497 while(ISMULOP(ctx
-> token
))
1499 int token
= ctx
-> token
;
1500 oberon_read_token(ctx
);
1502 oberon_expr_t
* inter
= oberon_factor(ctx
);
1503 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1509 #define ISADDOP(x) \
1510 ((x) >= PLUS && (x) <= OR)
1512 static oberon_expr_t
*
1513 oberon_simple_expr(oberon_context_t
* ctx
)
1515 oberon_expr_t
* expr
;
1518 if(ctx
-> token
== PLUS
)
1521 oberon_assert_token(ctx
, PLUS
);
1523 else if(ctx
-> token
== MINUS
)
1526 oberon_assert_token(ctx
, MINUS
);
1529 expr
= oberon_term_expr(ctx
);
1530 while(ISADDOP(ctx
-> token
))
1532 int token
= ctx
-> token
;
1533 oberon_read_token(ctx
);
1535 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1536 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1541 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1547 #define ISRELATION(x) \
1548 ((x) >= EQUAL && (x) <= GEQ)
1550 static oberon_expr_t
*
1551 oberon_expr(oberon_context_t
* ctx
)
1553 oberon_expr_t
* expr
;
1555 expr
= oberon_simple_expr(ctx
);
1556 while(ISRELATION(ctx
-> token
))
1558 int token
= ctx
-> token
;
1559 oberon_read_token(ctx
);
1561 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1562 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1568 static oberon_item_t
*
1569 oberon_const_expr(oberon_context_t
* ctx
)
1571 oberon_expr_t
* expr
;
1572 expr
= oberon_expr(ctx
);
1574 if(expr
-> is_item
== 0)
1576 oberon_error(ctx
, "const expression are required");
1579 return (oberon_item_t
*) expr
;
1582 // =======================================================================
1584 // =======================================================================
1586 static void oberon_decl_seq(oberon_context_t
* ctx
);
1587 static void oberon_statement_seq(oberon_context_t
* ctx
);
1588 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1591 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1593 if(ctx
-> token
!= token
)
1595 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
1600 oberon_assert_token(oberon_context_t
* ctx
, int token
)
1602 oberon_expect_token(ctx
, token
);
1603 oberon_read_token(ctx
);
1607 oberon_assert_ident(oberon_context_t
* ctx
)
1609 oberon_expect_token(ctx
, IDENT
);
1610 char * ident
= ctx
-> string
;
1611 oberon_read_token(ctx
);
1616 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
1618 switch(ctx
-> token
)
1621 oberon_assert_token(ctx
, STAR
);
1626 oberon_assert_token(ctx
, MINUS
);
1637 static oberon_object_t
*
1638 oberon_ident_def(oberon_context_t
* ctx
, int class)
1643 oberon_object_t
* x
;
1645 name
= oberon_assert_ident(ctx
);
1646 oberon_def(ctx
, &export
, &read_only
);
1648 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
);
1653 oberon_ident_list(oberon_context_t
* ctx
, int class, int * num
, oberon_object_t
** list
)
1656 *list
= oberon_ident_def(ctx
, class);
1657 while(ctx
-> token
== COMMA
)
1659 oberon_assert_token(ctx
, COMMA
);
1660 oberon_ident_def(ctx
, class);
1666 oberon_var_decl(oberon_context_t
* ctx
)
1669 oberon_object_t
* list
;
1670 oberon_type_t
* type
;
1671 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1673 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, &num
, &list
);
1674 oberon_assert_token(ctx
, COLON
);
1675 oberon_type(ctx
, &type
);
1677 oberon_object_t
* var
= list
;
1678 for(int i
= 0; i
< num
; i
++)
1685 static oberon_object_t
*
1686 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
1688 int class = OBERON_CLASS_PARAM
;
1689 if(ctx
-> token
== VAR
)
1691 oberon_read_token(ctx
);
1692 class = OBERON_CLASS_VAR_PARAM
;
1696 oberon_object_t
* list
;
1697 oberon_ident_list(ctx
, class, &num
, &list
);
1699 oberon_assert_token(ctx
, COLON
);
1701 oberon_type_t
* type
;
1702 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1703 oberon_type(ctx
, &type
);
1705 oberon_object_t
* param
= list
;
1706 for(int i
= 0; i
< num
; i
++)
1708 param
-> type
= type
;
1709 param
= param
-> next
;
1716 #define ISFPSECTION \
1717 ((ctx -> token == VAR) || (ctx -> token == IDENT))
1720 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
1722 oberon_assert_token(ctx
, LPAREN
);
1726 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
1727 while(ctx
-> token
== SEMICOLON
)
1729 oberon_assert_token(ctx
, SEMICOLON
);
1730 oberon_fp_section(ctx
, &signature
-> num_decl
);
1734 oberon_assert_token(ctx
, RPAREN
);
1736 if(ctx
-> token
== COLON
)
1738 oberon_assert_token(ctx
, COLON
);
1740 oberon_object_t
* typeobj
;
1741 typeobj
= oberon_qualident(ctx
, NULL
, 1);
1742 if(typeobj
-> class != OBERON_CLASS_TYPE
)
1744 oberon_error(ctx
, "function result is not type");
1746 signature
-> base
= typeobj
-> type
;
1751 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
1753 oberon_type_t
* signature
;
1755 signature
-> class = OBERON_TYPE_PROCEDURE
;
1756 signature
-> num_decl
= 0;
1757 signature
-> base
= ctx
-> void_type
;
1758 signature
-> decl
= NULL
;
1760 if(ctx
-> token
== LPAREN
)
1762 oberon_formal_pars(ctx
, signature
);
1767 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1769 if(a
-> num_decl
!= b
-> num_decl
)
1771 oberon_error(ctx
, "number parameters not matched");
1774 int num_param
= a
-> num_decl
;
1775 oberon_object_t
* param_a
= a
-> decl
;
1776 oberon_object_t
* param_b
= b
-> decl
;
1777 for(int i
= 0; i
< num_param
; i
++)
1779 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
1781 oberon_error(ctx
, "param %i name not matched", i
+ 1);
1784 if(param_a
-> type
!= param_b
-> type
)
1786 oberon_error(ctx
, "param %i type not matched", i
+ 1);
1789 param_a
= param_a
-> next
;
1790 param_b
= param_b
-> next
;
1795 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1797 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
1798 oberon_type_t
* result_type
= proc
-> type
-> base
;
1800 if(result_type
-> class == OBERON_TYPE_VOID
)
1804 oberon_error(ctx
, "procedure has no result type");
1811 oberon_error(ctx
, "procedure requires expression on result");
1814 oberon_autocast_to(ctx
, expr
, result_type
);
1817 proc
-> has_return
= 1;
1819 oberon_generate_return(ctx
, expr
);
1823 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
1825 oberon_assert_token(ctx
, SEMICOLON
);
1827 ctx
-> decl
= proc
-> scope
;
1829 oberon_decl_seq(ctx
);
1831 oberon_generate_begin_proc(ctx
, proc
);
1833 if(ctx
-> token
== BEGIN
)
1835 oberon_assert_token(ctx
, BEGIN
);
1836 oberon_statement_seq(ctx
);
1839 oberon_assert_token(ctx
, END
);
1840 char * name
= oberon_assert_ident(ctx
);
1841 if(strcmp(name
, proc
-> name
) != 0)
1843 oberon_error(ctx
, "procedure name not matched");
1846 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
1847 && proc
-> has_return
== 0)
1849 oberon_make_return(ctx
, NULL
);
1852 if(proc
-> has_return
== 0)
1854 oberon_error(ctx
, "procedure requires return");
1857 oberon_generate_end_proc(ctx
);
1858 oberon_close_scope(ctx
-> decl
);
1862 oberon_proc_decl(oberon_context_t
* ctx
)
1864 oberon_assert_token(ctx
, PROCEDURE
);
1867 if(ctx
-> token
== UPARROW
)
1869 oberon_assert_token(ctx
, UPARROW
);
1876 name
= oberon_assert_ident(ctx
);
1877 oberon_def(ctx
, &export
, &read_only
);
1879 oberon_scope_t
* proc_scope
;
1880 proc_scope
= oberon_open_scope(ctx
);
1881 ctx
-> decl
-> local
= 1;
1883 oberon_type_t
* signature
;
1884 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1885 oberon_opt_formal_pars(ctx
, &signature
);
1887 oberon_initialize_decl(ctx
);
1888 oberon_generator_init_type(ctx
, signature
);
1889 oberon_close_scope(ctx
-> decl
);
1891 oberon_object_t
* proc
;
1892 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
1895 if(proc
-> class != OBERON_CLASS_PROC
)
1897 oberon_error(ctx
, "mult definition");
1904 oberon_error(ctx
, "mult procedure definition");
1908 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
1910 oberon_error(ctx
, "export type not matched");
1913 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
1917 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
);
1918 proc
-> type
= signature
;
1919 proc
-> scope
= proc_scope
;
1920 oberon_generator_init_proc(ctx
, proc
);
1923 proc
-> scope
-> parent
= proc
;
1928 oberon_proc_decl_body(ctx
, proc
);
1933 oberon_const_decl(oberon_context_t
* ctx
)
1935 oberon_item_t
* value
;
1936 oberon_object_t
* constant
;
1938 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
);
1939 oberon_assert_token(ctx
, EQUAL
);
1940 value
= oberon_const_expr(ctx
);
1941 constant
-> value
= value
;
1945 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
1947 if(size
-> is_item
== 0)
1949 oberon_error(ctx
, "requires constant");
1952 if(size
-> item
.mode
!= MODE_INTEGER
)
1954 oberon_error(ctx
, "requires integer constant");
1957 oberon_type_t
* arr
;
1959 arr
-> class = OBERON_TYPE_ARRAY
;
1960 arr
-> size
= size
-> item
.integer
;
1965 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
)
1967 if(ctx
-> token
== IDENT
)
1970 oberon_object_t
* list
;
1971 oberon_type_t
* type
;
1972 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1974 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, &num
, &list
);
1975 oberon_assert_token(ctx
, COLON
);
1976 oberon_type(ctx
, &type
);
1978 oberon_object_t
* field
= list
;
1979 for(int i
= 0; i
< num
; i
++)
1981 field
-> type
= type
;
1982 field
= field
-> next
;
1985 rec
-> num_decl
+= num
;
1990 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
1993 oberon_object_t
* to
;
1995 to
= oberon_qualident(ctx
, &name
, 0);
1997 //name = oberon_assert_ident(ctx);
1998 //to = oberon_find_object(ctx -> decl, name, 0);
2002 if(to
-> class != OBERON_CLASS_TYPE
)
2004 oberon_error(ctx
, "not a type");
2009 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, 0, 0);
2010 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2016 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2019 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2023 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2031 oberon_type_t
* dim
;
2032 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2034 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2036 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2040 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2042 type
-> class = OBERON_TYPE_ARRAY
;
2044 type
-> base
= base
;
2048 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2050 if(ctx
-> token
== IDENT
)
2052 oberon_qualident_type(ctx
, type
);
2054 else if(ctx
-> token
== ARRAY
)
2056 oberon_assert_token(ctx
, ARRAY
);
2059 oberon_expr_t
* sizes
;
2061 if(ISEXPR(ctx
-> token
))
2063 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2066 oberon_assert_token(ctx
, OF
);
2068 oberon_type_t
* base
;
2069 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2070 oberon_type(ctx
, &base
);
2074 oberon_make_open_array(ctx
, base
, *type
);
2078 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2081 else if(ctx
-> token
== RECORD
)
2083 oberon_type_t
* rec
;
2085 rec
-> class = OBERON_TYPE_RECORD
;
2087 oberon_scope_t
* record_scope
;
2088 record_scope
= oberon_open_scope(ctx
);
2089 // TODO parent object
2090 //record_scope -> parent = NULL;
2091 record_scope
-> local
= 1;
2093 oberon_assert_token(ctx
, RECORD
);
2094 oberon_field_list(ctx
, rec
);
2095 while(ctx
-> token
== SEMICOLON
)
2097 oberon_assert_token(ctx
, SEMICOLON
);
2098 oberon_field_list(ctx
, rec
);
2100 oberon_assert_token(ctx
, END
);
2102 rec
-> decl
= record_scope
-> list
-> next
;
2103 oberon_close_scope(record_scope
);
2107 else if(ctx
-> token
== POINTER
)
2109 oberon_assert_token(ctx
, POINTER
);
2110 oberon_assert_token(ctx
, TO
);
2112 oberon_type_t
* base
;
2113 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2114 oberon_type(ctx
, &base
);
2116 oberon_type_t
* ptr
;
2118 ptr
-> class = OBERON_TYPE_POINTER
;
2121 else if(ctx
-> token
== PROCEDURE
)
2123 oberon_open_scope(ctx
);
2124 oberon_assert_token(ctx
, PROCEDURE
);
2125 oberon_opt_formal_pars(ctx
, type
);
2126 oberon_close_scope(ctx
-> decl
);
2130 oberon_error(ctx
, "invalid type declaration");
2135 oberon_type_decl(oberon_context_t
* ctx
)
2138 oberon_object_t
* newtype
;
2139 oberon_type_t
* type
;
2143 name
= oberon_assert_ident(ctx
);
2144 oberon_def(ctx
, &export
, &read_only
);
2146 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2149 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
);
2150 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2151 assert(newtype
-> type
);
2155 if(newtype
-> class != OBERON_CLASS_TYPE
)
2157 oberon_error(ctx
, "mult definition");
2160 if(newtype
-> linked
)
2162 oberon_error(ctx
, "mult definition - already linked");
2165 newtype
-> export
= export
;
2166 newtype
-> read_only
= read_only
;
2169 oberon_assert_token(ctx
, EQUAL
);
2171 type
= newtype
-> type
;
2172 oberon_type(ctx
, &type
);
2174 if(type
-> class == OBERON_TYPE_VOID
)
2176 oberon_error(ctx
, "recursive alias declaration");
2179 newtype
-> type
= type
;
2180 newtype
-> linked
= 1;
2183 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2184 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2187 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2189 if(type
-> class != OBERON_TYPE_POINTER
2190 && type
-> class != OBERON_TYPE_ARRAY
)
2195 if(type
-> recursive
)
2197 oberon_error(ctx
, "recursive pointer declaration");
2200 if(type
-> base
-> class == OBERON_TYPE_POINTER
)
2202 oberon_error(ctx
, "attempt to make pointer to pointer");
2205 type
-> recursive
= 1;
2207 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2209 type
-> recursive
= 0;
2213 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2215 if(type
-> class != OBERON_TYPE_RECORD
)
2220 if(type
-> recursive
)
2222 oberon_error(ctx
, "recursive record declaration");
2225 type
-> recursive
= 1;
2227 int num_fields
= type
-> num_decl
;
2228 oberon_object_t
* field
= type
-> decl
;
2229 for(int i
= 0; i
< num_fields
; i
++)
2231 oberon_prevent_recursive_object(ctx
, field
);
2232 field
= field
-> next
;
2235 type
-> recursive
= 0;
2238 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2240 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2245 if(type
-> recursive
)
2247 oberon_error(ctx
, "recursive procedure declaration");
2250 type
-> recursive
= 1;
2252 int num_fields
= type
-> num_decl
;
2253 oberon_object_t
* field
= type
-> decl
;
2254 for(int i
= 0; i
< num_fields
; i
++)
2256 oberon_prevent_recursive_object(ctx
, field
);
2257 field
= field
-> next
;
2260 type
-> recursive
= 0;
2264 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2266 if(type
-> class != OBERON_TYPE_ARRAY
)
2271 if(type
-> recursive
)
2273 oberon_error(ctx
, "recursive array declaration");
2276 type
-> recursive
= 1;
2278 oberon_prevent_recursive_type(ctx
, type
-> base
);
2280 type
-> recursive
= 0;
2284 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2286 if(type
-> class == OBERON_TYPE_POINTER
)
2288 oberon_prevent_recursive_pointer(ctx
, type
);
2290 else if(type
-> class == OBERON_TYPE_RECORD
)
2292 oberon_prevent_recursive_record(ctx
, type
);
2294 else if(type
-> class == OBERON_TYPE_ARRAY
)
2296 oberon_prevent_recursive_array(ctx
, type
);
2298 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2300 oberon_prevent_recursive_procedure(ctx
, type
);
2305 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2309 case OBERON_CLASS_VAR
:
2310 case OBERON_CLASS_TYPE
:
2311 case OBERON_CLASS_PARAM
:
2312 case OBERON_CLASS_VAR_PARAM
:
2313 case OBERON_CLASS_FIELD
:
2314 oberon_prevent_recursive_type(ctx
, x
-> type
);
2316 case OBERON_CLASS_CONST
:
2317 case OBERON_CLASS_PROC
:
2318 case OBERON_CLASS_MODULE
:
2321 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2327 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2329 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2333 oberon_prevent_recursive_object(ctx
, x
);
2338 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2339 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2342 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2344 if(type
-> class != OBERON_TYPE_RECORD
)
2349 int num_fields
= type
-> num_decl
;
2350 oberon_object_t
* field
= type
-> decl
;
2351 for(int i
= 0; i
< num_fields
; i
++)
2353 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2355 oberon_initialize_type(ctx
, field
-> type
);
2358 oberon_initialize_object(ctx
, field
);
2359 field
= field
-> next
;
2362 oberon_generator_init_record(ctx
, type
);
2366 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2368 if(type
-> class == OBERON_TYPE_VOID
)
2370 oberon_error(ctx
, "undeclarated type");
2373 if(type
-> initialized
)
2378 type
-> initialized
= 1;
2380 if(type
-> class == OBERON_TYPE_POINTER
)
2382 oberon_initialize_type(ctx
, type
-> base
);
2383 oberon_generator_init_type(ctx
, type
);
2385 else if(type
-> class == OBERON_TYPE_ARRAY
)
2387 if(type
-> size
!= 0)
2389 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2391 if(type
-> base
-> size
== 0)
2393 oberon_error(ctx
, "open array not allowed as array element");
2398 oberon_initialize_type(ctx
, type
-> base
);
2399 oberon_generator_init_type(ctx
, type
);
2401 else if(type
-> class == OBERON_TYPE_RECORD
)
2403 oberon_generator_init_type(ctx
, type
);
2404 oberon_initialize_record_fields(ctx
, type
);
2406 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2408 int num_fields
= type
-> num_decl
;
2409 oberon_object_t
* field
= type
-> decl
;
2410 for(int i
= 0; i
< num_fields
; i
++)
2412 oberon_initialize_object(ctx
, field
);
2413 field
= field
-> next
;
2416 oberon_generator_init_type(ctx
, type
);
2420 oberon_generator_init_type(ctx
, type
);
2425 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2427 if(x
-> initialized
)
2432 x
-> initialized
= 1;
2436 case OBERON_CLASS_TYPE
:
2437 oberon_initialize_type(ctx
, x
-> type
);
2439 case OBERON_CLASS_VAR
:
2440 case OBERON_CLASS_FIELD
:
2441 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2443 if(x
-> type
-> size
== 0)
2445 oberon_error(ctx
, "open array not allowed as variable or field");
2448 oberon_initialize_type(ctx
, x
-> type
);
2449 oberon_generator_init_var(ctx
, x
);
2451 case OBERON_CLASS_PARAM
:
2452 case OBERON_CLASS_VAR_PARAM
:
2453 oberon_initialize_type(ctx
, x
-> type
);
2454 oberon_generator_init_var(ctx
, x
);
2456 case OBERON_CLASS_CONST
:
2457 case OBERON_CLASS_PROC
:
2458 case OBERON_CLASS_MODULE
:
2461 oberon_error(ctx
, "oberon_initialize_object: wat");
2467 oberon_initialize_decl(oberon_context_t
* ctx
)
2469 oberon_object_t
* x
= ctx
-> decl
-> list
;
2473 oberon_initialize_object(ctx
, x
-> next
);
2479 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2481 oberon_object_t
* x
= ctx
-> decl
-> list
;
2485 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2487 if(x
-> next
-> linked
== 0)
2489 oberon_error(ctx
, "unresolved forward declaration");
2497 oberon_decl_seq(oberon_context_t
* ctx
)
2499 if(ctx
-> token
== CONST
)
2501 oberon_assert_token(ctx
, CONST
);
2502 while(ctx
-> token
== IDENT
)
2504 oberon_const_decl(ctx
);
2505 oberon_assert_token(ctx
, SEMICOLON
);
2509 if(ctx
-> token
== TYPE
)
2511 oberon_assert_token(ctx
, TYPE
);
2512 while(ctx
-> token
== IDENT
)
2514 oberon_type_decl(ctx
);
2515 oberon_assert_token(ctx
, SEMICOLON
);
2519 if(ctx
-> token
== VAR
)
2521 oberon_assert_token(ctx
, VAR
);
2522 while(ctx
-> token
== IDENT
)
2524 oberon_var_decl(ctx
);
2525 oberon_assert_token(ctx
, SEMICOLON
);
2529 oberon_prevent_recursive_decl(ctx
);
2530 oberon_initialize_decl(ctx
);
2532 while(ctx
-> token
== PROCEDURE
)
2534 oberon_proc_decl(ctx
);
2535 oberon_assert_token(ctx
, SEMICOLON
);
2538 oberon_prevent_undeclarated_procedures(ctx
);
2542 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
2544 if(dst
-> read_only
)
2546 oberon_error(ctx
, "read-only destination");
2549 oberon_autocast_to(ctx
, src
, dst
-> result
);
2550 oberon_generate_assign(ctx
, src
, dst
);
2554 oberon_statement(oberon_context_t
* ctx
)
2556 oberon_expr_t
* item1
;
2557 oberon_expr_t
* item2
;
2559 if(ctx
-> token
== IDENT
)
2561 item1
= oberon_designator(ctx
);
2562 if(ctx
-> token
== ASSIGN
)
2564 oberon_assert_token(ctx
, ASSIGN
);
2565 item2
= oberon_expr(ctx
);
2566 oberon_assign(ctx
, item2
, item1
);
2570 oberon_opt_proc_parens(ctx
, item1
);
2573 else if(ctx
-> token
== RETURN
)
2575 oberon_assert_token(ctx
, RETURN
);
2576 if(ISEXPR(ctx
-> token
))
2578 oberon_expr_t
* expr
;
2579 expr
= oberon_expr(ctx
);
2580 oberon_make_return(ctx
, expr
);
2584 oberon_make_return(ctx
, NULL
);
2590 oberon_statement_seq(oberon_context_t
* ctx
)
2592 oberon_statement(ctx
);
2593 while(ctx
-> token
== SEMICOLON
)
2595 oberon_assert_token(ctx
, SEMICOLON
);
2596 oberon_statement(ctx
);
2601 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
2603 oberon_module_t
* m
= ctx
-> module_list
;
2604 while(m
&& strcmp(m
-> name
, name
) != 0)
2612 code
= ctx
-> import_module(name
);
2615 oberon_error(ctx
, "no such module");
2618 m
= oberon_compile_module(ctx
, code
);
2624 oberon_error(ctx
, "cyclic module import");
2627 oberon_object_t
* ident
;
2628 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, 0, 0);
2629 ident
-> module
= m
;
2633 oberon_import_decl(oberon_context_t
* ctx
)
2638 alias
= name
= oberon_assert_ident(ctx
);
2639 if(ctx
-> token
== ASSIGN
)
2641 oberon_assert_token(ctx
, ASSIGN
);
2642 name
= oberon_assert_ident(ctx
);
2645 oberon_import_module(ctx
, alias
, name
);
2649 oberon_import_list(oberon_context_t
* ctx
)
2651 oberon_assert_token(ctx
, IMPORT
);
2653 oberon_import_decl(ctx
);
2654 while(ctx
-> token
== COMMA
)
2656 oberon_assert_token(ctx
, COMMA
);
2657 oberon_import_decl(ctx
);
2660 oberon_assert_token(ctx
, SEMICOLON
);
2664 oberon_parse_module(oberon_context_t
* ctx
)
2668 oberon_read_token(ctx
);
2670 oberon_assert_token(ctx
, MODULE
);
2671 name1
= oberon_assert_ident(ctx
);
2672 oberon_assert_token(ctx
, SEMICOLON
);
2673 ctx
-> mod
-> name
= name1
;
2675 if(ctx
-> token
== IMPORT
)
2677 oberon_import_list(ctx
);
2680 oberon_decl_seq(ctx
);
2682 oberon_generate_begin_module(ctx
);
2683 if(ctx
-> token
== BEGIN
)
2685 oberon_assert_token(ctx
, BEGIN
);
2686 oberon_statement_seq(ctx
);
2688 oberon_generate_end_module(ctx
);
2690 oberon_assert_token(ctx
, END
);
2691 name2
= oberon_assert_ident(ctx
);
2692 oberon_assert_token(ctx
, DOT
);
2694 if(strcmp(name1
, name2
) != 0)
2696 oberon_error(ctx
, "module name not matched");
2700 // =======================================================================
2702 // =======================================================================
2705 register_default_types(oberon_context_t
* ctx
)
2707 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2708 oberon_generator_init_type(ctx
, ctx
-> void_type
);
2710 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
2711 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
2712 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
2714 ctx
-> int_type
= oberon_new_type_integer(sizeof(int));
2715 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
2717 ctx
-> bool_type
= oberon_new_type_boolean(sizeof(bool));
2718 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
2720 ctx
-> real_type
= oberon_new_type_real(sizeof(float));
2721 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
2725 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
2727 oberon_object_t
* proc
;
2728 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, 1, 0);
2729 proc
-> sysproc
= 1;
2730 proc
-> genfunc
= f
;
2731 proc
-> genproc
= p
;
2732 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
2735 static oberon_expr_t
*
2736 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
2740 oberon_error(ctx
, "too few arguments");
2745 oberon_error(ctx
, "too mach arguments");
2748 oberon_expr_t
* arg
;
2751 oberon_type_t
* result_type
;
2752 result_type
= arg
-> result
;
2754 if(result_type
-> class != OBERON_TYPE_INTEGER
)
2756 oberon_error(ctx
, "ABS accepts only integers");
2760 oberon_expr_t
* expr
;
2761 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
2766 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
2770 oberon_error(ctx
, "too few arguments");
2773 oberon_expr_t
* dst
;
2776 oberon_type_t
* type
;
2777 type
= dst
-> result
;
2779 if(type
-> class != OBERON_TYPE_POINTER
)
2781 oberon_error(ctx
, "not a pointer");
2784 type
= type
-> base
;
2786 oberon_expr_t
* src
;
2787 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
2788 src
-> item
.num_args
= 0;
2789 src
-> item
.args
= NULL
;
2792 if(type
-> class == OBERON_TYPE_ARRAY
)
2794 if(type
-> size
== 0)
2796 oberon_type_t
* x
= type
;
2797 while(x
-> class == OBERON_TYPE_ARRAY
)
2807 if(num_args
< max_args
)
2809 oberon_error(ctx
, "too few arguments");
2812 if(num_args
> max_args
)
2814 oberon_error(ctx
, "too mach arguments");
2817 int num_sizes
= max_args
- 1;
2818 oberon_expr_t
* size_list
= list_args
-> next
;
2820 oberon_expr_t
* arg
= size_list
;
2821 for(int i
= 0; i
< max_args
- 1; i
++)
2823 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
2825 oberon_error(ctx
, "size must be integer");
2830 src
-> item
.num_args
= num_sizes
;
2831 src
-> item
.args
= size_list
;
2833 else if(type
-> class != OBERON_TYPE_RECORD
)
2835 oberon_error(ctx
, "oberon_make_new_call: wat");
2838 if(num_args
> max_args
)
2840 oberon_error(ctx
, "too mach arguments");
2843 oberon_assign(ctx
, src
, dst
);
2847 oberon_create_context(ModuleImportCallback import_module
)
2849 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
2851 oberon_scope_t
* world_scope
;
2852 world_scope
= oberon_open_scope(ctx
);
2853 ctx
-> world_scope
= world_scope
;
2855 ctx
-> import_module
= import_module
;
2857 oberon_generator_init_context(ctx
);
2859 register_default_types(ctx
);
2860 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
2861 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
2867 oberon_destroy_context(oberon_context_t
* ctx
)
2869 oberon_generator_destroy_context(ctx
);
2874 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
2876 const char * code
= ctx
-> code
;
2877 int code_index
= ctx
-> code_index
;
2879 int token
= ctx
-> token
;
2880 char * string
= ctx
-> string
;
2881 int integer
= ctx
-> integer
;
2882 oberon_scope_t
* decl
= ctx
-> decl
;
2883 oberon_module_t
* mod
= ctx
-> mod
;
2885 oberon_scope_t
* module_scope
;
2886 module_scope
= oberon_open_scope(ctx
);
2888 oberon_module_t
* module
;
2889 module
= calloc(1, sizeof *module
);
2890 module
-> decl
= module_scope
;
2891 module
-> next
= ctx
-> module_list
;
2893 ctx
-> mod
= module
;
2894 ctx
-> module_list
= module
;
2896 oberon_init_scaner(ctx
, newcode
);
2897 oberon_parse_module(ctx
);
2899 module
-> ready
= 1;
2902 ctx
-> code_index
= code_index
;
2904 ctx
-> token
= token
;
2905 ctx
-> string
= string
;
2906 ctx
-> integer
= integer
;