e4b9157d151cafa6075c86bbc1a07ebf2e845bef
10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
86 // =======================================================================
88 // =======================================================================
91 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
95 fprintf(stderr
, "error: ");
96 vfprintf(stderr
, fmt
, ptr
);
97 fprintf(stderr
, "\n");
98 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
99 fprintf(stderr
, " c = %c\n", ctx
-> c
);
100 fprintf(stderr
, " token = %i\n", ctx
-> token
);
105 static oberon_type_t
*
106 oberon_new_type_ptr(int class)
108 oberon_type_t
* x
= malloc(sizeof *x
);
109 memset(x
, 0, sizeof *x
);
114 static oberon_type_t
*
115 oberon_new_type_integer(int size
)
118 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
123 static oberon_type_t
*
124 oberon_new_type_boolean()
127 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
131 static oberon_type_t
*
132 oberon_new_type_real(int size
)
135 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
140 static oberon_type_t
*
141 oberon_new_type_char(int size
)
144 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
149 static oberon_type_t
*
150 oberon_new_type_string(int size
)
153 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
158 static oberon_type_t
*
159 oberon_new_type_set(int size
)
162 x
= oberon_new_type_ptr(OBERON_TYPE_SET
);
167 // =======================================================================
169 // =======================================================================
171 static oberon_scope_t
*
172 oberon_open_scope(oberon_context_t
* ctx
)
174 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
175 oberon_object_t
* list
= calloc(1, sizeof *list
);
178 scope
-> list
= list
;
179 scope
-> up
= ctx
-> decl
;
183 scope
-> local
= scope
-> up
-> local
;
184 scope
-> parent
= scope
-> up
-> parent
;
185 scope
-> parent_type
= scope
-> up
-> parent_type
;
186 scope
-> exit_label
= scope
-> up
-> exit_label
;
194 oberon_close_scope(oberon_scope_t
* scope
)
196 oberon_context_t
* ctx
= scope
-> ctx
;
197 ctx
-> decl
= scope
-> up
;
200 static oberon_object_t
*
201 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
203 oberon_object_t
* x
= list
;
204 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
211 static oberon_object_t
*
212 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
214 oberon_object_t
* result
= NULL
;
216 oberon_scope_t
* s
= scope
;
217 while(result
== NULL
&& s
!= NULL
)
219 result
= oberon_find_object_in_list(s
-> list
, name
);
223 if(check_it
&& result
== NULL
)
225 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
231 static oberon_object_t
*
232 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
234 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
235 memset(newvar
, 0, sizeof *newvar
);
236 newvar
-> name
= name
;
237 newvar
-> class = class;
238 newvar
-> export
= export
;
239 newvar
-> read_only
= read_only
;
240 newvar
-> local
= scope
-> local
;
241 newvar
-> parent
= scope
-> parent
;
242 newvar
-> parent_type
= scope
-> parent_type
;
243 newvar
-> module
= scope
-> ctx
-> mod
;
247 static oberon_object_t
*
248 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
252 if(oberon_find_object(scope
-> up
, name
, false))
254 oberon_error(scope
-> ctx
, "already defined");
258 oberon_object_t
* x
= scope
-> list
;
259 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
266 oberon_error(scope
-> ctx
, "already defined");
269 oberon_object_t
* newvar
;
270 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
276 static oberon_object_t
*
277 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
279 oberon_object_t
* id
;
280 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
282 oberon_generator_init_type(scope
-> ctx
, type
);
286 // =======================================================================
288 // =======================================================================
291 oberon_get_char(oberon_context_t
* ctx
)
293 if(ctx
-> code
[ctx
-> code_index
])
295 ctx
-> code_index
+= 1;
296 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
301 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
304 ctx
-> code_index
= 0;
305 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
309 oberon_read_ident(oberon_context_t
* ctx
)
312 int i
= ctx
-> code_index
;
314 int c
= ctx
-> code
[i
];
322 char * ident
= malloc(len
+ 1);
323 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
326 ctx
-> code_index
= i
;
327 ctx
-> c
= ctx
-> code
[i
];
328 ctx
-> string
= ident
;
329 ctx
-> token
= IDENT
;
331 if(strcmp(ident
, "MODULE") == 0)
333 ctx
-> token
= MODULE
;
335 else if(strcmp(ident
, "END") == 0)
339 else if(strcmp(ident
, "VAR") == 0)
343 else if(strcmp(ident
, "BEGIN") == 0)
345 ctx
-> token
= BEGIN
;
347 else if(strcmp(ident
, "TRUE") == 0)
351 else if(strcmp(ident
, "FALSE") == 0)
353 ctx
-> token
= FALSE
;
355 else if(strcmp(ident
, "OR") == 0)
359 else if(strcmp(ident
, "DIV") == 0)
363 else if(strcmp(ident
, "MOD") == 0)
367 else if(strcmp(ident
, "PROCEDURE") == 0)
369 ctx
-> token
= PROCEDURE
;
371 else if(strcmp(ident
, "RETURN") == 0)
373 ctx
-> token
= RETURN
;
375 else if(strcmp(ident
, "CONST") == 0)
377 ctx
-> token
= CONST
;
379 else if(strcmp(ident
, "TYPE") == 0)
383 else if(strcmp(ident
, "ARRAY") == 0)
385 ctx
-> token
= ARRAY
;
387 else if(strcmp(ident
, "OF") == 0)
391 else if(strcmp(ident
, "RECORD") == 0)
393 ctx
-> token
= RECORD
;
395 else if(strcmp(ident
, "POINTER") == 0)
397 ctx
-> token
= POINTER
;
399 else if(strcmp(ident
, "TO") == 0)
403 else if(strcmp(ident
, "NIL") == 0)
407 else if(strcmp(ident
, "IMPORT") == 0)
409 ctx
-> token
= IMPORT
;
411 else if(strcmp(ident
, "IN") == 0)
415 else if(strcmp(ident
, "IS") == 0)
419 else if(strcmp(ident
, "IF") == 0)
423 else if(strcmp(ident
, "THEN") == 0)
427 else if(strcmp(ident
, "ELSE") == 0)
431 else if(strcmp(ident
, "ELSIF") == 0)
433 ctx
-> token
= ELSIF
;
435 else if(strcmp(ident
, "WHILE") == 0)
437 ctx
-> token
= WHILE
;
439 else if(strcmp(ident
, "DO") == 0)
443 else if(strcmp(ident
, "REPEAT") == 0)
445 ctx
-> token
= REPEAT
;
447 else if(strcmp(ident
, "UNTIL") == 0)
449 ctx
-> token
= UNTIL
;
451 else if(strcmp(ident
, "FOR") == 0)
455 else if(strcmp(ident
, "BY") == 0)
459 else if(strcmp(ident
, "LOOP") == 0)
463 else if(strcmp(ident
, "EXIT") == 0)
467 else if(strcmp(ident
, "CASE") == 0)
471 else if(strcmp(ident
, "WITH") == 0)
478 oberon_read_number(oberon_context_t
* ctx
)
491 * mode = 3 == LONGREAL
495 start_i
= ctx
-> code_index
;
497 while(isdigit(ctx
-> c
))
499 oberon_get_char(ctx
);
502 end_i
= ctx
-> code_index
;
504 if(isxdigit(ctx
-> c
))
507 while(isxdigit(ctx
-> c
))
509 oberon_get_char(ctx
);
512 end_i
= ctx
-> code_index
;
517 oberon_get_char(ctx
);
519 else if(ctx
-> c
== 'X')
522 oberon_get_char(ctx
);
526 oberon_error(ctx
, "invalid hex number");
529 else if(ctx
-> c
== '.')
531 oberon_get_char(ctx
);
534 /* Чит: избегаем конфликта с DOTDOT */
535 ctx
-> code_index
-= 1;
541 while(isdigit(ctx
-> c
))
543 oberon_get_char(ctx
);
546 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
548 exp_i
= ctx
-> code_index
;
555 oberon_get_char(ctx
);
557 if(ctx
-> c
== '+' || ctx
-> c
== '-')
559 oberon_get_char(ctx
);
562 while(isdigit(ctx
-> c
))
564 oberon_get_char(ctx
);
568 end_i
= ctx
-> code_index
;
576 oberon_get_char(ctx
);
578 else if(ctx
-> c
== 'X')
581 oberon_get_char(ctx
);
585 int len
= end_i
- start_i
;
586 ident
= malloc(len
+ 1);
587 memcpy(ident
, &ctx
-> code
[start_i
], len
);
590 ctx
-> longmode
= false;
593 int i
= exp_i
- start_i
;
595 ctx
-> longmode
= true;
601 integer
= atol(ident
);
603 ctx
-> token
= INTEGER
;
606 sscanf(ident
, "%lx", &integer
);
608 ctx
-> token
= INTEGER
;
612 sscanf(ident
, "%lf", &real
);
616 sscanf(ident
, "%lx", &integer
);
621 oberon_error(ctx
, "oberon_read_number: wat");
625 ctx
-> string
= ident
;
626 ctx
-> integer
= integer
;
631 oberon_skip_space(oberon_context_t
* ctx
)
633 while(isspace(ctx
-> c
))
635 oberon_get_char(ctx
);
640 oberon_read_comment(oberon_context_t
* ctx
)
647 oberon_get_char(ctx
);
650 oberon_get_char(ctx
);
654 else if(ctx
-> c
== '*')
656 oberon_get_char(ctx
);
659 oberon_get_char(ctx
);
663 else if(ctx
-> c
== 0)
665 oberon_error(ctx
, "unterminated comment");
669 oberon_get_char(ctx
);
674 static void oberon_read_string(oberon_context_t
* ctx
)
677 oberon_get_char(ctx
);
679 int start
= ctx
-> code_index
;
681 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
683 oberon_get_char(ctx
);
688 oberon_error(ctx
, "unterminated string");
691 int end
= ctx
-> code_index
;
693 oberon_get_char(ctx
);
695 char * string
= calloc(1, end
- start
+ 1);
696 strncpy(string
, &ctx
-> code
[start
], end
- start
);
698 ctx
-> token
= STRING
;
699 ctx
-> string
= string
;
701 printf("oberon_read_string: string ((%s))\n", string
);
704 static void oberon_read_token(oberon_context_t
* ctx
);
707 oberon_read_symbol(oberon_context_t
* ctx
)
716 ctx
-> token
= SEMICOLON
;
717 oberon_get_char(ctx
);
720 ctx
-> token
= COLON
;
721 oberon_get_char(ctx
);
724 ctx
-> token
= ASSIGN
;
725 oberon_get_char(ctx
);
730 oberon_get_char(ctx
);
733 ctx
-> token
= DOTDOT
;
734 oberon_get_char(ctx
);
738 ctx
-> token
= LPAREN
;
739 oberon_get_char(ctx
);
742 oberon_get_char(ctx
);
743 oberon_read_comment(ctx
);
744 oberon_read_token(ctx
);
748 ctx
-> token
= RPAREN
;
749 oberon_get_char(ctx
);
752 ctx
-> token
= EQUAL
;
753 oberon_get_char(ctx
);
757 oberon_get_char(ctx
);
761 oberon_get_char(ctx
);
765 oberon_get_char(ctx
);
769 ctx
-> token
= GREAT
;
770 oberon_get_char(ctx
);
774 oberon_get_char(ctx
);
779 oberon_get_char(ctx
);
782 ctx
-> token
= MINUS
;
783 oberon_get_char(ctx
);
787 oberon_get_char(ctx
);
790 oberon_get_char(ctx
);
791 oberon_error(ctx
, "unstarted comment");
795 ctx
-> token
= SLASH
;
796 oberon_get_char(ctx
);
800 oberon_get_char(ctx
);
804 oberon_get_char(ctx
);
807 ctx
-> token
= COMMA
;
808 oberon_get_char(ctx
);
811 ctx
-> token
= LBRACK
;
812 oberon_get_char(ctx
);
815 ctx
-> token
= RBRACK
;
816 oberon_get_char(ctx
);
819 ctx
-> token
= UPARROW
;
820 oberon_get_char(ctx
);
823 oberon_read_string(ctx
);
826 oberon_read_string(ctx
);
829 ctx
-> token
= LBRACE
;
830 oberon_get_char(ctx
);
833 ctx
-> token
= RBRACE
;
834 oberon_get_char(ctx
);
838 oberon_get_char(ctx
);
841 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
847 oberon_read_token(oberon_context_t
* ctx
)
849 oberon_skip_space(ctx
);
854 oberon_read_ident(ctx
);
858 oberon_read_number(ctx
);
862 oberon_read_symbol(ctx
);
866 // =======================================================================
868 // =======================================================================
870 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
871 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
872 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
873 static char * oberon_assert_ident(oberon_context_t
* ctx
);
874 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
875 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
876 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
878 static oberon_expr_t
*
879 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
881 oberon_oper_t
* operator;
882 operator = malloc(sizeof *operator);
883 memset(operator, 0, sizeof *operator);
885 operator -> is_item
= 0;
886 operator -> result
= result
;
887 operator -> read_only
= 1;
889 operator -> left
= left
;
890 operator -> right
= right
;
892 return (oberon_expr_t
*) operator;
895 static oberon_expr_t
*
896 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
898 oberon_item_t
* item
;
899 item
= malloc(sizeof *item
);
900 memset(item
, 0, sizeof *item
);
903 item
-> result
= result
;
904 item
-> read_only
= read_only
;
907 return (oberon_expr_t
*)item
;
910 static oberon_expr_t
*
911 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
913 oberon_expr_t
* expr
;
914 oberon_type_t
* result
;
916 result
= a
-> result
;
920 if(result
-> class == OBERON_TYPE_SET
)
922 expr
= oberon_new_operator(OP_COMPLEMENTATION
, result
, a
, NULL
);
924 else if(result
-> class == OBERON_TYPE_INTEGER
)
926 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
930 oberon_error(ctx
, "incompatible operator type");
933 else if(token
== NOT
)
935 if(result
-> class != OBERON_TYPE_BOOLEAN
)
937 oberon_error(ctx
, "incompatible operator type");
940 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
944 oberon_error(ctx
, "oberon_make_unary_op: wat");
951 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
953 oberon_expr_t
* last
;
958 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
962 *first
= last
= oberon_expr(ctx
);
964 while(ctx
-> token
== COMMA
)
966 oberon_assert_token(ctx
, COMMA
);
967 oberon_expr_t
* current
;
971 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
975 current
= oberon_expr(ctx
);
978 last
-> next
= current
;
984 static oberon_expr_t
*
985 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
987 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
990 static oberon_expr_t
*
991 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
993 oberon_type_t
* from
= expr
-> result
;
994 oberon_type_t
* to
= rec
;
996 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
998 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1000 printf("oberno_make_record_cast: pointers\n");
1001 from
= from
-> base
;
1005 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1007 oberon_error(ctx
, "must be record type");
1010 return oberon_cast_expr(ctx
, expr
, rec
);
1013 static oberon_type_t
*
1014 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1016 oberon_type_t
* result
;
1017 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
1021 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
1025 else if(a
-> class != b
-> class)
1027 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
1029 else if(a
-> size
> b
-> size
)
1042 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
1044 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1046 from
= from
-> base
;
1050 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1052 oberon_error(ctx
, "not a record");
1055 oberon_type_t
* t
= from
;
1056 while(t
!= NULL
&& t
!= to
)
1063 oberon_error(ctx
, "incompatible record types");
1068 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1070 if(dst
-> is_item
== false)
1072 oberon_error(ctx
, "not variable");
1075 switch(dst
-> item
.mode
)
1086 oberon_error(ctx
, "not variable");
1092 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1096 if(src
-> item
.mode
== MODE_TYPE
)
1098 oberon_error(ctx
, "not variable");
1103 static oberon_expr_t
*
1104 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1107 // Если классы типов равны
1108 // Если INTEGER переводится в REAL
1109 // Есди STRING переводится в ARRAY OF CHAR
1111 oberon_check_src(ctx
, expr
);
1114 if(pref
-> class != expr
-> result
-> class)
1116 printf("expr class %i\n", expr
-> result
-> class);
1117 printf("pref class %i\n", pref
-> class);
1119 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1121 if(pref
-> class == OBERON_TYPE_ARRAY
)
1123 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1133 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1135 if(pref
-> class != OBERON_TYPE_REAL
)
1148 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1151 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1153 if(expr
-> result
-> size
> pref
-> size
)
1155 oberon_error(ctx
, "incompatible size");
1159 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1162 else if(pref
-> class == OBERON_TYPE_RECORD
)
1164 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1165 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1167 else if(pref
-> class == OBERON_TYPE_POINTER
)
1169 assert(pref
-> base
);
1170 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1172 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1173 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1175 else if(expr
-> result
-> base
!= pref
-> base
)
1177 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1179 oberon_error(ctx
, "incompatible pointer types");
1188 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1190 oberon_type_t
* a
= (*ea
) -> result
;
1191 oberon_type_t
* b
= (*eb
) -> result
;
1192 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1193 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1194 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1198 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1200 if(desig
-> mode
!= MODE_CALL
)
1202 oberon_error(ctx
, "expected mode CALL");
1205 oberon_type_t
* fn
= desig
-> parent
-> result
;
1206 int num_args
= desig
-> num_args
;
1207 int num_decl
= fn
-> num_decl
;
1209 if(num_args
< num_decl
)
1211 oberon_error(ctx
, "too few arguments");
1213 else if(num_args
> num_decl
)
1215 oberon_error(ctx
, "too many arguments");
1218 /* Делаем проверку на запись и делаем автокаст */
1219 oberon_expr_t
* casted
[num_args
];
1220 oberon_expr_t
* arg
= desig
-> args
;
1221 oberon_object_t
* param
= fn
-> decl
;
1222 for(int i
= 0; i
< num_args
; i
++)
1224 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1226 if(arg
-> read_only
)
1228 oberon_error(ctx
, "assign to read-only var");
1232 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1234 param
= param
-> next
;
1237 /* Создаём новый список выражений */
1241 for(int i
= 0; i
< num_args
- 1; i
++)
1243 casted
[i
] -> next
= casted
[i
+ 1];
1245 desig
-> args
= arg
;
1249 static oberon_expr_t
*
1250 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1252 oberon_type_t
* signature
= item
-> result
;
1253 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1255 oberon_error(ctx
, "not a procedure");
1258 oberon_expr_t
* call
;
1260 if(signature
-> sysproc
)
1262 if(signature
-> genfunc
== NULL
)
1264 oberon_error(ctx
, "not a function-procedure");
1267 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1271 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1273 oberon_error(ctx
, "attempt to call procedure in expression");
1276 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1277 call
-> item
.parent
= item
;
1278 call
-> item
.num_args
= num_args
;
1279 call
-> item
.args
= list_args
;
1280 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1287 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1289 oberon_type_t
* signature
= item
-> result
;
1290 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1292 oberon_error(ctx
, "not a procedure");
1295 oberon_expr_t
* call
;
1297 if(signature
-> sysproc
)
1299 if(signature
-> genproc
== NULL
)
1301 oberon_error(ctx
, "not a procedure");
1304 signature
-> genproc(ctx
, num_args
, list_args
);
1308 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1310 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1313 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1314 call
-> item
.parent
= item
;
1315 call
-> item
.num_args
= num_args
;
1316 call
-> item
.args
= list_args
;
1317 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1318 oberon_generate_call_proc(ctx
, call
);
1326 || ((x) == INTEGER) \
1329 || ((x) == STRING) \
1331 || ((x) == LPAREN) \
1336 static oberon_expr_t
*
1337 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1339 printf("oberno_make_dereferencing\n");
1340 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1342 oberon_error(ctx
, "not a pointer");
1345 assert(expr
-> is_item
);
1347 oberon_expr_t
* selector
;
1348 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1349 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1354 static oberon_expr_t
*
1355 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1357 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1359 desig
= oberno_make_dereferencing(ctx
, desig
);
1362 assert(desig
-> is_item
);
1364 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1366 oberon_error(ctx
, "not array");
1369 oberon_type_t
* base
;
1370 base
= desig
-> result
-> base
;
1372 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1374 oberon_error(ctx
, "index must be integer");
1377 // Статическая проверка границ массива
1378 if(desig
-> result
-> size
!= 0)
1380 if(index
-> is_item
)
1382 if(index
-> item
.mode
== MODE_INTEGER
)
1384 int arr_size
= desig
-> result
-> size
;
1385 int index_int
= index
-> item
.integer
;
1386 if(index_int
< 0 || index_int
> arr_size
- 1)
1388 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1394 oberon_expr_t
* selector
;
1395 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1396 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1397 selector
-> item
.num_args
= 1;
1398 selector
-> item
.args
= index
;
1403 static oberon_expr_t
*
1404 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1406 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1408 expr
= oberno_make_dereferencing(ctx
, expr
);
1411 assert(expr
-> is_item
);
1413 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1415 oberon_error(ctx
, "not record");
1418 oberon_type_t
* rec
= expr
-> result
;
1420 oberon_object_t
* field
;
1421 field
= oberon_find_object(rec
-> scope
, name
, true);
1423 if(field
-> export
== 0)
1425 if(field
-> module
!= ctx
-> mod
)
1427 oberon_error(ctx
, "field not exported");
1432 if(field
-> read_only
)
1434 if(field
-> module
!= ctx
-> mod
)
1440 oberon_expr_t
* selector
;
1441 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1442 selector
-> item
.var
= field
;
1443 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1448 #define ISSELECTOR(x) \
1451 || ((x) == UPARROW) \
1454 static oberon_object_t
*
1455 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1458 oberon_object_t
* x
;
1460 name
= oberon_assert_ident(ctx
);
1461 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1465 if(x
-> class == OBERON_CLASS_MODULE
)
1467 oberon_assert_token(ctx
, DOT
);
1468 name
= oberon_assert_ident(ctx
);
1469 /* Наличие объектов в левых модулях всегда проверяется */
1470 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1472 if(x
-> export
== 0)
1474 oberon_error(ctx
, "not exported");
1487 static oberon_expr_t
*
1488 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1491 oberon_object_t
* x
;
1492 oberon_expr_t
* expr
;
1494 x
= oberon_find_object(ctx
-> decl
, name
, true);
1497 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1502 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1503 expr
-> item
.var
= x
;
1507 static oberon_expr_t
*
1508 oberon_qualident_expr(oberon_context_t
* ctx
)
1510 oberon_object_t
* var
;
1511 oberon_expr_t
* expr
;
1513 var
= oberon_qualident(ctx
, NULL
, 1);
1516 if(var
-> read_only
)
1518 if(var
-> module
!= ctx
-> mod
)
1524 switch(var
-> class)
1526 case OBERON_CLASS_CONST
:
1528 expr
= (oberon_expr_t
*) var
-> value
;
1530 case OBERON_CLASS_TYPE
:
1531 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1533 case OBERON_CLASS_VAR
:
1534 case OBERON_CLASS_VAR_PARAM
:
1535 case OBERON_CLASS_PARAM
:
1536 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1538 case OBERON_CLASS_PROC
:
1539 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1542 oberon_error(ctx
, "invalid designator");
1546 expr
-> item
.var
= var
;
1551 static oberon_expr_t
*
1552 oberon_designator(oberon_context_t
* ctx
)
1555 oberon_expr_t
* expr
;
1557 expr
= oberon_qualident_expr(ctx
);
1559 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1561 switch(ctx
-> token
)
1564 oberon_assert_token(ctx
, DOT
);
1565 name
= oberon_assert_ident(ctx
);
1566 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1569 oberon_assert_token(ctx
, LBRACK
);
1570 int num_indexes
= 0;
1571 oberon_expr_t
* indexes
= NULL
;
1572 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1573 oberon_assert_token(ctx
, RBRACK
);
1575 for(int i
= 0; i
< num_indexes
; i
++)
1577 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1578 indexes
= indexes
-> next
;
1582 oberon_assert_token(ctx
, UPARROW
);
1583 expr
= oberno_make_dereferencing(ctx
, expr
);
1586 oberon_assert_token(ctx
, LPAREN
);
1587 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1588 if(objtype
-> class != OBERON_CLASS_TYPE
)
1590 oberon_error(ctx
, "must be type");
1592 oberon_assert_token(ctx
, RPAREN
);
1593 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1596 oberon_error(ctx
, "oberon_designator: wat");
1604 static oberon_expr_t
*
1605 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1607 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1608 if(ctx
-> token
== LPAREN
)
1610 oberon_assert_token(ctx
, LPAREN
);
1613 oberon_expr_t
* arguments
= NULL
;
1615 if(ISEXPR(ctx
-> token
))
1617 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1620 assert(expr
-> is_item
== 1);
1621 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1623 oberon_assert_token(ctx
, RPAREN
);
1630 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1632 assert(expr
-> is_item
);
1635 oberon_expr_t
* arguments
= NULL
;
1637 if(ctx
-> token
== LPAREN
)
1639 oberon_assert_token(ctx
, LPAREN
);
1641 if(ISEXPR(ctx
-> token
))
1643 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1646 oberon_assert_token(ctx
, RPAREN
);
1649 /* Вызов происходит даже без скобок */
1650 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1653 static oberon_type_t
*
1654 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1656 if(i
>= -128 && i
<= 127)
1658 return ctx
-> byte_type
;
1660 else if(i
>= -32768 && i
<= 32767)
1662 return ctx
-> shortint_type
;
1664 else if(i
>= -2147483648 && i
<= 2147483647)
1666 return ctx
-> int_type
;
1670 return ctx
-> longint_type
;
1674 static oberon_expr_t
*
1675 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1677 oberon_expr_t
* expr
;
1678 oberon_type_t
* result
;
1679 result
= oberon_get_type_of_int_value(ctx
, i
);
1680 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1681 expr
-> item
.integer
= i
;
1685 static oberon_expr_t
*
1686 oberon_element(oberon_context_t
* ctx
)
1691 e1
= oberon_expr(ctx
);
1692 if(e1
-> result
-> class != OBERON_TYPE_INTEGER
)
1694 oberon_error(ctx
, "expected integer");
1698 if(ctx
-> token
== DOTDOT
)
1700 oberon_assert_token(ctx
, DOTDOT
);
1701 e2
= oberon_expr(ctx
);
1702 if(e2
-> result
-> class != OBERON_TYPE_INTEGER
)
1704 oberon_error(ctx
, "expected integer");
1708 oberon_expr_t
* set
;
1709 set
= oberon_new_operator(OP_RANGE
, ctx
-> set_type
, e1
, e2
);
1713 static oberon_expr_t
*
1714 oberon_set(oberon_context_t
* ctx
)
1716 oberon_expr_t
* set
;
1717 oberon_expr_t
* elements
;
1718 set
= oberon_new_item(MODE_SET
, ctx
-> set_type
, true);
1719 set
-> item
.integer
= 0;
1721 oberon_assert_token(ctx
, LBRACE
);
1722 if(ISEXPR(ctx
-> token
))
1724 elements
= oberon_element(ctx
);
1725 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1726 while(ctx
-> token
== COMMA
)
1728 oberon_assert_token(ctx
, COMMA
);
1729 elements
= oberon_element(ctx
);
1730 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1733 oberon_assert_token(ctx
, RBRACE
);
1738 static oberon_expr_t
*
1739 oberon_factor(oberon_context_t
* ctx
)
1741 oberon_expr_t
* expr
;
1742 oberon_type_t
* result
;
1744 switch(ctx
-> token
)
1747 expr
= oberon_designator(ctx
);
1748 expr
= oberon_opt_func_parens(ctx
, expr
);
1751 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1752 oberon_assert_token(ctx
, INTEGER
);
1755 result
= ctx
-> char_type
;
1756 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1757 expr
-> item
.integer
= ctx
-> integer
;
1758 oberon_assert_token(ctx
, CHAR
);
1761 result
= ctx
-> string_type
;
1762 expr
= oberon_new_item(MODE_STRING
, result
, true);
1763 expr
-> item
.string
= ctx
-> string
;
1764 oberon_assert_token(ctx
, STRING
);
1767 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1768 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1769 expr
-> item
.real
= ctx
-> real
;
1770 oberon_assert_token(ctx
, REAL
);
1773 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1774 expr
-> item
.boolean
= true;
1775 oberon_assert_token(ctx
, TRUE
);
1778 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1779 expr
-> item
.boolean
= false;
1780 oberon_assert_token(ctx
, FALSE
);
1783 expr
= oberon_set(ctx
);
1786 oberon_assert_token(ctx
, LPAREN
);
1787 expr
= oberon_expr(ctx
);
1788 oberon_assert_token(ctx
, RPAREN
);
1791 oberon_assert_token(ctx
, NOT
);
1792 expr
= oberon_factor(ctx
);
1793 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1796 oberon_assert_token(ctx
, NIL
);
1797 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1800 oberon_error(ctx
, "invalid expression");
1806 #define ITMAKESBOOLEAN(x) \
1807 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1809 #define ITUSEONLYINTEGER(x) \
1810 ((x) >= LESS && (x) <= GEQ)
1812 #define ITUSEONLYBOOLEAN(x) \
1813 (((x) == OR) || ((x) == AND))
1816 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1818 oberon_expr_t
* expr
= *e
;
1819 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1821 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1823 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1827 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1830 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1832 oberon_error(ctx
, "required numeric type");
1836 static oberon_expr_t
*
1837 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1839 oberon_expr_t
* expr
;
1840 oberon_type_t
* result
;
1845 if(a
-> result
-> class != OBERON_TYPE_INTEGER
)
1847 oberon_error(ctx
, "must be integer");
1850 if(b
-> result
-> class != OBERON_TYPE_SET
)
1852 oberon_error(ctx
, "must be set");
1855 result
= ctx
-> bool_type
;
1856 expr
= oberon_new_operator(OP_IN
, result
, a
, b
);
1858 else if(token
== IS
)
1860 oberon_type_t
* v
= a
-> result
;
1861 if(v
-> class == OBERON_TYPE_POINTER
)
1864 if(v
-> class != OBERON_TYPE_RECORD
)
1866 oberon_error(ctx
, "must be record");
1869 else if(v
-> class != OBERON_TYPE_RECORD
)
1871 oberon_error(ctx
, "must be record");
1874 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1876 oberon_error(ctx
, "requires type");
1879 oberon_type_t
* t
= b
-> result
;
1880 if(t
-> class == OBERON_TYPE_POINTER
)
1883 if(t
-> class != OBERON_TYPE_RECORD
)
1885 oberon_error(ctx
, "must be record");
1888 else if(t
-> class != OBERON_TYPE_RECORD
)
1890 oberon_error(ctx
, "must be record");
1893 result
= ctx
-> bool_type
;
1894 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1896 else if(ITMAKESBOOLEAN(token
))
1898 if(ITUSEONLYINTEGER(token
))
1900 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1901 || b
-> result
-> class == OBERON_TYPE_INTEGER
1902 || a
-> result
-> class == OBERON_TYPE_REAL
1903 || b
-> result
-> class == OBERON_TYPE_REAL
)
1909 oberon_error(ctx
, "used only with numeric types");
1912 else if(ITUSEONLYBOOLEAN(token
))
1914 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1915 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1917 oberon_error(ctx
, "used only with boolean type");
1921 oberon_autocast_binary_op(ctx
, &a
, &b
);
1922 result
= ctx
-> bool_type
;
1926 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1928 else if(token
== NEQ
)
1930 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1932 else if(token
== LESS
)
1934 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1936 else if(token
== LEQ
)
1938 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1940 else if(token
== GREAT
)
1942 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1944 else if(token
== GEQ
)
1946 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1948 else if(token
== OR
)
1950 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1952 else if(token
== AND
)
1954 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1958 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1961 else if(token
== SLASH
)
1963 if(a
-> result
-> class == OBERON_TYPE_SET
1964 || b
-> result
-> class == OBERON_TYPE_SET
)
1966 oberon_autocast_binary_op(ctx
, &a
, &b
);
1967 result
= a
-> result
;
1968 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
1972 oberon_autocast_to_real(ctx
, &a
);
1973 oberon_autocast_to_real(ctx
, &b
);
1974 oberon_autocast_binary_op(ctx
, &a
, &b
);
1975 result
= a
-> result
;
1976 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
1979 else if(token
== DIV
)
1981 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1982 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1984 oberon_error(ctx
, "operator DIV requires integer type");
1987 oberon_autocast_binary_op(ctx
, &a
, &b
);
1988 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1992 oberon_autocast_binary_op(ctx
, &a
, &b
);
1993 result
= a
-> result
;
1994 if(result
-> class == OBERON_TYPE_SET
)
1999 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
2002 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
2005 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
2012 else if(result
-> class == OBERON_TYPE_INTEGER
2013 || result
-> class == OBERON_TYPE_REAL
)
2018 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
2021 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
2024 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
2027 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2042 oberon_error(ctx
, "invalid operation");
2048 #define ISMULOP(x) \
2049 ((x) >= STAR && (x) <= AND)
2051 static oberon_expr_t
*
2052 oberon_term_expr(oberon_context_t
* ctx
)
2054 oberon_expr_t
* expr
;
2056 expr
= oberon_factor(ctx
);
2057 while(ISMULOP(ctx
-> token
))
2059 int token
= ctx
-> token
;
2060 oberon_read_token(ctx
);
2062 oberon_expr_t
* inter
= oberon_factor(ctx
);
2063 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2069 #define ISADDOP(x) \
2070 ((x) >= PLUS && (x) <= OR)
2072 static oberon_expr_t
*
2073 oberon_simple_expr(oberon_context_t
* ctx
)
2075 oberon_expr_t
* expr
;
2078 if(ctx
-> token
== PLUS
)
2081 oberon_assert_token(ctx
, PLUS
);
2083 else if(ctx
-> token
== MINUS
)
2086 oberon_assert_token(ctx
, MINUS
);
2089 expr
= oberon_term_expr(ctx
);
2093 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2096 while(ISADDOP(ctx
-> token
))
2098 int token
= ctx
-> token
;
2099 oberon_read_token(ctx
);
2101 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2102 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2108 #define ISRELATION(x) \
2109 ((x) >= EQUAL && (x) <= IS)
2111 static oberon_expr_t
*
2112 oberon_expr(oberon_context_t
* ctx
)
2114 oberon_expr_t
* expr
;
2116 expr
= oberon_simple_expr(ctx
);
2117 while(ISRELATION(ctx
-> token
))
2119 int token
= ctx
-> token
;
2120 oberon_read_token(ctx
);
2122 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2123 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2129 static oberon_item_t
*
2130 oberon_const_expr(oberon_context_t
* ctx
)
2132 oberon_expr_t
* expr
;
2133 expr
= oberon_expr(ctx
);
2135 if(expr
-> is_item
== 0)
2137 oberon_error(ctx
, "const expression are required");
2140 switch(expr
-> item
.mode
)
2152 oberon_error(ctx
, "const expression are required");
2156 return (oberon_item_t
*) expr
;
2159 // =======================================================================
2161 // =======================================================================
2163 static void oberon_decl_seq(oberon_context_t
* ctx
);
2164 static void oberon_statement_seq(oberon_context_t
* ctx
);
2165 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2168 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2170 if(ctx
-> token
!= token
)
2172 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2177 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2179 oberon_expect_token(ctx
, token
);
2180 oberon_read_token(ctx
);
2184 oberon_assert_ident(oberon_context_t
* ctx
)
2186 oberon_expect_token(ctx
, IDENT
);
2187 char * ident
= ctx
-> string
;
2188 oberon_read_token(ctx
);
2193 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2195 switch(ctx
-> token
)
2198 oberon_assert_token(ctx
, STAR
);
2203 oberon_assert_token(ctx
, MINUS
);
2214 static oberon_object_t
*
2215 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2220 oberon_object_t
* x
;
2222 name
= oberon_assert_ident(ctx
);
2223 oberon_def(ctx
, &export
, &read_only
);
2225 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2230 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2233 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2234 while(ctx
-> token
== COMMA
)
2236 oberon_assert_token(ctx
, COMMA
);
2237 oberon_ident_def(ctx
, class, check_upscope
);
2243 oberon_var_decl(oberon_context_t
* ctx
)
2246 oberon_object_t
* list
;
2247 oberon_type_t
* type
;
2248 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2250 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2251 oberon_assert_token(ctx
, COLON
);
2252 oberon_type(ctx
, &type
);
2254 oberon_object_t
* var
= list
;
2255 for(int i
= 0; i
< num
; i
++)
2262 static oberon_object_t
*
2263 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2265 int class = OBERON_CLASS_PARAM
;
2266 if(ctx
-> token
== VAR
)
2268 oberon_read_token(ctx
);
2269 class = OBERON_CLASS_VAR_PARAM
;
2273 oberon_object_t
* list
;
2274 oberon_ident_list(ctx
, class, false, &num
, &list
);
2276 oberon_assert_token(ctx
, COLON
);
2278 oberon_type_t
* type
;
2279 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2280 oberon_type(ctx
, &type
);
2282 oberon_object_t
* param
= list
;
2283 for(int i
= 0; i
< num
; i
++)
2285 param
-> type
= type
;
2286 param
= param
-> next
;
2293 #define ISFPSECTION \
2294 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2297 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2299 oberon_assert_token(ctx
, LPAREN
);
2303 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2304 while(ctx
-> token
== SEMICOLON
)
2306 oberon_assert_token(ctx
, SEMICOLON
);
2307 oberon_fp_section(ctx
, &signature
-> num_decl
);
2311 oberon_assert_token(ctx
, RPAREN
);
2313 if(ctx
-> token
== COLON
)
2315 oberon_assert_token(ctx
, COLON
);
2317 oberon_object_t
* typeobj
;
2318 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2319 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2321 oberon_error(ctx
, "function result is not type");
2323 signature
-> base
= typeobj
-> type
;
2328 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2330 oberon_type_t
* signature
;
2332 signature
-> class = OBERON_TYPE_PROCEDURE
;
2333 signature
-> num_decl
= 0;
2334 signature
-> base
= ctx
-> void_type
;
2335 signature
-> decl
= NULL
;
2337 if(ctx
-> token
== LPAREN
)
2339 oberon_formal_pars(ctx
, signature
);
2344 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2346 if(a
-> num_decl
!= b
-> num_decl
)
2348 oberon_error(ctx
, "number parameters not matched");
2351 int num_param
= a
-> num_decl
;
2352 oberon_object_t
* param_a
= a
-> decl
;
2353 oberon_object_t
* param_b
= b
-> decl
;
2354 for(int i
= 0; i
< num_param
; i
++)
2356 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2358 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2361 if(param_a
-> type
!= param_b
-> type
)
2363 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2366 param_a
= param_a
-> next
;
2367 param_b
= param_b
-> next
;
2372 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2374 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2375 oberon_type_t
* result_type
= proc
-> type
-> base
;
2377 if(result_type
-> class == OBERON_TYPE_VOID
)
2381 oberon_error(ctx
, "procedure has no result type");
2388 oberon_error(ctx
, "procedure requires expression on result");
2391 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2394 proc
-> has_return
= 1;
2396 oberon_generate_return(ctx
, expr
);
2400 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2402 oberon_assert_token(ctx
, SEMICOLON
);
2404 ctx
-> decl
= proc
-> scope
;
2406 oberon_decl_seq(ctx
);
2408 oberon_generate_begin_proc(ctx
, proc
);
2410 if(ctx
-> token
== BEGIN
)
2412 oberon_assert_token(ctx
, BEGIN
);
2413 oberon_statement_seq(ctx
);
2416 oberon_assert_token(ctx
, END
);
2417 char * name
= oberon_assert_ident(ctx
);
2418 if(strcmp(name
, proc
-> name
) != 0)
2420 oberon_error(ctx
, "procedure name not matched");
2423 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2424 && proc
-> has_return
== 0)
2426 oberon_make_return(ctx
, NULL
);
2429 if(proc
-> has_return
== 0)
2431 oberon_error(ctx
, "procedure requires return");
2434 oberon_generate_end_proc(ctx
);
2435 oberon_close_scope(ctx
-> decl
);
2439 oberon_proc_decl(oberon_context_t
* ctx
)
2441 oberon_assert_token(ctx
, PROCEDURE
);
2444 if(ctx
-> token
== UPARROW
)
2446 oberon_assert_token(ctx
, UPARROW
);
2453 name
= oberon_assert_ident(ctx
);
2454 oberon_def(ctx
, &export
, &read_only
);
2456 oberon_scope_t
* proc_scope
;
2457 proc_scope
= oberon_open_scope(ctx
);
2458 ctx
-> decl
-> local
= 1;
2460 oberon_type_t
* signature
;
2461 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2462 oberon_opt_formal_pars(ctx
, &signature
);
2464 oberon_initialize_decl(ctx
);
2465 oberon_generator_init_type(ctx
, signature
);
2466 oberon_close_scope(ctx
-> decl
);
2468 oberon_object_t
* proc
;
2469 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2472 if(proc
-> class != OBERON_CLASS_PROC
)
2474 oberon_error(ctx
, "mult definition");
2481 oberon_error(ctx
, "mult procedure definition");
2485 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2487 oberon_error(ctx
, "export type not matched");
2490 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2494 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2495 proc
-> type
= signature
;
2496 proc
-> scope
= proc_scope
;
2497 oberon_generator_init_proc(ctx
, proc
);
2500 proc
-> scope
-> parent
= proc
;
2505 oberon_proc_decl_body(ctx
, proc
);
2510 oberon_const_decl(oberon_context_t
* ctx
)
2512 oberon_item_t
* value
;
2513 oberon_object_t
* constant
;
2515 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2516 oberon_assert_token(ctx
, EQUAL
);
2517 value
= oberon_const_expr(ctx
);
2518 constant
-> value
= value
;
2522 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2524 if(size
-> is_item
== 0)
2526 oberon_error(ctx
, "requires constant");
2529 if(size
-> item
.mode
!= MODE_INTEGER
)
2531 oberon_error(ctx
, "requires integer constant");
2534 oberon_type_t
* arr
;
2536 arr
-> class = OBERON_TYPE_ARRAY
;
2537 arr
-> size
= size
-> item
.integer
;
2542 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2545 oberon_object_t
* to
;
2547 to
= oberon_qualident(ctx
, &name
, 0);
2549 //name = oberon_assert_ident(ctx);
2550 //to = oberon_find_object(ctx -> decl, name, 0);
2554 if(to
-> class != OBERON_CLASS_TYPE
)
2556 oberon_error(ctx
, "not a type");
2561 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2562 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2568 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2571 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2575 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2583 oberon_type_t
* dim
;
2584 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2586 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2588 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2592 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2594 type
-> class = OBERON_TYPE_ARRAY
;
2596 type
-> base
= base
;
2600 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2602 if(ctx
-> token
== IDENT
)
2605 oberon_object_t
* list
;
2606 oberon_type_t
* type
;
2607 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2609 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2610 oberon_assert_token(ctx
, COLON
);
2612 oberon_scope_t
* current
= ctx
-> decl
;
2613 ctx
-> decl
= modscope
;
2614 oberon_type(ctx
, &type
);
2615 ctx
-> decl
= current
;
2617 oberon_object_t
* field
= list
;
2618 for(int i
= 0; i
< num
; i
++)
2620 field
-> type
= type
;
2621 field
= field
-> next
;
2624 rec
-> num_decl
+= num
;
2629 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2631 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2632 oberon_scope_t
* oldscope
= ctx
-> decl
;
2633 ctx
-> decl
= modscope
;
2635 if(ctx
-> token
== LPAREN
)
2637 oberon_assert_token(ctx
, LPAREN
);
2639 oberon_object_t
* typeobj
;
2640 typeobj
= oberon_qualident(ctx
, NULL
, true);
2642 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2644 oberon_error(ctx
, "base must be type");
2647 oberon_type_t
* base
= typeobj
-> type
;
2648 if(base
-> class == OBERON_TYPE_POINTER
)
2650 base
= base
-> base
;
2653 if(base
-> class != OBERON_TYPE_RECORD
)
2655 oberon_error(ctx
, "base must be record type");
2659 ctx
-> decl
= base
-> scope
;
2661 oberon_assert_token(ctx
, RPAREN
);
2668 oberon_scope_t
* this_scope
;
2669 this_scope
= oberon_open_scope(ctx
);
2670 this_scope
-> local
= true;
2671 this_scope
-> parent
= NULL
;
2672 this_scope
-> parent_type
= rec
;
2674 oberon_field_list(ctx
, rec
, modscope
);
2675 while(ctx
-> token
== SEMICOLON
)
2677 oberon_assert_token(ctx
, SEMICOLON
);
2678 oberon_field_list(ctx
, rec
, modscope
);
2681 rec
-> scope
= this_scope
;
2682 rec
-> decl
= this_scope
-> list
-> next
;
2683 ctx
-> decl
= oldscope
;
2687 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2689 if(ctx
-> token
== IDENT
)
2691 oberon_qualident_type(ctx
, type
);
2693 else if(ctx
-> token
== ARRAY
)
2695 oberon_assert_token(ctx
, ARRAY
);
2698 oberon_expr_t
* sizes
;
2700 if(ISEXPR(ctx
-> token
))
2702 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2705 oberon_assert_token(ctx
, OF
);
2707 oberon_type_t
* base
;
2708 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2709 oberon_type(ctx
, &base
);
2713 oberon_make_open_array(ctx
, base
, *type
);
2717 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2720 else if(ctx
-> token
== RECORD
)
2722 oberon_type_t
* rec
;
2724 rec
-> class = OBERON_TYPE_RECORD
;
2725 rec
-> module
= ctx
-> mod
;
2727 oberon_assert_token(ctx
, RECORD
);
2728 oberon_type_record_body(ctx
, rec
);
2729 oberon_assert_token(ctx
, END
);
2733 else if(ctx
-> token
== POINTER
)
2735 oberon_assert_token(ctx
, POINTER
);
2736 oberon_assert_token(ctx
, TO
);
2738 oberon_type_t
* base
;
2739 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2740 oberon_type(ctx
, &base
);
2742 oberon_type_t
* ptr
;
2744 ptr
-> class = OBERON_TYPE_POINTER
;
2747 else if(ctx
-> token
== PROCEDURE
)
2749 oberon_open_scope(ctx
);
2750 oberon_assert_token(ctx
, PROCEDURE
);
2751 oberon_opt_formal_pars(ctx
, type
);
2752 oberon_close_scope(ctx
-> decl
);
2756 oberon_error(ctx
, "invalid type declaration");
2761 oberon_type_decl(oberon_context_t
* ctx
)
2764 oberon_object_t
* newtype
;
2765 oberon_type_t
* type
;
2769 name
= oberon_assert_ident(ctx
);
2770 oberon_def(ctx
, &export
, &read_only
);
2772 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2775 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2776 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2777 assert(newtype
-> type
);
2781 if(newtype
-> class != OBERON_CLASS_TYPE
)
2783 oberon_error(ctx
, "mult definition");
2786 if(newtype
-> linked
)
2788 oberon_error(ctx
, "mult definition - already linked");
2791 newtype
-> export
= export
;
2792 newtype
-> read_only
= read_only
;
2795 oberon_assert_token(ctx
, EQUAL
);
2797 type
= newtype
-> type
;
2798 oberon_type(ctx
, &type
);
2800 if(type
-> class == OBERON_TYPE_VOID
)
2802 oberon_error(ctx
, "recursive alias declaration");
2805 newtype
-> type
= type
;
2806 newtype
-> linked
= 1;
2809 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2810 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2813 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2815 if(type
-> class != OBERON_TYPE_POINTER
2816 && type
-> class != OBERON_TYPE_ARRAY
)
2821 if(type
-> recursive
)
2823 oberon_error(ctx
, "recursive pointer declaration");
2826 if(type
-> class == OBERON_TYPE_POINTER
2827 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2829 oberon_error(ctx
, "attempt to make pointer to pointer");
2832 type
-> recursive
= 1;
2834 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2836 type
-> recursive
= 0;
2840 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2842 if(type
-> class != OBERON_TYPE_RECORD
)
2847 if(type
-> recursive
)
2849 oberon_error(ctx
, "recursive record declaration");
2852 type
-> recursive
= 1;
2854 int num_fields
= type
-> num_decl
;
2855 oberon_object_t
* field
= type
-> decl
;
2856 for(int i
= 0; i
< num_fields
; i
++)
2858 oberon_prevent_recursive_object(ctx
, field
);
2859 field
= field
-> next
;
2862 type
-> recursive
= 0;
2865 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2867 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2872 if(type
-> recursive
)
2874 oberon_error(ctx
, "recursive procedure declaration");
2877 type
-> recursive
= 1;
2879 int num_fields
= type
-> num_decl
;
2880 oberon_object_t
* field
= type
-> decl
;
2881 for(int i
= 0; i
< num_fields
; i
++)
2883 oberon_prevent_recursive_object(ctx
, field
);
2884 field
= field
-> next
;
2887 type
-> recursive
= 0;
2891 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2893 if(type
-> class != OBERON_TYPE_ARRAY
)
2898 if(type
-> recursive
)
2900 oberon_error(ctx
, "recursive array declaration");
2903 type
-> recursive
= 1;
2905 oberon_prevent_recursive_type(ctx
, type
-> base
);
2907 type
-> recursive
= 0;
2911 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2913 if(type
-> class == OBERON_TYPE_POINTER
)
2915 oberon_prevent_recursive_pointer(ctx
, type
);
2917 else if(type
-> class == OBERON_TYPE_RECORD
)
2919 oberon_prevent_recursive_record(ctx
, type
);
2921 else if(type
-> class == OBERON_TYPE_ARRAY
)
2923 oberon_prevent_recursive_array(ctx
, type
);
2925 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2927 oberon_prevent_recursive_procedure(ctx
, type
);
2932 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2936 case OBERON_CLASS_VAR
:
2937 case OBERON_CLASS_TYPE
:
2938 case OBERON_CLASS_PARAM
:
2939 case OBERON_CLASS_VAR_PARAM
:
2940 case OBERON_CLASS_FIELD
:
2941 oberon_prevent_recursive_type(ctx
, x
-> type
);
2943 case OBERON_CLASS_CONST
:
2944 case OBERON_CLASS_PROC
:
2945 case OBERON_CLASS_MODULE
:
2948 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2954 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2956 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2960 oberon_prevent_recursive_object(ctx
, x
);
2965 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2966 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2969 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2971 if(type
-> class != OBERON_TYPE_RECORD
)
2976 int num_fields
= type
-> num_decl
;
2977 oberon_object_t
* field
= type
-> decl
;
2978 for(int i
= 0; i
< num_fields
; i
++)
2980 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2982 oberon_initialize_type(ctx
, field
-> type
);
2985 oberon_initialize_object(ctx
, field
);
2986 field
= field
-> next
;
2989 oberon_generator_init_record(ctx
, type
);
2993 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2995 if(type
-> class == OBERON_TYPE_VOID
)
2997 oberon_error(ctx
, "undeclarated type");
3000 if(type
-> initialized
)
3005 type
-> initialized
= 1;
3007 if(type
-> class == OBERON_TYPE_POINTER
)
3009 oberon_initialize_type(ctx
, type
-> base
);
3010 oberon_generator_init_type(ctx
, type
);
3012 else if(type
-> class == OBERON_TYPE_ARRAY
)
3014 if(type
-> size
!= 0)
3016 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
3018 if(type
-> base
-> size
== 0)
3020 oberon_error(ctx
, "open array not allowed as array element");
3025 oberon_initialize_type(ctx
, type
-> base
);
3026 oberon_generator_init_type(ctx
, type
);
3028 else if(type
-> class == OBERON_TYPE_RECORD
)
3030 oberon_generator_init_type(ctx
, type
);
3031 oberon_initialize_record_fields(ctx
, type
);
3033 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3035 int num_fields
= type
-> num_decl
;
3036 oberon_object_t
* field
= type
-> decl
;
3037 for(int i
= 0; i
< num_fields
; i
++)
3039 oberon_initialize_object(ctx
, field
);
3040 field
= field
-> next
;
3043 oberon_generator_init_type(ctx
, type
);
3047 oberon_generator_init_type(ctx
, type
);
3052 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3054 if(x
-> initialized
)
3059 x
-> initialized
= 1;
3063 case OBERON_CLASS_TYPE
:
3064 oberon_initialize_type(ctx
, x
-> type
);
3066 case OBERON_CLASS_VAR
:
3067 case OBERON_CLASS_FIELD
:
3068 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3070 if(x
-> type
-> size
== 0)
3072 oberon_error(ctx
, "open array not allowed as variable or field");
3075 oberon_initialize_type(ctx
, x
-> type
);
3076 oberon_generator_init_var(ctx
, x
);
3078 case OBERON_CLASS_PARAM
:
3079 case OBERON_CLASS_VAR_PARAM
:
3080 oberon_initialize_type(ctx
, x
-> type
);
3081 oberon_generator_init_var(ctx
, x
);
3083 case OBERON_CLASS_CONST
:
3084 case OBERON_CLASS_PROC
:
3085 case OBERON_CLASS_MODULE
:
3088 oberon_error(ctx
, "oberon_initialize_object: wat");
3094 oberon_initialize_decl(oberon_context_t
* ctx
)
3096 oberon_object_t
* x
= ctx
-> decl
-> list
;
3100 oberon_initialize_object(ctx
, x
-> next
);
3106 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3108 oberon_object_t
* x
= ctx
-> decl
-> list
;
3112 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3114 if(x
-> next
-> linked
== 0)
3116 oberon_error(ctx
, "unresolved forward declaration");
3124 oberon_decl_seq(oberon_context_t
* ctx
)
3126 if(ctx
-> token
== CONST
)
3128 oberon_assert_token(ctx
, CONST
);
3129 while(ctx
-> token
== IDENT
)
3131 oberon_const_decl(ctx
);
3132 oberon_assert_token(ctx
, SEMICOLON
);
3136 if(ctx
-> token
== TYPE
)
3138 oberon_assert_token(ctx
, TYPE
);
3139 while(ctx
-> token
== IDENT
)
3141 oberon_type_decl(ctx
);
3142 oberon_assert_token(ctx
, SEMICOLON
);
3146 if(ctx
-> token
== VAR
)
3148 oberon_assert_token(ctx
, VAR
);
3149 while(ctx
-> token
== IDENT
)
3151 oberon_var_decl(ctx
);
3152 oberon_assert_token(ctx
, SEMICOLON
);
3156 oberon_prevent_recursive_decl(ctx
);
3157 oberon_initialize_decl(ctx
);
3159 while(ctx
-> token
== PROCEDURE
)
3161 oberon_proc_decl(ctx
);
3162 oberon_assert_token(ctx
, SEMICOLON
);
3165 oberon_prevent_undeclarated_procedures(ctx
);
3168 static oberon_expr_t
*
3169 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3171 oberon_object_t
* x
;
3172 oberon_expr_t
* expr
;
3174 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3177 oberon_generator_init_temp_var(ctx
, x
);
3179 expr
= oberon_new_item(MODE_VAR
, type
, false);
3180 expr
-> item
.var
= x
;
3185 oberon_statement_seq(oberon_context_t
* ctx
);
3188 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3190 if(dst
-> read_only
)
3192 oberon_error(ctx
, "read-only destination");
3195 oberon_check_dst(ctx
, dst
);
3196 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3197 oberon_generate_assign(ctx
, src
, dst
);
3200 static oberon_expr_t
*
3201 oberon_case_labels(oberon_context_t
* ctx
, oberon_expr_t
* val
)
3205 oberon_expr_t
* cond
;
3206 oberon_expr_t
* cond2
;
3208 e1
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3209 oberon_autocast_to(ctx
, e1
, val
-> result
);
3212 if(ctx
-> token
== DOTDOT
)
3214 oberon_assert_token(ctx
, DOTDOT
);
3215 e2
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3216 oberon_autocast_to(ctx
, e2
, val
-> result
);
3222 cond
= oberon_make_bin_op(ctx
, EQUAL
, val
, e1
);
3226 /* val >= e1 && val <= e2 */
3227 cond
= oberon_make_bin_op(ctx
, GEQ
, val
, e1
);
3228 cond2
= oberon_make_bin_op(ctx
, LEQ
, val
, e2
);
3229 cond
= oberon_make_bin_op(ctx
, AND
, cond
, cond2
);
3236 oberon_case(oberon_context_t
* ctx
, oberon_expr_t
* val
, gen_label_t
* end
)
3238 oberon_expr_t
* cond
;
3239 oberon_expr_t
* cond2
;
3240 gen_label_t
* this_end
;
3242 if(ISEXPR(ctx
-> token
))
3244 this_end
= oberon_generator_reserve_label(ctx
);
3246 cond
= oberon_case_labels(ctx
, val
);
3247 while(ctx
-> token
== COMMA
)
3249 oberon_assert_token(ctx
, COMMA
);
3251 cond2
= oberon_case_labels(ctx
, val
);
3252 cond
= oberon_make_bin_op(ctx
, OR
, cond
, cond2
);
3254 oberon_assert_token(ctx
, COLON
);
3256 oberon_generate_branch(ctx
, cond
, false, this_end
);
3257 oberon_statement_seq(ctx
);
3258 oberon_generate_goto(ctx
, end
);
3260 oberon_generate_label(ctx
, this_end
);
3265 oberon_case_statement(oberon_context_t
* ctx
)
3267 oberon_expr_t
* val
;
3268 oberon_expr_t
* expr
;
3271 end
= oberon_generator_reserve_label(ctx
);
3273 oberon_assert_token(ctx
, CASE
);
3274 expr
= oberon_expr(ctx
);
3275 val
= oberon_make_temp_var_item(ctx
, expr
-> result
);
3276 oberon_assign(ctx
, expr
, val
);
3277 oberon_assert_token(ctx
, OF
);
3278 oberon_case(ctx
, val
, end
);
3279 while(ctx
-> token
== BAR
)
3281 oberon_assert_token(ctx
, BAR
);
3282 oberon_case(ctx
, val
, end
);
3285 if(ctx
-> token
== ELSE
)
3287 oberon_assert_token(ctx
, ELSE
);
3288 oberon_statement_seq(ctx
);
3291 oberon_generate_label(ctx
, end
);
3292 oberon_assert_token(ctx
, END
);
3296 oberon_with_guard_do(oberon_context_t
* ctx
, gen_label_t
* end
)
3298 oberon_expr_t
* val
;
3299 oberon_expr_t
* var
;
3300 oberon_expr_t
* type
;
3301 oberon_expr_t
* cond
;
3302 oberon_expr_t
* cast
;
3303 oberon_type_t
* old_type
;
3304 gen_var_t
* old_var
;
3305 gen_label_t
* this_end
;
3307 this_end
= oberon_generator_reserve_label(ctx
);
3309 var
= oberon_qualident_expr(ctx
);
3310 oberon_assert_token(ctx
, COLON
);
3311 type
= oberon_qualident_expr(ctx
);
3312 cond
= oberon_make_bin_op(ctx
, IS
, var
, type
);
3314 oberon_assert_token(ctx
, DO
);
3315 oberon_generate_branch(ctx
, cond
, false, this_end
);
3317 /* Сохраняем ссылку во временной переменной */
3318 val
= oberon_make_temp_var_item(ctx
, type
-> result
);
3319 cast
= oberno_make_record_cast(ctx
, var
, type
-> result
);
3320 oberon_assign(ctx
, cast
, val
);
3321 /* Подменяем тип у оригинальной переменной */
3322 old_type
= var
-> item
.var
-> type
;
3323 var
-> item
.var
-> type
= type
-> result
;
3324 /* Подменяем ссылку на переменную */
3325 old_var
= var
-> item
.var
-> gen_var
;
3326 var
-> item
.var
-> gen_var
= val
-> item
.var
-> gen_var
;
3328 oberon_statement_seq(ctx
);
3329 oberon_generate_goto(ctx
, end
);
3330 oberon_generate_label(ctx
, this_end
);
3332 /* Возвращаем исходное состояние */
3333 var
-> item
.var
-> gen_var
= old_var
;
3334 var
-> item
.var
-> type
= old_type
;
3338 oberon_with_statement(oberon_context_t
* ctx
)
3341 end
= oberon_generator_reserve_label(ctx
);
3343 oberon_assert_token(ctx
, WITH
);
3344 oberon_with_guard_do(ctx
, end
);
3345 while(ctx
-> token
== BAR
)
3347 oberon_assert_token(ctx
, BAR
);
3348 oberon_with_guard_do(ctx
, end
);
3351 if(ctx
-> token
== ELSE
)
3353 oberon_assert_token(ctx
, ELSE
);
3354 oberon_statement_seq(ctx
);
3357 oberon_generate_label(ctx
, end
);
3358 oberon_assert_token(ctx
, END
);
3362 oberon_statement(oberon_context_t
* ctx
)
3364 oberon_expr_t
* item1
;
3365 oberon_expr_t
* item2
;
3367 if(ctx
-> token
== IDENT
)
3369 item1
= oberon_designator(ctx
);
3370 if(ctx
-> token
== ASSIGN
)
3372 oberon_assert_token(ctx
, ASSIGN
);
3373 item2
= oberon_expr(ctx
);
3374 oberon_assign(ctx
, item2
, item1
);
3378 oberon_opt_proc_parens(ctx
, item1
);
3381 else if(ctx
-> token
== IF
)
3385 oberon_expr_t
* cond
;
3387 els
= oberon_generator_reserve_label(ctx
);
3388 end
= oberon_generator_reserve_label(ctx
);
3390 oberon_assert_token(ctx
, IF
);
3391 cond
= oberon_expr(ctx
);
3392 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3394 oberon_error(ctx
, "condition must be boolean");
3396 oberon_assert_token(ctx
, THEN
);
3397 oberon_generate_branch(ctx
, cond
, false, els
);
3398 oberon_statement_seq(ctx
);
3399 oberon_generate_goto(ctx
, end
);
3400 oberon_generate_label(ctx
, els
);
3402 while(ctx
-> token
== ELSIF
)
3404 els
= oberon_generator_reserve_label(ctx
);
3406 oberon_assert_token(ctx
, ELSIF
);
3407 cond
= oberon_expr(ctx
);
3408 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3410 oberon_error(ctx
, "condition must be boolean");
3412 oberon_assert_token(ctx
, THEN
);
3413 oberon_generate_branch(ctx
, cond
, false, els
);
3414 oberon_statement_seq(ctx
);
3415 oberon_generate_goto(ctx
, end
);
3416 oberon_generate_label(ctx
, els
);
3419 if(ctx
-> token
== ELSE
)
3421 oberon_assert_token(ctx
, ELSE
);
3422 oberon_statement_seq(ctx
);
3425 oberon_generate_label(ctx
, end
);
3426 oberon_assert_token(ctx
, END
);
3428 else if(ctx
-> token
== WHILE
)
3430 gen_label_t
* begin
;
3432 oberon_expr_t
* cond
;
3434 begin
= oberon_generator_reserve_label(ctx
);
3435 end
= oberon_generator_reserve_label(ctx
);
3437 oberon_assert_token(ctx
, WHILE
);
3438 oberon_generate_label(ctx
, begin
);
3439 cond
= oberon_expr(ctx
);
3440 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3442 oberon_error(ctx
, "condition must be boolean");
3444 oberon_generate_branch(ctx
, cond
, false, end
);
3446 oberon_assert_token(ctx
, DO
);
3447 oberon_statement_seq(ctx
);
3448 oberon_generate_goto(ctx
, begin
);
3450 oberon_assert_token(ctx
, END
);
3451 oberon_generate_label(ctx
, end
);
3453 else if(ctx
-> token
== REPEAT
)
3455 gen_label_t
* begin
;
3456 oberon_expr_t
* cond
;
3458 begin
= oberon_generator_reserve_label(ctx
);
3459 oberon_generate_label(ctx
, begin
);
3460 oberon_assert_token(ctx
, REPEAT
);
3462 oberon_statement_seq(ctx
);
3464 oberon_assert_token(ctx
, UNTIL
);
3466 cond
= oberon_expr(ctx
);
3467 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3469 oberon_error(ctx
, "condition must be boolean");
3472 oberon_generate_branch(ctx
, cond
, true, begin
);
3474 else if(ctx
-> token
== FOR
)
3476 oberon_expr_t
* from
;
3477 oberon_expr_t
* index
;
3479 oberon_expr_t
* bound
;
3481 oberon_expr_t
* cond
;
3482 oberon_expr_t
* count
;
3483 gen_label_t
* begin
;
3488 begin
= oberon_generator_reserve_label(ctx
);
3489 end
= oberon_generator_reserve_label(ctx
);
3491 oberon_assert_token(ctx
, FOR
);
3492 iname
= oberon_assert_ident(ctx
);
3493 index
= oberon_ident_item(ctx
, iname
);
3494 oberon_assert_token(ctx
, ASSIGN
);
3495 from
= oberon_expr(ctx
);
3496 oberon_assign(ctx
, from
, index
);
3497 oberon_assert_token(ctx
, TO
);
3498 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3499 to
= oberon_expr(ctx
);
3500 oberon_assign(ctx
, to
, bound
);
3501 if(ctx
-> token
== BY
)
3503 oberon_assert_token(ctx
, BY
);
3504 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3508 by
= oberon_integer_item(ctx
, 1);
3511 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3513 oberon_error(ctx
, "must be integer");
3516 if(by
-> item
.integer
> 0)
3520 else if(by
-> item
.integer
< 0)
3526 oberon_error(ctx
, "zero step not allowed");
3529 oberon_assert_token(ctx
, DO
);
3530 oberon_generate_label(ctx
, begin
);
3531 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3532 oberon_generate_branch(ctx
, cond
, false, end
);
3533 oberon_statement_seq(ctx
);
3534 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3535 oberon_assign(ctx
, count
, index
);
3536 oberon_generate_goto(ctx
, begin
);
3537 oberon_generate_label(ctx
, end
);
3538 oberon_assert_token(ctx
, END
);
3540 else if(ctx
-> token
== LOOP
)
3542 gen_label_t
* begin
;
3545 begin
= oberon_generator_reserve_label(ctx
);
3546 end
= oberon_generator_reserve_label(ctx
);
3548 oberon_open_scope(ctx
);
3549 oberon_assert_token(ctx
, LOOP
);
3550 oberon_generate_label(ctx
, begin
);
3551 ctx
-> decl
-> exit_label
= end
;
3552 oberon_statement_seq(ctx
);
3553 oberon_generate_goto(ctx
, begin
);
3554 oberon_generate_label(ctx
, end
);
3555 oberon_assert_token(ctx
, END
);
3556 oberon_close_scope(ctx
-> decl
);
3558 else if(ctx
-> token
== EXIT
)
3560 oberon_assert_token(ctx
, EXIT
);
3561 if(ctx
-> decl
-> exit_label
== NULL
)
3563 oberon_error(ctx
, "not in LOOP-END");
3565 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3567 else if(ctx
-> token
== CASE
)
3569 oberon_case_statement(ctx
);
3571 else if(ctx
-> token
== WITH
)
3573 oberon_with_statement(ctx
);
3575 else if(ctx
-> token
== RETURN
)
3577 oberon_assert_token(ctx
, RETURN
);
3578 if(ISEXPR(ctx
-> token
))
3580 oberon_expr_t
* expr
;
3581 expr
= oberon_expr(ctx
);
3582 oberon_make_return(ctx
, expr
);
3586 oberon_make_return(ctx
, NULL
);
3592 oberon_statement_seq(oberon_context_t
* ctx
)
3594 oberon_statement(ctx
);
3595 while(ctx
-> token
== SEMICOLON
)
3597 oberon_assert_token(ctx
, SEMICOLON
);
3598 oberon_statement(ctx
);
3603 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3605 oberon_module_t
* m
= ctx
-> module_list
;
3606 while(m
&& strcmp(m
-> name
, name
) != 0)
3614 code
= ctx
-> import_module(name
);
3617 oberon_error(ctx
, "no such module");
3620 m
= oberon_compile_module(ctx
, code
);
3626 oberon_error(ctx
, "cyclic module import");
3629 oberon_object_t
* ident
;
3630 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3631 ident
-> module
= m
;
3635 oberon_import_decl(oberon_context_t
* ctx
)
3640 alias
= name
= oberon_assert_ident(ctx
);
3641 if(ctx
-> token
== ASSIGN
)
3643 oberon_assert_token(ctx
, ASSIGN
);
3644 name
= oberon_assert_ident(ctx
);
3647 oberon_import_module(ctx
, alias
, name
);
3651 oberon_import_list(oberon_context_t
* ctx
)
3653 oberon_assert_token(ctx
, IMPORT
);
3655 oberon_import_decl(ctx
);
3656 while(ctx
-> token
== COMMA
)
3658 oberon_assert_token(ctx
, COMMA
);
3659 oberon_import_decl(ctx
);
3662 oberon_assert_token(ctx
, SEMICOLON
);
3666 oberon_parse_module(oberon_context_t
* ctx
)
3670 oberon_read_token(ctx
);
3672 oberon_assert_token(ctx
, MODULE
);
3673 name1
= oberon_assert_ident(ctx
);
3674 oberon_assert_token(ctx
, SEMICOLON
);
3675 ctx
-> mod
-> name
= name1
;
3677 oberon_generator_init_module(ctx
, ctx
-> mod
);
3679 if(ctx
-> token
== IMPORT
)
3681 oberon_import_list(ctx
);
3684 oberon_decl_seq(ctx
);
3686 oberon_generate_begin_module(ctx
);
3687 if(ctx
-> token
== BEGIN
)
3689 oberon_assert_token(ctx
, BEGIN
);
3690 oberon_statement_seq(ctx
);
3692 oberon_generate_end_module(ctx
);
3694 oberon_assert_token(ctx
, END
);
3695 name2
= oberon_assert_ident(ctx
);
3696 oberon_assert_token(ctx
, DOT
);
3698 if(strcmp(name1
, name2
) != 0)
3700 oberon_error(ctx
, "module name not matched");
3703 oberon_generator_fini_module(ctx
-> mod
);
3706 // =======================================================================
3708 // =======================================================================
3711 register_default_types(oberon_context_t
* ctx
)
3713 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3714 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3716 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3717 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3718 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3720 ctx
-> string_type
= oberon_new_type_string(1);
3721 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3723 ctx
-> bool_type
= oberon_new_type_boolean();
3724 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3726 ctx
-> byte_type
= oberon_new_type_integer(1);
3727 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3729 ctx
-> shortint_type
= oberon_new_type_integer(2);
3730 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3732 ctx
-> int_type
= oberon_new_type_integer(4);
3733 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3735 ctx
-> longint_type
= oberon_new_type_integer(8);
3736 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3738 ctx
-> real_type
= oberon_new_type_real(4);
3739 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3741 ctx
-> longreal_type
= oberon_new_type_real(8);
3742 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3744 ctx
-> char_type
= oberon_new_type_char(1);
3745 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3747 ctx
-> set_type
= oberon_new_type_set(4);
3748 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3752 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3754 oberon_object_t
* proc
;
3755 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3756 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3757 proc
-> type
-> sysproc
= true;
3758 proc
-> type
-> genfunc
= f
;
3759 proc
-> type
-> genproc
= p
;
3762 static oberon_expr_t
*
3763 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3767 oberon_error(ctx
, "too few arguments");
3772 oberon_error(ctx
, "too mach arguments");
3775 oberon_expr_t
* arg
;
3778 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3780 oberon_error(ctx
, "MIN accept only type");
3783 oberon_expr_t
* expr
;
3784 int bits
= arg
-> result
-> size
* 8;
3785 switch(arg
-> result
-> class)
3787 case OBERON_TYPE_INTEGER
:
3788 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3790 case OBERON_TYPE_SET
:
3791 expr
= oberon_integer_item(ctx
, 0);
3794 oberon_error(ctx
, "allowed only basic types");
3801 static oberon_expr_t
*
3802 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3806 oberon_error(ctx
, "too few arguments");
3811 oberon_error(ctx
, "too mach arguments");
3814 oberon_expr_t
* arg
;
3817 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3819 oberon_error(ctx
, "MAX accept only type");
3822 oberon_expr_t
* expr
;
3823 int bits
= arg
-> result
-> size
* 8;
3824 switch(arg
-> result
-> class)
3826 case OBERON_TYPE_INTEGER
:
3827 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3829 case OBERON_TYPE_SET
:
3830 expr
= oberon_integer_item(ctx
, bits
);
3833 oberon_error(ctx
, "allowed only basic types");
3840 static oberon_expr_t
*
3841 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3845 oberon_error(ctx
, "too few arguments");
3850 oberon_error(ctx
, "too mach arguments");
3853 oberon_expr_t
* arg
;
3856 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3858 oberon_error(ctx
, "SIZE accept only type");
3862 oberon_expr_t
* expr
;
3863 oberon_type_t
* type
= arg
-> result
;
3864 switch(type
-> class)
3866 case OBERON_TYPE_INTEGER
:
3867 case OBERON_TYPE_BOOLEAN
:
3868 case OBERON_TYPE_REAL
:
3869 case OBERON_TYPE_CHAR
:
3870 case OBERON_TYPE_SET
:
3871 size
= type
-> size
;
3874 oberon_error(ctx
, "TODO SIZE");
3878 expr
= oberon_integer_item(ctx
, size
);
3882 static oberon_expr_t
*
3883 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3887 oberon_error(ctx
, "too few arguments");
3892 oberon_error(ctx
, "too mach arguments");
3895 oberon_expr_t
* arg
;
3897 oberon_check_src(ctx
, arg
);
3899 oberon_type_t
* result_type
;
3900 result_type
= arg
-> result
;
3902 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3904 oberon_error(ctx
, "ABS accepts only integers");
3907 oberon_expr_t
* expr
;
3908 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3913 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3917 oberon_error(ctx
, "too few arguments");
3921 oberon_expr_t
* dst
;
3923 oberon_check_dst(ctx
, dst
);
3925 oberon_type_t
* type
;
3926 type
= dst
-> result
;
3928 if(type
-> class != OBERON_TYPE_POINTER
)
3930 oberon_error(ctx
, "not a pointer");
3933 type
= type
-> base
;
3935 oberon_expr_t
* src
;
3936 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3937 src
-> item
.num_args
= 0;
3938 src
-> item
.args
= NULL
;
3941 if(type
-> class == OBERON_TYPE_ARRAY
)
3943 if(type
-> size
== 0)
3945 oberon_type_t
* x
= type
;
3946 while(x
-> class == OBERON_TYPE_ARRAY
)
3956 if(num_args
< max_args
)
3958 oberon_error(ctx
, "too few arguments");
3961 if(num_args
> max_args
)
3963 oberon_error(ctx
, "too mach arguments");
3966 int num_sizes
= max_args
- 1;
3967 oberon_expr_t
* size_list
= list_args
-> next
;
3969 oberon_expr_t
* arg
= size_list
;
3970 for(int i
= 0; i
< max_args
- 1; i
++)
3972 oberon_check_src(ctx
, arg
);
3973 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3975 oberon_error(ctx
, "size must be integer");
3980 src
-> item
.num_args
= num_sizes
;
3981 src
-> item
.args
= size_list
;
3983 else if(type
-> class != OBERON_TYPE_RECORD
)
3985 oberon_error(ctx
, "oberon_make_new_call: wat");
3988 if(num_args
> max_args
)
3990 oberon_error(ctx
, "too mach arguments");
3993 oberon_assign(ctx
, src
, dst
);
3997 oberon_create_context(ModuleImportCallback import_module
)
3999 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
4001 oberon_scope_t
* world_scope
;
4002 world_scope
= oberon_open_scope(ctx
);
4003 ctx
-> world_scope
= world_scope
;
4005 ctx
-> import_module
= import_module
;
4007 oberon_generator_init_context(ctx
);
4009 register_default_types(ctx
);
4012 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
4013 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
4014 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
4015 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
4018 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
4024 oberon_destroy_context(oberon_context_t
* ctx
)
4026 oberon_generator_destroy_context(ctx
);
4031 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
4033 const char * code
= ctx
-> code
;
4034 int code_index
= ctx
-> code_index
;
4036 int token
= ctx
-> token
;
4037 char * string
= ctx
-> string
;
4038 int integer
= ctx
-> integer
;
4039 int real
= ctx
-> real
;
4040 bool longmode
= ctx
-> longmode
;
4041 oberon_scope_t
* decl
= ctx
-> decl
;
4042 oberon_module_t
* mod
= ctx
-> mod
;
4044 oberon_scope_t
* module_scope
;
4045 module_scope
= oberon_open_scope(ctx
);
4047 oberon_module_t
* module
;
4048 module
= calloc(1, sizeof *module
);
4049 module
-> decl
= module_scope
;
4050 module
-> next
= ctx
-> module_list
;
4052 ctx
-> mod
= module
;
4053 ctx
-> module_list
= module
;
4055 oberon_init_scaner(ctx
, newcode
);
4056 oberon_parse_module(ctx
);
4058 module
-> ready
= 1;
4061 ctx
-> code_index
= code_index
;
4063 ctx
-> token
= token
;
4064 ctx
-> string
= string
;
4065 ctx
-> integer
= integer
;
4067 ctx
-> longmode
= longmode
;