10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
83 // =======================================================================
85 // =======================================================================
88 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
92 fprintf(stderr
, "error: ");
93 vfprintf(stderr
, fmt
, ptr
);
94 fprintf(stderr
, "\n");
95 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
96 fprintf(stderr
, " c = %c\n", ctx
-> c
);
97 fprintf(stderr
, " token = %i\n", ctx
-> token
);
102 static oberon_type_t
*
103 oberon_new_type_ptr(int class)
105 oberon_type_t
* x
= malloc(sizeof *x
);
106 memset(x
, 0, sizeof *x
);
111 static oberon_type_t
*
112 oberon_new_type_integer(int size
)
115 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
120 static oberon_type_t
*
121 oberon_new_type_boolean()
124 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
128 static oberon_type_t
*
129 oberon_new_type_real(int size
)
132 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
137 static oberon_type_t
*
138 oberon_new_type_char(int size
)
141 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
146 static oberon_type_t
*
147 oberon_new_type_string(int size
)
150 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
155 static oberon_type_t
*
156 oberon_new_type_set(int size
)
159 x
= oberon_new_type_ptr(OBERON_TYPE_SET
);
164 // =======================================================================
166 // =======================================================================
168 static oberon_scope_t
*
169 oberon_open_scope(oberon_context_t
* ctx
)
171 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
172 oberon_object_t
* list
= calloc(1, sizeof *list
);
175 scope
-> list
= list
;
176 scope
-> up
= ctx
-> decl
;
180 scope
-> local
= scope
-> up
-> local
;
181 scope
-> parent
= scope
-> up
-> parent
;
182 scope
-> parent_type
= scope
-> up
-> parent_type
;
183 scope
-> exit_label
= scope
-> up
-> exit_label
;
191 oberon_close_scope(oberon_scope_t
* scope
)
193 oberon_context_t
* ctx
= scope
-> ctx
;
194 ctx
-> decl
= scope
-> up
;
197 static oberon_object_t
*
198 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
200 oberon_object_t
* x
= list
;
201 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
208 static oberon_object_t
*
209 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
211 oberon_object_t
* result
= NULL
;
213 oberon_scope_t
* s
= scope
;
214 while(result
== NULL
&& s
!= NULL
)
216 result
= oberon_find_object_in_list(s
-> list
, name
);
220 if(check_it
&& result
== NULL
)
222 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
228 static oberon_object_t
*
229 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
231 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
232 memset(newvar
, 0, sizeof *newvar
);
233 newvar
-> name
= name
;
234 newvar
-> class = class;
235 newvar
-> export
= export
;
236 newvar
-> read_only
= read_only
;
237 newvar
-> local
= scope
-> local
;
238 newvar
-> parent
= scope
-> parent
;
239 newvar
-> parent_type
= scope
-> parent_type
;
240 newvar
-> module
= scope
-> ctx
-> mod
;
244 static oberon_object_t
*
245 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
249 if(oberon_find_object(scope
-> up
, name
, false))
251 oberon_error(scope
-> ctx
, "already defined");
255 oberon_object_t
* x
= scope
-> list
;
256 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
263 oberon_error(scope
-> ctx
, "already defined");
266 oberon_object_t
* newvar
;
267 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
273 static oberon_object_t
*
274 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
276 oberon_object_t
* id
;
277 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
279 oberon_generator_init_type(scope
-> ctx
, type
);
283 // =======================================================================
285 // =======================================================================
288 oberon_get_char(oberon_context_t
* ctx
)
290 if(ctx
-> code
[ctx
-> code_index
])
292 ctx
-> code_index
+= 1;
293 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
298 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
301 ctx
-> code_index
= 0;
302 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
306 oberon_read_ident(oberon_context_t
* ctx
)
309 int i
= ctx
-> code_index
;
311 int c
= ctx
-> code
[i
];
319 char * ident
= malloc(len
+ 1);
320 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
323 ctx
-> code_index
= i
;
324 ctx
-> c
= ctx
-> code
[i
];
325 ctx
-> string
= ident
;
326 ctx
-> token
= IDENT
;
328 if(strcmp(ident
, "MODULE") == 0)
330 ctx
-> token
= MODULE
;
332 else if(strcmp(ident
, "END") == 0)
336 else if(strcmp(ident
, "VAR") == 0)
340 else if(strcmp(ident
, "BEGIN") == 0)
342 ctx
-> token
= BEGIN
;
344 else if(strcmp(ident
, "TRUE") == 0)
348 else if(strcmp(ident
, "FALSE") == 0)
350 ctx
-> token
= FALSE
;
352 else if(strcmp(ident
, "OR") == 0)
356 else if(strcmp(ident
, "DIV") == 0)
360 else if(strcmp(ident
, "MOD") == 0)
364 else if(strcmp(ident
, "PROCEDURE") == 0)
366 ctx
-> token
= PROCEDURE
;
368 else if(strcmp(ident
, "RETURN") == 0)
370 ctx
-> token
= RETURN
;
372 else if(strcmp(ident
, "CONST") == 0)
374 ctx
-> token
= CONST
;
376 else if(strcmp(ident
, "TYPE") == 0)
380 else if(strcmp(ident
, "ARRAY") == 0)
382 ctx
-> token
= ARRAY
;
384 else if(strcmp(ident
, "OF") == 0)
388 else if(strcmp(ident
, "RECORD") == 0)
390 ctx
-> token
= RECORD
;
392 else if(strcmp(ident
, "POINTER") == 0)
394 ctx
-> token
= POINTER
;
396 else if(strcmp(ident
, "TO") == 0)
400 else if(strcmp(ident
, "NIL") == 0)
404 else if(strcmp(ident
, "IMPORT") == 0)
406 ctx
-> token
= IMPORT
;
408 else if(strcmp(ident
, "IN") == 0)
412 else if(strcmp(ident
, "IS") == 0)
416 else if(strcmp(ident
, "IF") == 0)
420 else if(strcmp(ident
, "THEN") == 0)
424 else if(strcmp(ident
, "ELSE") == 0)
428 else if(strcmp(ident
, "ELSIF") == 0)
430 ctx
-> token
= ELSIF
;
432 else if(strcmp(ident
, "WHILE") == 0)
434 ctx
-> token
= WHILE
;
436 else if(strcmp(ident
, "DO") == 0)
440 else if(strcmp(ident
, "REPEAT") == 0)
442 ctx
-> token
= REPEAT
;
444 else if(strcmp(ident
, "UNTIL") == 0)
446 ctx
-> token
= UNTIL
;
448 else if(strcmp(ident
, "FOR") == 0)
452 else if(strcmp(ident
, "BY") == 0)
456 else if(strcmp(ident
, "LOOP") == 0)
460 else if(strcmp(ident
, "EXIT") == 0)
467 oberon_read_number(oberon_context_t
* ctx
)
480 * mode = 3 == LONGREAL
484 start_i
= ctx
-> code_index
;
486 while(isdigit(ctx
-> c
))
488 oberon_get_char(ctx
);
491 end_i
= ctx
-> code_index
;
493 if(isxdigit(ctx
-> c
))
496 while(isxdigit(ctx
-> c
))
498 oberon_get_char(ctx
);
501 end_i
= ctx
-> code_index
;
506 oberon_get_char(ctx
);
508 else if(ctx
-> c
== 'X')
511 oberon_get_char(ctx
);
515 oberon_error(ctx
, "invalid hex number");
518 else if(ctx
-> c
== '.')
520 oberon_get_char(ctx
);
523 /* Чит: избегаем конфликта с DOTDOT */
524 ctx
-> code_index
-= 1;
530 while(isdigit(ctx
-> c
))
532 oberon_get_char(ctx
);
535 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
537 exp_i
= ctx
-> code_index
;
544 oberon_get_char(ctx
);
546 if(ctx
-> c
== '+' || ctx
-> c
== '-')
548 oberon_get_char(ctx
);
551 while(isdigit(ctx
-> c
))
553 oberon_get_char(ctx
);
557 end_i
= ctx
-> code_index
;
565 oberon_get_char(ctx
);
567 else if(ctx
-> c
== 'X')
570 oberon_get_char(ctx
);
574 int len
= end_i
- start_i
;
575 ident
= malloc(len
+ 1);
576 memcpy(ident
, &ctx
-> code
[start_i
], len
);
579 ctx
-> longmode
= false;
582 int i
= exp_i
- start_i
;
584 ctx
-> longmode
= true;
590 integer
= atol(ident
);
592 ctx
-> token
= INTEGER
;
595 sscanf(ident
, "%lx", &integer
);
597 ctx
-> token
= INTEGER
;
601 sscanf(ident
, "%lf", &real
);
605 sscanf(ident
, "%lx", &integer
);
610 oberon_error(ctx
, "oberon_read_number: wat");
614 ctx
-> string
= ident
;
615 ctx
-> integer
= integer
;
620 oberon_skip_space(oberon_context_t
* ctx
)
622 while(isspace(ctx
-> c
))
624 oberon_get_char(ctx
);
629 oberon_read_comment(oberon_context_t
* ctx
)
636 oberon_get_char(ctx
);
639 oberon_get_char(ctx
);
643 else if(ctx
-> c
== '*')
645 oberon_get_char(ctx
);
648 oberon_get_char(ctx
);
652 else if(ctx
-> c
== 0)
654 oberon_error(ctx
, "unterminated comment");
658 oberon_get_char(ctx
);
663 static void oberon_read_string(oberon_context_t
* ctx
)
666 oberon_get_char(ctx
);
668 int start
= ctx
-> code_index
;
670 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
672 oberon_get_char(ctx
);
677 oberon_error(ctx
, "unterminated string");
680 int end
= ctx
-> code_index
;
682 oberon_get_char(ctx
);
684 char * string
= calloc(1, end
- start
+ 1);
685 strncpy(string
, &ctx
-> code
[start
], end
- start
);
687 ctx
-> token
= STRING
;
688 ctx
-> string
= string
;
690 printf("oberon_read_string: string ((%s))\n", string
);
693 static void oberon_read_token(oberon_context_t
* ctx
);
696 oberon_read_symbol(oberon_context_t
* ctx
)
705 ctx
-> token
= SEMICOLON
;
706 oberon_get_char(ctx
);
709 ctx
-> token
= COLON
;
710 oberon_get_char(ctx
);
713 ctx
-> token
= ASSIGN
;
714 oberon_get_char(ctx
);
719 oberon_get_char(ctx
);
722 ctx
-> token
= DOTDOT
;
723 oberon_get_char(ctx
);
727 ctx
-> token
= LPAREN
;
728 oberon_get_char(ctx
);
731 oberon_get_char(ctx
);
732 oberon_read_comment(ctx
);
733 oberon_read_token(ctx
);
737 ctx
-> token
= RPAREN
;
738 oberon_get_char(ctx
);
741 ctx
-> token
= EQUAL
;
742 oberon_get_char(ctx
);
746 oberon_get_char(ctx
);
750 oberon_get_char(ctx
);
754 oberon_get_char(ctx
);
758 ctx
-> token
= GREAT
;
759 oberon_get_char(ctx
);
763 oberon_get_char(ctx
);
768 oberon_get_char(ctx
);
771 ctx
-> token
= MINUS
;
772 oberon_get_char(ctx
);
776 oberon_get_char(ctx
);
779 oberon_get_char(ctx
);
780 oberon_error(ctx
, "unstarted comment");
784 ctx
-> token
= SLASH
;
785 oberon_get_char(ctx
);
789 oberon_get_char(ctx
);
793 oberon_get_char(ctx
);
796 ctx
-> token
= COMMA
;
797 oberon_get_char(ctx
);
800 ctx
-> token
= LBRACK
;
801 oberon_get_char(ctx
);
804 ctx
-> token
= RBRACK
;
805 oberon_get_char(ctx
);
808 ctx
-> token
= UPARROW
;
809 oberon_get_char(ctx
);
812 oberon_read_string(ctx
);
815 oberon_read_string(ctx
);
818 ctx
-> token
= LBRACE
;
819 oberon_get_char(ctx
);
822 ctx
-> token
= RBRACE
;
823 oberon_get_char(ctx
);
826 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
832 oberon_read_token(oberon_context_t
* ctx
)
834 oberon_skip_space(ctx
);
839 oberon_read_ident(ctx
);
843 oberon_read_number(ctx
);
847 oberon_read_symbol(ctx
);
851 // =======================================================================
853 // =======================================================================
855 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
856 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
857 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
858 static char * oberon_assert_ident(oberon_context_t
* ctx
);
859 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
860 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
861 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
863 static oberon_expr_t
*
864 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
866 oberon_oper_t
* operator;
867 operator = malloc(sizeof *operator);
868 memset(operator, 0, sizeof *operator);
870 operator -> is_item
= 0;
871 operator -> result
= result
;
872 operator -> read_only
= 1;
874 operator -> left
= left
;
875 operator -> right
= right
;
877 return (oberon_expr_t
*) operator;
880 static oberon_expr_t
*
881 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
883 oberon_item_t
* item
;
884 item
= malloc(sizeof *item
);
885 memset(item
, 0, sizeof *item
);
888 item
-> result
= result
;
889 item
-> read_only
= read_only
;
892 return (oberon_expr_t
*)item
;
895 static oberon_expr_t
*
896 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
898 oberon_expr_t
* expr
;
899 oberon_type_t
* result
;
901 result
= a
-> result
;
905 if(result
-> class == OBERON_TYPE_SET
)
907 expr
= oberon_new_operator(OP_COMPLEMENTATION
, result
, a
, NULL
);
909 else if(result
-> class == OBERON_TYPE_INTEGER
)
911 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
915 oberon_error(ctx
, "incompatible operator type");
918 else if(token
== NOT
)
920 if(result
-> class != OBERON_TYPE_BOOLEAN
)
922 oberon_error(ctx
, "incompatible operator type");
925 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
929 oberon_error(ctx
, "oberon_make_unary_op: wat");
936 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
938 oberon_expr_t
* last
;
943 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
947 *first
= last
= oberon_expr(ctx
);
949 while(ctx
-> token
== COMMA
)
951 oberon_assert_token(ctx
, COMMA
);
952 oberon_expr_t
* current
;
956 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
960 current
= oberon_expr(ctx
);
963 last
-> next
= current
;
969 static oberon_expr_t
*
970 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
972 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
975 static oberon_expr_t
*
976 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
978 oberon_type_t
* from
= expr
-> result
;
979 oberon_type_t
* to
= rec
;
981 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
983 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
985 printf("oberno_make_record_cast: pointers\n");
990 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
992 oberon_error(ctx
, "must be record type");
995 return oberon_cast_expr(ctx
, expr
, rec
);
998 static oberon_type_t
*
999 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1001 oberon_type_t
* result
;
1002 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
1006 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
1010 else if(a
-> class != b
-> class)
1012 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
1014 else if(a
-> size
> b
-> size
)
1027 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
1029 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1031 from
= from
-> base
;
1035 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1037 oberon_error(ctx
, "not a record");
1040 oberon_type_t
* t
= from
;
1041 while(t
!= NULL
&& t
!= to
)
1048 oberon_error(ctx
, "incompatible record types");
1053 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1055 if(dst
-> is_item
== false)
1057 oberon_error(ctx
, "not variable");
1060 switch(dst
-> item
.mode
)
1071 oberon_error(ctx
, "not variable");
1077 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1081 if(src
-> item
.mode
== MODE_TYPE
)
1083 oberon_error(ctx
, "not variable");
1088 static oberon_expr_t
*
1089 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1092 // Если классы типов равны
1093 // Если INTEGER переводится в REAL
1094 // Есди STRING переводится в ARRAY OF CHAR
1096 oberon_check_src(ctx
, expr
);
1099 if(pref
-> class != expr
-> result
-> class)
1101 printf("expr class %i\n", expr
-> result
-> class);
1102 printf("pref class %i\n", pref
-> class);
1104 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1106 if(pref
-> class == OBERON_TYPE_ARRAY
)
1108 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1118 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1120 if(pref
-> class != OBERON_TYPE_REAL
)
1133 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1136 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1138 if(expr
-> result
-> size
> pref
-> size
)
1140 oberon_error(ctx
, "incompatible size");
1144 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1147 else if(pref
-> class == OBERON_TYPE_RECORD
)
1149 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1150 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1152 else if(pref
-> class == OBERON_TYPE_POINTER
)
1154 assert(pref
-> base
);
1155 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1157 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1158 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1160 else if(expr
-> result
-> base
!= pref
-> base
)
1162 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1164 oberon_error(ctx
, "incompatible pointer types");
1173 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1175 oberon_type_t
* a
= (*ea
) -> result
;
1176 oberon_type_t
* b
= (*eb
) -> result
;
1177 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1178 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1179 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1183 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1185 if(desig
-> mode
!= MODE_CALL
)
1187 oberon_error(ctx
, "expected mode CALL");
1190 oberon_type_t
* fn
= desig
-> parent
-> result
;
1191 int num_args
= desig
-> num_args
;
1192 int num_decl
= fn
-> num_decl
;
1194 if(num_args
< num_decl
)
1196 oberon_error(ctx
, "too few arguments");
1198 else if(num_args
> num_decl
)
1200 oberon_error(ctx
, "too many arguments");
1203 /* Делаем проверку на запись и делаем автокаст */
1204 oberon_expr_t
* casted
[num_args
];
1205 oberon_expr_t
* arg
= desig
-> args
;
1206 oberon_object_t
* param
= fn
-> decl
;
1207 for(int i
= 0; i
< num_args
; i
++)
1209 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1211 if(arg
-> read_only
)
1213 oberon_error(ctx
, "assign to read-only var");
1217 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1219 param
= param
-> next
;
1222 /* Создаём новый список выражений */
1226 for(int i
= 0; i
< num_args
- 1; i
++)
1228 casted
[i
] -> next
= casted
[i
+ 1];
1230 desig
-> args
= arg
;
1234 static oberon_expr_t
*
1235 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1237 oberon_type_t
* signature
= item
-> result
;
1238 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1240 oberon_error(ctx
, "not a procedure");
1243 oberon_expr_t
* call
;
1245 if(signature
-> sysproc
)
1247 if(signature
-> genfunc
== NULL
)
1249 oberon_error(ctx
, "not a function-procedure");
1252 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1256 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1258 oberon_error(ctx
, "attempt to call procedure in expression");
1261 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1262 call
-> item
.parent
= item
;
1263 call
-> item
.num_args
= num_args
;
1264 call
-> item
.args
= list_args
;
1265 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1272 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1274 oberon_type_t
* signature
= item
-> result
;
1275 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1277 oberon_error(ctx
, "not a procedure");
1280 oberon_expr_t
* call
;
1282 if(signature
-> sysproc
)
1284 if(signature
-> genproc
== NULL
)
1286 oberon_error(ctx
, "not a procedure");
1289 signature
-> genproc(ctx
, num_args
, list_args
);
1293 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1295 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1298 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1299 call
-> item
.parent
= item
;
1300 call
-> item
.num_args
= num_args
;
1301 call
-> item
.args
= list_args
;
1302 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1303 oberon_generate_call_proc(ctx
, call
);
1311 || ((x) == INTEGER) \
1314 || ((x) == STRING) \
1316 || ((x) == LPAREN) \
1321 static oberon_expr_t
*
1322 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1324 printf("oberno_make_dereferencing\n");
1325 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1327 oberon_error(ctx
, "not a pointer");
1330 assert(expr
-> is_item
);
1332 oberon_expr_t
* selector
;
1333 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1334 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1339 static oberon_expr_t
*
1340 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1342 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1344 desig
= oberno_make_dereferencing(ctx
, desig
);
1347 assert(desig
-> is_item
);
1349 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1351 oberon_error(ctx
, "not array");
1354 oberon_type_t
* base
;
1355 base
= desig
-> result
-> base
;
1357 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1359 oberon_error(ctx
, "index must be integer");
1362 // Статическая проверка границ массива
1363 if(desig
-> result
-> size
!= 0)
1365 if(index
-> is_item
)
1367 if(index
-> item
.mode
== MODE_INTEGER
)
1369 int arr_size
= desig
-> result
-> size
;
1370 int index_int
= index
-> item
.integer
;
1371 if(index_int
< 0 || index_int
> arr_size
- 1)
1373 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1379 oberon_expr_t
* selector
;
1380 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1381 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1382 selector
-> item
.num_args
= 1;
1383 selector
-> item
.args
= index
;
1388 static oberon_expr_t
*
1389 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1391 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1393 expr
= oberno_make_dereferencing(ctx
, expr
);
1396 assert(expr
-> is_item
);
1398 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1400 oberon_error(ctx
, "not record");
1403 oberon_type_t
* rec
= expr
-> result
;
1405 oberon_object_t
* field
;
1406 field
= oberon_find_object(rec
-> scope
, name
, true);
1408 if(field
-> export
== 0)
1410 if(field
-> module
!= ctx
-> mod
)
1412 oberon_error(ctx
, "field not exported");
1417 if(field
-> read_only
)
1419 if(field
-> module
!= ctx
-> mod
)
1425 oberon_expr_t
* selector
;
1426 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1427 selector
-> item
.var
= field
;
1428 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1433 #define ISSELECTOR(x) \
1436 || ((x) == UPARROW) \
1439 static oberon_object_t
*
1440 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1443 oberon_object_t
* x
;
1445 name
= oberon_assert_ident(ctx
);
1446 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1450 if(x
-> class == OBERON_CLASS_MODULE
)
1452 oberon_assert_token(ctx
, DOT
);
1453 name
= oberon_assert_ident(ctx
);
1454 /* Наличие объектов в левых модулях всегда проверяется */
1455 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1457 if(x
-> export
== 0)
1459 oberon_error(ctx
, "not exported");
1472 static oberon_expr_t
*
1473 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1476 oberon_object_t
* x
;
1477 oberon_expr_t
* expr
;
1479 x
= oberon_find_object(ctx
-> decl
, name
, true);
1482 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1487 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1488 expr
-> item
.var
= x
;
1492 static oberon_expr_t
*
1493 oberon_designator(oberon_context_t
* ctx
)
1496 oberon_object_t
* var
;
1497 oberon_expr_t
* expr
;
1499 var
= oberon_qualident(ctx
, NULL
, 1);
1502 if(var
-> read_only
)
1504 if(var
-> module
!= ctx
-> mod
)
1510 switch(var
-> class)
1512 case OBERON_CLASS_CONST
:
1514 expr
= (oberon_expr_t
*) var
-> value
;
1516 case OBERON_CLASS_TYPE
:
1517 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1519 case OBERON_CLASS_VAR
:
1520 case OBERON_CLASS_VAR_PARAM
:
1521 case OBERON_CLASS_PARAM
:
1522 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1524 case OBERON_CLASS_PROC
:
1525 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1528 oberon_error(ctx
, "invalid designator");
1531 expr
-> item
.var
= var
;
1533 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1535 switch(ctx
-> token
)
1538 oberon_assert_token(ctx
, DOT
);
1539 name
= oberon_assert_ident(ctx
);
1540 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1543 oberon_assert_token(ctx
, LBRACK
);
1544 int num_indexes
= 0;
1545 oberon_expr_t
* indexes
= NULL
;
1546 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1547 oberon_assert_token(ctx
, RBRACK
);
1549 for(int i
= 0; i
< num_indexes
; i
++)
1551 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1552 indexes
= indexes
-> next
;
1556 oberon_assert_token(ctx
, UPARROW
);
1557 expr
= oberno_make_dereferencing(ctx
, expr
);
1560 oberon_assert_token(ctx
, LPAREN
);
1561 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1562 if(objtype
-> class != OBERON_CLASS_TYPE
)
1564 oberon_error(ctx
, "must be type");
1566 oberon_assert_token(ctx
, RPAREN
);
1567 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1570 oberon_error(ctx
, "oberon_designator: wat");
1578 static oberon_expr_t
*
1579 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1581 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1582 if(ctx
-> token
== LPAREN
)
1584 oberon_assert_token(ctx
, LPAREN
);
1587 oberon_expr_t
* arguments
= NULL
;
1589 if(ISEXPR(ctx
-> token
))
1591 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1594 assert(expr
-> is_item
== 1);
1595 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1597 oberon_assert_token(ctx
, RPAREN
);
1604 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1606 assert(expr
-> is_item
);
1609 oberon_expr_t
* arguments
= NULL
;
1611 if(ctx
-> token
== LPAREN
)
1613 oberon_assert_token(ctx
, LPAREN
);
1615 if(ISEXPR(ctx
-> token
))
1617 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1620 oberon_assert_token(ctx
, RPAREN
);
1623 /* Вызов происходит даже без скобок */
1624 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1627 static oberon_type_t
*
1628 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1630 if(i
>= -128 && i
<= 127)
1632 return ctx
-> byte_type
;
1634 else if(i
>= -32768 && i
<= 32767)
1636 return ctx
-> shortint_type
;
1638 else if(i
>= -2147483648 && i
<= 2147483647)
1640 return ctx
-> int_type
;
1644 return ctx
-> longint_type
;
1648 static oberon_expr_t
*
1649 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1651 oberon_expr_t
* expr
;
1652 oberon_type_t
* result
;
1653 result
= oberon_get_type_of_int_value(ctx
, i
);
1654 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1655 expr
-> item
.integer
= i
;
1659 static oberon_expr_t
*
1660 oberon_element(oberon_context_t
* ctx
)
1665 e1
= oberon_expr(ctx
);
1666 if(e1
-> result
-> class != OBERON_TYPE_INTEGER
)
1668 oberon_error(ctx
, "expected integer");
1672 if(ctx
-> token
== DOTDOT
)
1674 oberon_assert_token(ctx
, DOTDOT
);
1675 e2
= oberon_expr(ctx
);
1676 if(e2
-> result
-> class != OBERON_TYPE_INTEGER
)
1678 oberon_error(ctx
, "expected integer");
1682 oberon_expr_t
* set
;
1683 set
= oberon_new_operator(OP_RANGE
, ctx
-> set_type
, e1
, e2
);
1687 static oberon_expr_t
*
1688 oberon_set(oberon_context_t
* ctx
)
1690 oberon_expr_t
* set
;
1691 oberon_expr_t
* elements
;
1692 set
= oberon_new_item(MODE_SET
, ctx
-> set_type
, true);
1693 set
-> item
.integer
= 0;
1695 oberon_assert_token(ctx
, LBRACE
);
1696 if(ISEXPR(ctx
-> token
))
1698 elements
= oberon_element(ctx
);
1699 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1700 while(ctx
-> token
== COMMA
)
1702 oberon_assert_token(ctx
, COMMA
);
1703 elements
= oberon_element(ctx
);
1704 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1707 oberon_assert_token(ctx
, RBRACE
);
1712 static oberon_expr_t
*
1713 oberon_factor(oberon_context_t
* ctx
)
1715 oberon_expr_t
* expr
;
1716 oberon_type_t
* result
;
1718 switch(ctx
-> token
)
1721 expr
= oberon_designator(ctx
);
1722 expr
= oberon_opt_func_parens(ctx
, expr
);
1725 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1726 oberon_assert_token(ctx
, INTEGER
);
1729 result
= ctx
-> char_type
;
1730 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1731 expr
-> item
.integer
= ctx
-> integer
;
1732 oberon_assert_token(ctx
, CHAR
);
1735 result
= ctx
-> string_type
;
1736 expr
= oberon_new_item(MODE_STRING
, result
, true);
1737 expr
-> item
.string
= ctx
-> string
;
1738 oberon_assert_token(ctx
, STRING
);
1741 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1742 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1743 expr
-> item
.real
= ctx
-> real
;
1744 oberon_assert_token(ctx
, REAL
);
1747 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1748 expr
-> item
.boolean
= true;
1749 oberon_assert_token(ctx
, TRUE
);
1752 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1753 expr
-> item
.boolean
= false;
1754 oberon_assert_token(ctx
, FALSE
);
1757 expr
= oberon_set(ctx
);
1760 oberon_assert_token(ctx
, LPAREN
);
1761 expr
= oberon_expr(ctx
);
1762 oberon_assert_token(ctx
, RPAREN
);
1765 oberon_assert_token(ctx
, NOT
);
1766 expr
= oberon_factor(ctx
);
1767 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1770 oberon_assert_token(ctx
, NIL
);
1771 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1774 oberon_error(ctx
, "invalid expression");
1780 #define ITMAKESBOOLEAN(x) \
1781 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1783 #define ITUSEONLYINTEGER(x) \
1784 ((x) >= LESS && (x) <= GEQ)
1786 #define ITUSEONLYBOOLEAN(x) \
1787 (((x) == OR) || ((x) == AND))
1790 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1792 oberon_expr_t
* expr
= *e
;
1793 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1795 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1797 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1801 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1804 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1806 oberon_error(ctx
, "required numeric type");
1810 static oberon_expr_t
*
1811 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1813 oberon_expr_t
* expr
;
1814 oberon_type_t
* result
;
1819 oberon_type_t
* v
= a
-> result
;
1820 if(v
-> class == OBERON_TYPE_POINTER
)
1823 if(v
-> class != OBERON_TYPE_RECORD
)
1825 oberon_error(ctx
, "must be record");
1828 else if(v
-> class != OBERON_TYPE_RECORD
)
1830 oberon_error(ctx
, "must be record");
1833 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1835 oberon_error(ctx
, "requires type");
1838 oberon_type_t
* t
= b
-> result
;
1839 if(t
-> class == OBERON_TYPE_POINTER
)
1842 if(t
-> class != OBERON_TYPE_RECORD
)
1844 oberon_error(ctx
, "must be record");
1847 else if(t
-> class != OBERON_TYPE_RECORD
)
1849 oberon_error(ctx
, "must be record");
1852 result
= ctx
-> bool_type
;
1853 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1855 else if(ITMAKESBOOLEAN(token
))
1857 if(ITUSEONLYINTEGER(token
))
1859 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1860 || b
-> result
-> class == OBERON_TYPE_INTEGER
1861 || a
-> result
-> class == OBERON_TYPE_REAL
1862 || b
-> result
-> class == OBERON_TYPE_REAL
)
1868 oberon_error(ctx
, "used only with numeric types");
1871 else if(ITUSEONLYBOOLEAN(token
))
1873 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1874 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1876 oberon_error(ctx
, "used only with boolean type");
1880 oberon_autocast_binary_op(ctx
, &a
, &b
);
1881 result
= ctx
-> bool_type
;
1885 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1887 else if(token
== NEQ
)
1889 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1891 else if(token
== LESS
)
1893 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1895 else if(token
== LEQ
)
1897 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1899 else if(token
== GREAT
)
1901 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1903 else if(token
== GEQ
)
1905 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1907 else if(token
== OR
)
1909 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1911 else if(token
== AND
)
1913 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1917 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1920 else if(token
== SLASH
)
1922 if(a
-> result
-> class == OBERON_TYPE_SET
1923 || b
-> result
-> class == OBERON_TYPE_SET
)
1925 oberon_autocast_binary_op(ctx
, &a
, &b
);
1926 result
= a
-> result
;
1927 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
1931 oberon_autocast_to_real(ctx
, &a
);
1932 oberon_autocast_to_real(ctx
, &b
);
1933 oberon_autocast_binary_op(ctx
, &a
, &b
);
1934 result
= a
-> result
;
1935 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
1938 else if(token
== DIV
)
1940 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1941 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1943 oberon_error(ctx
, "operator DIV requires integer type");
1946 oberon_autocast_binary_op(ctx
, &a
, &b
);
1947 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1951 oberon_autocast_binary_op(ctx
, &a
, &b
);
1952 result
= a
-> result
;
1953 if(result
-> class == OBERON_TYPE_SET
)
1958 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
1961 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
1964 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
1971 else if(result
-> class == OBERON_TYPE_INTEGER
1972 || result
-> class == OBERON_TYPE_REAL
)
1977 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
1980 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
1983 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
1986 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2001 oberon_error(ctx
, "invalid operation");
2007 #define ISMULOP(x) \
2008 ((x) >= STAR && (x) <= AND)
2010 static oberon_expr_t
*
2011 oberon_term_expr(oberon_context_t
* ctx
)
2013 oberon_expr_t
* expr
;
2015 expr
= oberon_factor(ctx
);
2016 while(ISMULOP(ctx
-> token
))
2018 int token
= ctx
-> token
;
2019 oberon_read_token(ctx
);
2021 oberon_expr_t
* inter
= oberon_factor(ctx
);
2022 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2028 #define ISADDOP(x) \
2029 ((x) >= PLUS && (x) <= OR)
2031 static oberon_expr_t
*
2032 oberon_simple_expr(oberon_context_t
* ctx
)
2034 oberon_expr_t
* expr
;
2037 if(ctx
-> token
== PLUS
)
2040 oberon_assert_token(ctx
, PLUS
);
2042 else if(ctx
-> token
== MINUS
)
2045 oberon_assert_token(ctx
, MINUS
);
2048 expr
= oberon_term_expr(ctx
);
2052 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2055 while(ISADDOP(ctx
-> token
))
2057 int token
= ctx
-> token
;
2058 oberon_read_token(ctx
);
2060 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2061 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2067 #define ISRELATION(x) \
2068 ((x) >= EQUAL && (x) <= IS)
2070 static oberon_expr_t
*
2071 oberon_expr(oberon_context_t
* ctx
)
2073 oberon_expr_t
* expr
;
2075 expr
= oberon_simple_expr(ctx
);
2076 while(ISRELATION(ctx
-> token
))
2078 int token
= ctx
-> token
;
2079 oberon_read_token(ctx
);
2081 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2082 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2088 static oberon_item_t
*
2089 oberon_const_expr(oberon_context_t
* ctx
)
2091 oberon_expr_t
* expr
;
2092 expr
= oberon_expr(ctx
);
2094 if(expr
-> is_item
== 0)
2096 oberon_error(ctx
, "const expression are required");
2099 switch(expr
-> item
.mode
)
2111 oberon_error(ctx
, "const expression are required");
2115 return (oberon_item_t
*) expr
;
2118 // =======================================================================
2120 // =======================================================================
2122 static void oberon_decl_seq(oberon_context_t
* ctx
);
2123 static void oberon_statement_seq(oberon_context_t
* ctx
);
2124 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2127 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2129 if(ctx
-> token
!= token
)
2131 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2136 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2138 oberon_expect_token(ctx
, token
);
2139 oberon_read_token(ctx
);
2143 oberon_assert_ident(oberon_context_t
* ctx
)
2145 oberon_expect_token(ctx
, IDENT
);
2146 char * ident
= ctx
-> string
;
2147 oberon_read_token(ctx
);
2152 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2154 switch(ctx
-> token
)
2157 oberon_assert_token(ctx
, STAR
);
2162 oberon_assert_token(ctx
, MINUS
);
2173 static oberon_object_t
*
2174 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2179 oberon_object_t
* x
;
2181 name
= oberon_assert_ident(ctx
);
2182 oberon_def(ctx
, &export
, &read_only
);
2184 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2189 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2192 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2193 while(ctx
-> token
== COMMA
)
2195 oberon_assert_token(ctx
, COMMA
);
2196 oberon_ident_def(ctx
, class, check_upscope
);
2202 oberon_var_decl(oberon_context_t
* ctx
)
2205 oberon_object_t
* list
;
2206 oberon_type_t
* type
;
2207 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2209 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2210 oberon_assert_token(ctx
, COLON
);
2211 oberon_type(ctx
, &type
);
2213 oberon_object_t
* var
= list
;
2214 for(int i
= 0; i
< num
; i
++)
2221 static oberon_object_t
*
2222 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2224 int class = OBERON_CLASS_PARAM
;
2225 if(ctx
-> token
== VAR
)
2227 oberon_read_token(ctx
);
2228 class = OBERON_CLASS_VAR_PARAM
;
2232 oberon_object_t
* list
;
2233 oberon_ident_list(ctx
, class, false, &num
, &list
);
2235 oberon_assert_token(ctx
, COLON
);
2237 oberon_type_t
* type
;
2238 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2239 oberon_type(ctx
, &type
);
2241 oberon_object_t
* param
= list
;
2242 for(int i
= 0; i
< num
; i
++)
2244 param
-> type
= type
;
2245 param
= param
-> next
;
2252 #define ISFPSECTION \
2253 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2256 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2258 oberon_assert_token(ctx
, LPAREN
);
2262 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2263 while(ctx
-> token
== SEMICOLON
)
2265 oberon_assert_token(ctx
, SEMICOLON
);
2266 oberon_fp_section(ctx
, &signature
-> num_decl
);
2270 oberon_assert_token(ctx
, RPAREN
);
2272 if(ctx
-> token
== COLON
)
2274 oberon_assert_token(ctx
, COLON
);
2276 oberon_object_t
* typeobj
;
2277 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2278 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2280 oberon_error(ctx
, "function result is not type");
2282 signature
-> base
= typeobj
-> type
;
2287 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2289 oberon_type_t
* signature
;
2291 signature
-> class = OBERON_TYPE_PROCEDURE
;
2292 signature
-> num_decl
= 0;
2293 signature
-> base
= ctx
-> void_type
;
2294 signature
-> decl
= NULL
;
2296 if(ctx
-> token
== LPAREN
)
2298 oberon_formal_pars(ctx
, signature
);
2303 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2305 if(a
-> num_decl
!= b
-> num_decl
)
2307 oberon_error(ctx
, "number parameters not matched");
2310 int num_param
= a
-> num_decl
;
2311 oberon_object_t
* param_a
= a
-> decl
;
2312 oberon_object_t
* param_b
= b
-> decl
;
2313 for(int i
= 0; i
< num_param
; i
++)
2315 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2317 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2320 if(param_a
-> type
!= param_b
-> type
)
2322 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2325 param_a
= param_a
-> next
;
2326 param_b
= param_b
-> next
;
2331 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2333 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2334 oberon_type_t
* result_type
= proc
-> type
-> base
;
2336 if(result_type
-> class == OBERON_TYPE_VOID
)
2340 oberon_error(ctx
, "procedure has no result type");
2347 oberon_error(ctx
, "procedure requires expression on result");
2350 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2353 proc
-> has_return
= 1;
2355 oberon_generate_return(ctx
, expr
);
2359 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2361 oberon_assert_token(ctx
, SEMICOLON
);
2363 ctx
-> decl
= proc
-> scope
;
2365 oberon_decl_seq(ctx
);
2367 oberon_generate_begin_proc(ctx
, proc
);
2369 if(ctx
-> token
== BEGIN
)
2371 oberon_assert_token(ctx
, BEGIN
);
2372 oberon_statement_seq(ctx
);
2375 oberon_assert_token(ctx
, END
);
2376 char * name
= oberon_assert_ident(ctx
);
2377 if(strcmp(name
, proc
-> name
) != 0)
2379 oberon_error(ctx
, "procedure name not matched");
2382 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2383 && proc
-> has_return
== 0)
2385 oberon_make_return(ctx
, NULL
);
2388 if(proc
-> has_return
== 0)
2390 oberon_error(ctx
, "procedure requires return");
2393 oberon_generate_end_proc(ctx
);
2394 oberon_close_scope(ctx
-> decl
);
2398 oberon_proc_decl(oberon_context_t
* ctx
)
2400 oberon_assert_token(ctx
, PROCEDURE
);
2403 if(ctx
-> token
== UPARROW
)
2405 oberon_assert_token(ctx
, UPARROW
);
2412 name
= oberon_assert_ident(ctx
);
2413 oberon_def(ctx
, &export
, &read_only
);
2415 oberon_scope_t
* proc_scope
;
2416 proc_scope
= oberon_open_scope(ctx
);
2417 ctx
-> decl
-> local
= 1;
2419 oberon_type_t
* signature
;
2420 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2421 oberon_opt_formal_pars(ctx
, &signature
);
2423 oberon_initialize_decl(ctx
);
2424 oberon_generator_init_type(ctx
, signature
);
2425 oberon_close_scope(ctx
-> decl
);
2427 oberon_object_t
* proc
;
2428 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2431 if(proc
-> class != OBERON_CLASS_PROC
)
2433 oberon_error(ctx
, "mult definition");
2440 oberon_error(ctx
, "mult procedure definition");
2444 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2446 oberon_error(ctx
, "export type not matched");
2449 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2453 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2454 proc
-> type
= signature
;
2455 proc
-> scope
= proc_scope
;
2456 oberon_generator_init_proc(ctx
, proc
);
2459 proc
-> scope
-> parent
= proc
;
2464 oberon_proc_decl_body(ctx
, proc
);
2469 oberon_const_decl(oberon_context_t
* ctx
)
2471 oberon_item_t
* value
;
2472 oberon_object_t
* constant
;
2474 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2475 oberon_assert_token(ctx
, EQUAL
);
2476 value
= oberon_const_expr(ctx
);
2477 constant
-> value
= value
;
2481 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2483 if(size
-> is_item
== 0)
2485 oberon_error(ctx
, "requires constant");
2488 if(size
-> item
.mode
!= MODE_INTEGER
)
2490 oberon_error(ctx
, "requires integer constant");
2493 oberon_type_t
* arr
;
2495 arr
-> class = OBERON_TYPE_ARRAY
;
2496 arr
-> size
= size
-> item
.integer
;
2501 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2504 oberon_object_t
* to
;
2506 to
= oberon_qualident(ctx
, &name
, 0);
2508 //name = oberon_assert_ident(ctx);
2509 //to = oberon_find_object(ctx -> decl, name, 0);
2513 if(to
-> class != OBERON_CLASS_TYPE
)
2515 oberon_error(ctx
, "not a type");
2520 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2521 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2527 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2530 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2534 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2542 oberon_type_t
* dim
;
2543 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2545 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2547 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2551 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2553 type
-> class = OBERON_TYPE_ARRAY
;
2555 type
-> base
= base
;
2559 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2561 if(ctx
-> token
== IDENT
)
2564 oberon_object_t
* list
;
2565 oberon_type_t
* type
;
2566 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2568 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2569 oberon_assert_token(ctx
, COLON
);
2571 oberon_scope_t
* current
= ctx
-> decl
;
2572 ctx
-> decl
= modscope
;
2573 oberon_type(ctx
, &type
);
2574 ctx
-> decl
= current
;
2576 oberon_object_t
* field
= list
;
2577 for(int i
= 0; i
< num
; i
++)
2579 field
-> type
= type
;
2580 field
= field
-> next
;
2583 rec
-> num_decl
+= num
;
2588 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2590 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2591 oberon_scope_t
* oldscope
= ctx
-> decl
;
2592 ctx
-> decl
= modscope
;
2594 if(ctx
-> token
== LPAREN
)
2596 oberon_assert_token(ctx
, LPAREN
);
2598 oberon_object_t
* typeobj
;
2599 typeobj
= oberon_qualident(ctx
, NULL
, true);
2601 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2603 oberon_error(ctx
, "base must be type");
2606 oberon_type_t
* base
= typeobj
-> type
;
2607 if(base
-> class == OBERON_TYPE_POINTER
)
2609 base
= base
-> base
;
2612 if(base
-> class != OBERON_TYPE_RECORD
)
2614 oberon_error(ctx
, "base must be record type");
2618 ctx
-> decl
= base
-> scope
;
2620 oberon_assert_token(ctx
, RPAREN
);
2627 oberon_scope_t
* this_scope
;
2628 this_scope
= oberon_open_scope(ctx
);
2629 this_scope
-> local
= true;
2630 this_scope
-> parent
= NULL
;
2631 this_scope
-> parent_type
= rec
;
2633 oberon_field_list(ctx
, rec
, modscope
);
2634 while(ctx
-> token
== SEMICOLON
)
2636 oberon_assert_token(ctx
, SEMICOLON
);
2637 oberon_field_list(ctx
, rec
, modscope
);
2640 rec
-> scope
= this_scope
;
2641 rec
-> decl
= this_scope
-> list
-> next
;
2642 ctx
-> decl
= oldscope
;
2646 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2648 if(ctx
-> token
== IDENT
)
2650 oberon_qualident_type(ctx
, type
);
2652 else if(ctx
-> token
== ARRAY
)
2654 oberon_assert_token(ctx
, ARRAY
);
2657 oberon_expr_t
* sizes
;
2659 if(ISEXPR(ctx
-> token
))
2661 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2664 oberon_assert_token(ctx
, OF
);
2666 oberon_type_t
* base
;
2667 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2668 oberon_type(ctx
, &base
);
2672 oberon_make_open_array(ctx
, base
, *type
);
2676 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2679 else if(ctx
-> token
== RECORD
)
2681 oberon_type_t
* rec
;
2683 rec
-> class = OBERON_TYPE_RECORD
;
2684 rec
-> module
= ctx
-> mod
;
2686 oberon_assert_token(ctx
, RECORD
);
2687 oberon_type_record_body(ctx
, rec
);
2688 oberon_assert_token(ctx
, END
);
2692 else if(ctx
-> token
== POINTER
)
2694 oberon_assert_token(ctx
, POINTER
);
2695 oberon_assert_token(ctx
, TO
);
2697 oberon_type_t
* base
;
2698 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2699 oberon_type(ctx
, &base
);
2701 oberon_type_t
* ptr
;
2703 ptr
-> class = OBERON_TYPE_POINTER
;
2706 else if(ctx
-> token
== PROCEDURE
)
2708 oberon_open_scope(ctx
);
2709 oberon_assert_token(ctx
, PROCEDURE
);
2710 oberon_opt_formal_pars(ctx
, type
);
2711 oberon_close_scope(ctx
-> decl
);
2715 oberon_error(ctx
, "invalid type declaration");
2720 oberon_type_decl(oberon_context_t
* ctx
)
2723 oberon_object_t
* newtype
;
2724 oberon_type_t
* type
;
2728 name
= oberon_assert_ident(ctx
);
2729 oberon_def(ctx
, &export
, &read_only
);
2731 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2734 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2735 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2736 assert(newtype
-> type
);
2740 if(newtype
-> class != OBERON_CLASS_TYPE
)
2742 oberon_error(ctx
, "mult definition");
2745 if(newtype
-> linked
)
2747 oberon_error(ctx
, "mult definition - already linked");
2750 newtype
-> export
= export
;
2751 newtype
-> read_only
= read_only
;
2754 oberon_assert_token(ctx
, EQUAL
);
2756 type
= newtype
-> type
;
2757 oberon_type(ctx
, &type
);
2759 if(type
-> class == OBERON_TYPE_VOID
)
2761 oberon_error(ctx
, "recursive alias declaration");
2764 newtype
-> type
= type
;
2765 newtype
-> linked
= 1;
2768 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2769 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2772 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2774 if(type
-> class != OBERON_TYPE_POINTER
2775 && type
-> class != OBERON_TYPE_ARRAY
)
2780 if(type
-> recursive
)
2782 oberon_error(ctx
, "recursive pointer declaration");
2785 if(type
-> class == OBERON_TYPE_POINTER
2786 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2788 oberon_error(ctx
, "attempt to make pointer to pointer");
2791 type
-> recursive
= 1;
2793 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2795 type
-> recursive
= 0;
2799 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2801 if(type
-> class != OBERON_TYPE_RECORD
)
2806 if(type
-> recursive
)
2808 oberon_error(ctx
, "recursive record declaration");
2811 type
-> recursive
= 1;
2813 int num_fields
= type
-> num_decl
;
2814 oberon_object_t
* field
= type
-> decl
;
2815 for(int i
= 0; i
< num_fields
; i
++)
2817 oberon_prevent_recursive_object(ctx
, field
);
2818 field
= field
-> next
;
2821 type
-> recursive
= 0;
2824 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2826 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2831 if(type
-> recursive
)
2833 oberon_error(ctx
, "recursive procedure declaration");
2836 type
-> recursive
= 1;
2838 int num_fields
= type
-> num_decl
;
2839 oberon_object_t
* field
= type
-> decl
;
2840 for(int i
= 0; i
< num_fields
; i
++)
2842 oberon_prevent_recursive_object(ctx
, field
);
2843 field
= field
-> next
;
2846 type
-> recursive
= 0;
2850 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2852 if(type
-> class != OBERON_TYPE_ARRAY
)
2857 if(type
-> recursive
)
2859 oberon_error(ctx
, "recursive array declaration");
2862 type
-> recursive
= 1;
2864 oberon_prevent_recursive_type(ctx
, type
-> base
);
2866 type
-> recursive
= 0;
2870 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2872 if(type
-> class == OBERON_TYPE_POINTER
)
2874 oberon_prevent_recursive_pointer(ctx
, type
);
2876 else if(type
-> class == OBERON_TYPE_RECORD
)
2878 oberon_prevent_recursive_record(ctx
, type
);
2880 else if(type
-> class == OBERON_TYPE_ARRAY
)
2882 oberon_prevent_recursive_array(ctx
, type
);
2884 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2886 oberon_prevent_recursive_procedure(ctx
, type
);
2891 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2895 case OBERON_CLASS_VAR
:
2896 case OBERON_CLASS_TYPE
:
2897 case OBERON_CLASS_PARAM
:
2898 case OBERON_CLASS_VAR_PARAM
:
2899 case OBERON_CLASS_FIELD
:
2900 oberon_prevent_recursive_type(ctx
, x
-> type
);
2902 case OBERON_CLASS_CONST
:
2903 case OBERON_CLASS_PROC
:
2904 case OBERON_CLASS_MODULE
:
2907 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2913 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2915 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2919 oberon_prevent_recursive_object(ctx
, x
);
2924 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2925 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2928 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2930 if(type
-> class != OBERON_TYPE_RECORD
)
2935 int num_fields
= type
-> num_decl
;
2936 oberon_object_t
* field
= type
-> decl
;
2937 for(int i
= 0; i
< num_fields
; i
++)
2939 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2941 oberon_initialize_type(ctx
, field
-> type
);
2944 oberon_initialize_object(ctx
, field
);
2945 field
= field
-> next
;
2948 oberon_generator_init_record(ctx
, type
);
2952 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2954 if(type
-> class == OBERON_TYPE_VOID
)
2956 oberon_error(ctx
, "undeclarated type");
2959 if(type
-> initialized
)
2964 type
-> initialized
= 1;
2966 if(type
-> class == OBERON_TYPE_POINTER
)
2968 oberon_initialize_type(ctx
, type
-> base
);
2969 oberon_generator_init_type(ctx
, type
);
2971 else if(type
-> class == OBERON_TYPE_ARRAY
)
2973 if(type
-> size
!= 0)
2975 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2977 if(type
-> base
-> size
== 0)
2979 oberon_error(ctx
, "open array not allowed as array element");
2984 oberon_initialize_type(ctx
, type
-> base
);
2985 oberon_generator_init_type(ctx
, type
);
2987 else if(type
-> class == OBERON_TYPE_RECORD
)
2989 oberon_generator_init_type(ctx
, type
);
2990 oberon_initialize_record_fields(ctx
, type
);
2992 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2994 int num_fields
= type
-> num_decl
;
2995 oberon_object_t
* field
= type
-> decl
;
2996 for(int i
= 0; i
< num_fields
; i
++)
2998 oberon_initialize_object(ctx
, field
);
2999 field
= field
-> next
;
3002 oberon_generator_init_type(ctx
, type
);
3006 oberon_generator_init_type(ctx
, type
);
3011 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3013 if(x
-> initialized
)
3018 x
-> initialized
= 1;
3022 case OBERON_CLASS_TYPE
:
3023 oberon_initialize_type(ctx
, x
-> type
);
3025 case OBERON_CLASS_VAR
:
3026 case OBERON_CLASS_FIELD
:
3027 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3029 if(x
-> type
-> size
== 0)
3031 oberon_error(ctx
, "open array not allowed as variable or field");
3034 oberon_initialize_type(ctx
, x
-> type
);
3035 oberon_generator_init_var(ctx
, x
);
3037 case OBERON_CLASS_PARAM
:
3038 case OBERON_CLASS_VAR_PARAM
:
3039 oberon_initialize_type(ctx
, x
-> type
);
3040 oberon_generator_init_var(ctx
, x
);
3042 case OBERON_CLASS_CONST
:
3043 case OBERON_CLASS_PROC
:
3044 case OBERON_CLASS_MODULE
:
3047 oberon_error(ctx
, "oberon_initialize_object: wat");
3053 oberon_initialize_decl(oberon_context_t
* ctx
)
3055 oberon_object_t
* x
= ctx
-> decl
-> list
;
3059 oberon_initialize_object(ctx
, x
-> next
);
3065 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3067 oberon_object_t
* x
= ctx
-> decl
-> list
;
3071 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3073 if(x
-> next
-> linked
== 0)
3075 oberon_error(ctx
, "unresolved forward declaration");
3083 oberon_decl_seq(oberon_context_t
* ctx
)
3085 if(ctx
-> token
== CONST
)
3087 oberon_assert_token(ctx
, CONST
);
3088 while(ctx
-> token
== IDENT
)
3090 oberon_const_decl(ctx
);
3091 oberon_assert_token(ctx
, SEMICOLON
);
3095 if(ctx
-> token
== TYPE
)
3097 oberon_assert_token(ctx
, TYPE
);
3098 while(ctx
-> token
== IDENT
)
3100 oberon_type_decl(ctx
);
3101 oberon_assert_token(ctx
, SEMICOLON
);
3105 if(ctx
-> token
== VAR
)
3107 oberon_assert_token(ctx
, VAR
);
3108 while(ctx
-> token
== IDENT
)
3110 oberon_var_decl(ctx
);
3111 oberon_assert_token(ctx
, SEMICOLON
);
3115 oberon_prevent_recursive_decl(ctx
);
3116 oberon_initialize_decl(ctx
);
3118 while(ctx
-> token
== PROCEDURE
)
3120 oberon_proc_decl(ctx
);
3121 oberon_assert_token(ctx
, SEMICOLON
);
3124 oberon_prevent_undeclarated_procedures(ctx
);
3127 static oberon_expr_t
*
3128 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3130 oberon_object_t
* x
;
3131 oberon_expr_t
* expr
;
3133 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3136 oberon_generator_init_temp_var(ctx
, x
);
3138 expr
= oberon_new_item(MODE_VAR
, type
, false);
3139 expr
-> item
.var
= x
;
3144 oberon_statement_seq(oberon_context_t
* ctx
);
3147 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3149 if(dst
-> read_only
)
3151 oberon_error(ctx
, "read-only destination");
3154 oberon_check_dst(ctx
, dst
);
3155 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3156 oberon_generate_assign(ctx
, src
, dst
);
3160 oberon_statement(oberon_context_t
* ctx
)
3162 oberon_expr_t
* item1
;
3163 oberon_expr_t
* item2
;
3165 if(ctx
-> token
== IDENT
)
3167 item1
= oberon_designator(ctx
);
3168 if(ctx
-> token
== ASSIGN
)
3170 oberon_assert_token(ctx
, ASSIGN
);
3171 item2
= oberon_expr(ctx
);
3172 oberon_assign(ctx
, item2
, item1
);
3176 oberon_opt_proc_parens(ctx
, item1
);
3179 else if(ctx
-> token
== IF
)
3183 oberon_expr_t
* cond
;
3185 els
= oberon_generator_reserve_label(ctx
);
3186 end
= oberon_generator_reserve_label(ctx
);
3188 oberon_assert_token(ctx
, IF
);
3189 cond
= oberon_expr(ctx
);
3190 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3192 oberon_error(ctx
, "condition must be boolean");
3194 oberon_assert_token(ctx
, THEN
);
3195 oberon_generate_branch(ctx
, cond
, false, els
);
3196 oberon_statement_seq(ctx
);
3197 oberon_generate_goto(ctx
, end
);
3198 oberon_generate_label(ctx
, els
);
3200 while(ctx
-> token
== ELSIF
)
3202 els
= oberon_generator_reserve_label(ctx
);
3204 oberon_assert_token(ctx
, ELSIF
);
3205 cond
= oberon_expr(ctx
);
3206 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3208 oberon_error(ctx
, "condition must be boolean");
3210 oberon_assert_token(ctx
, THEN
);
3211 oberon_generate_branch(ctx
, cond
, false, els
);
3212 oberon_statement_seq(ctx
);
3213 oberon_generate_goto(ctx
, end
);
3214 oberon_generate_label(ctx
, els
);
3217 if(ctx
-> token
== ELSE
)
3219 oberon_assert_token(ctx
, ELSE
);
3220 oberon_statement_seq(ctx
);
3223 oberon_generate_label(ctx
, end
);
3224 oberon_assert_token(ctx
, END
);
3226 else if(ctx
-> token
== WHILE
)
3228 gen_label_t
* begin
;
3230 oberon_expr_t
* cond
;
3232 begin
= oberon_generator_reserve_label(ctx
);
3233 end
= oberon_generator_reserve_label(ctx
);
3235 oberon_assert_token(ctx
, WHILE
);
3236 oberon_generate_label(ctx
, begin
);
3237 cond
= oberon_expr(ctx
);
3238 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3240 oberon_error(ctx
, "condition must be boolean");
3242 oberon_generate_branch(ctx
, cond
, false, end
);
3244 oberon_assert_token(ctx
, DO
);
3245 oberon_statement_seq(ctx
);
3246 oberon_generate_goto(ctx
, begin
);
3248 oberon_assert_token(ctx
, END
);
3249 oberon_generate_label(ctx
, end
);
3251 else if(ctx
-> token
== REPEAT
)
3253 gen_label_t
* begin
;
3254 oberon_expr_t
* cond
;
3256 begin
= oberon_generator_reserve_label(ctx
);
3257 oberon_generate_label(ctx
, begin
);
3258 oberon_assert_token(ctx
, REPEAT
);
3260 oberon_statement_seq(ctx
);
3262 oberon_assert_token(ctx
, UNTIL
);
3264 cond
= oberon_expr(ctx
);
3265 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3267 oberon_error(ctx
, "condition must be boolean");
3270 oberon_generate_branch(ctx
, cond
, true, begin
);
3272 else if(ctx
-> token
== FOR
)
3274 oberon_expr_t
* from
;
3275 oberon_expr_t
* index
;
3277 oberon_expr_t
* bound
;
3279 oberon_expr_t
* cond
;
3280 oberon_expr_t
* count
;
3281 gen_label_t
* begin
;
3286 begin
= oberon_generator_reserve_label(ctx
);
3287 end
= oberon_generator_reserve_label(ctx
);
3289 oberon_assert_token(ctx
, FOR
);
3290 iname
= oberon_assert_ident(ctx
);
3291 index
= oberon_ident_item(ctx
, iname
);
3292 oberon_assert_token(ctx
, ASSIGN
);
3293 from
= oberon_expr(ctx
);
3294 oberon_assign(ctx
, from
, index
);
3295 oberon_assert_token(ctx
, TO
);
3296 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3297 to
= oberon_expr(ctx
);
3298 oberon_assign(ctx
, to
, bound
);
3299 if(ctx
-> token
== BY
)
3301 oberon_assert_token(ctx
, BY
);
3302 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3306 by
= oberon_integer_item(ctx
, 1);
3309 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3311 oberon_error(ctx
, "must be integer");
3314 if(by
-> item
.integer
> 0)
3318 else if(by
-> item
.integer
< 0)
3324 oberon_error(ctx
, "zero step not allowed");
3327 oberon_assert_token(ctx
, DO
);
3328 oberon_generate_label(ctx
, begin
);
3329 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3330 oberon_generate_branch(ctx
, cond
, false, end
);
3331 oberon_statement_seq(ctx
);
3332 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3333 oberon_assign(ctx
, count
, index
);
3334 oberon_generate_goto(ctx
, begin
);
3335 oberon_generate_label(ctx
, end
);
3336 oberon_assert_token(ctx
, END
);
3338 else if(ctx
-> token
== LOOP
)
3340 gen_label_t
* begin
;
3343 begin
= oberon_generator_reserve_label(ctx
);
3344 end
= oberon_generator_reserve_label(ctx
);
3346 oberon_open_scope(ctx
);
3347 oberon_assert_token(ctx
, LOOP
);
3348 oberon_generate_label(ctx
, begin
);
3349 ctx
-> decl
-> exit_label
= end
;
3350 oberon_statement_seq(ctx
);
3351 oberon_generate_goto(ctx
, begin
);
3352 oberon_generate_label(ctx
, end
);
3353 oberon_assert_token(ctx
, END
);
3354 oberon_close_scope(ctx
-> decl
);
3356 else if(ctx
-> token
== EXIT
)
3358 oberon_assert_token(ctx
, EXIT
);
3359 if(ctx
-> decl
-> exit_label
== NULL
)
3361 oberon_error(ctx
, "not in LOOP-END");
3363 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3365 else if(ctx
-> token
== RETURN
)
3367 oberon_assert_token(ctx
, RETURN
);
3368 if(ISEXPR(ctx
-> token
))
3370 oberon_expr_t
* expr
;
3371 expr
= oberon_expr(ctx
);
3372 oberon_make_return(ctx
, expr
);
3376 oberon_make_return(ctx
, NULL
);
3382 oberon_statement_seq(oberon_context_t
* ctx
)
3384 oberon_statement(ctx
);
3385 while(ctx
-> token
== SEMICOLON
)
3387 oberon_assert_token(ctx
, SEMICOLON
);
3388 oberon_statement(ctx
);
3393 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3395 oberon_module_t
* m
= ctx
-> module_list
;
3396 while(m
&& strcmp(m
-> name
, name
) != 0)
3404 code
= ctx
-> import_module(name
);
3407 oberon_error(ctx
, "no such module");
3410 m
= oberon_compile_module(ctx
, code
);
3416 oberon_error(ctx
, "cyclic module import");
3419 oberon_object_t
* ident
;
3420 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3421 ident
-> module
= m
;
3425 oberon_import_decl(oberon_context_t
* ctx
)
3430 alias
= name
= oberon_assert_ident(ctx
);
3431 if(ctx
-> token
== ASSIGN
)
3433 oberon_assert_token(ctx
, ASSIGN
);
3434 name
= oberon_assert_ident(ctx
);
3437 oberon_import_module(ctx
, alias
, name
);
3441 oberon_import_list(oberon_context_t
* ctx
)
3443 oberon_assert_token(ctx
, IMPORT
);
3445 oberon_import_decl(ctx
);
3446 while(ctx
-> token
== COMMA
)
3448 oberon_assert_token(ctx
, COMMA
);
3449 oberon_import_decl(ctx
);
3452 oberon_assert_token(ctx
, SEMICOLON
);
3456 oberon_parse_module(oberon_context_t
* ctx
)
3460 oberon_read_token(ctx
);
3462 oberon_assert_token(ctx
, MODULE
);
3463 name1
= oberon_assert_ident(ctx
);
3464 oberon_assert_token(ctx
, SEMICOLON
);
3465 ctx
-> mod
-> name
= name1
;
3467 oberon_generator_init_module(ctx
, ctx
-> mod
);
3469 if(ctx
-> token
== IMPORT
)
3471 oberon_import_list(ctx
);
3474 oberon_decl_seq(ctx
);
3476 oberon_generate_begin_module(ctx
);
3477 if(ctx
-> token
== BEGIN
)
3479 oberon_assert_token(ctx
, BEGIN
);
3480 oberon_statement_seq(ctx
);
3482 oberon_generate_end_module(ctx
);
3484 oberon_assert_token(ctx
, END
);
3485 name2
= oberon_assert_ident(ctx
);
3486 oberon_assert_token(ctx
, DOT
);
3488 if(strcmp(name1
, name2
) != 0)
3490 oberon_error(ctx
, "module name not matched");
3493 oberon_generator_fini_module(ctx
-> mod
);
3496 // =======================================================================
3498 // =======================================================================
3501 register_default_types(oberon_context_t
* ctx
)
3503 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3504 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3506 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3507 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3508 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3510 ctx
-> string_type
= oberon_new_type_string(1);
3511 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3513 ctx
-> bool_type
= oberon_new_type_boolean();
3514 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3516 ctx
-> byte_type
= oberon_new_type_integer(1);
3517 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3519 ctx
-> shortint_type
= oberon_new_type_integer(2);
3520 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3522 ctx
-> int_type
= oberon_new_type_integer(4);
3523 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3525 ctx
-> longint_type
= oberon_new_type_integer(8);
3526 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3528 ctx
-> real_type
= oberon_new_type_real(4);
3529 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3531 ctx
-> longreal_type
= oberon_new_type_real(8);
3532 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3534 ctx
-> char_type
= oberon_new_type_char(1);
3535 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3537 ctx
-> set_type
= oberon_new_type_set(4);
3538 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3542 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3544 oberon_object_t
* proc
;
3545 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3546 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3547 proc
-> type
-> sysproc
= true;
3548 proc
-> type
-> genfunc
= f
;
3549 proc
-> type
-> genproc
= p
;
3552 static oberon_expr_t
*
3553 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3557 oberon_error(ctx
, "too few arguments");
3562 oberon_error(ctx
, "too mach arguments");
3565 oberon_expr_t
* arg
;
3568 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3570 oberon_error(ctx
, "MIN accept only type");
3573 oberon_expr_t
* expr
;
3574 int bits
= arg
-> result
-> size
* 8;
3575 switch(arg
-> result
-> class)
3577 case OBERON_TYPE_INTEGER
:
3578 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3580 case OBERON_TYPE_SET
:
3581 expr
= oberon_integer_item(ctx
, 0);
3584 oberon_error(ctx
, "allowed only basic types");
3591 static oberon_expr_t
*
3592 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3596 oberon_error(ctx
, "too few arguments");
3601 oberon_error(ctx
, "too mach arguments");
3604 oberon_expr_t
* arg
;
3607 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3609 oberon_error(ctx
, "MAX accept only type");
3612 oberon_expr_t
* expr
;
3613 int bits
= arg
-> result
-> size
* 8;
3614 switch(arg
-> result
-> class)
3616 case OBERON_TYPE_INTEGER
:
3617 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3619 case OBERON_TYPE_SET
:
3620 expr
= oberon_integer_item(ctx
, bits
);
3623 oberon_error(ctx
, "allowed only basic types");
3630 static oberon_expr_t
*
3631 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3635 oberon_error(ctx
, "too few arguments");
3640 oberon_error(ctx
, "too mach arguments");
3643 oberon_expr_t
* arg
;
3646 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3648 oberon_error(ctx
, "SIZE accept only type");
3652 oberon_expr_t
* expr
;
3653 oberon_type_t
* type
= arg
-> result
;
3654 switch(type
-> class)
3656 case OBERON_TYPE_INTEGER
:
3657 case OBERON_TYPE_BOOLEAN
:
3658 case OBERON_TYPE_REAL
:
3659 case OBERON_TYPE_CHAR
:
3660 case OBERON_TYPE_SET
:
3661 size
= type
-> size
;
3664 oberon_error(ctx
, "TODO SIZE");
3668 expr
= oberon_integer_item(ctx
, size
);
3672 static oberon_expr_t
*
3673 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3677 oberon_error(ctx
, "too few arguments");
3682 oberon_error(ctx
, "too mach arguments");
3685 oberon_expr_t
* arg
;
3687 oberon_check_src(ctx
, arg
);
3689 oberon_type_t
* result_type
;
3690 result_type
= arg
-> result
;
3692 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3694 oberon_error(ctx
, "ABS accepts only integers");
3697 oberon_expr_t
* expr
;
3698 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3703 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3707 oberon_error(ctx
, "too few arguments");
3711 oberon_expr_t
* dst
;
3713 oberon_check_dst(ctx
, dst
);
3715 oberon_type_t
* type
;
3716 type
= dst
-> result
;
3718 if(type
-> class != OBERON_TYPE_POINTER
)
3720 oberon_error(ctx
, "not a pointer");
3723 type
= type
-> base
;
3725 oberon_expr_t
* src
;
3726 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3727 src
-> item
.num_args
= 0;
3728 src
-> item
.args
= NULL
;
3731 if(type
-> class == OBERON_TYPE_ARRAY
)
3733 if(type
-> size
== 0)
3735 oberon_type_t
* x
= type
;
3736 while(x
-> class == OBERON_TYPE_ARRAY
)
3746 if(num_args
< max_args
)
3748 oberon_error(ctx
, "too few arguments");
3751 if(num_args
> max_args
)
3753 oberon_error(ctx
, "too mach arguments");
3756 int num_sizes
= max_args
- 1;
3757 oberon_expr_t
* size_list
= list_args
-> next
;
3759 oberon_expr_t
* arg
= size_list
;
3760 for(int i
= 0; i
< max_args
- 1; i
++)
3762 oberon_check_src(ctx
, arg
);
3763 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3765 oberon_error(ctx
, "size must be integer");
3770 src
-> item
.num_args
= num_sizes
;
3771 src
-> item
.args
= size_list
;
3773 else if(type
-> class != OBERON_TYPE_RECORD
)
3775 oberon_error(ctx
, "oberon_make_new_call: wat");
3778 if(num_args
> max_args
)
3780 oberon_error(ctx
, "too mach arguments");
3783 oberon_assign(ctx
, src
, dst
);
3787 oberon_create_context(ModuleImportCallback import_module
)
3789 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3791 oberon_scope_t
* world_scope
;
3792 world_scope
= oberon_open_scope(ctx
);
3793 ctx
-> world_scope
= world_scope
;
3795 ctx
-> import_module
= import_module
;
3797 oberon_generator_init_context(ctx
);
3799 register_default_types(ctx
);
3802 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3803 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
3804 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
3805 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
3808 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3814 oberon_destroy_context(oberon_context_t
* ctx
)
3816 oberon_generator_destroy_context(ctx
);
3821 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3823 const char * code
= ctx
-> code
;
3824 int code_index
= ctx
-> code_index
;
3826 int token
= ctx
-> token
;
3827 char * string
= ctx
-> string
;
3828 int integer
= ctx
-> integer
;
3829 int real
= ctx
-> real
;
3830 bool longmode
= ctx
-> longmode
;
3831 oberon_scope_t
* decl
= ctx
-> decl
;
3832 oberon_module_t
* mod
= ctx
-> mod
;
3834 oberon_scope_t
* module_scope
;
3835 module_scope
= oberon_open_scope(ctx
);
3837 oberon_module_t
* module
;
3838 module
= calloc(1, sizeof *module
);
3839 module
-> decl
= module_scope
;
3840 module
-> next
= ctx
-> module_list
;
3842 ctx
-> mod
= module
;
3843 ctx
-> module_list
= module
;
3845 oberon_init_scaner(ctx
, newcode
);
3846 oberon_parse_module(ctx
);
3848 module
-> ready
= 1;
3851 ctx
-> code_index
= code_index
;
3853 ctx
-> token
= token
;
3854 ctx
-> string
= string
;
3855 ctx
-> integer
= integer
;
3857 ctx
-> longmode
= longmode
;