e88ff79159e28e93c010a73ebde045ee8a6c9132
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
;
1725 oberon_type_t
* v
= a
-> result
;
1726 if(v
-> class == OBERON_TYPE_POINTER
)
1729 if(v
-> class != OBERON_TYPE_RECORD
)
1731 oberon_error(ctx
, "must be record");
1734 else if(v
-> class != OBERON_TYPE_RECORD
)
1736 oberon_error(ctx
, "must be record");
1739 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1741 oberon_error(ctx
, "requires type");
1744 oberon_type_t
* t
= b
-> result
;
1745 if(t
-> class == OBERON_TYPE_POINTER
)
1748 if(t
-> class != OBERON_TYPE_RECORD
)
1750 oberon_error(ctx
, "must be record");
1753 else if(t
-> class != OBERON_TYPE_RECORD
)
1755 oberon_error(ctx
, "must be record");
1758 result
= ctx
-> bool_type
;
1759 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1761 else if(ITMAKESBOOLEAN(token
))
1763 if(ITUSEONLYINTEGER(token
))
1765 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1766 || b
-> result
-> class == OBERON_TYPE_INTEGER
1767 || a
-> result
-> class == OBERON_TYPE_REAL
1768 || b
-> result
-> class == OBERON_TYPE_REAL
)
1774 oberon_error(ctx
, "used only with numeric types");
1777 else if(ITUSEONLYBOOLEAN(token
))
1779 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1780 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1782 oberon_error(ctx
, "used only with boolean type");
1786 oberon_autocast_binary_op(ctx
, &a
, &b
);
1787 result
= ctx
-> bool_type
;
1791 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1793 else if(token
== NEQ
)
1795 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1797 else if(token
== LESS
)
1799 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1801 else if(token
== LEQ
)
1803 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1805 else if(token
== GREAT
)
1807 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1809 else if(token
== GEQ
)
1811 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1813 else if(token
== OR
)
1815 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1817 else if(token
== AND
)
1819 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1823 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1826 else if(token
== SLASH
)
1828 oberon_autocast_to_real(ctx
, &a
);
1829 oberon_autocast_to_real(ctx
, &b
);
1830 oberon_autocast_binary_op(ctx
, &a
, &b
);
1831 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1833 else if(token
== DIV
)
1835 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1836 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1838 oberon_error(ctx
, "operator DIV requires integer type");
1841 oberon_autocast_binary_op(ctx
, &a
, &b
);
1842 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1846 oberon_autocast_binary_op(ctx
, &a
, &b
);
1850 expr
= oberon_new_operator(OP_ADD
, a
-> result
, a
, b
);
1852 else if(token
== MINUS
)
1854 expr
= oberon_new_operator(OP_SUB
, a
-> result
, a
, b
);
1856 else if(token
== STAR
)
1858 expr
= oberon_new_operator(OP_MUL
, a
-> result
, a
, b
);
1860 else if(token
== MOD
)
1862 expr
= oberon_new_operator(OP_MOD
, a
-> result
, a
, b
);
1866 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1873 #define ISMULOP(x) \
1874 ((x) >= STAR && (x) <= AND)
1876 static oberon_expr_t
*
1877 oberon_term_expr(oberon_context_t
* ctx
)
1879 oberon_expr_t
* expr
;
1881 expr
= oberon_factor(ctx
);
1882 while(ISMULOP(ctx
-> token
))
1884 int token
= ctx
-> token
;
1885 oberon_read_token(ctx
);
1887 oberon_expr_t
* inter
= oberon_factor(ctx
);
1888 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1894 #define ISADDOP(x) \
1895 ((x) >= PLUS && (x) <= OR)
1897 static oberon_expr_t
*
1898 oberon_simple_expr(oberon_context_t
* ctx
)
1900 oberon_expr_t
* expr
;
1903 if(ctx
-> token
== PLUS
)
1906 oberon_assert_token(ctx
, PLUS
);
1908 else if(ctx
-> token
== MINUS
)
1911 oberon_assert_token(ctx
, MINUS
);
1914 expr
= oberon_term_expr(ctx
);
1918 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1921 while(ISADDOP(ctx
-> token
))
1923 int token
= ctx
-> token
;
1924 oberon_read_token(ctx
);
1926 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1927 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1933 #define ISRELATION(x) \
1934 ((x) >= EQUAL && (x) <= IS)
1936 static oberon_expr_t
*
1937 oberon_expr(oberon_context_t
* ctx
)
1939 oberon_expr_t
* expr
;
1941 expr
= oberon_simple_expr(ctx
);
1942 while(ISRELATION(ctx
-> token
))
1944 int token
= ctx
-> token
;
1945 oberon_read_token(ctx
);
1947 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1948 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1954 static oberon_item_t
*
1955 oberon_const_expr(oberon_context_t
* ctx
)
1957 oberon_expr_t
* expr
;
1958 expr
= oberon_expr(ctx
);
1960 if(expr
-> is_item
== 0)
1962 oberon_error(ctx
, "const expression are required");
1965 switch(expr
-> item
.mode
)
1977 oberon_error(ctx
, "const expression are required");
1981 return (oberon_item_t
*) expr
;
1984 // =======================================================================
1986 // =======================================================================
1988 static void oberon_decl_seq(oberon_context_t
* ctx
);
1989 static void oberon_statement_seq(oberon_context_t
* ctx
);
1990 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1993 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1995 if(ctx
-> token
!= token
)
1997 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2002 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2004 oberon_expect_token(ctx
, token
);
2005 oberon_read_token(ctx
);
2009 oberon_assert_ident(oberon_context_t
* ctx
)
2011 oberon_expect_token(ctx
, IDENT
);
2012 char * ident
= ctx
-> string
;
2013 oberon_read_token(ctx
);
2018 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2020 switch(ctx
-> token
)
2023 oberon_assert_token(ctx
, STAR
);
2028 oberon_assert_token(ctx
, MINUS
);
2039 static oberon_object_t
*
2040 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2045 oberon_object_t
* x
;
2047 name
= oberon_assert_ident(ctx
);
2048 oberon_def(ctx
, &export
, &read_only
);
2050 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2055 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2058 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2059 while(ctx
-> token
== COMMA
)
2061 oberon_assert_token(ctx
, COMMA
);
2062 oberon_ident_def(ctx
, class, check_upscope
);
2068 oberon_var_decl(oberon_context_t
* ctx
)
2071 oberon_object_t
* list
;
2072 oberon_type_t
* type
;
2073 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2075 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2076 oberon_assert_token(ctx
, COLON
);
2077 oberon_type(ctx
, &type
);
2079 oberon_object_t
* var
= list
;
2080 for(int i
= 0; i
< num
; i
++)
2087 static oberon_object_t
*
2088 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2090 int class = OBERON_CLASS_PARAM
;
2091 if(ctx
-> token
== VAR
)
2093 oberon_read_token(ctx
);
2094 class = OBERON_CLASS_VAR_PARAM
;
2098 oberon_object_t
* list
;
2099 oberon_ident_list(ctx
, class, false, &num
, &list
);
2101 oberon_assert_token(ctx
, COLON
);
2103 oberon_type_t
* type
;
2104 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2105 oberon_type(ctx
, &type
);
2107 oberon_object_t
* param
= list
;
2108 for(int i
= 0; i
< num
; i
++)
2110 param
-> type
= type
;
2111 param
= param
-> next
;
2118 #define ISFPSECTION \
2119 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2122 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2124 oberon_assert_token(ctx
, LPAREN
);
2128 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2129 while(ctx
-> token
== SEMICOLON
)
2131 oberon_assert_token(ctx
, SEMICOLON
);
2132 oberon_fp_section(ctx
, &signature
-> num_decl
);
2136 oberon_assert_token(ctx
, RPAREN
);
2138 if(ctx
-> token
== COLON
)
2140 oberon_assert_token(ctx
, COLON
);
2142 oberon_object_t
* typeobj
;
2143 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2144 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2146 oberon_error(ctx
, "function result is not type");
2148 signature
-> base
= typeobj
-> type
;
2153 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2155 oberon_type_t
* signature
;
2157 signature
-> class = OBERON_TYPE_PROCEDURE
;
2158 signature
-> num_decl
= 0;
2159 signature
-> base
= ctx
-> void_type
;
2160 signature
-> decl
= NULL
;
2162 if(ctx
-> token
== LPAREN
)
2164 oberon_formal_pars(ctx
, signature
);
2169 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2171 if(a
-> num_decl
!= b
-> num_decl
)
2173 oberon_error(ctx
, "number parameters not matched");
2176 int num_param
= a
-> num_decl
;
2177 oberon_object_t
* param_a
= a
-> decl
;
2178 oberon_object_t
* param_b
= b
-> decl
;
2179 for(int i
= 0; i
< num_param
; i
++)
2181 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2183 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2186 if(param_a
-> type
!= param_b
-> type
)
2188 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2191 param_a
= param_a
-> next
;
2192 param_b
= param_b
-> next
;
2197 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2199 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2200 oberon_type_t
* result_type
= proc
-> type
-> base
;
2202 if(result_type
-> class == OBERON_TYPE_VOID
)
2206 oberon_error(ctx
, "procedure has no result type");
2213 oberon_error(ctx
, "procedure requires expression on result");
2216 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2219 proc
-> has_return
= 1;
2221 oberon_generate_return(ctx
, expr
);
2225 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2227 oberon_assert_token(ctx
, SEMICOLON
);
2229 ctx
-> decl
= proc
-> scope
;
2231 oberon_decl_seq(ctx
);
2233 oberon_generate_begin_proc(ctx
, proc
);
2235 if(ctx
-> token
== BEGIN
)
2237 oberon_assert_token(ctx
, BEGIN
);
2238 oberon_statement_seq(ctx
);
2241 oberon_assert_token(ctx
, END
);
2242 char * name
= oberon_assert_ident(ctx
);
2243 if(strcmp(name
, proc
-> name
) != 0)
2245 oberon_error(ctx
, "procedure name not matched");
2248 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2249 && proc
-> has_return
== 0)
2251 oberon_make_return(ctx
, NULL
);
2254 if(proc
-> has_return
== 0)
2256 oberon_error(ctx
, "procedure requires return");
2259 oberon_generate_end_proc(ctx
);
2260 oberon_close_scope(ctx
-> decl
);
2264 oberon_proc_decl(oberon_context_t
* ctx
)
2266 oberon_assert_token(ctx
, PROCEDURE
);
2269 if(ctx
-> token
== UPARROW
)
2271 oberon_assert_token(ctx
, UPARROW
);
2278 name
= oberon_assert_ident(ctx
);
2279 oberon_def(ctx
, &export
, &read_only
);
2281 oberon_scope_t
* proc_scope
;
2282 proc_scope
= oberon_open_scope(ctx
);
2283 ctx
-> decl
-> local
= 1;
2285 oberon_type_t
* signature
;
2286 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2287 oberon_opt_formal_pars(ctx
, &signature
);
2289 oberon_initialize_decl(ctx
);
2290 oberon_generator_init_type(ctx
, signature
);
2291 oberon_close_scope(ctx
-> decl
);
2293 oberon_object_t
* proc
;
2294 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2297 if(proc
-> class != OBERON_CLASS_PROC
)
2299 oberon_error(ctx
, "mult definition");
2306 oberon_error(ctx
, "mult procedure definition");
2310 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2312 oberon_error(ctx
, "export type not matched");
2315 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2319 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2320 proc
-> type
= signature
;
2321 proc
-> scope
= proc_scope
;
2322 oberon_generator_init_proc(ctx
, proc
);
2325 proc
-> scope
-> parent
= proc
;
2330 oberon_proc_decl_body(ctx
, proc
);
2335 oberon_const_decl(oberon_context_t
* ctx
)
2337 oberon_item_t
* value
;
2338 oberon_object_t
* constant
;
2340 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2341 oberon_assert_token(ctx
, EQUAL
);
2342 value
= oberon_const_expr(ctx
);
2343 constant
-> value
= value
;
2347 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2349 if(size
-> is_item
== 0)
2351 oberon_error(ctx
, "requires constant");
2354 if(size
-> item
.mode
!= MODE_INTEGER
)
2356 oberon_error(ctx
, "requires integer constant");
2359 oberon_type_t
* arr
;
2361 arr
-> class = OBERON_TYPE_ARRAY
;
2362 arr
-> size
= size
-> item
.integer
;
2367 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2370 oberon_object_t
* to
;
2372 to
= oberon_qualident(ctx
, &name
, 0);
2374 //name = oberon_assert_ident(ctx);
2375 //to = oberon_find_object(ctx -> decl, name, 0);
2379 if(to
-> class != OBERON_CLASS_TYPE
)
2381 oberon_error(ctx
, "not a type");
2386 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2387 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2393 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2396 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2400 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2408 oberon_type_t
* dim
;
2409 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2411 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2413 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2417 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2419 type
-> class = OBERON_TYPE_ARRAY
;
2421 type
-> base
= base
;
2425 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2427 if(ctx
-> token
== IDENT
)
2430 oberon_object_t
* list
;
2431 oberon_type_t
* type
;
2432 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2434 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2435 oberon_assert_token(ctx
, COLON
);
2437 oberon_scope_t
* current
= ctx
-> decl
;
2438 ctx
-> decl
= modscope
;
2439 oberon_type(ctx
, &type
);
2440 ctx
-> decl
= current
;
2442 oberon_object_t
* field
= list
;
2443 for(int i
= 0; i
< num
; i
++)
2445 field
-> type
= type
;
2446 field
= field
-> next
;
2449 rec
-> num_decl
+= num
;
2454 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2456 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2457 oberon_scope_t
* oldscope
= ctx
-> decl
;
2458 ctx
-> decl
= modscope
;
2460 if(ctx
-> token
== LPAREN
)
2462 oberon_assert_token(ctx
, LPAREN
);
2464 oberon_object_t
* typeobj
;
2465 typeobj
= oberon_qualident(ctx
, NULL
, true);
2467 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2469 oberon_error(ctx
, "base must be type");
2472 oberon_type_t
* base
= typeobj
-> type
;
2473 if(base
-> class == OBERON_TYPE_POINTER
)
2475 base
= base
-> base
;
2478 if(base
-> class != OBERON_TYPE_RECORD
)
2480 oberon_error(ctx
, "base must be record type");
2484 ctx
-> decl
= base
-> scope
;
2486 oberon_assert_token(ctx
, RPAREN
);
2493 oberon_scope_t
* this_scope
;
2494 this_scope
= oberon_open_scope(ctx
);
2495 this_scope
-> local
= true;
2496 this_scope
-> parent
= NULL
;
2497 this_scope
-> parent_type
= rec
;
2499 oberon_field_list(ctx
, rec
, modscope
);
2500 while(ctx
-> token
== SEMICOLON
)
2502 oberon_assert_token(ctx
, SEMICOLON
);
2503 oberon_field_list(ctx
, rec
, modscope
);
2506 rec
-> scope
= this_scope
;
2507 rec
-> decl
= this_scope
-> list
-> next
;
2508 ctx
-> decl
= oldscope
;
2512 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2514 if(ctx
-> token
== IDENT
)
2516 oberon_qualident_type(ctx
, type
);
2518 else if(ctx
-> token
== ARRAY
)
2520 oberon_assert_token(ctx
, ARRAY
);
2523 oberon_expr_t
* sizes
;
2525 if(ISEXPR(ctx
-> token
))
2527 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2530 oberon_assert_token(ctx
, OF
);
2532 oberon_type_t
* base
;
2533 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2534 oberon_type(ctx
, &base
);
2538 oberon_make_open_array(ctx
, base
, *type
);
2542 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2545 else if(ctx
-> token
== RECORD
)
2547 oberon_type_t
* rec
;
2549 rec
-> class = OBERON_TYPE_RECORD
;
2550 rec
-> module
= ctx
-> mod
;
2552 oberon_assert_token(ctx
, RECORD
);
2553 oberon_type_record_body(ctx
, rec
);
2554 oberon_assert_token(ctx
, END
);
2558 else if(ctx
-> token
== POINTER
)
2560 oberon_assert_token(ctx
, POINTER
);
2561 oberon_assert_token(ctx
, TO
);
2563 oberon_type_t
* base
;
2564 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2565 oberon_type(ctx
, &base
);
2567 oberon_type_t
* ptr
;
2569 ptr
-> class = OBERON_TYPE_POINTER
;
2572 else if(ctx
-> token
== PROCEDURE
)
2574 oberon_open_scope(ctx
);
2575 oberon_assert_token(ctx
, PROCEDURE
);
2576 oberon_opt_formal_pars(ctx
, type
);
2577 oberon_close_scope(ctx
-> decl
);
2581 oberon_error(ctx
, "invalid type declaration");
2586 oberon_type_decl(oberon_context_t
* ctx
)
2589 oberon_object_t
* newtype
;
2590 oberon_type_t
* type
;
2594 name
= oberon_assert_ident(ctx
);
2595 oberon_def(ctx
, &export
, &read_only
);
2597 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2600 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2601 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2602 assert(newtype
-> type
);
2606 if(newtype
-> class != OBERON_CLASS_TYPE
)
2608 oberon_error(ctx
, "mult definition");
2611 if(newtype
-> linked
)
2613 oberon_error(ctx
, "mult definition - already linked");
2616 newtype
-> export
= export
;
2617 newtype
-> read_only
= read_only
;
2620 oberon_assert_token(ctx
, EQUAL
);
2622 type
= newtype
-> type
;
2623 oberon_type(ctx
, &type
);
2625 if(type
-> class == OBERON_TYPE_VOID
)
2627 oberon_error(ctx
, "recursive alias declaration");
2630 newtype
-> type
= type
;
2631 newtype
-> linked
= 1;
2634 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2635 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2638 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2640 if(type
-> class != OBERON_TYPE_POINTER
2641 && type
-> class != OBERON_TYPE_ARRAY
)
2646 if(type
-> recursive
)
2648 oberon_error(ctx
, "recursive pointer declaration");
2651 if(type
-> class == OBERON_TYPE_POINTER
2652 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2654 oberon_error(ctx
, "attempt to make pointer to pointer");
2657 type
-> recursive
= 1;
2659 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2661 type
-> recursive
= 0;
2665 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2667 if(type
-> class != OBERON_TYPE_RECORD
)
2672 if(type
-> recursive
)
2674 oberon_error(ctx
, "recursive record declaration");
2677 type
-> recursive
= 1;
2679 int num_fields
= type
-> num_decl
;
2680 oberon_object_t
* field
= type
-> decl
;
2681 for(int i
= 0; i
< num_fields
; i
++)
2683 oberon_prevent_recursive_object(ctx
, field
);
2684 field
= field
-> next
;
2687 type
-> recursive
= 0;
2690 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2692 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2697 if(type
-> recursive
)
2699 oberon_error(ctx
, "recursive procedure declaration");
2702 type
-> recursive
= 1;
2704 int num_fields
= type
-> num_decl
;
2705 oberon_object_t
* field
= type
-> decl
;
2706 for(int i
= 0; i
< num_fields
; i
++)
2708 oberon_prevent_recursive_object(ctx
, field
);
2709 field
= field
-> next
;
2712 type
-> recursive
= 0;
2716 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2718 if(type
-> class != OBERON_TYPE_ARRAY
)
2723 if(type
-> recursive
)
2725 oberon_error(ctx
, "recursive array declaration");
2728 type
-> recursive
= 1;
2730 oberon_prevent_recursive_type(ctx
, type
-> base
);
2732 type
-> recursive
= 0;
2736 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2738 if(type
-> class == OBERON_TYPE_POINTER
)
2740 oberon_prevent_recursive_pointer(ctx
, type
);
2742 else if(type
-> class == OBERON_TYPE_RECORD
)
2744 oberon_prevent_recursive_record(ctx
, type
);
2746 else if(type
-> class == OBERON_TYPE_ARRAY
)
2748 oberon_prevent_recursive_array(ctx
, type
);
2750 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2752 oberon_prevent_recursive_procedure(ctx
, type
);
2757 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2761 case OBERON_CLASS_VAR
:
2762 case OBERON_CLASS_TYPE
:
2763 case OBERON_CLASS_PARAM
:
2764 case OBERON_CLASS_VAR_PARAM
:
2765 case OBERON_CLASS_FIELD
:
2766 oberon_prevent_recursive_type(ctx
, x
-> type
);
2768 case OBERON_CLASS_CONST
:
2769 case OBERON_CLASS_PROC
:
2770 case OBERON_CLASS_MODULE
:
2773 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2779 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2781 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2785 oberon_prevent_recursive_object(ctx
, x
);
2790 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2791 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2794 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2796 if(type
-> class != OBERON_TYPE_RECORD
)
2801 int num_fields
= type
-> num_decl
;
2802 oberon_object_t
* field
= type
-> decl
;
2803 for(int i
= 0; i
< num_fields
; i
++)
2805 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2807 oberon_initialize_type(ctx
, field
-> type
);
2810 oberon_initialize_object(ctx
, field
);
2811 field
= field
-> next
;
2814 oberon_generator_init_record(ctx
, type
);
2818 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2820 if(type
-> class == OBERON_TYPE_VOID
)
2822 oberon_error(ctx
, "undeclarated type");
2825 if(type
-> initialized
)
2830 type
-> initialized
= 1;
2832 if(type
-> class == OBERON_TYPE_POINTER
)
2834 oberon_initialize_type(ctx
, type
-> base
);
2835 oberon_generator_init_type(ctx
, type
);
2837 else if(type
-> class == OBERON_TYPE_ARRAY
)
2839 if(type
-> size
!= 0)
2841 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2843 if(type
-> base
-> size
== 0)
2845 oberon_error(ctx
, "open array not allowed as array element");
2850 oberon_initialize_type(ctx
, type
-> base
);
2851 oberon_generator_init_type(ctx
, type
);
2853 else if(type
-> class == OBERON_TYPE_RECORD
)
2855 oberon_generator_init_type(ctx
, type
);
2856 oberon_initialize_record_fields(ctx
, type
);
2858 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2860 int num_fields
= type
-> num_decl
;
2861 oberon_object_t
* field
= type
-> decl
;
2862 for(int i
= 0; i
< num_fields
; i
++)
2864 oberon_initialize_object(ctx
, field
);
2865 field
= field
-> next
;
2868 oberon_generator_init_type(ctx
, type
);
2872 oberon_generator_init_type(ctx
, type
);
2877 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2879 if(x
-> initialized
)
2884 x
-> initialized
= 1;
2888 case OBERON_CLASS_TYPE
:
2889 oberon_initialize_type(ctx
, x
-> type
);
2891 case OBERON_CLASS_VAR
:
2892 case OBERON_CLASS_FIELD
:
2893 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2895 if(x
-> type
-> size
== 0)
2897 oberon_error(ctx
, "open array not allowed as variable or field");
2900 oberon_initialize_type(ctx
, x
-> type
);
2901 oberon_generator_init_var(ctx
, x
);
2903 case OBERON_CLASS_PARAM
:
2904 case OBERON_CLASS_VAR_PARAM
:
2905 oberon_initialize_type(ctx
, x
-> type
);
2906 oberon_generator_init_var(ctx
, x
);
2908 case OBERON_CLASS_CONST
:
2909 case OBERON_CLASS_PROC
:
2910 case OBERON_CLASS_MODULE
:
2913 oberon_error(ctx
, "oberon_initialize_object: wat");
2919 oberon_initialize_decl(oberon_context_t
* ctx
)
2921 oberon_object_t
* x
= ctx
-> decl
-> list
;
2925 oberon_initialize_object(ctx
, x
-> next
);
2931 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2933 oberon_object_t
* x
= ctx
-> decl
-> list
;
2937 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2939 if(x
-> next
-> linked
== 0)
2941 oberon_error(ctx
, "unresolved forward declaration");
2949 oberon_decl_seq(oberon_context_t
* ctx
)
2951 if(ctx
-> token
== CONST
)
2953 oberon_assert_token(ctx
, CONST
);
2954 while(ctx
-> token
== IDENT
)
2956 oberon_const_decl(ctx
);
2957 oberon_assert_token(ctx
, SEMICOLON
);
2961 if(ctx
-> token
== TYPE
)
2963 oberon_assert_token(ctx
, TYPE
);
2964 while(ctx
-> token
== IDENT
)
2966 oberon_type_decl(ctx
);
2967 oberon_assert_token(ctx
, SEMICOLON
);
2971 if(ctx
-> token
== VAR
)
2973 oberon_assert_token(ctx
, VAR
);
2974 while(ctx
-> token
== IDENT
)
2976 oberon_var_decl(ctx
);
2977 oberon_assert_token(ctx
, SEMICOLON
);
2981 oberon_prevent_recursive_decl(ctx
);
2982 oberon_initialize_decl(ctx
);
2984 while(ctx
-> token
== PROCEDURE
)
2986 oberon_proc_decl(ctx
);
2987 oberon_assert_token(ctx
, SEMICOLON
);
2990 oberon_prevent_undeclarated_procedures(ctx
);
2993 static oberon_expr_t
*
2994 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
2996 oberon_object_t
* x
;
2997 oberon_expr_t
* expr
;
2999 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3002 oberon_generator_init_temp_var(ctx
, x
);
3004 expr
= oberon_new_item(MODE_VAR
, type
, false);
3005 expr
-> item
.var
= x
;
3010 oberon_statement_seq(oberon_context_t
* ctx
);
3013 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3015 if(dst
-> read_only
)
3017 oberon_error(ctx
, "read-only destination");
3020 oberon_check_dst(ctx
, dst
);
3021 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3022 oberon_generate_assign(ctx
, src
, dst
);
3026 oberon_statement(oberon_context_t
* ctx
)
3028 oberon_expr_t
* item1
;
3029 oberon_expr_t
* item2
;
3031 if(ctx
-> token
== IDENT
)
3033 item1
= oberon_designator(ctx
);
3034 if(ctx
-> token
== ASSIGN
)
3036 oberon_assert_token(ctx
, ASSIGN
);
3037 item2
= oberon_expr(ctx
);
3038 oberon_assign(ctx
, item2
, item1
);
3042 oberon_opt_proc_parens(ctx
, item1
);
3045 else if(ctx
-> token
== IF
)
3049 oberon_expr_t
* cond
;
3051 els
= oberon_generator_reserve_label(ctx
);
3052 end
= oberon_generator_reserve_label(ctx
);
3054 oberon_assert_token(ctx
, IF
);
3055 cond
= oberon_expr(ctx
);
3056 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3058 oberon_error(ctx
, "condition must be boolean");
3060 oberon_assert_token(ctx
, THEN
);
3061 oberon_generate_branch(ctx
, cond
, false, els
);
3062 oberon_statement_seq(ctx
);
3063 oberon_generate_goto(ctx
, end
);
3064 oberon_generate_label(ctx
, els
);
3066 while(ctx
-> token
== ELSIF
)
3068 els
= oberon_generator_reserve_label(ctx
);
3070 oberon_assert_token(ctx
, ELSIF
);
3071 cond
= oberon_expr(ctx
);
3072 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3074 oberon_error(ctx
, "condition must be boolean");
3076 oberon_assert_token(ctx
, THEN
);
3077 oberon_generate_branch(ctx
, cond
, false, els
);
3078 oberon_statement_seq(ctx
);
3079 oberon_generate_goto(ctx
, end
);
3080 oberon_generate_label(ctx
, els
);
3083 if(ctx
-> token
== ELSE
)
3085 oberon_assert_token(ctx
, ELSE
);
3086 oberon_statement_seq(ctx
);
3089 oberon_generate_label(ctx
, end
);
3090 oberon_assert_token(ctx
, END
);
3092 else if(ctx
-> token
== WHILE
)
3094 gen_label_t
* begin
;
3096 oberon_expr_t
* cond
;
3098 begin
= oberon_generator_reserve_label(ctx
);
3099 end
= oberon_generator_reserve_label(ctx
);
3101 oberon_assert_token(ctx
, WHILE
);
3102 oberon_generate_label(ctx
, begin
);
3103 cond
= oberon_expr(ctx
);
3104 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3106 oberon_error(ctx
, "condition must be boolean");
3108 oberon_generate_branch(ctx
, cond
, false, end
);
3110 oberon_assert_token(ctx
, DO
);
3111 oberon_statement_seq(ctx
);
3112 oberon_generate_goto(ctx
, begin
);
3114 oberon_assert_token(ctx
, END
);
3115 oberon_generate_label(ctx
, end
);
3117 else if(ctx
-> token
== REPEAT
)
3119 gen_label_t
* begin
;
3120 oberon_expr_t
* cond
;
3122 begin
= oberon_generator_reserve_label(ctx
);
3123 oberon_generate_label(ctx
, begin
);
3124 oberon_assert_token(ctx
, REPEAT
);
3126 oberon_statement_seq(ctx
);
3128 oberon_assert_token(ctx
, UNTIL
);
3130 cond
= oberon_expr(ctx
);
3131 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3133 oberon_error(ctx
, "condition must be boolean");
3136 oberon_generate_branch(ctx
, cond
, true, begin
);
3138 else if(ctx
-> token
== FOR
)
3140 oberon_expr_t
* from
;
3141 oberon_expr_t
* index
;
3143 oberon_expr_t
* bound
;
3145 oberon_expr_t
* cond
;
3146 oberon_expr_t
* count
;
3147 gen_label_t
* begin
;
3152 begin
= oberon_generator_reserve_label(ctx
);
3153 end
= oberon_generator_reserve_label(ctx
);
3155 oberon_assert_token(ctx
, FOR
);
3156 iname
= oberon_assert_ident(ctx
);
3157 index
= oberon_ident_item(ctx
, iname
);
3158 oberon_assert_token(ctx
, ASSIGN
);
3159 from
= oberon_expr(ctx
);
3160 oberon_assign(ctx
, from
, index
);
3161 oberon_assert_token(ctx
, TO
);
3162 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3163 to
= oberon_expr(ctx
);
3164 oberon_assign(ctx
, to
, bound
);
3165 if(ctx
-> token
== BY
)
3167 oberon_assert_token(ctx
, BY
);
3168 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3172 by
= oberon_integer_item(ctx
, 1);
3175 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3177 oberon_error(ctx
, "must be integer");
3180 if(by
-> item
.integer
> 0)
3184 else if(by
-> item
.integer
< 0)
3190 oberon_error(ctx
, "zero step not allowed");
3193 oberon_assert_token(ctx
, DO
);
3194 oberon_generate_label(ctx
, begin
);
3195 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3196 oberon_generate_branch(ctx
, cond
, false, end
);
3197 oberon_statement_seq(ctx
);
3198 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3199 oberon_assign(ctx
, count
, index
);
3200 oberon_generate_goto(ctx
, begin
);
3201 oberon_generate_label(ctx
, end
);
3202 oberon_assert_token(ctx
, END
);
3204 else if(ctx
-> token
== LOOP
)
3206 gen_label_t
* begin
;
3209 begin
= oberon_generator_reserve_label(ctx
);
3210 end
= oberon_generator_reserve_label(ctx
);
3212 oberon_open_scope(ctx
);
3213 oberon_assert_token(ctx
, LOOP
);
3214 oberon_generate_label(ctx
, begin
);
3215 ctx
-> decl
-> exit_label
= end
;
3216 oberon_statement_seq(ctx
);
3217 oberon_generate_goto(ctx
, begin
);
3218 oberon_generate_label(ctx
, end
);
3219 oberon_assert_token(ctx
, END
);
3220 oberon_close_scope(ctx
-> decl
);
3222 else if(ctx
-> token
== EXIT
)
3224 oberon_assert_token(ctx
, EXIT
);
3225 if(ctx
-> decl
-> exit_label
== NULL
)
3227 oberon_error(ctx
, "not in LOOP-END");
3229 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3231 else if(ctx
-> token
== RETURN
)
3233 oberon_assert_token(ctx
, RETURN
);
3234 if(ISEXPR(ctx
-> token
))
3236 oberon_expr_t
* expr
;
3237 expr
= oberon_expr(ctx
);
3238 oberon_make_return(ctx
, expr
);
3242 oberon_make_return(ctx
, NULL
);
3248 oberon_statement_seq(oberon_context_t
* ctx
)
3250 oberon_statement(ctx
);
3251 while(ctx
-> token
== SEMICOLON
)
3253 oberon_assert_token(ctx
, SEMICOLON
);
3254 oberon_statement(ctx
);
3259 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3261 oberon_module_t
* m
= ctx
-> module_list
;
3262 while(m
&& strcmp(m
-> name
, name
) != 0)
3270 code
= ctx
-> import_module(name
);
3273 oberon_error(ctx
, "no such module");
3276 m
= oberon_compile_module(ctx
, code
);
3282 oberon_error(ctx
, "cyclic module import");
3285 oberon_object_t
* ident
;
3286 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3287 ident
-> module
= m
;
3291 oberon_import_decl(oberon_context_t
* ctx
)
3296 alias
= name
= oberon_assert_ident(ctx
);
3297 if(ctx
-> token
== ASSIGN
)
3299 oberon_assert_token(ctx
, ASSIGN
);
3300 name
= oberon_assert_ident(ctx
);
3303 oberon_import_module(ctx
, alias
, name
);
3307 oberon_import_list(oberon_context_t
* ctx
)
3309 oberon_assert_token(ctx
, IMPORT
);
3311 oberon_import_decl(ctx
);
3312 while(ctx
-> token
== COMMA
)
3314 oberon_assert_token(ctx
, COMMA
);
3315 oberon_import_decl(ctx
);
3318 oberon_assert_token(ctx
, SEMICOLON
);
3322 oberon_parse_module(oberon_context_t
* ctx
)
3326 oberon_read_token(ctx
);
3328 oberon_assert_token(ctx
, MODULE
);
3329 name1
= oberon_assert_ident(ctx
);
3330 oberon_assert_token(ctx
, SEMICOLON
);
3331 ctx
-> mod
-> name
= name1
;
3333 oberon_generator_init_module(ctx
, ctx
-> mod
);
3335 if(ctx
-> token
== IMPORT
)
3337 oberon_import_list(ctx
);
3340 oberon_decl_seq(ctx
);
3342 oberon_generate_begin_module(ctx
);
3343 if(ctx
-> token
== BEGIN
)
3345 oberon_assert_token(ctx
, BEGIN
);
3346 oberon_statement_seq(ctx
);
3348 oberon_generate_end_module(ctx
);
3350 oberon_assert_token(ctx
, END
);
3351 name2
= oberon_assert_ident(ctx
);
3352 oberon_assert_token(ctx
, DOT
);
3354 if(strcmp(name1
, name2
) != 0)
3356 oberon_error(ctx
, "module name not matched");
3359 oberon_generator_fini_module(ctx
-> mod
);
3362 // =======================================================================
3364 // =======================================================================
3367 register_default_types(oberon_context_t
* ctx
)
3369 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3370 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3372 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3373 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3374 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3376 ctx
-> string_type
= oberon_new_type_string(1);
3377 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3379 ctx
-> bool_type
= oberon_new_type_boolean();
3380 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3382 ctx
-> byte_type
= oberon_new_type_integer(1);
3383 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3385 ctx
-> shortint_type
= oberon_new_type_integer(2);
3386 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3388 ctx
-> int_type
= oberon_new_type_integer(4);
3389 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3391 ctx
-> longint_type
= oberon_new_type_integer(8);
3392 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3394 ctx
-> real_type
= oberon_new_type_real(4);
3395 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3397 ctx
-> longreal_type
= oberon_new_type_real(8);
3398 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3400 ctx
-> char_type
= oberon_new_type_char(1);
3401 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3405 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3407 oberon_object_t
* proc
;
3408 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3409 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3410 proc
-> type
-> sysproc
= true;
3411 proc
-> type
-> genfunc
= f
;
3412 proc
-> type
-> genproc
= p
;
3415 static oberon_expr_t
*
3416 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3420 oberon_error(ctx
, "too few arguments");
3425 oberon_error(ctx
, "too mach arguments");
3428 oberon_expr_t
* arg
;
3431 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3433 oberon_error(ctx
, "MIN accept only type");
3436 oberon_expr_t
* expr
;
3437 int bits
= arg
-> result
-> size
* 8;
3438 switch(arg
-> result
-> class)
3440 case OBERON_TYPE_INTEGER
:
3441 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3444 oberon_error(ctx
, "allowed only basic types");
3451 static oberon_expr_t
*
3452 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3456 oberon_error(ctx
, "too few arguments");
3461 oberon_error(ctx
, "too mach arguments");
3464 oberon_expr_t
* arg
;
3467 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3469 oberon_error(ctx
, "MAX accept only type");
3472 oberon_expr_t
* expr
;
3473 int bits
= arg
-> result
-> size
* 8;
3474 switch(arg
-> result
-> class)
3476 case OBERON_TYPE_INTEGER
:
3477 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3480 oberon_error(ctx
, "allowed only basic types");
3487 static oberon_expr_t
*
3488 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3492 oberon_error(ctx
, "too few arguments");
3497 oberon_error(ctx
, "too mach arguments");
3500 oberon_expr_t
* arg
;
3503 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3505 oberon_error(ctx
, "SIZE accept only type");
3509 oberon_expr_t
* expr
;
3510 oberon_type_t
* type
= arg
-> result
;
3511 switch(type
-> class)
3513 case OBERON_TYPE_INTEGER
:
3514 case OBERON_TYPE_BOOLEAN
:
3515 case OBERON_TYPE_REAL
:
3516 size
= type
-> size
;
3519 oberon_error(ctx
, "TODO SIZE");
3523 expr
= oberon_integer_item(ctx
, size
);
3527 static oberon_expr_t
*
3528 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3532 oberon_error(ctx
, "too few arguments");
3537 oberon_error(ctx
, "too mach arguments");
3540 oberon_expr_t
* arg
;
3542 oberon_check_src(ctx
, arg
);
3544 oberon_type_t
* result_type
;
3545 result_type
= arg
-> result
;
3547 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3549 oberon_error(ctx
, "ABS accepts only integers");
3552 oberon_expr_t
* expr
;
3553 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3558 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3562 oberon_error(ctx
, "too few arguments");
3566 oberon_expr_t
* dst
;
3568 oberon_check_dst(ctx
, dst
);
3570 oberon_type_t
* type
;
3571 type
= dst
-> result
;
3573 if(type
-> class != OBERON_TYPE_POINTER
)
3575 oberon_error(ctx
, "not a pointer");
3578 type
= type
-> base
;
3580 oberon_expr_t
* src
;
3581 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3582 src
-> item
.num_args
= 0;
3583 src
-> item
.args
= NULL
;
3586 if(type
-> class == OBERON_TYPE_ARRAY
)
3588 if(type
-> size
== 0)
3590 oberon_type_t
* x
= type
;
3591 while(x
-> class == OBERON_TYPE_ARRAY
)
3601 if(num_args
< max_args
)
3603 oberon_error(ctx
, "too few arguments");
3606 if(num_args
> max_args
)
3608 oberon_error(ctx
, "too mach arguments");
3611 int num_sizes
= max_args
- 1;
3612 oberon_expr_t
* size_list
= list_args
-> next
;
3614 oberon_expr_t
* arg
= size_list
;
3615 for(int i
= 0; i
< max_args
- 1; i
++)
3617 oberon_check_src(ctx
, arg
);
3618 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3620 oberon_error(ctx
, "size must be integer");
3625 src
-> item
.num_args
= num_sizes
;
3626 src
-> item
.args
= size_list
;
3628 else if(type
-> class != OBERON_TYPE_RECORD
)
3630 oberon_error(ctx
, "oberon_make_new_call: wat");
3633 if(num_args
> max_args
)
3635 oberon_error(ctx
, "too mach arguments");
3638 oberon_assign(ctx
, src
, dst
);
3642 oberon_create_context(ModuleImportCallback import_module
)
3644 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3646 oberon_scope_t
* world_scope
;
3647 world_scope
= oberon_open_scope(ctx
);
3648 ctx
-> world_scope
= world_scope
;
3650 ctx
-> import_module
= import_module
;
3652 oberon_generator_init_context(ctx
);
3654 register_default_types(ctx
);
3657 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3658 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
3659 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
3660 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
3663 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3669 oberon_destroy_context(oberon_context_t
* ctx
)
3671 oberon_generator_destroy_context(ctx
);
3676 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3678 const char * code
= ctx
-> code
;
3679 int code_index
= ctx
-> code_index
;
3681 int token
= ctx
-> token
;
3682 char * string
= ctx
-> string
;
3683 int integer
= ctx
-> integer
;
3684 int real
= ctx
-> real
;
3685 bool longmode
= ctx
-> longmode
;
3686 oberon_scope_t
* decl
= ctx
-> decl
;
3687 oberon_module_t
* mod
= ctx
-> mod
;
3689 oberon_scope_t
* module_scope
;
3690 module_scope
= oberon_open_scope(ctx
);
3692 oberon_module_t
* module
;
3693 module
= calloc(1, sizeof *module
);
3694 module
-> decl
= module_scope
;
3695 module
-> next
= ctx
-> module_list
;
3697 ctx
-> mod
= module
;
3698 ctx
-> module_list
= module
;
3700 oberon_init_scaner(ctx
, newcode
);
3701 oberon_parse_module(ctx
);
3703 module
-> ready
= 1;
3706 ctx
-> code_index
= code_index
;
3708 ctx
-> token
= token
;
3709 ctx
-> string
= string
;
3710 ctx
-> integer
= integer
;
3712 ctx
-> longmode
= longmode
;