9 #include "../include/oberon.h"
11 #include "oberon-internals.h"
12 #include "generator.h"
71 // =======================================================================
73 // =======================================================================
76 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
80 fprintf(stderr
, "error: ");
81 vfprintf(stderr
, fmt
, ptr
);
82 fprintf(stderr
, "\n");
83 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
84 fprintf(stderr
, " c = %c\n", ctx
-> c
);
85 fprintf(stderr
, " token = %i\n", ctx
-> token
);
90 static oberon_type_t
*
91 oberon_new_type_ptr(int class)
93 oberon_type_t
* x
= malloc(sizeof *x
);
94 memset(x
, 0, sizeof *x
);
99 static oberon_type_t
*
100 oberon_new_type_integer(int size
)
103 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
108 static oberon_type_t
*
109 oberon_new_type_boolean()
112 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
116 static oberon_type_t
*
117 oberon_new_type_real(int size
)
120 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
125 static oberon_type_t
*
126 oberon_new_type_char(int size
)
129 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
134 static oberon_type_t
*
135 oberon_new_type_string(int size
)
138 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
143 // =======================================================================
145 // =======================================================================
147 static oberon_scope_t
*
148 oberon_open_scope(oberon_context_t
* ctx
)
150 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
151 oberon_object_t
* list
= calloc(1, sizeof *list
);
154 scope
-> list
= list
;
155 scope
-> up
= ctx
-> decl
;
159 scope
-> local
= scope
-> up
-> local
;
160 scope
-> parent
= scope
-> up
-> parent
;
161 scope
-> parent_type
= scope
-> up
-> parent_type
;
169 oberon_close_scope(oberon_scope_t
* scope
)
171 oberon_context_t
* ctx
= scope
-> ctx
;
172 ctx
-> decl
= scope
-> up
;
175 static oberon_object_t
*
176 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
178 oberon_object_t
* x
= list
;
179 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
186 static oberon_object_t
*
187 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
189 oberon_object_t
* result
= NULL
;
191 oberon_scope_t
* s
= scope
;
192 while(result
== NULL
&& s
!= NULL
)
194 result
= oberon_find_object_in_list(s
-> list
, name
);
198 if(check_it
&& result
== NULL
)
200 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
206 static oberon_object_t
*
207 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
211 if(oberon_find_object(scope
-> up
, name
, false))
213 oberon_error(scope
-> ctx
, "already defined");
217 oberon_object_t
* x
= scope
-> list
;
218 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
225 oberon_error(scope
-> ctx
, "already defined");
228 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
229 memset(newvar
, 0, sizeof *newvar
);
230 newvar
-> name
= name
;
231 newvar
-> class = class;
232 newvar
-> export
= export
;
233 newvar
-> read_only
= read_only
;
234 newvar
-> local
= scope
-> local
;
235 newvar
-> parent
= scope
-> parent
;
236 newvar
-> parent_type
= scope
-> parent_type
;
237 newvar
-> module
= scope
-> ctx
-> mod
;
244 static oberon_object_t
*
245 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
247 oberon_object_t
* id
;
248 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
250 oberon_generator_init_type(scope
-> ctx
, type
);
254 // =======================================================================
256 // =======================================================================
259 oberon_get_char(oberon_context_t
* ctx
)
261 if(ctx
-> code
[ctx
-> code_index
])
263 ctx
-> code_index
+= 1;
264 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
269 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
272 ctx
-> code_index
= 0;
273 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
277 oberon_read_ident(oberon_context_t
* ctx
)
280 int i
= ctx
-> code_index
;
282 int c
= ctx
-> code
[i
];
290 char * ident
= malloc(len
+ 1);
291 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
294 ctx
-> code_index
= i
;
295 ctx
-> c
= ctx
-> code
[i
];
296 ctx
-> string
= ident
;
297 ctx
-> token
= IDENT
;
299 if(strcmp(ident
, "MODULE") == 0)
301 ctx
-> token
= MODULE
;
303 else if(strcmp(ident
, "END") == 0)
307 else if(strcmp(ident
, "VAR") == 0)
311 else if(strcmp(ident
, "BEGIN") == 0)
313 ctx
-> token
= BEGIN
;
315 else if(strcmp(ident
, "TRUE") == 0)
319 else if(strcmp(ident
, "FALSE") == 0)
321 ctx
-> token
= FALSE
;
323 else if(strcmp(ident
, "OR") == 0)
327 else if(strcmp(ident
, "DIV") == 0)
331 else if(strcmp(ident
, "MOD") == 0)
335 else if(strcmp(ident
, "PROCEDURE") == 0)
337 ctx
-> token
= PROCEDURE
;
339 else if(strcmp(ident
, "RETURN") == 0)
341 ctx
-> token
= RETURN
;
343 else if(strcmp(ident
, "CONST") == 0)
345 ctx
-> token
= CONST
;
347 else if(strcmp(ident
, "TYPE") == 0)
351 else if(strcmp(ident
, "ARRAY") == 0)
353 ctx
-> token
= ARRAY
;
355 else if(strcmp(ident
, "OF") == 0)
359 else if(strcmp(ident
, "RECORD") == 0)
361 ctx
-> token
= RECORD
;
363 else if(strcmp(ident
, "POINTER") == 0)
365 ctx
-> token
= POINTER
;
367 else if(strcmp(ident
, "TO") == 0)
371 else if(strcmp(ident
, "NIL") == 0)
375 else if(strcmp(ident
, "IMPORT") == 0)
377 ctx
-> token
= IMPORT
;
379 else if(strcmp(ident
, "IN") == 0)
383 else if(strcmp(ident
, "IS") == 0)
387 else if(strcmp(ident
, "IF") == 0)
391 else if(strcmp(ident
, "THEN") == 0)
395 else if(strcmp(ident
, "ELSE") == 0)
399 else if(strcmp(ident
, "ELSIF") == 0)
401 ctx
-> token
= ELSIF
;
406 oberon_read_number(oberon_context_t
* ctx
)
419 * mode = 3 == LONGREAL
423 start_i
= ctx
-> code_index
;
425 while(isdigit(ctx
-> c
))
427 oberon_get_char(ctx
);
430 end_i
= ctx
-> code_index
;
432 if(isxdigit(ctx
-> c
))
435 while(isxdigit(ctx
-> c
))
437 oberon_get_char(ctx
);
440 end_i
= ctx
-> code_index
;
445 oberon_get_char(ctx
);
447 else if(ctx
-> c
== 'X')
450 oberon_get_char(ctx
);
454 oberon_error(ctx
, "invalid hex number");
457 else if(ctx
-> c
== '.')
460 oberon_get_char(ctx
);
462 while(isdigit(ctx
-> c
))
464 oberon_get_char(ctx
);
467 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
469 exp_i
= ctx
-> code_index
;
476 oberon_get_char(ctx
);
478 if(ctx
-> c
== '+' || ctx
-> c
== '-')
480 oberon_get_char(ctx
);
483 while(isdigit(ctx
-> c
))
485 oberon_get_char(ctx
);
490 end_i
= ctx
-> code_index
;
498 oberon_get_char(ctx
);
500 else if(ctx
-> c
== 'X')
503 oberon_get_char(ctx
);
507 int len
= end_i
- start_i
;
508 ident
= malloc(len
+ 1);
509 memcpy(ident
, &ctx
-> code
[start_i
], len
);
512 ctx
-> longmode
= false;
515 int i
= exp_i
- start_i
;
517 ctx
-> longmode
= true;
523 integer
= atol(ident
);
525 ctx
-> token
= INTEGER
;
528 sscanf(ident
, "%lx", &integer
);
530 ctx
-> token
= INTEGER
;
534 sscanf(ident
, "%lf", &real
);
538 sscanf(ident
, "%lx", &integer
);
543 oberon_error(ctx
, "oberon_read_number: wat");
547 ctx
-> string
= ident
;
548 ctx
-> integer
= integer
;
553 oberon_skip_space(oberon_context_t
* ctx
)
555 while(isspace(ctx
-> c
))
557 oberon_get_char(ctx
);
562 oberon_read_comment(oberon_context_t
* ctx
)
569 oberon_get_char(ctx
);
572 oberon_get_char(ctx
);
576 else if(ctx
-> c
== '*')
578 oberon_get_char(ctx
);
581 oberon_get_char(ctx
);
585 else if(ctx
-> c
== 0)
587 oberon_error(ctx
, "unterminated comment");
591 oberon_get_char(ctx
);
596 static void oberon_read_string(oberon_context_t
* ctx
)
599 oberon_get_char(ctx
);
601 int start
= ctx
-> code_index
;
603 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
605 oberon_get_char(ctx
);
610 oberon_error(ctx
, "unterminated string");
613 int end
= ctx
-> code_index
;
615 oberon_get_char(ctx
);
617 char * string
= calloc(1, end
- start
+ 1);
618 strncpy(string
, &ctx
-> code
[start
], end
- start
);
620 ctx
-> token
= STRING
;
621 ctx
-> string
= string
;
623 printf("oberon_read_string: string ((%s))\n", string
);
626 static void oberon_read_token(oberon_context_t
* ctx
);
629 oberon_read_symbol(oberon_context_t
* ctx
)
638 ctx
-> token
= SEMICOLON
;
639 oberon_get_char(ctx
);
642 ctx
-> token
= COLON
;
643 oberon_get_char(ctx
);
646 ctx
-> token
= ASSIGN
;
647 oberon_get_char(ctx
);
652 oberon_get_char(ctx
);
655 ctx
-> token
= LPAREN
;
656 oberon_get_char(ctx
);
659 oberon_get_char(ctx
);
660 oberon_read_comment(ctx
);
661 oberon_read_token(ctx
);
665 ctx
-> token
= RPAREN
;
666 oberon_get_char(ctx
);
669 ctx
-> token
= EQUAL
;
670 oberon_get_char(ctx
);
674 oberon_get_char(ctx
);
678 oberon_get_char(ctx
);
682 oberon_get_char(ctx
);
686 ctx
-> token
= GREAT
;
687 oberon_get_char(ctx
);
691 oberon_get_char(ctx
);
696 oberon_get_char(ctx
);
699 ctx
-> token
= MINUS
;
700 oberon_get_char(ctx
);
704 oberon_get_char(ctx
);
707 oberon_get_char(ctx
);
708 oberon_error(ctx
, "unstarted comment");
712 ctx
-> token
= SLASH
;
713 oberon_get_char(ctx
);
717 oberon_get_char(ctx
);
721 oberon_get_char(ctx
);
724 ctx
-> token
= COMMA
;
725 oberon_get_char(ctx
);
728 ctx
-> token
= LBRACE
;
729 oberon_get_char(ctx
);
732 ctx
-> token
= RBRACE
;
733 oberon_get_char(ctx
);
736 ctx
-> token
= UPARROW
;
737 oberon_get_char(ctx
);
740 oberon_read_string(ctx
);
743 oberon_read_string(ctx
);
746 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
752 oberon_read_token(oberon_context_t
* ctx
)
754 oberon_skip_space(ctx
);
759 oberon_read_ident(ctx
);
763 oberon_read_number(ctx
);
767 oberon_read_symbol(ctx
);
771 // =======================================================================
773 // =======================================================================
775 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
776 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
777 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
778 static char * oberon_assert_ident(oberon_context_t
* ctx
);
779 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
780 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
781 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
783 static oberon_expr_t
*
784 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
786 oberon_oper_t
* operator;
787 operator = malloc(sizeof *operator);
788 memset(operator, 0, sizeof *operator);
790 operator -> is_item
= 0;
791 operator -> result
= result
;
792 operator -> read_only
= 1;
794 operator -> left
= left
;
795 operator -> right
= right
;
797 return (oberon_expr_t
*) operator;
800 static oberon_expr_t
*
801 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
803 oberon_item_t
* item
;
804 item
= malloc(sizeof *item
);
805 memset(item
, 0, sizeof *item
);
808 item
-> result
= result
;
809 item
-> read_only
= read_only
;
812 return (oberon_expr_t
*)item
;
815 static oberon_expr_t
*
816 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
818 oberon_expr_t
* expr
;
819 oberon_type_t
* result
;
821 result
= a
-> result
;
825 if(result
-> class != OBERON_TYPE_INTEGER
)
827 oberon_error(ctx
, "incompatible operator type");
830 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
832 else if(token
== NOT
)
834 if(result
-> class != OBERON_TYPE_BOOLEAN
)
836 oberon_error(ctx
, "incompatible operator type");
839 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
843 oberon_error(ctx
, "oberon_make_unary_op: wat");
850 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
852 oberon_expr_t
* last
;
857 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
861 *first
= last
= oberon_expr(ctx
);
863 while(ctx
-> token
== COMMA
)
865 oberon_assert_token(ctx
, COMMA
);
866 oberon_expr_t
* current
;
870 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
874 current
= oberon_expr(ctx
);
877 last
-> next
= current
;
883 static oberon_expr_t
*
884 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
886 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
889 static oberon_expr_t
*
890 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
892 oberon_type_t
* from
= expr
-> result
;
893 oberon_type_t
* to
= rec
;
895 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
897 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
899 printf("oberno_make_record_cast: pointers\n");
904 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
906 oberon_error(ctx
, "must be record type");
909 return oberon_cast_expr(ctx
, expr
, rec
);
912 static oberon_type_t
*
913 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
915 oberon_type_t
* result
;
916 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
920 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
924 else if(a
-> class != b
-> class)
926 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
928 else if(a
-> size
> b
-> size
)
941 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
943 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
949 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
951 oberon_error(ctx
, "not a record");
954 oberon_type_t
* t
= from
;
955 while(t
!= NULL
&& t
!= to
)
962 oberon_error(ctx
, "incompatible record types");
966 static oberon_expr_t
*
967 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
970 // Если классы типов равны
971 // Если INTEGER переводится в REAL
972 // Есди STRING переводится в ARRAY OF CHAR
975 if(pref
-> class != expr
-> result
-> class)
977 printf("expr class %i\n", expr
-> result
-> class);
978 printf("pref class %i\n", pref
-> class);
980 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
982 if(pref
-> class == OBERON_TYPE_ARRAY
)
984 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
994 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
996 if(pref
-> class != OBERON_TYPE_REAL
)
1009 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1012 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1014 if(expr
-> result
-> size
> pref
-> size
)
1016 oberon_error(ctx
, "incompatible size");
1020 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1023 else if(pref
-> class == OBERON_TYPE_RECORD
)
1025 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1026 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1028 else if(pref
-> class == OBERON_TYPE_POINTER
)
1030 assert(pref
-> base
);
1031 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1033 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1034 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1036 else if(expr
-> result
-> base
!= pref
-> base
)
1038 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1040 oberon_error(ctx
, "incompatible pointer types");
1049 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1051 oberon_type_t
* a
= (*ea
) -> result
;
1052 oberon_type_t
* b
= (*eb
) -> result
;
1053 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1054 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1055 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1059 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1061 if(desig
-> mode
!= MODE_CALL
)
1063 oberon_error(ctx
, "expected mode CALL");
1066 oberon_type_t
* fn
= desig
-> parent
-> result
;
1067 int num_args
= desig
-> num_args
;
1068 int num_decl
= fn
-> num_decl
;
1070 if(num_args
< num_decl
)
1072 oberon_error(ctx
, "too few arguments");
1074 else if(num_args
> num_decl
)
1076 oberon_error(ctx
, "too many arguments");
1079 /* Делаем проверку на запись и делаем автокаст */
1080 oberon_expr_t
* casted
[num_args
];
1081 oberon_expr_t
* arg
= desig
-> args
;
1082 oberon_object_t
* param
= fn
-> decl
;
1083 for(int i
= 0; i
< num_args
; i
++)
1085 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1087 if(arg
-> read_only
)
1089 oberon_error(ctx
, "assign to read-only var");
1093 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1095 param
= param
-> next
;
1098 /* Создаём новый список выражений */
1102 for(int i
= 0; i
< num_args
- 1; i
++)
1104 casted
[i
] -> next
= casted
[i
+ 1];
1106 desig
-> args
= arg
;
1110 static oberon_expr_t
*
1111 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1113 oberon_type_t
* signature
= item
-> result
;
1114 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1116 oberon_error(ctx
, "not a procedure");
1119 oberon_expr_t
* call
;
1121 if(signature
-> sysproc
)
1123 if(signature
-> genfunc
== NULL
)
1125 oberon_error(ctx
, "not a function-procedure");
1128 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1132 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1134 oberon_error(ctx
, "attempt to call procedure in expression");
1137 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1138 call
-> item
.parent
= item
;
1139 call
-> item
.num_args
= num_args
;
1140 call
-> item
.args
= list_args
;
1141 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1148 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1150 oberon_type_t
* signature
= item
-> result
;
1151 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1153 oberon_error(ctx
, "not a procedure");
1156 oberon_expr_t
* call
;
1158 if(signature
-> sysproc
)
1160 if(signature
-> genproc
== NULL
)
1162 oberon_error(ctx
, "not a procedure");
1165 signature
-> genproc(ctx
, num_args
, list_args
);
1169 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1171 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1174 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1175 call
-> item
.parent
= item
;
1176 call
-> item
.num_args
= num_args
;
1177 call
-> item
.args
= list_args
;
1178 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1179 oberon_generate_call_proc(ctx
, call
);
1185 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
1187 switch(proc -> class)
1189 case OBERON_CLASS_PROC:
1190 if(proc -> class != OBERON_CLASS_PROC)
1192 oberon_error(ctx, "not a procedure");
1195 case OBERON_CLASS_VAR:
1196 case OBERON_CLASS_VAR_PARAM:
1197 case OBERON_CLASS_PARAM:
1198 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
1200 oberon_error(ctx, "not a procedure");
1204 oberon_error(ctx, "not a procedure");
1210 if(proc -> genproc == NULL)
1212 oberon_error(ctx, "requres non-typed procedure");
1215 proc -> genproc(ctx, num_args, list_args);
1219 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
1221 oberon_error(ctx, "attempt to call function as non-typed procedure");
1224 oberon_expr_t * call;
1225 call = oberon_new_item(MODE_CALL, proc -> type -> base, 1);
1226 call -> item.var = proc;
1227 call -> item.num_args = num_args;
1228 call -> item.args = list_args;
1229 oberon_autocast_call(ctx, call);
1230 oberon_generate_call_proc(ctx, call);
1239 || ((x) == INTEGER) \
1242 || ((x) == STRING) \
1244 || ((x) == LPAREN) \
1249 static oberon_expr_t
*
1250 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1252 printf("oberno_make_dereferencing\n");
1253 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1255 oberon_error(ctx
, "not a pointer");
1258 assert(expr
-> is_item
);
1260 oberon_expr_t
* selector
;
1261 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1262 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1267 static oberon_expr_t
*
1268 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1270 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1272 desig
= oberno_make_dereferencing(ctx
, desig
);
1275 assert(desig
-> is_item
);
1277 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1279 oberon_error(ctx
, "not array");
1282 oberon_type_t
* base
;
1283 base
= desig
-> result
-> base
;
1285 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1287 oberon_error(ctx
, "index must be integer");
1290 // Статическая проверка границ массива
1291 if(desig
-> result
-> size
!= 0)
1293 if(index
-> is_item
)
1295 if(index
-> item
.mode
== MODE_INTEGER
)
1297 int arr_size
= desig
-> result
-> size
;
1298 int index_int
= index
-> item
.integer
;
1299 if(index_int
< 0 || index_int
> arr_size
- 1)
1301 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1307 oberon_expr_t
* selector
;
1308 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1309 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1310 selector
-> item
.num_args
= 1;
1311 selector
-> item
.args
= index
;
1316 static oberon_expr_t
*
1317 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1319 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1321 expr
= oberno_make_dereferencing(ctx
, expr
);
1324 assert(expr
-> is_item
);
1326 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1328 oberon_error(ctx
, "not record");
1331 oberon_type_t
* rec
= expr
-> result
;
1333 oberon_object_t
* field
;
1334 field
= oberon_find_object(rec
-> scope
, name
, true);
1336 if(field
-> export
== 0)
1338 if(field
-> module
!= ctx
-> mod
)
1340 oberon_error(ctx
, "field not exported");
1345 if(field
-> read_only
)
1347 if(field
-> module
!= ctx
-> mod
)
1353 oberon_expr_t
* selector
;
1354 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1355 selector
-> item
.var
= field
;
1356 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1361 #define ISSELECTOR(x) \
1364 || ((x) == UPARROW) \
1367 static oberon_object_t
*
1368 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1371 oberon_object_t
* x
;
1373 name
= oberon_assert_ident(ctx
);
1374 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1378 if(x
-> class == OBERON_CLASS_MODULE
)
1380 oberon_assert_token(ctx
, DOT
);
1381 name
= oberon_assert_ident(ctx
);
1382 /* Наличие объектов в левых модулях всегда проверяется */
1383 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1385 if(x
-> export
== 0)
1387 oberon_error(ctx
, "not exported");
1400 static oberon_expr_t
*
1401 oberon_designator(oberon_context_t
* ctx
)
1404 oberon_object_t
* var
;
1405 oberon_expr_t
* expr
;
1407 var
= oberon_qualident(ctx
, NULL
, 1);
1410 if(var
-> read_only
)
1412 if(var
-> module
!= ctx
-> mod
)
1418 switch(var
-> class)
1420 case OBERON_CLASS_CONST
:
1422 expr
= (oberon_expr_t
*) var
-> value
;
1424 case OBERON_CLASS_VAR
:
1425 case OBERON_CLASS_VAR_PARAM
:
1426 case OBERON_CLASS_PARAM
:
1427 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1429 case OBERON_CLASS_PROC
:
1430 expr
= oberon_new_item(MODE_VAR
, var
-> type
, 1);
1433 oberon_error(ctx
, "invalid designator");
1436 expr
-> item
.var
= var
;
1438 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1440 switch(ctx
-> token
)
1443 oberon_assert_token(ctx
, DOT
);
1444 name
= oberon_assert_ident(ctx
);
1445 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1448 oberon_assert_token(ctx
, LBRACE
);
1449 int num_indexes
= 0;
1450 oberon_expr_t
* indexes
= NULL
;
1451 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1452 oberon_assert_token(ctx
, RBRACE
);
1454 for(int i
= 0; i
< num_indexes
; i
++)
1456 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1457 indexes
= indexes
-> next
;
1461 oberon_assert_token(ctx
, UPARROW
);
1462 expr
= oberno_make_dereferencing(ctx
, expr
);
1465 oberon_assert_token(ctx
, LPAREN
);
1466 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1467 if(objtype
-> class != OBERON_CLASS_TYPE
)
1469 oberon_error(ctx
, "must be type");
1471 oberon_assert_token(ctx
, RPAREN
);
1472 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1475 oberon_error(ctx
, "oberon_designator: wat");
1483 static oberon_expr_t
*
1484 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1486 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1487 if(ctx
-> token
== LPAREN
)
1489 oberon_assert_token(ctx
, LPAREN
);
1492 oberon_expr_t
* arguments
= NULL
;
1494 if(ISEXPR(ctx
-> token
))
1496 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1499 assert(expr
-> is_item
== 1);
1500 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1502 oberon_assert_token(ctx
, RPAREN
);
1509 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1511 assert(expr
-> is_item
);
1514 oberon_expr_t
* arguments
= NULL
;
1516 if(ctx
-> token
== LPAREN
)
1518 oberon_assert_token(ctx
, LPAREN
);
1520 if(ISEXPR(ctx
-> token
))
1522 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1525 oberon_assert_token(ctx
, RPAREN
);
1528 /* Вызов происходит даже без скобок */
1529 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1532 static oberon_type_t
*
1533 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1535 if(i
>= -128 && i
<= 127)
1537 return ctx
-> byte_type
;
1539 else if(i
>= -32768 && i
<= 32767)
1541 return ctx
-> shortint_type
;
1543 else if(i
>= -2147483648 && i
<= 2147483647)
1545 return ctx
-> int_type
;
1549 return ctx
-> longint_type
;
1553 static oberon_expr_t
*
1554 oberon_factor(oberon_context_t
* ctx
)
1556 oberon_expr_t
* expr
;
1557 oberon_type_t
* result
;
1559 switch(ctx
-> token
)
1562 expr
= oberon_designator(ctx
);
1563 expr
= oberon_opt_func_parens(ctx
, expr
);
1566 result
= oberon_get_type_of_int_value(ctx
, ctx
-> integer
);
1567 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1568 expr
-> item
.integer
= ctx
-> integer
;
1569 oberon_assert_token(ctx
, INTEGER
);
1572 result
= ctx
-> char_type
;
1573 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1574 expr
-> item
.integer
= ctx
-> integer
;
1575 oberon_assert_token(ctx
, CHAR
);
1578 result
= ctx
-> string_type
;
1579 expr
= oberon_new_item(MODE_STRING
, result
, true);
1580 expr
-> item
.string
= ctx
-> string
;
1581 oberon_assert_token(ctx
, STRING
);
1584 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1585 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1586 expr
-> item
.real
= ctx
-> real
;
1587 oberon_assert_token(ctx
, REAL
);
1590 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1591 expr
-> item
.boolean
= true;
1592 oberon_assert_token(ctx
, TRUE
);
1595 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1596 expr
-> item
.boolean
= false;
1597 oberon_assert_token(ctx
, FALSE
);
1600 oberon_assert_token(ctx
, LPAREN
);
1601 expr
= oberon_expr(ctx
);
1602 oberon_assert_token(ctx
, RPAREN
);
1605 oberon_assert_token(ctx
, NOT
);
1606 expr
= oberon_factor(ctx
);
1607 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1610 oberon_assert_token(ctx
, NIL
);
1611 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1614 oberon_error(ctx
, "invalid expression");
1620 #define ITMAKESBOOLEAN(x) \
1621 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1623 #define ITUSEONLYINTEGER(x) \
1624 ((x) >= LESS && (x) <= GEQ)
1626 #define ITUSEONLYBOOLEAN(x) \
1627 (((x) == OR) || ((x) == AND))
1630 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1632 oberon_expr_t
* expr
= *e
;
1633 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1635 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1637 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1641 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1644 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1646 oberon_error(ctx
, "required numeric type");
1650 static oberon_expr_t
*
1651 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1653 oberon_expr_t
* expr
;
1654 oberon_type_t
* result
;
1656 if(ITMAKESBOOLEAN(token
))
1658 if(ITUSEONLYINTEGER(token
))
1660 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1661 || b
-> result
-> class == OBERON_TYPE_INTEGER
1662 || a
-> result
-> class == OBERON_TYPE_REAL
1663 || b
-> result
-> class == OBERON_TYPE_REAL
)
1669 oberon_error(ctx
, "used only with numeric types");
1672 else if(ITUSEONLYBOOLEAN(token
))
1674 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1675 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1677 oberon_error(ctx
, "used only with boolean type");
1681 oberon_autocast_binary_op(ctx
, &a
, &b
);
1682 result
= ctx
-> bool_type
;
1686 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1688 else if(token
== NEQ
)
1690 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1692 else if(token
== LESS
)
1694 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1696 else if(token
== LEQ
)
1698 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1700 else if(token
== GREAT
)
1702 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1704 else if(token
== GEQ
)
1706 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1708 else if(token
== OR
)
1710 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1712 else if(token
== AND
)
1714 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1718 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1721 else if(token
== SLASH
)
1723 oberon_autocast_to_real(ctx
, &a
);
1724 oberon_autocast_to_real(ctx
, &b
);
1725 oberon_autocast_binary_op(ctx
, &a
, &b
);
1726 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1728 else if(token
== DIV
)
1730 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1731 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1733 oberon_error(ctx
, "operator DIV requires integer type");
1736 oberon_autocast_binary_op(ctx
, &a
, &b
);
1737 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1741 oberon_autocast_binary_op(ctx
, &a
, &b
);
1745 expr
= oberon_new_operator(OP_ADD
, a
-> result
, a
, b
);
1747 else if(token
== MINUS
)
1749 expr
= oberon_new_operator(OP_SUB
, a
-> result
, a
, b
);
1751 else if(token
== STAR
)
1753 expr
= oberon_new_operator(OP_MUL
, a
-> result
, a
, b
);
1755 else if(token
== MOD
)
1757 expr
= oberon_new_operator(OP_MOD
, a
-> result
, a
, b
);
1761 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1768 #define ISMULOP(x) \
1769 ((x) >= STAR && (x) <= AND)
1771 static oberon_expr_t
*
1772 oberon_term_expr(oberon_context_t
* ctx
)
1774 oberon_expr_t
* expr
;
1776 expr
= oberon_factor(ctx
);
1777 while(ISMULOP(ctx
-> token
))
1779 int token
= ctx
-> token
;
1780 oberon_read_token(ctx
);
1782 oberon_expr_t
* inter
= oberon_factor(ctx
);
1783 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1789 #define ISADDOP(x) \
1790 ((x) >= PLUS && (x) <= OR)
1792 static oberon_expr_t
*
1793 oberon_simple_expr(oberon_context_t
* ctx
)
1795 oberon_expr_t
* expr
;
1798 if(ctx
-> token
== PLUS
)
1801 oberon_assert_token(ctx
, PLUS
);
1803 else if(ctx
-> token
== MINUS
)
1806 oberon_assert_token(ctx
, MINUS
);
1809 expr
= oberon_term_expr(ctx
);
1813 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1816 while(ISADDOP(ctx
-> token
))
1818 int token
= ctx
-> token
;
1819 oberon_read_token(ctx
);
1821 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1822 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1828 #define ISRELATION(x) \
1829 ((x) >= EQUAL && (x) <= IS)
1831 static oberon_expr_t
*
1832 oberon_expr(oberon_context_t
* ctx
)
1834 oberon_expr_t
* expr
;
1836 expr
= oberon_simple_expr(ctx
);
1837 while(ISRELATION(ctx
-> token
))
1839 int token
= ctx
-> token
;
1840 oberon_read_token(ctx
);
1842 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1843 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1849 static oberon_item_t
*
1850 oberon_const_expr(oberon_context_t
* ctx
)
1852 oberon_expr_t
* expr
;
1853 expr
= oberon_expr(ctx
);
1855 if(expr
-> is_item
== 0)
1857 oberon_error(ctx
, "const expression are required");
1860 return (oberon_item_t
*) expr
;
1863 // =======================================================================
1865 // =======================================================================
1867 static void oberon_decl_seq(oberon_context_t
* ctx
);
1868 static void oberon_statement_seq(oberon_context_t
* ctx
);
1869 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1872 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1874 if(ctx
-> token
!= token
)
1876 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
1881 oberon_assert_token(oberon_context_t
* ctx
, int token
)
1883 oberon_expect_token(ctx
, token
);
1884 oberon_read_token(ctx
);
1888 oberon_assert_ident(oberon_context_t
* ctx
)
1890 oberon_expect_token(ctx
, IDENT
);
1891 char * ident
= ctx
-> string
;
1892 oberon_read_token(ctx
);
1897 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
1899 switch(ctx
-> token
)
1902 oberon_assert_token(ctx
, STAR
);
1907 oberon_assert_token(ctx
, MINUS
);
1918 static oberon_object_t
*
1919 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
1924 oberon_object_t
* x
;
1926 name
= oberon_assert_ident(ctx
);
1927 oberon_def(ctx
, &export
, &read_only
);
1929 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
1934 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
1937 *list
= oberon_ident_def(ctx
, class, check_upscope
);
1938 while(ctx
-> token
== COMMA
)
1940 oberon_assert_token(ctx
, COMMA
);
1941 oberon_ident_def(ctx
, class, check_upscope
);
1947 oberon_var_decl(oberon_context_t
* ctx
)
1950 oberon_object_t
* list
;
1951 oberon_type_t
* type
;
1952 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1954 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
1955 oberon_assert_token(ctx
, COLON
);
1956 oberon_type(ctx
, &type
);
1958 oberon_object_t
* var
= list
;
1959 for(int i
= 0; i
< num
; i
++)
1966 static oberon_object_t
*
1967 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
1969 int class = OBERON_CLASS_PARAM
;
1970 if(ctx
-> token
== VAR
)
1972 oberon_read_token(ctx
);
1973 class = OBERON_CLASS_VAR_PARAM
;
1977 oberon_object_t
* list
;
1978 oberon_ident_list(ctx
, class, false, &num
, &list
);
1980 oberon_assert_token(ctx
, COLON
);
1982 oberon_type_t
* type
;
1983 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1984 oberon_type(ctx
, &type
);
1986 oberon_object_t
* param
= list
;
1987 for(int i
= 0; i
< num
; i
++)
1989 param
-> type
= type
;
1990 param
= param
-> next
;
1997 #define ISFPSECTION \
1998 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2001 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2003 oberon_assert_token(ctx
, LPAREN
);
2007 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2008 while(ctx
-> token
== SEMICOLON
)
2010 oberon_assert_token(ctx
, SEMICOLON
);
2011 oberon_fp_section(ctx
, &signature
-> num_decl
);
2015 oberon_assert_token(ctx
, RPAREN
);
2017 if(ctx
-> token
== COLON
)
2019 oberon_assert_token(ctx
, COLON
);
2021 oberon_object_t
* typeobj
;
2022 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2023 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2025 oberon_error(ctx
, "function result is not type");
2027 signature
-> base
= typeobj
-> type
;
2032 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2034 oberon_type_t
* signature
;
2036 signature
-> class = OBERON_TYPE_PROCEDURE
;
2037 signature
-> num_decl
= 0;
2038 signature
-> base
= ctx
-> void_type
;
2039 signature
-> decl
= NULL
;
2041 if(ctx
-> token
== LPAREN
)
2043 oberon_formal_pars(ctx
, signature
);
2048 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2050 if(a
-> num_decl
!= b
-> num_decl
)
2052 oberon_error(ctx
, "number parameters not matched");
2055 int num_param
= a
-> num_decl
;
2056 oberon_object_t
* param_a
= a
-> decl
;
2057 oberon_object_t
* param_b
= b
-> decl
;
2058 for(int i
= 0; i
< num_param
; i
++)
2060 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2062 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2065 if(param_a
-> type
!= param_b
-> type
)
2067 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2070 param_a
= param_a
-> next
;
2071 param_b
= param_b
-> next
;
2076 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2078 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2079 oberon_type_t
* result_type
= proc
-> type
-> base
;
2081 if(result_type
-> class == OBERON_TYPE_VOID
)
2085 oberon_error(ctx
, "procedure has no result type");
2092 oberon_error(ctx
, "procedure requires expression on result");
2095 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2098 proc
-> has_return
= 1;
2100 oberon_generate_return(ctx
, expr
);
2104 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2106 oberon_assert_token(ctx
, SEMICOLON
);
2108 ctx
-> decl
= proc
-> scope
;
2110 oberon_decl_seq(ctx
);
2112 oberon_generate_begin_proc(ctx
, proc
);
2114 if(ctx
-> token
== BEGIN
)
2116 oberon_assert_token(ctx
, BEGIN
);
2117 oberon_statement_seq(ctx
);
2120 oberon_assert_token(ctx
, END
);
2121 char * name
= oberon_assert_ident(ctx
);
2122 if(strcmp(name
, proc
-> name
) != 0)
2124 oberon_error(ctx
, "procedure name not matched");
2127 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2128 && proc
-> has_return
== 0)
2130 oberon_make_return(ctx
, NULL
);
2133 if(proc
-> has_return
== 0)
2135 oberon_error(ctx
, "procedure requires return");
2138 oberon_generate_end_proc(ctx
);
2139 oberon_close_scope(ctx
-> decl
);
2143 oberon_proc_decl(oberon_context_t
* ctx
)
2145 oberon_assert_token(ctx
, PROCEDURE
);
2148 if(ctx
-> token
== UPARROW
)
2150 oberon_assert_token(ctx
, UPARROW
);
2157 name
= oberon_assert_ident(ctx
);
2158 oberon_def(ctx
, &export
, &read_only
);
2160 oberon_scope_t
* proc_scope
;
2161 proc_scope
= oberon_open_scope(ctx
);
2162 ctx
-> decl
-> local
= 1;
2164 oberon_type_t
* signature
;
2165 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2166 oberon_opt_formal_pars(ctx
, &signature
);
2168 oberon_initialize_decl(ctx
);
2169 oberon_generator_init_type(ctx
, signature
);
2170 oberon_close_scope(ctx
-> decl
);
2172 oberon_object_t
* proc
;
2173 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2176 if(proc
-> class != OBERON_CLASS_PROC
)
2178 oberon_error(ctx
, "mult definition");
2185 oberon_error(ctx
, "mult procedure definition");
2189 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2191 oberon_error(ctx
, "export type not matched");
2194 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2198 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2199 proc
-> type
= signature
;
2200 proc
-> scope
= proc_scope
;
2201 oberon_generator_init_proc(ctx
, proc
);
2204 proc
-> scope
-> parent
= proc
;
2209 oberon_proc_decl_body(ctx
, proc
);
2214 oberon_const_decl(oberon_context_t
* ctx
)
2216 oberon_item_t
* value
;
2217 oberon_object_t
* constant
;
2219 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2220 oberon_assert_token(ctx
, EQUAL
);
2221 value
= oberon_const_expr(ctx
);
2222 constant
-> value
= value
;
2226 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2228 if(size
-> is_item
== 0)
2230 oberon_error(ctx
, "requires constant");
2233 if(size
-> item
.mode
!= MODE_INTEGER
)
2235 oberon_error(ctx
, "requires integer constant");
2238 oberon_type_t
* arr
;
2240 arr
-> class = OBERON_TYPE_ARRAY
;
2241 arr
-> size
= size
-> item
.integer
;
2246 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2249 oberon_object_t
* to
;
2251 to
= oberon_qualident(ctx
, &name
, 0);
2253 //name = oberon_assert_ident(ctx);
2254 //to = oberon_find_object(ctx -> decl, name, 0);
2258 if(to
-> class != OBERON_CLASS_TYPE
)
2260 oberon_error(ctx
, "not a type");
2265 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2266 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2272 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2275 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2279 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2287 oberon_type_t
* dim
;
2288 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2290 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2292 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2296 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2298 type
-> class = OBERON_TYPE_ARRAY
;
2300 type
-> base
= base
;
2304 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2306 if(ctx
-> token
== IDENT
)
2309 oberon_object_t
* list
;
2310 oberon_type_t
* type
;
2311 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2313 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2314 oberon_assert_token(ctx
, COLON
);
2316 oberon_scope_t
* current
= ctx
-> decl
;
2317 ctx
-> decl
= modscope
;
2318 oberon_type(ctx
, &type
);
2319 ctx
-> decl
= current
;
2321 oberon_object_t
* field
= list
;
2322 for(int i
= 0; i
< num
; i
++)
2324 field
-> type
= type
;
2325 field
= field
-> next
;
2328 rec
-> num_decl
+= num
;
2333 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2335 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2336 oberon_scope_t
* oldscope
= ctx
-> decl
;
2337 ctx
-> decl
= modscope
;
2339 if(ctx
-> token
== LPAREN
)
2341 oberon_assert_token(ctx
, LPAREN
);
2343 oberon_object_t
* typeobj
;
2344 typeobj
= oberon_qualident(ctx
, NULL
, true);
2346 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2348 oberon_error(ctx
, "base must be type");
2351 oberon_type_t
* base
= typeobj
-> type
;
2352 if(base
-> class == OBERON_TYPE_POINTER
)
2354 base
= base
-> base
;
2357 if(base
-> class != OBERON_TYPE_RECORD
)
2359 oberon_error(ctx
, "base must be record type");
2363 ctx
-> decl
= base
-> scope
;
2365 oberon_assert_token(ctx
, RPAREN
);
2372 oberon_scope_t
* this_scope
;
2373 this_scope
= oberon_open_scope(ctx
);
2374 this_scope
-> local
= true;
2375 this_scope
-> parent
= NULL
;
2376 this_scope
-> parent_type
= rec
;
2378 oberon_field_list(ctx
, rec
, modscope
);
2379 while(ctx
-> token
== SEMICOLON
)
2381 oberon_assert_token(ctx
, SEMICOLON
);
2382 oberon_field_list(ctx
, rec
, modscope
);
2385 rec
-> scope
= this_scope
;
2386 rec
-> decl
= this_scope
-> list
-> next
;
2387 ctx
-> decl
= oldscope
;
2391 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2393 if(ctx
-> token
== IDENT
)
2395 oberon_qualident_type(ctx
, type
);
2397 else if(ctx
-> token
== ARRAY
)
2399 oberon_assert_token(ctx
, ARRAY
);
2402 oberon_expr_t
* sizes
;
2404 if(ISEXPR(ctx
-> token
))
2406 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2409 oberon_assert_token(ctx
, OF
);
2411 oberon_type_t
* base
;
2412 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2413 oberon_type(ctx
, &base
);
2417 oberon_make_open_array(ctx
, base
, *type
);
2421 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2424 else if(ctx
-> token
== RECORD
)
2426 oberon_type_t
* rec
;
2428 rec
-> class = OBERON_TYPE_RECORD
;
2429 rec
-> module
= ctx
-> mod
;
2431 oberon_assert_token(ctx
, RECORD
);
2432 oberon_type_record_body(ctx
, rec
);
2433 oberon_assert_token(ctx
, END
);
2437 else if(ctx
-> token
== POINTER
)
2439 oberon_assert_token(ctx
, POINTER
);
2440 oberon_assert_token(ctx
, TO
);
2442 oberon_type_t
* base
;
2443 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2444 oberon_type(ctx
, &base
);
2446 oberon_type_t
* ptr
;
2448 ptr
-> class = OBERON_TYPE_POINTER
;
2451 else if(ctx
-> token
== PROCEDURE
)
2453 oberon_open_scope(ctx
);
2454 oberon_assert_token(ctx
, PROCEDURE
);
2455 oberon_opt_formal_pars(ctx
, type
);
2456 oberon_close_scope(ctx
-> decl
);
2460 oberon_error(ctx
, "invalid type declaration");
2465 oberon_type_decl(oberon_context_t
* ctx
)
2468 oberon_object_t
* newtype
;
2469 oberon_type_t
* type
;
2473 name
= oberon_assert_ident(ctx
);
2474 oberon_def(ctx
, &export
, &read_only
);
2476 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2479 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2480 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2481 assert(newtype
-> type
);
2485 if(newtype
-> class != OBERON_CLASS_TYPE
)
2487 oberon_error(ctx
, "mult definition");
2490 if(newtype
-> linked
)
2492 oberon_error(ctx
, "mult definition - already linked");
2495 newtype
-> export
= export
;
2496 newtype
-> read_only
= read_only
;
2499 oberon_assert_token(ctx
, EQUAL
);
2501 type
= newtype
-> type
;
2502 oberon_type(ctx
, &type
);
2504 if(type
-> class == OBERON_TYPE_VOID
)
2506 oberon_error(ctx
, "recursive alias declaration");
2509 newtype
-> type
= type
;
2510 newtype
-> linked
= 1;
2513 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2514 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2517 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2519 if(type
-> class != OBERON_TYPE_POINTER
2520 && type
-> class != OBERON_TYPE_ARRAY
)
2525 if(type
-> recursive
)
2527 oberon_error(ctx
, "recursive pointer declaration");
2530 if(type
-> class == OBERON_TYPE_POINTER
2531 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2533 oberon_error(ctx
, "attempt to make pointer to pointer");
2536 type
-> recursive
= 1;
2538 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2540 type
-> recursive
= 0;
2544 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2546 if(type
-> class != OBERON_TYPE_RECORD
)
2551 if(type
-> recursive
)
2553 oberon_error(ctx
, "recursive record declaration");
2556 type
-> recursive
= 1;
2558 int num_fields
= type
-> num_decl
;
2559 oberon_object_t
* field
= type
-> decl
;
2560 for(int i
= 0; i
< num_fields
; i
++)
2562 oberon_prevent_recursive_object(ctx
, field
);
2563 field
= field
-> next
;
2566 type
-> recursive
= 0;
2569 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2571 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2576 if(type
-> recursive
)
2578 oberon_error(ctx
, "recursive procedure declaration");
2581 type
-> recursive
= 1;
2583 int num_fields
= type
-> num_decl
;
2584 oberon_object_t
* field
= type
-> decl
;
2585 for(int i
= 0; i
< num_fields
; i
++)
2587 oberon_prevent_recursive_object(ctx
, field
);
2588 field
= field
-> next
;
2591 type
-> recursive
= 0;
2595 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2597 if(type
-> class != OBERON_TYPE_ARRAY
)
2602 if(type
-> recursive
)
2604 oberon_error(ctx
, "recursive array declaration");
2607 type
-> recursive
= 1;
2609 oberon_prevent_recursive_type(ctx
, type
-> base
);
2611 type
-> recursive
= 0;
2615 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2617 if(type
-> class == OBERON_TYPE_POINTER
)
2619 oberon_prevent_recursive_pointer(ctx
, type
);
2621 else if(type
-> class == OBERON_TYPE_RECORD
)
2623 oberon_prevent_recursive_record(ctx
, type
);
2625 else if(type
-> class == OBERON_TYPE_ARRAY
)
2627 oberon_prevent_recursive_array(ctx
, type
);
2629 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2631 oberon_prevent_recursive_procedure(ctx
, type
);
2636 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2640 case OBERON_CLASS_VAR
:
2641 case OBERON_CLASS_TYPE
:
2642 case OBERON_CLASS_PARAM
:
2643 case OBERON_CLASS_VAR_PARAM
:
2644 case OBERON_CLASS_FIELD
:
2645 oberon_prevent_recursive_type(ctx
, x
-> type
);
2647 case OBERON_CLASS_CONST
:
2648 case OBERON_CLASS_PROC
:
2649 case OBERON_CLASS_MODULE
:
2652 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2658 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2660 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2664 oberon_prevent_recursive_object(ctx
, x
);
2669 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2670 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2673 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2675 if(type
-> class != OBERON_TYPE_RECORD
)
2680 int num_fields
= type
-> num_decl
;
2681 oberon_object_t
* field
= type
-> decl
;
2682 for(int i
= 0; i
< num_fields
; i
++)
2684 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2686 oberon_initialize_type(ctx
, field
-> type
);
2689 oberon_initialize_object(ctx
, field
);
2690 field
= field
-> next
;
2693 oberon_generator_init_record(ctx
, type
);
2697 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2699 if(type
-> class == OBERON_TYPE_VOID
)
2701 oberon_error(ctx
, "undeclarated type");
2704 if(type
-> initialized
)
2709 type
-> initialized
= 1;
2711 if(type
-> class == OBERON_TYPE_POINTER
)
2713 oberon_initialize_type(ctx
, type
-> base
);
2714 oberon_generator_init_type(ctx
, type
);
2716 else if(type
-> class == OBERON_TYPE_ARRAY
)
2718 if(type
-> size
!= 0)
2720 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2722 if(type
-> base
-> size
== 0)
2724 oberon_error(ctx
, "open array not allowed as array element");
2729 oberon_initialize_type(ctx
, type
-> base
);
2730 oberon_generator_init_type(ctx
, type
);
2732 else if(type
-> class == OBERON_TYPE_RECORD
)
2734 oberon_generator_init_type(ctx
, type
);
2735 oberon_initialize_record_fields(ctx
, type
);
2737 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2739 int num_fields
= type
-> num_decl
;
2740 oberon_object_t
* field
= type
-> decl
;
2741 for(int i
= 0; i
< num_fields
; i
++)
2743 oberon_initialize_object(ctx
, field
);
2744 field
= field
-> next
;
2747 oberon_generator_init_type(ctx
, type
);
2751 oberon_generator_init_type(ctx
, type
);
2756 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2758 if(x
-> initialized
)
2763 x
-> initialized
= 1;
2767 case OBERON_CLASS_TYPE
:
2768 oberon_initialize_type(ctx
, x
-> type
);
2770 case OBERON_CLASS_VAR
:
2771 case OBERON_CLASS_FIELD
:
2772 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2774 if(x
-> type
-> size
== 0)
2776 oberon_error(ctx
, "open array not allowed as variable or field");
2779 oberon_initialize_type(ctx
, x
-> type
);
2780 oberon_generator_init_var(ctx
, x
);
2782 case OBERON_CLASS_PARAM
:
2783 case OBERON_CLASS_VAR_PARAM
:
2784 oberon_initialize_type(ctx
, x
-> type
);
2785 oberon_generator_init_var(ctx
, x
);
2787 case OBERON_CLASS_CONST
:
2788 case OBERON_CLASS_PROC
:
2789 case OBERON_CLASS_MODULE
:
2792 oberon_error(ctx
, "oberon_initialize_object: wat");
2798 oberon_initialize_decl(oberon_context_t
* ctx
)
2800 oberon_object_t
* x
= ctx
-> decl
-> list
;
2804 oberon_initialize_object(ctx
, x
-> next
);
2810 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2812 oberon_object_t
* x
= ctx
-> decl
-> list
;
2816 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2818 if(x
-> next
-> linked
== 0)
2820 oberon_error(ctx
, "unresolved forward declaration");
2828 oberon_decl_seq(oberon_context_t
* ctx
)
2830 if(ctx
-> token
== CONST
)
2832 oberon_assert_token(ctx
, CONST
);
2833 while(ctx
-> token
== IDENT
)
2835 oberon_const_decl(ctx
);
2836 oberon_assert_token(ctx
, SEMICOLON
);
2840 if(ctx
-> token
== TYPE
)
2842 oberon_assert_token(ctx
, TYPE
);
2843 while(ctx
-> token
== IDENT
)
2845 oberon_type_decl(ctx
);
2846 oberon_assert_token(ctx
, SEMICOLON
);
2850 if(ctx
-> token
== VAR
)
2852 oberon_assert_token(ctx
, VAR
);
2853 while(ctx
-> token
== IDENT
)
2855 oberon_var_decl(ctx
);
2856 oberon_assert_token(ctx
, SEMICOLON
);
2860 oberon_prevent_recursive_decl(ctx
);
2861 oberon_initialize_decl(ctx
);
2863 while(ctx
-> token
== PROCEDURE
)
2865 oberon_proc_decl(ctx
);
2866 oberon_assert_token(ctx
, SEMICOLON
);
2869 oberon_prevent_undeclarated_procedures(ctx
);
2873 oberon_statement_seq(oberon_context_t
* ctx
);
2876 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
2878 if(dst
-> read_only
)
2880 oberon_error(ctx
, "read-only destination");
2883 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
2884 oberon_generate_assign(ctx
, src
, dst
);
2888 oberon_statement(oberon_context_t
* ctx
)
2890 oberon_expr_t
* item1
;
2891 oberon_expr_t
* item2
;
2893 if(ctx
-> token
== IDENT
)
2895 item1
= oberon_designator(ctx
);
2896 if(ctx
-> token
== ASSIGN
)
2898 oberon_assert_token(ctx
, ASSIGN
);
2899 item2
= oberon_expr(ctx
);
2900 oberon_assign(ctx
, item2
, item1
);
2904 oberon_opt_proc_parens(ctx
, item1
);
2907 else if(ctx
-> token
== IF
)
2911 oberon_expr_t
* cond
;
2913 els
= oberon_generator_reserve_label(ctx
);
2914 end
= oberon_generator_reserve_label(ctx
);
2916 oberon_assert_token(ctx
, IF
);
2917 cond
= oberon_expr(ctx
);
2918 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
2920 oberon_error(ctx
, "condition must be boolean");
2922 oberon_assert_token(ctx
, THEN
);
2923 oberon_generate_branch(ctx
, cond
, false, els
);
2924 oberon_statement_seq(ctx
);
2925 oberon_generate_goto(ctx
, end
);
2926 oberon_generate_label(ctx
, els
);
2928 while(ctx
-> token
== ELSIF
)
2930 els
= oberon_generator_reserve_label(ctx
);
2932 oberon_assert_token(ctx
, ELSIF
);
2933 cond
= oberon_expr(ctx
);
2934 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
2936 oberon_error(ctx
, "condition must be boolean");
2938 oberon_assert_token(ctx
, THEN
);
2939 oberon_generate_branch(ctx
, cond
, false, els
);
2940 oberon_statement_seq(ctx
);
2941 oberon_generate_goto(ctx
, end
);
2942 oberon_generate_label(ctx
, els
);
2945 if(ctx
-> token
== ELSE
)
2947 oberon_assert_token(ctx
, ELSE
);
2948 oberon_statement_seq(ctx
);
2951 oberon_generate_label(ctx
, end
);
2952 oberon_assert_token(ctx
, END
);
2954 else if(ctx
-> token
== RETURN
)
2956 oberon_assert_token(ctx
, RETURN
);
2957 if(ISEXPR(ctx
-> token
))
2959 oberon_expr_t
* expr
;
2960 expr
= oberon_expr(ctx
);
2961 oberon_make_return(ctx
, expr
);
2965 oberon_make_return(ctx
, NULL
);
2971 oberon_statement_seq(oberon_context_t
* ctx
)
2973 oberon_statement(ctx
);
2974 while(ctx
-> token
== SEMICOLON
)
2976 oberon_assert_token(ctx
, SEMICOLON
);
2977 oberon_statement(ctx
);
2982 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
2984 oberon_module_t
* m
= ctx
-> module_list
;
2985 while(m
&& strcmp(m
-> name
, name
) != 0)
2993 code
= ctx
-> import_module(name
);
2996 oberon_error(ctx
, "no such module");
2999 m
= oberon_compile_module(ctx
, code
);
3005 oberon_error(ctx
, "cyclic module import");
3008 oberon_object_t
* ident
;
3009 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3010 ident
-> module
= m
;
3014 oberon_import_decl(oberon_context_t
* ctx
)
3019 alias
= name
= oberon_assert_ident(ctx
);
3020 if(ctx
-> token
== ASSIGN
)
3022 oberon_assert_token(ctx
, ASSIGN
);
3023 name
= oberon_assert_ident(ctx
);
3026 oberon_import_module(ctx
, alias
, name
);
3030 oberon_import_list(oberon_context_t
* ctx
)
3032 oberon_assert_token(ctx
, IMPORT
);
3034 oberon_import_decl(ctx
);
3035 while(ctx
-> token
== COMMA
)
3037 oberon_assert_token(ctx
, COMMA
);
3038 oberon_import_decl(ctx
);
3041 oberon_assert_token(ctx
, SEMICOLON
);
3045 oberon_parse_module(oberon_context_t
* ctx
)
3049 oberon_read_token(ctx
);
3051 oberon_assert_token(ctx
, MODULE
);
3052 name1
= oberon_assert_ident(ctx
);
3053 oberon_assert_token(ctx
, SEMICOLON
);
3054 ctx
-> mod
-> name
= name1
;
3056 oberon_generator_init_module(ctx
, ctx
-> mod
);
3058 if(ctx
-> token
== IMPORT
)
3060 oberon_import_list(ctx
);
3063 oberon_decl_seq(ctx
);
3065 oberon_generate_begin_module(ctx
);
3066 if(ctx
-> token
== BEGIN
)
3068 oberon_assert_token(ctx
, BEGIN
);
3069 oberon_statement_seq(ctx
);
3071 oberon_generate_end_module(ctx
);
3073 oberon_assert_token(ctx
, END
);
3074 name2
= oberon_assert_ident(ctx
);
3075 oberon_assert_token(ctx
, DOT
);
3077 if(strcmp(name1
, name2
) != 0)
3079 oberon_error(ctx
, "module name not matched");
3082 oberon_generator_fini_module(ctx
-> mod
);
3085 // =======================================================================
3087 // =======================================================================
3090 register_default_types(oberon_context_t
* ctx
)
3092 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3093 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3095 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3096 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3097 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3099 ctx
-> string_type
= oberon_new_type_string(1);
3100 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3102 ctx
-> bool_type
= oberon_new_type_boolean();
3103 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3105 ctx
-> byte_type
= oberon_new_type_integer(1);
3106 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3108 ctx
-> shortint_type
= oberon_new_type_integer(2);
3109 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3111 ctx
-> int_type
= oberon_new_type_integer(4);
3112 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3114 ctx
-> longint_type
= oberon_new_type_integer(8);
3115 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3117 ctx
-> real_type
= oberon_new_type_real(4);
3118 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3120 ctx
-> longreal_type
= oberon_new_type_real(8);
3121 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3123 ctx
-> char_type
= oberon_new_type_char(1);
3124 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3128 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3130 oberon_object_t
* proc
;
3131 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3132 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3133 proc
-> type
-> sysproc
= true;
3134 proc
-> type
-> genfunc
= f
;
3135 proc
-> type
-> genproc
= p
;
3138 static oberon_expr_t
*
3139 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3143 oberon_error(ctx
, "too few arguments");
3148 oberon_error(ctx
, "too mach arguments");
3151 oberon_expr_t
* arg
;
3154 oberon_type_t
* result_type
;
3155 result_type
= arg
-> result
;
3157 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3159 oberon_error(ctx
, "ABS accepts only integers");
3163 oberon_expr_t
* expr
;
3164 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3169 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3173 oberon_error(ctx
, "too few arguments");
3176 oberon_expr_t
* dst
;
3179 oberon_type_t
* type
;
3180 type
= dst
-> result
;
3182 if(type
-> class != OBERON_TYPE_POINTER
)
3184 oberon_error(ctx
, "not a pointer");
3187 type
= type
-> base
;
3189 oberon_expr_t
* src
;
3190 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3191 src
-> item
.num_args
= 0;
3192 src
-> item
.args
= NULL
;
3195 if(type
-> class == OBERON_TYPE_ARRAY
)
3197 if(type
-> size
== 0)
3199 oberon_type_t
* x
= type
;
3200 while(x
-> class == OBERON_TYPE_ARRAY
)
3210 if(num_args
< max_args
)
3212 oberon_error(ctx
, "too few arguments");
3215 if(num_args
> max_args
)
3217 oberon_error(ctx
, "too mach arguments");
3220 int num_sizes
= max_args
- 1;
3221 oberon_expr_t
* size_list
= list_args
-> next
;
3223 oberon_expr_t
* arg
= size_list
;
3224 for(int i
= 0; i
< max_args
- 1; i
++)
3226 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3228 oberon_error(ctx
, "size must be integer");
3233 src
-> item
.num_args
= num_sizes
;
3234 src
-> item
.args
= size_list
;
3236 else if(type
-> class != OBERON_TYPE_RECORD
)
3238 oberon_error(ctx
, "oberon_make_new_call: wat");
3241 if(num_args
> max_args
)
3243 oberon_error(ctx
, "too mach arguments");
3246 oberon_assign(ctx
, src
, dst
);
3250 oberon_create_context(ModuleImportCallback import_module
)
3252 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3254 oberon_scope_t
* world_scope
;
3255 world_scope
= oberon_open_scope(ctx
);
3256 ctx
-> world_scope
= world_scope
;
3258 ctx
-> import_module
= import_module
;
3260 oberon_generator_init_context(ctx
);
3262 register_default_types(ctx
);
3263 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3264 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3270 oberon_destroy_context(oberon_context_t
* ctx
)
3272 oberon_generator_destroy_context(ctx
);
3277 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3279 const char * code
= ctx
-> code
;
3280 int code_index
= ctx
-> code_index
;
3282 int token
= ctx
-> token
;
3283 char * string
= ctx
-> string
;
3284 int integer
= ctx
-> integer
;
3285 int real
= ctx
-> real
;
3286 bool longmode
= ctx
-> longmode
;
3287 oberon_scope_t
* decl
= ctx
-> decl
;
3288 oberon_module_t
* mod
= ctx
-> mod
;
3290 oberon_scope_t
* module_scope
;
3291 module_scope
= oberon_open_scope(ctx
);
3293 oberon_module_t
* module
;
3294 module
= calloc(1, sizeof *module
);
3295 module
-> decl
= module_scope
;
3296 module
-> next
= ctx
-> module_list
;
3298 ctx
-> mod
= module
;
3299 ctx
-> module_list
= module
;
3301 oberon_init_scaner(ctx
, newcode
);
3302 oberon_parse_module(ctx
);
3304 module
-> ready
= 1;
3307 ctx
-> code_index
= code_index
;
3309 ctx
-> token
= token
;
3310 ctx
-> string
= string
;
3311 ctx
-> integer
= integer
;
3313 ctx
-> longmode
= longmode
;