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 if(a
-> result
-> class != OBERON_TYPE_INTEGER
)
1821 oberon_error(ctx
, "must be integer");
1824 if(b
-> result
-> class != OBERON_TYPE_SET
)
1826 oberon_error(ctx
, "must be set");
1829 result
= ctx
-> bool_type
;
1830 expr
= oberon_new_operator(OP_IN
, result
, a
, b
);
1832 else if(token
== IS
)
1834 oberon_type_t
* v
= a
-> result
;
1835 if(v
-> class == OBERON_TYPE_POINTER
)
1838 if(v
-> class != OBERON_TYPE_RECORD
)
1840 oberon_error(ctx
, "must be record");
1843 else if(v
-> class != OBERON_TYPE_RECORD
)
1845 oberon_error(ctx
, "must be record");
1848 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1850 oberon_error(ctx
, "requires type");
1853 oberon_type_t
* t
= b
-> result
;
1854 if(t
-> class == OBERON_TYPE_POINTER
)
1857 if(t
-> class != OBERON_TYPE_RECORD
)
1859 oberon_error(ctx
, "must be record");
1862 else if(t
-> class != OBERON_TYPE_RECORD
)
1864 oberon_error(ctx
, "must be record");
1867 result
= ctx
-> bool_type
;
1868 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1870 else if(ITMAKESBOOLEAN(token
))
1872 if(ITUSEONLYINTEGER(token
))
1874 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1875 || b
-> result
-> class == OBERON_TYPE_INTEGER
1876 || a
-> result
-> class == OBERON_TYPE_REAL
1877 || b
-> result
-> class == OBERON_TYPE_REAL
)
1883 oberon_error(ctx
, "used only with numeric types");
1886 else if(ITUSEONLYBOOLEAN(token
))
1888 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1889 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1891 oberon_error(ctx
, "used only with boolean type");
1895 oberon_autocast_binary_op(ctx
, &a
, &b
);
1896 result
= ctx
-> bool_type
;
1900 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1902 else if(token
== NEQ
)
1904 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1906 else if(token
== LESS
)
1908 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1910 else if(token
== LEQ
)
1912 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1914 else if(token
== GREAT
)
1916 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1918 else if(token
== GEQ
)
1920 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1922 else if(token
== OR
)
1924 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1926 else if(token
== AND
)
1928 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1932 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1935 else if(token
== SLASH
)
1937 if(a
-> result
-> class == OBERON_TYPE_SET
1938 || b
-> result
-> class == OBERON_TYPE_SET
)
1940 oberon_autocast_binary_op(ctx
, &a
, &b
);
1941 result
= a
-> result
;
1942 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
1946 oberon_autocast_to_real(ctx
, &a
);
1947 oberon_autocast_to_real(ctx
, &b
);
1948 oberon_autocast_binary_op(ctx
, &a
, &b
);
1949 result
= a
-> result
;
1950 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
1953 else if(token
== DIV
)
1955 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1956 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1958 oberon_error(ctx
, "operator DIV requires integer type");
1961 oberon_autocast_binary_op(ctx
, &a
, &b
);
1962 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1966 oberon_autocast_binary_op(ctx
, &a
, &b
);
1967 result
= a
-> result
;
1968 if(result
-> class == OBERON_TYPE_SET
)
1973 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
1976 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
1979 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
1986 else if(result
-> class == OBERON_TYPE_INTEGER
1987 || result
-> class == OBERON_TYPE_REAL
)
1992 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
1995 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
1998 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
2001 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2016 oberon_error(ctx
, "invalid operation");
2022 #define ISMULOP(x) \
2023 ((x) >= STAR && (x) <= AND)
2025 static oberon_expr_t
*
2026 oberon_term_expr(oberon_context_t
* ctx
)
2028 oberon_expr_t
* expr
;
2030 expr
= oberon_factor(ctx
);
2031 while(ISMULOP(ctx
-> token
))
2033 int token
= ctx
-> token
;
2034 oberon_read_token(ctx
);
2036 oberon_expr_t
* inter
= oberon_factor(ctx
);
2037 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2043 #define ISADDOP(x) \
2044 ((x) >= PLUS && (x) <= OR)
2046 static oberon_expr_t
*
2047 oberon_simple_expr(oberon_context_t
* ctx
)
2049 oberon_expr_t
* expr
;
2052 if(ctx
-> token
== PLUS
)
2055 oberon_assert_token(ctx
, PLUS
);
2057 else if(ctx
-> token
== MINUS
)
2060 oberon_assert_token(ctx
, MINUS
);
2063 expr
= oberon_term_expr(ctx
);
2067 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2070 while(ISADDOP(ctx
-> token
))
2072 int token
= ctx
-> token
;
2073 oberon_read_token(ctx
);
2075 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2076 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2082 #define ISRELATION(x) \
2083 ((x) >= EQUAL && (x) <= IS)
2085 static oberon_expr_t
*
2086 oberon_expr(oberon_context_t
* ctx
)
2088 oberon_expr_t
* expr
;
2090 expr
= oberon_simple_expr(ctx
);
2091 while(ISRELATION(ctx
-> token
))
2093 int token
= ctx
-> token
;
2094 oberon_read_token(ctx
);
2096 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2097 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2103 static oberon_item_t
*
2104 oberon_const_expr(oberon_context_t
* ctx
)
2106 oberon_expr_t
* expr
;
2107 expr
= oberon_expr(ctx
);
2109 if(expr
-> is_item
== 0)
2111 oberon_error(ctx
, "const expression are required");
2114 switch(expr
-> item
.mode
)
2126 oberon_error(ctx
, "const expression are required");
2130 return (oberon_item_t
*) expr
;
2133 // =======================================================================
2135 // =======================================================================
2137 static void oberon_decl_seq(oberon_context_t
* ctx
);
2138 static void oberon_statement_seq(oberon_context_t
* ctx
);
2139 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2142 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2144 if(ctx
-> token
!= token
)
2146 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2151 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2153 oberon_expect_token(ctx
, token
);
2154 oberon_read_token(ctx
);
2158 oberon_assert_ident(oberon_context_t
* ctx
)
2160 oberon_expect_token(ctx
, IDENT
);
2161 char * ident
= ctx
-> string
;
2162 oberon_read_token(ctx
);
2167 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2169 switch(ctx
-> token
)
2172 oberon_assert_token(ctx
, STAR
);
2177 oberon_assert_token(ctx
, MINUS
);
2188 static oberon_object_t
*
2189 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2194 oberon_object_t
* x
;
2196 name
= oberon_assert_ident(ctx
);
2197 oberon_def(ctx
, &export
, &read_only
);
2199 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2204 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2207 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2208 while(ctx
-> token
== COMMA
)
2210 oberon_assert_token(ctx
, COMMA
);
2211 oberon_ident_def(ctx
, class, check_upscope
);
2217 oberon_var_decl(oberon_context_t
* ctx
)
2220 oberon_object_t
* list
;
2221 oberon_type_t
* type
;
2222 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2224 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2225 oberon_assert_token(ctx
, COLON
);
2226 oberon_type(ctx
, &type
);
2228 oberon_object_t
* var
= list
;
2229 for(int i
= 0; i
< num
; i
++)
2236 static oberon_object_t
*
2237 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2239 int class = OBERON_CLASS_PARAM
;
2240 if(ctx
-> token
== VAR
)
2242 oberon_read_token(ctx
);
2243 class = OBERON_CLASS_VAR_PARAM
;
2247 oberon_object_t
* list
;
2248 oberon_ident_list(ctx
, class, false, &num
, &list
);
2250 oberon_assert_token(ctx
, COLON
);
2252 oberon_type_t
* type
;
2253 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2254 oberon_type(ctx
, &type
);
2256 oberon_object_t
* param
= list
;
2257 for(int i
= 0; i
< num
; i
++)
2259 param
-> type
= type
;
2260 param
= param
-> next
;
2267 #define ISFPSECTION \
2268 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2271 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2273 oberon_assert_token(ctx
, LPAREN
);
2277 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2278 while(ctx
-> token
== SEMICOLON
)
2280 oberon_assert_token(ctx
, SEMICOLON
);
2281 oberon_fp_section(ctx
, &signature
-> num_decl
);
2285 oberon_assert_token(ctx
, RPAREN
);
2287 if(ctx
-> token
== COLON
)
2289 oberon_assert_token(ctx
, COLON
);
2291 oberon_object_t
* typeobj
;
2292 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2293 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2295 oberon_error(ctx
, "function result is not type");
2297 signature
-> base
= typeobj
-> type
;
2302 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2304 oberon_type_t
* signature
;
2306 signature
-> class = OBERON_TYPE_PROCEDURE
;
2307 signature
-> num_decl
= 0;
2308 signature
-> base
= ctx
-> void_type
;
2309 signature
-> decl
= NULL
;
2311 if(ctx
-> token
== LPAREN
)
2313 oberon_formal_pars(ctx
, signature
);
2318 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2320 if(a
-> num_decl
!= b
-> num_decl
)
2322 oberon_error(ctx
, "number parameters not matched");
2325 int num_param
= a
-> num_decl
;
2326 oberon_object_t
* param_a
= a
-> decl
;
2327 oberon_object_t
* param_b
= b
-> decl
;
2328 for(int i
= 0; i
< num_param
; i
++)
2330 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2332 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2335 if(param_a
-> type
!= param_b
-> type
)
2337 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2340 param_a
= param_a
-> next
;
2341 param_b
= param_b
-> next
;
2346 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2348 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2349 oberon_type_t
* result_type
= proc
-> type
-> base
;
2351 if(result_type
-> class == OBERON_TYPE_VOID
)
2355 oberon_error(ctx
, "procedure has no result type");
2362 oberon_error(ctx
, "procedure requires expression on result");
2365 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2368 proc
-> has_return
= 1;
2370 oberon_generate_return(ctx
, expr
);
2374 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2376 oberon_assert_token(ctx
, SEMICOLON
);
2378 ctx
-> decl
= proc
-> scope
;
2380 oberon_decl_seq(ctx
);
2382 oberon_generate_begin_proc(ctx
, proc
);
2384 if(ctx
-> token
== BEGIN
)
2386 oberon_assert_token(ctx
, BEGIN
);
2387 oberon_statement_seq(ctx
);
2390 oberon_assert_token(ctx
, END
);
2391 char * name
= oberon_assert_ident(ctx
);
2392 if(strcmp(name
, proc
-> name
) != 0)
2394 oberon_error(ctx
, "procedure name not matched");
2397 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2398 && proc
-> has_return
== 0)
2400 oberon_make_return(ctx
, NULL
);
2403 if(proc
-> has_return
== 0)
2405 oberon_error(ctx
, "procedure requires return");
2408 oberon_generate_end_proc(ctx
);
2409 oberon_close_scope(ctx
-> decl
);
2413 oberon_proc_decl(oberon_context_t
* ctx
)
2415 oberon_assert_token(ctx
, PROCEDURE
);
2418 if(ctx
-> token
== UPARROW
)
2420 oberon_assert_token(ctx
, UPARROW
);
2427 name
= oberon_assert_ident(ctx
);
2428 oberon_def(ctx
, &export
, &read_only
);
2430 oberon_scope_t
* proc_scope
;
2431 proc_scope
= oberon_open_scope(ctx
);
2432 ctx
-> decl
-> local
= 1;
2434 oberon_type_t
* signature
;
2435 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2436 oberon_opt_formal_pars(ctx
, &signature
);
2438 oberon_initialize_decl(ctx
);
2439 oberon_generator_init_type(ctx
, signature
);
2440 oberon_close_scope(ctx
-> decl
);
2442 oberon_object_t
* proc
;
2443 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2446 if(proc
-> class != OBERON_CLASS_PROC
)
2448 oberon_error(ctx
, "mult definition");
2455 oberon_error(ctx
, "mult procedure definition");
2459 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2461 oberon_error(ctx
, "export type not matched");
2464 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2468 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2469 proc
-> type
= signature
;
2470 proc
-> scope
= proc_scope
;
2471 oberon_generator_init_proc(ctx
, proc
);
2474 proc
-> scope
-> parent
= proc
;
2479 oberon_proc_decl_body(ctx
, proc
);
2484 oberon_const_decl(oberon_context_t
* ctx
)
2486 oberon_item_t
* value
;
2487 oberon_object_t
* constant
;
2489 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2490 oberon_assert_token(ctx
, EQUAL
);
2491 value
= oberon_const_expr(ctx
);
2492 constant
-> value
= value
;
2496 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2498 if(size
-> is_item
== 0)
2500 oberon_error(ctx
, "requires constant");
2503 if(size
-> item
.mode
!= MODE_INTEGER
)
2505 oberon_error(ctx
, "requires integer constant");
2508 oberon_type_t
* arr
;
2510 arr
-> class = OBERON_TYPE_ARRAY
;
2511 arr
-> size
= size
-> item
.integer
;
2516 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2519 oberon_object_t
* to
;
2521 to
= oberon_qualident(ctx
, &name
, 0);
2523 //name = oberon_assert_ident(ctx);
2524 //to = oberon_find_object(ctx -> decl, name, 0);
2528 if(to
-> class != OBERON_CLASS_TYPE
)
2530 oberon_error(ctx
, "not a type");
2535 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2536 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2542 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2545 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2549 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2557 oberon_type_t
* dim
;
2558 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2560 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2562 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2566 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2568 type
-> class = OBERON_TYPE_ARRAY
;
2570 type
-> base
= base
;
2574 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2576 if(ctx
-> token
== IDENT
)
2579 oberon_object_t
* list
;
2580 oberon_type_t
* type
;
2581 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2583 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2584 oberon_assert_token(ctx
, COLON
);
2586 oberon_scope_t
* current
= ctx
-> decl
;
2587 ctx
-> decl
= modscope
;
2588 oberon_type(ctx
, &type
);
2589 ctx
-> decl
= current
;
2591 oberon_object_t
* field
= list
;
2592 for(int i
= 0; i
< num
; i
++)
2594 field
-> type
= type
;
2595 field
= field
-> next
;
2598 rec
-> num_decl
+= num
;
2603 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2605 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2606 oberon_scope_t
* oldscope
= ctx
-> decl
;
2607 ctx
-> decl
= modscope
;
2609 if(ctx
-> token
== LPAREN
)
2611 oberon_assert_token(ctx
, LPAREN
);
2613 oberon_object_t
* typeobj
;
2614 typeobj
= oberon_qualident(ctx
, NULL
, true);
2616 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2618 oberon_error(ctx
, "base must be type");
2621 oberon_type_t
* base
= typeobj
-> type
;
2622 if(base
-> class == OBERON_TYPE_POINTER
)
2624 base
= base
-> base
;
2627 if(base
-> class != OBERON_TYPE_RECORD
)
2629 oberon_error(ctx
, "base must be record type");
2633 ctx
-> decl
= base
-> scope
;
2635 oberon_assert_token(ctx
, RPAREN
);
2642 oberon_scope_t
* this_scope
;
2643 this_scope
= oberon_open_scope(ctx
);
2644 this_scope
-> local
= true;
2645 this_scope
-> parent
= NULL
;
2646 this_scope
-> parent_type
= rec
;
2648 oberon_field_list(ctx
, rec
, modscope
);
2649 while(ctx
-> token
== SEMICOLON
)
2651 oberon_assert_token(ctx
, SEMICOLON
);
2652 oberon_field_list(ctx
, rec
, modscope
);
2655 rec
-> scope
= this_scope
;
2656 rec
-> decl
= this_scope
-> list
-> next
;
2657 ctx
-> decl
= oldscope
;
2661 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2663 if(ctx
-> token
== IDENT
)
2665 oberon_qualident_type(ctx
, type
);
2667 else if(ctx
-> token
== ARRAY
)
2669 oberon_assert_token(ctx
, ARRAY
);
2672 oberon_expr_t
* sizes
;
2674 if(ISEXPR(ctx
-> token
))
2676 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2679 oberon_assert_token(ctx
, OF
);
2681 oberon_type_t
* base
;
2682 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2683 oberon_type(ctx
, &base
);
2687 oberon_make_open_array(ctx
, base
, *type
);
2691 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2694 else if(ctx
-> token
== RECORD
)
2696 oberon_type_t
* rec
;
2698 rec
-> class = OBERON_TYPE_RECORD
;
2699 rec
-> module
= ctx
-> mod
;
2701 oberon_assert_token(ctx
, RECORD
);
2702 oberon_type_record_body(ctx
, rec
);
2703 oberon_assert_token(ctx
, END
);
2707 else if(ctx
-> token
== POINTER
)
2709 oberon_assert_token(ctx
, POINTER
);
2710 oberon_assert_token(ctx
, TO
);
2712 oberon_type_t
* base
;
2713 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2714 oberon_type(ctx
, &base
);
2716 oberon_type_t
* ptr
;
2718 ptr
-> class = OBERON_TYPE_POINTER
;
2721 else if(ctx
-> token
== PROCEDURE
)
2723 oberon_open_scope(ctx
);
2724 oberon_assert_token(ctx
, PROCEDURE
);
2725 oberon_opt_formal_pars(ctx
, type
);
2726 oberon_close_scope(ctx
-> decl
);
2730 oberon_error(ctx
, "invalid type declaration");
2735 oberon_type_decl(oberon_context_t
* ctx
)
2738 oberon_object_t
* newtype
;
2739 oberon_type_t
* type
;
2743 name
= oberon_assert_ident(ctx
);
2744 oberon_def(ctx
, &export
, &read_only
);
2746 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2749 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2750 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2751 assert(newtype
-> type
);
2755 if(newtype
-> class != OBERON_CLASS_TYPE
)
2757 oberon_error(ctx
, "mult definition");
2760 if(newtype
-> linked
)
2762 oberon_error(ctx
, "mult definition - already linked");
2765 newtype
-> export
= export
;
2766 newtype
-> read_only
= read_only
;
2769 oberon_assert_token(ctx
, EQUAL
);
2771 type
= newtype
-> type
;
2772 oberon_type(ctx
, &type
);
2774 if(type
-> class == OBERON_TYPE_VOID
)
2776 oberon_error(ctx
, "recursive alias declaration");
2779 newtype
-> type
= type
;
2780 newtype
-> linked
= 1;
2783 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2784 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2787 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2789 if(type
-> class != OBERON_TYPE_POINTER
2790 && type
-> class != OBERON_TYPE_ARRAY
)
2795 if(type
-> recursive
)
2797 oberon_error(ctx
, "recursive pointer declaration");
2800 if(type
-> class == OBERON_TYPE_POINTER
2801 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2803 oberon_error(ctx
, "attempt to make pointer to pointer");
2806 type
-> recursive
= 1;
2808 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2810 type
-> recursive
= 0;
2814 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2816 if(type
-> class != OBERON_TYPE_RECORD
)
2821 if(type
-> recursive
)
2823 oberon_error(ctx
, "recursive record declaration");
2826 type
-> recursive
= 1;
2828 int num_fields
= type
-> num_decl
;
2829 oberon_object_t
* field
= type
-> decl
;
2830 for(int i
= 0; i
< num_fields
; i
++)
2832 oberon_prevent_recursive_object(ctx
, field
);
2833 field
= field
-> next
;
2836 type
-> recursive
= 0;
2839 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2841 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2846 if(type
-> recursive
)
2848 oberon_error(ctx
, "recursive procedure declaration");
2851 type
-> recursive
= 1;
2853 int num_fields
= type
-> num_decl
;
2854 oberon_object_t
* field
= type
-> decl
;
2855 for(int i
= 0; i
< num_fields
; i
++)
2857 oberon_prevent_recursive_object(ctx
, field
);
2858 field
= field
-> next
;
2861 type
-> recursive
= 0;
2865 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2867 if(type
-> class != OBERON_TYPE_ARRAY
)
2872 if(type
-> recursive
)
2874 oberon_error(ctx
, "recursive array declaration");
2877 type
-> recursive
= 1;
2879 oberon_prevent_recursive_type(ctx
, type
-> base
);
2881 type
-> recursive
= 0;
2885 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2887 if(type
-> class == OBERON_TYPE_POINTER
)
2889 oberon_prevent_recursive_pointer(ctx
, type
);
2891 else if(type
-> class == OBERON_TYPE_RECORD
)
2893 oberon_prevent_recursive_record(ctx
, type
);
2895 else if(type
-> class == OBERON_TYPE_ARRAY
)
2897 oberon_prevent_recursive_array(ctx
, type
);
2899 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2901 oberon_prevent_recursive_procedure(ctx
, type
);
2906 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2910 case OBERON_CLASS_VAR
:
2911 case OBERON_CLASS_TYPE
:
2912 case OBERON_CLASS_PARAM
:
2913 case OBERON_CLASS_VAR_PARAM
:
2914 case OBERON_CLASS_FIELD
:
2915 oberon_prevent_recursive_type(ctx
, x
-> type
);
2917 case OBERON_CLASS_CONST
:
2918 case OBERON_CLASS_PROC
:
2919 case OBERON_CLASS_MODULE
:
2922 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2928 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2930 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2934 oberon_prevent_recursive_object(ctx
, x
);
2939 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2940 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2943 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2945 if(type
-> class != OBERON_TYPE_RECORD
)
2950 int num_fields
= type
-> num_decl
;
2951 oberon_object_t
* field
= type
-> decl
;
2952 for(int i
= 0; i
< num_fields
; i
++)
2954 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2956 oberon_initialize_type(ctx
, field
-> type
);
2959 oberon_initialize_object(ctx
, field
);
2960 field
= field
-> next
;
2963 oberon_generator_init_record(ctx
, type
);
2967 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2969 if(type
-> class == OBERON_TYPE_VOID
)
2971 oberon_error(ctx
, "undeclarated type");
2974 if(type
-> initialized
)
2979 type
-> initialized
= 1;
2981 if(type
-> class == OBERON_TYPE_POINTER
)
2983 oberon_initialize_type(ctx
, type
-> base
);
2984 oberon_generator_init_type(ctx
, type
);
2986 else if(type
-> class == OBERON_TYPE_ARRAY
)
2988 if(type
-> size
!= 0)
2990 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2992 if(type
-> base
-> size
== 0)
2994 oberon_error(ctx
, "open array not allowed as array element");
2999 oberon_initialize_type(ctx
, type
-> base
);
3000 oberon_generator_init_type(ctx
, type
);
3002 else if(type
-> class == OBERON_TYPE_RECORD
)
3004 oberon_generator_init_type(ctx
, type
);
3005 oberon_initialize_record_fields(ctx
, type
);
3007 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3009 int num_fields
= type
-> num_decl
;
3010 oberon_object_t
* field
= type
-> decl
;
3011 for(int i
= 0; i
< num_fields
; i
++)
3013 oberon_initialize_object(ctx
, field
);
3014 field
= field
-> next
;
3017 oberon_generator_init_type(ctx
, type
);
3021 oberon_generator_init_type(ctx
, type
);
3026 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3028 if(x
-> initialized
)
3033 x
-> initialized
= 1;
3037 case OBERON_CLASS_TYPE
:
3038 oberon_initialize_type(ctx
, x
-> type
);
3040 case OBERON_CLASS_VAR
:
3041 case OBERON_CLASS_FIELD
:
3042 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3044 if(x
-> type
-> size
== 0)
3046 oberon_error(ctx
, "open array not allowed as variable or field");
3049 oberon_initialize_type(ctx
, x
-> type
);
3050 oberon_generator_init_var(ctx
, x
);
3052 case OBERON_CLASS_PARAM
:
3053 case OBERON_CLASS_VAR_PARAM
:
3054 oberon_initialize_type(ctx
, x
-> type
);
3055 oberon_generator_init_var(ctx
, x
);
3057 case OBERON_CLASS_CONST
:
3058 case OBERON_CLASS_PROC
:
3059 case OBERON_CLASS_MODULE
:
3062 oberon_error(ctx
, "oberon_initialize_object: wat");
3068 oberon_initialize_decl(oberon_context_t
* ctx
)
3070 oberon_object_t
* x
= ctx
-> decl
-> list
;
3074 oberon_initialize_object(ctx
, x
-> next
);
3080 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3082 oberon_object_t
* x
= ctx
-> decl
-> list
;
3086 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3088 if(x
-> next
-> linked
== 0)
3090 oberon_error(ctx
, "unresolved forward declaration");
3098 oberon_decl_seq(oberon_context_t
* ctx
)
3100 if(ctx
-> token
== CONST
)
3102 oberon_assert_token(ctx
, CONST
);
3103 while(ctx
-> token
== IDENT
)
3105 oberon_const_decl(ctx
);
3106 oberon_assert_token(ctx
, SEMICOLON
);
3110 if(ctx
-> token
== TYPE
)
3112 oberon_assert_token(ctx
, TYPE
);
3113 while(ctx
-> token
== IDENT
)
3115 oberon_type_decl(ctx
);
3116 oberon_assert_token(ctx
, SEMICOLON
);
3120 if(ctx
-> token
== VAR
)
3122 oberon_assert_token(ctx
, VAR
);
3123 while(ctx
-> token
== IDENT
)
3125 oberon_var_decl(ctx
);
3126 oberon_assert_token(ctx
, SEMICOLON
);
3130 oberon_prevent_recursive_decl(ctx
);
3131 oberon_initialize_decl(ctx
);
3133 while(ctx
-> token
== PROCEDURE
)
3135 oberon_proc_decl(ctx
);
3136 oberon_assert_token(ctx
, SEMICOLON
);
3139 oberon_prevent_undeclarated_procedures(ctx
);
3142 static oberon_expr_t
*
3143 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3145 oberon_object_t
* x
;
3146 oberon_expr_t
* expr
;
3148 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3151 oberon_generator_init_temp_var(ctx
, x
);
3153 expr
= oberon_new_item(MODE_VAR
, type
, false);
3154 expr
-> item
.var
= x
;
3159 oberon_statement_seq(oberon_context_t
* ctx
);
3162 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3164 if(dst
-> read_only
)
3166 oberon_error(ctx
, "read-only destination");
3169 oberon_check_dst(ctx
, dst
);
3170 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3171 oberon_generate_assign(ctx
, src
, dst
);
3175 oberon_statement(oberon_context_t
* ctx
)
3177 oberon_expr_t
* item1
;
3178 oberon_expr_t
* item2
;
3180 if(ctx
-> token
== IDENT
)
3182 item1
= oberon_designator(ctx
);
3183 if(ctx
-> token
== ASSIGN
)
3185 oberon_assert_token(ctx
, ASSIGN
);
3186 item2
= oberon_expr(ctx
);
3187 oberon_assign(ctx
, item2
, item1
);
3191 oberon_opt_proc_parens(ctx
, item1
);
3194 else if(ctx
-> token
== IF
)
3198 oberon_expr_t
* cond
;
3200 els
= oberon_generator_reserve_label(ctx
);
3201 end
= oberon_generator_reserve_label(ctx
);
3203 oberon_assert_token(ctx
, IF
);
3204 cond
= oberon_expr(ctx
);
3205 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3207 oberon_error(ctx
, "condition must be boolean");
3209 oberon_assert_token(ctx
, THEN
);
3210 oberon_generate_branch(ctx
, cond
, false, els
);
3211 oberon_statement_seq(ctx
);
3212 oberon_generate_goto(ctx
, end
);
3213 oberon_generate_label(ctx
, els
);
3215 while(ctx
-> token
== ELSIF
)
3217 els
= oberon_generator_reserve_label(ctx
);
3219 oberon_assert_token(ctx
, ELSIF
);
3220 cond
= oberon_expr(ctx
);
3221 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3223 oberon_error(ctx
, "condition must be boolean");
3225 oberon_assert_token(ctx
, THEN
);
3226 oberon_generate_branch(ctx
, cond
, false, els
);
3227 oberon_statement_seq(ctx
);
3228 oberon_generate_goto(ctx
, end
);
3229 oberon_generate_label(ctx
, els
);
3232 if(ctx
-> token
== ELSE
)
3234 oberon_assert_token(ctx
, ELSE
);
3235 oberon_statement_seq(ctx
);
3238 oberon_generate_label(ctx
, end
);
3239 oberon_assert_token(ctx
, END
);
3241 else if(ctx
-> token
== WHILE
)
3243 gen_label_t
* begin
;
3245 oberon_expr_t
* cond
;
3247 begin
= oberon_generator_reserve_label(ctx
);
3248 end
= oberon_generator_reserve_label(ctx
);
3250 oberon_assert_token(ctx
, WHILE
);
3251 oberon_generate_label(ctx
, begin
);
3252 cond
= oberon_expr(ctx
);
3253 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3255 oberon_error(ctx
, "condition must be boolean");
3257 oberon_generate_branch(ctx
, cond
, false, end
);
3259 oberon_assert_token(ctx
, DO
);
3260 oberon_statement_seq(ctx
);
3261 oberon_generate_goto(ctx
, begin
);
3263 oberon_assert_token(ctx
, END
);
3264 oberon_generate_label(ctx
, end
);
3266 else if(ctx
-> token
== REPEAT
)
3268 gen_label_t
* begin
;
3269 oberon_expr_t
* cond
;
3271 begin
= oberon_generator_reserve_label(ctx
);
3272 oberon_generate_label(ctx
, begin
);
3273 oberon_assert_token(ctx
, REPEAT
);
3275 oberon_statement_seq(ctx
);
3277 oberon_assert_token(ctx
, UNTIL
);
3279 cond
= oberon_expr(ctx
);
3280 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3282 oberon_error(ctx
, "condition must be boolean");
3285 oberon_generate_branch(ctx
, cond
, true, begin
);
3287 else if(ctx
-> token
== FOR
)
3289 oberon_expr_t
* from
;
3290 oberon_expr_t
* index
;
3292 oberon_expr_t
* bound
;
3294 oberon_expr_t
* cond
;
3295 oberon_expr_t
* count
;
3296 gen_label_t
* begin
;
3301 begin
= oberon_generator_reserve_label(ctx
);
3302 end
= oberon_generator_reserve_label(ctx
);
3304 oberon_assert_token(ctx
, FOR
);
3305 iname
= oberon_assert_ident(ctx
);
3306 index
= oberon_ident_item(ctx
, iname
);
3307 oberon_assert_token(ctx
, ASSIGN
);
3308 from
= oberon_expr(ctx
);
3309 oberon_assign(ctx
, from
, index
);
3310 oberon_assert_token(ctx
, TO
);
3311 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3312 to
= oberon_expr(ctx
);
3313 oberon_assign(ctx
, to
, bound
);
3314 if(ctx
-> token
== BY
)
3316 oberon_assert_token(ctx
, BY
);
3317 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3321 by
= oberon_integer_item(ctx
, 1);
3324 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3326 oberon_error(ctx
, "must be integer");
3329 if(by
-> item
.integer
> 0)
3333 else if(by
-> item
.integer
< 0)
3339 oberon_error(ctx
, "zero step not allowed");
3342 oberon_assert_token(ctx
, DO
);
3343 oberon_generate_label(ctx
, begin
);
3344 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3345 oberon_generate_branch(ctx
, cond
, false, end
);
3346 oberon_statement_seq(ctx
);
3347 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3348 oberon_assign(ctx
, count
, index
);
3349 oberon_generate_goto(ctx
, begin
);
3350 oberon_generate_label(ctx
, end
);
3351 oberon_assert_token(ctx
, END
);
3353 else if(ctx
-> token
== LOOP
)
3355 gen_label_t
* begin
;
3358 begin
= oberon_generator_reserve_label(ctx
);
3359 end
= oberon_generator_reserve_label(ctx
);
3361 oberon_open_scope(ctx
);
3362 oberon_assert_token(ctx
, LOOP
);
3363 oberon_generate_label(ctx
, begin
);
3364 ctx
-> decl
-> exit_label
= end
;
3365 oberon_statement_seq(ctx
);
3366 oberon_generate_goto(ctx
, begin
);
3367 oberon_generate_label(ctx
, end
);
3368 oberon_assert_token(ctx
, END
);
3369 oberon_close_scope(ctx
-> decl
);
3371 else if(ctx
-> token
== EXIT
)
3373 oberon_assert_token(ctx
, EXIT
);
3374 if(ctx
-> decl
-> exit_label
== NULL
)
3376 oberon_error(ctx
, "not in LOOP-END");
3378 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3380 else if(ctx
-> token
== RETURN
)
3382 oberon_assert_token(ctx
, RETURN
);
3383 if(ISEXPR(ctx
-> token
))
3385 oberon_expr_t
* expr
;
3386 expr
= oberon_expr(ctx
);
3387 oberon_make_return(ctx
, expr
);
3391 oberon_make_return(ctx
, NULL
);
3397 oberon_statement_seq(oberon_context_t
* ctx
)
3399 oberon_statement(ctx
);
3400 while(ctx
-> token
== SEMICOLON
)
3402 oberon_assert_token(ctx
, SEMICOLON
);
3403 oberon_statement(ctx
);
3408 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3410 oberon_module_t
* m
= ctx
-> module_list
;
3411 while(m
&& strcmp(m
-> name
, name
) != 0)
3419 code
= ctx
-> import_module(name
);
3422 oberon_error(ctx
, "no such module");
3425 m
= oberon_compile_module(ctx
, code
);
3431 oberon_error(ctx
, "cyclic module import");
3434 oberon_object_t
* ident
;
3435 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3436 ident
-> module
= m
;
3440 oberon_import_decl(oberon_context_t
* ctx
)
3445 alias
= name
= oberon_assert_ident(ctx
);
3446 if(ctx
-> token
== ASSIGN
)
3448 oberon_assert_token(ctx
, ASSIGN
);
3449 name
= oberon_assert_ident(ctx
);
3452 oberon_import_module(ctx
, alias
, name
);
3456 oberon_import_list(oberon_context_t
* ctx
)
3458 oberon_assert_token(ctx
, IMPORT
);
3460 oberon_import_decl(ctx
);
3461 while(ctx
-> token
== COMMA
)
3463 oberon_assert_token(ctx
, COMMA
);
3464 oberon_import_decl(ctx
);
3467 oberon_assert_token(ctx
, SEMICOLON
);
3471 oberon_parse_module(oberon_context_t
* ctx
)
3475 oberon_read_token(ctx
);
3477 oberon_assert_token(ctx
, MODULE
);
3478 name1
= oberon_assert_ident(ctx
);
3479 oberon_assert_token(ctx
, SEMICOLON
);
3480 ctx
-> mod
-> name
= name1
;
3482 oberon_generator_init_module(ctx
, ctx
-> mod
);
3484 if(ctx
-> token
== IMPORT
)
3486 oberon_import_list(ctx
);
3489 oberon_decl_seq(ctx
);
3491 oberon_generate_begin_module(ctx
);
3492 if(ctx
-> token
== BEGIN
)
3494 oberon_assert_token(ctx
, BEGIN
);
3495 oberon_statement_seq(ctx
);
3497 oberon_generate_end_module(ctx
);
3499 oberon_assert_token(ctx
, END
);
3500 name2
= oberon_assert_ident(ctx
);
3501 oberon_assert_token(ctx
, DOT
);
3503 if(strcmp(name1
, name2
) != 0)
3505 oberon_error(ctx
, "module name not matched");
3508 oberon_generator_fini_module(ctx
-> mod
);
3511 // =======================================================================
3513 // =======================================================================
3516 register_default_types(oberon_context_t
* ctx
)
3518 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3519 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3521 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3522 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3523 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3525 ctx
-> string_type
= oberon_new_type_string(1);
3526 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3528 ctx
-> bool_type
= oberon_new_type_boolean();
3529 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3531 ctx
-> byte_type
= oberon_new_type_integer(1);
3532 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3534 ctx
-> shortint_type
= oberon_new_type_integer(2);
3535 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3537 ctx
-> int_type
= oberon_new_type_integer(4);
3538 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3540 ctx
-> longint_type
= oberon_new_type_integer(8);
3541 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3543 ctx
-> real_type
= oberon_new_type_real(4);
3544 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3546 ctx
-> longreal_type
= oberon_new_type_real(8);
3547 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3549 ctx
-> char_type
= oberon_new_type_char(1);
3550 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3552 ctx
-> set_type
= oberon_new_type_set(4);
3553 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3557 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3559 oberon_object_t
* proc
;
3560 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3561 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3562 proc
-> type
-> sysproc
= true;
3563 proc
-> type
-> genfunc
= f
;
3564 proc
-> type
-> genproc
= p
;
3567 static oberon_expr_t
*
3568 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3572 oberon_error(ctx
, "too few arguments");
3577 oberon_error(ctx
, "too mach arguments");
3580 oberon_expr_t
* arg
;
3583 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3585 oberon_error(ctx
, "MIN accept only type");
3588 oberon_expr_t
* expr
;
3589 int bits
= arg
-> result
-> size
* 8;
3590 switch(arg
-> result
-> class)
3592 case OBERON_TYPE_INTEGER
:
3593 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3595 case OBERON_TYPE_SET
:
3596 expr
= oberon_integer_item(ctx
, 0);
3599 oberon_error(ctx
, "allowed only basic types");
3606 static oberon_expr_t
*
3607 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3611 oberon_error(ctx
, "too few arguments");
3616 oberon_error(ctx
, "too mach arguments");
3619 oberon_expr_t
* arg
;
3622 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3624 oberon_error(ctx
, "MAX accept only type");
3627 oberon_expr_t
* expr
;
3628 int bits
= arg
-> result
-> size
* 8;
3629 switch(arg
-> result
-> class)
3631 case OBERON_TYPE_INTEGER
:
3632 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3634 case OBERON_TYPE_SET
:
3635 expr
= oberon_integer_item(ctx
, bits
);
3638 oberon_error(ctx
, "allowed only basic types");
3645 static oberon_expr_t
*
3646 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3650 oberon_error(ctx
, "too few arguments");
3655 oberon_error(ctx
, "too mach arguments");
3658 oberon_expr_t
* arg
;
3661 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3663 oberon_error(ctx
, "SIZE accept only type");
3667 oberon_expr_t
* expr
;
3668 oberon_type_t
* type
= arg
-> result
;
3669 switch(type
-> class)
3671 case OBERON_TYPE_INTEGER
:
3672 case OBERON_TYPE_BOOLEAN
:
3673 case OBERON_TYPE_REAL
:
3674 case OBERON_TYPE_CHAR
:
3675 case OBERON_TYPE_SET
:
3676 size
= type
-> size
;
3679 oberon_error(ctx
, "TODO SIZE");
3683 expr
= oberon_integer_item(ctx
, size
);
3687 static oberon_expr_t
*
3688 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3692 oberon_error(ctx
, "too few arguments");
3697 oberon_error(ctx
, "too mach arguments");
3700 oberon_expr_t
* arg
;
3702 oberon_check_src(ctx
, arg
);
3704 oberon_type_t
* result_type
;
3705 result_type
= arg
-> result
;
3707 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3709 oberon_error(ctx
, "ABS accepts only integers");
3712 oberon_expr_t
* expr
;
3713 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3718 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3722 oberon_error(ctx
, "too few arguments");
3726 oberon_expr_t
* dst
;
3728 oberon_check_dst(ctx
, dst
);
3730 oberon_type_t
* type
;
3731 type
= dst
-> result
;
3733 if(type
-> class != OBERON_TYPE_POINTER
)
3735 oberon_error(ctx
, "not a pointer");
3738 type
= type
-> base
;
3740 oberon_expr_t
* src
;
3741 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3742 src
-> item
.num_args
= 0;
3743 src
-> item
.args
= NULL
;
3746 if(type
-> class == OBERON_TYPE_ARRAY
)
3748 if(type
-> size
== 0)
3750 oberon_type_t
* x
= type
;
3751 while(x
-> class == OBERON_TYPE_ARRAY
)
3761 if(num_args
< max_args
)
3763 oberon_error(ctx
, "too few arguments");
3766 if(num_args
> max_args
)
3768 oberon_error(ctx
, "too mach arguments");
3771 int num_sizes
= max_args
- 1;
3772 oberon_expr_t
* size_list
= list_args
-> next
;
3774 oberon_expr_t
* arg
= size_list
;
3775 for(int i
= 0; i
< max_args
- 1; i
++)
3777 oberon_check_src(ctx
, arg
);
3778 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3780 oberon_error(ctx
, "size must be integer");
3785 src
-> item
.num_args
= num_sizes
;
3786 src
-> item
.args
= size_list
;
3788 else if(type
-> class != OBERON_TYPE_RECORD
)
3790 oberon_error(ctx
, "oberon_make_new_call: wat");
3793 if(num_args
> max_args
)
3795 oberon_error(ctx
, "too mach arguments");
3798 oberon_assign(ctx
, src
, dst
);
3802 oberon_create_context(ModuleImportCallback import_module
)
3804 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3806 oberon_scope_t
* world_scope
;
3807 world_scope
= oberon_open_scope(ctx
);
3808 ctx
-> world_scope
= world_scope
;
3810 ctx
-> import_module
= import_module
;
3812 oberon_generator_init_context(ctx
);
3814 register_default_types(ctx
);
3817 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3818 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
3819 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
3820 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
3823 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3829 oberon_destroy_context(oberon_context_t
* ctx
)
3831 oberon_generator_destroy_context(ctx
);
3836 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3838 const char * code
= ctx
-> code
;
3839 int code_index
= ctx
-> code_index
;
3841 int token
= ctx
-> token
;
3842 char * string
= ctx
-> string
;
3843 int integer
= ctx
-> integer
;
3844 int real
= ctx
-> real
;
3845 bool longmode
= ctx
-> longmode
;
3846 oberon_scope_t
* decl
= ctx
-> decl
;
3847 oberon_module_t
* mod
= ctx
-> mod
;
3849 oberon_scope_t
* module_scope
;
3850 module_scope
= oberon_open_scope(ctx
);
3852 oberon_module_t
* module
;
3853 module
= calloc(1, sizeof *module
);
3854 module
-> decl
= module_scope
;
3855 module
-> next
= ctx
-> module_list
;
3857 ctx
-> mod
= module
;
3858 ctx
-> module_list
= module
;
3860 oberon_init_scaner(ctx
, newcode
);
3861 oberon_parse_module(ctx
);
3863 module
-> ready
= 1;
3866 ctx
-> code_index
= code_index
;
3868 ctx
-> token
= token
;
3869 ctx
-> string
= string
;
3870 ctx
-> integer
= integer
;
3872 ctx
-> longmode
= longmode
;