e8cbf428c12a7261b90dd057c8dc96381f465a3b
9 #include "../include/oberon.h"
11 #include "oberon-internals.h"
12 #include "generator.h"
75 // =======================================================================
77 // =======================================================================
80 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
84 fprintf(stderr
, "error: ");
85 vfprintf(stderr
, fmt
, ptr
);
86 fprintf(stderr
, "\n");
87 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
88 fprintf(stderr
, " c = %c\n", ctx
-> c
);
89 fprintf(stderr
, " token = %i\n", ctx
-> token
);
94 static oberon_type_t
*
95 oberon_new_type_ptr(int class)
97 oberon_type_t
* x
= malloc(sizeof *x
);
98 memset(x
, 0, sizeof *x
);
103 static oberon_type_t
*
104 oberon_new_type_integer(int size
)
107 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
112 static oberon_type_t
*
113 oberon_new_type_boolean()
116 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
120 static oberon_type_t
*
121 oberon_new_type_real(int size
)
124 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
129 static oberon_type_t
*
130 oberon_new_type_char(int size
)
133 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
138 static oberon_type_t
*
139 oberon_new_type_string(int size
)
142 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
147 // =======================================================================
149 // =======================================================================
151 static oberon_scope_t
*
152 oberon_open_scope(oberon_context_t
* ctx
)
154 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
155 oberon_object_t
* list
= calloc(1, sizeof *list
);
158 scope
-> list
= list
;
159 scope
-> up
= ctx
-> decl
;
163 scope
-> local
= scope
-> up
-> local
;
164 scope
-> parent
= scope
-> up
-> parent
;
165 scope
-> parent_type
= scope
-> up
-> parent_type
;
173 oberon_close_scope(oberon_scope_t
* scope
)
175 oberon_context_t
* ctx
= scope
-> ctx
;
176 ctx
-> decl
= scope
-> up
;
179 static oberon_object_t
*
180 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
182 oberon_object_t
* x
= list
;
183 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
190 static oberon_object_t
*
191 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
193 oberon_object_t
* result
= NULL
;
195 oberon_scope_t
* s
= scope
;
196 while(result
== NULL
&& s
!= NULL
)
198 result
= oberon_find_object_in_list(s
-> list
, name
);
202 if(check_it
&& result
== NULL
)
204 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
210 static oberon_object_t
*
211 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
215 if(oberon_find_object(scope
-> up
, name
, false))
217 oberon_error(scope
-> ctx
, "already defined");
221 oberon_object_t
* x
= scope
-> list
;
222 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
229 oberon_error(scope
-> ctx
, "already defined");
232 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
233 memset(newvar
, 0, sizeof *newvar
);
234 newvar
-> name
= name
;
235 newvar
-> class = class;
236 newvar
-> export
= export
;
237 newvar
-> read_only
= read_only
;
238 newvar
-> local
= scope
-> local
;
239 newvar
-> parent
= scope
-> parent
;
240 newvar
-> parent_type
= scope
-> parent_type
;
241 newvar
-> module
= scope
-> ctx
-> mod
;
248 static oberon_object_t
*
249 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
251 oberon_object_t
* id
;
252 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
254 oberon_generator_init_type(scope
-> ctx
, type
);
258 // =======================================================================
260 // =======================================================================
263 oberon_get_char(oberon_context_t
* ctx
)
265 if(ctx
-> code
[ctx
-> code_index
])
267 ctx
-> code_index
+= 1;
268 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
273 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
276 ctx
-> code_index
= 0;
277 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
281 oberon_read_ident(oberon_context_t
* ctx
)
284 int i
= ctx
-> code_index
;
286 int c
= ctx
-> code
[i
];
294 char * ident
= malloc(len
+ 1);
295 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
298 ctx
-> code_index
= i
;
299 ctx
-> c
= ctx
-> code
[i
];
300 ctx
-> string
= ident
;
301 ctx
-> token
= IDENT
;
303 if(strcmp(ident
, "MODULE") == 0)
305 ctx
-> token
= MODULE
;
307 else if(strcmp(ident
, "END") == 0)
311 else if(strcmp(ident
, "VAR") == 0)
315 else if(strcmp(ident
, "BEGIN") == 0)
317 ctx
-> token
= BEGIN
;
319 else if(strcmp(ident
, "TRUE") == 0)
323 else if(strcmp(ident
, "FALSE") == 0)
325 ctx
-> token
= FALSE
;
327 else if(strcmp(ident
, "OR") == 0)
331 else if(strcmp(ident
, "DIV") == 0)
335 else if(strcmp(ident
, "MOD") == 0)
339 else if(strcmp(ident
, "PROCEDURE") == 0)
341 ctx
-> token
= PROCEDURE
;
343 else if(strcmp(ident
, "RETURN") == 0)
345 ctx
-> token
= RETURN
;
347 else if(strcmp(ident
, "CONST") == 0)
349 ctx
-> token
= CONST
;
351 else if(strcmp(ident
, "TYPE") == 0)
355 else if(strcmp(ident
, "ARRAY") == 0)
357 ctx
-> token
= ARRAY
;
359 else if(strcmp(ident
, "OF") == 0)
363 else if(strcmp(ident
, "RECORD") == 0)
365 ctx
-> token
= RECORD
;
367 else if(strcmp(ident
, "POINTER") == 0)
369 ctx
-> token
= POINTER
;
371 else if(strcmp(ident
, "TO") == 0)
375 else if(strcmp(ident
, "NIL") == 0)
379 else if(strcmp(ident
, "IMPORT") == 0)
381 ctx
-> token
= IMPORT
;
383 else if(strcmp(ident
, "IN") == 0)
387 else if(strcmp(ident
, "IS") == 0)
391 else if(strcmp(ident
, "IF") == 0)
395 else if(strcmp(ident
, "THEN") == 0)
399 else if(strcmp(ident
, "ELSE") == 0)
403 else if(strcmp(ident
, "ELSIF") == 0)
405 ctx
-> token
= ELSIF
;
407 else if(strcmp(ident
, "WHILE") == 0)
409 ctx
-> token
= WHILE
;
411 else if(strcmp(ident
, "DO") == 0)
415 else if(strcmp(ident
, "REPEAT") == 0)
417 ctx
-> token
= REPEAT
;
419 else if(strcmp(ident
, "UNTIL") == 0)
421 ctx
-> token
= UNTIL
;
426 oberon_read_number(oberon_context_t
* ctx
)
439 * mode = 3 == LONGREAL
443 start_i
= ctx
-> code_index
;
445 while(isdigit(ctx
-> c
))
447 oberon_get_char(ctx
);
450 end_i
= ctx
-> code_index
;
452 if(isxdigit(ctx
-> c
))
455 while(isxdigit(ctx
-> c
))
457 oberon_get_char(ctx
);
460 end_i
= ctx
-> code_index
;
465 oberon_get_char(ctx
);
467 else if(ctx
-> c
== 'X')
470 oberon_get_char(ctx
);
474 oberon_error(ctx
, "invalid hex number");
477 else if(ctx
-> c
== '.')
480 oberon_get_char(ctx
);
482 while(isdigit(ctx
-> c
))
484 oberon_get_char(ctx
);
487 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
489 exp_i
= ctx
-> code_index
;
496 oberon_get_char(ctx
);
498 if(ctx
-> c
== '+' || ctx
-> c
== '-')
500 oberon_get_char(ctx
);
503 while(isdigit(ctx
-> c
))
505 oberon_get_char(ctx
);
510 end_i
= ctx
-> code_index
;
518 oberon_get_char(ctx
);
520 else if(ctx
-> c
== 'X')
523 oberon_get_char(ctx
);
527 int len
= end_i
- start_i
;
528 ident
= malloc(len
+ 1);
529 memcpy(ident
, &ctx
-> code
[start_i
], len
);
532 ctx
-> longmode
= false;
535 int i
= exp_i
- start_i
;
537 ctx
-> longmode
= true;
543 integer
= atol(ident
);
545 ctx
-> token
= INTEGER
;
548 sscanf(ident
, "%lx", &integer
);
550 ctx
-> token
= INTEGER
;
554 sscanf(ident
, "%lf", &real
);
558 sscanf(ident
, "%lx", &integer
);
563 oberon_error(ctx
, "oberon_read_number: wat");
567 ctx
-> string
= ident
;
568 ctx
-> integer
= integer
;
573 oberon_skip_space(oberon_context_t
* ctx
)
575 while(isspace(ctx
-> c
))
577 oberon_get_char(ctx
);
582 oberon_read_comment(oberon_context_t
* ctx
)
589 oberon_get_char(ctx
);
592 oberon_get_char(ctx
);
596 else if(ctx
-> c
== '*')
598 oberon_get_char(ctx
);
601 oberon_get_char(ctx
);
605 else if(ctx
-> c
== 0)
607 oberon_error(ctx
, "unterminated comment");
611 oberon_get_char(ctx
);
616 static void oberon_read_string(oberon_context_t
* ctx
)
619 oberon_get_char(ctx
);
621 int start
= ctx
-> code_index
;
623 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
625 oberon_get_char(ctx
);
630 oberon_error(ctx
, "unterminated string");
633 int end
= ctx
-> code_index
;
635 oberon_get_char(ctx
);
637 char * string
= calloc(1, end
- start
+ 1);
638 strncpy(string
, &ctx
-> code
[start
], end
- start
);
640 ctx
-> token
= STRING
;
641 ctx
-> string
= string
;
643 printf("oberon_read_string: string ((%s))\n", string
);
646 static void oberon_read_token(oberon_context_t
* ctx
);
649 oberon_read_symbol(oberon_context_t
* ctx
)
658 ctx
-> token
= SEMICOLON
;
659 oberon_get_char(ctx
);
662 ctx
-> token
= COLON
;
663 oberon_get_char(ctx
);
666 ctx
-> token
= ASSIGN
;
667 oberon_get_char(ctx
);
672 oberon_get_char(ctx
);
675 ctx
-> token
= LPAREN
;
676 oberon_get_char(ctx
);
679 oberon_get_char(ctx
);
680 oberon_read_comment(ctx
);
681 oberon_read_token(ctx
);
685 ctx
-> token
= RPAREN
;
686 oberon_get_char(ctx
);
689 ctx
-> token
= EQUAL
;
690 oberon_get_char(ctx
);
694 oberon_get_char(ctx
);
698 oberon_get_char(ctx
);
702 oberon_get_char(ctx
);
706 ctx
-> token
= GREAT
;
707 oberon_get_char(ctx
);
711 oberon_get_char(ctx
);
716 oberon_get_char(ctx
);
719 ctx
-> token
= MINUS
;
720 oberon_get_char(ctx
);
724 oberon_get_char(ctx
);
727 oberon_get_char(ctx
);
728 oberon_error(ctx
, "unstarted comment");
732 ctx
-> token
= SLASH
;
733 oberon_get_char(ctx
);
737 oberon_get_char(ctx
);
741 oberon_get_char(ctx
);
744 ctx
-> token
= COMMA
;
745 oberon_get_char(ctx
);
748 ctx
-> token
= LBRACE
;
749 oberon_get_char(ctx
);
752 ctx
-> token
= RBRACE
;
753 oberon_get_char(ctx
);
756 ctx
-> token
= UPARROW
;
757 oberon_get_char(ctx
);
760 oberon_read_string(ctx
);
763 oberon_read_string(ctx
);
766 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
772 oberon_read_token(oberon_context_t
* ctx
)
774 oberon_skip_space(ctx
);
779 oberon_read_ident(ctx
);
783 oberon_read_number(ctx
);
787 oberon_read_symbol(ctx
);
791 // =======================================================================
793 // =======================================================================
795 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
796 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
797 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
798 static char * oberon_assert_ident(oberon_context_t
* ctx
);
799 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
800 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
801 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
803 static oberon_expr_t
*
804 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
806 oberon_oper_t
* operator;
807 operator = malloc(sizeof *operator);
808 memset(operator, 0, sizeof *operator);
810 operator -> is_item
= 0;
811 operator -> result
= result
;
812 operator -> read_only
= 1;
814 operator -> left
= left
;
815 operator -> right
= right
;
817 return (oberon_expr_t
*) operator;
820 static oberon_expr_t
*
821 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
823 oberon_item_t
* item
;
824 item
= malloc(sizeof *item
);
825 memset(item
, 0, sizeof *item
);
828 item
-> result
= result
;
829 item
-> read_only
= read_only
;
832 return (oberon_expr_t
*)item
;
835 static oberon_expr_t
*
836 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
838 oberon_expr_t
* expr
;
839 oberon_type_t
* result
;
841 result
= a
-> result
;
845 if(result
-> class != OBERON_TYPE_INTEGER
)
847 oberon_error(ctx
, "incompatible operator type");
850 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
852 else if(token
== NOT
)
854 if(result
-> class != OBERON_TYPE_BOOLEAN
)
856 oberon_error(ctx
, "incompatible operator type");
859 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
863 oberon_error(ctx
, "oberon_make_unary_op: wat");
870 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
872 oberon_expr_t
* last
;
877 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
881 *first
= last
= oberon_expr(ctx
);
883 while(ctx
-> token
== COMMA
)
885 oberon_assert_token(ctx
, COMMA
);
886 oberon_expr_t
* current
;
890 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
894 current
= oberon_expr(ctx
);
897 last
-> next
= current
;
903 static oberon_expr_t
*
904 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
906 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
909 static oberon_expr_t
*
910 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
912 oberon_type_t
* from
= expr
-> result
;
913 oberon_type_t
* to
= rec
;
915 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
917 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
919 printf("oberno_make_record_cast: pointers\n");
924 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
926 oberon_error(ctx
, "must be record type");
929 return oberon_cast_expr(ctx
, expr
, rec
);
932 static oberon_type_t
*
933 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
935 oberon_type_t
* result
;
936 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
940 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
944 else if(a
-> class != b
-> class)
946 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
948 else if(a
-> size
> b
-> size
)
961 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
963 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
969 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
971 oberon_error(ctx
, "not a record");
974 oberon_type_t
* t
= from
;
975 while(t
!= NULL
&& t
!= to
)
982 oberon_error(ctx
, "incompatible record types");
986 static oberon_expr_t
*
987 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
990 // Если классы типов равны
991 // Если INTEGER переводится в REAL
992 // Есди STRING переводится в ARRAY OF CHAR
995 if(pref
-> class != expr
-> result
-> class)
997 printf("expr class %i\n", expr
-> result
-> class);
998 printf("pref class %i\n", pref
-> class);
1000 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1002 if(pref
-> class == OBERON_TYPE_ARRAY
)
1004 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1014 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1016 if(pref
-> class != OBERON_TYPE_REAL
)
1029 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1032 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1034 if(expr
-> result
-> size
> pref
-> size
)
1036 oberon_error(ctx
, "incompatible size");
1040 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1043 else if(pref
-> class == OBERON_TYPE_RECORD
)
1045 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1046 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1048 else if(pref
-> class == OBERON_TYPE_POINTER
)
1050 assert(pref
-> base
);
1051 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1053 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1054 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1056 else if(expr
-> result
-> base
!= pref
-> base
)
1058 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1060 oberon_error(ctx
, "incompatible pointer types");
1069 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1071 oberon_type_t
* a
= (*ea
) -> result
;
1072 oberon_type_t
* b
= (*eb
) -> result
;
1073 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1074 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1075 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1079 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1081 if(desig
-> mode
!= MODE_CALL
)
1083 oberon_error(ctx
, "expected mode CALL");
1086 oberon_type_t
* fn
= desig
-> parent
-> result
;
1087 int num_args
= desig
-> num_args
;
1088 int num_decl
= fn
-> num_decl
;
1090 if(num_args
< num_decl
)
1092 oberon_error(ctx
, "too few arguments");
1094 else if(num_args
> num_decl
)
1096 oberon_error(ctx
, "too many arguments");
1099 /* Делаем проверку на запись и делаем автокаст */
1100 oberon_expr_t
* casted
[num_args
];
1101 oberon_expr_t
* arg
= desig
-> args
;
1102 oberon_object_t
* param
= fn
-> decl
;
1103 for(int i
= 0; i
< num_args
; i
++)
1105 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1107 if(arg
-> read_only
)
1109 oberon_error(ctx
, "assign to read-only var");
1113 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1115 param
= param
-> next
;
1118 /* Создаём новый список выражений */
1122 for(int i
= 0; i
< num_args
- 1; i
++)
1124 casted
[i
] -> next
= casted
[i
+ 1];
1126 desig
-> args
= arg
;
1130 static oberon_expr_t
*
1131 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1133 oberon_type_t
* signature
= item
-> result
;
1134 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1136 oberon_error(ctx
, "not a procedure");
1139 oberon_expr_t
* call
;
1141 if(signature
-> sysproc
)
1143 if(signature
-> genfunc
== NULL
)
1145 oberon_error(ctx
, "not a function-procedure");
1148 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1152 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1154 oberon_error(ctx
, "attempt to call procedure in expression");
1157 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1158 call
-> item
.parent
= item
;
1159 call
-> item
.num_args
= num_args
;
1160 call
-> item
.args
= list_args
;
1161 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1168 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1170 oberon_type_t
* signature
= item
-> result
;
1171 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1173 oberon_error(ctx
, "not a procedure");
1176 oberon_expr_t
* call
;
1178 if(signature
-> sysproc
)
1180 if(signature
-> genproc
== NULL
)
1182 oberon_error(ctx
, "not a procedure");
1185 signature
-> genproc(ctx
, num_args
, list_args
);
1189 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1191 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1194 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1195 call
-> item
.parent
= item
;
1196 call
-> item
.num_args
= num_args
;
1197 call
-> item
.args
= list_args
;
1198 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1199 oberon_generate_call_proc(ctx
, call
);
1205 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
1207 switch(proc -> class)
1209 case OBERON_CLASS_PROC:
1210 if(proc -> class != OBERON_CLASS_PROC)
1212 oberon_error(ctx, "not a procedure");
1215 case OBERON_CLASS_VAR:
1216 case OBERON_CLASS_VAR_PARAM:
1217 case OBERON_CLASS_PARAM:
1218 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
1220 oberon_error(ctx, "not a procedure");
1224 oberon_error(ctx, "not a procedure");
1230 if(proc -> genproc == NULL)
1232 oberon_error(ctx, "requres non-typed procedure");
1235 proc -> genproc(ctx, num_args, list_args);
1239 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
1241 oberon_error(ctx, "attempt to call function as non-typed procedure");
1244 oberon_expr_t * call;
1245 call = oberon_new_item(MODE_CALL, proc -> type -> base, 1);
1246 call -> item.var = proc;
1247 call -> item.num_args = num_args;
1248 call -> item.args = list_args;
1249 oberon_autocast_call(ctx, call);
1250 oberon_generate_call_proc(ctx, call);
1259 || ((x) == INTEGER) \
1262 || ((x) == STRING) \
1264 || ((x) == LPAREN) \
1269 static oberon_expr_t
*
1270 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1272 printf("oberno_make_dereferencing\n");
1273 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1275 oberon_error(ctx
, "not a pointer");
1278 assert(expr
-> is_item
);
1280 oberon_expr_t
* selector
;
1281 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1282 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1287 static oberon_expr_t
*
1288 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1290 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1292 desig
= oberno_make_dereferencing(ctx
, desig
);
1295 assert(desig
-> is_item
);
1297 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1299 oberon_error(ctx
, "not array");
1302 oberon_type_t
* base
;
1303 base
= desig
-> result
-> base
;
1305 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1307 oberon_error(ctx
, "index must be integer");
1310 // Статическая проверка границ массива
1311 if(desig
-> result
-> size
!= 0)
1313 if(index
-> is_item
)
1315 if(index
-> item
.mode
== MODE_INTEGER
)
1317 int arr_size
= desig
-> result
-> size
;
1318 int index_int
= index
-> item
.integer
;
1319 if(index_int
< 0 || index_int
> arr_size
- 1)
1321 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1327 oberon_expr_t
* selector
;
1328 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1329 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1330 selector
-> item
.num_args
= 1;
1331 selector
-> item
.args
= index
;
1336 static oberon_expr_t
*
1337 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1339 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1341 expr
= oberno_make_dereferencing(ctx
, expr
);
1344 assert(expr
-> is_item
);
1346 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1348 oberon_error(ctx
, "not record");
1351 oberon_type_t
* rec
= expr
-> result
;
1353 oberon_object_t
* field
;
1354 field
= oberon_find_object(rec
-> scope
, name
, true);
1356 if(field
-> export
== 0)
1358 if(field
-> module
!= ctx
-> mod
)
1360 oberon_error(ctx
, "field not exported");
1365 if(field
-> read_only
)
1367 if(field
-> module
!= ctx
-> mod
)
1373 oberon_expr_t
* selector
;
1374 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1375 selector
-> item
.var
= field
;
1376 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1381 #define ISSELECTOR(x) \
1384 || ((x) == UPARROW) \
1387 static oberon_object_t
*
1388 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1391 oberon_object_t
* x
;
1393 name
= oberon_assert_ident(ctx
);
1394 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1398 if(x
-> class == OBERON_CLASS_MODULE
)
1400 oberon_assert_token(ctx
, DOT
);
1401 name
= oberon_assert_ident(ctx
);
1402 /* Наличие объектов в левых модулях всегда проверяется */
1403 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1405 if(x
-> export
== 0)
1407 oberon_error(ctx
, "not exported");
1420 static oberon_expr_t
*
1421 oberon_designator(oberon_context_t
* ctx
)
1424 oberon_object_t
* var
;
1425 oberon_expr_t
* expr
;
1427 var
= oberon_qualident(ctx
, NULL
, 1);
1430 if(var
-> read_only
)
1432 if(var
-> module
!= ctx
-> mod
)
1438 switch(var
-> class)
1440 case OBERON_CLASS_CONST
:
1442 expr
= (oberon_expr_t
*) var
-> value
;
1444 case OBERON_CLASS_VAR
:
1445 case OBERON_CLASS_VAR_PARAM
:
1446 case OBERON_CLASS_PARAM
:
1447 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1449 case OBERON_CLASS_PROC
:
1450 expr
= oberon_new_item(MODE_VAR
, var
-> type
, 1);
1453 oberon_error(ctx
, "invalid designator");
1456 expr
-> item
.var
= var
;
1458 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1460 switch(ctx
-> token
)
1463 oberon_assert_token(ctx
, DOT
);
1464 name
= oberon_assert_ident(ctx
);
1465 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1468 oberon_assert_token(ctx
, LBRACE
);
1469 int num_indexes
= 0;
1470 oberon_expr_t
* indexes
= NULL
;
1471 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1472 oberon_assert_token(ctx
, RBRACE
);
1474 for(int i
= 0; i
< num_indexes
; i
++)
1476 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1477 indexes
= indexes
-> next
;
1481 oberon_assert_token(ctx
, UPARROW
);
1482 expr
= oberno_make_dereferencing(ctx
, expr
);
1485 oberon_assert_token(ctx
, LPAREN
);
1486 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1487 if(objtype
-> class != OBERON_CLASS_TYPE
)
1489 oberon_error(ctx
, "must be type");
1491 oberon_assert_token(ctx
, RPAREN
);
1492 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1495 oberon_error(ctx
, "oberon_designator: wat");
1503 static oberon_expr_t
*
1504 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1506 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1507 if(ctx
-> token
== LPAREN
)
1509 oberon_assert_token(ctx
, LPAREN
);
1512 oberon_expr_t
* arguments
= NULL
;
1514 if(ISEXPR(ctx
-> token
))
1516 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1519 assert(expr
-> is_item
== 1);
1520 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1522 oberon_assert_token(ctx
, RPAREN
);
1529 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1531 assert(expr
-> is_item
);
1534 oberon_expr_t
* arguments
= NULL
;
1536 if(ctx
-> token
== LPAREN
)
1538 oberon_assert_token(ctx
, LPAREN
);
1540 if(ISEXPR(ctx
-> token
))
1542 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1545 oberon_assert_token(ctx
, RPAREN
);
1548 /* Вызов происходит даже без скобок */
1549 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1552 static oberon_type_t
*
1553 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1555 if(i
>= -128 && i
<= 127)
1557 return ctx
-> byte_type
;
1559 else if(i
>= -32768 && i
<= 32767)
1561 return ctx
-> shortint_type
;
1563 else if(i
>= -2147483648 && i
<= 2147483647)
1565 return ctx
-> int_type
;
1569 return ctx
-> longint_type
;
1573 static oberon_expr_t
*
1574 oberon_factor(oberon_context_t
* ctx
)
1576 oberon_expr_t
* expr
;
1577 oberon_type_t
* result
;
1579 switch(ctx
-> token
)
1582 expr
= oberon_designator(ctx
);
1583 expr
= oberon_opt_func_parens(ctx
, expr
);
1586 result
= oberon_get_type_of_int_value(ctx
, ctx
-> integer
);
1587 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1588 expr
-> item
.integer
= ctx
-> integer
;
1589 oberon_assert_token(ctx
, INTEGER
);
1592 result
= ctx
-> char_type
;
1593 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1594 expr
-> item
.integer
= ctx
-> integer
;
1595 oberon_assert_token(ctx
, CHAR
);
1598 result
= ctx
-> string_type
;
1599 expr
= oberon_new_item(MODE_STRING
, result
, true);
1600 expr
-> item
.string
= ctx
-> string
;
1601 oberon_assert_token(ctx
, STRING
);
1604 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1605 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1606 expr
-> item
.real
= ctx
-> real
;
1607 oberon_assert_token(ctx
, REAL
);
1610 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1611 expr
-> item
.boolean
= true;
1612 oberon_assert_token(ctx
, TRUE
);
1615 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1616 expr
-> item
.boolean
= false;
1617 oberon_assert_token(ctx
, FALSE
);
1620 oberon_assert_token(ctx
, LPAREN
);
1621 expr
= oberon_expr(ctx
);
1622 oberon_assert_token(ctx
, RPAREN
);
1625 oberon_assert_token(ctx
, NOT
);
1626 expr
= oberon_factor(ctx
);
1627 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1630 oberon_assert_token(ctx
, NIL
);
1631 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1634 oberon_error(ctx
, "invalid expression");
1640 #define ITMAKESBOOLEAN(x) \
1641 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1643 #define ITUSEONLYINTEGER(x) \
1644 ((x) >= LESS && (x) <= GEQ)
1646 #define ITUSEONLYBOOLEAN(x) \
1647 (((x) == OR) || ((x) == AND))
1650 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1652 oberon_expr_t
* expr
= *e
;
1653 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1655 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1657 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1661 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1664 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1666 oberon_error(ctx
, "required numeric type");
1670 static oberon_expr_t
*
1671 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1673 oberon_expr_t
* expr
;
1674 oberon_type_t
* result
;
1676 if(ITMAKESBOOLEAN(token
))
1678 if(ITUSEONLYINTEGER(token
))
1680 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1681 || b
-> result
-> class == OBERON_TYPE_INTEGER
1682 || a
-> result
-> class == OBERON_TYPE_REAL
1683 || b
-> result
-> class == OBERON_TYPE_REAL
)
1689 oberon_error(ctx
, "used only with numeric types");
1692 else if(ITUSEONLYBOOLEAN(token
))
1694 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1695 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1697 oberon_error(ctx
, "used only with boolean type");
1701 oberon_autocast_binary_op(ctx
, &a
, &b
);
1702 result
= ctx
-> bool_type
;
1706 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1708 else if(token
== NEQ
)
1710 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1712 else if(token
== LESS
)
1714 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1716 else if(token
== LEQ
)
1718 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1720 else if(token
== GREAT
)
1722 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1724 else if(token
== GEQ
)
1726 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1728 else if(token
== OR
)
1730 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1732 else if(token
== AND
)
1734 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1738 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1741 else if(token
== SLASH
)
1743 oberon_autocast_to_real(ctx
, &a
);
1744 oberon_autocast_to_real(ctx
, &b
);
1745 oberon_autocast_binary_op(ctx
, &a
, &b
);
1746 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1748 else if(token
== DIV
)
1750 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1751 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1753 oberon_error(ctx
, "operator DIV requires integer type");
1756 oberon_autocast_binary_op(ctx
, &a
, &b
);
1757 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1761 oberon_autocast_binary_op(ctx
, &a
, &b
);
1765 expr
= oberon_new_operator(OP_ADD
, a
-> result
, a
, b
);
1767 else if(token
== MINUS
)
1769 expr
= oberon_new_operator(OP_SUB
, a
-> result
, a
, b
);
1771 else if(token
== STAR
)
1773 expr
= oberon_new_operator(OP_MUL
, a
-> result
, a
, b
);
1775 else if(token
== MOD
)
1777 expr
= oberon_new_operator(OP_MOD
, a
-> result
, a
, b
);
1781 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1788 #define ISMULOP(x) \
1789 ((x) >= STAR && (x) <= AND)
1791 static oberon_expr_t
*
1792 oberon_term_expr(oberon_context_t
* ctx
)
1794 oberon_expr_t
* expr
;
1796 expr
= oberon_factor(ctx
);
1797 while(ISMULOP(ctx
-> token
))
1799 int token
= ctx
-> token
;
1800 oberon_read_token(ctx
);
1802 oberon_expr_t
* inter
= oberon_factor(ctx
);
1803 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1809 #define ISADDOP(x) \
1810 ((x) >= PLUS && (x) <= OR)
1812 static oberon_expr_t
*
1813 oberon_simple_expr(oberon_context_t
* ctx
)
1815 oberon_expr_t
* expr
;
1818 if(ctx
-> token
== PLUS
)
1821 oberon_assert_token(ctx
, PLUS
);
1823 else if(ctx
-> token
== MINUS
)
1826 oberon_assert_token(ctx
, MINUS
);
1829 expr
= oberon_term_expr(ctx
);
1833 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1836 while(ISADDOP(ctx
-> token
))
1838 int token
= ctx
-> token
;
1839 oberon_read_token(ctx
);
1841 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1842 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1848 #define ISRELATION(x) \
1849 ((x) >= EQUAL && (x) <= IS)
1851 static oberon_expr_t
*
1852 oberon_expr(oberon_context_t
* ctx
)
1854 oberon_expr_t
* expr
;
1856 expr
= oberon_simple_expr(ctx
);
1857 while(ISRELATION(ctx
-> token
))
1859 int token
= ctx
-> token
;
1860 oberon_read_token(ctx
);
1862 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1863 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1869 static oberon_item_t
*
1870 oberon_const_expr(oberon_context_t
* ctx
)
1872 oberon_expr_t
* expr
;
1873 expr
= oberon_expr(ctx
);
1875 if(expr
-> is_item
== 0)
1877 oberon_error(ctx
, "const expression are required");
1880 return (oberon_item_t
*) expr
;
1883 // =======================================================================
1885 // =======================================================================
1887 static void oberon_decl_seq(oberon_context_t
* ctx
);
1888 static void oberon_statement_seq(oberon_context_t
* ctx
);
1889 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1892 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1894 if(ctx
-> token
!= token
)
1896 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
1901 oberon_assert_token(oberon_context_t
* ctx
, int token
)
1903 oberon_expect_token(ctx
, token
);
1904 oberon_read_token(ctx
);
1908 oberon_assert_ident(oberon_context_t
* ctx
)
1910 oberon_expect_token(ctx
, IDENT
);
1911 char * ident
= ctx
-> string
;
1912 oberon_read_token(ctx
);
1917 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
1919 switch(ctx
-> token
)
1922 oberon_assert_token(ctx
, STAR
);
1927 oberon_assert_token(ctx
, MINUS
);
1938 static oberon_object_t
*
1939 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
1944 oberon_object_t
* x
;
1946 name
= oberon_assert_ident(ctx
);
1947 oberon_def(ctx
, &export
, &read_only
);
1949 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
1954 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
1957 *list
= oberon_ident_def(ctx
, class, check_upscope
);
1958 while(ctx
-> token
== COMMA
)
1960 oberon_assert_token(ctx
, COMMA
);
1961 oberon_ident_def(ctx
, class, check_upscope
);
1967 oberon_var_decl(oberon_context_t
* ctx
)
1970 oberon_object_t
* list
;
1971 oberon_type_t
* type
;
1972 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1974 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
1975 oberon_assert_token(ctx
, COLON
);
1976 oberon_type(ctx
, &type
);
1978 oberon_object_t
* var
= list
;
1979 for(int i
= 0; i
< num
; i
++)
1986 static oberon_object_t
*
1987 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
1989 int class = OBERON_CLASS_PARAM
;
1990 if(ctx
-> token
== VAR
)
1992 oberon_read_token(ctx
);
1993 class = OBERON_CLASS_VAR_PARAM
;
1997 oberon_object_t
* list
;
1998 oberon_ident_list(ctx
, class, false, &num
, &list
);
2000 oberon_assert_token(ctx
, COLON
);
2002 oberon_type_t
* type
;
2003 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2004 oberon_type(ctx
, &type
);
2006 oberon_object_t
* param
= list
;
2007 for(int i
= 0; i
< num
; i
++)
2009 param
-> type
= type
;
2010 param
= param
-> next
;
2017 #define ISFPSECTION \
2018 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2021 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2023 oberon_assert_token(ctx
, LPAREN
);
2027 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2028 while(ctx
-> token
== SEMICOLON
)
2030 oberon_assert_token(ctx
, SEMICOLON
);
2031 oberon_fp_section(ctx
, &signature
-> num_decl
);
2035 oberon_assert_token(ctx
, RPAREN
);
2037 if(ctx
-> token
== COLON
)
2039 oberon_assert_token(ctx
, COLON
);
2041 oberon_object_t
* typeobj
;
2042 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2043 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2045 oberon_error(ctx
, "function result is not type");
2047 signature
-> base
= typeobj
-> type
;
2052 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2054 oberon_type_t
* signature
;
2056 signature
-> class = OBERON_TYPE_PROCEDURE
;
2057 signature
-> num_decl
= 0;
2058 signature
-> base
= ctx
-> void_type
;
2059 signature
-> decl
= NULL
;
2061 if(ctx
-> token
== LPAREN
)
2063 oberon_formal_pars(ctx
, signature
);
2068 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2070 if(a
-> num_decl
!= b
-> num_decl
)
2072 oberon_error(ctx
, "number parameters not matched");
2075 int num_param
= a
-> num_decl
;
2076 oberon_object_t
* param_a
= a
-> decl
;
2077 oberon_object_t
* param_b
= b
-> decl
;
2078 for(int i
= 0; i
< num_param
; i
++)
2080 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2082 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2085 if(param_a
-> type
!= param_b
-> type
)
2087 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2090 param_a
= param_a
-> next
;
2091 param_b
= param_b
-> next
;
2096 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2098 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2099 oberon_type_t
* result_type
= proc
-> type
-> base
;
2101 if(result_type
-> class == OBERON_TYPE_VOID
)
2105 oberon_error(ctx
, "procedure has no result type");
2112 oberon_error(ctx
, "procedure requires expression on result");
2115 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2118 proc
-> has_return
= 1;
2120 oberon_generate_return(ctx
, expr
);
2124 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2126 oberon_assert_token(ctx
, SEMICOLON
);
2128 ctx
-> decl
= proc
-> scope
;
2130 oberon_decl_seq(ctx
);
2132 oberon_generate_begin_proc(ctx
, proc
);
2134 if(ctx
-> token
== BEGIN
)
2136 oberon_assert_token(ctx
, BEGIN
);
2137 oberon_statement_seq(ctx
);
2140 oberon_assert_token(ctx
, END
);
2141 char * name
= oberon_assert_ident(ctx
);
2142 if(strcmp(name
, proc
-> name
) != 0)
2144 oberon_error(ctx
, "procedure name not matched");
2147 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2148 && proc
-> has_return
== 0)
2150 oberon_make_return(ctx
, NULL
);
2153 if(proc
-> has_return
== 0)
2155 oberon_error(ctx
, "procedure requires return");
2158 oberon_generate_end_proc(ctx
);
2159 oberon_close_scope(ctx
-> decl
);
2163 oberon_proc_decl(oberon_context_t
* ctx
)
2165 oberon_assert_token(ctx
, PROCEDURE
);
2168 if(ctx
-> token
== UPARROW
)
2170 oberon_assert_token(ctx
, UPARROW
);
2177 name
= oberon_assert_ident(ctx
);
2178 oberon_def(ctx
, &export
, &read_only
);
2180 oberon_scope_t
* proc_scope
;
2181 proc_scope
= oberon_open_scope(ctx
);
2182 ctx
-> decl
-> local
= 1;
2184 oberon_type_t
* signature
;
2185 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2186 oberon_opt_formal_pars(ctx
, &signature
);
2188 oberon_initialize_decl(ctx
);
2189 oberon_generator_init_type(ctx
, signature
);
2190 oberon_close_scope(ctx
-> decl
);
2192 oberon_object_t
* proc
;
2193 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2196 if(proc
-> class != OBERON_CLASS_PROC
)
2198 oberon_error(ctx
, "mult definition");
2205 oberon_error(ctx
, "mult procedure definition");
2209 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2211 oberon_error(ctx
, "export type not matched");
2214 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2218 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2219 proc
-> type
= signature
;
2220 proc
-> scope
= proc_scope
;
2221 oberon_generator_init_proc(ctx
, proc
);
2224 proc
-> scope
-> parent
= proc
;
2229 oberon_proc_decl_body(ctx
, proc
);
2234 oberon_const_decl(oberon_context_t
* ctx
)
2236 oberon_item_t
* value
;
2237 oberon_object_t
* constant
;
2239 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2240 oberon_assert_token(ctx
, EQUAL
);
2241 value
= oberon_const_expr(ctx
);
2242 constant
-> value
= value
;
2246 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2248 if(size
-> is_item
== 0)
2250 oberon_error(ctx
, "requires constant");
2253 if(size
-> item
.mode
!= MODE_INTEGER
)
2255 oberon_error(ctx
, "requires integer constant");
2258 oberon_type_t
* arr
;
2260 arr
-> class = OBERON_TYPE_ARRAY
;
2261 arr
-> size
= size
-> item
.integer
;
2266 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2269 oberon_object_t
* to
;
2271 to
= oberon_qualident(ctx
, &name
, 0);
2273 //name = oberon_assert_ident(ctx);
2274 //to = oberon_find_object(ctx -> decl, name, 0);
2278 if(to
-> class != OBERON_CLASS_TYPE
)
2280 oberon_error(ctx
, "not a type");
2285 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2286 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2292 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2295 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2299 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2307 oberon_type_t
* dim
;
2308 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2310 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2312 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2316 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2318 type
-> class = OBERON_TYPE_ARRAY
;
2320 type
-> base
= base
;
2324 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2326 if(ctx
-> token
== IDENT
)
2329 oberon_object_t
* list
;
2330 oberon_type_t
* type
;
2331 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2333 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2334 oberon_assert_token(ctx
, COLON
);
2336 oberon_scope_t
* current
= ctx
-> decl
;
2337 ctx
-> decl
= modscope
;
2338 oberon_type(ctx
, &type
);
2339 ctx
-> decl
= current
;
2341 oberon_object_t
* field
= list
;
2342 for(int i
= 0; i
< num
; i
++)
2344 field
-> type
= type
;
2345 field
= field
-> next
;
2348 rec
-> num_decl
+= num
;
2353 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2355 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2356 oberon_scope_t
* oldscope
= ctx
-> decl
;
2357 ctx
-> decl
= modscope
;
2359 if(ctx
-> token
== LPAREN
)
2361 oberon_assert_token(ctx
, LPAREN
);
2363 oberon_object_t
* typeobj
;
2364 typeobj
= oberon_qualident(ctx
, NULL
, true);
2366 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2368 oberon_error(ctx
, "base must be type");
2371 oberon_type_t
* base
= typeobj
-> type
;
2372 if(base
-> class == OBERON_TYPE_POINTER
)
2374 base
= base
-> base
;
2377 if(base
-> class != OBERON_TYPE_RECORD
)
2379 oberon_error(ctx
, "base must be record type");
2383 ctx
-> decl
= base
-> scope
;
2385 oberon_assert_token(ctx
, RPAREN
);
2392 oberon_scope_t
* this_scope
;
2393 this_scope
= oberon_open_scope(ctx
);
2394 this_scope
-> local
= true;
2395 this_scope
-> parent
= NULL
;
2396 this_scope
-> parent_type
= rec
;
2398 oberon_field_list(ctx
, rec
, modscope
);
2399 while(ctx
-> token
== SEMICOLON
)
2401 oberon_assert_token(ctx
, SEMICOLON
);
2402 oberon_field_list(ctx
, rec
, modscope
);
2405 rec
-> scope
= this_scope
;
2406 rec
-> decl
= this_scope
-> list
-> next
;
2407 ctx
-> decl
= oldscope
;
2411 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2413 if(ctx
-> token
== IDENT
)
2415 oberon_qualident_type(ctx
, type
);
2417 else if(ctx
-> token
== ARRAY
)
2419 oberon_assert_token(ctx
, ARRAY
);
2422 oberon_expr_t
* sizes
;
2424 if(ISEXPR(ctx
-> token
))
2426 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2429 oberon_assert_token(ctx
, OF
);
2431 oberon_type_t
* base
;
2432 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2433 oberon_type(ctx
, &base
);
2437 oberon_make_open_array(ctx
, base
, *type
);
2441 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2444 else if(ctx
-> token
== RECORD
)
2446 oberon_type_t
* rec
;
2448 rec
-> class = OBERON_TYPE_RECORD
;
2449 rec
-> module
= ctx
-> mod
;
2451 oberon_assert_token(ctx
, RECORD
);
2452 oberon_type_record_body(ctx
, rec
);
2453 oberon_assert_token(ctx
, END
);
2457 else if(ctx
-> token
== POINTER
)
2459 oberon_assert_token(ctx
, POINTER
);
2460 oberon_assert_token(ctx
, TO
);
2462 oberon_type_t
* base
;
2463 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2464 oberon_type(ctx
, &base
);
2466 oberon_type_t
* ptr
;
2468 ptr
-> class = OBERON_TYPE_POINTER
;
2471 else if(ctx
-> token
== PROCEDURE
)
2473 oberon_open_scope(ctx
);
2474 oberon_assert_token(ctx
, PROCEDURE
);
2475 oberon_opt_formal_pars(ctx
, type
);
2476 oberon_close_scope(ctx
-> decl
);
2480 oberon_error(ctx
, "invalid type declaration");
2485 oberon_type_decl(oberon_context_t
* ctx
)
2488 oberon_object_t
* newtype
;
2489 oberon_type_t
* type
;
2493 name
= oberon_assert_ident(ctx
);
2494 oberon_def(ctx
, &export
, &read_only
);
2496 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2499 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2500 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2501 assert(newtype
-> type
);
2505 if(newtype
-> class != OBERON_CLASS_TYPE
)
2507 oberon_error(ctx
, "mult definition");
2510 if(newtype
-> linked
)
2512 oberon_error(ctx
, "mult definition - already linked");
2515 newtype
-> export
= export
;
2516 newtype
-> read_only
= read_only
;
2519 oberon_assert_token(ctx
, EQUAL
);
2521 type
= newtype
-> type
;
2522 oberon_type(ctx
, &type
);
2524 if(type
-> class == OBERON_TYPE_VOID
)
2526 oberon_error(ctx
, "recursive alias declaration");
2529 newtype
-> type
= type
;
2530 newtype
-> linked
= 1;
2533 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2534 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2537 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2539 if(type
-> class != OBERON_TYPE_POINTER
2540 && type
-> class != OBERON_TYPE_ARRAY
)
2545 if(type
-> recursive
)
2547 oberon_error(ctx
, "recursive pointer declaration");
2550 if(type
-> class == OBERON_TYPE_POINTER
2551 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2553 oberon_error(ctx
, "attempt to make pointer to pointer");
2556 type
-> recursive
= 1;
2558 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2560 type
-> recursive
= 0;
2564 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2566 if(type
-> class != OBERON_TYPE_RECORD
)
2571 if(type
-> recursive
)
2573 oberon_error(ctx
, "recursive record declaration");
2576 type
-> recursive
= 1;
2578 int num_fields
= type
-> num_decl
;
2579 oberon_object_t
* field
= type
-> decl
;
2580 for(int i
= 0; i
< num_fields
; i
++)
2582 oberon_prevent_recursive_object(ctx
, field
);
2583 field
= field
-> next
;
2586 type
-> recursive
= 0;
2589 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2591 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2596 if(type
-> recursive
)
2598 oberon_error(ctx
, "recursive procedure declaration");
2601 type
-> recursive
= 1;
2603 int num_fields
= type
-> num_decl
;
2604 oberon_object_t
* field
= type
-> decl
;
2605 for(int i
= 0; i
< num_fields
; i
++)
2607 oberon_prevent_recursive_object(ctx
, field
);
2608 field
= field
-> next
;
2611 type
-> recursive
= 0;
2615 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2617 if(type
-> class != OBERON_TYPE_ARRAY
)
2622 if(type
-> recursive
)
2624 oberon_error(ctx
, "recursive array declaration");
2627 type
-> recursive
= 1;
2629 oberon_prevent_recursive_type(ctx
, type
-> base
);
2631 type
-> recursive
= 0;
2635 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2637 if(type
-> class == OBERON_TYPE_POINTER
)
2639 oberon_prevent_recursive_pointer(ctx
, type
);
2641 else if(type
-> class == OBERON_TYPE_RECORD
)
2643 oberon_prevent_recursive_record(ctx
, type
);
2645 else if(type
-> class == OBERON_TYPE_ARRAY
)
2647 oberon_prevent_recursive_array(ctx
, type
);
2649 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2651 oberon_prevent_recursive_procedure(ctx
, type
);
2656 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2660 case OBERON_CLASS_VAR
:
2661 case OBERON_CLASS_TYPE
:
2662 case OBERON_CLASS_PARAM
:
2663 case OBERON_CLASS_VAR_PARAM
:
2664 case OBERON_CLASS_FIELD
:
2665 oberon_prevent_recursive_type(ctx
, x
-> type
);
2667 case OBERON_CLASS_CONST
:
2668 case OBERON_CLASS_PROC
:
2669 case OBERON_CLASS_MODULE
:
2672 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2678 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2680 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2684 oberon_prevent_recursive_object(ctx
, x
);
2689 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2690 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2693 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2695 if(type
-> class != OBERON_TYPE_RECORD
)
2700 int num_fields
= type
-> num_decl
;
2701 oberon_object_t
* field
= type
-> decl
;
2702 for(int i
= 0; i
< num_fields
; i
++)
2704 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2706 oberon_initialize_type(ctx
, field
-> type
);
2709 oberon_initialize_object(ctx
, field
);
2710 field
= field
-> next
;
2713 oberon_generator_init_record(ctx
, type
);
2717 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2719 if(type
-> class == OBERON_TYPE_VOID
)
2721 oberon_error(ctx
, "undeclarated type");
2724 if(type
-> initialized
)
2729 type
-> initialized
= 1;
2731 if(type
-> class == OBERON_TYPE_POINTER
)
2733 oberon_initialize_type(ctx
, type
-> base
);
2734 oberon_generator_init_type(ctx
, type
);
2736 else if(type
-> class == OBERON_TYPE_ARRAY
)
2738 if(type
-> size
!= 0)
2740 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2742 if(type
-> base
-> size
== 0)
2744 oberon_error(ctx
, "open array not allowed as array element");
2749 oberon_initialize_type(ctx
, type
-> base
);
2750 oberon_generator_init_type(ctx
, type
);
2752 else if(type
-> class == OBERON_TYPE_RECORD
)
2754 oberon_generator_init_type(ctx
, type
);
2755 oberon_initialize_record_fields(ctx
, type
);
2757 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2759 int num_fields
= type
-> num_decl
;
2760 oberon_object_t
* field
= type
-> decl
;
2761 for(int i
= 0; i
< num_fields
; i
++)
2763 oberon_initialize_object(ctx
, field
);
2764 field
= field
-> next
;
2767 oberon_generator_init_type(ctx
, type
);
2771 oberon_generator_init_type(ctx
, type
);
2776 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2778 if(x
-> initialized
)
2783 x
-> initialized
= 1;
2787 case OBERON_CLASS_TYPE
:
2788 oberon_initialize_type(ctx
, x
-> type
);
2790 case OBERON_CLASS_VAR
:
2791 case OBERON_CLASS_FIELD
:
2792 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2794 if(x
-> type
-> size
== 0)
2796 oberon_error(ctx
, "open array not allowed as variable or field");
2799 oberon_initialize_type(ctx
, x
-> type
);
2800 oberon_generator_init_var(ctx
, x
);
2802 case OBERON_CLASS_PARAM
:
2803 case OBERON_CLASS_VAR_PARAM
:
2804 oberon_initialize_type(ctx
, x
-> type
);
2805 oberon_generator_init_var(ctx
, x
);
2807 case OBERON_CLASS_CONST
:
2808 case OBERON_CLASS_PROC
:
2809 case OBERON_CLASS_MODULE
:
2812 oberon_error(ctx
, "oberon_initialize_object: wat");
2818 oberon_initialize_decl(oberon_context_t
* ctx
)
2820 oberon_object_t
* x
= ctx
-> decl
-> list
;
2824 oberon_initialize_object(ctx
, x
-> next
);
2830 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2832 oberon_object_t
* x
= ctx
-> decl
-> list
;
2836 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2838 if(x
-> next
-> linked
== 0)
2840 oberon_error(ctx
, "unresolved forward declaration");
2848 oberon_decl_seq(oberon_context_t
* ctx
)
2850 if(ctx
-> token
== CONST
)
2852 oberon_assert_token(ctx
, CONST
);
2853 while(ctx
-> token
== IDENT
)
2855 oberon_const_decl(ctx
);
2856 oberon_assert_token(ctx
, SEMICOLON
);
2860 if(ctx
-> token
== TYPE
)
2862 oberon_assert_token(ctx
, TYPE
);
2863 while(ctx
-> token
== IDENT
)
2865 oberon_type_decl(ctx
);
2866 oberon_assert_token(ctx
, SEMICOLON
);
2870 if(ctx
-> token
== VAR
)
2872 oberon_assert_token(ctx
, VAR
);
2873 while(ctx
-> token
== IDENT
)
2875 oberon_var_decl(ctx
);
2876 oberon_assert_token(ctx
, SEMICOLON
);
2880 oberon_prevent_recursive_decl(ctx
);
2881 oberon_initialize_decl(ctx
);
2883 while(ctx
-> token
== PROCEDURE
)
2885 oberon_proc_decl(ctx
);
2886 oberon_assert_token(ctx
, SEMICOLON
);
2889 oberon_prevent_undeclarated_procedures(ctx
);
2893 oberon_statement_seq(oberon_context_t
* ctx
);
2896 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
2898 if(dst
-> read_only
)
2900 oberon_error(ctx
, "read-only destination");
2903 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
2904 oberon_generate_assign(ctx
, src
, dst
);
2908 oberon_statement(oberon_context_t
* ctx
)
2910 oberon_expr_t
* item1
;
2911 oberon_expr_t
* item2
;
2913 if(ctx
-> token
== IDENT
)
2915 item1
= oberon_designator(ctx
);
2916 if(ctx
-> token
== ASSIGN
)
2918 oberon_assert_token(ctx
, ASSIGN
);
2919 item2
= oberon_expr(ctx
);
2920 oberon_assign(ctx
, item2
, item1
);
2924 oberon_opt_proc_parens(ctx
, item1
);
2927 else if(ctx
-> token
== IF
)
2931 oberon_expr_t
* cond
;
2933 els
= oberon_generator_reserve_label(ctx
);
2934 end
= oberon_generator_reserve_label(ctx
);
2936 oberon_assert_token(ctx
, IF
);
2937 cond
= oberon_expr(ctx
);
2938 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
2940 oberon_error(ctx
, "condition must be boolean");
2942 oberon_assert_token(ctx
, THEN
);
2943 oberon_generate_branch(ctx
, cond
, false, els
);
2944 oberon_statement_seq(ctx
);
2945 oberon_generate_goto(ctx
, end
);
2946 oberon_generate_label(ctx
, els
);
2948 while(ctx
-> token
== ELSIF
)
2950 els
= oberon_generator_reserve_label(ctx
);
2952 oberon_assert_token(ctx
, ELSIF
);
2953 cond
= oberon_expr(ctx
);
2954 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
2956 oberon_error(ctx
, "condition must be boolean");
2958 oberon_assert_token(ctx
, THEN
);
2959 oberon_generate_branch(ctx
, cond
, false, els
);
2960 oberon_statement_seq(ctx
);
2961 oberon_generate_goto(ctx
, end
);
2962 oberon_generate_label(ctx
, els
);
2965 if(ctx
-> token
== ELSE
)
2967 oberon_assert_token(ctx
, ELSE
);
2968 oberon_statement_seq(ctx
);
2971 oberon_generate_label(ctx
, end
);
2972 oberon_assert_token(ctx
, END
);
2974 else if(ctx
-> token
== WHILE
)
2976 gen_label_t
* begin
;
2978 oberon_expr_t
* cond
;
2980 begin
= oberon_generator_reserve_label(ctx
);
2981 end
= oberon_generator_reserve_label(ctx
);
2983 oberon_assert_token(ctx
, WHILE
);
2984 oberon_generate_label(ctx
, begin
);
2985 cond
= oberon_expr(ctx
);
2986 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
2988 oberon_error(ctx
, "condition must be boolean");
2990 oberon_generate_branch(ctx
, cond
, false, end
);
2992 oberon_assert_token(ctx
, DO
);
2993 oberon_statement_seq(ctx
);
2994 oberon_generate_goto(ctx
, begin
);
2996 oberon_assert_token(ctx
, END
);
2997 oberon_generate_label(ctx
, end
);
2999 else if(ctx
-> token
== REPEAT
)
3001 gen_label_t
* begin
;
3002 oberon_expr_t
* cond
;
3004 begin
= oberon_generator_reserve_label(ctx
);
3005 oberon_generate_label(ctx
, begin
);
3006 oberon_assert_token(ctx
, REPEAT
);
3008 oberon_statement_seq(ctx
);
3010 oberon_assert_token(ctx
, UNTIL
);
3012 cond
= oberon_expr(ctx
);
3013 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3015 oberon_error(ctx
, "condition must be boolean");
3018 oberon_generate_branch(ctx
, cond
, true, begin
);
3020 else if(ctx
-> token
== RETURN
)
3022 oberon_assert_token(ctx
, RETURN
);
3023 if(ISEXPR(ctx
-> token
))
3025 oberon_expr_t
* expr
;
3026 expr
= oberon_expr(ctx
);
3027 oberon_make_return(ctx
, expr
);
3031 oberon_make_return(ctx
, NULL
);
3037 oberon_statement_seq(oberon_context_t
* ctx
)
3039 oberon_statement(ctx
);
3040 while(ctx
-> token
== SEMICOLON
)
3042 oberon_assert_token(ctx
, SEMICOLON
);
3043 oberon_statement(ctx
);
3048 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3050 oberon_module_t
* m
= ctx
-> module_list
;
3051 while(m
&& strcmp(m
-> name
, name
) != 0)
3059 code
= ctx
-> import_module(name
);
3062 oberon_error(ctx
, "no such module");
3065 m
= oberon_compile_module(ctx
, code
);
3071 oberon_error(ctx
, "cyclic module import");
3074 oberon_object_t
* ident
;
3075 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3076 ident
-> module
= m
;
3080 oberon_import_decl(oberon_context_t
* ctx
)
3085 alias
= name
= oberon_assert_ident(ctx
);
3086 if(ctx
-> token
== ASSIGN
)
3088 oberon_assert_token(ctx
, ASSIGN
);
3089 name
= oberon_assert_ident(ctx
);
3092 oberon_import_module(ctx
, alias
, name
);
3096 oberon_import_list(oberon_context_t
* ctx
)
3098 oberon_assert_token(ctx
, IMPORT
);
3100 oberon_import_decl(ctx
);
3101 while(ctx
-> token
== COMMA
)
3103 oberon_assert_token(ctx
, COMMA
);
3104 oberon_import_decl(ctx
);
3107 oberon_assert_token(ctx
, SEMICOLON
);
3111 oberon_parse_module(oberon_context_t
* ctx
)
3115 oberon_read_token(ctx
);
3117 oberon_assert_token(ctx
, MODULE
);
3118 name1
= oberon_assert_ident(ctx
);
3119 oberon_assert_token(ctx
, SEMICOLON
);
3120 ctx
-> mod
-> name
= name1
;
3122 oberon_generator_init_module(ctx
, ctx
-> mod
);
3124 if(ctx
-> token
== IMPORT
)
3126 oberon_import_list(ctx
);
3129 oberon_decl_seq(ctx
);
3131 oberon_generate_begin_module(ctx
);
3132 if(ctx
-> token
== BEGIN
)
3134 oberon_assert_token(ctx
, BEGIN
);
3135 oberon_statement_seq(ctx
);
3137 oberon_generate_end_module(ctx
);
3139 oberon_assert_token(ctx
, END
);
3140 name2
= oberon_assert_ident(ctx
);
3141 oberon_assert_token(ctx
, DOT
);
3143 if(strcmp(name1
, name2
) != 0)
3145 oberon_error(ctx
, "module name not matched");
3148 oberon_generator_fini_module(ctx
-> mod
);
3151 // =======================================================================
3153 // =======================================================================
3156 register_default_types(oberon_context_t
* ctx
)
3158 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3159 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3161 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3162 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3163 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3165 ctx
-> string_type
= oberon_new_type_string(1);
3166 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3168 ctx
-> bool_type
= oberon_new_type_boolean();
3169 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3171 ctx
-> byte_type
= oberon_new_type_integer(1);
3172 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3174 ctx
-> shortint_type
= oberon_new_type_integer(2);
3175 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3177 ctx
-> int_type
= oberon_new_type_integer(4);
3178 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3180 ctx
-> longint_type
= oberon_new_type_integer(8);
3181 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3183 ctx
-> real_type
= oberon_new_type_real(4);
3184 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3186 ctx
-> longreal_type
= oberon_new_type_real(8);
3187 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3189 ctx
-> char_type
= oberon_new_type_char(1);
3190 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3194 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3196 oberon_object_t
* proc
;
3197 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3198 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3199 proc
-> type
-> sysproc
= true;
3200 proc
-> type
-> genfunc
= f
;
3201 proc
-> type
-> genproc
= p
;
3204 static oberon_expr_t
*
3205 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3209 oberon_error(ctx
, "too few arguments");
3214 oberon_error(ctx
, "too mach arguments");
3217 oberon_expr_t
* arg
;
3220 oberon_type_t
* result_type
;
3221 result_type
= arg
-> result
;
3223 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3225 oberon_error(ctx
, "ABS accepts only integers");
3229 oberon_expr_t
* expr
;
3230 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3235 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3239 oberon_error(ctx
, "too few arguments");
3242 oberon_expr_t
* dst
;
3245 oberon_type_t
* type
;
3246 type
= dst
-> result
;
3248 if(type
-> class != OBERON_TYPE_POINTER
)
3250 oberon_error(ctx
, "not a pointer");
3253 type
= type
-> base
;
3255 oberon_expr_t
* src
;
3256 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3257 src
-> item
.num_args
= 0;
3258 src
-> item
.args
= NULL
;
3261 if(type
-> class == OBERON_TYPE_ARRAY
)
3263 if(type
-> size
== 0)
3265 oberon_type_t
* x
= type
;
3266 while(x
-> class == OBERON_TYPE_ARRAY
)
3276 if(num_args
< max_args
)
3278 oberon_error(ctx
, "too few arguments");
3281 if(num_args
> max_args
)
3283 oberon_error(ctx
, "too mach arguments");
3286 int num_sizes
= max_args
- 1;
3287 oberon_expr_t
* size_list
= list_args
-> next
;
3289 oberon_expr_t
* arg
= size_list
;
3290 for(int i
= 0; i
< max_args
- 1; i
++)
3292 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3294 oberon_error(ctx
, "size must be integer");
3299 src
-> item
.num_args
= num_sizes
;
3300 src
-> item
.args
= size_list
;
3302 else if(type
-> class != OBERON_TYPE_RECORD
)
3304 oberon_error(ctx
, "oberon_make_new_call: wat");
3307 if(num_args
> max_args
)
3309 oberon_error(ctx
, "too mach arguments");
3312 oberon_assign(ctx
, src
, dst
);
3316 oberon_create_context(ModuleImportCallback import_module
)
3318 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3320 oberon_scope_t
* world_scope
;
3321 world_scope
= oberon_open_scope(ctx
);
3322 ctx
-> world_scope
= world_scope
;
3324 ctx
-> import_module
= import_module
;
3326 oberon_generator_init_context(ctx
);
3328 register_default_types(ctx
);
3329 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3330 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3336 oberon_destroy_context(oberon_context_t
* ctx
)
3338 oberon_generator_destroy_context(ctx
);
3343 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3345 const char * code
= ctx
-> code
;
3346 int code_index
= ctx
-> code_index
;
3348 int token
= ctx
-> token
;
3349 char * string
= ctx
-> string
;
3350 int integer
= ctx
-> integer
;
3351 int real
= ctx
-> real
;
3352 bool longmode
= ctx
-> longmode
;
3353 oberon_scope_t
* decl
= ctx
-> decl
;
3354 oberon_module_t
* mod
= ctx
-> mod
;
3356 oberon_scope_t
* module_scope
;
3357 module_scope
= oberon_open_scope(ctx
);
3359 oberon_module_t
* module
;
3360 module
= calloc(1, sizeof *module
);
3361 module
-> decl
= module_scope
;
3362 module
-> next
= ctx
-> module_list
;
3364 ctx
-> mod
= module
;
3365 ctx
-> module_list
= module
;
3367 oberon_init_scaner(ctx
, newcode
);
3368 oberon_parse_module(ctx
);
3370 module
-> ready
= 1;
3373 ctx
-> code_index
= code_index
;
3375 ctx
-> token
= token
;
3376 ctx
-> string
= string
;
3377 ctx
-> integer
= integer
;
3379 ctx
-> longmode
= longmode
;