10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
80 // =======================================================================
82 // =======================================================================
85 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
89 fprintf(stderr
, "error: ");
90 vfprintf(stderr
, fmt
, ptr
);
91 fprintf(stderr
, "\n");
92 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
93 fprintf(stderr
, " c = %c\n", ctx
-> c
);
94 fprintf(stderr
, " token = %i\n", ctx
-> token
);
99 static oberon_type_t
*
100 oberon_new_type_ptr(int class)
102 oberon_type_t
* x
= malloc(sizeof *x
);
103 memset(x
, 0, sizeof *x
);
108 static oberon_type_t
*
109 oberon_new_type_integer(int size
)
112 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
117 static oberon_type_t
*
118 oberon_new_type_boolean()
121 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
125 static oberon_type_t
*
126 oberon_new_type_real(int size
)
129 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
134 static oberon_type_t
*
135 oberon_new_type_char(int size
)
138 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
143 static oberon_type_t
*
144 oberon_new_type_string(int size
)
147 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
152 // =======================================================================
154 // =======================================================================
156 static oberon_scope_t
*
157 oberon_open_scope(oberon_context_t
* ctx
)
159 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
160 oberon_object_t
* list
= calloc(1, sizeof *list
);
163 scope
-> list
= list
;
164 scope
-> up
= ctx
-> decl
;
168 scope
-> local
= scope
-> up
-> local
;
169 scope
-> parent
= scope
-> up
-> parent
;
170 scope
-> parent_type
= scope
-> up
-> parent_type
;
171 scope
-> exit_label
= scope
-> up
-> exit_label
;
179 oberon_close_scope(oberon_scope_t
* scope
)
181 oberon_context_t
* ctx
= scope
-> ctx
;
182 ctx
-> decl
= scope
-> up
;
185 static oberon_object_t
*
186 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
188 oberon_object_t
* x
= list
;
189 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
196 static oberon_object_t
*
197 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
199 oberon_object_t
* result
= NULL
;
201 oberon_scope_t
* s
= scope
;
202 while(result
== NULL
&& s
!= NULL
)
204 result
= oberon_find_object_in_list(s
-> list
, name
);
208 if(check_it
&& result
== NULL
)
210 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
216 static oberon_object_t
*
217 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
219 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
220 memset(newvar
, 0, sizeof *newvar
);
221 newvar
-> name
= name
;
222 newvar
-> class = class;
223 newvar
-> export
= export
;
224 newvar
-> read_only
= read_only
;
225 newvar
-> local
= scope
-> local
;
226 newvar
-> parent
= scope
-> parent
;
227 newvar
-> parent_type
= scope
-> parent_type
;
228 newvar
-> module
= scope
-> ctx
-> mod
;
232 static oberon_object_t
*
233 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
237 if(oberon_find_object(scope
-> up
, name
, false))
239 oberon_error(scope
-> ctx
, "already defined");
243 oberon_object_t
* x
= scope
-> list
;
244 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
251 oberon_error(scope
-> ctx
, "already defined");
254 oberon_object_t
* newvar
;
255 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
261 static oberon_object_t
*
262 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
264 oberon_object_t
* id
;
265 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
267 oberon_generator_init_type(scope
-> ctx
, type
);
271 // =======================================================================
273 // =======================================================================
276 oberon_get_char(oberon_context_t
* ctx
)
278 if(ctx
-> code
[ctx
-> code_index
])
280 ctx
-> code_index
+= 1;
281 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
286 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
289 ctx
-> code_index
= 0;
290 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
294 oberon_read_ident(oberon_context_t
* ctx
)
297 int i
= ctx
-> code_index
;
299 int c
= ctx
-> code
[i
];
307 char * ident
= malloc(len
+ 1);
308 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
311 ctx
-> code_index
= i
;
312 ctx
-> c
= ctx
-> code
[i
];
313 ctx
-> string
= ident
;
314 ctx
-> token
= IDENT
;
316 if(strcmp(ident
, "MODULE") == 0)
318 ctx
-> token
= MODULE
;
320 else if(strcmp(ident
, "END") == 0)
324 else if(strcmp(ident
, "VAR") == 0)
328 else if(strcmp(ident
, "BEGIN") == 0)
330 ctx
-> token
= BEGIN
;
332 else if(strcmp(ident
, "TRUE") == 0)
336 else if(strcmp(ident
, "FALSE") == 0)
338 ctx
-> token
= FALSE
;
340 else if(strcmp(ident
, "OR") == 0)
344 else if(strcmp(ident
, "DIV") == 0)
348 else if(strcmp(ident
, "MOD") == 0)
352 else if(strcmp(ident
, "PROCEDURE") == 0)
354 ctx
-> token
= PROCEDURE
;
356 else if(strcmp(ident
, "RETURN") == 0)
358 ctx
-> token
= RETURN
;
360 else if(strcmp(ident
, "CONST") == 0)
362 ctx
-> token
= CONST
;
364 else if(strcmp(ident
, "TYPE") == 0)
368 else if(strcmp(ident
, "ARRAY") == 0)
370 ctx
-> token
= ARRAY
;
372 else if(strcmp(ident
, "OF") == 0)
376 else if(strcmp(ident
, "RECORD") == 0)
378 ctx
-> token
= RECORD
;
380 else if(strcmp(ident
, "POINTER") == 0)
382 ctx
-> token
= POINTER
;
384 else if(strcmp(ident
, "TO") == 0)
388 else if(strcmp(ident
, "NIL") == 0)
392 else if(strcmp(ident
, "IMPORT") == 0)
394 ctx
-> token
= IMPORT
;
396 else if(strcmp(ident
, "IN") == 0)
400 else if(strcmp(ident
, "IS") == 0)
404 else if(strcmp(ident
, "IF") == 0)
408 else if(strcmp(ident
, "THEN") == 0)
412 else if(strcmp(ident
, "ELSE") == 0)
416 else if(strcmp(ident
, "ELSIF") == 0)
418 ctx
-> token
= ELSIF
;
420 else if(strcmp(ident
, "WHILE") == 0)
422 ctx
-> token
= WHILE
;
424 else if(strcmp(ident
, "DO") == 0)
428 else if(strcmp(ident
, "REPEAT") == 0)
430 ctx
-> token
= REPEAT
;
432 else if(strcmp(ident
, "UNTIL") == 0)
434 ctx
-> token
= UNTIL
;
436 else if(strcmp(ident
, "FOR") == 0)
440 else if(strcmp(ident
, "BY") == 0)
444 else if(strcmp(ident
, "LOOP") == 0)
448 else if(strcmp(ident
, "EXIT") == 0)
455 oberon_read_number(oberon_context_t
* ctx
)
468 * mode = 3 == LONGREAL
472 start_i
= ctx
-> code_index
;
474 while(isdigit(ctx
-> c
))
476 oberon_get_char(ctx
);
479 end_i
= ctx
-> code_index
;
481 if(isxdigit(ctx
-> c
))
484 while(isxdigit(ctx
-> c
))
486 oberon_get_char(ctx
);
489 end_i
= ctx
-> code_index
;
494 oberon_get_char(ctx
);
496 else if(ctx
-> c
== 'X')
499 oberon_get_char(ctx
);
503 oberon_error(ctx
, "invalid hex number");
506 else if(ctx
-> c
== '.')
509 oberon_get_char(ctx
);
511 while(isdigit(ctx
-> c
))
513 oberon_get_char(ctx
);
516 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
518 exp_i
= ctx
-> code_index
;
525 oberon_get_char(ctx
);
527 if(ctx
-> c
== '+' || ctx
-> c
== '-')
529 oberon_get_char(ctx
);
532 while(isdigit(ctx
-> c
))
534 oberon_get_char(ctx
);
539 end_i
= ctx
-> code_index
;
547 oberon_get_char(ctx
);
549 else if(ctx
-> c
== 'X')
552 oberon_get_char(ctx
);
556 int len
= end_i
- start_i
;
557 ident
= malloc(len
+ 1);
558 memcpy(ident
, &ctx
-> code
[start_i
], len
);
561 ctx
-> longmode
= false;
564 int i
= exp_i
- start_i
;
566 ctx
-> longmode
= true;
572 integer
= atol(ident
);
574 ctx
-> token
= INTEGER
;
577 sscanf(ident
, "%lx", &integer
);
579 ctx
-> token
= INTEGER
;
583 sscanf(ident
, "%lf", &real
);
587 sscanf(ident
, "%lx", &integer
);
592 oberon_error(ctx
, "oberon_read_number: wat");
596 ctx
-> string
= ident
;
597 ctx
-> integer
= integer
;
602 oberon_skip_space(oberon_context_t
* ctx
)
604 while(isspace(ctx
-> c
))
606 oberon_get_char(ctx
);
611 oberon_read_comment(oberon_context_t
* ctx
)
618 oberon_get_char(ctx
);
621 oberon_get_char(ctx
);
625 else if(ctx
-> c
== '*')
627 oberon_get_char(ctx
);
630 oberon_get_char(ctx
);
634 else if(ctx
-> c
== 0)
636 oberon_error(ctx
, "unterminated comment");
640 oberon_get_char(ctx
);
645 static void oberon_read_string(oberon_context_t
* ctx
)
648 oberon_get_char(ctx
);
650 int start
= ctx
-> code_index
;
652 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
654 oberon_get_char(ctx
);
659 oberon_error(ctx
, "unterminated string");
662 int end
= ctx
-> code_index
;
664 oberon_get_char(ctx
);
666 char * string
= calloc(1, end
- start
+ 1);
667 strncpy(string
, &ctx
-> code
[start
], end
- start
);
669 ctx
-> token
= STRING
;
670 ctx
-> string
= string
;
672 printf("oberon_read_string: string ((%s))\n", string
);
675 static void oberon_read_token(oberon_context_t
* ctx
);
678 oberon_read_symbol(oberon_context_t
* ctx
)
687 ctx
-> token
= SEMICOLON
;
688 oberon_get_char(ctx
);
691 ctx
-> token
= COLON
;
692 oberon_get_char(ctx
);
695 ctx
-> token
= ASSIGN
;
696 oberon_get_char(ctx
);
701 oberon_get_char(ctx
);
704 ctx
-> token
= LPAREN
;
705 oberon_get_char(ctx
);
708 oberon_get_char(ctx
);
709 oberon_read_comment(ctx
);
710 oberon_read_token(ctx
);
714 ctx
-> token
= RPAREN
;
715 oberon_get_char(ctx
);
718 ctx
-> token
= EQUAL
;
719 oberon_get_char(ctx
);
723 oberon_get_char(ctx
);
727 oberon_get_char(ctx
);
731 oberon_get_char(ctx
);
735 ctx
-> token
= GREAT
;
736 oberon_get_char(ctx
);
740 oberon_get_char(ctx
);
745 oberon_get_char(ctx
);
748 ctx
-> token
= MINUS
;
749 oberon_get_char(ctx
);
753 oberon_get_char(ctx
);
756 oberon_get_char(ctx
);
757 oberon_error(ctx
, "unstarted comment");
761 ctx
-> token
= SLASH
;
762 oberon_get_char(ctx
);
766 oberon_get_char(ctx
);
770 oberon_get_char(ctx
);
773 ctx
-> token
= COMMA
;
774 oberon_get_char(ctx
);
777 ctx
-> token
= LBRACE
;
778 oberon_get_char(ctx
);
781 ctx
-> token
= RBRACE
;
782 oberon_get_char(ctx
);
785 ctx
-> token
= UPARROW
;
786 oberon_get_char(ctx
);
789 oberon_read_string(ctx
);
792 oberon_read_string(ctx
);
795 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
801 oberon_read_token(oberon_context_t
* ctx
)
803 oberon_skip_space(ctx
);
808 oberon_read_ident(ctx
);
812 oberon_read_number(ctx
);
816 oberon_read_symbol(ctx
);
820 // =======================================================================
822 // =======================================================================
824 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
825 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
826 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
827 static char * oberon_assert_ident(oberon_context_t
* ctx
);
828 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
829 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
830 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
832 static oberon_expr_t
*
833 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
835 oberon_oper_t
* operator;
836 operator = malloc(sizeof *operator);
837 memset(operator, 0, sizeof *operator);
839 operator -> is_item
= 0;
840 operator -> result
= result
;
841 operator -> read_only
= 1;
843 operator -> left
= left
;
844 operator -> right
= right
;
846 return (oberon_expr_t
*) operator;
849 static oberon_expr_t
*
850 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
852 oberon_item_t
* item
;
853 item
= malloc(sizeof *item
);
854 memset(item
, 0, sizeof *item
);
857 item
-> result
= result
;
858 item
-> read_only
= read_only
;
861 return (oberon_expr_t
*)item
;
864 static oberon_expr_t
*
865 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
867 oberon_expr_t
* expr
;
868 oberon_type_t
* result
;
870 result
= a
-> result
;
874 if(result
-> class != OBERON_TYPE_INTEGER
)
876 oberon_error(ctx
, "incompatible operator type");
879 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
881 else if(token
== NOT
)
883 if(result
-> class != OBERON_TYPE_BOOLEAN
)
885 oberon_error(ctx
, "incompatible operator type");
888 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
892 oberon_error(ctx
, "oberon_make_unary_op: wat");
899 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
901 oberon_expr_t
* last
;
906 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
910 *first
= last
= oberon_expr(ctx
);
912 while(ctx
-> token
== COMMA
)
914 oberon_assert_token(ctx
, COMMA
);
915 oberon_expr_t
* current
;
919 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
923 current
= oberon_expr(ctx
);
926 last
-> next
= current
;
932 static oberon_expr_t
*
933 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
935 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
938 static oberon_expr_t
*
939 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
941 oberon_type_t
* from
= expr
-> result
;
942 oberon_type_t
* to
= rec
;
944 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
946 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
948 printf("oberno_make_record_cast: pointers\n");
953 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
955 oberon_error(ctx
, "must be record type");
958 return oberon_cast_expr(ctx
, expr
, rec
);
961 static oberon_type_t
*
962 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
964 oberon_type_t
* result
;
965 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
969 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
973 else if(a
-> class != b
-> class)
975 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
977 else if(a
-> size
> b
-> size
)
990 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
992 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
998 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1000 oberon_error(ctx
, "not a record");
1003 oberon_type_t
* t
= from
;
1004 while(t
!= NULL
&& t
!= to
)
1011 oberon_error(ctx
, "incompatible record types");
1016 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1018 if(dst
-> is_item
== false)
1020 oberon_error(ctx
, "not variable");
1023 switch(dst
-> item
.mode
)
1034 oberon_error(ctx
, "not variable");
1040 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1044 if(src
-> item
.mode
== MODE_TYPE
)
1046 oberon_error(ctx
, "not variable");
1051 static oberon_expr_t
*
1052 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1055 // Если классы типов равны
1056 // Если INTEGER переводится в REAL
1057 // Есди STRING переводится в ARRAY OF CHAR
1059 oberon_check_src(ctx
, expr
);
1062 if(pref
-> class != expr
-> result
-> class)
1064 printf("expr class %i\n", expr
-> result
-> class);
1065 printf("pref class %i\n", pref
-> class);
1067 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1069 if(pref
-> class == OBERON_TYPE_ARRAY
)
1071 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1081 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1083 if(pref
-> class != OBERON_TYPE_REAL
)
1096 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1099 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1101 if(expr
-> result
-> size
> pref
-> size
)
1103 oberon_error(ctx
, "incompatible size");
1107 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1110 else if(pref
-> class == OBERON_TYPE_RECORD
)
1112 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1113 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1115 else if(pref
-> class == OBERON_TYPE_POINTER
)
1117 assert(pref
-> base
);
1118 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1120 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1121 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1123 else if(expr
-> result
-> base
!= pref
-> base
)
1125 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1127 oberon_error(ctx
, "incompatible pointer types");
1136 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1138 oberon_type_t
* a
= (*ea
) -> result
;
1139 oberon_type_t
* b
= (*eb
) -> result
;
1140 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1141 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1142 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1146 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1148 if(desig
-> mode
!= MODE_CALL
)
1150 oberon_error(ctx
, "expected mode CALL");
1153 oberon_type_t
* fn
= desig
-> parent
-> result
;
1154 int num_args
= desig
-> num_args
;
1155 int num_decl
= fn
-> num_decl
;
1157 if(num_args
< num_decl
)
1159 oberon_error(ctx
, "too few arguments");
1161 else if(num_args
> num_decl
)
1163 oberon_error(ctx
, "too many arguments");
1166 /* Делаем проверку на запись и делаем автокаст */
1167 oberon_expr_t
* casted
[num_args
];
1168 oberon_expr_t
* arg
= desig
-> args
;
1169 oberon_object_t
* param
= fn
-> decl
;
1170 for(int i
= 0; i
< num_args
; i
++)
1172 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1174 if(arg
-> read_only
)
1176 oberon_error(ctx
, "assign to read-only var");
1180 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1182 param
= param
-> next
;
1185 /* Создаём новый список выражений */
1189 for(int i
= 0; i
< num_args
- 1; i
++)
1191 casted
[i
] -> next
= casted
[i
+ 1];
1193 desig
-> args
= arg
;
1197 static oberon_expr_t
*
1198 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1200 oberon_type_t
* signature
= item
-> result
;
1201 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1203 oberon_error(ctx
, "not a procedure");
1206 oberon_expr_t
* call
;
1208 if(signature
-> sysproc
)
1210 if(signature
-> genfunc
== NULL
)
1212 oberon_error(ctx
, "not a function-procedure");
1215 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1219 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1221 oberon_error(ctx
, "attempt to call procedure in expression");
1224 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1225 call
-> item
.parent
= item
;
1226 call
-> item
.num_args
= num_args
;
1227 call
-> item
.args
= list_args
;
1228 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1235 oberon_make_call_proc(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
-> genproc
== NULL
)
1249 oberon_error(ctx
, "not a procedure");
1252 signature
-> genproc(ctx
, num_args
, list_args
);
1256 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1258 oberon_error(ctx
, "attempt to call function as non-typed procedure");
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
);
1266 oberon_generate_call_proc(ctx
, call
);
1274 || ((x) == INTEGER) \
1277 || ((x) == STRING) \
1279 || ((x) == LPAREN) \
1284 static oberon_expr_t
*
1285 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1287 printf("oberno_make_dereferencing\n");
1288 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1290 oberon_error(ctx
, "not a pointer");
1293 assert(expr
-> is_item
);
1295 oberon_expr_t
* selector
;
1296 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1297 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1302 static oberon_expr_t
*
1303 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1305 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1307 desig
= oberno_make_dereferencing(ctx
, desig
);
1310 assert(desig
-> is_item
);
1312 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1314 oberon_error(ctx
, "not array");
1317 oberon_type_t
* base
;
1318 base
= desig
-> result
-> base
;
1320 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1322 oberon_error(ctx
, "index must be integer");
1325 // Статическая проверка границ массива
1326 if(desig
-> result
-> size
!= 0)
1328 if(index
-> is_item
)
1330 if(index
-> item
.mode
== MODE_INTEGER
)
1332 int arr_size
= desig
-> result
-> size
;
1333 int index_int
= index
-> item
.integer
;
1334 if(index_int
< 0 || index_int
> arr_size
- 1)
1336 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1342 oberon_expr_t
* selector
;
1343 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1344 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1345 selector
-> item
.num_args
= 1;
1346 selector
-> item
.args
= index
;
1351 static oberon_expr_t
*
1352 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1354 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1356 expr
= oberno_make_dereferencing(ctx
, expr
);
1359 assert(expr
-> is_item
);
1361 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1363 oberon_error(ctx
, "not record");
1366 oberon_type_t
* rec
= expr
-> result
;
1368 oberon_object_t
* field
;
1369 field
= oberon_find_object(rec
-> scope
, name
, true);
1371 if(field
-> export
== 0)
1373 if(field
-> module
!= ctx
-> mod
)
1375 oberon_error(ctx
, "field not exported");
1380 if(field
-> read_only
)
1382 if(field
-> module
!= ctx
-> mod
)
1388 oberon_expr_t
* selector
;
1389 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1390 selector
-> item
.var
= field
;
1391 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1396 #define ISSELECTOR(x) \
1399 || ((x) == UPARROW) \
1402 static oberon_object_t
*
1403 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1406 oberon_object_t
* x
;
1408 name
= oberon_assert_ident(ctx
);
1409 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1413 if(x
-> class == OBERON_CLASS_MODULE
)
1415 oberon_assert_token(ctx
, DOT
);
1416 name
= oberon_assert_ident(ctx
);
1417 /* Наличие объектов в левых модулях всегда проверяется */
1418 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1420 if(x
-> export
== 0)
1422 oberon_error(ctx
, "not exported");
1435 static oberon_expr_t
*
1436 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1439 oberon_object_t
* x
;
1440 oberon_expr_t
* expr
;
1442 x
= oberon_find_object(ctx
-> decl
, name
, true);
1445 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1450 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1451 expr
-> item
.var
= x
;
1455 static oberon_expr_t
*
1456 oberon_designator(oberon_context_t
* ctx
)
1459 oberon_object_t
* var
;
1460 oberon_expr_t
* expr
;
1462 var
= oberon_qualident(ctx
, NULL
, 1);
1465 if(var
-> read_only
)
1467 if(var
-> module
!= ctx
-> mod
)
1473 switch(var
-> class)
1475 case OBERON_CLASS_CONST
:
1477 expr
= (oberon_expr_t
*) var
-> value
;
1479 case OBERON_CLASS_TYPE
:
1480 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1482 case OBERON_CLASS_VAR
:
1483 case OBERON_CLASS_VAR_PARAM
:
1484 case OBERON_CLASS_PARAM
:
1485 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1487 case OBERON_CLASS_PROC
:
1488 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1491 oberon_error(ctx
, "invalid designator");
1494 expr
-> item
.var
= var
;
1496 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1498 switch(ctx
-> token
)
1501 oberon_assert_token(ctx
, DOT
);
1502 name
= oberon_assert_ident(ctx
);
1503 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1506 oberon_assert_token(ctx
, LBRACE
);
1507 int num_indexes
= 0;
1508 oberon_expr_t
* indexes
= NULL
;
1509 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1510 oberon_assert_token(ctx
, RBRACE
);
1512 for(int i
= 0; i
< num_indexes
; i
++)
1514 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1515 indexes
= indexes
-> next
;
1519 oberon_assert_token(ctx
, UPARROW
);
1520 expr
= oberno_make_dereferencing(ctx
, expr
);
1523 oberon_assert_token(ctx
, LPAREN
);
1524 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1525 if(objtype
-> class != OBERON_CLASS_TYPE
)
1527 oberon_error(ctx
, "must be type");
1529 oberon_assert_token(ctx
, RPAREN
);
1530 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1533 oberon_error(ctx
, "oberon_designator: wat");
1541 static oberon_expr_t
*
1542 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1544 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1545 if(ctx
-> token
== LPAREN
)
1547 oberon_assert_token(ctx
, LPAREN
);
1550 oberon_expr_t
* arguments
= NULL
;
1552 if(ISEXPR(ctx
-> token
))
1554 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1557 assert(expr
-> is_item
== 1);
1558 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1560 oberon_assert_token(ctx
, RPAREN
);
1567 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1569 assert(expr
-> is_item
);
1572 oberon_expr_t
* arguments
= NULL
;
1574 if(ctx
-> token
== LPAREN
)
1576 oberon_assert_token(ctx
, LPAREN
);
1578 if(ISEXPR(ctx
-> token
))
1580 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1583 oberon_assert_token(ctx
, RPAREN
);
1586 /* Вызов происходит даже без скобок */
1587 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1590 static oberon_type_t
*
1591 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1593 if(i
>= -128 && i
<= 127)
1595 return ctx
-> byte_type
;
1597 else if(i
>= -32768 && i
<= 32767)
1599 return ctx
-> shortint_type
;
1601 else if(i
>= -2147483648 && i
<= 2147483647)
1603 return ctx
-> int_type
;
1607 return ctx
-> longint_type
;
1611 static oberon_expr_t
*
1612 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1614 oberon_expr_t
* expr
;
1615 oberon_type_t
* result
;
1616 result
= oberon_get_type_of_int_value(ctx
, i
);
1617 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1618 expr
-> item
.integer
= i
;
1622 static oberon_expr_t
*
1623 oberon_factor(oberon_context_t
* ctx
)
1625 oberon_expr_t
* expr
;
1626 oberon_type_t
* result
;
1628 switch(ctx
-> token
)
1631 expr
= oberon_designator(ctx
);
1632 expr
= oberon_opt_func_parens(ctx
, expr
);
1635 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1636 oberon_assert_token(ctx
, INTEGER
);
1639 result
= ctx
-> char_type
;
1640 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1641 expr
-> item
.integer
= ctx
-> integer
;
1642 oberon_assert_token(ctx
, CHAR
);
1645 result
= ctx
-> string_type
;
1646 expr
= oberon_new_item(MODE_STRING
, result
, true);
1647 expr
-> item
.string
= ctx
-> string
;
1648 oberon_assert_token(ctx
, STRING
);
1651 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1652 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1653 expr
-> item
.real
= ctx
-> real
;
1654 oberon_assert_token(ctx
, REAL
);
1657 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1658 expr
-> item
.boolean
= true;
1659 oberon_assert_token(ctx
, TRUE
);
1662 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1663 expr
-> item
.boolean
= false;
1664 oberon_assert_token(ctx
, FALSE
);
1667 oberon_assert_token(ctx
, LPAREN
);
1668 expr
= oberon_expr(ctx
);
1669 oberon_assert_token(ctx
, RPAREN
);
1672 oberon_assert_token(ctx
, NOT
);
1673 expr
= oberon_factor(ctx
);
1674 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1677 oberon_assert_token(ctx
, NIL
);
1678 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1681 oberon_error(ctx
, "invalid expression");
1687 #define ITMAKESBOOLEAN(x) \
1688 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1690 #define ITUSEONLYINTEGER(x) \
1691 ((x) >= LESS && (x) <= GEQ)
1693 #define ITUSEONLYBOOLEAN(x) \
1694 (((x) == OR) || ((x) == AND))
1697 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1699 oberon_expr_t
* expr
= *e
;
1700 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1702 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1704 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1708 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1711 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1713 oberon_error(ctx
, "required numeric type");
1717 static oberon_expr_t
*
1718 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1720 oberon_expr_t
* expr
;
1721 oberon_type_t
* result
;
1723 if(ITMAKESBOOLEAN(token
))
1725 if(ITUSEONLYINTEGER(token
))
1727 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1728 || b
-> result
-> class == OBERON_TYPE_INTEGER
1729 || a
-> result
-> class == OBERON_TYPE_REAL
1730 || b
-> result
-> class == OBERON_TYPE_REAL
)
1736 oberon_error(ctx
, "used only with numeric types");
1739 else if(ITUSEONLYBOOLEAN(token
))
1741 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1742 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1744 oberon_error(ctx
, "used only with boolean type");
1748 oberon_autocast_binary_op(ctx
, &a
, &b
);
1749 result
= ctx
-> bool_type
;
1753 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1755 else if(token
== NEQ
)
1757 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1759 else if(token
== LESS
)
1761 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1763 else if(token
== LEQ
)
1765 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1767 else if(token
== GREAT
)
1769 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1771 else if(token
== GEQ
)
1773 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1775 else if(token
== OR
)
1777 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1779 else if(token
== AND
)
1781 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1785 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1788 else if(token
== SLASH
)
1790 oberon_autocast_to_real(ctx
, &a
);
1791 oberon_autocast_to_real(ctx
, &b
);
1792 oberon_autocast_binary_op(ctx
, &a
, &b
);
1793 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1795 else if(token
== DIV
)
1797 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1798 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1800 oberon_error(ctx
, "operator DIV requires integer type");
1803 oberon_autocast_binary_op(ctx
, &a
, &b
);
1804 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1808 oberon_autocast_binary_op(ctx
, &a
, &b
);
1812 expr
= oberon_new_operator(OP_ADD
, a
-> result
, a
, b
);
1814 else if(token
== MINUS
)
1816 expr
= oberon_new_operator(OP_SUB
, a
-> result
, a
, b
);
1818 else if(token
== STAR
)
1820 expr
= oberon_new_operator(OP_MUL
, a
-> result
, a
, b
);
1822 else if(token
== MOD
)
1824 expr
= oberon_new_operator(OP_MOD
, a
-> result
, a
, b
);
1828 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1835 #define ISMULOP(x) \
1836 ((x) >= STAR && (x) <= AND)
1838 static oberon_expr_t
*
1839 oberon_term_expr(oberon_context_t
* ctx
)
1841 oberon_expr_t
* expr
;
1843 expr
= oberon_factor(ctx
);
1844 while(ISMULOP(ctx
-> token
))
1846 int token
= ctx
-> token
;
1847 oberon_read_token(ctx
);
1849 oberon_expr_t
* inter
= oberon_factor(ctx
);
1850 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1856 #define ISADDOP(x) \
1857 ((x) >= PLUS && (x) <= OR)
1859 static oberon_expr_t
*
1860 oberon_simple_expr(oberon_context_t
* ctx
)
1862 oberon_expr_t
* expr
;
1865 if(ctx
-> token
== PLUS
)
1868 oberon_assert_token(ctx
, PLUS
);
1870 else if(ctx
-> token
== MINUS
)
1873 oberon_assert_token(ctx
, MINUS
);
1876 expr
= oberon_term_expr(ctx
);
1880 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1883 while(ISADDOP(ctx
-> token
))
1885 int token
= ctx
-> token
;
1886 oberon_read_token(ctx
);
1888 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1889 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1895 #define ISRELATION(x) \
1896 ((x) >= EQUAL && (x) <= IS)
1898 static oberon_expr_t
*
1899 oberon_expr(oberon_context_t
* ctx
)
1901 oberon_expr_t
* expr
;
1903 expr
= oberon_simple_expr(ctx
);
1904 while(ISRELATION(ctx
-> token
))
1906 int token
= ctx
-> token
;
1907 oberon_read_token(ctx
);
1909 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1910 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1916 static oberon_item_t
*
1917 oberon_const_expr(oberon_context_t
* ctx
)
1919 oberon_expr_t
* expr
;
1920 expr
= oberon_expr(ctx
);
1922 if(expr
-> is_item
== 0)
1924 oberon_error(ctx
, "const expression are required");
1927 switch(expr
-> item
.mode
)
1939 oberon_error(ctx
, "const expression are required");
1943 return (oberon_item_t
*) expr
;
1946 // =======================================================================
1948 // =======================================================================
1950 static void oberon_decl_seq(oberon_context_t
* ctx
);
1951 static void oberon_statement_seq(oberon_context_t
* ctx
);
1952 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1955 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1957 if(ctx
-> token
!= token
)
1959 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
1964 oberon_assert_token(oberon_context_t
* ctx
, int token
)
1966 oberon_expect_token(ctx
, token
);
1967 oberon_read_token(ctx
);
1971 oberon_assert_ident(oberon_context_t
* ctx
)
1973 oberon_expect_token(ctx
, IDENT
);
1974 char * ident
= ctx
-> string
;
1975 oberon_read_token(ctx
);
1980 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
1982 switch(ctx
-> token
)
1985 oberon_assert_token(ctx
, STAR
);
1990 oberon_assert_token(ctx
, MINUS
);
2001 static oberon_object_t
*
2002 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2007 oberon_object_t
* x
;
2009 name
= oberon_assert_ident(ctx
);
2010 oberon_def(ctx
, &export
, &read_only
);
2012 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2017 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2020 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2021 while(ctx
-> token
== COMMA
)
2023 oberon_assert_token(ctx
, COMMA
);
2024 oberon_ident_def(ctx
, class, check_upscope
);
2030 oberon_var_decl(oberon_context_t
* ctx
)
2033 oberon_object_t
* list
;
2034 oberon_type_t
* type
;
2035 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2037 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2038 oberon_assert_token(ctx
, COLON
);
2039 oberon_type(ctx
, &type
);
2041 oberon_object_t
* var
= list
;
2042 for(int i
= 0; i
< num
; i
++)
2049 static oberon_object_t
*
2050 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2052 int class = OBERON_CLASS_PARAM
;
2053 if(ctx
-> token
== VAR
)
2055 oberon_read_token(ctx
);
2056 class = OBERON_CLASS_VAR_PARAM
;
2060 oberon_object_t
* list
;
2061 oberon_ident_list(ctx
, class, false, &num
, &list
);
2063 oberon_assert_token(ctx
, COLON
);
2065 oberon_type_t
* type
;
2066 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2067 oberon_type(ctx
, &type
);
2069 oberon_object_t
* param
= list
;
2070 for(int i
= 0; i
< num
; i
++)
2072 param
-> type
= type
;
2073 param
= param
-> next
;
2080 #define ISFPSECTION \
2081 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2084 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2086 oberon_assert_token(ctx
, LPAREN
);
2090 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2091 while(ctx
-> token
== SEMICOLON
)
2093 oberon_assert_token(ctx
, SEMICOLON
);
2094 oberon_fp_section(ctx
, &signature
-> num_decl
);
2098 oberon_assert_token(ctx
, RPAREN
);
2100 if(ctx
-> token
== COLON
)
2102 oberon_assert_token(ctx
, COLON
);
2104 oberon_object_t
* typeobj
;
2105 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2106 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2108 oberon_error(ctx
, "function result is not type");
2110 signature
-> base
= typeobj
-> type
;
2115 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2117 oberon_type_t
* signature
;
2119 signature
-> class = OBERON_TYPE_PROCEDURE
;
2120 signature
-> num_decl
= 0;
2121 signature
-> base
= ctx
-> void_type
;
2122 signature
-> decl
= NULL
;
2124 if(ctx
-> token
== LPAREN
)
2126 oberon_formal_pars(ctx
, signature
);
2131 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2133 if(a
-> num_decl
!= b
-> num_decl
)
2135 oberon_error(ctx
, "number parameters not matched");
2138 int num_param
= a
-> num_decl
;
2139 oberon_object_t
* param_a
= a
-> decl
;
2140 oberon_object_t
* param_b
= b
-> decl
;
2141 for(int i
= 0; i
< num_param
; i
++)
2143 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2145 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2148 if(param_a
-> type
!= param_b
-> type
)
2150 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2153 param_a
= param_a
-> next
;
2154 param_b
= param_b
-> next
;
2159 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2161 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2162 oberon_type_t
* result_type
= proc
-> type
-> base
;
2164 if(result_type
-> class == OBERON_TYPE_VOID
)
2168 oberon_error(ctx
, "procedure has no result type");
2175 oberon_error(ctx
, "procedure requires expression on result");
2178 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2181 proc
-> has_return
= 1;
2183 oberon_generate_return(ctx
, expr
);
2187 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2189 oberon_assert_token(ctx
, SEMICOLON
);
2191 ctx
-> decl
= proc
-> scope
;
2193 oberon_decl_seq(ctx
);
2195 oberon_generate_begin_proc(ctx
, proc
);
2197 if(ctx
-> token
== BEGIN
)
2199 oberon_assert_token(ctx
, BEGIN
);
2200 oberon_statement_seq(ctx
);
2203 oberon_assert_token(ctx
, END
);
2204 char * name
= oberon_assert_ident(ctx
);
2205 if(strcmp(name
, proc
-> name
) != 0)
2207 oberon_error(ctx
, "procedure name not matched");
2210 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2211 && proc
-> has_return
== 0)
2213 oberon_make_return(ctx
, NULL
);
2216 if(proc
-> has_return
== 0)
2218 oberon_error(ctx
, "procedure requires return");
2221 oberon_generate_end_proc(ctx
);
2222 oberon_close_scope(ctx
-> decl
);
2226 oberon_proc_decl(oberon_context_t
* ctx
)
2228 oberon_assert_token(ctx
, PROCEDURE
);
2231 if(ctx
-> token
== UPARROW
)
2233 oberon_assert_token(ctx
, UPARROW
);
2240 name
= oberon_assert_ident(ctx
);
2241 oberon_def(ctx
, &export
, &read_only
);
2243 oberon_scope_t
* proc_scope
;
2244 proc_scope
= oberon_open_scope(ctx
);
2245 ctx
-> decl
-> local
= 1;
2247 oberon_type_t
* signature
;
2248 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2249 oberon_opt_formal_pars(ctx
, &signature
);
2251 oberon_initialize_decl(ctx
);
2252 oberon_generator_init_type(ctx
, signature
);
2253 oberon_close_scope(ctx
-> decl
);
2255 oberon_object_t
* proc
;
2256 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2259 if(proc
-> class != OBERON_CLASS_PROC
)
2261 oberon_error(ctx
, "mult definition");
2268 oberon_error(ctx
, "mult procedure definition");
2272 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2274 oberon_error(ctx
, "export type not matched");
2277 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2281 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2282 proc
-> type
= signature
;
2283 proc
-> scope
= proc_scope
;
2284 oberon_generator_init_proc(ctx
, proc
);
2287 proc
-> scope
-> parent
= proc
;
2292 oberon_proc_decl_body(ctx
, proc
);
2297 oberon_const_decl(oberon_context_t
* ctx
)
2299 oberon_item_t
* value
;
2300 oberon_object_t
* constant
;
2302 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2303 oberon_assert_token(ctx
, EQUAL
);
2304 value
= oberon_const_expr(ctx
);
2305 constant
-> value
= value
;
2309 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2311 if(size
-> is_item
== 0)
2313 oberon_error(ctx
, "requires constant");
2316 if(size
-> item
.mode
!= MODE_INTEGER
)
2318 oberon_error(ctx
, "requires integer constant");
2321 oberon_type_t
* arr
;
2323 arr
-> class = OBERON_TYPE_ARRAY
;
2324 arr
-> size
= size
-> item
.integer
;
2329 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2332 oberon_object_t
* to
;
2334 to
= oberon_qualident(ctx
, &name
, 0);
2336 //name = oberon_assert_ident(ctx);
2337 //to = oberon_find_object(ctx -> decl, name, 0);
2341 if(to
-> class != OBERON_CLASS_TYPE
)
2343 oberon_error(ctx
, "not a type");
2348 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2349 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2355 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2358 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2362 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2370 oberon_type_t
* dim
;
2371 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2373 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2375 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2379 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2381 type
-> class = OBERON_TYPE_ARRAY
;
2383 type
-> base
= base
;
2387 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2389 if(ctx
-> token
== IDENT
)
2392 oberon_object_t
* list
;
2393 oberon_type_t
* type
;
2394 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2396 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2397 oberon_assert_token(ctx
, COLON
);
2399 oberon_scope_t
* current
= ctx
-> decl
;
2400 ctx
-> decl
= modscope
;
2401 oberon_type(ctx
, &type
);
2402 ctx
-> decl
= current
;
2404 oberon_object_t
* field
= list
;
2405 for(int i
= 0; i
< num
; i
++)
2407 field
-> type
= type
;
2408 field
= field
-> next
;
2411 rec
-> num_decl
+= num
;
2416 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2418 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2419 oberon_scope_t
* oldscope
= ctx
-> decl
;
2420 ctx
-> decl
= modscope
;
2422 if(ctx
-> token
== LPAREN
)
2424 oberon_assert_token(ctx
, LPAREN
);
2426 oberon_object_t
* typeobj
;
2427 typeobj
= oberon_qualident(ctx
, NULL
, true);
2429 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2431 oberon_error(ctx
, "base must be type");
2434 oberon_type_t
* base
= typeobj
-> type
;
2435 if(base
-> class == OBERON_TYPE_POINTER
)
2437 base
= base
-> base
;
2440 if(base
-> class != OBERON_TYPE_RECORD
)
2442 oberon_error(ctx
, "base must be record type");
2446 ctx
-> decl
= base
-> scope
;
2448 oberon_assert_token(ctx
, RPAREN
);
2455 oberon_scope_t
* this_scope
;
2456 this_scope
= oberon_open_scope(ctx
);
2457 this_scope
-> local
= true;
2458 this_scope
-> parent
= NULL
;
2459 this_scope
-> parent_type
= rec
;
2461 oberon_field_list(ctx
, rec
, modscope
);
2462 while(ctx
-> token
== SEMICOLON
)
2464 oberon_assert_token(ctx
, SEMICOLON
);
2465 oberon_field_list(ctx
, rec
, modscope
);
2468 rec
-> scope
= this_scope
;
2469 rec
-> decl
= this_scope
-> list
-> next
;
2470 ctx
-> decl
= oldscope
;
2474 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2476 if(ctx
-> token
== IDENT
)
2478 oberon_qualident_type(ctx
, type
);
2480 else if(ctx
-> token
== ARRAY
)
2482 oberon_assert_token(ctx
, ARRAY
);
2485 oberon_expr_t
* sizes
;
2487 if(ISEXPR(ctx
-> token
))
2489 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2492 oberon_assert_token(ctx
, OF
);
2494 oberon_type_t
* base
;
2495 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2496 oberon_type(ctx
, &base
);
2500 oberon_make_open_array(ctx
, base
, *type
);
2504 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2507 else if(ctx
-> token
== RECORD
)
2509 oberon_type_t
* rec
;
2511 rec
-> class = OBERON_TYPE_RECORD
;
2512 rec
-> module
= ctx
-> mod
;
2514 oberon_assert_token(ctx
, RECORD
);
2515 oberon_type_record_body(ctx
, rec
);
2516 oberon_assert_token(ctx
, END
);
2520 else if(ctx
-> token
== POINTER
)
2522 oberon_assert_token(ctx
, POINTER
);
2523 oberon_assert_token(ctx
, TO
);
2525 oberon_type_t
* base
;
2526 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2527 oberon_type(ctx
, &base
);
2529 oberon_type_t
* ptr
;
2531 ptr
-> class = OBERON_TYPE_POINTER
;
2534 else if(ctx
-> token
== PROCEDURE
)
2536 oberon_open_scope(ctx
);
2537 oberon_assert_token(ctx
, PROCEDURE
);
2538 oberon_opt_formal_pars(ctx
, type
);
2539 oberon_close_scope(ctx
-> decl
);
2543 oberon_error(ctx
, "invalid type declaration");
2548 oberon_type_decl(oberon_context_t
* ctx
)
2551 oberon_object_t
* newtype
;
2552 oberon_type_t
* type
;
2556 name
= oberon_assert_ident(ctx
);
2557 oberon_def(ctx
, &export
, &read_only
);
2559 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2562 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2563 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2564 assert(newtype
-> type
);
2568 if(newtype
-> class != OBERON_CLASS_TYPE
)
2570 oberon_error(ctx
, "mult definition");
2573 if(newtype
-> linked
)
2575 oberon_error(ctx
, "mult definition - already linked");
2578 newtype
-> export
= export
;
2579 newtype
-> read_only
= read_only
;
2582 oberon_assert_token(ctx
, EQUAL
);
2584 type
= newtype
-> type
;
2585 oberon_type(ctx
, &type
);
2587 if(type
-> class == OBERON_TYPE_VOID
)
2589 oberon_error(ctx
, "recursive alias declaration");
2592 newtype
-> type
= type
;
2593 newtype
-> linked
= 1;
2596 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2597 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2600 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2602 if(type
-> class != OBERON_TYPE_POINTER
2603 && type
-> class != OBERON_TYPE_ARRAY
)
2608 if(type
-> recursive
)
2610 oberon_error(ctx
, "recursive pointer declaration");
2613 if(type
-> class == OBERON_TYPE_POINTER
2614 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2616 oberon_error(ctx
, "attempt to make pointer to pointer");
2619 type
-> recursive
= 1;
2621 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2623 type
-> recursive
= 0;
2627 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2629 if(type
-> class != OBERON_TYPE_RECORD
)
2634 if(type
-> recursive
)
2636 oberon_error(ctx
, "recursive record declaration");
2639 type
-> recursive
= 1;
2641 int num_fields
= type
-> num_decl
;
2642 oberon_object_t
* field
= type
-> decl
;
2643 for(int i
= 0; i
< num_fields
; i
++)
2645 oberon_prevent_recursive_object(ctx
, field
);
2646 field
= field
-> next
;
2649 type
-> recursive
= 0;
2652 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2654 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2659 if(type
-> recursive
)
2661 oberon_error(ctx
, "recursive procedure declaration");
2664 type
-> recursive
= 1;
2666 int num_fields
= type
-> num_decl
;
2667 oberon_object_t
* field
= type
-> decl
;
2668 for(int i
= 0; i
< num_fields
; i
++)
2670 oberon_prevent_recursive_object(ctx
, field
);
2671 field
= field
-> next
;
2674 type
-> recursive
= 0;
2678 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2680 if(type
-> class != OBERON_TYPE_ARRAY
)
2685 if(type
-> recursive
)
2687 oberon_error(ctx
, "recursive array declaration");
2690 type
-> recursive
= 1;
2692 oberon_prevent_recursive_type(ctx
, type
-> base
);
2694 type
-> recursive
= 0;
2698 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2700 if(type
-> class == OBERON_TYPE_POINTER
)
2702 oberon_prevent_recursive_pointer(ctx
, type
);
2704 else if(type
-> class == OBERON_TYPE_RECORD
)
2706 oberon_prevent_recursive_record(ctx
, type
);
2708 else if(type
-> class == OBERON_TYPE_ARRAY
)
2710 oberon_prevent_recursive_array(ctx
, type
);
2712 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2714 oberon_prevent_recursive_procedure(ctx
, type
);
2719 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2723 case OBERON_CLASS_VAR
:
2724 case OBERON_CLASS_TYPE
:
2725 case OBERON_CLASS_PARAM
:
2726 case OBERON_CLASS_VAR_PARAM
:
2727 case OBERON_CLASS_FIELD
:
2728 oberon_prevent_recursive_type(ctx
, x
-> type
);
2730 case OBERON_CLASS_CONST
:
2731 case OBERON_CLASS_PROC
:
2732 case OBERON_CLASS_MODULE
:
2735 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2741 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2743 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2747 oberon_prevent_recursive_object(ctx
, x
);
2752 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2753 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2756 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2758 if(type
-> class != OBERON_TYPE_RECORD
)
2763 int num_fields
= type
-> num_decl
;
2764 oberon_object_t
* field
= type
-> decl
;
2765 for(int i
= 0; i
< num_fields
; i
++)
2767 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2769 oberon_initialize_type(ctx
, field
-> type
);
2772 oberon_initialize_object(ctx
, field
);
2773 field
= field
-> next
;
2776 oberon_generator_init_record(ctx
, type
);
2780 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2782 if(type
-> class == OBERON_TYPE_VOID
)
2784 oberon_error(ctx
, "undeclarated type");
2787 if(type
-> initialized
)
2792 type
-> initialized
= 1;
2794 if(type
-> class == OBERON_TYPE_POINTER
)
2796 oberon_initialize_type(ctx
, type
-> base
);
2797 oberon_generator_init_type(ctx
, type
);
2799 else if(type
-> class == OBERON_TYPE_ARRAY
)
2801 if(type
-> size
!= 0)
2803 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2805 if(type
-> base
-> size
== 0)
2807 oberon_error(ctx
, "open array not allowed as array element");
2812 oberon_initialize_type(ctx
, type
-> base
);
2813 oberon_generator_init_type(ctx
, type
);
2815 else if(type
-> class == OBERON_TYPE_RECORD
)
2817 oberon_generator_init_type(ctx
, type
);
2818 oberon_initialize_record_fields(ctx
, type
);
2820 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2822 int num_fields
= type
-> num_decl
;
2823 oberon_object_t
* field
= type
-> decl
;
2824 for(int i
= 0; i
< num_fields
; i
++)
2826 oberon_initialize_object(ctx
, field
);
2827 field
= field
-> next
;
2830 oberon_generator_init_type(ctx
, type
);
2834 oberon_generator_init_type(ctx
, type
);
2839 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2841 if(x
-> initialized
)
2846 x
-> initialized
= 1;
2850 case OBERON_CLASS_TYPE
:
2851 oberon_initialize_type(ctx
, x
-> type
);
2853 case OBERON_CLASS_VAR
:
2854 case OBERON_CLASS_FIELD
:
2855 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2857 if(x
-> type
-> size
== 0)
2859 oberon_error(ctx
, "open array not allowed as variable or field");
2862 oberon_initialize_type(ctx
, x
-> type
);
2863 oberon_generator_init_var(ctx
, x
);
2865 case OBERON_CLASS_PARAM
:
2866 case OBERON_CLASS_VAR_PARAM
:
2867 oberon_initialize_type(ctx
, x
-> type
);
2868 oberon_generator_init_var(ctx
, x
);
2870 case OBERON_CLASS_CONST
:
2871 case OBERON_CLASS_PROC
:
2872 case OBERON_CLASS_MODULE
:
2875 oberon_error(ctx
, "oberon_initialize_object: wat");
2881 oberon_initialize_decl(oberon_context_t
* ctx
)
2883 oberon_object_t
* x
= ctx
-> decl
-> list
;
2887 oberon_initialize_object(ctx
, x
-> next
);
2893 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2895 oberon_object_t
* x
= ctx
-> decl
-> list
;
2899 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2901 if(x
-> next
-> linked
== 0)
2903 oberon_error(ctx
, "unresolved forward declaration");
2911 oberon_decl_seq(oberon_context_t
* ctx
)
2913 if(ctx
-> token
== CONST
)
2915 oberon_assert_token(ctx
, CONST
);
2916 while(ctx
-> token
== IDENT
)
2918 oberon_const_decl(ctx
);
2919 oberon_assert_token(ctx
, SEMICOLON
);
2923 if(ctx
-> token
== TYPE
)
2925 oberon_assert_token(ctx
, TYPE
);
2926 while(ctx
-> token
== IDENT
)
2928 oberon_type_decl(ctx
);
2929 oberon_assert_token(ctx
, SEMICOLON
);
2933 if(ctx
-> token
== VAR
)
2935 oberon_assert_token(ctx
, VAR
);
2936 while(ctx
-> token
== IDENT
)
2938 oberon_var_decl(ctx
);
2939 oberon_assert_token(ctx
, SEMICOLON
);
2943 oberon_prevent_recursive_decl(ctx
);
2944 oberon_initialize_decl(ctx
);
2946 while(ctx
-> token
== PROCEDURE
)
2948 oberon_proc_decl(ctx
);
2949 oberon_assert_token(ctx
, SEMICOLON
);
2952 oberon_prevent_undeclarated_procedures(ctx
);
2955 static oberon_expr_t
*
2956 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
2958 oberon_object_t
* x
;
2959 oberon_expr_t
* expr
;
2961 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
2964 oberon_generator_init_temp_var(ctx
, x
);
2966 expr
= oberon_new_item(MODE_VAR
, type
, false);
2967 expr
-> item
.var
= x
;
2972 oberon_statement_seq(oberon_context_t
* ctx
);
2975 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
2977 if(dst
-> read_only
)
2979 oberon_error(ctx
, "read-only destination");
2982 oberon_check_dst(ctx
, dst
);
2983 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
2984 oberon_generate_assign(ctx
, src
, dst
);
2988 oberon_statement(oberon_context_t
* ctx
)
2990 oberon_expr_t
* item1
;
2991 oberon_expr_t
* item2
;
2993 if(ctx
-> token
== IDENT
)
2995 item1
= oberon_designator(ctx
);
2996 if(ctx
-> token
== ASSIGN
)
2998 oberon_assert_token(ctx
, ASSIGN
);
2999 item2
= oberon_expr(ctx
);
3000 oberon_assign(ctx
, item2
, item1
);
3004 oberon_opt_proc_parens(ctx
, item1
);
3007 else if(ctx
-> token
== IF
)
3011 oberon_expr_t
* cond
;
3013 els
= oberon_generator_reserve_label(ctx
);
3014 end
= oberon_generator_reserve_label(ctx
);
3016 oberon_assert_token(ctx
, IF
);
3017 cond
= oberon_expr(ctx
);
3018 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3020 oberon_error(ctx
, "condition must be boolean");
3022 oberon_assert_token(ctx
, THEN
);
3023 oberon_generate_branch(ctx
, cond
, false, els
);
3024 oberon_statement_seq(ctx
);
3025 oberon_generate_goto(ctx
, end
);
3026 oberon_generate_label(ctx
, els
);
3028 while(ctx
-> token
== ELSIF
)
3030 els
= oberon_generator_reserve_label(ctx
);
3032 oberon_assert_token(ctx
, ELSIF
);
3033 cond
= oberon_expr(ctx
);
3034 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3036 oberon_error(ctx
, "condition must be boolean");
3038 oberon_assert_token(ctx
, THEN
);
3039 oberon_generate_branch(ctx
, cond
, false, els
);
3040 oberon_statement_seq(ctx
);
3041 oberon_generate_goto(ctx
, end
);
3042 oberon_generate_label(ctx
, els
);
3045 if(ctx
-> token
== ELSE
)
3047 oberon_assert_token(ctx
, ELSE
);
3048 oberon_statement_seq(ctx
);
3051 oberon_generate_label(ctx
, end
);
3052 oberon_assert_token(ctx
, END
);
3054 else if(ctx
-> token
== WHILE
)
3056 gen_label_t
* begin
;
3058 oberon_expr_t
* cond
;
3060 begin
= oberon_generator_reserve_label(ctx
);
3061 end
= oberon_generator_reserve_label(ctx
);
3063 oberon_assert_token(ctx
, WHILE
);
3064 oberon_generate_label(ctx
, begin
);
3065 cond
= oberon_expr(ctx
);
3066 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3068 oberon_error(ctx
, "condition must be boolean");
3070 oberon_generate_branch(ctx
, cond
, false, end
);
3072 oberon_assert_token(ctx
, DO
);
3073 oberon_statement_seq(ctx
);
3074 oberon_generate_goto(ctx
, begin
);
3076 oberon_assert_token(ctx
, END
);
3077 oberon_generate_label(ctx
, end
);
3079 else if(ctx
-> token
== REPEAT
)
3081 gen_label_t
* begin
;
3082 oberon_expr_t
* cond
;
3084 begin
= oberon_generator_reserve_label(ctx
);
3085 oberon_generate_label(ctx
, begin
);
3086 oberon_assert_token(ctx
, REPEAT
);
3088 oberon_statement_seq(ctx
);
3090 oberon_assert_token(ctx
, UNTIL
);
3092 cond
= oberon_expr(ctx
);
3093 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3095 oberon_error(ctx
, "condition must be boolean");
3098 oberon_generate_branch(ctx
, cond
, true, begin
);
3100 else if(ctx
-> token
== FOR
)
3102 oberon_expr_t
* from
;
3103 oberon_expr_t
* index
;
3105 oberon_expr_t
* bound
;
3107 oberon_expr_t
* cond
;
3108 oberon_expr_t
* count
;
3109 gen_label_t
* begin
;
3114 begin
= oberon_generator_reserve_label(ctx
);
3115 end
= oberon_generator_reserve_label(ctx
);
3117 oberon_assert_token(ctx
, FOR
);
3118 iname
= oberon_assert_ident(ctx
);
3119 index
= oberon_ident_item(ctx
, iname
);
3120 oberon_assert_token(ctx
, ASSIGN
);
3121 from
= oberon_expr(ctx
);
3122 oberon_assign(ctx
, from
, index
);
3123 oberon_assert_token(ctx
, TO
);
3124 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3125 to
= oberon_expr(ctx
);
3126 oberon_assign(ctx
, to
, bound
);
3127 if(ctx
-> token
== BY
)
3129 oberon_assert_token(ctx
, BY
);
3130 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3134 by
= oberon_integer_item(ctx
, 1);
3137 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3139 oberon_error(ctx
, "must be integer");
3142 if(by
-> item
.integer
> 0)
3146 else if(by
-> item
.integer
< 0)
3152 oberon_error(ctx
, "zero step not allowed");
3155 oberon_assert_token(ctx
, DO
);
3156 oberon_generate_label(ctx
, begin
);
3157 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3158 oberon_generate_branch(ctx
, cond
, false, end
);
3159 oberon_statement_seq(ctx
);
3160 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3161 oberon_assign(ctx
, count
, index
);
3162 oberon_generate_goto(ctx
, begin
);
3163 oberon_generate_label(ctx
, end
);
3164 oberon_assert_token(ctx
, END
);
3166 else if(ctx
-> token
== LOOP
)
3168 gen_label_t
* begin
;
3171 begin
= oberon_generator_reserve_label(ctx
);
3172 end
= oberon_generator_reserve_label(ctx
);
3174 oberon_open_scope(ctx
);
3175 oberon_assert_token(ctx
, LOOP
);
3176 oberon_generate_label(ctx
, begin
);
3177 ctx
-> decl
-> exit_label
= end
;
3178 oberon_statement_seq(ctx
);
3179 oberon_generate_goto(ctx
, begin
);
3180 oberon_generate_label(ctx
, end
);
3181 oberon_assert_token(ctx
, END
);
3182 oberon_close_scope(ctx
-> decl
);
3184 else if(ctx
-> token
== EXIT
)
3186 oberon_assert_token(ctx
, EXIT
);
3187 if(ctx
-> decl
-> exit_label
== NULL
)
3189 oberon_error(ctx
, "not in LOOP-END");
3191 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3193 else if(ctx
-> token
== RETURN
)
3195 oberon_assert_token(ctx
, RETURN
);
3196 if(ISEXPR(ctx
-> token
))
3198 oberon_expr_t
* expr
;
3199 expr
= oberon_expr(ctx
);
3200 oberon_make_return(ctx
, expr
);
3204 oberon_make_return(ctx
, NULL
);
3210 oberon_statement_seq(oberon_context_t
* ctx
)
3212 oberon_statement(ctx
);
3213 while(ctx
-> token
== SEMICOLON
)
3215 oberon_assert_token(ctx
, SEMICOLON
);
3216 oberon_statement(ctx
);
3221 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3223 oberon_module_t
* m
= ctx
-> module_list
;
3224 while(m
&& strcmp(m
-> name
, name
) != 0)
3232 code
= ctx
-> import_module(name
);
3235 oberon_error(ctx
, "no such module");
3238 m
= oberon_compile_module(ctx
, code
);
3244 oberon_error(ctx
, "cyclic module import");
3247 oberon_object_t
* ident
;
3248 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3249 ident
-> module
= m
;
3253 oberon_import_decl(oberon_context_t
* ctx
)
3258 alias
= name
= oberon_assert_ident(ctx
);
3259 if(ctx
-> token
== ASSIGN
)
3261 oberon_assert_token(ctx
, ASSIGN
);
3262 name
= oberon_assert_ident(ctx
);
3265 oberon_import_module(ctx
, alias
, name
);
3269 oberon_import_list(oberon_context_t
* ctx
)
3271 oberon_assert_token(ctx
, IMPORT
);
3273 oberon_import_decl(ctx
);
3274 while(ctx
-> token
== COMMA
)
3276 oberon_assert_token(ctx
, COMMA
);
3277 oberon_import_decl(ctx
);
3280 oberon_assert_token(ctx
, SEMICOLON
);
3284 oberon_parse_module(oberon_context_t
* ctx
)
3288 oberon_read_token(ctx
);
3290 oberon_assert_token(ctx
, MODULE
);
3291 name1
= oberon_assert_ident(ctx
);
3292 oberon_assert_token(ctx
, SEMICOLON
);
3293 ctx
-> mod
-> name
= name1
;
3295 oberon_generator_init_module(ctx
, ctx
-> mod
);
3297 if(ctx
-> token
== IMPORT
)
3299 oberon_import_list(ctx
);
3302 oberon_decl_seq(ctx
);
3304 oberon_generate_begin_module(ctx
);
3305 if(ctx
-> token
== BEGIN
)
3307 oberon_assert_token(ctx
, BEGIN
);
3308 oberon_statement_seq(ctx
);
3310 oberon_generate_end_module(ctx
);
3312 oberon_assert_token(ctx
, END
);
3313 name2
= oberon_assert_ident(ctx
);
3314 oberon_assert_token(ctx
, DOT
);
3316 if(strcmp(name1
, name2
) != 0)
3318 oberon_error(ctx
, "module name not matched");
3321 oberon_generator_fini_module(ctx
-> mod
);
3324 // =======================================================================
3326 // =======================================================================
3329 register_default_types(oberon_context_t
* ctx
)
3331 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3332 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3334 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3335 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3336 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3338 ctx
-> string_type
= oberon_new_type_string(1);
3339 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3341 ctx
-> bool_type
= oberon_new_type_boolean();
3342 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3344 ctx
-> byte_type
= oberon_new_type_integer(1);
3345 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3347 ctx
-> shortint_type
= oberon_new_type_integer(2);
3348 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3350 ctx
-> int_type
= oberon_new_type_integer(4);
3351 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3353 ctx
-> longint_type
= oberon_new_type_integer(8);
3354 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3356 ctx
-> real_type
= oberon_new_type_real(4);
3357 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3359 ctx
-> longreal_type
= oberon_new_type_real(8);
3360 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3362 ctx
-> char_type
= oberon_new_type_char(1);
3363 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3367 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3369 oberon_object_t
* proc
;
3370 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3371 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3372 proc
-> type
-> sysproc
= true;
3373 proc
-> type
-> genfunc
= f
;
3374 proc
-> type
-> genproc
= p
;
3377 static oberon_expr_t
*
3378 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3382 oberon_error(ctx
, "too few arguments");
3387 oberon_error(ctx
, "too mach arguments");
3390 oberon_expr_t
* arg
;
3393 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3395 oberon_error(ctx
, "MIN accept only type");
3398 oberon_expr_t
* expr
;
3399 int bits
= arg
-> result
-> size
* 8;
3400 switch(arg
-> result
-> class)
3402 case OBERON_TYPE_INTEGER
:
3403 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3406 oberon_error(ctx
, "allowed only basic types");
3413 static oberon_expr_t
*
3414 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3418 oberon_error(ctx
, "too few arguments");
3423 oberon_error(ctx
, "too mach arguments");
3426 oberon_expr_t
* arg
;
3429 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3431 oberon_error(ctx
, "MAX accept only type");
3434 oberon_expr_t
* expr
;
3435 int bits
= arg
-> result
-> size
* 8;
3436 switch(arg
-> result
-> class)
3438 case OBERON_TYPE_INTEGER
:
3439 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3442 oberon_error(ctx
, "allowed only basic types");
3449 static oberon_expr_t
*
3450 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3454 oberon_error(ctx
, "too few arguments");
3459 oberon_error(ctx
, "too mach arguments");
3462 oberon_expr_t
* arg
;
3465 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3467 oberon_error(ctx
, "SIZE accept only type");
3471 oberon_expr_t
* expr
;
3472 oberon_type_t
* type
= arg
-> result
;
3473 switch(type
-> class)
3475 case OBERON_TYPE_INTEGER
:
3476 case OBERON_TYPE_BOOLEAN
:
3477 case OBERON_TYPE_REAL
:
3478 size
= type
-> size
;
3481 oberon_error(ctx
, "TODO SIZE");
3485 expr
= oberon_integer_item(ctx
, size
);
3489 static oberon_expr_t
*
3490 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3494 oberon_error(ctx
, "too few arguments");
3499 oberon_error(ctx
, "too mach arguments");
3502 oberon_expr_t
* arg
;
3505 oberon_type_t
* result_type
;
3506 result_type
= arg
-> result
;
3508 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3510 oberon_error(ctx
, "ABS accepts only integers");
3513 oberon_expr_t
* expr
;
3514 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3519 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3523 oberon_error(ctx
, "too few arguments");
3526 oberon_expr_t
* dst
;
3529 oberon_type_t
* type
;
3530 type
= dst
-> result
;
3532 if(type
-> class != OBERON_TYPE_POINTER
)
3534 oberon_error(ctx
, "not a pointer");
3537 type
= type
-> base
;
3539 oberon_expr_t
* src
;
3540 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3541 src
-> item
.num_args
= 0;
3542 src
-> item
.args
= NULL
;
3545 if(type
-> class == OBERON_TYPE_ARRAY
)
3547 if(type
-> size
== 0)
3549 oberon_type_t
* x
= type
;
3550 while(x
-> class == OBERON_TYPE_ARRAY
)
3560 if(num_args
< max_args
)
3562 oberon_error(ctx
, "too few arguments");
3565 if(num_args
> max_args
)
3567 oberon_error(ctx
, "too mach arguments");
3570 int num_sizes
= max_args
- 1;
3571 oberon_expr_t
* size_list
= list_args
-> next
;
3573 oberon_expr_t
* arg
= size_list
;
3574 for(int i
= 0; i
< max_args
- 1; i
++)
3576 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3578 oberon_error(ctx
, "size must be integer");
3583 src
-> item
.num_args
= num_sizes
;
3584 src
-> item
.args
= size_list
;
3586 else if(type
-> class != OBERON_TYPE_RECORD
)
3588 oberon_error(ctx
, "oberon_make_new_call: wat");
3591 if(num_args
> max_args
)
3593 oberon_error(ctx
, "too mach arguments");
3596 oberon_assign(ctx
, src
, dst
);
3600 oberon_create_context(ModuleImportCallback import_module
)
3602 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3604 oberon_scope_t
* world_scope
;
3605 world_scope
= oberon_open_scope(ctx
);
3606 ctx
-> world_scope
= world_scope
;
3608 ctx
-> import_module
= import_module
;
3610 oberon_generator_init_context(ctx
);
3612 register_default_types(ctx
);
3615 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3616 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
3617 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
3618 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
3621 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3627 oberon_destroy_context(oberon_context_t
* ctx
)
3629 oberon_generator_destroy_context(ctx
);
3634 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3636 const char * code
= ctx
-> code
;
3637 int code_index
= ctx
-> code_index
;
3639 int token
= ctx
-> token
;
3640 char * string
= ctx
-> string
;
3641 int integer
= ctx
-> integer
;
3642 int real
= ctx
-> real
;
3643 bool longmode
= ctx
-> longmode
;
3644 oberon_scope_t
* decl
= ctx
-> decl
;
3645 oberon_module_t
* mod
= ctx
-> mod
;
3647 oberon_scope_t
* module_scope
;
3648 module_scope
= oberon_open_scope(ctx
);
3650 oberon_module_t
* module
;
3651 module
= calloc(1, sizeof *module
);
3652 module
-> decl
= module_scope
;
3653 module
-> next
= ctx
-> module_list
;
3655 ctx
-> mod
= module
;
3656 ctx
-> module_list
= module
;
3658 oberon_init_scaner(ctx
, newcode
);
3659 oberon_parse_module(ctx
);
3661 module
-> ready
= 1;
3664 ctx
-> code_index
= code_index
;
3666 ctx
-> token
= token
;
3667 ctx
-> string
= string
;
3668 ctx
-> integer
= integer
;
3670 ctx
-> longmode
= longmode
;