9 #include "../include/oberon.h"
11 #include "oberon-internals.h"
12 #include "generator.h"
79 // =======================================================================
81 // =======================================================================
84 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
88 fprintf(stderr
, "error: ");
89 vfprintf(stderr
, fmt
, ptr
);
90 fprintf(stderr
, "\n");
91 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
92 fprintf(stderr
, " c = %c\n", ctx
-> c
);
93 fprintf(stderr
, " token = %i\n", ctx
-> token
);
98 static oberon_type_t
*
99 oberon_new_type_ptr(int class)
101 oberon_type_t
* x
= malloc(sizeof *x
);
102 memset(x
, 0, sizeof *x
);
107 static oberon_type_t
*
108 oberon_new_type_integer(int size
)
111 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
116 static oberon_type_t
*
117 oberon_new_type_boolean()
120 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
124 static oberon_type_t
*
125 oberon_new_type_real(int size
)
128 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
133 static oberon_type_t
*
134 oberon_new_type_char(int size
)
137 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
142 static oberon_type_t
*
143 oberon_new_type_string(int size
)
146 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
151 // =======================================================================
153 // =======================================================================
155 static oberon_scope_t
*
156 oberon_open_scope(oberon_context_t
* ctx
)
158 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
159 oberon_object_t
* list
= calloc(1, sizeof *list
);
162 scope
-> list
= list
;
163 scope
-> up
= ctx
-> decl
;
167 scope
-> local
= scope
-> up
-> local
;
168 scope
-> parent
= scope
-> up
-> parent
;
169 scope
-> parent_type
= scope
-> up
-> parent_type
;
170 scope
-> exit_label
= scope
-> up
-> exit_label
;
178 oberon_close_scope(oberon_scope_t
* scope
)
180 oberon_context_t
* ctx
= scope
-> ctx
;
181 ctx
-> decl
= scope
-> up
;
184 static oberon_object_t
*
185 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
187 oberon_object_t
* x
= list
;
188 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
195 static oberon_object_t
*
196 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
198 oberon_object_t
* result
= NULL
;
200 oberon_scope_t
* s
= scope
;
201 while(result
== NULL
&& s
!= NULL
)
203 result
= oberon_find_object_in_list(s
-> list
, name
);
207 if(check_it
&& result
== NULL
)
209 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
215 static oberon_object_t
*
216 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
218 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
219 memset(newvar
, 0, sizeof *newvar
);
220 newvar
-> name
= name
;
221 newvar
-> class = class;
222 newvar
-> export
= export
;
223 newvar
-> read_only
= read_only
;
224 newvar
-> local
= scope
-> local
;
225 newvar
-> parent
= scope
-> parent
;
226 newvar
-> parent_type
= scope
-> parent_type
;
227 newvar
-> module
= scope
-> ctx
-> mod
;
231 static oberon_object_t
*
232 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
236 if(oberon_find_object(scope
-> up
, name
, false))
238 oberon_error(scope
-> ctx
, "already defined");
242 oberon_object_t
* x
= scope
-> list
;
243 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
250 oberon_error(scope
-> ctx
, "already defined");
253 oberon_object_t
* newvar
;
254 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
260 static oberon_object_t
*
261 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
263 oberon_object_t
* id
;
264 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
266 oberon_generator_init_type(scope
-> ctx
, type
);
270 // =======================================================================
272 // =======================================================================
275 oberon_get_char(oberon_context_t
* ctx
)
277 if(ctx
-> code
[ctx
-> code_index
])
279 ctx
-> code_index
+= 1;
280 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
285 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
288 ctx
-> code_index
= 0;
289 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
293 oberon_read_ident(oberon_context_t
* ctx
)
296 int i
= ctx
-> code_index
;
298 int c
= ctx
-> code
[i
];
306 char * ident
= malloc(len
+ 1);
307 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
310 ctx
-> code_index
= i
;
311 ctx
-> c
= ctx
-> code
[i
];
312 ctx
-> string
= ident
;
313 ctx
-> token
= IDENT
;
315 if(strcmp(ident
, "MODULE") == 0)
317 ctx
-> token
= MODULE
;
319 else if(strcmp(ident
, "END") == 0)
323 else if(strcmp(ident
, "VAR") == 0)
327 else if(strcmp(ident
, "BEGIN") == 0)
329 ctx
-> token
= BEGIN
;
331 else if(strcmp(ident
, "TRUE") == 0)
335 else if(strcmp(ident
, "FALSE") == 0)
337 ctx
-> token
= FALSE
;
339 else if(strcmp(ident
, "OR") == 0)
343 else if(strcmp(ident
, "DIV") == 0)
347 else if(strcmp(ident
, "MOD") == 0)
351 else if(strcmp(ident
, "PROCEDURE") == 0)
353 ctx
-> token
= PROCEDURE
;
355 else if(strcmp(ident
, "RETURN") == 0)
357 ctx
-> token
= RETURN
;
359 else if(strcmp(ident
, "CONST") == 0)
361 ctx
-> token
= CONST
;
363 else if(strcmp(ident
, "TYPE") == 0)
367 else if(strcmp(ident
, "ARRAY") == 0)
369 ctx
-> token
= ARRAY
;
371 else if(strcmp(ident
, "OF") == 0)
375 else if(strcmp(ident
, "RECORD") == 0)
377 ctx
-> token
= RECORD
;
379 else if(strcmp(ident
, "POINTER") == 0)
381 ctx
-> token
= POINTER
;
383 else if(strcmp(ident
, "TO") == 0)
387 else if(strcmp(ident
, "NIL") == 0)
391 else if(strcmp(ident
, "IMPORT") == 0)
393 ctx
-> token
= IMPORT
;
395 else if(strcmp(ident
, "IN") == 0)
399 else if(strcmp(ident
, "IS") == 0)
403 else if(strcmp(ident
, "IF") == 0)
407 else if(strcmp(ident
, "THEN") == 0)
411 else if(strcmp(ident
, "ELSE") == 0)
415 else if(strcmp(ident
, "ELSIF") == 0)
417 ctx
-> token
= ELSIF
;
419 else if(strcmp(ident
, "WHILE") == 0)
421 ctx
-> token
= WHILE
;
423 else if(strcmp(ident
, "DO") == 0)
427 else if(strcmp(ident
, "REPEAT") == 0)
429 ctx
-> token
= REPEAT
;
431 else if(strcmp(ident
, "UNTIL") == 0)
433 ctx
-> token
= UNTIL
;
435 else if(strcmp(ident
, "FOR") == 0)
439 else if(strcmp(ident
, "BY") == 0)
443 else if(strcmp(ident
, "LOOP") == 0)
447 else if(strcmp(ident
, "EXIT") == 0)
454 oberon_read_number(oberon_context_t
* ctx
)
467 * mode = 3 == LONGREAL
471 start_i
= ctx
-> code_index
;
473 while(isdigit(ctx
-> c
))
475 oberon_get_char(ctx
);
478 end_i
= ctx
-> code_index
;
480 if(isxdigit(ctx
-> c
))
483 while(isxdigit(ctx
-> c
))
485 oberon_get_char(ctx
);
488 end_i
= ctx
-> code_index
;
493 oberon_get_char(ctx
);
495 else if(ctx
-> c
== 'X')
498 oberon_get_char(ctx
);
502 oberon_error(ctx
, "invalid hex number");
505 else if(ctx
-> c
== '.')
508 oberon_get_char(ctx
);
510 while(isdigit(ctx
-> c
))
512 oberon_get_char(ctx
);
515 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
517 exp_i
= ctx
-> code_index
;
524 oberon_get_char(ctx
);
526 if(ctx
-> c
== '+' || ctx
-> c
== '-')
528 oberon_get_char(ctx
);
531 while(isdigit(ctx
-> c
))
533 oberon_get_char(ctx
);
538 end_i
= ctx
-> code_index
;
546 oberon_get_char(ctx
);
548 else if(ctx
-> c
== 'X')
551 oberon_get_char(ctx
);
555 int len
= end_i
- start_i
;
556 ident
= malloc(len
+ 1);
557 memcpy(ident
, &ctx
-> code
[start_i
], len
);
560 ctx
-> longmode
= false;
563 int i
= exp_i
- start_i
;
565 ctx
-> longmode
= true;
571 integer
= atol(ident
);
573 ctx
-> token
= INTEGER
;
576 sscanf(ident
, "%lx", &integer
);
578 ctx
-> token
= INTEGER
;
582 sscanf(ident
, "%lf", &real
);
586 sscanf(ident
, "%lx", &integer
);
591 oberon_error(ctx
, "oberon_read_number: wat");
595 ctx
-> string
= ident
;
596 ctx
-> integer
= integer
;
601 oberon_skip_space(oberon_context_t
* ctx
)
603 while(isspace(ctx
-> c
))
605 oberon_get_char(ctx
);
610 oberon_read_comment(oberon_context_t
* ctx
)
617 oberon_get_char(ctx
);
620 oberon_get_char(ctx
);
624 else if(ctx
-> c
== '*')
626 oberon_get_char(ctx
);
629 oberon_get_char(ctx
);
633 else if(ctx
-> c
== 0)
635 oberon_error(ctx
, "unterminated comment");
639 oberon_get_char(ctx
);
644 static void oberon_read_string(oberon_context_t
* ctx
)
647 oberon_get_char(ctx
);
649 int start
= ctx
-> code_index
;
651 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
653 oberon_get_char(ctx
);
658 oberon_error(ctx
, "unterminated string");
661 int end
= ctx
-> code_index
;
663 oberon_get_char(ctx
);
665 char * string
= calloc(1, end
- start
+ 1);
666 strncpy(string
, &ctx
-> code
[start
], end
- start
);
668 ctx
-> token
= STRING
;
669 ctx
-> string
= string
;
671 printf("oberon_read_string: string ((%s))\n", string
);
674 static void oberon_read_token(oberon_context_t
* ctx
);
677 oberon_read_symbol(oberon_context_t
* ctx
)
686 ctx
-> token
= SEMICOLON
;
687 oberon_get_char(ctx
);
690 ctx
-> token
= COLON
;
691 oberon_get_char(ctx
);
694 ctx
-> token
= ASSIGN
;
695 oberon_get_char(ctx
);
700 oberon_get_char(ctx
);
703 ctx
-> token
= LPAREN
;
704 oberon_get_char(ctx
);
707 oberon_get_char(ctx
);
708 oberon_read_comment(ctx
);
709 oberon_read_token(ctx
);
713 ctx
-> token
= RPAREN
;
714 oberon_get_char(ctx
);
717 ctx
-> token
= EQUAL
;
718 oberon_get_char(ctx
);
722 oberon_get_char(ctx
);
726 oberon_get_char(ctx
);
730 oberon_get_char(ctx
);
734 ctx
-> token
= GREAT
;
735 oberon_get_char(ctx
);
739 oberon_get_char(ctx
);
744 oberon_get_char(ctx
);
747 ctx
-> token
= MINUS
;
748 oberon_get_char(ctx
);
752 oberon_get_char(ctx
);
755 oberon_get_char(ctx
);
756 oberon_error(ctx
, "unstarted comment");
760 ctx
-> token
= SLASH
;
761 oberon_get_char(ctx
);
765 oberon_get_char(ctx
);
769 oberon_get_char(ctx
);
772 ctx
-> token
= COMMA
;
773 oberon_get_char(ctx
);
776 ctx
-> token
= LBRACE
;
777 oberon_get_char(ctx
);
780 ctx
-> token
= RBRACE
;
781 oberon_get_char(ctx
);
784 ctx
-> token
= UPARROW
;
785 oberon_get_char(ctx
);
788 oberon_read_string(ctx
);
791 oberon_read_string(ctx
);
794 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
800 oberon_read_token(oberon_context_t
* ctx
)
802 oberon_skip_space(ctx
);
807 oberon_read_ident(ctx
);
811 oberon_read_number(ctx
);
815 oberon_read_symbol(ctx
);
819 // =======================================================================
821 // =======================================================================
823 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
824 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
825 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
826 static char * oberon_assert_ident(oberon_context_t
* ctx
);
827 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
828 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
829 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
831 static oberon_expr_t
*
832 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
834 oberon_oper_t
* operator;
835 operator = malloc(sizeof *operator);
836 memset(operator, 0, sizeof *operator);
838 operator -> is_item
= 0;
839 operator -> result
= result
;
840 operator -> read_only
= 1;
842 operator -> left
= left
;
843 operator -> right
= right
;
845 return (oberon_expr_t
*) operator;
848 static oberon_expr_t
*
849 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
851 oberon_item_t
* item
;
852 item
= malloc(sizeof *item
);
853 memset(item
, 0, sizeof *item
);
856 item
-> result
= result
;
857 item
-> read_only
= read_only
;
860 return (oberon_expr_t
*)item
;
863 static oberon_expr_t
*
864 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
866 oberon_expr_t
* expr
;
867 oberon_type_t
* result
;
869 result
= a
-> result
;
873 if(result
-> class != OBERON_TYPE_INTEGER
)
875 oberon_error(ctx
, "incompatible operator type");
878 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
880 else if(token
== NOT
)
882 if(result
-> class != OBERON_TYPE_BOOLEAN
)
884 oberon_error(ctx
, "incompatible operator type");
887 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
891 oberon_error(ctx
, "oberon_make_unary_op: wat");
898 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
900 oberon_expr_t
* last
;
905 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
909 *first
= last
= oberon_expr(ctx
);
911 while(ctx
-> token
== COMMA
)
913 oberon_assert_token(ctx
, COMMA
);
914 oberon_expr_t
* current
;
918 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
922 current
= oberon_expr(ctx
);
925 last
-> next
= current
;
931 static oberon_expr_t
*
932 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
934 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
937 static oberon_expr_t
*
938 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
940 oberon_type_t
* from
= expr
-> result
;
941 oberon_type_t
* to
= rec
;
943 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
945 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
947 printf("oberno_make_record_cast: pointers\n");
952 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
954 oberon_error(ctx
, "must be record type");
957 return oberon_cast_expr(ctx
, expr
, rec
);
960 static oberon_type_t
*
961 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
963 oberon_type_t
* result
;
964 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
968 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
972 else if(a
-> class != b
-> class)
974 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
976 else if(a
-> size
> b
-> size
)
989 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
991 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
997 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
999 oberon_error(ctx
, "not a record");
1002 oberon_type_t
* t
= from
;
1003 while(t
!= NULL
&& t
!= to
)
1010 oberon_error(ctx
, "incompatible record types");
1014 static oberon_expr_t
*
1015 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1018 // Если классы типов равны
1019 // Если INTEGER переводится в REAL
1020 // Есди STRING переводится в ARRAY OF CHAR
1023 if(pref
-> class != expr
-> result
-> class)
1025 printf("expr class %i\n", expr
-> result
-> class);
1026 printf("pref class %i\n", pref
-> class);
1028 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1030 if(pref
-> class == OBERON_TYPE_ARRAY
)
1032 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1042 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1044 if(pref
-> class != OBERON_TYPE_REAL
)
1057 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1060 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1062 if(expr
-> result
-> size
> pref
-> size
)
1064 oberon_error(ctx
, "incompatible size");
1068 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1071 else if(pref
-> class == OBERON_TYPE_RECORD
)
1073 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1074 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1076 else if(pref
-> class == OBERON_TYPE_POINTER
)
1078 assert(pref
-> base
);
1079 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1081 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1082 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1084 else if(expr
-> result
-> base
!= pref
-> base
)
1086 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1088 oberon_error(ctx
, "incompatible pointer types");
1097 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1099 oberon_type_t
* a
= (*ea
) -> result
;
1100 oberon_type_t
* b
= (*eb
) -> result
;
1101 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1102 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1103 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1107 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1109 if(desig
-> mode
!= MODE_CALL
)
1111 oberon_error(ctx
, "expected mode CALL");
1114 oberon_type_t
* fn
= desig
-> parent
-> result
;
1115 int num_args
= desig
-> num_args
;
1116 int num_decl
= fn
-> num_decl
;
1118 if(num_args
< num_decl
)
1120 oberon_error(ctx
, "too few arguments");
1122 else if(num_args
> num_decl
)
1124 oberon_error(ctx
, "too many arguments");
1127 /* Делаем проверку на запись и делаем автокаст */
1128 oberon_expr_t
* casted
[num_args
];
1129 oberon_expr_t
* arg
= desig
-> args
;
1130 oberon_object_t
* param
= fn
-> decl
;
1131 for(int i
= 0; i
< num_args
; i
++)
1133 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1135 if(arg
-> read_only
)
1137 oberon_error(ctx
, "assign to read-only var");
1141 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1143 param
= param
-> next
;
1146 /* Создаём новый список выражений */
1150 for(int i
= 0; i
< num_args
- 1; i
++)
1152 casted
[i
] -> next
= casted
[i
+ 1];
1154 desig
-> args
= arg
;
1158 static oberon_expr_t
*
1159 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1161 oberon_type_t
* signature
= item
-> result
;
1162 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1164 oberon_error(ctx
, "not a procedure");
1167 oberon_expr_t
* call
;
1169 if(signature
-> sysproc
)
1171 if(signature
-> genfunc
== NULL
)
1173 oberon_error(ctx
, "not a function-procedure");
1176 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1180 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1182 oberon_error(ctx
, "attempt to call procedure in expression");
1185 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1186 call
-> item
.parent
= item
;
1187 call
-> item
.num_args
= num_args
;
1188 call
-> item
.args
= list_args
;
1189 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1196 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1198 oberon_type_t
* signature
= item
-> result
;
1199 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1201 oberon_error(ctx
, "not a procedure");
1204 oberon_expr_t
* call
;
1206 if(signature
-> sysproc
)
1208 if(signature
-> genproc
== NULL
)
1210 oberon_error(ctx
, "not a procedure");
1213 signature
-> genproc(ctx
, num_args
, list_args
);
1217 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1219 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1222 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1223 call
-> item
.parent
= item
;
1224 call
-> item
.num_args
= num_args
;
1225 call
-> item
.args
= list_args
;
1226 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1227 oberon_generate_call_proc(ctx
, call
);
1233 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
1235 switch(proc -> class)
1237 case OBERON_CLASS_PROC:
1238 if(proc -> class != OBERON_CLASS_PROC)
1240 oberon_error(ctx, "not a procedure");
1243 case OBERON_CLASS_VAR:
1244 case OBERON_CLASS_VAR_PARAM:
1245 case OBERON_CLASS_PARAM:
1246 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
1248 oberon_error(ctx, "not a procedure");
1252 oberon_error(ctx, "not a procedure");
1258 if(proc -> genproc == NULL)
1260 oberon_error(ctx, "requres non-typed procedure");
1263 proc -> genproc(ctx, num_args, list_args);
1267 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
1269 oberon_error(ctx, "attempt to call function as non-typed procedure");
1272 oberon_expr_t * call;
1273 call = oberon_new_item(MODE_CALL, proc -> type -> base, 1);
1274 call -> item.var = proc;
1275 call -> item.num_args = num_args;
1276 call -> item.args = list_args;
1277 oberon_autocast_call(ctx, call);
1278 oberon_generate_call_proc(ctx, call);
1287 || ((x) == INTEGER) \
1290 || ((x) == STRING) \
1292 || ((x) == LPAREN) \
1297 static oberon_expr_t
*
1298 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1300 printf("oberno_make_dereferencing\n");
1301 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1303 oberon_error(ctx
, "not a pointer");
1306 assert(expr
-> is_item
);
1308 oberon_expr_t
* selector
;
1309 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1310 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1315 static oberon_expr_t
*
1316 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1318 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1320 desig
= oberno_make_dereferencing(ctx
, desig
);
1323 assert(desig
-> is_item
);
1325 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1327 oberon_error(ctx
, "not array");
1330 oberon_type_t
* base
;
1331 base
= desig
-> result
-> base
;
1333 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1335 oberon_error(ctx
, "index must be integer");
1338 // Статическая проверка границ массива
1339 if(desig
-> result
-> size
!= 0)
1341 if(index
-> is_item
)
1343 if(index
-> item
.mode
== MODE_INTEGER
)
1345 int arr_size
= desig
-> result
-> size
;
1346 int index_int
= index
-> item
.integer
;
1347 if(index_int
< 0 || index_int
> arr_size
- 1)
1349 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1355 oberon_expr_t
* selector
;
1356 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1357 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1358 selector
-> item
.num_args
= 1;
1359 selector
-> item
.args
= index
;
1364 static oberon_expr_t
*
1365 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1367 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1369 expr
= oberno_make_dereferencing(ctx
, expr
);
1372 assert(expr
-> is_item
);
1374 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1376 oberon_error(ctx
, "not record");
1379 oberon_type_t
* rec
= expr
-> result
;
1381 oberon_object_t
* field
;
1382 field
= oberon_find_object(rec
-> scope
, name
, true);
1384 if(field
-> export
== 0)
1386 if(field
-> module
!= ctx
-> mod
)
1388 oberon_error(ctx
, "field not exported");
1393 if(field
-> read_only
)
1395 if(field
-> module
!= ctx
-> mod
)
1401 oberon_expr_t
* selector
;
1402 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1403 selector
-> item
.var
= field
;
1404 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1409 #define ISSELECTOR(x) \
1412 || ((x) == UPARROW) \
1415 static oberon_object_t
*
1416 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1419 oberon_object_t
* x
;
1421 name
= oberon_assert_ident(ctx
);
1422 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1426 if(x
-> class == OBERON_CLASS_MODULE
)
1428 oberon_assert_token(ctx
, DOT
);
1429 name
= oberon_assert_ident(ctx
);
1430 /* Наличие объектов в левых модулях всегда проверяется */
1431 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1433 if(x
-> export
== 0)
1435 oberon_error(ctx
, "not exported");
1448 static oberon_expr_t
*
1449 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1452 oberon_object_t
* x
;
1453 oberon_expr_t
* expr
;
1455 x
= oberon_find_object(ctx
-> decl
, name
, true);
1458 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1463 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1464 expr
-> item
.var
= x
;
1468 static oberon_expr_t
*
1469 oberon_designator(oberon_context_t
* ctx
)
1472 oberon_object_t
* var
;
1473 oberon_expr_t
* expr
;
1475 var
= oberon_qualident(ctx
, NULL
, 1);
1478 if(var
-> read_only
)
1480 if(var
-> module
!= ctx
-> mod
)
1486 switch(var
-> class)
1488 case OBERON_CLASS_CONST
:
1490 expr
= (oberon_expr_t
*) var
-> value
;
1492 case OBERON_CLASS_VAR
:
1493 case OBERON_CLASS_VAR_PARAM
:
1494 case OBERON_CLASS_PARAM
:
1495 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1497 case OBERON_CLASS_PROC
:
1498 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1501 oberon_error(ctx
, "invalid designator");
1504 expr
-> item
.var
= var
;
1506 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1508 switch(ctx
-> token
)
1511 oberon_assert_token(ctx
, DOT
);
1512 name
= oberon_assert_ident(ctx
);
1513 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1516 oberon_assert_token(ctx
, LBRACE
);
1517 int num_indexes
= 0;
1518 oberon_expr_t
* indexes
= NULL
;
1519 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1520 oberon_assert_token(ctx
, RBRACE
);
1522 for(int i
= 0; i
< num_indexes
; i
++)
1524 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1525 indexes
= indexes
-> next
;
1529 oberon_assert_token(ctx
, UPARROW
);
1530 expr
= oberno_make_dereferencing(ctx
, expr
);
1533 oberon_assert_token(ctx
, LPAREN
);
1534 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1535 if(objtype
-> class != OBERON_CLASS_TYPE
)
1537 oberon_error(ctx
, "must be type");
1539 oberon_assert_token(ctx
, RPAREN
);
1540 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1543 oberon_error(ctx
, "oberon_designator: wat");
1551 static oberon_expr_t
*
1552 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1554 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1555 if(ctx
-> token
== LPAREN
)
1557 oberon_assert_token(ctx
, LPAREN
);
1560 oberon_expr_t
* arguments
= NULL
;
1562 if(ISEXPR(ctx
-> token
))
1564 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1567 assert(expr
-> is_item
== 1);
1568 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1570 oberon_assert_token(ctx
, RPAREN
);
1577 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1579 assert(expr
-> is_item
);
1582 oberon_expr_t
* arguments
= NULL
;
1584 if(ctx
-> token
== LPAREN
)
1586 oberon_assert_token(ctx
, LPAREN
);
1588 if(ISEXPR(ctx
-> token
))
1590 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1593 oberon_assert_token(ctx
, RPAREN
);
1596 /* Вызов происходит даже без скобок */
1597 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1600 static oberon_type_t
*
1601 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1603 if(i
>= -128 && i
<= 127)
1605 return ctx
-> byte_type
;
1607 else if(i
>= -32768 && i
<= 32767)
1609 return ctx
-> shortint_type
;
1611 else if(i
>= -2147483648 && i
<= 2147483647)
1613 return ctx
-> int_type
;
1617 return ctx
-> longint_type
;
1621 static oberon_expr_t
*
1622 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1624 oberon_expr_t
* expr
;
1625 oberon_type_t
* result
;
1626 result
= oberon_get_type_of_int_value(ctx
, i
);
1627 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1628 expr
-> item
.integer
= i
;
1632 static oberon_expr_t
*
1633 oberon_factor(oberon_context_t
* ctx
)
1635 oberon_expr_t
* expr
;
1636 oberon_type_t
* result
;
1638 switch(ctx
-> token
)
1641 expr
= oberon_designator(ctx
);
1642 expr
= oberon_opt_func_parens(ctx
, expr
);
1645 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1646 oberon_assert_token(ctx
, INTEGER
);
1649 result
= ctx
-> char_type
;
1650 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1651 expr
-> item
.integer
= ctx
-> integer
;
1652 oberon_assert_token(ctx
, CHAR
);
1655 result
= ctx
-> string_type
;
1656 expr
= oberon_new_item(MODE_STRING
, result
, true);
1657 expr
-> item
.string
= ctx
-> string
;
1658 oberon_assert_token(ctx
, STRING
);
1661 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1662 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1663 expr
-> item
.real
= ctx
-> real
;
1664 oberon_assert_token(ctx
, REAL
);
1667 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1668 expr
-> item
.boolean
= true;
1669 oberon_assert_token(ctx
, TRUE
);
1672 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1673 expr
-> item
.boolean
= false;
1674 oberon_assert_token(ctx
, FALSE
);
1677 oberon_assert_token(ctx
, LPAREN
);
1678 expr
= oberon_expr(ctx
);
1679 oberon_assert_token(ctx
, RPAREN
);
1682 oberon_assert_token(ctx
, NOT
);
1683 expr
= oberon_factor(ctx
);
1684 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1687 oberon_assert_token(ctx
, NIL
);
1688 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1691 oberon_error(ctx
, "invalid expression");
1697 #define ITMAKESBOOLEAN(x) \
1698 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1700 #define ITUSEONLYINTEGER(x) \
1701 ((x) >= LESS && (x) <= GEQ)
1703 #define ITUSEONLYBOOLEAN(x) \
1704 (((x) == OR) || ((x) == AND))
1707 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1709 oberon_expr_t
* expr
= *e
;
1710 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1712 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1714 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1718 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1721 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1723 oberon_error(ctx
, "required numeric type");
1727 static oberon_expr_t
*
1728 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1730 oberon_expr_t
* expr
;
1731 oberon_type_t
* result
;
1733 if(ITMAKESBOOLEAN(token
))
1735 if(ITUSEONLYINTEGER(token
))
1737 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1738 || b
-> result
-> class == OBERON_TYPE_INTEGER
1739 || a
-> result
-> class == OBERON_TYPE_REAL
1740 || b
-> result
-> class == OBERON_TYPE_REAL
)
1746 oberon_error(ctx
, "used only with numeric types");
1749 else if(ITUSEONLYBOOLEAN(token
))
1751 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1752 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1754 oberon_error(ctx
, "used only with boolean type");
1758 oberon_autocast_binary_op(ctx
, &a
, &b
);
1759 result
= ctx
-> bool_type
;
1763 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1765 else if(token
== NEQ
)
1767 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1769 else if(token
== LESS
)
1771 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1773 else if(token
== LEQ
)
1775 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1777 else if(token
== GREAT
)
1779 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1781 else if(token
== GEQ
)
1783 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1785 else if(token
== OR
)
1787 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1789 else if(token
== AND
)
1791 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1795 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1798 else if(token
== SLASH
)
1800 oberon_autocast_to_real(ctx
, &a
);
1801 oberon_autocast_to_real(ctx
, &b
);
1802 oberon_autocast_binary_op(ctx
, &a
, &b
);
1803 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1805 else if(token
== DIV
)
1807 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1808 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1810 oberon_error(ctx
, "operator DIV requires integer type");
1813 oberon_autocast_binary_op(ctx
, &a
, &b
);
1814 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1818 oberon_autocast_binary_op(ctx
, &a
, &b
);
1822 expr
= oberon_new_operator(OP_ADD
, a
-> result
, a
, b
);
1824 else if(token
== MINUS
)
1826 expr
= oberon_new_operator(OP_SUB
, a
-> result
, a
, b
);
1828 else if(token
== STAR
)
1830 expr
= oberon_new_operator(OP_MUL
, a
-> result
, a
, b
);
1832 else if(token
== MOD
)
1834 expr
= oberon_new_operator(OP_MOD
, a
-> result
, a
, b
);
1838 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1845 #define ISMULOP(x) \
1846 ((x) >= STAR && (x) <= AND)
1848 static oberon_expr_t
*
1849 oberon_term_expr(oberon_context_t
* ctx
)
1851 oberon_expr_t
* expr
;
1853 expr
= oberon_factor(ctx
);
1854 while(ISMULOP(ctx
-> token
))
1856 int token
= ctx
-> token
;
1857 oberon_read_token(ctx
);
1859 oberon_expr_t
* inter
= oberon_factor(ctx
);
1860 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1866 #define ISADDOP(x) \
1867 ((x) >= PLUS && (x) <= OR)
1869 static oberon_expr_t
*
1870 oberon_simple_expr(oberon_context_t
* ctx
)
1872 oberon_expr_t
* expr
;
1875 if(ctx
-> token
== PLUS
)
1878 oberon_assert_token(ctx
, PLUS
);
1880 else if(ctx
-> token
== MINUS
)
1883 oberon_assert_token(ctx
, MINUS
);
1886 expr
= oberon_term_expr(ctx
);
1890 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1893 while(ISADDOP(ctx
-> token
))
1895 int token
= ctx
-> token
;
1896 oberon_read_token(ctx
);
1898 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1899 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1905 #define ISRELATION(x) \
1906 ((x) >= EQUAL && (x) <= IS)
1908 static oberon_expr_t
*
1909 oberon_expr(oberon_context_t
* ctx
)
1911 oberon_expr_t
* expr
;
1913 expr
= oberon_simple_expr(ctx
);
1914 while(ISRELATION(ctx
-> token
))
1916 int token
= ctx
-> token
;
1917 oberon_read_token(ctx
);
1919 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1920 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1926 static oberon_item_t
*
1927 oberon_const_expr(oberon_context_t
* ctx
)
1929 oberon_expr_t
* expr
;
1930 expr
= oberon_expr(ctx
);
1932 if(expr
-> is_item
== 0)
1934 oberon_error(ctx
, "const expression are required");
1937 switch(expr
-> item
.mode
)
1948 oberon_error(ctx
, "const expression are required");
1952 return (oberon_item_t
*) expr
;
1955 // =======================================================================
1957 // =======================================================================
1959 static void oberon_decl_seq(oberon_context_t
* ctx
);
1960 static void oberon_statement_seq(oberon_context_t
* ctx
);
1961 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1964 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1966 if(ctx
-> token
!= token
)
1968 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
1973 oberon_assert_token(oberon_context_t
* ctx
, int token
)
1975 oberon_expect_token(ctx
, token
);
1976 oberon_read_token(ctx
);
1980 oberon_assert_ident(oberon_context_t
* ctx
)
1982 oberon_expect_token(ctx
, IDENT
);
1983 char * ident
= ctx
-> string
;
1984 oberon_read_token(ctx
);
1989 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
1991 switch(ctx
-> token
)
1994 oberon_assert_token(ctx
, STAR
);
1999 oberon_assert_token(ctx
, MINUS
);
2010 static oberon_object_t
*
2011 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2016 oberon_object_t
* x
;
2018 name
= oberon_assert_ident(ctx
);
2019 oberon_def(ctx
, &export
, &read_only
);
2021 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2026 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2029 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2030 while(ctx
-> token
== COMMA
)
2032 oberon_assert_token(ctx
, COMMA
);
2033 oberon_ident_def(ctx
, class, check_upscope
);
2039 oberon_var_decl(oberon_context_t
* ctx
)
2042 oberon_object_t
* list
;
2043 oberon_type_t
* type
;
2044 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2046 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2047 oberon_assert_token(ctx
, COLON
);
2048 oberon_type(ctx
, &type
);
2050 oberon_object_t
* var
= list
;
2051 for(int i
= 0; i
< num
; i
++)
2058 static oberon_object_t
*
2059 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2061 int class = OBERON_CLASS_PARAM
;
2062 if(ctx
-> token
== VAR
)
2064 oberon_read_token(ctx
);
2065 class = OBERON_CLASS_VAR_PARAM
;
2069 oberon_object_t
* list
;
2070 oberon_ident_list(ctx
, class, false, &num
, &list
);
2072 oberon_assert_token(ctx
, COLON
);
2074 oberon_type_t
* type
;
2075 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2076 oberon_type(ctx
, &type
);
2078 oberon_object_t
* param
= list
;
2079 for(int i
= 0; i
< num
; i
++)
2081 param
-> type
= type
;
2082 param
= param
-> next
;
2089 #define ISFPSECTION \
2090 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2093 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2095 oberon_assert_token(ctx
, LPAREN
);
2099 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2100 while(ctx
-> token
== SEMICOLON
)
2102 oberon_assert_token(ctx
, SEMICOLON
);
2103 oberon_fp_section(ctx
, &signature
-> num_decl
);
2107 oberon_assert_token(ctx
, RPAREN
);
2109 if(ctx
-> token
== COLON
)
2111 oberon_assert_token(ctx
, COLON
);
2113 oberon_object_t
* typeobj
;
2114 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2115 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2117 oberon_error(ctx
, "function result is not type");
2119 signature
-> base
= typeobj
-> type
;
2124 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2126 oberon_type_t
* signature
;
2128 signature
-> class = OBERON_TYPE_PROCEDURE
;
2129 signature
-> num_decl
= 0;
2130 signature
-> base
= ctx
-> void_type
;
2131 signature
-> decl
= NULL
;
2133 if(ctx
-> token
== LPAREN
)
2135 oberon_formal_pars(ctx
, signature
);
2140 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2142 if(a
-> num_decl
!= b
-> num_decl
)
2144 oberon_error(ctx
, "number parameters not matched");
2147 int num_param
= a
-> num_decl
;
2148 oberon_object_t
* param_a
= a
-> decl
;
2149 oberon_object_t
* param_b
= b
-> decl
;
2150 for(int i
= 0; i
< num_param
; i
++)
2152 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2154 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2157 if(param_a
-> type
!= param_b
-> type
)
2159 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2162 param_a
= param_a
-> next
;
2163 param_b
= param_b
-> next
;
2168 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2170 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2171 oberon_type_t
* result_type
= proc
-> type
-> base
;
2173 if(result_type
-> class == OBERON_TYPE_VOID
)
2177 oberon_error(ctx
, "procedure has no result type");
2184 oberon_error(ctx
, "procedure requires expression on result");
2187 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2190 proc
-> has_return
= 1;
2192 oberon_generate_return(ctx
, expr
);
2196 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2198 oberon_assert_token(ctx
, SEMICOLON
);
2200 ctx
-> decl
= proc
-> scope
;
2202 oberon_decl_seq(ctx
);
2204 oberon_generate_begin_proc(ctx
, proc
);
2206 if(ctx
-> token
== BEGIN
)
2208 oberon_assert_token(ctx
, BEGIN
);
2209 oberon_statement_seq(ctx
);
2212 oberon_assert_token(ctx
, END
);
2213 char * name
= oberon_assert_ident(ctx
);
2214 if(strcmp(name
, proc
-> name
) != 0)
2216 oberon_error(ctx
, "procedure name not matched");
2219 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2220 && proc
-> has_return
== 0)
2222 oberon_make_return(ctx
, NULL
);
2225 if(proc
-> has_return
== 0)
2227 oberon_error(ctx
, "procedure requires return");
2230 oberon_generate_end_proc(ctx
);
2231 oberon_close_scope(ctx
-> decl
);
2235 oberon_proc_decl(oberon_context_t
* ctx
)
2237 oberon_assert_token(ctx
, PROCEDURE
);
2240 if(ctx
-> token
== UPARROW
)
2242 oberon_assert_token(ctx
, UPARROW
);
2249 name
= oberon_assert_ident(ctx
);
2250 oberon_def(ctx
, &export
, &read_only
);
2252 oberon_scope_t
* proc_scope
;
2253 proc_scope
= oberon_open_scope(ctx
);
2254 ctx
-> decl
-> local
= 1;
2256 oberon_type_t
* signature
;
2257 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2258 oberon_opt_formal_pars(ctx
, &signature
);
2260 oberon_initialize_decl(ctx
);
2261 oberon_generator_init_type(ctx
, signature
);
2262 oberon_close_scope(ctx
-> decl
);
2264 oberon_object_t
* proc
;
2265 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2268 if(proc
-> class != OBERON_CLASS_PROC
)
2270 oberon_error(ctx
, "mult definition");
2277 oberon_error(ctx
, "mult procedure definition");
2281 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2283 oberon_error(ctx
, "export type not matched");
2286 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2290 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2291 proc
-> type
= signature
;
2292 proc
-> scope
= proc_scope
;
2293 oberon_generator_init_proc(ctx
, proc
);
2296 proc
-> scope
-> parent
= proc
;
2301 oberon_proc_decl_body(ctx
, proc
);
2306 oberon_const_decl(oberon_context_t
* ctx
)
2308 oberon_item_t
* value
;
2309 oberon_object_t
* constant
;
2311 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2312 oberon_assert_token(ctx
, EQUAL
);
2313 value
= oberon_const_expr(ctx
);
2314 constant
-> value
= value
;
2318 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2320 if(size
-> is_item
== 0)
2322 oberon_error(ctx
, "requires constant");
2325 if(size
-> item
.mode
!= MODE_INTEGER
)
2327 oberon_error(ctx
, "requires integer constant");
2330 oberon_type_t
* arr
;
2332 arr
-> class = OBERON_TYPE_ARRAY
;
2333 arr
-> size
= size
-> item
.integer
;
2338 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2341 oberon_object_t
* to
;
2343 to
= oberon_qualident(ctx
, &name
, 0);
2345 //name = oberon_assert_ident(ctx);
2346 //to = oberon_find_object(ctx -> decl, name, 0);
2350 if(to
-> class != OBERON_CLASS_TYPE
)
2352 oberon_error(ctx
, "not a type");
2357 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2358 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2364 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2367 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2371 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2379 oberon_type_t
* dim
;
2380 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2382 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2384 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2388 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2390 type
-> class = OBERON_TYPE_ARRAY
;
2392 type
-> base
= base
;
2396 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2398 if(ctx
-> token
== IDENT
)
2401 oberon_object_t
* list
;
2402 oberon_type_t
* type
;
2403 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2405 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2406 oberon_assert_token(ctx
, COLON
);
2408 oberon_scope_t
* current
= ctx
-> decl
;
2409 ctx
-> decl
= modscope
;
2410 oberon_type(ctx
, &type
);
2411 ctx
-> decl
= current
;
2413 oberon_object_t
* field
= list
;
2414 for(int i
= 0; i
< num
; i
++)
2416 field
-> type
= type
;
2417 field
= field
-> next
;
2420 rec
-> num_decl
+= num
;
2425 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2427 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2428 oberon_scope_t
* oldscope
= ctx
-> decl
;
2429 ctx
-> decl
= modscope
;
2431 if(ctx
-> token
== LPAREN
)
2433 oberon_assert_token(ctx
, LPAREN
);
2435 oberon_object_t
* typeobj
;
2436 typeobj
= oberon_qualident(ctx
, NULL
, true);
2438 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2440 oberon_error(ctx
, "base must be type");
2443 oberon_type_t
* base
= typeobj
-> type
;
2444 if(base
-> class == OBERON_TYPE_POINTER
)
2446 base
= base
-> base
;
2449 if(base
-> class != OBERON_TYPE_RECORD
)
2451 oberon_error(ctx
, "base must be record type");
2455 ctx
-> decl
= base
-> scope
;
2457 oberon_assert_token(ctx
, RPAREN
);
2464 oberon_scope_t
* this_scope
;
2465 this_scope
= oberon_open_scope(ctx
);
2466 this_scope
-> local
= true;
2467 this_scope
-> parent
= NULL
;
2468 this_scope
-> parent_type
= rec
;
2470 oberon_field_list(ctx
, rec
, modscope
);
2471 while(ctx
-> token
== SEMICOLON
)
2473 oberon_assert_token(ctx
, SEMICOLON
);
2474 oberon_field_list(ctx
, rec
, modscope
);
2477 rec
-> scope
= this_scope
;
2478 rec
-> decl
= this_scope
-> list
-> next
;
2479 ctx
-> decl
= oldscope
;
2483 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2485 if(ctx
-> token
== IDENT
)
2487 oberon_qualident_type(ctx
, type
);
2489 else if(ctx
-> token
== ARRAY
)
2491 oberon_assert_token(ctx
, ARRAY
);
2494 oberon_expr_t
* sizes
;
2496 if(ISEXPR(ctx
-> token
))
2498 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2501 oberon_assert_token(ctx
, OF
);
2503 oberon_type_t
* base
;
2504 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2505 oberon_type(ctx
, &base
);
2509 oberon_make_open_array(ctx
, base
, *type
);
2513 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2516 else if(ctx
-> token
== RECORD
)
2518 oberon_type_t
* rec
;
2520 rec
-> class = OBERON_TYPE_RECORD
;
2521 rec
-> module
= ctx
-> mod
;
2523 oberon_assert_token(ctx
, RECORD
);
2524 oberon_type_record_body(ctx
, rec
);
2525 oberon_assert_token(ctx
, END
);
2529 else if(ctx
-> token
== POINTER
)
2531 oberon_assert_token(ctx
, POINTER
);
2532 oberon_assert_token(ctx
, TO
);
2534 oberon_type_t
* base
;
2535 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2536 oberon_type(ctx
, &base
);
2538 oberon_type_t
* ptr
;
2540 ptr
-> class = OBERON_TYPE_POINTER
;
2543 else if(ctx
-> token
== PROCEDURE
)
2545 oberon_open_scope(ctx
);
2546 oberon_assert_token(ctx
, PROCEDURE
);
2547 oberon_opt_formal_pars(ctx
, type
);
2548 oberon_close_scope(ctx
-> decl
);
2552 oberon_error(ctx
, "invalid type declaration");
2557 oberon_type_decl(oberon_context_t
* ctx
)
2560 oberon_object_t
* newtype
;
2561 oberon_type_t
* type
;
2565 name
= oberon_assert_ident(ctx
);
2566 oberon_def(ctx
, &export
, &read_only
);
2568 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2571 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2572 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2573 assert(newtype
-> type
);
2577 if(newtype
-> class != OBERON_CLASS_TYPE
)
2579 oberon_error(ctx
, "mult definition");
2582 if(newtype
-> linked
)
2584 oberon_error(ctx
, "mult definition - already linked");
2587 newtype
-> export
= export
;
2588 newtype
-> read_only
= read_only
;
2591 oberon_assert_token(ctx
, EQUAL
);
2593 type
= newtype
-> type
;
2594 oberon_type(ctx
, &type
);
2596 if(type
-> class == OBERON_TYPE_VOID
)
2598 oberon_error(ctx
, "recursive alias declaration");
2601 newtype
-> type
= type
;
2602 newtype
-> linked
= 1;
2605 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2606 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2609 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POINTER
2612 && type
-> class != OBERON_TYPE_ARRAY
)
2617 if(type
-> recursive
)
2619 oberon_error(ctx
, "recursive pointer declaration");
2622 if(type
-> class == OBERON_TYPE_POINTER
2623 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2625 oberon_error(ctx
, "attempt to make pointer to pointer");
2628 type
-> recursive
= 1;
2630 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2632 type
-> recursive
= 0;
2636 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2638 if(type
-> class != OBERON_TYPE_RECORD
)
2643 if(type
-> recursive
)
2645 oberon_error(ctx
, "recursive record declaration");
2648 type
-> recursive
= 1;
2650 int num_fields
= type
-> num_decl
;
2651 oberon_object_t
* field
= type
-> decl
;
2652 for(int i
= 0; i
< num_fields
; i
++)
2654 oberon_prevent_recursive_object(ctx
, field
);
2655 field
= field
-> next
;
2658 type
-> recursive
= 0;
2661 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2663 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2668 if(type
-> recursive
)
2670 oberon_error(ctx
, "recursive procedure declaration");
2673 type
-> recursive
= 1;
2675 int num_fields
= type
-> num_decl
;
2676 oberon_object_t
* field
= type
-> decl
;
2677 for(int i
= 0; i
< num_fields
; i
++)
2679 oberon_prevent_recursive_object(ctx
, field
);
2680 field
= field
-> next
;
2683 type
-> recursive
= 0;
2687 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2689 if(type
-> class != OBERON_TYPE_ARRAY
)
2694 if(type
-> recursive
)
2696 oberon_error(ctx
, "recursive array declaration");
2699 type
-> recursive
= 1;
2701 oberon_prevent_recursive_type(ctx
, type
-> base
);
2703 type
-> recursive
= 0;
2707 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2709 if(type
-> class == OBERON_TYPE_POINTER
)
2711 oberon_prevent_recursive_pointer(ctx
, type
);
2713 else if(type
-> class == OBERON_TYPE_RECORD
)
2715 oberon_prevent_recursive_record(ctx
, type
);
2717 else if(type
-> class == OBERON_TYPE_ARRAY
)
2719 oberon_prevent_recursive_array(ctx
, type
);
2721 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2723 oberon_prevent_recursive_procedure(ctx
, type
);
2728 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2732 case OBERON_CLASS_VAR
:
2733 case OBERON_CLASS_TYPE
:
2734 case OBERON_CLASS_PARAM
:
2735 case OBERON_CLASS_VAR_PARAM
:
2736 case OBERON_CLASS_FIELD
:
2737 oberon_prevent_recursive_type(ctx
, x
-> type
);
2739 case OBERON_CLASS_CONST
:
2740 case OBERON_CLASS_PROC
:
2741 case OBERON_CLASS_MODULE
:
2744 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2750 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2752 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2756 oberon_prevent_recursive_object(ctx
, x
);
2761 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2762 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2765 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2767 if(type
-> class != OBERON_TYPE_RECORD
)
2772 int num_fields
= type
-> num_decl
;
2773 oberon_object_t
* field
= type
-> decl
;
2774 for(int i
= 0; i
< num_fields
; i
++)
2776 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2778 oberon_initialize_type(ctx
, field
-> type
);
2781 oberon_initialize_object(ctx
, field
);
2782 field
= field
-> next
;
2785 oberon_generator_init_record(ctx
, type
);
2789 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2791 if(type
-> class == OBERON_TYPE_VOID
)
2793 oberon_error(ctx
, "undeclarated type");
2796 if(type
-> initialized
)
2801 type
-> initialized
= 1;
2803 if(type
-> class == OBERON_TYPE_POINTER
)
2805 oberon_initialize_type(ctx
, type
-> base
);
2806 oberon_generator_init_type(ctx
, type
);
2808 else if(type
-> class == OBERON_TYPE_ARRAY
)
2810 if(type
-> size
!= 0)
2812 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2814 if(type
-> base
-> size
== 0)
2816 oberon_error(ctx
, "open array not allowed as array element");
2821 oberon_initialize_type(ctx
, type
-> base
);
2822 oberon_generator_init_type(ctx
, type
);
2824 else if(type
-> class == OBERON_TYPE_RECORD
)
2826 oberon_generator_init_type(ctx
, type
);
2827 oberon_initialize_record_fields(ctx
, type
);
2829 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2831 int num_fields
= type
-> num_decl
;
2832 oberon_object_t
* field
= type
-> decl
;
2833 for(int i
= 0; i
< num_fields
; i
++)
2835 oberon_initialize_object(ctx
, field
);
2836 field
= field
-> next
;
2839 oberon_generator_init_type(ctx
, type
);
2843 oberon_generator_init_type(ctx
, type
);
2848 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2850 if(x
-> initialized
)
2855 x
-> initialized
= 1;
2859 case OBERON_CLASS_TYPE
:
2860 oberon_initialize_type(ctx
, x
-> type
);
2862 case OBERON_CLASS_VAR
:
2863 case OBERON_CLASS_FIELD
:
2864 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2866 if(x
-> type
-> size
== 0)
2868 oberon_error(ctx
, "open array not allowed as variable or field");
2871 oberon_initialize_type(ctx
, x
-> type
);
2872 oberon_generator_init_var(ctx
, x
);
2874 case OBERON_CLASS_PARAM
:
2875 case OBERON_CLASS_VAR_PARAM
:
2876 oberon_initialize_type(ctx
, x
-> type
);
2877 oberon_generator_init_var(ctx
, x
);
2879 case OBERON_CLASS_CONST
:
2880 case OBERON_CLASS_PROC
:
2881 case OBERON_CLASS_MODULE
:
2884 oberon_error(ctx
, "oberon_initialize_object: wat");
2890 oberon_initialize_decl(oberon_context_t
* ctx
)
2892 oberon_object_t
* x
= ctx
-> decl
-> list
;
2896 oberon_initialize_object(ctx
, x
-> next
);
2902 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2904 oberon_object_t
* x
= ctx
-> decl
-> list
;
2908 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2910 if(x
-> next
-> linked
== 0)
2912 oberon_error(ctx
, "unresolved forward declaration");
2920 oberon_decl_seq(oberon_context_t
* ctx
)
2922 if(ctx
-> token
== CONST
)
2924 oberon_assert_token(ctx
, CONST
);
2925 while(ctx
-> token
== IDENT
)
2927 oberon_const_decl(ctx
);
2928 oberon_assert_token(ctx
, SEMICOLON
);
2932 if(ctx
-> token
== TYPE
)
2934 oberon_assert_token(ctx
, TYPE
);
2935 while(ctx
-> token
== IDENT
)
2937 oberon_type_decl(ctx
);
2938 oberon_assert_token(ctx
, SEMICOLON
);
2942 if(ctx
-> token
== VAR
)
2944 oberon_assert_token(ctx
, VAR
);
2945 while(ctx
-> token
== IDENT
)
2947 oberon_var_decl(ctx
);
2948 oberon_assert_token(ctx
, SEMICOLON
);
2952 oberon_prevent_recursive_decl(ctx
);
2953 oberon_initialize_decl(ctx
);
2955 while(ctx
-> token
== PROCEDURE
)
2957 oberon_proc_decl(ctx
);
2958 oberon_assert_token(ctx
, SEMICOLON
);
2961 oberon_prevent_undeclarated_procedures(ctx
);
2964 static oberon_expr_t
*
2965 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
2967 oberon_object_t
* x
;
2968 oberon_expr_t
* expr
;
2970 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
2973 oberon_generator_init_temp_var(ctx
, x
);
2975 expr
= oberon_new_item(MODE_VAR
, type
, false);
2976 expr
-> item
.var
= x
;
2981 oberon_statement_seq(oberon_context_t
* ctx
);
2984 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
2986 if(dst
-> read_only
)
2988 oberon_error(ctx
, "read-only destination");
2991 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
2992 oberon_generate_assign(ctx
, src
, dst
);
2996 oberon_statement(oberon_context_t
* ctx
)
2998 oberon_expr_t
* item1
;
2999 oberon_expr_t
* item2
;
3001 if(ctx
-> token
== IDENT
)
3003 item1
= oberon_designator(ctx
);
3004 if(ctx
-> token
== ASSIGN
)
3006 oberon_assert_token(ctx
, ASSIGN
);
3007 item2
= oberon_expr(ctx
);
3008 oberon_assign(ctx
, item2
, item1
);
3012 oberon_opt_proc_parens(ctx
, item1
);
3015 else if(ctx
-> token
== IF
)
3019 oberon_expr_t
* cond
;
3021 els
= oberon_generator_reserve_label(ctx
);
3022 end
= oberon_generator_reserve_label(ctx
);
3024 oberon_assert_token(ctx
, IF
);
3025 cond
= oberon_expr(ctx
);
3026 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3028 oberon_error(ctx
, "condition must be boolean");
3030 oberon_assert_token(ctx
, THEN
);
3031 oberon_generate_branch(ctx
, cond
, false, els
);
3032 oberon_statement_seq(ctx
);
3033 oberon_generate_goto(ctx
, end
);
3034 oberon_generate_label(ctx
, els
);
3036 while(ctx
-> token
== ELSIF
)
3038 els
= oberon_generator_reserve_label(ctx
);
3040 oberon_assert_token(ctx
, ELSIF
);
3041 cond
= oberon_expr(ctx
);
3042 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3044 oberon_error(ctx
, "condition must be boolean");
3046 oberon_assert_token(ctx
, THEN
);
3047 oberon_generate_branch(ctx
, cond
, false, els
);
3048 oberon_statement_seq(ctx
);
3049 oberon_generate_goto(ctx
, end
);
3050 oberon_generate_label(ctx
, els
);
3053 if(ctx
-> token
== ELSE
)
3055 oberon_assert_token(ctx
, ELSE
);
3056 oberon_statement_seq(ctx
);
3059 oberon_generate_label(ctx
, end
);
3060 oberon_assert_token(ctx
, END
);
3062 else if(ctx
-> token
== WHILE
)
3064 gen_label_t
* begin
;
3066 oberon_expr_t
* cond
;
3068 begin
= oberon_generator_reserve_label(ctx
);
3069 end
= oberon_generator_reserve_label(ctx
);
3071 oberon_assert_token(ctx
, WHILE
);
3072 oberon_generate_label(ctx
, begin
);
3073 cond
= oberon_expr(ctx
);
3074 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3076 oberon_error(ctx
, "condition must be boolean");
3078 oberon_generate_branch(ctx
, cond
, false, end
);
3080 oberon_assert_token(ctx
, DO
);
3081 oberon_statement_seq(ctx
);
3082 oberon_generate_goto(ctx
, begin
);
3084 oberon_assert_token(ctx
, END
);
3085 oberon_generate_label(ctx
, end
);
3087 else if(ctx
-> token
== REPEAT
)
3089 gen_label_t
* begin
;
3090 oberon_expr_t
* cond
;
3092 begin
= oberon_generator_reserve_label(ctx
);
3093 oberon_generate_label(ctx
, begin
);
3094 oberon_assert_token(ctx
, REPEAT
);
3096 oberon_statement_seq(ctx
);
3098 oberon_assert_token(ctx
, UNTIL
);
3100 cond
= oberon_expr(ctx
);
3101 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3103 oberon_error(ctx
, "condition must be boolean");
3106 oberon_generate_branch(ctx
, cond
, true, begin
);
3108 else if(ctx
-> token
== FOR
)
3110 oberon_expr_t
* from
;
3111 oberon_expr_t
* index
;
3113 oberon_expr_t
* bound
;
3115 oberon_expr_t
* cond
;
3116 oberon_expr_t
* count
;
3117 gen_label_t
* begin
;
3122 begin
= oberon_generator_reserve_label(ctx
);
3123 end
= oberon_generator_reserve_label(ctx
);
3125 oberon_assert_token(ctx
, FOR
);
3126 iname
= oberon_assert_ident(ctx
);
3127 index
= oberon_ident_item(ctx
, iname
);
3128 oberon_assert_token(ctx
, ASSIGN
);
3129 from
= oberon_expr(ctx
);
3130 oberon_assign(ctx
, from
, index
);
3131 oberon_assert_token(ctx
, TO
);
3132 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3133 to
= oberon_expr(ctx
);
3134 oberon_assign(ctx
, to
, bound
);
3135 if(ctx
-> token
== BY
)
3137 oberon_assert_token(ctx
, BY
);
3138 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3142 by
= oberon_integer_item(ctx
, 1);
3145 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3147 oberon_error(ctx
, "must be integer");
3150 if(by
-> item
.integer
> 0)
3154 else if(by
-> item
.integer
< 0)
3160 oberon_error(ctx
, "zero step not allowed");
3163 oberon_assert_token(ctx
, DO
);
3164 oberon_generate_label(ctx
, begin
);
3165 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3166 oberon_generate_branch(ctx
, cond
, false, end
);
3167 oberon_statement_seq(ctx
);
3168 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3169 oberon_assign(ctx
, count
, index
);
3170 oberon_generate_goto(ctx
, begin
);
3171 oberon_generate_label(ctx
, end
);
3172 oberon_assert_token(ctx
, END
);
3174 else if(ctx
-> token
== LOOP
)
3176 gen_label_t
* begin
;
3179 begin
= oberon_generator_reserve_label(ctx
);
3180 end
= oberon_generator_reserve_label(ctx
);
3182 oberon_open_scope(ctx
);
3183 oberon_assert_token(ctx
, LOOP
);
3184 oberon_generate_label(ctx
, begin
);
3185 ctx
-> decl
-> exit_label
= end
;
3186 oberon_statement_seq(ctx
);
3187 oberon_generate_goto(ctx
, begin
);
3188 oberon_generate_label(ctx
, end
);
3189 oberon_assert_token(ctx
, END
);
3190 oberon_close_scope(ctx
-> decl
);
3192 else if(ctx
-> token
== EXIT
)
3194 oberon_assert_token(ctx
, EXIT
);
3195 if(ctx
-> decl
-> exit_label
== NULL
)
3197 oberon_error(ctx
, "not in LOOP-END");
3199 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3201 else if(ctx
-> token
== RETURN
)
3203 oberon_assert_token(ctx
, RETURN
);
3204 if(ISEXPR(ctx
-> token
))
3206 oberon_expr_t
* expr
;
3207 expr
= oberon_expr(ctx
);
3208 oberon_make_return(ctx
, expr
);
3212 oberon_make_return(ctx
, NULL
);
3218 oberon_statement_seq(oberon_context_t
* ctx
)
3220 oberon_statement(ctx
);
3221 while(ctx
-> token
== SEMICOLON
)
3223 oberon_assert_token(ctx
, SEMICOLON
);
3224 oberon_statement(ctx
);
3229 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3231 oberon_module_t
* m
= ctx
-> module_list
;
3232 while(m
&& strcmp(m
-> name
, name
) != 0)
3240 code
= ctx
-> import_module(name
);
3243 oberon_error(ctx
, "no such module");
3246 m
= oberon_compile_module(ctx
, code
);
3252 oberon_error(ctx
, "cyclic module import");
3255 oberon_object_t
* ident
;
3256 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3257 ident
-> module
= m
;
3261 oberon_import_decl(oberon_context_t
* ctx
)
3266 alias
= name
= oberon_assert_ident(ctx
);
3267 if(ctx
-> token
== ASSIGN
)
3269 oberon_assert_token(ctx
, ASSIGN
);
3270 name
= oberon_assert_ident(ctx
);
3273 oberon_import_module(ctx
, alias
, name
);
3277 oberon_import_list(oberon_context_t
* ctx
)
3279 oberon_assert_token(ctx
, IMPORT
);
3281 oberon_import_decl(ctx
);
3282 while(ctx
-> token
== COMMA
)
3284 oberon_assert_token(ctx
, COMMA
);
3285 oberon_import_decl(ctx
);
3288 oberon_assert_token(ctx
, SEMICOLON
);
3292 oberon_parse_module(oberon_context_t
* ctx
)
3296 oberon_read_token(ctx
);
3298 oberon_assert_token(ctx
, MODULE
);
3299 name1
= oberon_assert_ident(ctx
);
3300 oberon_assert_token(ctx
, SEMICOLON
);
3301 ctx
-> mod
-> name
= name1
;
3303 oberon_generator_init_module(ctx
, ctx
-> mod
);
3305 if(ctx
-> token
== IMPORT
)
3307 oberon_import_list(ctx
);
3310 oberon_decl_seq(ctx
);
3312 oberon_generate_begin_module(ctx
);
3313 if(ctx
-> token
== BEGIN
)
3315 oberon_assert_token(ctx
, BEGIN
);
3316 oberon_statement_seq(ctx
);
3318 oberon_generate_end_module(ctx
);
3320 oberon_assert_token(ctx
, END
);
3321 name2
= oberon_assert_ident(ctx
);
3322 oberon_assert_token(ctx
, DOT
);
3324 if(strcmp(name1
, name2
) != 0)
3326 oberon_error(ctx
, "module name not matched");
3329 oberon_generator_fini_module(ctx
-> mod
);
3332 // =======================================================================
3334 // =======================================================================
3337 register_default_types(oberon_context_t
* ctx
)
3339 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3340 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3342 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3343 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3344 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3346 ctx
-> string_type
= oberon_new_type_string(1);
3347 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3349 ctx
-> bool_type
= oberon_new_type_boolean();
3350 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3352 ctx
-> byte_type
= oberon_new_type_integer(1);
3353 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3355 ctx
-> shortint_type
= oberon_new_type_integer(2);
3356 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3358 ctx
-> int_type
= oberon_new_type_integer(4);
3359 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3361 ctx
-> longint_type
= oberon_new_type_integer(8);
3362 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3364 ctx
-> real_type
= oberon_new_type_real(4);
3365 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3367 ctx
-> longreal_type
= oberon_new_type_real(8);
3368 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3370 ctx
-> char_type
= oberon_new_type_char(1);
3371 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3375 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3377 oberon_object_t
* proc
;
3378 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3379 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3380 proc
-> type
-> sysproc
= true;
3381 proc
-> type
-> genfunc
= f
;
3382 proc
-> type
-> genproc
= p
;
3385 static oberon_expr_t
*
3386 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3390 oberon_error(ctx
, "too few arguments");
3395 oberon_error(ctx
, "too mach arguments");
3398 oberon_expr_t
* arg
;
3401 oberon_type_t
* result_type
;
3402 result_type
= arg
-> result
;
3404 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3406 oberon_error(ctx
, "ABS accepts only integers");
3410 oberon_expr_t
* expr
;
3411 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3416 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3420 oberon_error(ctx
, "too few arguments");
3423 oberon_expr_t
* dst
;
3426 oberon_type_t
* type
;
3427 type
= dst
-> result
;
3429 if(type
-> class != OBERON_TYPE_POINTER
)
3431 oberon_error(ctx
, "not a pointer");
3434 type
= type
-> base
;
3436 oberon_expr_t
* src
;
3437 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3438 src
-> item
.num_args
= 0;
3439 src
-> item
.args
= NULL
;
3442 if(type
-> class == OBERON_TYPE_ARRAY
)
3444 if(type
-> size
== 0)
3446 oberon_type_t
* x
= type
;
3447 while(x
-> class == OBERON_TYPE_ARRAY
)
3457 if(num_args
< max_args
)
3459 oberon_error(ctx
, "too few arguments");
3462 if(num_args
> max_args
)
3464 oberon_error(ctx
, "too mach arguments");
3467 int num_sizes
= max_args
- 1;
3468 oberon_expr_t
* size_list
= list_args
-> next
;
3470 oberon_expr_t
* arg
= size_list
;
3471 for(int i
= 0; i
< max_args
- 1; i
++)
3473 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3475 oberon_error(ctx
, "size must be integer");
3480 src
-> item
.num_args
= num_sizes
;
3481 src
-> item
.args
= size_list
;
3483 else if(type
-> class != OBERON_TYPE_RECORD
)
3485 oberon_error(ctx
, "oberon_make_new_call: wat");
3488 if(num_args
> max_args
)
3490 oberon_error(ctx
, "too mach arguments");
3493 oberon_assign(ctx
, src
, dst
);
3497 oberon_create_context(ModuleImportCallback import_module
)
3499 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3501 oberon_scope_t
* world_scope
;
3502 world_scope
= oberon_open_scope(ctx
);
3503 ctx
-> world_scope
= world_scope
;
3505 ctx
-> import_module
= import_module
;
3507 oberon_generator_init_context(ctx
);
3509 register_default_types(ctx
);
3510 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3511 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3517 oberon_destroy_context(oberon_context_t
* ctx
)
3519 oberon_generator_destroy_context(ctx
);
3524 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3526 const char * code
= ctx
-> code
;
3527 int code_index
= ctx
-> code_index
;
3529 int token
= ctx
-> token
;
3530 char * string
= ctx
-> string
;
3531 int integer
= ctx
-> integer
;
3532 int real
= ctx
-> real
;
3533 bool longmode
= ctx
-> longmode
;
3534 oberon_scope_t
* decl
= ctx
-> decl
;
3535 oberon_module_t
* mod
= ctx
-> mod
;
3537 oberon_scope_t
* module_scope
;
3538 module_scope
= oberon_open_scope(ctx
);
3540 oberon_module_t
* module
;
3541 module
= calloc(1, sizeof *module
);
3542 module
-> decl
= module_scope
;
3543 module
-> next
= ctx
-> module_list
;
3545 ctx
-> mod
= module
;
3546 ctx
-> module_list
= module
;
3548 oberon_init_scaner(ctx
, newcode
);
3549 oberon_parse_module(ctx
);
3551 module
-> ready
= 1;
3554 ctx
-> code_index
= code_index
;
3556 ctx
-> token
= token
;
3557 ctx
-> string
= string
;
3558 ctx
-> integer
= integer
;
3560 ctx
-> longmode
= longmode
;