10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
84 // =======================================================================
86 // =======================================================================
89 oberon_make_copy_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
);
92 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
96 fprintf(stderr
, "error: ");
97 vfprintf(stderr
, fmt
, ptr
);
98 fprintf(stderr
, "\n");
99 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
100 fprintf(stderr
, " c = %c\n", ctx
-> c
);
101 fprintf(stderr
, " token = %i\n", ctx
-> token
);
106 static oberon_type_t
*
107 oberon_new_type_ptr(int class)
109 oberon_type_t
* x
= malloc(sizeof *x
);
110 memset(x
, 0, sizeof *x
);
115 static oberon_type_t
*
116 oberon_new_type_integer(int size
)
119 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
124 static oberon_type_t
*
125 oberon_new_type_boolean()
128 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
132 static oberon_type_t
*
133 oberon_new_type_real(int size
)
136 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
141 static oberon_type_t
*
142 oberon_new_type_char(int size
)
145 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
150 static oberon_type_t
*
151 oberon_new_type_string(int size
)
154 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
159 static oberon_type_t
*
160 oberon_new_type_set(int size
)
163 x
= oberon_new_type_ptr(OBERON_TYPE_SET
);
168 // =======================================================================
170 // =======================================================================
172 static oberon_scope_t
*
173 oberon_open_scope(oberon_context_t
* ctx
)
175 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
176 oberon_object_t
* list
= calloc(1, sizeof *list
);
179 scope
-> list
= list
;
180 scope
-> up
= ctx
-> decl
;
184 scope
-> local
= scope
-> up
-> local
;
185 scope
-> parent
= scope
-> up
-> parent
;
186 scope
-> parent_type
= scope
-> up
-> parent_type
;
187 scope
-> exit_label
= scope
-> up
-> exit_label
;
195 oberon_close_scope(oberon_scope_t
* scope
)
197 oberon_context_t
* ctx
= scope
-> ctx
;
198 ctx
-> decl
= scope
-> up
;
201 static oberon_object_t
*
202 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
204 oberon_object_t
* x
= list
;
205 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
212 static oberon_object_t
*
213 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
215 oberon_object_t
* result
= NULL
;
217 oberon_scope_t
* s
= scope
;
218 while(result
== NULL
&& s
!= NULL
)
220 result
= oberon_find_object_in_list(s
-> list
, name
);
224 if(check_it
&& result
== NULL
)
226 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
232 static oberon_object_t
*
233 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
235 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
236 memset(newvar
, 0, sizeof *newvar
);
237 newvar
-> name
= name
;
238 newvar
-> class = class;
239 newvar
-> export
= export
;
240 newvar
-> read_only
= read_only
;
241 newvar
-> local
= scope
-> local
;
242 newvar
-> parent
= scope
-> parent
;
243 newvar
-> parent_type
= scope
-> parent_type
;
244 newvar
-> module
= scope
-> ctx
-> mod
;
248 static oberon_object_t
*
249 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
253 if(oberon_find_object(scope
-> up
, name
, false))
255 oberon_error(scope
-> ctx
, "already defined");
259 oberon_object_t
* x
= scope
-> list
;
260 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
267 oberon_error(scope
-> ctx
, "already defined");
270 oberon_object_t
* newvar
;
271 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
277 static oberon_object_t
*
278 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
280 oberon_object_t
* id
;
281 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
283 oberon_generator_init_type(scope
-> ctx
, type
);
287 // =======================================================================
289 // =======================================================================
292 oberon_get_char(oberon_context_t
* ctx
)
294 if(ctx
-> code
[ctx
-> code_index
])
296 ctx
-> code_index
+= 1;
297 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
302 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
305 ctx
-> code_index
= 0;
306 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
310 oberon_read_ident(oberon_context_t
* ctx
)
313 int i
= ctx
-> code_index
;
315 int c
= ctx
-> code
[i
];
323 char * ident
= malloc(len
+ 1);
324 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
327 ctx
-> code_index
= i
;
328 ctx
-> c
= ctx
-> code
[i
];
329 ctx
-> string
= ident
;
330 ctx
-> token
= IDENT
;
332 if(strcmp(ident
, "MODULE") == 0)
334 ctx
-> token
= MODULE
;
336 else if(strcmp(ident
, "END") == 0)
340 else if(strcmp(ident
, "VAR") == 0)
344 else if(strcmp(ident
, "BEGIN") == 0)
346 ctx
-> token
= BEGIN
;
348 else if(strcmp(ident
, "OR") == 0)
352 else if(strcmp(ident
, "DIV") == 0)
356 else if(strcmp(ident
, "MOD") == 0)
360 else if(strcmp(ident
, "PROCEDURE") == 0)
362 ctx
-> token
= PROCEDURE
;
364 else if(strcmp(ident
, "RETURN") == 0)
366 ctx
-> token
= RETURN
;
368 else if(strcmp(ident
, "CONST") == 0)
370 ctx
-> token
= CONST
;
372 else if(strcmp(ident
, "TYPE") == 0)
376 else if(strcmp(ident
, "ARRAY") == 0)
378 ctx
-> token
= ARRAY
;
380 else if(strcmp(ident
, "OF") == 0)
384 else if(strcmp(ident
, "RECORD") == 0)
386 ctx
-> token
= RECORD
;
388 else if(strcmp(ident
, "POINTER") == 0)
390 ctx
-> token
= POINTER
;
392 else if(strcmp(ident
, "TO") == 0)
396 else if(strcmp(ident
, "NIL") == 0)
400 else if(strcmp(ident
, "IMPORT") == 0)
402 ctx
-> token
= IMPORT
;
404 else if(strcmp(ident
, "IN") == 0)
408 else if(strcmp(ident
, "IS") == 0)
412 else if(strcmp(ident
, "IF") == 0)
416 else if(strcmp(ident
, "THEN") == 0)
420 else if(strcmp(ident
, "ELSE") == 0)
424 else if(strcmp(ident
, "ELSIF") == 0)
426 ctx
-> token
= ELSIF
;
428 else if(strcmp(ident
, "WHILE") == 0)
430 ctx
-> token
= WHILE
;
432 else if(strcmp(ident
, "DO") == 0)
436 else if(strcmp(ident
, "REPEAT") == 0)
438 ctx
-> token
= REPEAT
;
440 else if(strcmp(ident
, "UNTIL") == 0)
442 ctx
-> token
= UNTIL
;
444 else if(strcmp(ident
, "FOR") == 0)
448 else if(strcmp(ident
, "BY") == 0)
452 else if(strcmp(ident
, "LOOP") == 0)
456 else if(strcmp(ident
, "EXIT") == 0)
460 else if(strcmp(ident
, "CASE") == 0)
464 else if(strcmp(ident
, "WITH") == 0)
470 #define ISHEXDIGIT(x) \
471 (((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F'))
474 oberon_read_number(oberon_context_t
* ctx
)
487 * mode = 3 == LONGREAL
491 start_i
= ctx
-> code_index
;
493 while(isdigit(ctx
-> c
))
495 oberon_get_char(ctx
);
498 end_i
= ctx
-> code_index
;
500 if(ISHEXDIGIT(ctx
-> c
))
503 while(ISHEXDIGIT(ctx
-> c
))
505 oberon_get_char(ctx
);
508 end_i
= ctx
-> code_index
;
513 oberon_get_char(ctx
);
515 else if(ctx
-> c
== 'X')
518 oberon_get_char(ctx
);
522 oberon_error(ctx
, "invalid hex number");
525 else if(ctx
-> c
== '.')
527 oberon_get_char(ctx
);
530 /* Чит: избегаем конфликта с DOTDOT */
531 ctx
-> code_index
-= 1;
537 while(isdigit(ctx
-> c
))
539 oberon_get_char(ctx
);
542 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
544 exp_i
= ctx
-> code_index
;
551 oberon_get_char(ctx
);
553 if(ctx
-> c
== '+' || ctx
-> c
== '-')
555 oberon_get_char(ctx
);
558 while(isdigit(ctx
-> c
))
560 oberon_get_char(ctx
);
564 end_i
= ctx
-> code_index
;
572 oberon_get_char(ctx
);
574 else if(ctx
-> c
== 'X')
577 oberon_get_char(ctx
);
581 int len
= end_i
- start_i
;
582 ident
= malloc(len
+ 1);
583 memcpy(ident
, &ctx
-> code
[start_i
], len
);
586 ctx
-> longmode
= false;
589 int i
= exp_i
- start_i
;
591 ctx
-> longmode
= true;
597 integer
= atol(ident
);
599 ctx
-> token
= INTEGER
;
602 sscanf(ident
, "%lx", &integer
);
604 ctx
-> token
= INTEGER
;
608 sscanf(ident
, "%lf", &real
);
612 sscanf(ident
, "%lx", &integer
);
617 oberon_error(ctx
, "oberon_read_number: wat");
621 ctx
-> string
= ident
;
622 ctx
-> integer
= integer
;
627 oberon_skip_space(oberon_context_t
* ctx
)
629 while(isspace(ctx
-> c
))
631 oberon_get_char(ctx
);
636 oberon_read_comment(oberon_context_t
* ctx
)
643 oberon_get_char(ctx
);
646 oberon_get_char(ctx
);
650 else if(ctx
-> c
== '*')
652 oberon_get_char(ctx
);
655 oberon_get_char(ctx
);
659 else if(ctx
-> c
== 0)
661 oberon_error(ctx
, "unterminated comment");
665 oberon_get_char(ctx
);
670 static void oberon_read_string(oberon_context_t
* ctx
)
673 oberon_get_char(ctx
);
675 int start
= ctx
-> code_index
;
677 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
679 oberon_get_char(ctx
);
684 oberon_error(ctx
, "unterminated string");
687 int end
= ctx
-> code_index
;
689 oberon_get_char(ctx
);
691 char * string
= calloc(1, end
- start
+ 1);
692 strncpy(string
, &ctx
-> code
[start
], end
- start
);
694 ctx
-> token
= STRING
;
695 ctx
-> string
= string
;
698 static void oberon_read_token(oberon_context_t
* ctx
);
701 oberon_read_symbol(oberon_context_t
* ctx
)
710 ctx
-> token
= SEMICOLON
;
711 oberon_get_char(ctx
);
714 ctx
-> token
= COLON
;
715 oberon_get_char(ctx
);
718 ctx
-> token
= ASSIGN
;
719 oberon_get_char(ctx
);
724 oberon_get_char(ctx
);
727 ctx
-> token
= DOTDOT
;
728 oberon_get_char(ctx
);
732 ctx
-> token
= LPAREN
;
733 oberon_get_char(ctx
);
736 oberon_get_char(ctx
);
737 oberon_read_comment(ctx
);
738 oberon_read_token(ctx
);
742 ctx
-> token
= RPAREN
;
743 oberon_get_char(ctx
);
746 ctx
-> token
= EQUAL
;
747 oberon_get_char(ctx
);
751 oberon_get_char(ctx
);
755 oberon_get_char(ctx
);
759 oberon_get_char(ctx
);
763 ctx
-> token
= GREAT
;
764 oberon_get_char(ctx
);
768 oberon_get_char(ctx
);
773 oberon_get_char(ctx
);
776 ctx
-> token
= MINUS
;
777 oberon_get_char(ctx
);
781 oberon_get_char(ctx
);
784 oberon_get_char(ctx
);
785 oberon_error(ctx
, "unstarted comment");
789 ctx
-> token
= SLASH
;
790 oberon_get_char(ctx
);
794 oberon_get_char(ctx
);
798 oberon_get_char(ctx
);
801 ctx
-> token
= COMMA
;
802 oberon_get_char(ctx
);
805 ctx
-> token
= LBRACK
;
806 oberon_get_char(ctx
);
809 ctx
-> token
= RBRACK
;
810 oberon_get_char(ctx
);
813 ctx
-> token
= UPARROW
;
814 oberon_get_char(ctx
);
817 oberon_read_string(ctx
);
820 oberon_read_string(ctx
);
823 ctx
-> token
= LBRACE
;
824 oberon_get_char(ctx
);
827 ctx
-> token
= RBRACE
;
828 oberon_get_char(ctx
);
832 oberon_get_char(ctx
);
835 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
841 oberon_read_token(oberon_context_t
* ctx
)
843 oberon_skip_space(ctx
);
848 oberon_read_ident(ctx
);
852 oberon_read_number(ctx
);
856 oberon_read_symbol(ctx
);
860 // =======================================================================
862 // =======================================================================
864 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
865 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
866 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
867 static char * oberon_assert_ident(oberon_context_t
* ctx
);
868 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
869 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
870 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
872 static oberon_expr_t
*
873 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
875 oberon_oper_t
* operator;
876 operator = malloc(sizeof *operator);
877 memset(operator, 0, sizeof *operator);
879 operator -> is_item
= 0;
880 operator -> result
= result
;
881 operator -> read_only
= 1;
883 operator -> left
= left
;
884 operator -> right
= right
;
886 return (oberon_expr_t
*) operator;
889 static oberon_expr_t
*
890 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
892 oberon_item_t
* item
;
893 item
= malloc(sizeof *item
);
894 memset(item
, 0, sizeof *item
);
897 item
-> result
= result
;
898 item
-> read_only
= read_only
;
901 return (oberon_expr_t
*)item
;
904 static oberon_expr_t
*
905 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
907 oberon_expr_t
* expr
;
908 oberon_type_t
* result
;
910 result
= a
-> result
;
914 if(result
-> class == OBERON_TYPE_SET
)
916 expr
= oberon_new_operator(OP_COMPLEMENTATION
, result
, a
, NULL
);
918 else if(result
-> class == OBERON_TYPE_INTEGER
)
920 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
924 oberon_error(ctx
, "incompatible operator type");
927 else if(token
== NOT
)
929 if(result
-> class != OBERON_TYPE_BOOLEAN
)
931 oberon_error(ctx
, "incompatible operator type");
934 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
938 oberon_error(ctx
, "oberon_make_unary_op: wat");
945 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
947 oberon_expr_t
* last
;
952 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
956 *first
= last
= oberon_expr(ctx
);
958 while(ctx
-> token
== COMMA
)
960 oberon_assert_token(ctx
, COMMA
);
961 oberon_expr_t
* current
;
965 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
969 current
= oberon_expr(ctx
);
972 last
-> next
= current
;
978 static oberon_expr_t
*
979 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
981 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
984 static oberon_expr_t
*
985 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
987 oberon_type_t
* from
= expr
-> result
;
988 oberon_type_t
* to
= rec
;
990 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
996 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
998 oberon_error(ctx
, "must be record type");
1001 return oberon_cast_expr(ctx
, expr
, rec
);
1004 static oberon_type_t
*
1005 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1007 oberon_type_t
* result
;
1008 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
1012 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
1016 else if(a
-> class != b
-> class)
1018 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
1020 else if(a
-> size
> b
-> size
)
1033 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
1035 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1037 from
= from
-> base
;
1041 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1043 oberon_error(ctx
, "not a record");
1046 oberon_type_t
* t
= from
;
1047 while(t
!= NULL
&& t
!= to
)
1054 oberon_error(ctx
, "incompatible record types");
1059 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1061 if(dst
-> read_only
)
1063 oberon_error(ctx
, "read-only destination");
1066 if(dst
-> is_item
== false)
1068 oberon_error(ctx
, "not variable");
1071 switch(dst
-> item
.mode
)
1082 oberon_error(ctx
, "not variable");
1088 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1092 if(src
-> item
.mode
== MODE_TYPE
)
1094 oberon_error(ctx
, "not variable");
1099 static oberon_expr_t
*
1100 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1103 // Если классы типов равны
1104 // Если INTEGER переводится в REAL
1105 // Если STRING переводится в CHAR
1106 // Если STRING переводится в ARRAY OF CHAR
1107 // Если NIL переводится в POINTER
1108 // Если NIL переводится в PROCEDURE
1110 oberon_check_src(ctx
, expr
);
1113 if(pref
-> class != expr
-> result
-> class)
1115 if(expr
-> result
-> class == OBERON_TYPE_NIL
)
1117 if(pref
-> class != OBERON_TYPE_POINTER
1118 && pref
-> class != OBERON_TYPE_PROCEDURE
)
1123 else if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1125 if(pref
-> class == OBERON_TYPE_CHAR
)
1127 if(expr
-> is_item
&& expr
-> item
.mode
== MODE_STRING
)
1129 if(strlen(expr
-> item
.string
) != 1)
1139 else if(pref
-> class == OBERON_TYPE_ARRAY
)
1141 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1151 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1153 if(pref
-> class != OBERON_TYPE_REAL
)
1166 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1169 if(pref
-> class == OBERON_TYPE_CHAR
)
1171 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1173 int c
= expr
-> item
.string
[0];
1174 expr
= oberon_new_item(MODE_CHAR
, ctx
-> char_type
, true);
1175 expr
-> item
.integer
= c
;
1178 else if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1180 if(expr
-> result
-> size
> pref
-> size
)
1182 oberon_error(ctx
, "incompatible size");
1186 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1189 else if(pref
-> class == OBERON_TYPE_RECORD
)
1191 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1192 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1194 else if(pref
-> class == OBERON_TYPE_POINTER
)
1196 assert(pref
-> base
);
1197 if(expr
-> result
-> class == OBERON_TYPE_NIL
)
1201 else if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1203 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1204 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1206 else if(expr
-> result
-> base
!= pref
-> base
)
1208 oberon_error(ctx
, "incompatible pointer types");
1216 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1218 oberon_type_t
* a
= (*ea
) -> result
;
1219 oberon_type_t
* b
= (*eb
) -> result
;
1220 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1221 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1222 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1226 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1228 if(desig
-> mode
!= MODE_CALL
)
1230 oberon_error(ctx
, "expected mode CALL");
1233 oberon_type_t
* fn
= desig
-> parent
-> result
;
1234 int num_args
= desig
-> num_args
;
1235 int num_decl
= fn
-> num_decl
;
1237 if(num_args
< num_decl
)
1239 oberon_error(ctx
, "too few arguments");
1241 else if(num_args
> num_decl
)
1243 oberon_error(ctx
, "too many arguments");
1246 /* Делаем проверку на запись и делаем автокаст */
1247 oberon_expr_t
* casted
[num_args
];
1248 oberon_expr_t
* arg
= desig
-> args
;
1249 oberon_object_t
* param
= fn
-> decl
;
1250 for(int i
= 0; i
< num_args
; i
++)
1252 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1254 if(arg
-> result
!= param
-> type
)
1256 oberon_error(ctx
, "incompatible type");
1258 if(arg
-> read_only
)
1260 oberon_error(ctx
, "assign to read-only var");
1266 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1270 param
= param
-> next
;
1273 /* Создаём новый список выражений */
1277 for(int i
= 0; i
< num_args
- 1; i
++)
1279 casted
[i
] -> next
= casted
[i
+ 1];
1281 desig
-> args
= arg
;
1285 static oberon_expr_t
*
1286 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1288 oberon_type_t
* signature
= item
-> result
;
1289 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1291 oberon_error(ctx
, "not a procedure");
1294 oberon_expr_t
* call
;
1296 if(signature
-> sysproc
)
1298 if(signature
-> genfunc
== NULL
)
1300 oberon_error(ctx
, "not a function-procedure");
1303 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1307 if(signature
-> base
-> class == OBERON_TYPE_NOTYPE
)
1309 oberon_error(ctx
, "attempt to call procedure in expression");
1312 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1313 call
-> item
.parent
= item
;
1314 call
-> item
.num_args
= num_args
;
1315 call
-> item
.args
= list_args
;
1316 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1323 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1325 oberon_type_t
* signature
= item
-> result
;
1326 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1328 oberon_error(ctx
, "not a procedure");
1331 oberon_expr_t
* call
;
1333 if(signature
-> sysproc
)
1335 if(signature
-> genproc
== NULL
)
1337 oberon_error(ctx
, "not a procedure");
1340 signature
-> genproc(ctx
, num_args
, list_args
);
1344 if(signature
-> base
-> class != OBERON_TYPE_NOTYPE
)
1346 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1349 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1350 call
-> item
.parent
= item
;
1351 call
-> item
.num_args
= num_args
;
1352 call
-> item
.args
= list_args
;
1353 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1354 oberon_generate_call_proc(ctx
, call
);
1362 || ((x) == INTEGER) \
1365 || ((x) == STRING) \
1367 || ((x) == LPAREN) \
1370 static oberon_expr_t
*
1371 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1373 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1375 oberon_error(ctx
, "not a pointer");
1378 assert(expr
-> is_item
);
1380 oberon_expr_t
* selector
;
1381 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1382 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1387 static oberon_expr_t
*
1388 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1390 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1392 desig
= oberno_make_dereferencing(ctx
, desig
);
1395 assert(desig
-> is_item
);
1397 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1399 oberon_error(ctx
, "not array");
1402 oberon_type_t
* base
;
1403 base
= desig
-> result
-> base
;
1405 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1407 oberon_error(ctx
, "index must be integer");
1410 // Статическая проверка границ массива
1411 if(desig
-> result
-> size
!= 0)
1413 if(index
-> is_item
)
1415 if(index
-> item
.mode
== MODE_INTEGER
)
1417 int arr_size
= desig
-> result
-> size
;
1418 int index_int
= index
-> item
.integer
;
1419 if(index_int
< 0 || index_int
> arr_size
- 1)
1421 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1427 oberon_expr_t
* selector
;
1428 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1429 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1430 selector
-> item
.num_args
= 1;
1431 selector
-> item
.args
= index
;
1436 static oberon_expr_t
*
1437 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1439 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1441 expr
= oberno_make_dereferencing(ctx
, expr
);
1444 assert(expr
-> is_item
);
1446 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1448 oberon_error(ctx
, "not record");
1451 oberon_type_t
* rec
= expr
-> result
;
1453 oberon_object_t
* field
;
1454 field
= oberon_find_object(rec
-> scope
, name
, true);
1456 if(field
-> export
== 0)
1458 if(field
-> module
!= ctx
-> mod
)
1460 oberon_error(ctx
, "field not exported");
1464 int read_only
= expr
-> read_only
;
1465 if(field
-> read_only
)
1467 if(field
-> module
!= ctx
-> mod
)
1473 oberon_expr_t
* selector
;
1474 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1475 selector
-> item
.var
= field
;
1476 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1481 #define ISSELECTOR(x) \
1484 || ((x) == UPARROW) \
1487 static oberon_object_t
*
1488 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1491 oberon_object_t
* x
;
1493 name
= oberon_assert_ident(ctx
);
1494 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1498 if(x
-> class == OBERON_CLASS_MODULE
)
1500 oberon_assert_token(ctx
, DOT
);
1501 name
= oberon_assert_ident(ctx
);
1502 /* Наличие объектов в левых модулях всегда проверяется */
1503 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1505 if(x
-> export
== 0)
1507 oberon_error(ctx
, "not exported");
1520 static oberon_expr_t
*
1521 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1524 oberon_object_t
* x
;
1525 oberon_expr_t
* expr
;
1527 x
= oberon_find_object(ctx
-> decl
, name
, true);
1530 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1535 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1536 expr
-> item
.var
= x
;
1540 static oberon_expr_t
*
1541 oberon_qualident_expr(oberon_context_t
* ctx
)
1543 oberon_object_t
* var
;
1544 oberon_expr_t
* expr
;
1546 var
= oberon_qualident(ctx
, NULL
, 1);
1549 if(var
-> read_only
)
1551 if(var
-> module
!= ctx
-> mod
)
1557 switch(var
-> class)
1559 case OBERON_CLASS_CONST
:
1561 expr
= (oberon_expr_t
*) var
-> value
;
1563 case OBERON_CLASS_TYPE
:
1564 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1566 case OBERON_CLASS_VAR
:
1567 case OBERON_CLASS_VAR_PARAM
:
1568 case OBERON_CLASS_PARAM
:
1569 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1571 case OBERON_CLASS_PROC
:
1572 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1575 oberon_error(ctx
, "invalid designator");
1579 expr
-> item
.var
= var
;
1585 oberon_check_type_guard(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* type
)
1587 /* Охрана типа применима, если */
1588 /* 1. v - параметр-переменная типа запись, или v - указатель, и если */
1589 /* 2. T - расширение статического типа v */
1592 && expr
-> item
.mode
== MODE_VAR
1593 && expr
-> item
.var
-> class == OBERON_CLASS_VAR_PARAM
)
1597 else if(expr
-> result
-> class == OBERON_TYPE_POINTER
1598 || expr
-> result
-> class == OBERON_TYPE_RECORD
)
1604 oberon_error(ctx
, "guard type used only with var-param or pointers");
1607 oberon_check_record_compatibility(ctx
, type
, expr
-> result
);
1610 static oberon_expr_t
*
1611 oberon_make_type_guard(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_object_t
* objtype
)
1613 oberon_type_t
* type
;
1615 if(objtype
-> class != OBERON_CLASS_TYPE
)
1617 oberon_error(ctx
, "must be type");
1619 type
= objtype
-> type
;
1621 oberon_check_type_guard(ctx
, expr
, type
);
1622 return oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1625 static oberon_expr_t
*
1626 oberon_designator(oberon_context_t
* ctx
)
1629 oberon_expr_t
* expr
;
1630 oberon_object_t
* objtype
;
1632 expr
= oberon_qualident_expr(ctx
);
1634 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1636 switch(ctx
-> token
)
1639 oberon_assert_token(ctx
, DOT
);
1640 name
= oberon_assert_ident(ctx
);
1641 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1644 oberon_assert_token(ctx
, LBRACK
);
1645 int num_indexes
= 0;
1646 oberon_expr_t
* indexes
= NULL
;
1647 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1648 oberon_assert_token(ctx
, RBRACK
);
1650 for(int i
= 0; i
< num_indexes
; i
++)
1652 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1653 indexes
= indexes
-> next
;
1657 oberon_assert_token(ctx
, UPARROW
);
1658 expr
= oberno_make_dereferencing(ctx
, expr
);
1661 oberon_assert_token(ctx
, LPAREN
);
1662 objtype
= oberon_qualident(ctx
, NULL
, true);
1663 oberon_assert_token(ctx
, RPAREN
);
1664 expr
= oberon_make_type_guard(ctx
, expr
, objtype
);
1667 oberon_error(ctx
, "oberon_designator: wat");
1675 static oberon_expr_t
*
1676 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1678 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1679 if(ctx
-> token
== LPAREN
)
1681 oberon_assert_token(ctx
, LPAREN
);
1684 oberon_expr_t
* arguments
= NULL
;
1686 if(ISEXPR(ctx
-> token
))
1688 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1691 assert(expr
-> is_item
== 1);
1692 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1694 oberon_assert_token(ctx
, RPAREN
);
1701 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1703 assert(expr
-> is_item
);
1706 oberon_expr_t
* arguments
= NULL
;
1708 if(ctx
-> token
== LPAREN
)
1710 oberon_assert_token(ctx
, LPAREN
);
1712 if(ISEXPR(ctx
-> token
))
1714 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1717 oberon_assert_token(ctx
, RPAREN
);
1720 /* Вызов происходит даже без скобок */
1721 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1724 static oberon_type_t
*
1725 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1727 if(i
>= -128 && i
<= 127)
1729 return ctx
-> byte_type
;
1731 else if(i
>= -32768 && i
<= 32767)
1733 return ctx
-> shortint_type
;
1735 else if(i
>= -2147483648 && i
<= 2147483647)
1737 return ctx
-> int_type
;
1741 return ctx
-> longint_type
;
1745 static oberon_expr_t
*
1746 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1748 oberon_expr_t
* expr
;
1749 oberon_type_t
* result
;
1750 result
= oberon_get_type_of_int_value(ctx
, i
);
1751 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1752 expr
-> item
.integer
= i
;
1756 static oberon_expr_t
*
1757 oberon_element(oberon_context_t
* ctx
)
1762 e1
= oberon_expr(ctx
);
1763 if(e1
-> result
-> class != OBERON_TYPE_INTEGER
)
1765 oberon_error(ctx
, "expected integer");
1769 if(ctx
-> token
== DOTDOT
)
1771 oberon_assert_token(ctx
, DOTDOT
);
1772 e2
= oberon_expr(ctx
);
1773 if(e2
-> result
-> class != OBERON_TYPE_INTEGER
)
1775 oberon_error(ctx
, "expected integer");
1779 oberon_expr_t
* set
;
1780 set
= oberon_new_operator(OP_RANGE
, ctx
-> set_type
, e1
, e2
);
1784 static oberon_expr_t
*
1785 oberon_set(oberon_context_t
* ctx
)
1787 oberon_expr_t
* set
;
1788 oberon_expr_t
* elements
;
1789 set
= oberon_new_item(MODE_SET
, ctx
-> set_type
, true);
1790 set
-> item
.integer
= 0;
1792 oberon_assert_token(ctx
, LBRACE
);
1793 if(ISEXPR(ctx
-> token
))
1795 elements
= oberon_element(ctx
);
1796 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1797 while(ctx
-> token
== COMMA
)
1799 oberon_assert_token(ctx
, COMMA
);
1800 elements
= oberon_element(ctx
);
1801 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1804 oberon_assert_token(ctx
, RBRACE
);
1809 static oberon_expr_t
*
1810 oberon_make_boolean(oberon_context_t
* ctx
, bool cond
)
1812 oberon_expr_t
* expr
;
1813 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1814 expr
-> item
.integer
= cond
;
1818 static oberon_expr_t
*
1819 oberon_factor(oberon_context_t
* ctx
)
1821 oberon_expr_t
* expr
;
1822 oberon_type_t
* result
;
1824 switch(ctx
-> token
)
1827 expr
= oberon_designator(ctx
);
1828 expr
= oberon_opt_func_parens(ctx
, expr
);
1831 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1832 oberon_assert_token(ctx
, INTEGER
);
1835 result
= ctx
-> char_type
;
1836 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1837 expr
-> item
.integer
= ctx
-> integer
;
1838 oberon_assert_token(ctx
, CHAR
);
1841 result
= ctx
-> string_type
;
1842 expr
= oberon_new_item(MODE_STRING
, result
, true);
1843 expr
-> item
.string
= ctx
-> string
;
1844 oberon_assert_token(ctx
, STRING
);
1847 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1848 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1849 expr
-> item
.real
= ctx
-> real
;
1850 oberon_assert_token(ctx
, REAL
);
1853 expr
= oberon_set(ctx
);
1856 oberon_assert_token(ctx
, LPAREN
);
1857 expr
= oberon_expr(ctx
);
1858 oberon_assert_token(ctx
, RPAREN
);
1861 oberon_assert_token(ctx
, NOT
);
1862 expr
= oberon_factor(ctx
);
1863 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1866 oberon_assert_token(ctx
, NIL
);
1867 expr
= oberon_new_item(MODE_NIL
, ctx
-> nil_type
, true);
1870 oberon_error(ctx
, "invalid expression");
1877 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1879 oberon_expr_t
* expr
= *e
;
1880 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1882 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1884 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1888 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1891 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1893 oberon_error(ctx
, "required numeric type");
1898 oberon_is_numeric_type(oberon_type_t
* t
)
1900 return (t
-> class == OBERON_TYPE_INTEGER
) || (t
-> class == OBERON_TYPE_REAL
);
1904 oberon_is_char_type(oberon_type_t
* t
)
1906 return (t
-> class == OBERON_TYPE_CHAR
);
1910 oberon_is_string_type(oberon_type_t
* t
)
1912 return (t
-> class == OBERON_TYPE_STRING
)
1913 || (t
-> class == OBERON_TYPE_ARRAY
&& t
-> base
-> class == OBERON_TYPE_CHAR
);
1917 oberon_is_boolean_type(oberon_type_t
* t
)
1919 return (t
-> class == OBERON_TYPE_BOOLEAN
);
1923 oberon_is_set_type(oberon_type_t
* t
)
1925 return (t
-> class == OBERON_TYPE_SET
);
1929 oberon_is_pointer_type(oberon_type_t
* t
)
1931 return (t
-> class == OBERON_TYPE_POINTER
) || (t
-> class == OBERON_TYPE_NIL
);
1935 oberon_is_procedure_type(oberon_type_t
* t
)
1937 return (t
-> class == OBERON_TYPE_POINTER
) || (t
-> class == OBERON_TYPE_NIL
);
1940 static oberon_expr_t
*
1941 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1943 oberon_expr_t
* expr
;
1944 oberon_type_t
* result
;
1949 if(a
-> result
-> class != OBERON_TYPE_INTEGER
)
1951 oberon_error(ctx
, "must be integer");
1954 if(b
-> result
-> class != OBERON_TYPE_SET
)
1956 oberon_error(ctx
, "must be set");
1959 result
= ctx
-> bool_type
;
1960 expr
= oberon_new_operator(OP_IN
, result
, a
, b
);
1962 else if(token
== IS
)
1964 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1966 oberon_error(ctx
, "requires type");
1969 result
= ctx
-> bool_type
;
1970 oberon_check_type_guard(ctx
, a
, b
-> result
);
1971 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1973 else if((token
>= EQUAL
&& token
<= GEQ
) || token
== OR
|| token
== AND
)
1975 if(token
>= LESS
&& token
<= GEQ
)
1977 if(oberon_is_numeric_type(a
-> result
) && oberon_is_numeric_type(b
-> result
))
1981 else if(oberon_is_char_type(a
-> result
) && oberon_is_char_type(b
-> result
))
1985 else if(oberon_is_string_type(a
-> result
) && oberon_is_string_type(b
-> result
))
1991 oberon_error(ctx
, "invalid comparation");
1994 else if(token
== EQUAL
|| token
== NEQ
)
1996 if(oberon_is_numeric_type(a
-> result
) && oberon_is_numeric_type(b
-> result
))
2000 else if(oberon_is_char_type(a
-> result
) && oberon_is_char_type(b
-> result
))
2004 else if(oberon_is_string_type(a
-> result
) && oberon_is_string_type(b
-> result
))
2008 else if(oberon_is_boolean_type(a
-> result
) && oberon_is_boolean_type(b
-> result
))
2012 else if(oberon_is_set_type(a
-> result
) && oberon_is_set_type(b
-> result
))
2016 else if(oberon_is_pointer_type(a
-> result
) && oberon_is_pointer_type(b
-> result
))
2020 else if(oberon_is_procedure_type(a
-> result
) && oberon_is_procedure_type(b
-> result
))
2026 oberon_error(ctx
, "invalid comparation");
2029 else if(token
== AND
|| token
== OR
)
2031 if(!oberon_is_boolean_type(a
-> result
) || !oberon_is_boolean_type(b
-> result
))
2033 oberon_error(ctx
, "invalid comparation");
2038 oberon_error(ctx
, "wat");
2041 oberon_autocast_binary_op(ctx
, &a
, &b
);
2042 result
= ctx
-> bool_type
;
2046 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
2048 else if(token
== NEQ
)
2050 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
2052 else if(token
== LESS
)
2054 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
2056 else if(token
== LEQ
)
2058 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
2060 else if(token
== GREAT
)
2062 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
2064 else if(token
== GEQ
)
2066 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
2068 else if(token
== OR
)
2070 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
2072 else if(token
== AND
)
2074 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
2078 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
2081 else if(token
== SLASH
)
2083 if(a
-> result
-> class == OBERON_TYPE_SET
2084 || b
-> result
-> class == OBERON_TYPE_SET
)
2086 oberon_autocast_binary_op(ctx
, &a
, &b
);
2087 result
= a
-> result
;
2088 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
2092 oberon_autocast_to_real(ctx
, &a
);
2093 oberon_autocast_to_real(ctx
, &b
);
2094 oberon_autocast_binary_op(ctx
, &a
, &b
);
2095 result
= a
-> result
;
2096 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
2099 else if(token
== DIV
)
2101 if(a
-> result
-> class != OBERON_TYPE_INTEGER
2102 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
2104 oberon_error(ctx
, "operator DIV requires integer type");
2107 oberon_autocast_binary_op(ctx
, &a
, &b
);
2108 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
2112 oberon_autocast_binary_op(ctx
, &a
, &b
);
2113 result
= a
-> result
;
2114 if(result
-> class == OBERON_TYPE_SET
)
2119 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
2122 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
2125 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
2132 else if(result
-> class == OBERON_TYPE_INTEGER
2133 || result
-> class == OBERON_TYPE_REAL
)
2138 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
2141 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
2144 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
2147 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2162 oberon_error(ctx
, "invalid operation");
2168 #define ISMULOP(x) \
2169 ((x) >= STAR && (x) <= AND)
2171 static oberon_expr_t
*
2172 oberon_term_expr(oberon_context_t
* ctx
)
2174 oberon_expr_t
* expr
;
2176 expr
= oberon_factor(ctx
);
2177 while(ISMULOP(ctx
-> token
))
2179 int token
= ctx
-> token
;
2180 oberon_read_token(ctx
);
2182 oberon_expr_t
* inter
= oberon_factor(ctx
);
2183 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2189 #define ISADDOP(x) \
2190 ((x) >= PLUS && (x) <= OR)
2192 static oberon_expr_t
*
2193 oberon_simple_expr(oberon_context_t
* ctx
)
2195 oberon_expr_t
* expr
;
2198 if(ctx
-> token
== PLUS
)
2201 oberon_assert_token(ctx
, PLUS
);
2203 else if(ctx
-> token
== MINUS
)
2206 oberon_assert_token(ctx
, MINUS
);
2209 expr
= oberon_term_expr(ctx
);
2213 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2216 while(ISADDOP(ctx
-> token
))
2218 int token
= ctx
-> token
;
2219 oberon_read_token(ctx
);
2221 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2222 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2228 #define ISRELATION(x) \
2229 ((x) >= EQUAL && (x) <= IS)
2231 static oberon_expr_t
*
2232 oberon_expr(oberon_context_t
* ctx
)
2234 oberon_expr_t
* expr
;
2236 expr
= oberon_simple_expr(ctx
);
2237 while(ISRELATION(ctx
-> token
))
2239 int token
= ctx
-> token
;
2240 oberon_read_token(ctx
);
2242 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2243 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2250 oberon_check_const(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2252 if(expr
-> is_item
== 0)
2254 oberon_error(ctx
, "const expression are required");
2257 switch(expr
-> item
.mode
)
2269 oberon_error(ctx
, "const expression are required");
2274 static oberon_item_t
*
2275 oberon_const_expr(oberon_context_t
* ctx
)
2277 oberon_expr_t
* expr
;
2278 expr
= oberon_expr(ctx
);
2279 oberon_check_const(ctx
, expr
);
2280 return (oberon_item_t
*) expr
;
2283 // =======================================================================
2285 // =======================================================================
2287 static void oberon_decl_seq(oberon_context_t
* ctx
);
2288 static void oberon_statement_seq(oberon_context_t
* ctx
);
2289 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2292 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2294 if(ctx
-> token
!= token
)
2296 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2301 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2303 oberon_expect_token(ctx
, token
);
2304 oberon_read_token(ctx
);
2308 oberon_assert_ident(oberon_context_t
* ctx
)
2310 oberon_expect_token(ctx
, IDENT
);
2311 char * ident
= ctx
-> string
;
2312 oberon_read_token(ctx
);
2317 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2319 switch(ctx
-> token
)
2322 oberon_assert_token(ctx
, STAR
);
2327 oberon_assert_token(ctx
, MINUS
);
2338 static oberon_object_t
*
2339 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2344 oberon_object_t
* x
;
2346 name
= oberon_assert_ident(ctx
);
2347 oberon_def(ctx
, &export
, &read_only
);
2349 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2354 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2357 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2358 while(ctx
-> token
== COMMA
)
2360 oberon_assert_token(ctx
, COMMA
);
2361 oberon_ident_def(ctx
, class, check_upscope
);
2367 oberon_var_decl(oberon_context_t
* ctx
)
2370 oberon_object_t
* list
;
2371 oberon_type_t
* type
;
2372 type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2374 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2375 oberon_assert_token(ctx
, COLON
);
2376 oberon_type(ctx
, &type
);
2378 oberon_object_t
* var
= list
;
2379 for(int i
= 0; i
< num
; i
++)
2386 static oberon_object_t
*
2387 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2389 int class = OBERON_CLASS_PARAM
;
2390 if(ctx
-> token
== VAR
)
2392 oberon_read_token(ctx
);
2393 class = OBERON_CLASS_VAR_PARAM
;
2397 oberon_object_t
* list
;
2398 oberon_ident_list(ctx
, class, false, &num
, &list
);
2400 oberon_assert_token(ctx
, COLON
);
2402 oberon_type_t
* type
;
2403 type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2404 oberon_type(ctx
, &type
);
2406 oberon_object_t
* param
= list
;
2407 for(int i
= 0; i
< num
; i
++)
2409 param
-> type
= type
;
2410 param
= param
-> next
;
2417 #define ISFPSECTION \
2418 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2421 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2423 oberon_assert_token(ctx
, LPAREN
);
2427 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2428 while(ctx
-> token
== SEMICOLON
)
2430 oberon_assert_token(ctx
, SEMICOLON
);
2431 oberon_fp_section(ctx
, &signature
-> num_decl
);
2435 oberon_assert_token(ctx
, RPAREN
);
2437 if(ctx
-> token
== COLON
)
2439 oberon_assert_token(ctx
, COLON
);
2441 oberon_object_t
* typeobj
;
2442 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2443 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2445 oberon_error(ctx
, "function result is not type");
2447 signature
-> base
= typeobj
-> type
;
2452 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2454 oberon_type_t
* signature
;
2456 signature
-> class = OBERON_TYPE_PROCEDURE
;
2457 signature
-> num_decl
= 0;
2458 signature
-> base
= ctx
-> notype_type
;
2459 signature
-> decl
= NULL
;
2461 if(ctx
-> token
== LPAREN
)
2463 oberon_formal_pars(ctx
, signature
);
2468 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2470 if(a
-> num_decl
!= b
-> num_decl
)
2472 oberon_error(ctx
, "number parameters not matched");
2475 int num_param
= a
-> num_decl
;
2476 oberon_object_t
* param_a
= a
-> decl
;
2477 oberon_object_t
* param_b
= b
-> decl
;
2478 for(int i
= 0; i
< num_param
; i
++)
2480 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2482 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2485 if(param_a
-> type
!= param_b
-> type
)
2487 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2490 param_a
= param_a
-> next
;
2491 param_b
= param_b
-> next
;
2496 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2498 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2499 oberon_type_t
* result_type
= proc
-> type
-> base
;
2501 if(result_type
-> class == OBERON_TYPE_NOTYPE
)
2505 oberon_error(ctx
, "procedure has no result type");
2512 oberon_error(ctx
, "procedure requires expression on result");
2515 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2518 proc
-> has_return
= 1;
2520 oberon_generate_return(ctx
, expr
);
2524 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2526 oberon_assert_token(ctx
, SEMICOLON
);
2528 ctx
-> decl
= proc
-> scope
;
2530 oberon_decl_seq(ctx
);
2532 oberon_generate_begin_proc(ctx
, proc
);
2534 if(ctx
-> token
== BEGIN
)
2536 oberon_assert_token(ctx
, BEGIN
);
2537 oberon_statement_seq(ctx
);
2540 oberon_assert_token(ctx
, END
);
2541 char * name
= oberon_assert_ident(ctx
);
2542 if(strcmp(name
, proc
-> name
) != 0)
2544 oberon_error(ctx
, "procedure name not matched");
2547 if(proc
-> type
-> base
-> class == OBERON_TYPE_NOTYPE
2548 && proc
-> has_return
== 0)
2550 oberon_make_return(ctx
, NULL
);
2553 if(proc
-> has_return
== 0)
2555 oberon_error(ctx
, "procedure requires return");
2558 oberon_generate_end_proc(ctx
);
2559 oberon_close_scope(ctx
-> decl
);
2563 oberon_proc_decl(oberon_context_t
* ctx
)
2565 oberon_assert_token(ctx
, PROCEDURE
);
2568 if(ctx
-> token
== UPARROW
)
2570 oberon_assert_token(ctx
, UPARROW
);
2577 name
= oberon_assert_ident(ctx
);
2578 oberon_def(ctx
, &export
, &read_only
);
2580 oberon_scope_t
* proc_scope
;
2581 proc_scope
= oberon_open_scope(ctx
);
2582 ctx
-> decl
-> local
= 1;
2584 oberon_type_t
* signature
;
2585 signature
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2586 oberon_opt_formal_pars(ctx
, &signature
);
2588 //oberon_initialize_decl(ctx);
2589 oberon_generator_init_type(ctx
, signature
);
2590 oberon_close_scope(ctx
-> decl
);
2592 oberon_object_t
* proc
;
2593 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2596 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2597 proc
-> type
= signature
;
2598 proc
-> scope
= proc_scope
;
2599 oberon_generator_init_proc(ctx
, proc
);
2603 if(proc
-> class != OBERON_CLASS_PROC
)
2605 oberon_error(ctx
, "mult definition");
2612 oberon_error(ctx
, "mult procedure definition");
2616 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2618 oberon_error(ctx
, "export type not matched");
2621 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2624 proc_scope
-> parent
= proc
;
2625 oberon_object_t
* param
= proc_scope
-> list
-> next
;
2628 param
-> parent
= proc
;
2629 param
= param
-> next
;
2635 oberon_proc_decl_body(ctx
, proc
);
2640 oberon_const_decl(oberon_context_t
* ctx
)
2642 oberon_item_t
* value
;
2643 oberon_object_t
* constant
;
2645 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2646 oberon_assert_token(ctx
, EQUAL
);
2647 value
= oberon_const_expr(ctx
);
2648 constant
-> value
= value
;
2652 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2654 if(size
-> is_item
== 0)
2656 oberon_error(ctx
, "requires constant");
2659 if(size
-> item
.mode
!= MODE_INTEGER
)
2661 oberon_error(ctx
, "requires integer constant");
2664 oberon_type_t
* arr
;
2666 arr
-> class = OBERON_TYPE_ARRAY
;
2667 arr
-> size
= size
-> item
.integer
;
2672 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2675 oberon_object_t
* to
;
2677 to
= oberon_qualident(ctx
, &name
, 0);
2679 //name = oberon_assert_ident(ctx);
2680 //to = oberon_find_object(ctx -> decl, name, 0);
2684 if(to
-> class != OBERON_CLASS_TYPE
)
2686 oberon_error(ctx
, "not a type");
2691 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2692 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2698 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2701 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2705 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2713 oberon_type_t
* dim
;
2714 dim
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2716 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2718 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2722 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2724 type
-> class = OBERON_TYPE_ARRAY
;
2726 type
-> base
= base
;
2730 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2732 if(ctx
-> token
== IDENT
)
2735 oberon_object_t
* list
;
2736 oberon_type_t
* type
;
2737 type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2739 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2740 oberon_assert_token(ctx
, COLON
);
2742 oberon_scope_t
* current
= ctx
-> decl
;
2743 ctx
-> decl
= modscope
;
2744 oberon_type(ctx
, &type
);
2745 ctx
-> decl
= current
;
2747 oberon_object_t
* field
= list
;
2748 for(int i
= 0; i
< num
; i
++)
2750 field
-> type
= type
;
2751 field
= field
-> next
;
2754 rec
-> num_decl
+= num
;
2759 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2761 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2762 oberon_scope_t
* oldscope
= ctx
-> decl
;
2763 ctx
-> decl
= modscope
;
2765 if(ctx
-> token
== LPAREN
)
2767 oberon_assert_token(ctx
, LPAREN
);
2769 oberon_object_t
* typeobj
;
2770 typeobj
= oberon_qualident(ctx
, NULL
, true);
2772 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2774 oberon_error(ctx
, "base must be type");
2777 oberon_type_t
* base
= typeobj
-> type
;
2778 if(base
-> class == OBERON_TYPE_POINTER
)
2780 base
= base
-> base
;
2783 if(base
-> class != OBERON_TYPE_RECORD
)
2785 oberon_error(ctx
, "base must be record type");
2789 ctx
-> decl
= base
-> scope
;
2791 oberon_assert_token(ctx
, RPAREN
);
2798 oberon_scope_t
* this_scope
;
2799 this_scope
= oberon_open_scope(ctx
);
2800 this_scope
-> local
= true;
2801 this_scope
-> parent
= NULL
;
2802 this_scope
-> parent_type
= rec
;
2804 oberon_field_list(ctx
, rec
, modscope
);
2805 while(ctx
-> token
== SEMICOLON
)
2807 oberon_assert_token(ctx
, SEMICOLON
);
2808 oberon_field_list(ctx
, rec
, modscope
);
2811 rec
-> scope
= this_scope
;
2812 rec
-> decl
= this_scope
-> list
-> next
;
2813 ctx
-> decl
= oldscope
;
2817 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2819 if(ctx
-> token
== IDENT
)
2821 oberon_qualident_type(ctx
, type
);
2823 else if(ctx
-> token
== ARRAY
)
2825 oberon_assert_token(ctx
, ARRAY
);
2828 oberon_expr_t
* sizes
;
2830 if(ISEXPR(ctx
-> token
))
2832 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2835 oberon_assert_token(ctx
, OF
);
2837 oberon_type_t
* base
;
2838 base
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2839 oberon_type(ctx
, &base
);
2843 oberon_make_open_array(ctx
, base
, *type
);
2847 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2850 else if(ctx
-> token
== RECORD
)
2852 oberon_type_t
* rec
;
2854 rec
-> class = OBERON_TYPE_RECORD
;
2855 rec
-> module
= ctx
-> mod
;
2857 oberon_assert_token(ctx
, RECORD
);
2858 oberon_type_record_body(ctx
, rec
);
2859 oberon_assert_token(ctx
, END
);
2863 else if(ctx
-> token
== POINTER
)
2865 oberon_assert_token(ctx
, POINTER
);
2866 oberon_assert_token(ctx
, TO
);
2868 oberon_type_t
* base
;
2869 base
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2870 oberon_type(ctx
, &base
);
2872 oberon_type_t
* ptr
;
2874 ptr
-> class = OBERON_TYPE_POINTER
;
2877 else if(ctx
-> token
== PROCEDURE
)
2879 oberon_open_scope(ctx
);
2880 oberon_assert_token(ctx
, PROCEDURE
);
2881 oberon_opt_formal_pars(ctx
, type
);
2882 oberon_close_scope(ctx
-> decl
);
2886 oberon_error(ctx
, "invalid type declaration");
2891 oberon_type_decl(oberon_context_t
* ctx
)
2894 oberon_object_t
* newtype
;
2895 oberon_type_t
* type
;
2899 name
= oberon_assert_ident(ctx
);
2900 oberon_def(ctx
, &export
, &read_only
);
2902 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2905 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2906 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2907 assert(newtype
-> type
);
2911 if(newtype
-> class != OBERON_CLASS_TYPE
)
2913 oberon_error(ctx
, "mult definition");
2916 if(newtype
-> linked
)
2918 oberon_error(ctx
, "mult definition - already linked");
2921 newtype
-> export
= export
;
2922 newtype
-> read_only
= read_only
;
2925 oberon_assert_token(ctx
, EQUAL
);
2927 type
= newtype
-> type
;
2928 oberon_type(ctx
, &type
);
2930 if(type
-> class == OBERON_TYPE_NOTYPE
)
2932 oberon_error(ctx
, "recursive alias declaration");
2935 newtype
-> type
= type
;
2936 newtype
-> linked
= 1;
2939 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2940 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2943 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2945 if(type
-> class != OBERON_TYPE_POINTER
2946 && type
-> class != OBERON_TYPE_ARRAY
)
2951 if(type
-> recursive
)
2953 oberon_error(ctx
, "recursive pointer declaration");
2956 if(type
-> class == OBERON_TYPE_POINTER
2957 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2959 oberon_error(ctx
, "attempt to make pointer to pointer");
2962 type
-> recursive
= 1;
2964 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2966 type
-> recursive
= 0;
2970 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2972 if(type
-> class != OBERON_TYPE_RECORD
)
2977 if(type
-> recursive
)
2979 oberon_error(ctx
, "recursive record declaration");
2982 type
-> recursive
= 1;
2986 oberon_prevent_recursive_record(ctx
, type
-> base
);
2989 int num_fields
= type
-> num_decl
;
2990 oberon_object_t
* field
= type
-> decl
;
2991 for(int i
= 0; i
< num_fields
; i
++)
2993 oberon_prevent_recursive_object(ctx
, field
);
2994 field
= field
-> next
;
2997 type
-> recursive
= 0;
3000 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
3002 if(type
-> class != OBERON_TYPE_PROCEDURE
)
3007 if(type
-> recursive
)
3009 oberon_error(ctx
, "recursive procedure declaration");
3012 type
-> recursive
= 1;
3014 int num_fields
= type
-> num_decl
;
3015 oberon_object_t
* field
= type
-> decl
;
3016 for(int i
= 0; i
< num_fields
; i
++)
3018 oberon_prevent_recursive_object(ctx
, field
);
3019 field
= field
-> next
;
3022 type
-> recursive
= 0;
3026 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
3028 if(type
-> class != OBERON_TYPE_ARRAY
)
3033 if(type
-> recursive
)
3035 oberon_error(ctx
, "recursive array declaration");
3038 type
-> recursive
= 1;
3040 oberon_prevent_recursive_type(ctx
, type
-> base
);
3042 type
-> recursive
= 0;
3046 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
3048 if(type
-> class == OBERON_TYPE_POINTER
)
3050 oberon_prevent_recursive_pointer(ctx
, type
);
3052 else if(type
-> class == OBERON_TYPE_RECORD
)
3054 oberon_prevent_recursive_record(ctx
, type
);
3056 else if(type
-> class == OBERON_TYPE_ARRAY
)
3058 oberon_prevent_recursive_array(ctx
, type
);
3060 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3062 oberon_prevent_recursive_procedure(ctx
, type
);
3067 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3071 case OBERON_CLASS_VAR
:
3072 case OBERON_CLASS_TYPE
:
3073 case OBERON_CLASS_PARAM
:
3074 case OBERON_CLASS_VAR_PARAM
:
3075 case OBERON_CLASS_FIELD
:
3076 oberon_prevent_recursive_type(ctx
, x
-> type
);
3078 case OBERON_CLASS_CONST
:
3079 case OBERON_CLASS_PROC
:
3080 case OBERON_CLASS_MODULE
:
3083 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
3089 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
3091 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
3095 oberon_prevent_recursive_object(ctx
, x
);
3100 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
3101 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
3104 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
3106 if(type
-> class != OBERON_TYPE_RECORD
)
3111 int num_fields
= type
-> num_decl
;
3112 oberon_object_t
* field
= type
-> decl
;
3113 for(int i
= 0; i
< num_fields
; i
++)
3115 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
3117 oberon_initialize_type(ctx
, field
-> type
);
3120 oberon_initialize_object(ctx
, field
);
3121 field
= field
-> next
;
3124 oberon_generator_init_record(ctx
, type
);
3128 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
3130 if(type
-> class == OBERON_TYPE_NOTYPE
)
3132 oberon_error(ctx
, "undeclarated type");
3135 if(type
-> initialized
)
3140 type
-> initialized
= 1;
3142 if(type
-> class == OBERON_TYPE_POINTER
)
3144 oberon_initialize_type(ctx
, type
-> base
);
3145 oberon_generator_init_type(ctx
, type
);
3147 else if(type
-> class == OBERON_TYPE_ARRAY
)
3149 if(type
-> size
!= 0)
3151 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
3153 if(type
-> base
-> size
== 0)
3155 oberon_error(ctx
, "open array not allowed as array element");
3160 oberon_initialize_type(ctx
, type
-> base
);
3161 oberon_generator_init_type(ctx
, type
);
3163 else if(type
-> class == OBERON_TYPE_RECORD
)
3165 oberon_generator_init_type(ctx
, type
);
3166 oberon_initialize_record_fields(ctx
, type
);
3168 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3170 int num_fields
= type
-> num_decl
;
3171 oberon_object_t
* field
= type
-> decl
;
3172 for(int i
= 0; i
< num_fields
; i
++)
3174 oberon_initialize_object(ctx
, field
);
3175 field
= field
-> next
;
3178 oberon_generator_init_type(ctx
, type
);
3182 oberon_generator_init_type(ctx
, type
);
3187 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3189 if(x
-> initialized
)
3194 x
-> initialized
= 1;
3198 case OBERON_CLASS_TYPE
:
3199 oberon_initialize_type(ctx
, x
-> type
);
3201 case OBERON_CLASS_VAR
:
3202 case OBERON_CLASS_FIELD
:
3203 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3205 if(x
-> type
-> size
== 0)
3207 oberon_error(ctx
, "open array not allowed as variable or field");
3210 oberon_initialize_type(ctx
, x
-> type
);
3211 oberon_generator_init_var(ctx
, x
);
3213 case OBERON_CLASS_PARAM
:
3214 case OBERON_CLASS_VAR_PARAM
:
3215 oberon_initialize_type(ctx
, x
-> type
);
3216 oberon_generator_init_var(ctx
, x
);
3218 case OBERON_CLASS_CONST
:
3219 case OBERON_CLASS_PROC
:
3220 case OBERON_CLASS_MODULE
:
3223 oberon_error(ctx
, "oberon_initialize_object: wat");
3229 oberon_initialize_decl(oberon_context_t
* ctx
)
3231 oberon_object_t
* x
= ctx
-> decl
-> list
;
3235 oberon_initialize_object(ctx
, x
-> next
);
3241 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3243 oberon_object_t
* x
= ctx
-> decl
-> list
;
3247 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3249 if(x
-> next
-> linked
== 0)
3251 oberon_error(ctx
, "unresolved forward declaration");
3259 oberon_decl_seq(oberon_context_t
* ctx
)
3261 if(ctx
-> token
== CONST
)
3263 oberon_assert_token(ctx
, CONST
);
3264 while(ctx
-> token
== IDENT
)
3266 oberon_const_decl(ctx
);
3267 oberon_assert_token(ctx
, SEMICOLON
);
3271 if(ctx
-> token
== TYPE
)
3273 oberon_assert_token(ctx
, TYPE
);
3274 while(ctx
-> token
== IDENT
)
3276 oberon_type_decl(ctx
);
3277 oberon_assert_token(ctx
, SEMICOLON
);
3281 if(ctx
-> token
== VAR
)
3283 oberon_assert_token(ctx
, VAR
);
3284 while(ctx
-> token
== IDENT
)
3286 oberon_var_decl(ctx
);
3287 oberon_assert_token(ctx
, SEMICOLON
);
3291 oberon_prevent_recursive_decl(ctx
);
3292 oberon_initialize_decl(ctx
);
3294 while(ctx
-> token
== PROCEDURE
)
3296 oberon_proc_decl(ctx
);
3297 oberon_assert_token(ctx
, SEMICOLON
);
3300 oberon_prevent_undeclarated_procedures(ctx
);
3303 static oberon_expr_t
*
3304 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3306 oberon_object_t
* x
;
3307 oberon_expr_t
* expr
;
3309 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3312 oberon_generator_init_temp_var(ctx
, x
);
3314 expr
= oberon_new_item(MODE_VAR
, type
, false);
3315 expr
-> item
.var
= x
;
3320 oberon_statement_seq(oberon_context_t
* ctx
);
3323 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3326 && src
-> item
.mode
== MODE_STRING
3327 && src
-> result
-> class == OBERON_TYPE_STRING
3328 && dst
-> result
-> class == OBERON_TYPE_ARRAY
3329 && dst
-> result
-> base
-> class == OBERON_TYPE_CHAR
3330 && dst
-> result
-> size
> 0)
3333 if(strlen(src
-> item
.string
) < dst
-> result
-> size
)
3336 oberon_make_copy_call(ctx
, 2, src
);
3340 oberon_error(ctx
, "string too long for destination");
3345 oberon_check_dst(ctx
, dst
);
3346 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3347 oberon_generate_assign(ctx
, src
, dst
);
3351 static oberon_expr_t
*
3352 oberon_case_labels(oberon_context_t
* ctx
, oberon_expr_t
* val
)
3356 oberon_expr_t
* cond
;
3357 oberon_expr_t
* cond2
;
3359 e1
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3360 oberon_autocast_to(ctx
, e1
, val
-> result
);
3363 if(ctx
-> token
== DOTDOT
)
3365 oberon_assert_token(ctx
, DOTDOT
);
3366 e2
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3367 oberon_autocast_to(ctx
, e2
, val
-> result
);
3373 cond
= oberon_make_bin_op(ctx
, EQUAL
, val
, e1
);
3377 /* val >= e1 && val <= e2 */
3378 cond
= oberon_make_bin_op(ctx
, GEQ
, val
, e1
);
3379 cond2
= oberon_make_bin_op(ctx
, LEQ
, val
, e2
);
3380 cond
= oberon_make_bin_op(ctx
, AND
, cond
, cond2
);
3387 oberon_case(oberon_context_t
* ctx
, oberon_expr_t
* val
, gen_label_t
* end
)
3389 oberon_expr_t
* cond
;
3390 oberon_expr_t
* cond2
;
3391 gen_label_t
* this_end
;
3393 if(ISEXPR(ctx
-> token
))
3395 this_end
= oberon_generator_reserve_label(ctx
);
3397 cond
= oberon_case_labels(ctx
, val
);
3398 while(ctx
-> token
== COMMA
)
3400 oberon_assert_token(ctx
, COMMA
);
3402 cond2
= oberon_case_labels(ctx
, val
);
3403 cond
= oberon_make_bin_op(ctx
, OR
, cond
, cond2
);
3405 oberon_assert_token(ctx
, COLON
);
3407 oberon_generate_branch(ctx
, cond
, false, this_end
);
3408 oberon_statement_seq(ctx
);
3409 oberon_generate_goto(ctx
, end
);
3411 oberon_generate_label(ctx
, this_end
);
3416 oberon_case_statement(oberon_context_t
* ctx
)
3418 oberon_expr_t
* val
;
3419 oberon_expr_t
* expr
;
3422 end
= oberon_generator_reserve_label(ctx
);
3424 oberon_assert_token(ctx
, CASE
);
3425 expr
= oberon_expr(ctx
);
3426 val
= oberon_make_temp_var_item(ctx
, expr
-> result
);
3427 oberon_assign(ctx
, expr
, val
);
3428 oberon_assert_token(ctx
, OF
);
3429 oberon_case(ctx
, val
, end
);
3430 while(ctx
-> token
== BAR
)
3432 oberon_assert_token(ctx
, BAR
);
3433 oberon_case(ctx
, val
, end
);
3436 if(ctx
-> token
== ELSE
)
3438 oberon_assert_token(ctx
, ELSE
);
3439 oberon_statement_seq(ctx
);
3442 oberon_generate_label(ctx
, end
);
3443 oberon_assert_token(ctx
, END
);
3447 oberon_with_guard_do(oberon_context_t
* ctx
, gen_label_t
* end
)
3449 oberon_expr_t
* val
;
3450 oberon_expr_t
* var
;
3451 oberon_expr_t
* type
;
3452 oberon_expr_t
* cond
;
3453 oberon_expr_t
* cast
;
3454 oberon_type_t
* old_type
;
3455 gen_var_t
* old_var
;
3456 gen_label_t
* this_end
;
3458 this_end
= oberon_generator_reserve_label(ctx
);
3460 var
= oberon_qualident_expr(ctx
);
3461 oberon_assert_token(ctx
, COLON
);
3462 type
= oberon_qualident_expr(ctx
);
3463 cond
= oberon_make_bin_op(ctx
, IS
, var
, type
);
3465 oberon_assert_token(ctx
, DO
);
3466 oberon_generate_branch(ctx
, cond
, false, this_end
);
3468 /* Сохраняем ссылку во временной переменной */
3469 val
= oberon_make_temp_var_item(ctx
, type
-> result
);
3470 cast
= oberno_make_record_cast(ctx
, var
, type
-> result
);
3471 oberon_assign(ctx
, cast
, val
);
3472 /* Подменяем тип у оригинальной переменной */
3473 old_type
= var
-> item
.var
-> type
;
3474 var
-> item
.var
-> type
= type
-> result
;
3475 /* Подменяем ссылку на переменную */
3476 old_var
= var
-> item
.var
-> gen_var
;
3477 var
-> item
.var
-> gen_var
= val
-> item
.var
-> gen_var
;
3479 oberon_statement_seq(ctx
);
3480 oberon_generate_goto(ctx
, end
);
3481 oberon_generate_label(ctx
, this_end
);
3483 /* Возвращаем исходное состояние */
3484 var
-> item
.var
-> gen_var
= old_var
;
3485 var
-> item
.var
-> type
= old_type
;
3489 oberon_with_statement(oberon_context_t
* ctx
)
3492 end
= oberon_generator_reserve_label(ctx
);
3494 oberon_assert_token(ctx
, WITH
);
3495 oberon_with_guard_do(ctx
, end
);
3496 while(ctx
-> token
== BAR
)
3498 oberon_assert_token(ctx
, BAR
);
3499 oberon_with_guard_do(ctx
, end
);
3502 if(ctx
-> token
== ELSE
)
3504 oberon_assert_token(ctx
, ELSE
);
3505 oberon_statement_seq(ctx
);
3508 oberon_generate_label(ctx
, end
);
3509 oberon_assert_token(ctx
, END
);
3513 oberon_statement(oberon_context_t
* ctx
)
3515 oberon_expr_t
* item1
;
3516 oberon_expr_t
* item2
;
3518 if(ctx
-> token
== IDENT
)
3520 item1
= oberon_designator(ctx
);
3521 if(ctx
-> token
== ASSIGN
)
3523 oberon_assert_token(ctx
, ASSIGN
);
3524 item2
= oberon_expr(ctx
);
3525 oberon_assign(ctx
, item2
, item1
);
3529 oberon_opt_proc_parens(ctx
, item1
);
3532 else if(ctx
-> token
== IF
)
3536 oberon_expr_t
* cond
;
3538 els
= oberon_generator_reserve_label(ctx
);
3539 end
= oberon_generator_reserve_label(ctx
);
3541 oberon_assert_token(ctx
, IF
);
3542 cond
= oberon_expr(ctx
);
3543 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3545 oberon_error(ctx
, "condition must be boolean");
3547 oberon_assert_token(ctx
, THEN
);
3548 oberon_generate_branch(ctx
, cond
, false, els
);
3549 oberon_statement_seq(ctx
);
3550 oberon_generate_goto(ctx
, end
);
3551 oberon_generate_label(ctx
, els
);
3553 while(ctx
-> token
== ELSIF
)
3555 els
= oberon_generator_reserve_label(ctx
);
3557 oberon_assert_token(ctx
, ELSIF
);
3558 cond
= oberon_expr(ctx
);
3559 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3561 oberon_error(ctx
, "condition must be boolean");
3563 oberon_assert_token(ctx
, THEN
);
3564 oberon_generate_branch(ctx
, cond
, false, els
);
3565 oberon_statement_seq(ctx
);
3566 oberon_generate_goto(ctx
, end
);
3567 oberon_generate_label(ctx
, els
);
3570 if(ctx
-> token
== ELSE
)
3572 oberon_assert_token(ctx
, ELSE
);
3573 oberon_statement_seq(ctx
);
3576 oberon_generate_label(ctx
, end
);
3577 oberon_assert_token(ctx
, END
);
3579 else if(ctx
-> token
== WHILE
)
3581 gen_label_t
* begin
;
3583 oberon_expr_t
* cond
;
3585 begin
= oberon_generator_reserve_label(ctx
);
3586 end
= oberon_generator_reserve_label(ctx
);
3588 oberon_assert_token(ctx
, WHILE
);
3589 oberon_generate_label(ctx
, begin
);
3590 cond
= oberon_expr(ctx
);
3591 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3593 oberon_error(ctx
, "condition must be boolean");
3595 oberon_generate_branch(ctx
, cond
, false, end
);
3597 oberon_assert_token(ctx
, DO
);
3598 oberon_statement_seq(ctx
);
3599 oberon_generate_goto(ctx
, begin
);
3601 oberon_assert_token(ctx
, END
);
3602 oberon_generate_label(ctx
, end
);
3604 else if(ctx
-> token
== REPEAT
)
3606 gen_label_t
* begin
;
3607 oberon_expr_t
* cond
;
3609 begin
= oberon_generator_reserve_label(ctx
);
3610 oberon_generate_label(ctx
, begin
);
3611 oberon_assert_token(ctx
, REPEAT
);
3613 oberon_statement_seq(ctx
);
3615 oberon_assert_token(ctx
, UNTIL
);
3617 cond
= oberon_expr(ctx
);
3618 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3620 oberon_error(ctx
, "condition must be boolean");
3623 oberon_generate_branch(ctx
, cond
, true, begin
);
3625 else if(ctx
-> token
== FOR
)
3627 oberon_expr_t
* from
;
3628 oberon_expr_t
* index
;
3630 oberon_expr_t
* bound
;
3632 oberon_expr_t
* cond
;
3633 oberon_expr_t
* count
;
3634 gen_label_t
* begin
;
3639 begin
= oberon_generator_reserve_label(ctx
);
3640 end
= oberon_generator_reserve_label(ctx
);
3642 oberon_assert_token(ctx
, FOR
);
3643 iname
= oberon_assert_ident(ctx
);
3644 index
= oberon_ident_item(ctx
, iname
);
3645 oberon_assert_token(ctx
, ASSIGN
);
3646 from
= oberon_expr(ctx
);
3647 oberon_assign(ctx
, from
, index
);
3648 oberon_assert_token(ctx
, TO
);
3649 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3650 to
= oberon_expr(ctx
);
3651 oberon_assign(ctx
, to
, bound
);
3652 if(ctx
-> token
== BY
)
3654 oberon_assert_token(ctx
, BY
);
3655 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3659 by
= oberon_integer_item(ctx
, 1);
3662 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3664 oberon_error(ctx
, "must be integer");
3667 if(by
-> item
.integer
> 0)
3671 else if(by
-> item
.integer
< 0)
3677 oberon_error(ctx
, "zero step not allowed");
3680 oberon_assert_token(ctx
, DO
);
3681 oberon_generate_label(ctx
, begin
);
3682 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3683 oberon_generate_branch(ctx
, cond
, false, end
);
3684 oberon_statement_seq(ctx
);
3685 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3686 oberon_assign(ctx
, count
, index
);
3687 oberon_generate_goto(ctx
, begin
);
3688 oberon_generate_label(ctx
, end
);
3689 oberon_assert_token(ctx
, END
);
3691 else if(ctx
-> token
== LOOP
)
3693 gen_label_t
* begin
;
3696 begin
= oberon_generator_reserve_label(ctx
);
3697 end
= oberon_generator_reserve_label(ctx
);
3699 oberon_open_scope(ctx
);
3700 oberon_assert_token(ctx
, LOOP
);
3701 oberon_generate_label(ctx
, begin
);
3702 ctx
-> decl
-> exit_label
= end
;
3703 oberon_statement_seq(ctx
);
3704 oberon_generate_goto(ctx
, begin
);
3705 oberon_generate_label(ctx
, end
);
3706 oberon_assert_token(ctx
, END
);
3707 oberon_close_scope(ctx
-> decl
);
3709 else if(ctx
-> token
== EXIT
)
3711 oberon_assert_token(ctx
, EXIT
);
3712 if(ctx
-> decl
-> exit_label
== NULL
)
3714 oberon_error(ctx
, "not in LOOP-END");
3716 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3718 else if(ctx
-> token
== CASE
)
3720 oberon_case_statement(ctx
);
3722 else if(ctx
-> token
== WITH
)
3724 oberon_with_statement(ctx
);
3726 else if(ctx
-> token
== RETURN
)
3728 oberon_assert_token(ctx
, RETURN
);
3729 if(ISEXPR(ctx
-> token
))
3731 oberon_expr_t
* expr
;
3732 expr
= oberon_expr(ctx
);
3733 oberon_make_return(ctx
, expr
);
3737 oberon_make_return(ctx
, NULL
);
3743 oberon_statement_seq(oberon_context_t
* ctx
)
3745 oberon_statement(ctx
);
3746 while(ctx
-> token
== SEMICOLON
)
3748 oberon_assert_token(ctx
, SEMICOLON
);
3749 oberon_statement(ctx
);
3754 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3756 oberon_module_t
* m
= ctx
-> module_list
;
3757 while(m
&& strcmp(m
-> name
, name
) != 0)
3765 code
= ctx
-> import_module(name
);
3768 oberon_error(ctx
, "no such module");
3771 m
= oberon_compile_module(ctx
, code
);
3777 oberon_error(ctx
, "cyclic module import");
3780 oberon_object_t
* ident
;
3781 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3782 ident
-> module
= m
;
3786 oberon_import_decl(oberon_context_t
* ctx
)
3791 alias
= name
= oberon_assert_ident(ctx
);
3792 if(ctx
-> token
== ASSIGN
)
3794 oberon_assert_token(ctx
, ASSIGN
);
3795 name
= oberon_assert_ident(ctx
);
3798 oberon_import_module(ctx
, alias
, name
);
3802 oberon_import_list(oberon_context_t
* ctx
)
3804 oberon_assert_token(ctx
, IMPORT
);
3806 oberon_import_decl(ctx
);
3807 while(ctx
-> token
== COMMA
)
3809 oberon_assert_token(ctx
, COMMA
);
3810 oberon_import_decl(ctx
);
3813 oberon_assert_token(ctx
, SEMICOLON
);
3817 oberon_parse_module(oberon_context_t
* ctx
)
3821 oberon_read_token(ctx
);
3823 oberon_assert_token(ctx
, MODULE
);
3824 name1
= oberon_assert_ident(ctx
);
3825 oberon_assert_token(ctx
, SEMICOLON
);
3826 ctx
-> mod
-> name
= name1
;
3828 oberon_generator_init_module(ctx
, ctx
-> mod
);
3830 if(ctx
-> token
== IMPORT
)
3832 oberon_import_list(ctx
);
3835 oberon_decl_seq(ctx
);
3837 oberon_generate_begin_module(ctx
);
3838 if(ctx
-> token
== BEGIN
)
3840 oberon_assert_token(ctx
, BEGIN
);
3841 oberon_statement_seq(ctx
);
3843 oberon_generate_end_module(ctx
);
3845 oberon_assert_token(ctx
, END
);
3846 name2
= oberon_assert_ident(ctx
);
3847 oberon_expect_token(ctx
, DOT
);
3849 if(strcmp(name1
, name2
) != 0)
3851 oberon_error(ctx
, "module name not matched");
3854 oberon_generator_fini_module(ctx
-> mod
);
3857 // =======================================================================
3859 // =======================================================================
3862 register_default_types(oberon_context_t
* ctx
)
3864 ctx
-> notype_type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
3865 oberon_generator_init_type(ctx
, ctx
-> notype_type
);
3867 ctx
-> nil_type
= oberon_new_type_ptr(OBERON_TYPE_NIL
);
3868 oberon_generator_init_type(ctx
, ctx
-> nil_type
);
3870 ctx
-> string_type
= oberon_new_type_string(1);
3871 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3873 ctx
-> bool_type
= oberon_new_type_boolean();
3874 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3876 ctx
-> char_type
= oberon_new_type_char(1);
3877 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3879 ctx
-> byte_type
= oberon_new_type_integer(1);
3880 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> byte_type
, 1);
3882 ctx
-> shortint_type
= oberon_new_type_integer(2);
3883 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> shortint_type
, 1);
3885 ctx
-> int_type
= oberon_new_type_integer(4);
3886 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> int_type
, 1);
3888 ctx
-> longint_type
= oberon_new_type_integer(8);
3889 oberon_define_type(ctx
-> world_scope
, "HUGEINT", ctx
-> longint_type
, 1);
3891 ctx
-> real_type
= oberon_new_type_real(4);
3892 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3894 ctx
-> longreal_type
= oberon_new_type_real(8);
3895 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3897 ctx
-> set_type
= oberon_new_type_set(4);
3898 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3902 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3904 oberon_object_t
* proc
;
3905 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3906 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3907 proc
-> type
-> sysproc
= true;
3908 proc
-> type
-> genfunc
= f
;
3909 proc
-> type
-> genproc
= p
;
3912 static oberon_expr_t
*
3913 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3917 oberon_error(ctx
, "too few arguments");
3922 oberon_error(ctx
, "too mach arguments");
3925 oberon_expr_t
* arg
;
3928 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3930 oberon_error(ctx
, "MIN accept only type");
3933 oberon_expr_t
* expr
;
3934 int bits
= arg
-> result
-> size
* 8;
3935 switch(arg
-> result
-> class)
3937 case OBERON_TYPE_INTEGER
:
3938 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3940 case OBERON_TYPE_SET
:
3941 expr
= oberon_integer_item(ctx
, 0);
3944 oberon_error(ctx
, "allowed only basic types");
3951 static oberon_expr_t
*
3952 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3956 oberon_error(ctx
, "too few arguments");
3961 oberon_error(ctx
, "too mach arguments");
3964 oberon_expr_t
* arg
;
3967 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3969 oberon_error(ctx
, "MAX accept only type");
3972 oberon_expr_t
* expr
;
3973 int bits
= arg
-> result
-> size
* 8;
3974 switch(arg
-> result
-> class)
3976 case OBERON_TYPE_INTEGER
:
3977 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3979 case OBERON_TYPE_SET
:
3980 expr
= oberon_integer_item(ctx
, bits
);
3983 oberon_error(ctx
, "allowed only basic types");
3990 static oberon_expr_t
*
3991 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3995 oberon_error(ctx
, "too few arguments");
4000 oberon_error(ctx
, "too mach arguments");
4003 oberon_expr_t
* arg
;
4006 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
4008 oberon_error(ctx
, "SIZE accept only type");
4012 oberon_expr_t
* expr
;
4013 oberon_type_t
* type
= arg
-> result
;
4014 switch(type
-> class)
4016 case OBERON_TYPE_INTEGER
:
4017 case OBERON_TYPE_BOOLEAN
:
4018 case OBERON_TYPE_REAL
:
4019 case OBERON_TYPE_CHAR
:
4020 case OBERON_TYPE_SET
:
4021 size
= type
-> size
;
4024 oberon_error(ctx
, "TODO SIZE");
4028 expr
= oberon_integer_item(ctx
, size
);
4032 static oberon_expr_t
*
4033 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
4037 oberon_error(ctx
, "too few arguments");
4042 oberon_error(ctx
, "too mach arguments");
4045 oberon_expr_t
* arg
;
4047 oberon_check_src(ctx
, arg
);
4049 oberon_type_t
* result_type
;
4050 result_type
= arg
-> result
;
4052 if(result_type
-> class != OBERON_TYPE_INTEGER
)
4054 oberon_error(ctx
, "ABS accepts only integers");
4057 oberon_expr_t
* expr
;
4058 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
4063 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
4067 oberon_error(ctx
, "too few arguments");
4070 oberon_expr_t
* dst
;
4072 oberon_check_dst(ctx
, dst
);
4074 oberon_type_t
* type
;
4075 type
= dst
-> result
;
4077 if(type
-> class != OBERON_TYPE_POINTER
)
4079 oberon_error(ctx
, "not a pointer");
4082 type
= type
-> base
;
4084 oberon_expr_t
* src
;
4085 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
4086 src
-> item
.num_args
= 0;
4087 src
-> item
.args
= NULL
;
4090 if(type
-> class == OBERON_TYPE_ARRAY
)
4092 if(type
-> size
== 0)
4094 oberon_type_t
* x
= type
;
4095 while(x
-> class == OBERON_TYPE_ARRAY
)
4105 if(num_args
< max_args
)
4107 oberon_error(ctx
, "too few arguments");
4110 if(num_args
> max_args
)
4112 oberon_error(ctx
, "too mach arguments");
4115 int num_sizes
= max_args
- 1;
4116 oberon_expr_t
* size_list
= list_args
-> next
;
4118 oberon_expr_t
* arg
= size_list
;
4119 for(int i
= 0; i
< max_args
- 1; i
++)
4121 oberon_check_src(ctx
, arg
);
4122 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
4124 oberon_error(ctx
, "size must be integer");
4129 src
-> item
.num_args
= num_sizes
;
4130 src
-> item
.args
= size_list
;
4132 else if(type
-> class != OBERON_TYPE_RECORD
)
4134 oberon_error(ctx
, "oberon_make_new_call: wat");
4137 if(num_args
> max_args
)
4139 oberon_error(ctx
, "too mach arguments");
4142 oberon_assign(ctx
, src
, dst
);
4146 oberon_make_copy_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
4150 oberon_error(ctx
, "too few arguments");
4155 oberon_error(ctx
, "too mach arguments");
4158 oberon_expr_t
* src
;
4160 oberon_check_src(ctx
, src
);
4162 oberon_expr_t
* dst
;
4163 dst
= list_args
-> next
;
4164 oberon_check_dst(ctx
, dst
);
4166 if(!oberon_is_string_type(src
-> result
))
4168 oberon_error(ctx
, "source must be string or array of char");
4171 if(!oberon_is_string_type(dst
-> result
))
4173 oberon_error(ctx
, "dst must be array of char");
4176 oberon_generate_copy(ctx
, src
, dst
);
4180 oberon_make_assert_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
4184 oberon_error(ctx
, "too few arguments");
4189 oberon_error(ctx
, "too mach arguments");
4192 oberon_expr_t
* cond
;
4194 oberon_check_src(ctx
, cond
);
4196 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
4198 oberon_error(ctx
, "expected boolean");
4203 oberon_generate_assert(ctx
, cond
);
4207 oberon_expr_t
* num
;
4208 num
= list_args
-> next
;
4209 oberon_check_src(ctx
, num
);
4211 if(num
-> result
-> class != OBERON_TYPE_INTEGER
)
4213 oberon_error(ctx
, "expected integer");
4216 oberon_check_const(ctx
, num
);
4218 oberon_generate_assert_n(ctx
, cond
, num
-> item
.integer
);
4223 oberon_make_halt_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
4227 oberon_error(ctx
, "too few arguments");
4232 oberon_error(ctx
, "too mach arguments");
4235 oberon_expr_t
* num
;
4237 oberon_check_src(ctx
, num
);
4239 if(num
-> result
-> class != OBERON_TYPE_INTEGER
)
4241 oberon_error(ctx
, "expected integer");
4244 oberon_check_const(ctx
, num
);
4246 oberon_generate_halt(ctx
, num
-> item
.integer
);
4250 oberon_new_const(oberon_context_t
* ctx
, char * name
, oberon_expr_t
* expr
)
4252 oberon_object_t
* constant
;
4253 constant
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_CONST
, true, false, false);
4254 oberon_check_const(ctx
, expr
);
4255 constant
-> value
= (oberon_item_t
*) expr
;
4259 oberon_create_context(ModuleImportCallback import_module
)
4261 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
4263 oberon_scope_t
* world_scope
;
4264 world_scope
= oberon_open_scope(ctx
);
4265 ctx
-> world_scope
= world_scope
;
4267 ctx
-> import_module
= import_module
;
4269 oberon_generator_init_context(ctx
);
4271 register_default_types(ctx
);
4274 oberon_new_const(ctx
, "TRUE", oberon_make_boolean(ctx
, true));
4275 oberon_new_const(ctx
, "FALSE", oberon_make_boolean(ctx
, false));
4278 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
4279 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
4280 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
4281 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
4284 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
4285 oberon_new_intrinsic(ctx
, "COPY", NULL
, oberon_make_copy_call
);
4286 oberon_new_intrinsic(ctx
, "ASSERT", NULL
, oberon_make_assert_call
);
4287 oberon_new_intrinsic(ctx
, "HALT", NULL
, oberon_make_halt_call
);
4293 oberon_destroy_context(oberon_context_t
* ctx
)
4295 oberon_generator_destroy_context(ctx
);
4300 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
4302 const char * code
= ctx
-> code
;
4303 int code_index
= ctx
-> code_index
;
4305 int token
= ctx
-> token
;
4306 char * string
= ctx
-> string
;
4307 int integer
= ctx
-> integer
;
4308 int real
= ctx
-> real
;
4309 bool longmode
= ctx
-> longmode
;
4310 oberon_scope_t
* decl
= ctx
-> decl
;
4311 oberon_module_t
* mod
= ctx
-> mod
;
4313 oberon_scope_t
* module_scope
;
4314 module_scope
= oberon_open_scope(ctx
);
4316 oberon_module_t
* module
;
4317 module
= calloc(1, sizeof *module
);
4318 module
-> decl
= module_scope
;
4319 module
-> next
= ctx
-> module_list
;
4321 ctx
-> mod
= module
;
4322 ctx
-> module_list
= module
;
4324 oberon_init_scaner(ctx
, newcode
);
4325 oberon_parse_module(ctx
);
4327 module
-> ready
= 1;
4330 ctx
-> code_index
= code_index
;
4332 ctx
-> token
= token
;
4333 ctx
-> string
= string
;
4334 ctx
-> integer
= integer
;
4336 ctx
-> longmode
= longmode
;