570db39df1417efe4ffde397dc3f600ab2cc9e22
9 #include "../include/oberon.h"
11 #include "oberon-internals.h"
12 #include "generator.h"
65 // =======================================================================
67 // =======================================================================
70 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
74 fprintf(stderr
, "error: ");
75 vfprintf(stderr
, fmt
, ptr
);
76 fprintf(stderr
, "\n");
77 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
78 fprintf(stderr
, " c = %c\n", ctx
-> c
);
79 fprintf(stderr
, " token = %i\n", ctx
-> token
);
84 static oberon_type_t
*
85 oberon_new_type_ptr(int class)
87 oberon_type_t
* x
= malloc(sizeof *x
);
88 memset(x
, 0, sizeof *x
);
93 static oberon_type_t
*
94 oberon_new_type_integer(int size
)
97 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
102 static oberon_type_t
*
103 oberon_new_type_boolean()
106 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
110 static oberon_type_t
*
111 oberon_new_type_real(int size
)
114 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
119 // =======================================================================
121 // =======================================================================
123 static oberon_scope_t
*
124 oberon_open_scope(oberon_context_t
* ctx
)
126 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
127 oberon_object_t
* list
= calloc(1, sizeof *list
);
130 scope
-> list
= list
;
131 scope
-> up
= ctx
-> decl
;
135 scope
-> local
= scope
-> up
-> local
;
136 scope
-> parent
= scope
-> up
-> parent
;
137 scope
-> parent_type
= scope
-> up
-> parent_type
;
145 oberon_close_scope(oberon_scope_t
* scope
)
147 oberon_context_t
* ctx
= scope
-> ctx
;
148 ctx
-> decl
= scope
-> up
;
151 static oberon_object_t
*
152 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
154 oberon_object_t
* x
= list
;
155 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
162 static oberon_object_t
*
163 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
165 oberon_object_t
* result
= NULL
;
167 oberon_scope_t
* s
= scope
;
168 while(result
== NULL
&& s
!= NULL
)
170 result
= oberon_find_object_in_list(s
-> list
, name
);
174 if(check_it
&& result
== NULL
)
176 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
182 static oberon_object_t
*
183 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
187 if(oberon_find_object(scope
-> up
, name
, false))
189 oberon_error(scope
-> ctx
, "already defined");
193 oberon_object_t
* x
= scope
-> list
;
194 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
201 oberon_error(scope
-> ctx
, "already defined");
204 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
205 memset(newvar
, 0, sizeof *newvar
);
206 newvar
-> name
= name
;
207 newvar
-> class = class;
208 newvar
-> export
= export
;
209 newvar
-> read_only
= read_only
;
210 newvar
-> local
= scope
-> local
;
211 newvar
-> parent
= scope
-> parent
;
212 newvar
-> parent_type
= scope
-> parent_type
;
213 newvar
-> module
= scope
-> ctx
-> mod
;
220 static oberon_object_t
*
221 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
223 oberon_object_t
* id
;
224 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
226 oberon_generator_init_type(scope
-> ctx
, type
);
230 // =======================================================================
232 // =======================================================================
235 oberon_get_char(oberon_context_t
* ctx
)
237 if(ctx
-> code
[ctx
-> code_index
])
239 ctx
-> code_index
+= 1;
240 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
245 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
248 ctx
-> code_index
= 0;
249 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
253 oberon_read_ident(oberon_context_t
* ctx
)
256 int i
= ctx
-> code_index
;
258 int c
= ctx
-> code
[i
];
266 char * ident
= malloc(len
+ 1);
267 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
270 ctx
-> code_index
= i
;
271 ctx
-> c
= ctx
-> code
[i
];
272 ctx
-> string
= ident
;
273 ctx
-> token
= IDENT
;
275 if(strcmp(ident
, "MODULE") == 0)
277 ctx
-> token
= MODULE
;
279 else if(strcmp(ident
, "END") == 0)
283 else if(strcmp(ident
, "VAR") == 0)
287 else if(strcmp(ident
, "BEGIN") == 0)
289 ctx
-> token
= BEGIN
;
291 else if(strcmp(ident
, "TRUE") == 0)
295 else if(strcmp(ident
, "FALSE") == 0)
297 ctx
-> token
= FALSE
;
299 else if(strcmp(ident
, "OR") == 0)
303 else if(strcmp(ident
, "DIV") == 0)
307 else if(strcmp(ident
, "MOD") == 0)
311 else if(strcmp(ident
, "PROCEDURE") == 0)
313 ctx
-> token
= PROCEDURE
;
315 else if(strcmp(ident
, "RETURN") == 0)
317 ctx
-> token
= RETURN
;
319 else if(strcmp(ident
, "CONST") == 0)
321 ctx
-> token
= CONST
;
323 else if(strcmp(ident
, "TYPE") == 0)
327 else if(strcmp(ident
, "ARRAY") == 0)
329 ctx
-> token
= ARRAY
;
331 else if(strcmp(ident
, "OF") == 0)
335 else if(strcmp(ident
, "RECORD") == 0)
337 ctx
-> token
= RECORD
;
339 else if(strcmp(ident
, "POINTER") == 0)
341 ctx
-> token
= POINTER
;
343 else if(strcmp(ident
, "TO") == 0)
347 else if(strcmp(ident
, "NIL") == 0)
351 else if(strcmp(ident
, "IMPORT") == 0)
353 ctx
-> token
= IMPORT
;
355 else if(strcmp(ident
, "IN") == 0)
359 else if(strcmp(ident
, "IS") == 0)
366 oberon_read_number(oberon_context_t
* ctx
)
379 * mode = 3 == LONGREAL
382 start_i
= ctx
-> code_index
;
384 while(isdigit(ctx
-> c
))
386 oberon_get_char(ctx
);
389 end_i
= ctx
-> code_index
;
391 if(isxdigit(ctx
-> c
))
394 while(isxdigit(ctx
-> c
))
396 oberon_get_char(ctx
);
399 end_i
= ctx
-> code_index
;
403 oberon_error(ctx
, "invalid hex number");
405 oberon_get_char(ctx
);
407 else if(ctx
-> c
== '.')
410 oberon_get_char(ctx
);
412 while(isdigit(ctx
-> c
))
414 oberon_get_char(ctx
);
417 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
419 exp_i
= ctx
-> code_index
;
426 oberon_get_char(ctx
);
428 if(ctx
-> c
== '+' || ctx
-> c
== '-')
430 oberon_get_char(ctx
);
433 while(isdigit(ctx
-> c
))
435 oberon_get_char(ctx
);
440 end_i
= ctx
-> code_index
;
443 int len
= end_i
- start_i
;
444 ident
= malloc(len
+ 1);
445 memcpy(ident
, &ctx
-> code
[start_i
], len
);
448 ctx
-> longmode
= false;
451 int i
= exp_i
- start_i
;
453 ctx
-> longmode
= true;
459 integer
= atol(ident
);
461 ctx
-> token
= INTEGER
;
464 sscanf(ident
, "%lx", &integer
);
466 ctx
-> token
= INTEGER
;
470 sscanf(ident
, "%lf", &real
);
474 oberon_error(ctx
, "oberon_read_number: wat");
478 ctx
-> string
= ident
;
479 ctx
-> integer
= integer
;
484 oberon_skip_space(oberon_context_t
* ctx
)
486 while(isspace(ctx
-> c
))
488 oberon_get_char(ctx
);
493 oberon_read_comment(oberon_context_t
* ctx
)
500 oberon_get_char(ctx
);
503 oberon_get_char(ctx
);
507 else if(ctx
-> c
== '*')
509 oberon_get_char(ctx
);
512 oberon_get_char(ctx
);
516 else if(ctx
-> c
== 0)
518 oberon_error(ctx
, "unterminated comment");
522 oberon_get_char(ctx
);
527 static void oberon_read_token(oberon_context_t
* ctx
);
530 oberon_read_symbol(oberon_context_t
* ctx
)
539 ctx
-> token
= SEMICOLON
;
540 oberon_get_char(ctx
);
543 ctx
-> token
= COLON
;
544 oberon_get_char(ctx
);
547 ctx
-> token
= ASSIGN
;
548 oberon_get_char(ctx
);
553 oberon_get_char(ctx
);
556 ctx
-> token
= LPAREN
;
557 oberon_get_char(ctx
);
560 oberon_get_char(ctx
);
561 oberon_read_comment(ctx
);
562 oberon_read_token(ctx
);
566 ctx
-> token
= RPAREN
;
567 oberon_get_char(ctx
);
570 ctx
-> token
= EQUAL
;
571 oberon_get_char(ctx
);
575 oberon_get_char(ctx
);
579 oberon_get_char(ctx
);
583 oberon_get_char(ctx
);
587 ctx
-> token
= GREAT
;
588 oberon_get_char(ctx
);
592 oberon_get_char(ctx
);
597 oberon_get_char(ctx
);
600 ctx
-> token
= MINUS
;
601 oberon_get_char(ctx
);
605 oberon_get_char(ctx
);
608 oberon_get_char(ctx
);
609 oberon_error(ctx
, "unstarted comment");
613 ctx
-> token
= SLASH
;
614 oberon_get_char(ctx
);
618 oberon_get_char(ctx
);
622 oberon_get_char(ctx
);
625 ctx
-> token
= COMMA
;
626 oberon_get_char(ctx
);
629 ctx
-> token
= LBRACE
;
630 oberon_get_char(ctx
);
633 ctx
-> token
= RBRACE
;
634 oberon_get_char(ctx
);
637 ctx
-> token
= UPARROW
;
638 oberon_get_char(ctx
);
641 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
647 oberon_read_token(oberon_context_t
* ctx
)
649 oberon_skip_space(ctx
);
654 oberon_read_ident(ctx
);
658 oberon_read_number(ctx
);
662 oberon_read_symbol(ctx
);
666 // =======================================================================
668 // =======================================================================
670 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
671 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
672 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
673 static char * oberon_assert_ident(oberon_context_t
* ctx
);
674 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
675 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
677 static oberon_expr_t
*
678 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
680 oberon_oper_t
* operator;
681 operator = malloc(sizeof *operator);
682 memset(operator, 0, sizeof *operator);
684 operator -> is_item
= 0;
685 operator -> result
= result
;
686 operator -> read_only
= 1;
688 operator -> left
= left
;
689 operator -> right
= right
;
691 return (oberon_expr_t
*) operator;
694 static oberon_expr_t
*
695 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
697 oberon_item_t
* item
;
698 item
= malloc(sizeof *item
);
699 memset(item
, 0, sizeof *item
);
702 item
-> result
= result
;
703 item
-> read_only
= read_only
;
706 return (oberon_expr_t
*)item
;
709 static oberon_expr_t
*
710 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
712 oberon_expr_t
* expr
;
713 oberon_type_t
* result
;
715 result
= a
-> result
;
719 if(result
-> class != OBERON_TYPE_INTEGER
)
721 oberon_error(ctx
, "incompatible operator type");
724 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
726 else if(token
== NOT
)
728 if(result
-> class != OBERON_TYPE_BOOLEAN
)
730 oberon_error(ctx
, "incompatible operator type");
733 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
737 oberon_error(ctx
, "oberon_make_unary_op: wat");
744 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
746 oberon_expr_t
* last
;
749 *first
= last
= oberon_expr(ctx
);
750 while(ctx
-> token
== COMMA
)
752 oberon_assert_token(ctx
, COMMA
);
753 oberon_expr_t
* current
;
757 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
761 current
= oberon_expr(ctx
);
764 last
-> next
= current
;
770 static oberon_expr_t
*
771 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
773 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
776 static oberon_expr_t
*
777 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
779 if(expr
-> result
-> class != OBERON_TYPE_RECORD
780 || rec
-> class != OBERON_TYPE_RECORD
)
782 oberon_error(ctx
, "must be record type");
785 return oberon_cast_expr(ctx
, expr
, rec
);
788 static oberon_type_t
*
789 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
791 oberon_type_t
* result
;
792 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
796 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
800 else if(a
-> class != b
-> class)
802 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
804 else if(a
-> size
> b
-> size
)
816 static oberon_expr_t
*
817 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
819 if(pref
-> class != expr
-> result
-> class)
821 if(pref
-> class == OBERON_TYPE_POINTER
)
823 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
829 oberon_error(ctx
, "incompatible types");
832 else if(pref
-> class == OBERON_TYPE_REAL
)
834 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
840 oberon_error(ctx
, "incompatible types");
845 oberon_error(ctx
, "incompatible types");
849 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
851 if(expr
-> result
-> size
> pref
-> size
)
853 oberon_error(ctx
, "incompatible size");
857 expr
= oberon_cast_expr(ctx
, expr
, pref
);
860 else if(pref
-> class == OBERON_TYPE_RECORD
)
862 oberon_type_t
* t
= expr
-> result
;
863 while(t
!= NULL
&& t
!= pref
)
869 printf("oberon_autocast_to: rec %p != %p\n", expr
-> result
, pref
);
870 oberon_error(ctx
, "incompatible record types");
872 if(expr
-> result
!= pref
)
874 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
877 else if(pref
-> class == OBERON_TYPE_POINTER
)
879 if(expr
-> result
-> base
!= pref
-> base
)
881 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
883 oberon_error(ctx
, "incompatible pointer types");
892 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
894 oberon_type_t
* a
= (*ea
) -> result
;
895 oberon_type_t
* b
= (*eb
) -> result
;
896 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
897 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
898 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
902 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
904 if(desig
-> mode
!= MODE_CALL
)
906 oberon_error(ctx
, "expected mode CALL");
909 oberon_type_t
* fn
= desig
-> parent
-> result
;
910 int num_args
= desig
-> num_args
;
911 int num_decl
= fn
-> num_decl
;
913 if(num_args
< num_decl
)
915 oberon_error(ctx
, "too few arguments");
917 else if(num_args
> num_decl
)
919 oberon_error(ctx
, "too many arguments");
922 /* Делаем проверку на запись и делаем автокаст */
923 oberon_expr_t
* casted
[num_args
];
924 oberon_expr_t
* arg
= desig
-> args
;
925 oberon_object_t
* param
= fn
-> decl
;
926 for(int i
= 0; i
< num_args
; i
++)
928 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
932 oberon_error(ctx
, "assign to read-only var");
936 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
938 param
= param
-> next
;
941 /* Создаём новый список выражений */
945 for(int i
= 0; i
< num_args
- 1; i
++)
947 casted
[i
] -> next
= casted
[i
+ 1];
953 static oberon_expr_t
*
954 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
956 oberon_type_t
* signature
= item
-> result
;
957 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
959 oberon_error(ctx
, "not a procedure");
962 oberon_expr_t
* call
;
964 if(signature
-> sysproc
)
966 if(signature
-> genfunc
== NULL
)
968 oberon_error(ctx
, "not a function-procedure");
971 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
975 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
977 oberon_error(ctx
, "attempt to call procedure in expression");
980 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
981 call
-> item
.parent
= item
;
982 call
-> item
.num_args
= num_args
;
983 call
-> item
.args
= list_args
;
984 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
991 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
993 oberon_type_t
* signature
= item
-> result
;
994 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
996 oberon_error(ctx
, "not a procedure");
999 oberon_expr_t
* call
;
1001 if(signature
-> sysproc
)
1003 if(signature
-> genproc
== NULL
)
1005 oberon_error(ctx
, "not a procedure");
1008 signature
-> genproc(ctx
, num_args
, list_args
);
1012 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1014 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1017 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1018 call
-> item
.parent
= item
;
1019 call
-> item
.num_args
= num_args
;
1020 call
-> item
.args
= list_args
;
1021 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1022 oberon_generate_call_proc(ctx
, call
);
1028 oberon_make_call_proc(oberon_context_t * ctx, oberon_object_t * proc, int num_args, oberon_expr_t * list_args)
1030 switch(proc -> class)
1032 case OBERON_CLASS_PROC:
1033 if(proc -> class != OBERON_CLASS_PROC)
1035 oberon_error(ctx, "not a procedure");
1038 case OBERON_CLASS_VAR:
1039 case OBERON_CLASS_VAR_PARAM:
1040 case OBERON_CLASS_PARAM:
1041 if(proc -> type -> class != OBERON_TYPE_PROCEDURE)
1043 oberon_error(ctx, "not a procedure");
1047 oberon_error(ctx, "not a procedure");
1053 if(proc -> genproc == NULL)
1055 oberon_error(ctx, "requres non-typed procedure");
1058 proc -> genproc(ctx, num_args, list_args);
1062 if(proc -> type -> base -> class != OBERON_TYPE_VOID)
1064 oberon_error(ctx, "attempt to call function as non-typed procedure");
1067 oberon_expr_t * call;
1068 call = oberon_new_item(MODE_CALL, proc -> type -> base, 1);
1069 call -> item.var = proc;
1070 call -> item.num_args = num_args;
1071 call -> item.args = list_args;
1072 oberon_autocast_call(ctx, call);
1073 oberon_generate_call_proc(ctx, call);
1082 || ((x) == INTEGER) \
1083 || ((x) == LPAREN) \
1088 static oberon_expr_t
*
1089 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1091 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1093 oberon_error(ctx
, "not a pointer");
1096 assert(expr
-> is_item
);
1098 oberon_expr_t
* selector
;
1099 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1100 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1105 static oberon_expr_t
*
1106 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1108 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1110 desig
= oberno_make_dereferencing(ctx
, desig
);
1113 assert(desig
-> is_item
);
1115 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1117 oberon_error(ctx
, "not array");
1120 oberon_type_t
* base
;
1121 base
= desig
-> result
-> base
;
1123 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1125 oberon_error(ctx
, "index must be integer");
1128 // Статическая проверка границ массива
1129 if(desig
-> result
-> size
!= 0)
1131 if(index
-> is_item
)
1133 if(index
-> item
.mode
== MODE_INTEGER
)
1135 int arr_size
= desig
-> result
-> size
;
1136 int index_int
= index
-> item
.integer
;
1137 if(index_int
< 0 || index_int
> arr_size
- 1)
1139 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1145 oberon_expr_t
* selector
;
1146 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1147 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1148 selector
-> item
.num_args
= 1;
1149 selector
-> item
.args
= index
;
1154 static oberon_expr_t
*
1155 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1157 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1159 expr
= oberno_make_dereferencing(ctx
, expr
);
1162 assert(expr
-> is_item
);
1164 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1166 oberon_error(ctx
, "not record");
1169 oberon_type_t
* rec
= expr
-> result
;
1171 oberon_object_t
* field
;
1172 field
= oberon_find_object(rec
-> scope
, name
, true);
1174 if(field
-> export
== 0)
1176 if(field
-> module
!= ctx
-> mod
)
1178 oberon_error(ctx
, "field not exported");
1183 if(field
-> read_only
)
1185 if(field
-> module
!= ctx
-> mod
)
1191 oberon_expr_t
* selector
;
1192 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1193 selector
-> item
.var
= field
;
1194 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1199 #define ISSELECTOR(x) \
1202 || ((x) == UPARROW) \
1205 static oberon_object_t
*
1206 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1209 oberon_object_t
* x
;
1211 name
= oberon_assert_ident(ctx
);
1212 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1216 if(x
-> class == OBERON_CLASS_MODULE
)
1218 oberon_assert_token(ctx
, DOT
);
1219 name
= oberon_assert_ident(ctx
);
1220 /* Наличие объектов в левых модулях всегда проверяется */
1221 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1223 if(x
-> export
== 0)
1225 oberon_error(ctx
, "not exported");
1238 static oberon_expr_t
*
1239 oberon_designator(oberon_context_t
* ctx
)
1242 oberon_object_t
* var
;
1243 oberon_expr_t
* expr
;
1245 var
= oberon_qualident(ctx
, NULL
, 1);
1248 if(var
-> read_only
)
1250 if(var
-> module
!= ctx
-> mod
)
1256 switch(var
-> class)
1258 case OBERON_CLASS_CONST
:
1260 expr
= (oberon_expr_t
*) var
-> value
;
1262 case OBERON_CLASS_VAR
:
1263 case OBERON_CLASS_VAR_PARAM
:
1264 case OBERON_CLASS_PARAM
:
1265 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1267 case OBERON_CLASS_PROC
:
1268 expr
= oberon_new_item(MODE_VAR
, var
-> type
, 1);
1271 oberon_error(ctx
, "invalid designator");
1274 expr
-> item
.var
= var
;
1277 while(brk
== false && ISSELECTOR(ctx
-> token
))
1279 switch(ctx
-> token
)
1282 oberon_assert_token(ctx
, DOT
);
1283 name
= oberon_assert_ident(ctx
);
1284 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1287 oberon_assert_token(ctx
, LBRACE
);
1288 int num_indexes
= 0;
1289 oberon_expr_t
* indexes
= NULL
;
1290 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1291 oberon_assert_token(ctx
, RBRACE
);
1293 for(int i
= 0; i
< num_indexes
; i
++)
1295 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1296 indexes
= indexes
-> next
;
1300 oberon_assert_token(ctx
, UPARROW
);
1301 expr
= oberno_make_dereferencing(ctx
, expr
);
1304 if(expr
-> result
-> class == OBERON_TYPE_PROCEDURE
)
1309 oberon_assert_token(ctx
, LPAREN
);
1310 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1311 if(objtype
-> class != OBERON_CLASS_TYPE
)
1313 oberon_error(ctx
, "must be type");
1315 oberon_assert_token(ctx
, RPAREN
);
1316 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1319 oberon_error(ctx
, "oberon_designator: wat");
1326 static oberon_expr_t
*
1327 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1329 assert(expr
-> is_item
== 1);
1331 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1332 if(ctx
-> token
== LPAREN
)
1334 oberon_assert_token(ctx
, LPAREN
);
1337 oberon_expr_t
* arguments
= NULL
;
1339 if(ISEXPR(ctx
-> token
))
1341 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1344 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1346 oberon_assert_token(ctx
, RPAREN
);
1353 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1355 assert(expr
-> is_item
== 1);
1358 oberon_expr_t
* arguments
= NULL
;
1360 if(ctx
-> token
== LPAREN
)
1362 oberon_assert_token(ctx
, LPAREN
);
1364 if(ISEXPR(ctx
-> token
))
1366 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1369 oberon_assert_token(ctx
, RPAREN
);
1372 /* Вызов происходит даже без скобок */
1373 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1376 static oberon_type_t
*
1377 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1379 if(i
>= -128 && i
<= 127)
1381 return ctx
-> byte_type
;
1383 else if(i
>= -32768 && i
<= 32767)
1385 return ctx
-> shortint_type
;
1387 else if(i
>= -2147483648 && i
<= 2147483647)
1389 return ctx
-> int_type
;
1393 return ctx
-> longint_type
;
1397 static oberon_expr_t
*
1398 oberon_factor(oberon_context_t
* ctx
)
1400 oberon_expr_t
* expr
;
1401 oberon_type_t
* result
;
1403 switch(ctx
-> token
)
1406 expr
= oberon_designator(ctx
);
1407 expr
= oberon_opt_func_parens(ctx
, expr
);
1410 result
= oberon_get_type_of_int_value(ctx
, ctx
-> integer
);
1411 expr
= oberon_new_item(MODE_INTEGER
, result
, 1);
1412 expr
-> item
.integer
= ctx
-> integer
;
1413 oberon_assert_token(ctx
, INTEGER
);
1416 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1417 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1418 expr
-> item
.real
= ctx
-> real
;
1419 oberon_assert_token(ctx
, REAL
);
1422 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, 1);
1423 expr
-> item
.boolean
= true;
1424 oberon_assert_token(ctx
, TRUE
);
1427 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, 1);
1428 expr
-> item
.boolean
= false;
1429 oberon_assert_token(ctx
, FALSE
);
1432 oberon_assert_token(ctx
, LPAREN
);
1433 expr
= oberon_expr(ctx
);
1434 oberon_assert_token(ctx
, RPAREN
);
1437 oberon_assert_token(ctx
, NOT
);
1438 expr
= oberon_factor(ctx
);
1439 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1442 oberon_assert_token(ctx
, NIL
);
1443 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, 1);
1446 oberon_error(ctx
, "invalid expression");
1452 #define ITMAKESBOOLEAN(x) \
1453 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1455 #define ITUSEONLYINTEGER(x) \
1456 ((x) >= LESS && (x) <= GEQ)
1458 #define ITUSEONLYBOOLEAN(x) \
1459 (((x) == OR) || ((x) == AND))
1462 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1464 oberon_expr_t
* expr
= *e
;
1465 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1467 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1469 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1473 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1476 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1478 oberon_error(ctx
, "required numeric type");
1482 static oberon_expr_t
*
1483 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1485 oberon_expr_t
* expr
;
1486 oberon_type_t
* result
;
1488 if(ITMAKESBOOLEAN(token
))
1490 if(ITUSEONLYINTEGER(token
))
1492 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1493 || b
-> result
-> class == OBERON_TYPE_INTEGER
1494 || a
-> result
-> class == OBERON_TYPE_REAL
1495 || b
-> result
-> class == OBERON_TYPE_REAL
)
1497 oberon_error(ctx
, "used only with numeric types");
1500 else if(ITUSEONLYBOOLEAN(token
))
1502 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1503 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1505 oberon_error(ctx
, "used only with boolean type");
1509 oberon_autocast_binary_op(ctx
, &a
, &b
);
1510 result
= ctx
-> bool_type
;
1514 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1516 else if(token
== NEQ
)
1518 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1520 else if(token
== LESS
)
1522 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1524 else if(token
== LEQ
)
1526 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1528 else if(token
== GREAT
)
1530 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1532 else if(token
== GEQ
)
1534 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1536 else if(token
== OR
)
1538 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1540 else if(token
== AND
)
1542 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1546 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1549 else if(token
== SLASH
)
1551 oberon_autocast_to_real(ctx
, &a
);
1552 oberon_autocast_to_real(ctx
, &b
);
1553 oberon_autocast_binary_op(ctx
, &a
, &b
);
1554 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1556 else if(token
== DIV
)
1558 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1559 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1561 oberon_error(ctx
, "operator DIV requires integer type");
1564 oberon_autocast_binary_op(ctx
, &a
, &b
);
1565 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1569 oberon_autocast_binary_op(ctx
, &a
, &b
);
1573 expr
= oberon_new_operator(OP_ADD
, a
-> result
, a
, b
);
1575 else if(token
== MINUS
)
1577 expr
= oberon_new_operator(OP_SUB
, a
-> result
, a
, b
);
1579 else if(token
== STAR
)
1581 expr
= oberon_new_operator(OP_MUL
, a
-> result
, a
, b
);
1583 else if(token
== MOD
)
1585 expr
= oberon_new_operator(OP_MOD
, a
-> result
, a
, b
);
1589 oberon_error(ctx
, "oberon_make_bin_op: bin wat");
1596 #define ISMULOP(x) \
1597 ((x) >= STAR && (x) <= AND)
1599 static oberon_expr_t
*
1600 oberon_term_expr(oberon_context_t
* ctx
)
1602 oberon_expr_t
* expr
;
1604 expr
= oberon_factor(ctx
);
1605 while(ISMULOP(ctx
-> token
))
1607 int token
= ctx
-> token
;
1608 oberon_read_token(ctx
);
1610 oberon_expr_t
* inter
= oberon_factor(ctx
);
1611 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1617 #define ISADDOP(x) \
1618 ((x) >= PLUS && (x) <= OR)
1620 static oberon_expr_t
*
1621 oberon_simple_expr(oberon_context_t
* ctx
)
1623 oberon_expr_t
* expr
;
1626 if(ctx
-> token
== PLUS
)
1629 oberon_assert_token(ctx
, PLUS
);
1631 else if(ctx
-> token
== MINUS
)
1634 oberon_assert_token(ctx
, MINUS
);
1637 expr
= oberon_term_expr(ctx
);
1641 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
1644 while(ISADDOP(ctx
-> token
))
1646 int token
= ctx
-> token
;
1647 oberon_read_token(ctx
);
1649 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
1650 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1656 #define ISRELATION(x) \
1657 ((x) >= EQUAL && (x) <= IS)
1659 static oberon_expr_t
*
1660 oberon_expr(oberon_context_t
* ctx
)
1662 oberon_expr_t
* expr
;
1664 expr
= oberon_simple_expr(ctx
);
1665 while(ISRELATION(ctx
-> token
))
1667 int token
= ctx
-> token
;
1668 oberon_read_token(ctx
);
1670 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
1671 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
1677 static oberon_item_t
*
1678 oberon_const_expr(oberon_context_t
* ctx
)
1680 oberon_expr_t
* expr
;
1681 expr
= oberon_expr(ctx
);
1683 if(expr
-> is_item
== 0)
1685 oberon_error(ctx
, "const expression are required");
1688 return (oberon_item_t
*) expr
;
1691 // =======================================================================
1693 // =======================================================================
1695 static void oberon_decl_seq(oberon_context_t
* ctx
);
1696 static void oberon_statement_seq(oberon_context_t
* ctx
);
1697 static void oberon_initialize_decl(oberon_context_t
* ctx
);
1700 oberon_expect_token(oberon_context_t
* ctx
, int token
)
1702 if(ctx
-> token
!= token
)
1704 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
1709 oberon_assert_token(oberon_context_t
* ctx
, int token
)
1711 oberon_expect_token(ctx
, token
);
1712 oberon_read_token(ctx
);
1716 oberon_assert_ident(oberon_context_t
* ctx
)
1718 oberon_expect_token(ctx
, IDENT
);
1719 char * ident
= ctx
-> string
;
1720 oberon_read_token(ctx
);
1725 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
1727 switch(ctx
-> token
)
1730 oberon_assert_token(ctx
, STAR
);
1735 oberon_assert_token(ctx
, MINUS
);
1746 static oberon_object_t
*
1747 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
1752 oberon_object_t
* x
;
1754 name
= oberon_assert_ident(ctx
);
1755 oberon_def(ctx
, &export
, &read_only
);
1757 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
1762 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
1765 *list
= oberon_ident_def(ctx
, class, check_upscope
);
1766 while(ctx
-> token
== COMMA
)
1768 oberon_assert_token(ctx
, COMMA
);
1769 oberon_ident_def(ctx
, class, check_upscope
);
1775 oberon_var_decl(oberon_context_t
* ctx
)
1778 oberon_object_t
* list
;
1779 oberon_type_t
* type
;
1780 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1782 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
1783 oberon_assert_token(ctx
, COLON
);
1784 oberon_type(ctx
, &type
);
1786 oberon_object_t
* var
= list
;
1787 for(int i
= 0; i
< num
; i
++)
1794 static oberon_object_t
*
1795 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
1797 int class = OBERON_CLASS_PARAM
;
1798 if(ctx
-> token
== VAR
)
1800 oberon_read_token(ctx
);
1801 class = OBERON_CLASS_VAR_PARAM
;
1805 oberon_object_t
* list
;
1806 oberon_ident_list(ctx
, class, false, &num
, &list
);
1808 oberon_assert_token(ctx
, COLON
);
1810 oberon_type_t
* type
;
1811 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1812 oberon_type(ctx
, &type
);
1814 oberon_object_t
* param
= list
;
1815 for(int i
= 0; i
< num
; i
++)
1817 param
-> type
= type
;
1818 param
= param
-> next
;
1825 #define ISFPSECTION \
1826 ((ctx -> token == VAR) || (ctx -> token == IDENT))
1829 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
1831 oberon_assert_token(ctx
, LPAREN
);
1835 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
1836 while(ctx
-> token
== SEMICOLON
)
1838 oberon_assert_token(ctx
, SEMICOLON
);
1839 oberon_fp_section(ctx
, &signature
-> num_decl
);
1843 oberon_assert_token(ctx
, RPAREN
);
1845 if(ctx
-> token
== COLON
)
1847 oberon_assert_token(ctx
, COLON
);
1849 oberon_object_t
* typeobj
;
1850 typeobj
= oberon_qualident(ctx
, NULL
, 1);
1851 if(typeobj
-> class != OBERON_CLASS_TYPE
)
1853 oberon_error(ctx
, "function result is not type");
1855 signature
-> base
= typeobj
-> type
;
1860 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
1862 oberon_type_t
* signature
;
1864 signature
-> class = OBERON_TYPE_PROCEDURE
;
1865 signature
-> num_decl
= 0;
1866 signature
-> base
= ctx
-> void_type
;
1867 signature
-> decl
= NULL
;
1869 if(ctx
-> token
== LPAREN
)
1871 oberon_formal_pars(ctx
, signature
);
1876 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1878 if(a
-> num_decl
!= b
-> num_decl
)
1880 oberon_error(ctx
, "number parameters not matched");
1883 int num_param
= a
-> num_decl
;
1884 oberon_object_t
* param_a
= a
-> decl
;
1885 oberon_object_t
* param_b
= b
-> decl
;
1886 for(int i
= 0; i
< num_param
; i
++)
1888 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
1890 oberon_error(ctx
, "param %i name not matched", i
+ 1);
1893 if(param_a
-> type
!= param_b
-> type
)
1895 oberon_error(ctx
, "param %i type not matched", i
+ 1);
1898 param_a
= param_a
-> next
;
1899 param_b
= param_b
-> next
;
1904 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1906 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
1907 oberon_type_t
* result_type
= proc
-> type
-> base
;
1909 if(result_type
-> class == OBERON_TYPE_VOID
)
1913 oberon_error(ctx
, "procedure has no result type");
1920 oberon_error(ctx
, "procedure requires expression on result");
1923 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
1926 proc
-> has_return
= 1;
1928 oberon_generate_return(ctx
, expr
);
1932 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
1934 oberon_assert_token(ctx
, SEMICOLON
);
1936 ctx
-> decl
= proc
-> scope
;
1938 oberon_decl_seq(ctx
);
1940 oberon_generate_begin_proc(ctx
, proc
);
1942 if(ctx
-> token
== BEGIN
)
1944 oberon_assert_token(ctx
, BEGIN
);
1945 oberon_statement_seq(ctx
);
1948 oberon_assert_token(ctx
, END
);
1949 char * name
= oberon_assert_ident(ctx
);
1950 if(strcmp(name
, proc
-> name
) != 0)
1952 oberon_error(ctx
, "procedure name not matched");
1955 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
1956 && proc
-> has_return
== 0)
1958 oberon_make_return(ctx
, NULL
);
1961 if(proc
-> has_return
== 0)
1963 oberon_error(ctx
, "procedure requires return");
1966 oberon_generate_end_proc(ctx
);
1967 oberon_close_scope(ctx
-> decl
);
1971 oberon_proc_decl(oberon_context_t
* ctx
)
1973 oberon_assert_token(ctx
, PROCEDURE
);
1976 if(ctx
-> token
== UPARROW
)
1978 oberon_assert_token(ctx
, UPARROW
);
1985 name
= oberon_assert_ident(ctx
);
1986 oberon_def(ctx
, &export
, &read_only
);
1988 oberon_scope_t
* proc_scope
;
1989 proc_scope
= oberon_open_scope(ctx
);
1990 ctx
-> decl
-> local
= 1;
1992 oberon_type_t
* signature
;
1993 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
1994 oberon_opt_formal_pars(ctx
, &signature
);
1996 oberon_initialize_decl(ctx
);
1997 oberon_generator_init_type(ctx
, signature
);
1998 oberon_close_scope(ctx
-> decl
);
2000 oberon_object_t
* proc
;
2001 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2004 if(proc
-> class != OBERON_CLASS_PROC
)
2006 oberon_error(ctx
, "mult definition");
2013 oberon_error(ctx
, "mult procedure definition");
2017 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2019 oberon_error(ctx
, "export type not matched");
2022 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2026 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2027 proc
-> type
= signature
;
2028 proc
-> scope
= proc_scope
;
2029 oberon_generator_init_proc(ctx
, proc
);
2032 proc
-> scope
-> parent
= proc
;
2037 oberon_proc_decl_body(ctx
, proc
);
2042 oberon_const_decl(oberon_context_t
* ctx
)
2044 oberon_item_t
* value
;
2045 oberon_object_t
* constant
;
2047 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2048 oberon_assert_token(ctx
, EQUAL
);
2049 value
= oberon_const_expr(ctx
);
2050 constant
-> value
= value
;
2054 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2056 if(size
-> is_item
== 0)
2058 oberon_error(ctx
, "requires constant");
2061 if(size
-> item
.mode
!= MODE_INTEGER
)
2063 oberon_error(ctx
, "requires integer constant");
2066 oberon_type_t
* arr
;
2068 arr
-> class = OBERON_TYPE_ARRAY
;
2069 arr
-> size
= size
-> item
.integer
;
2074 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2077 oberon_object_t
* to
;
2079 to
= oberon_qualident(ctx
, &name
, 0);
2081 //name = oberon_assert_ident(ctx);
2082 //to = oberon_find_object(ctx -> decl, name, 0);
2086 if(to
-> class != OBERON_CLASS_TYPE
)
2088 oberon_error(ctx
, "not a type");
2093 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2094 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2100 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2103 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2107 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2115 oberon_type_t
* dim
;
2116 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2118 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2120 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2124 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2126 type
-> class = OBERON_TYPE_ARRAY
;
2128 type
-> base
= base
;
2132 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2134 if(ctx
-> token
== IDENT
)
2137 oberon_object_t
* list
;
2138 oberon_type_t
* type
;
2139 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2141 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2142 oberon_assert_token(ctx
, COLON
);
2144 oberon_scope_t
* current
= ctx
-> decl
;
2145 ctx
-> decl
= modscope
;
2146 oberon_type(ctx
, &type
);
2147 ctx
-> decl
= current
;
2149 oberon_object_t
* field
= list
;
2150 for(int i
= 0; i
< num
; i
++)
2152 field
-> type
= type
;
2153 field
= field
-> next
;
2156 rec
-> num_decl
+= num
;
2161 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2163 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2164 oberon_scope_t
* oldscope
= ctx
-> decl
;
2165 ctx
-> decl
= modscope
;
2167 if(ctx
-> token
== LPAREN
)
2169 oberon_assert_token(ctx
, LPAREN
);
2171 oberon_object_t
* typeobj
;
2172 typeobj
= oberon_qualident(ctx
, NULL
, true);
2174 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2176 oberon_error(ctx
, "base must be type");
2179 if(typeobj
-> type
-> class != OBERON_TYPE_RECORD
)
2181 oberon_error(ctx
, "base must be record type");
2184 rec
-> base
= typeobj
-> type
;
2185 ctx
-> decl
= rec
-> base
-> scope
;
2187 oberon_assert_token(ctx
, RPAREN
);
2194 oberon_scope_t
* this_scope
;
2195 this_scope
= oberon_open_scope(ctx
);
2196 this_scope
-> local
= true;
2197 this_scope
-> parent
= NULL
;
2198 this_scope
-> parent_type
= rec
;
2200 oberon_field_list(ctx
, rec
, modscope
);
2201 while(ctx
-> token
== SEMICOLON
)
2203 oberon_assert_token(ctx
, SEMICOLON
);
2204 oberon_field_list(ctx
, rec
, modscope
);
2207 rec
-> scope
= this_scope
;
2208 rec
-> decl
= this_scope
-> list
-> next
;
2209 ctx
-> decl
= oldscope
;
2213 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2215 if(ctx
-> token
== IDENT
)
2217 oberon_qualident_type(ctx
, type
);
2219 else if(ctx
-> token
== ARRAY
)
2221 oberon_assert_token(ctx
, ARRAY
);
2224 oberon_expr_t
* sizes
;
2226 if(ISEXPR(ctx
-> token
))
2228 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2231 oberon_assert_token(ctx
, OF
);
2233 oberon_type_t
* base
;
2234 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2235 oberon_type(ctx
, &base
);
2239 oberon_make_open_array(ctx
, base
, *type
);
2243 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2246 else if(ctx
-> token
== RECORD
)
2248 oberon_type_t
* rec
;
2250 rec
-> class = OBERON_TYPE_RECORD
;
2251 rec
-> module
= ctx
-> mod
;
2253 oberon_assert_token(ctx
, RECORD
);
2254 oberon_type_record_body(ctx
, rec
);
2255 oberon_assert_token(ctx
, END
);
2259 else if(ctx
-> token
== POINTER
)
2261 oberon_assert_token(ctx
, POINTER
);
2262 oberon_assert_token(ctx
, TO
);
2264 oberon_type_t
* base
;
2265 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2266 oberon_type(ctx
, &base
);
2268 oberon_type_t
* ptr
;
2270 ptr
-> class = OBERON_TYPE_POINTER
;
2273 else if(ctx
-> token
== PROCEDURE
)
2275 oberon_open_scope(ctx
);
2276 oberon_assert_token(ctx
, PROCEDURE
);
2277 oberon_opt_formal_pars(ctx
, type
);
2278 oberon_close_scope(ctx
-> decl
);
2282 oberon_error(ctx
, "invalid type declaration");
2287 oberon_type_decl(oberon_context_t
* ctx
)
2290 oberon_object_t
* newtype
;
2291 oberon_type_t
* type
;
2295 name
= oberon_assert_ident(ctx
);
2296 oberon_def(ctx
, &export
, &read_only
);
2298 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2301 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2302 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2303 assert(newtype
-> type
);
2307 if(newtype
-> class != OBERON_CLASS_TYPE
)
2309 oberon_error(ctx
, "mult definition");
2312 if(newtype
-> linked
)
2314 oberon_error(ctx
, "mult definition - already linked");
2317 newtype
-> export
= export
;
2318 newtype
-> read_only
= read_only
;
2321 oberon_assert_token(ctx
, EQUAL
);
2323 type
= newtype
-> type
;
2324 oberon_type(ctx
, &type
);
2326 if(type
-> class == OBERON_TYPE_VOID
)
2328 oberon_error(ctx
, "recursive alias declaration");
2331 newtype
-> type
= type
;
2332 newtype
-> linked
= 1;
2335 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2336 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2339 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2341 if(type
-> class != OBERON_TYPE_POINTER
2342 && type
-> class != OBERON_TYPE_ARRAY
)
2347 if(type
-> recursive
)
2349 oberon_error(ctx
, "recursive pointer declaration");
2352 if(type
-> class == OBERON_TYPE_POINTER
2353 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2355 oberon_error(ctx
, "attempt to make pointer to pointer");
2358 type
-> recursive
= 1;
2360 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2362 type
-> recursive
= 0;
2366 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2368 if(type
-> class != OBERON_TYPE_RECORD
)
2373 if(type
-> recursive
)
2375 oberon_error(ctx
, "recursive record declaration");
2378 type
-> recursive
= 1;
2380 int num_fields
= type
-> num_decl
;
2381 oberon_object_t
* field
= type
-> decl
;
2382 for(int i
= 0; i
< num_fields
; i
++)
2384 oberon_prevent_recursive_object(ctx
, field
);
2385 field
= field
-> next
;
2388 type
-> recursive
= 0;
2391 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2393 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2398 if(type
-> recursive
)
2400 oberon_error(ctx
, "recursive procedure declaration");
2403 type
-> recursive
= 1;
2405 int num_fields
= type
-> num_decl
;
2406 oberon_object_t
* field
= type
-> decl
;
2407 for(int i
= 0; i
< num_fields
; i
++)
2409 oberon_prevent_recursive_object(ctx
, field
);
2410 field
= field
-> next
;
2413 type
-> recursive
= 0;
2417 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2419 if(type
-> class != OBERON_TYPE_ARRAY
)
2424 if(type
-> recursive
)
2426 oberon_error(ctx
, "recursive array declaration");
2429 type
-> recursive
= 1;
2431 oberon_prevent_recursive_type(ctx
, type
-> base
);
2433 type
-> recursive
= 0;
2437 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2439 if(type
-> class == OBERON_TYPE_POINTER
)
2441 oberon_prevent_recursive_pointer(ctx
, type
);
2443 else if(type
-> class == OBERON_TYPE_RECORD
)
2445 oberon_prevent_recursive_record(ctx
, type
);
2447 else if(type
-> class == OBERON_TYPE_ARRAY
)
2449 oberon_prevent_recursive_array(ctx
, type
);
2451 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2453 oberon_prevent_recursive_procedure(ctx
, type
);
2458 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2462 case OBERON_CLASS_VAR
:
2463 case OBERON_CLASS_TYPE
:
2464 case OBERON_CLASS_PARAM
:
2465 case OBERON_CLASS_VAR_PARAM
:
2466 case OBERON_CLASS_FIELD
:
2467 oberon_prevent_recursive_type(ctx
, x
-> type
);
2469 case OBERON_CLASS_CONST
:
2470 case OBERON_CLASS_PROC
:
2471 case OBERON_CLASS_MODULE
:
2474 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2480 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2482 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2486 oberon_prevent_recursive_object(ctx
, x
);
2491 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2492 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2495 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2497 if(type
-> class != OBERON_TYPE_RECORD
)
2502 int num_fields
= type
-> num_decl
;
2503 oberon_object_t
* field
= type
-> decl
;
2504 for(int i
= 0; i
< num_fields
; i
++)
2506 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2508 oberon_initialize_type(ctx
, field
-> type
);
2511 oberon_initialize_object(ctx
, field
);
2512 field
= field
-> next
;
2515 oberon_generator_init_record(ctx
, type
);
2519 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2521 if(type
-> class == OBERON_TYPE_VOID
)
2523 oberon_error(ctx
, "undeclarated type");
2526 if(type
-> initialized
)
2531 type
-> initialized
= 1;
2533 if(type
-> class == OBERON_TYPE_POINTER
)
2535 oberon_initialize_type(ctx
, type
-> base
);
2536 oberon_generator_init_type(ctx
, type
);
2538 else if(type
-> class == OBERON_TYPE_ARRAY
)
2540 if(type
-> size
!= 0)
2542 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
2544 if(type
-> base
-> size
== 0)
2546 oberon_error(ctx
, "open array not allowed as array element");
2551 oberon_initialize_type(ctx
, type
-> base
);
2552 oberon_generator_init_type(ctx
, type
);
2554 else if(type
-> class == OBERON_TYPE_RECORD
)
2556 oberon_generator_init_type(ctx
, type
);
2557 oberon_initialize_record_fields(ctx
, type
);
2559 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2561 int num_fields
= type
-> num_decl
;
2562 oberon_object_t
* field
= type
-> decl
;
2563 for(int i
= 0; i
< num_fields
; i
++)
2565 oberon_initialize_object(ctx
, field
);
2566 field
= field
-> next
;
2569 oberon_generator_init_type(ctx
, type
);
2573 oberon_generator_init_type(ctx
, type
);
2578 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2580 if(x
-> initialized
)
2585 x
-> initialized
= 1;
2589 case OBERON_CLASS_TYPE
:
2590 oberon_initialize_type(ctx
, x
-> type
);
2592 case OBERON_CLASS_VAR
:
2593 case OBERON_CLASS_FIELD
:
2594 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
2596 if(x
-> type
-> size
== 0)
2598 oberon_error(ctx
, "open array not allowed as variable or field");
2601 oberon_initialize_type(ctx
, x
-> type
);
2602 oberon_generator_init_var(ctx
, x
);
2604 case OBERON_CLASS_PARAM
:
2605 case OBERON_CLASS_VAR_PARAM
:
2606 oberon_initialize_type(ctx
, x
-> type
);
2607 oberon_generator_init_var(ctx
, x
);
2609 case OBERON_CLASS_CONST
:
2610 case OBERON_CLASS_PROC
:
2611 case OBERON_CLASS_MODULE
:
2614 oberon_error(ctx
, "oberon_initialize_object: wat");
2620 oberon_initialize_decl(oberon_context_t
* ctx
)
2622 oberon_object_t
* x
= ctx
-> decl
-> list
;
2626 oberon_initialize_object(ctx
, x
-> next
);
2632 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
2634 oberon_object_t
* x
= ctx
-> decl
-> list
;
2638 if(x
-> next
-> class == OBERON_CLASS_PROC
)
2640 if(x
-> next
-> linked
== 0)
2642 oberon_error(ctx
, "unresolved forward declaration");
2650 oberon_decl_seq(oberon_context_t
* ctx
)
2652 if(ctx
-> token
== CONST
)
2654 oberon_assert_token(ctx
, CONST
);
2655 while(ctx
-> token
== IDENT
)
2657 oberon_const_decl(ctx
);
2658 oberon_assert_token(ctx
, SEMICOLON
);
2662 if(ctx
-> token
== TYPE
)
2664 oberon_assert_token(ctx
, TYPE
);
2665 while(ctx
-> token
== IDENT
)
2667 oberon_type_decl(ctx
);
2668 oberon_assert_token(ctx
, SEMICOLON
);
2672 if(ctx
-> token
== VAR
)
2674 oberon_assert_token(ctx
, VAR
);
2675 while(ctx
-> token
== IDENT
)
2677 oberon_var_decl(ctx
);
2678 oberon_assert_token(ctx
, SEMICOLON
);
2682 oberon_prevent_recursive_decl(ctx
);
2683 oberon_initialize_decl(ctx
);
2685 while(ctx
-> token
== PROCEDURE
)
2687 oberon_proc_decl(ctx
);
2688 oberon_assert_token(ctx
, SEMICOLON
);
2691 oberon_prevent_undeclarated_procedures(ctx
);
2695 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
2697 if(dst
-> read_only
)
2699 oberon_error(ctx
, "read-only destination");
2702 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
2703 oberon_generate_assign(ctx
, src
, dst
);
2707 oberon_statement(oberon_context_t
* ctx
)
2709 oberon_expr_t
* item1
;
2710 oberon_expr_t
* item2
;
2712 if(ctx
-> token
== IDENT
)
2714 item1
= oberon_designator(ctx
);
2715 if(ctx
-> token
== ASSIGN
)
2717 oberon_assert_token(ctx
, ASSIGN
);
2718 item2
= oberon_expr(ctx
);
2719 oberon_assign(ctx
, item2
, item1
);
2723 oberon_opt_proc_parens(ctx
, item1
);
2726 else if(ctx
-> token
== RETURN
)
2728 oberon_assert_token(ctx
, RETURN
);
2729 if(ISEXPR(ctx
-> token
))
2731 oberon_expr_t
* expr
;
2732 expr
= oberon_expr(ctx
);
2733 oberon_make_return(ctx
, expr
);
2737 oberon_make_return(ctx
, NULL
);
2743 oberon_statement_seq(oberon_context_t
* ctx
)
2745 oberon_statement(ctx
);
2746 while(ctx
-> token
== SEMICOLON
)
2748 oberon_assert_token(ctx
, SEMICOLON
);
2749 oberon_statement(ctx
);
2754 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
2756 oberon_module_t
* m
= ctx
-> module_list
;
2757 while(m
&& strcmp(m
-> name
, name
) != 0)
2765 code
= ctx
-> import_module(name
);
2768 oberon_error(ctx
, "no such module");
2771 m
= oberon_compile_module(ctx
, code
);
2777 oberon_error(ctx
, "cyclic module import");
2780 oberon_object_t
* ident
;
2781 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
2782 ident
-> module
= m
;
2786 oberon_import_decl(oberon_context_t
* ctx
)
2791 alias
= name
= oberon_assert_ident(ctx
);
2792 if(ctx
-> token
== ASSIGN
)
2794 oberon_assert_token(ctx
, ASSIGN
);
2795 name
= oberon_assert_ident(ctx
);
2798 oberon_import_module(ctx
, alias
, name
);
2802 oberon_import_list(oberon_context_t
* ctx
)
2804 oberon_assert_token(ctx
, IMPORT
);
2806 oberon_import_decl(ctx
);
2807 while(ctx
-> token
== COMMA
)
2809 oberon_assert_token(ctx
, COMMA
);
2810 oberon_import_decl(ctx
);
2813 oberon_assert_token(ctx
, SEMICOLON
);
2817 oberon_parse_module(oberon_context_t
* ctx
)
2821 oberon_read_token(ctx
);
2823 oberon_assert_token(ctx
, MODULE
);
2824 name1
= oberon_assert_ident(ctx
);
2825 oberon_assert_token(ctx
, SEMICOLON
);
2826 ctx
-> mod
-> name
= name1
;
2828 oberon_generator_init_module(ctx
, ctx
-> mod
);
2830 if(ctx
-> token
== IMPORT
)
2832 oberon_import_list(ctx
);
2835 oberon_decl_seq(ctx
);
2837 oberon_generate_begin_module(ctx
);
2838 if(ctx
-> token
== BEGIN
)
2840 oberon_assert_token(ctx
, BEGIN
);
2841 oberon_statement_seq(ctx
);
2843 oberon_generate_end_module(ctx
);
2845 oberon_assert_token(ctx
, END
);
2846 name2
= oberon_assert_ident(ctx
);
2847 oberon_assert_token(ctx
, DOT
);
2849 if(strcmp(name1
, name2
) != 0)
2851 oberon_error(ctx
, "module name not matched");
2854 oberon_generator_fini_module(ctx
-> mod
);
2857 // =======================================================================
2859 // =======================================================================
2862 register_default_types(oberon_context_t
* ctx
)
2864 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2865 oberon_generator_init_type(ctx
, ctx
-> void_type
);
2867 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
2868 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
2869 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
2871 ctx
-> bool_type
= oberon_new_type_boolean();
2872 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
2874 ctx
-> byte_type
= oberon_new_type_integer(1);
2875 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
2877 ctx
-> shortint_type
= oberon_new_type_integer(2);
2878 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
2880 ctx
-> int_type
= oberon_new_type_integer(4);
2881 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
2883 ctx
-> longint_type
= oberon_new_type_integer(8);
2884 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
2886 ctx
-> real_type
= oberon_new_type_real(4);
2887 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
2889 ctx
-> longreal_type
= oberon_new_type_real(8);
2890 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
2894 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
2896 oberon_object_t
* proc
;
2897 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
2898 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
2899 proc
-> type
-> sysproc
= true;
2900 proc
-> type
-> genfunc
= f
;
2901 proc
-> type
-> genproc
= p
;
2904 static oberon_expr_t
*
2905 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
2909 oberon_error(ctx
, "too few arguments");
2914 oberon_error(ctx
, "too mach arguments");
2917 oberon_expr_t
* arg
;
2920 oberon_type_t
* result_type
;
2921 result_type
= arg
-> result
;
2923 if(result_type
-> class != OBERON_TYPE_INTEGER
)
2925 oberon_error(ctx
, "ABS accepts only integers");
2929 oberon_expr_t
* expr
;
2930 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
2935 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
2939 oberon_error(ctx
, "too few arguments");
2942 oberon_expr_t
* dst
;
2945 oberon_type_t
* type
;
2946 type
= dst
-> result
;
2948 if(type
-> class != OBERON_TYPE_POINTER
)
2950 oberon_error(ctx
, "not a pointer");
2953 type
= type
-> base
;
2955 oberon_expr_t
* src
;
2956 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
2957 src
-> item
.num_args
= 0;
2958 src
-> item
.args
= NULL
;
2961 if(type
-> class == OBERON_TYPE_ARRAY
)
2963 if(type
-> size
== 0)
2965 oberon_type_t
* x
= type
;
2966 while(x
-> class == OBERON_TYPE_ARRAY
)
2976 if(num_args
< max_args
)
2978 oberon_error(ctx
, "too few arguments");
2981 if(num_args
> max_args
)
2983 oberon_error(ctx
, "too mach arguments");
2986 int num_sizes
= max_args
- 1;
2987 oberon_expr_t
* size_list
= list_args
-> next
;
2989 oberon_expr_t
* arg
= size_list
;
2990 for(int i
= 0; i
< max_args
- 1; i
++)
2992 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
2994 oberon_error(ctx
, "size must be integer");
2999 src
-> item
.num_args
= num_sizes
;
3000 src
-> item
.args
= size_list
;
3002 else if(type
-> class != OBERON_TYPE_RECORD
)
3004 oberon_error(ctx
, "oberon_make_new_call: wat");
3007 if(num_args
> max_args
)
3009 oberon_error(ctx
, "too mach arguments");
3012 oberon_assign(ctx
, src
, dst
);
3016 oberon_create_context(ModuleImportCallback import_module
)
3018 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3020 oberon_scope_t
* world_scope
;
3021 world_scope
= oberon_open_scope(ctx
);
3022 ctx
-> world_scope
= world_scope
;
3024 ctx
-> import_module
= import_module
;
3026 oberon_generator_init_context(ctx
);
3028 register_default_types(ctx
);
3029 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3030 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3036 oberon_destroy_context(oberon_context_t
* ctx
)
3038 oberon_generator_destroy_context(ctx
);
3043 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3045 const char * code
= ctx
-> code
;
3046 int code_index
= ctx
-> code_index
;
3048 int token
= ctx
-> token
;
3049 char * string
= ctx
-> string
;
3050 int integer
= ctx
-> integer
;
3051 int real
= ctx
-> real
;
3052 bool longmode
= ctx
-> longmode
;
3053 oberon_scope_t
* decl
= ctx
-> decl
;
3054 oberon_module_t
* mod
= ctx
-> mod
;
3056 oberon_scope_t
* module_scope
;
3057 module_scope
= oberon_open_scope(ctx
);
3059 oberon_module_t
* module
;
3060 module
= calloc(1, sizeof *module
);
3061 module
-> decl
= module_scope
;
3062 module
-> next
= ctx
-> module_list
;
3064 ctx
-> mod
= module
;
3065 ctx
-> module_list
= module
;
3067 oberon_init_scaner(ctx
, newcode
);
3068 oberon_parse_module(ctx
);
3070 module
-> ready
= 1;
3073 ctx
-> code_index
= code_index
;
3075 ctx
-> token
= token
;
3076 ctx
-> string
= string
;
3077 ctx
-> integer
= integer
;
3079 ctx
-> longmode
= longmode
;