10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
85 // =======================================================================
87 // =======================================================================
90 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
94 fprintf(stderr
, "error: ");
95 vfprintf(stderr
, fmt
, ptr
);
96 fprintf(stderr
, "\n");
97 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
98 fprintf(stderr
, " c = %c\n", ctx
-> c
);
99 fprintf(stderr
, " token = %i\n", ctx
-> token
);
104 static oberon_type_t
*
105 oberon_new_type_ptr(int class)
107 oberon_type_t
* x
= malloc(sizeof *x
);
108 memset(x
, 0, sizeof *x
);
113 static oberon_type_t
*
114 oberon_new_type_integer(int size
)
117 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
122 static oberon_type_t
*
123 oberon_new_type_boolean()
126 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
130 static oberon_type_t
*
131 oberon_new_type_real(int size
)
134 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
139 static oberon_type_t
*
140 oberon_new_type_char(int size
)
143 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
148 static oberon_type_t
*
149 oberon_new_type_string(int size
)
152 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
157 static oberon_type_t
*
158 oberon_new_type_set(int size
)
161 x
= oberon_new_type_ptr(OBERON_TYPE_SET
);
166 // =======================================================================
168 // =======================================================================
170 static oberon_scope_t
*
171 oberon_open_scope(oberon_context_t
* ctx
)
173 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
174 oberon_object_t
* list
= calloc(1, sizeof *list
);
177 scope
-> list
= list
;
178 scope
-> up
= ctx
-> decl
;
182 scope
-> local
= scope
-> up
-> local
;
183 scope
-> parent
= scope
-> up
-> parent
;
184 scope
-> parent_type
= scope
-> up
-> parent_type
;
185 scope
-> exit_label
= scope
-> up
-> exit_label
;
193 oberon_close_scope(oberon_scope_t
* scope
)
195 oberon_context_t
* ctx
= scope
-> ctx
;
196 ctx
-> decl
= scope
-> up
;
199 static oberon_object_t
*
200 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
202 oberon_object_t
* x
= list
;
203 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
210 static oberon_object_t
*
211 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
213 oberon_object_t
* result
= NULL
;
215 oberon_scope_t
* s
= scope
;
216 while(result
== NULL
&& s
!= NULL
)
218 result
= oberon_find_object_in_list(s
-> list
, name
);
222 if(check_it
&& result
== NULL
)
224 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
230 static oberon_object_t
*
231 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
233 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
234 memset(newvar
, 0, sizeof *newvar
);
235 newvar
-> name
= name
;
236 newvar
-> class = class;
237 newvar
-> export
= export
;
238 newvar
-> read_only
= read_only
;
239 newvar
-> local
= scope
-> local
;
240 newvar
-> parent
= scope
-> parent
;
241 newvar
-> parent_type
= scope
-> parent_type
;
242 newvar
-> module
= scope
-> ctx
-> mod
;
246 static oberon_object_t
*
247 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
251 if(oberon_find_object(scope
-> up
, name
, false))
253 oberon_error(scope
-> ctx
, "already defined");
257 oberon_object_t
* x
= scope
-> list
;
258 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
265 oberon_error(scope
-> ctx
, "already defined");
268 oberon_object_t
* newvar
;
269 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
275 static oberon_object_t
*
276 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
278 oberon_object_t
* id
;
279 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
281 oberon_generator_init_type(scope
-> ctx
, type
);
285 // =======================================================================
287 // =======================================================================
290 oberon_get_char(oberon_context_t
* ctx
)
292 if(ctx
-> code
[ctx
-> code_index
])
294 ctx
-> code_index
+= 1;
295 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
300 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
303 ctx
-> code_index
= 0;
304 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
308 oberon_read_ident(oberon_context_t
* ctx
)
311 int i
= ctx
-> code_index
;
313 int c
= ctx
-> code
[i
];
321 char * ident
= malloc(len
+ 1);
322 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
325 ctx
-> code_index
= i
;
326 ctx
-> c
= ctx
-> code
[i
];
327 ctx
-> string
= ident
;
328 ctx
-> token
= IDENT
;
330 if(strcmp(ident
, "MODULE") == 0)
332 ctx
-> token
= MODULE
;
334 else if(strcmp(ident
, "END") == 0)
338 else if(strcmp(ident
, "VAR") == 0)
342 else if(strcmp(ident
, "BEGIN") == 0)
344 ctx
-> token
= BEGIN
;
346 else if(strcmp(ident
, "TRUE") == 0)
350 else if(strcmp(ident
, "FALSE") == 0)
352 ctx
-> token
= FALSE
;
354 else if(strcmp(ident
, "OR") == 0)
358 else if(strcmp(ident
, "DIV") == 0)
362 else if(strcmp(ident
, "MOD") == 0)
366 else if(strcmp(ident
, "PROCEDURE") == 0)
368 ctx
-> token
= PROCEDURE
;
370 else if(strcmp(ident
, "RETURN") == 0)
372 ctx
-> token
= RETURN
;
374 else if(strcmp(ident
, "CONST") == 0)
376 ctx
-> token
= CONST
;
378 else if(strcmp(ident
, "TYPE") == 0)
382 else if(strcmp(ident
, "ARRAY") == 0)
384 ctx
-> token
= ARRAY
;
386 else if(strcmp(ident
, "OF") == 0)
390 else if(strcmp(ident
, "RECORD") == 0)
392 ctx
-> token
= RECORD
;
394 else if(strcmp(ident
, "POINTER") == 0)
396 ctx
-> token
= POINTER
;
398 else if(strcmp(ident
, "TO") == 0)
402 else if(strcmp(ident
, "NIL") == 0)
406 else if(strcmp(ident
, "IMPORT") == 0)
408 ctx
-> token
= IMPORT
;
410 else if(strcmp(ident
, "IN") == 0)
414 else if(strcmp(ident
, "IS") == 0)
418 else if(strcmp(ident
, "IF") == 0)
422 else if(strcmp(ident
, "THEN") == 0)
426 else if(strcmp(ident
, "ELSE") == 0)
430 else if(strcmp(ident
, "ELSIF") == 0)
432 ctx
-> token
= ELSIF
;
434 else if(strcmp(ident
, "WHILE") == 0)
436 ctx
-> token
= WHILE
;
438 else if(strcmp(ident
, "DO") == 0)
442 else if(strcmp(ident
, "REPEAT") == 0)
444 ctx
-> token
= REPEAT
;
446 else if(strcmp(ident
, "UNTIL") == 0)
448 ctx
-> token
= UNTIL
;
450 else if(strcmp(ident
, "FOR") == 0)
454 else if(strcmp(ident
, "BY") == 0)
458 else if(strcmp(ident
, "LOOP") == 0)
462 else if(strcmp(ident
, "EXIT") == 0)
466 else if(strcmp(ident
, "CASE") == 0)
473 oberon_read_number(oberon_context_t
* ctx
)
486 * mode = 3 == LONGREAL
490 start_i
= ctx
-> code_index
;
492 while(isdigit(ctx
-> c
))
494 oberon_get_char(ctx
);
497 end_i
= ctx
-> code_index
;
499 if(isxdigit(ctx
-> c
))
502 while(isxdigit(ctx
-> c
))
504 oberon_get_char(ctx
);
507 end_i
= ctx
-> code_index
;
512 oberon_get_char(ctx
);
514 else if(ctx
-> c
== 'X')
517 oberon_get_char(ctx
);
521 oberon_error(ctx
, "invalid hex number");
524 else if(ctx
-> c
== '.')
526 oberon_get_char(ctx
);
529 /* Чит: избегаем конфликта с DOTDOT */
530 ctx
-> code_index
-= 1;
536 while(isdigit(ctx
-> c
))
538 oberon_get_char(ctx
);
541 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
543 exp_i
= ctx
-> code_index
;
550 oberon_get_char(ctx
);
552 if(ctx
-> c
== '+' || ctx
-> c
== '-')
554 oberon_get_char(ctx
);
557 while(isdigit(ctx
-> c
))
559 oberon_get_char(ctx
);
563 end_i
= ctx
-> code_index
;
571 oberon_get_char(ctx
);
573 else if(ctx
-> c
== 'X')
576 oberon_get_char(ctx
);
580 int len
= end_i
- start_i
;
581 ident
= malloc(len
+ 1);
582 memcpy(ident
, &ctx
-> code
[start_i
], len
);
585 ctx
-> longmode
= false;
588 int i
= exp_i
- start_i
;
590 ctx
-> longmode
= true;
596 integer
= atol(ident
);
598 ctx
-> token
= INTEGER
;
601 sscanf(ident
, "%lx", &integer
);
603 ctx
-> token
= INTEGER
;
607 sscanf(ident
, "%lf", &real
);
611 sscanf(ident
, "%lx", &integer
);
616 oberon_error(ctx
, "oberon_read_number: wat");
620 ctx
-> string
= ident
;
621 ctx
-> integer
= integer
;
626 oberon_skip_space(oberon_context_t
* ctx
)
628 while(isspace(ctx
-> c
))
630 oberon_get_char(ctx
);
635 oberon_read_comment(oberon_context_t
* ctx
)
642 oberon_get_char(ctx
);
645 oberon_get_char(ctx
);
649 else if(ctx
-> c
== '*')
651 oberon_get_char(ctx
);
654 oberon_get_char(ctx
);
658 else if(ctx
-> c
== 0)
660 oberon_error(ctx
, "unterminated comment");
664 oberon_get_char(ctx
);
669 static void oberon_read_string(oberon_context_t
* ctx
)
672 oberon_get_char(ctx
);
674 int start
= ctx
-> code_index
;
676 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
678 oberon_get_char(ctx
);
683 oberon_error(ctx
, "unterminated string");
686 int end
= ctx
-> code_index
;
688 oberon_get_char(ctx
);
690 char * string
= calloc(1, end
- start
+ 1);
691 strncpy(string
, &ctx
-> code
[start
], end
- start
);
693 ctx
-> token
= STRING
;
694 ctx
-> string
= string
;
696 printf("oberon_read_string: string ((%s))\n", string
);
699 static void oberon_read_token(oberon_context_t
* ctx
);
702 oberon_read_symbol(oberon_context_t
* ctx
)
711 ctx
-> token
= SEMICOLON
;
712 oberon_get_char(ctx
);
715 ctx
-> token
= COLON
;
716 oberon_get_char(ctx
);
719 ctx
-> token
= ASSIGN
;
720 oberon_get_char(ctx
);
725 oberon_get_char(ctx
);
728 ctx
-> token
= DOTDOT
;
729 oberon_get_char(ctx
);
733 ctx
-> token
= LPAREN
;
734 oberon_get_char(ctx
);
737 oberon_get_char(ctx
);
738 oberon_read_comment(ctx
);
739 oberon_read_token(ctx
);
743 ctx
-> token
= RPAREN
;
744 oberon_get_char(ctx
);
747 ctx
-> token
= EQUAL
;
748 oberon_get_char(ctx
);
752 oberon_get_char(ctx
);
756 oberon_get_char(ctx
);
760 oberon_get_char(ctx
);
764 ctx
-> token
= GREAT
;
765 oberon_get_char(ctx
);
769 oberon_get_char(ctx
);
774 oberon_get_char(ctx
);
777 ctx
-> token
= MINUS
;
778 oberon_get_char(ctx
);
782 oberon_get_char(ctx
);
785 oberon_get_char(ctx
);
786 oberon_error(ctx
, "unstarted comment");
790 ctx
-> token
= SLASH
;
791 oberon_get_char(ctx
);
795 oberon_get_char(ctx
);
799 oberon_get_char(ctx
);
802 ctx
-> token
= COMMA
;
803 oberon_get_char(ctx
);
806 ctx
-> token
= LBRACK
;
807 oberon_get_char(ctx
);
810 ctx
-> token
= RBRACK
;
811 oberon_get_char(ctx
);
814 ctx
-> token
= UPARROW
;
815 oberon_get_char(ctx
);
818 oberon_read_string(ctx
);
821 oberon_read_string(ctx
);
824 ctx
-> token
= LBRACE
;
825 oberon_get_char(ctx
);
828 ctx
-> token
= RBRACE
;
829 oberon_get_char(ctx
);
833 oberon_get_char(ctx
);
836 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
842 oberon_read_token(oberon_context_t
* ctx
)
844 oberon_skip_space(ctx
);
849 oberon_read_ident(ctx
);
853 oberon_read_number(ctx
);
857 oberon_read_symbol(ctx
);
861 // =======================================================================
863 // =======================================================================
865 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
866 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
867 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
868 static char * oberon_assert_ident(oberon_context_t
* ctx
);
869 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
870 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
871 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
873 static oberon_expr_t
*
874 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
876 oberon_oper_t
* operator;
877 operator = malloc(sizeof *operator);
878 memset(operator, 0, sizeof *operator);
880 operator -> is_item
= 0;
881 operator -> result
= result
;
882 operator -> read_only
= 1;
884 operator -> left
= left
;
885 operator -> right
= right
;
887 return (oberon_expr_t
*) operator;
890 static oberon_expr_t
*
891 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
893 oberon_item_t
* item
;
894 item
= malloc(sizeof *item
);
895 memset(item
, 0, sizeof *item
);
898 item
-> result
= result
;
899 item
-> read_only
= read_only
;
902 return (oberon_expr_t
*)item
;
905 static oberon_expr_t
*
906 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
908 oberon_expr_t
* expr
;
909 oberon_type_t
* result
;
911 result
= a
-> result
;
915 if(result
-> class == OBERON_TYPE_SET
)
917 expr
= oberon_new_operator(OP_COMPLEMENTATION
, result
, a
, NULL
);
919 else if(result
-> class == OBERON_TYPE_INTEGER
)
921 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
925 oberon_error(ctx
, "incompatible operator type");
928 else if(token
== NOT
)
930 if(result
-> class != OBERON_TYPE_BOOLEAN
)
932 oberon_error(ctx
, "incompatible operator type");
935 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
939 oberon_error(ctx
, "oberon_make_unary_op: wat");
946 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
948 oberon_expr_t
* last
;
953 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
957 *first
= last
= oberon_expr(ctx
);
959 while(ctx
-> token
== COMMA
)
961 oberon_assert_token(ctx
, COMMA
);
962 oberon_expr_t
* current
;
966 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
970 current
= oberon_expr(ctx
);
973 last
-> next
= current
;
979 static oberon_expr_t
*
980 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
982 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
985 static oberon_expr_t
*
986 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
988 oberon_type_t
* from
= expr
-> result
;
989 oberon_type_t
* to
= rec
;
991 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
993 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
995 printf("oberno_make_record_cast: pointers\n");
1000 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1002 oberon_error(ctx
, "must be record type");
1005 return oberon_cast_expr(ctx
, expr
, rec
);
1008 static oberon_type_t
*
1009 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1011 oberon_type_t
* result
;
1012 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
1016 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
1020 else if(a
-> class != b
-> class)
1022 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
1024 else if(a
-> size
> b
-> size
)
1037 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
1039 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1041 from
= from
-> base
;
1045 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1047 oberon_error(ctx
, "not a record");
1050 oberon_type_t
* t
= from
;
1051 while(t
!= NULL
&& t
!= to
)
1058 oberon_error(ctx
, "incompatible record types");
1063 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1065 if(dst
-> is_item
== false)
1067 oberon_error(ctx
, "not variable");
1070 switch(dst
-> item
.mode
)
1081 oberon_error(ctx
, "not variable");
1087 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1091 if(src
-> item
.mode
== MODE_TYPE
)
1093 oberon_error(ctx
, "not variable");
1098 static oberon_expr_t
*
1099 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1102 // Если классы типов равны
1103 // Если INTEGER переводится в REAL
1104 // Есди STRING переводится в ARRAY OF CHAR
1106 oberon_check_src(ctx
, expr
);
1109 if(pref
-> class != expr
-> result
-> class)
1111 printf("expr class %i\n", expr
-> result
-> class);
1112 printf("pref class %i\n", pref
-> class);
1114 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1116 if(pref
-> class == OBERON_TYPE_ARRAY
)
1118 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1128 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1130 if(pref
-> class != OBERON_TYPE_REAL
)
1143 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1146 if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1148 if(expr
-> result
-> size
> pref
-> size
)
1150 oberon_error(ctx
, "incompatible size");
1154 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1157 else if(pref
-> class == OBERON_TYPE_RECORD
)
1159 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1160 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1162 else if(pref
-> class == OBERON_TYPE_POINTER
)
1164 assert(pref
-> base
);
1165 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1167 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1168 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1170 else if(expr
-> result
-> base
!= pref
-> base
)
1172 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1174 oberon_error(ctx
, "incompatible pointer types");
1183 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1185 oberon_type_t
* a
= (*ea
) -> result
;
1186 oberon_type_t
* b
= (*eb
) -> result
;
1187 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1188 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1189 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1193 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1195 if(desig
-> mode
!= MODE_CALL
)
1197 oberon_error(ctx
, "expected mode CALL");
1200 oberon_type_t
* fn
= desig
-> parent
-> result
;
1201 int num_args
= desig
-> num_args
;
1202 int num_decl
= fn
-> num_decl
;
1204 if(num_args
< num_decl
)
1206 oberon_error(ctx
, "too few arguments");
1208 else if(num_args
> num_decl
)
1210 oberon_error(ctx
, "too many arguments");
1213 /* Делаем проверку на запись и делаем автокаст */
1214 oberon_expr_t
* casted
[num_args
];
1215 oberon_expr_t
* arg
= desig
-> args
;
1216 oberon_object_t
* param
= fn
-> decl
;
1217 for(int i
= 0; i
< num_args
; i
++)
1219 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1221 if(arg
-> read_only
)
1223 oberon_error(ctx
, "assign to read-only var");
1227 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1229 param
= param
-> next
;
1232 /* Создаём новый список выражений */
1236 for(int i
= 0; i
< num_args
- 1; i
++)
1238 casted
[i
] -> next
= casted
[i
+ 1];
1240 desig
-> args
= arg
;
1244 static oberon_expr_t
*
1245 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1247 oberon_type_t
* signature
= item
-> result
;
1248 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1250 oberon_error(ctx
, "not a procedure");
1253 oberon_expr_t
* call
;
1255 if(signature
-> sysproc
)
1257 if(signature
-> genfunc
== NULL
)
1259 oberon_error(ctx
, "not a function-procedure");
1262 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1266 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1268 oberon_error(ctx
, "attempt to call procedure in expression");
1271 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1272 call
-> item
.parent
= item
;
1273 call
-> item
.num_args
= num_args
;
1274 call
-> item
.args
= list_args
;
1275 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1282 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1284 oberon_type_t
* signature
= item
-> result
;
1285 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1287 oberon_error(ctx
, "not a procedure");
1290 oberon_expr_t
* call
;
1292 if(signature
-> sysproc
)
1294 if(signature
-> genproc
== NULL
)
1296 oberon_error(ctx
, "not a procedure");
1299 signature
-> genproc(ctx
, num_args
, list_args
);
1303 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1305 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1308 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1309 call
-> item
.parent
= item
;
1310 call
-> item
.num_args
= num_args
;
1311 call
-> item
.args
= list_args
;
1312 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1313 oberon_generate_call_proc(ctx
, call
);
1321 || ((x) == INTEGER) \
1324 || ((x) == STRING) \
1326 || ((x) == LPAREN) \
1331 static oberon_expr_t
*
1332 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1334 printf("oberno_make_dereferencing\n");
1335 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1337 oberon_error(ctx
, "not a pointer");
1340 assert(expr
-> is_item
);
1342 oberon_expr_t
* selector
;
1343 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1344 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1349 static oberon_expr_t
*
1350 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1352 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1354 desig
= oberno_make_dereferencing(ctx
, desig
);
1357 assert(desig
-> is_item
);
1359 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1361 oberon_error(ctx
, "not array");
1364 oberon_type_t
* base
;
1365 base
= desig
-> result
-> base
;
1367 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1369 oberon_error(ctx
, "index must be integer");
1372 // Статическая проверка границ массива
1373 if(desig
-> result
-> size
!= 0)
1375 if(index
-> is_item
)
1377 if(index
-> item
.mode
== MODE_INTEGER
)
1379 int arr_size
= desig
-> result
-> size
;
1380 int index_int
= index
-> item
.integer
;
1381 if(index_int
< 0 || index_int
> arr_size
- 1)
1383 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1389 oberon_expr_t
* selector
;
1390 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1391 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1392 selector
-> item
.num_args
= 1;
1393 selector
-> item
.args
= index
;
1398 static oberon_expr_t
*
1399 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1401 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1403 expr
= oberno_make_dereferencing(ctx
, expr
);
1406 assert(expr
-> is_item
);
1408 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1410 oberon_error(ctx
, "not record");
1413 oberon_type_t
* rec
= expr
-> result
;
1415 oberon_object_t
* field
;
1416 field
= oberon_find_object(rec
-> scope
, name
, true);
1418 if(field
-> export
== 0)
1420 if(field
-> module
!= ctx
-> mod
)
1422 oberon_error(ctx
, "field not exported");
1427 if(field
-> read_only
)
1429 if(field
-> module
!= ctx
-> mod
)
1435 oberon_expr_t
* selector
;
1436 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1437 selector
-> item
.var
= field
;
1438 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1443 #define ISSELECTOR(x) \
1446 || ((x) == UPARROW) \
1449 static oberon_object_t
*
1450 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1453 oberon_object_t
* x
;
1455 name
= oberon_assert_ident(ctx
);
1456 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1460 if(x
-> class == OBERON_CLASS_MODULE
)
1462 oberon_assert_token(ctx
, DOT
);
1463 name
= oberon_assert_ident(ctx
);
1464 /* Наличие объектов в левых модулях всегда проверяется */
1465 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1467 if(x
-> export
== 0)
1469 oberon_error(ctx
, "not exported");
1482 static oberon_expr_t
*
1483 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1486 oberon_object_t
* x
;
1487 oberon_expr_t
* expr
;
1489 x
= oberon_find_object(ctx
-> decl
, name
, true);
1492 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1497 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1498 expr
-> item
.var
= x
;
1502 static oberon_expr_t
*
1503 oberon_designator(oberon_context_t
* ctx
)
1506 oberon_object_t
* var
;
1507 oberon_expr_t
* expr
;
1509 var
= oberon_qualident(ctx
, NULL
, 1);
1512 if(var
-> read_only
)
1514 if(var
-> module
!= ctx
-> mod
)
1520 switch(var
-> class)
1522 case OBERON_CLASS_CONST
:
1524 expr
= (oberon_expr_t
*) var
-> value
;
1526 case OBERON_CLASS_TYPE
:
1527 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1529 case OBERON_CLASS_VAR
:
1530 case OBERON_CLASS_VAR_PARAM
:
1531 case OBERON_CLASS_PARAM
:
1532 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1534 case OBERON_CLASS_PROC
:
1535 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1538 oberon_error(ctx
, "invalid designator");
1541 expr
-> item
.var
= var
;
1543 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1545 switch(ctx
-> token
)
1548 oberon_assert_token(ctx
, DOT
);
1549 name
= oberon_assert_ident(ctx
);
1550 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1553 oberon_assert_token(ctx
, LBRACK
);
1554 int num_indexes
= 0;
1555 oberon_expr_t
* indexes
= NULL
;
1556 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1557 oberon_assert_token(ctx
, RBRACK
);
1559 for(int i
= 0; i
< num_indexes
; i
++)
1561 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1562 indexes
= indexes
-> next
;
1566 oberon_assert_token(ctx
, UPARROW
);
1567 expr
= oberno_make_dereferencing(ctx
, expr
);
1570 oberon_assert_token(ctx
, LPAREN
);
1571 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1572 if(objtype
-> class != OBERON_CLASS_TYPE
)
1574 oberon_error(ctx
, "must be type");
1576 oberon_assert_token(ctx
, RPAREN
);
1577 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1580 oberon_error(ctx
, "oberon_designator: wat");
1588 static oberon_expr_t
*
1589 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1591 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1592 if(ctx
-> token
== LPAREN
)
1594 oberon_assert_token(ctx
, LPAREN
);
1597 oberon_expr_t
* arguments
= NULL
;
1599 if(ISEXPR(ctx
-> token
))
1601 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1604 assert(expr
-> is_item
== 1);
1605 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1607 oberon_assert_token(ctx
, RPAREN
);
1614 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1616 assert(expr
-> is_item
);
1619 oberon_expr_t
* arguments
= NULL
;
1621 if(ctx
-> token
== LPAREN
)
1623 oberon_assert_token(ctx
, LPAREN
);
1625 if(ISEXPR(ctx
-> token
))
1627 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1630 oberon_assert_token(ctx
, RPAREN
);
1633 /* Вызов происходит даже без скобок */
1634 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1637 static oberon_type_t
*
1638 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1640 if(i
>= -128 && i
<= 127)
1642 return ctx
-> byte_type
;
1644 else if(i
>= -32768 && i
<= 32767)
1646 return ctx
-> shortint_type
;
1648 else if(i
>= -2147483648 && i
<= 2147483647)
1650 return ctx
-> int_type
;
1654 return ctx
-> longint_type
;
1658 static oberon_expr_t
*
1659 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1661 oberon_expr_t
* expr
;
1662 oberon_type_t
* result
;
1663 result
= oberon_get_type_of_int_value(ctx
, i
);
1664 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1665 expr
-> item
.integer
= i
;
1669 static oberon_expr_t
*
1670 oberon_element(oberon_context_t
* ctx
)
1675 e1
= oberon_expr(ctx
);
1676 if(e1
-> result
-> class != OBERON_TYPE_INTEGER
)
1678 oberon_error(ctx
, "expected integer");
1682 if(ctx
-> token
== DOTDOT
)
1684 oberon_assert_token(ctx
, DOTDOT
);
1685 e2
= oberon_expr(ctx
);
1686 if(e2
-> result
-> class != OBERON_TYPE_INTEGER
)
1688 oberon_error(ctx
, "expected integer");
1692 oberon_expr_t
* set
;
1693 set
= oberon_new_operator(OP_RANGE
, ctx
-> set_type
, e1
, e2
);
1697 static oberon_expr_t
*
1698 oberon_set(oberon_context_t
* ctx
)
1700 oberon_expr_t
* set
;
1701 oberon_expr_t
* elements
;
1702 set
= oberon_new_item(MODE_SET
, ctx
-> set_type
, true);
1703 set
-> item
.integer
= 0;
1705 oberon_assert_token(ctx
, LBRACE
);
1706 if(ISEXPR(ctx
-> token
))
1708 elements
= oberon_element(ctx
);
1709 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1710 while(ctx
-> token
== COMMA
)
1712 oberon_assert_token(ctx
, COMMA
);
1713 elements
= oberon_element(ctx
);
1714 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1717 oberon_assert_token(ctx
, RBRACE
);
1722 static oberon_expr_t
*
1723 oberon_factor(oberon_context_t
* ctx
)
1725 oberon_expr_t
* expr
;
1726 oberon_type_t
* result
;
1728 switch(ctx
-> token
)
1731 expr
= oberon_designator(ctx
);
1732 expr
= oberon_opt_func_parens(ctx
, expr
);
1735 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1736 oberon_assert_token(ctx
, INTEGER
);
1739 result
= ctx
-> char_type
;
1740 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1741 expr
-> item
.integer
= ctx
-> integer
;
1742 oberon_assert_token(ctx
, CHAR
);
1745 result
= ctx
-> string_type
;
1746 expr
= oberon_new_item(MODE_STRING
, result
, true);
1747 expr
-> item
.string
= ctx
-> string
;
1748 oberon_assert_token(ctx
, STRING
);
1751 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1752 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1753 expr
-> item
.real
= ctx
-> real
;
1754 oberon_assert_token(ctx
, REAL
);
1757 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1758 expr
-> item
.boolean
= true;
1759 oberon_assert_token(ctx
, TRUE
);
1762 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1763 expr
-> item
.boolean
= false;
1764 oberon_assert_token(ctx
, FALSE
);
1767 expr
= oberon_set(ctx
);
1770 oberon_assert_token(ctx
, LPAREN
);
1771 expr
= oberon_expr(ctx
);
1772 oberon_assert_token(ctx
, RPAREN
);
1775 oberon_assert_token(ctx
, NOT
);
1776 expr
= oberon_factor(ctx
);
1777 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1780 oberon_assert_token(ctx
, NIL
);
1781 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1784 oberon_error(ctx
, "invalid expression");
1790 #define ITMAKESBOOLEAN(x) \
1791 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1793 #define ITUSEONLYINTEGER(x) \
1794 ((x) >= LESS && (x) <= GEQ)
1796 #define ITUSEONLYBOOLEAN(x) \
1797 (((x) == OR) || ((x) == AND))
1800 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1802 oberon_expr_t
* expr
= *e
;
1803 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1805 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1807 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1811 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1814 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1816 oberon_error(ctx
, "required numeric type");
1820 static oberon_expr_t
*
1821 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1823 oberon_expr_t
* expr
;
1824 oberon_type_t
* result
;
1829 if(a
-> result
-> class != OBERON_TYPE_INTEGER
)
1831 oberon_error(ctx
, "must be integer");
1834 if(b
-> result
-> class != OBERON_TYPE_SET
)
1836 oberon_error(ctx
, "must be set");
1839 result
= ctx
-> bool_type
;
1840 expr
= oberon_new_operator(OP_IN
, result
, a
, b
);
1842 else if(token
== IS
)
1844 oberon_type_t
* v
= a
-> result
;
1845 if(v
-> class == OBERON_TYPE_POINTER
)
1848 if(v
-> class != OBERON_TYPE_RECORD
)
1850 oberon_error(ctx
, "must be record");
1853 else if(v
-> class != OBERON_TYPE_RECORD
)
1855 oberon_error(ctx
, "must be record");
1858 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1860 oberon_error(ctx
, "requires type");
1863 oberon_type_t
* t
= b
-> result
;
1864 if(t
-> class == OBERON_TYPE_POINTER
)
1867 if(t
-> class != OBERON_TYPE_RECORD
)
1869 oberon_error(ctx
, "must be record");
1872 else if(t
-> class != OBERON_TYPE_RECORD
)
1874 oberon_error(ctx
, "must be record");
1877 result
= ctx
-> bool_type
;
1878 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1880 else if(ITMAKESBOOLEAN(token
))
1882 if(ITUSEONLYINTEGER(token
))
1884 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1885 || b
-> result
-> class == OBERON_TYPE_INTEGER
1886 || a
-> result
-> class == OBERON_TYPE_REAL
1887 || b
-> result
-> class == OBERON_TYPE_REAL
)
1893 oberon_error(ctx
, "used only with numeric types");
1896 else if(ITUSEONLYBOOLEAN(token
))
1898 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1899 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1901 oberon_error(ctx
, "used only with boolean type");
1905 oberon_autocast_binary_op(ctx
, &a
, &b
);
1906 result
= ctx
-> bool_type
;
1910 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1912 else if(token
== NEQ
)
1914 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1916 else if(token
== LESS
)
1918 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1920 else if(token
== LEQ
)
1922 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1924 else if(token
== GREAT
)
1926 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1928 else if(token
== GEQ
)
1930 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1932 else if(token
== OR
)
1934 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1936 else if(token
== AND
)
1938 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1942 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1945 else if(token
== SLASH
)
1947 if(a
-> result
-> class == OBERON_TYPE_SET
1948 || b
-> result
-> class == OBERON_TYPE_SET
)
1950 oberon_autocast_binary_op(ctx
, &a
, &b
);
1951 result
= a
-> result
;
1952 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
1956 oberon_autocast_to_real(ctx
, &a
);
1957 oberon_autocast_to_real(ctx
, &b
);
1958 oberon_autocast_binary_op(ctx
, &a
, &b
);
1959 result
= a
-> result
;
1960 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
1963 else if(token
== DIV
)
1965 if(a
-> result
-> class != OBERON_TYPE_INTEGER
1966 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
1968 oberon_error(ctx
, "operator DIV requires integer type");
1971 oberon_autocast_binary_op(ctx
, &a
, &b
);
1972 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
1976 oberon_autocast_binary_op(ctx
, &a
, &b
);
1977 result
= a
-> result
;
1978 if(result
-> class == OBERON_TYPE_SET
)
1983 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
1986 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
1989 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
1996 else if(result
-> class == OBERON_TYPE_INTEGER
1997 || result
-> class == OBERON_TYPE_REAL
)
2002 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
2005 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
2008 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
2011 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2026 oberon_error(ctx
, "invalid operation");
2032 #define ISMULOP(x) \
2033 ((x) >= STAR && (x) <= AND)
2035 static oberon_expr_t
*
2036 oberon_term_expr(oberon_context_t
* ctx
)
2038 oberon_expr_t
* expr
;
2040 expr
= oberon_factor(ctx
);
2041 while(ISMULOP(ctx
-> token
))
2043 int token
= ctx
-> token
;
2044 oberon_read_token(ctx
);
2046 oberon_expr_t
* inter
= oberon_factor(ctx
);
2047 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2053 #define ISADDOP(x) \
2054 ((x) >= PLUS && (x) <= OR)
2056 static oberon_expr_t
*
2057 oberon_simple_expr(oberon_context_t
* ctx
)
2059 oberon_expr_t
* expr
;
2062 if(ctx
-> token
== PLUS
)
2065 oberon_assert_token(ctx
, PLUS
);
2067 else if(ctx
-> token
== MINUS
)
2070 oberon_assert_token(ctx
, MINUS
);
2073 expr
= oberon_term_expr(ctx
);
2077 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2080 while(ISADDOP(ctx
-> token
))
2082 int token
= ctx
-> token
;
2083 oberon_read_token(ctx
);
2085 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2086 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2092 #define ISRELATION(x) \
2093 ((x) >= EQUAL && (x) <= IS)
2095 static oberon_expr_t
*
2096 oberon_expr(oberon_context_t
* ctx
)
2098 oberon_expr_t
* expr
;
2100 expr
= oberon_simple_expr(ctx
);
2101 while(ISRELATION(ctx
-> token
))
2103 int token
= ctx
-> token
;
2104 oberon_read_token(ctx
);
2106 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2107 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2113 static oberon_item_t
*
2114 oberon_const_expr(oberon_context_t
* ctx
)
2116 oberon_expr_t
* expr
;
2117 expr
= oberon_expr(ctx
);
2119 if(expr
-> is_item
== 0)
2121 oberon_error(ctx
, "const expression are required");
2124 switch(expr
-> item
.mode
)
2136 oberon_error(ctx
, "const expression are required");
2140 return (oberon_item_t
*) expr
;
2143 // =======================================================================
2145 // =======================================================================
2147 static void oberon_decl_seq(oberon_context_t
* ctx
);
2148 static void oberon_statement_seq(oberon_context_t
* ctx
);
2149 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2152 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2154 if(ctx
-> token
!= token
)
2156 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2161 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2163 oberon_expect_token(ctx
, token
);
2164 oberon_read_token(ctx
);
2168 oberon_assert_ident(oberon_context_t
* ctx
)
2170 oberon_expect_token(ctx
, IDENT
);
2171 char * ident
= ctx
-> string
;
2172 oberon_read_token(ctx
);
2177 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2179 switch(ctx
-> token
)
2182 oberon_assert_token(ctx
, STAR
);
2187 oberon_assert_token(ctx
, MINUS
);
2198 static oberon_object_t
*
2199 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2204 oberon_object_t
* x
;
2206 name
= oberon_assert_ident(ctx
);
2207 oberon_def(ctx
, &export
, &read_only
);
2209 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2214 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2217 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2218 while(ctx
-> token
== COMMA
)
2220 oberon_assert_token(ctx
, COMMA
);
2221 oberon_ident_def(ctx
, class, check_upscope
);
2227 oberon_var_decl(oberon_context_t
* ctx
)
2230 oberon_object_t
* list
;
2231 oberon_type_t
* type
;
2232 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2234 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2235 oberon_assert_token(ctx
, COLON
);
2236 oberon_type(ctx
, &type
);
2238 oberon_object_t
* var
= list
;
2239 for(int i
= 0; i
< num
; i
++)
2246 static oberon_object_t
*
2247 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2249 int class = OBERON_CLASS_PARAM
;
2250 if(ctx
-> token
== VAR
)
2252 oberon_read_token(ctx
);
2253 class = OBERON_CLASS_VAR_PARAM
;
2257 oberon_object_t
* list
;
2258 oberon_ident_list(ctx
, class, false, &num
, &list
);
2260 oberon_assert_token(ctx
, COLON
);
2262 oberon_type_t
* type
;
2263 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2264 oberon_type(ctx
, &type
);
2266 oberon_object_t
* param
= list
;
2267 for(int i
= 0; i
< num
; i
++)
2269 param
-> type
= type
;
2270 param
= param
-> next
;
2277 #define ISFPSECTION \
2278 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2281 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2283 oberon_assert_token(ctx
, LPAREN
);
2287 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2288 while(ctx
-> token
== SEMICOLON
)
2290 oberon_assert_token(ctx
, SEMICOLON
);
2291 oberon_fp_section(ctx
, &signature
-> num_decl
);
2295 oberon_assert_token(ctx
, RPAREN
);
2297 if(ctx
-> token
== COLON
)
2299 oberon_assert_token(ctx
, COLON
);
2301 oberon_object_t
* typeobj
;
2302 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2303 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2305 oberon_error(ctx
, "function result is not type");
2307 signature
-> base
= typeobj
-> type
;
2312 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2314 oberon_type_t
* signature
;
2316 signature
-> class = OBERON_TYPE_PROCEDURE
;
2317 signature
-> num_decl
= 0;
2318 signature
-> base
= ctx
-> void_type
;
2319 signature
-> decl
= NULL
;
2321 if(ctx
-> token
== LPAREN
)
2323 oberon_formal_pars(ctx
, signature
);
2328 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2330 if(a
-> num_decl
!= b
-> num_decl
)
2332 oberon_error(ctx
, "number parameters not matched");
2335 int num_param
= a
-> num_decl
;
2336 oberon_object_t
* param_a
= a
-> decl
;
2337 oberon_object_t
* param_b
= b
-> decl
;
2338 for(int i
= 0; i
< num_param
; i
++)
2340 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2342 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2345 if(param_a
-> type
!= param_b
-> type
)
2347 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2350 param_a
= param_a
-> next
;
2351 param_b
= param_b
-> next
;
2356 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2358 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2359 oberon_type_t
* result_type
= proc
-> type
-> base
;
2361 if(result_type
-> class == OBERON_TYPE_VOID
)
2365 oberon_error(ctx
, "procedure has no result type");
2372 oberon_error(ctx
, "procedure requires expression on result");
2375 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2378 proc
-> has_return
= 1;
2380 oberon_generate_return(ctx
, expr
);
2384 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2386 oberon_assert_token(ctx
, SEMICOLON
);
2388 ctx
-> decl
= proc
-> scope
;
2390 oberon_decl_seq(ctx
);
2392 oberon_generate_begin_proc(ctx
, proc
);
2394 if(ctx
-> token
== BEGIN
)
2396 oberon_assert_token(ctx
, BEGIN
);
2397 oberon_statement_seq(ctx
);
2400 oberon_assert_token(ctx
, END
);
2401 char * name
= oberon_assert_ident(ctx
);
2402 if(strcmp(name
, proc
-> name
) != 0)
2404 oberon_error(ctx
, "procedure name not matched");
2407 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2408 && proc
-> has_return
== 0)
2410 oberon_make_return(ctx
, NULL
);
2413 if(proc
-> has_return
== 0)
2415 oberon_error(ctx
, "procedure requires return");
2418 oberon_generate_end_proc(ctx
);
2419 oberon_close_scope(ctx
-> decl
);
2423 oberon_proc_decl(oberon_context_t
* ctx
)
2425 oberon_assert_token(ctx
, PROCEDURE
);
2428 if(ctx
-> token
== UPARROW
)
2430 oberon_assert_token(ctx
, UPARROW
);
2437 name
= oberon_assert_ident(ctx
);
2438 oberon_def(ctx
, &export
, &read_only
);
2440 oberon_scope_t
* proc_scope
;
2441 proc_scope
= oberon_open_scope(ctx
);
2442 ctx
-> decl
-> local
= 1;
2444 oberon_type_t
* signature
;
2445 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2446 oberon_opt_formal_pars(ctx
, &signature
);
2448 oberon_initialize_decl(ctx
);
2449 oberon_generator_init_type(ctx
, signature
);
2450 oberon_close_scope(ctx
-> decl
);
2452 oberon_object_t
* proc
;
2453 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2456 if(proc
-> class != OBERON_CLASS_PROC
)
2458 oberon_error(ctx
, "mult definition");
2465 oberon_error(ctx
, "mult procedure definition");
2469 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2471 oberon_error(ctx
, "export type not matched");
2474 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2478 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2479 proc
-> type
= signature
;
2480 proc
-> scope
= proc_scope
;
2481 oberon_generator_init_proc(ctx
, proc
);
2484 proc
-> scope
-> parent
= proc
;
2489 oberon_proc_decl_body(ctx
, proc
);
2494 oberon_const_decl(oberon_context_t
* ctx
)
2496 oberon_item_t
* value
;
2497 oberon_object_t
* constant
;
2499 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2500 oberon_assert_token(ctx
, EQUAL
);
2501 value
= oberon_const_expr(ctx
);
2502 constant
-> value
= value
;
2506 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2508 if(size
-> is_item
== 0)
2510 oberon_error(ctx
, "requires constant");
2513 if(size
-> item
.mode
!= MODE_INTEGER
)
2515 oberon_error(ctx
, "requires integer constant");
2518 oberon_type_t
* arr
;
2520 arr
-> class = OBERON_TYPE_ARRAY
;
2521 arr
-> size
= size
-> item
.integer
;
2526 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2529 oberon_object_t
* to
;
2531 to
= oberon_qualident(ctx
, &name
, 0);
2533 //name = oberon_assert_ident(ctx);
2534 //to = oberon_find_object(ctx -> decl, name, 0);
2538 if(to
-> class != OBERON_CLASS_TYPE
)
2540 oberon_error(ctx
, "not a type");
2545 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2546 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2552 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2555 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2559 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2567 oberon_type_t
* dim
;
2568 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2570 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2572 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2576 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2578 type
-> class = OBERON_TYPE_ARRAY
;
2580 type
-> base
= base
;
2584 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2586 if(ctx
-> token
== IDENT
)
2589 oberon_object_t
* list
;
2590 oberon_type_t
* type
;
2591 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2593 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2594 oberon_assert_token(ctx
, COLON
);
2596 oberon_scope_t
* current
= ctx
-> decl
;
2597 ctx
-> decl
= modscope
;
2598 oberon_type(ctx
, &type
);
2599 ctx
-> decl
= current
;
2601 oberon_object_t
* field
= list
;
2602 for(int i
= 0; i
< num
; i
++)
2604 field
-> type
= type
;
2605 field
= field
-> next
;
2608 rec
-> num_decl
+= num
;
2613 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2615 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2616 oberon_scope_t
* oldscope
= ctx
-> decl
;
2617 ctx
-> decl
= modscope
;
2619 if(ctx
-> token
== LPAREN
)
2621 oberon_assert_token(ctx
, LPAREN
);
2623 oberon_object_t
* typeobj
;
2624 typeobj
= oberon_qualident(ctx
, NULL
, true);
2626 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2628 oberon_error(ctx
, "base must be type");
2631 oberon_type_t
* base
= typeobj
-> type
;
2632 if(base
-> class == OBERON_TYPE_POINTER
)
2634 base
= base
-> base
;
2637 if(base
-> class != OBERON_TYPE_RECORD
)
2639 oberon_error(ctx
, "base must be record type");
2643 ctx
-> decl
= base
-> scope
;
2645 oberon_assert_token(ctx
, RPAREN
);
2652 oberon_scope_t
* this_scope
;
2653 this_scope
= oberon_open_scope(ctx
);
2654 this_scope
-> local
= true;
2655 this_scope
-> parent
= NULL
;
2656 this_scope
-> parent_type
= rec
;
2658 oberon_field_list(ctx
, rec
, modscope
);
2659 while(ctx
-> token
== SEMICOLON
)
2661 oberon_assert_token(ctx
, SEMICOLON
);
2662 oberon_field_list(ctx
, rec
, modscope
);
2665 rec
-> scope
= this_scope
;
2666 rec
-> decl
= this_scope
-> list
-> next
;
2667 ctx
-> decl
= oldscope
;
2671 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2673 if(ctx
-> token
== IDENT
)
2675 oberon_qualident_type(ctx
, type
);
2677 else if(ctx
-> token
== ARRAY
)
2679 oberon_assert_token(ctx
, ARRAY
);
2682 oberon_expr_t
* sizes
;
2684 if(ISEXPR(ctx
-> token
))
2686 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2689 oberon_assert_token(ctx
, OF
);
2691 oberon_type_t
* base
;
2692 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2693 oberon_type(ctx
, &base
);
2697 oberon_make_open_array(ctx
, base
, *type
);
2701 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2704 else if(ctx
-> token
== RECORD
)
2706 oberon_type_t
* rec
;
2708 rec
-> class = OBERON_TYPE_RECORD
;
2709 rec
-> module
= ctx
-> mod
;
2711 oberon_assert_token(ctx
, RECORD
);
2712 oberon_type_record_body(ctx
, rec
);
2713 oberon_assert_token(ctx
, END
);
2717 else if(ctx
-> token
== POINTER
)
2719 oberon_assert_token(ctx
, POINTER
);
2720 oberon_assert_token(ctx
, TO
);
2722 oberon_type_t
* base
;
2723 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2724 oberon_type(ctx
, &base
);
2726 oberon_type_t
* ptr
;
2728 ptr
-> class = OBERON_TYPE_POINTER
;
2731 else if(ctx
-> token
== PROCEDURE
)
2733 oberon_open_scope(ctx
);
2734 oberon_assert_token(ctx
, PROCEDURE
);
2735 oberon_opt_formal_pars(ctx
, type
);
2736 oberon_close_scope(ctx
-> decl
);
2740 oberon_error(ctx
, "invalid type declaration");
2745 oberon_type_decl(oberon_context_t
* ctx
)
2748 oberon_object_t
* newtype
;
2749 oberon_type_t
* type
;
2753 name
= oberon_assert_ident(ctx
);
2754 oberon_def(ctx
, &export
, &read_only
);
2756 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2759 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2760 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2761 assert(newtype
-> type
);
2765 if(newtype
-> class != OBERON_CLASS_TYPE
)
2767 oberon_error(ctx
, "mult definition");
2770 if(newtype
-> linked
)
2772 oberon_error(ctx
, "mult definition - already linked");
2775 newtype
-> export
= export
;
2776 newtype
-> read_only
= read_only
;
2779 oberon_assert_token(ctx
, EQUAL
);
2781 type
= newtype
-> type
;
2782 oberon_type(ctx
, &type
);
2784 if(type
-> class == OBERON_TYPE_VOID
)
2786 oberon_error(ctx
, "recursive alias declaration");
2789 newtype
-> type
= type
;
2790 newtype
-> linked
= 1;
2793 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2794 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2797 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2799 if(type
-> class != OBERON_TYPE_POINTER
2800 && type
-> class != OBERON_TYPE_ARRAY
)
2805 if(type
-> recursive
)
2807 oberon_error(ctx
, "recursive pointer declaration");
2810 if(type
-> class == OBERON_TYPE_POINTER
2811 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2813 oberon_error(ctx
, "attempt to make pointer to pointer");
2816 type
-> recursive
= 1;
2818 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2820 type
-> recursive
= 0;
2824 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2826 if(type
-> class != OBERON_TYPE_RECORD
)
2831 if(type
-> recursive
)
2833 oberon_error(ctx
, "recursive record declaration");
2836 type
-> recursive
= 1;
2838 int num_fields
= type
-> num_decl
;
2839 oberon_object_t
* field
= type
-> decl
;
2840 for(int i
= 0; i
< num_fields
; i
++)
2842 oberon_prevent_recursive_object(ctx
, field
);
2843 field
= field
-> next
;
2846 type
-> recursive
= 0;
2849 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2851 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2856 if(type
-> recursive
)
2858 oberon_error(ctx
, "recursive procedure declaration");
2861 type
-> recursive
= 1;
2863 int num_fields
= type
-> num_decl
;
2864 oberon_object_t
* field
= type
-> decl
;
2865 for(int i
= 0; i
< num_fields
; i
++)
2867 oberon_prevent_recursive_object(ctx
, field
);
2868 field
= field
-> next
;
2871 type
-> recursive
= 0;
2875 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2877 if(type
-> class != OBERON_TYPE_ARRAY
)
2882 if(type
-> recursive
)
2884 oberon_error(ctx
, "recursive array declaration");
2887 type
-> recursive
= 1;
2889 oberon_prevent_recursive_type(ctx
, type
-> base
);
2891 type
-> recursive
= 0;
2895 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2897 if(type
-> class == OBERON_TYPE_POINTER
)
2899 oberon_prevent_recursive_pointer(ctx
, type
);
2901 else if(type
-> class == OBERON_TYPE_RECORD
)
2903 oberon_prevent_recursive_record(ctx
, type
);
2905 else if(type
-> class == OBERON_TYPE_ARRAY
)
2907 oberon_prevent_recursive_array(ctx
, type
);
2909 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2911 oberon_prevent_recursive_procedure(ctx
, type
);
2916 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2920 case OBERON_CLASS_VAR
:
2921 case OBERON_CLASS_TYPE
:
2922 case OBERON_CLASS_PARAM
:
2923 case OBERON_CLASS_VAR_PARAM
:
2924 case OBERON_CLASS_FIELD
:
2925 oberon_prevent_recursive_type(ctx
, x
-> type
);
2927 case OBERON_CLASS_CONST
:
2928 case OBERON_CLASS_PROC
:
2929 case OBERON_CLASS_MODULE
:
2932 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2938 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2940 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2944 oberon_prevent_recursive_object(ctx
, x
);
2949 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2950 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2953 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
2955 if(type
-> class != OBERON_TYPE_RECORD
)
2960 int num_fields
= type
-> num_decl
;
2961 oberon_object_t
* field
= type
-> decl
;
2962 for(int i
= 0; i
< num_fields
; i
++)
2964 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
2966 oberon_initialize_type(ctx
, field
-> type
);
2969 oberon_initialize_object(ctx
, field
);
2970 field
= field
-> next
;
2973 oberon_generator_init_record(ctx
, type
);
2977 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2979 if(type
-> class == OBERON_TYPE_VOID
)
2981 oberon_error(ctx
, "undeclarated type");
2984 if(type
-> initialized
)
2989 type
-> initialized
= 1;
2991 if(type
-> class == OBERON_TYPE_POINTER
)
2993 oberon_initialize_type(ctx
, type
-> base
);
2994 oberon_generator_init_type(ctx
, type
);
2996 else if(type
-> class == OBERON_TYPE_ARRAY
)
2998 if(type
-> size
!= 0)
3000 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
3002 if(type
-> base
-> size
== 0)
3004 oberon_error(ctx
, "open array not allowed as array element");
3009 oberon_initialize_type(ctx
, type
-> base
);
3010 oberon_generator_init_type(ctx
, type
);
3012 else if(type
-> class == OBERON_TYPE_RECORD
)
3014 oberon_generator_init_type(ctx
, type
);
3015 oberon_initialize_record_fields(ctx
, type
);
3017 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3019 int num_fields
= type
-> num_decl
;
3020 oberon_object_t
* field
= type
-> decl
;
3021 for(int i
= 0; i
< num_fields
; i
++)
3023 oberon_initialize_object(ctx
, field
);
3024 field
= field
-> next
;
3027 oberon_generator_init_type(ctx
, type
);
3031 oberon_generator_init_type(ctx
, type
);
3036 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3038 if(x
-> initialized
)
3043 x
-> initialized
= 1;
3047 case OBERON_CLASS_TYPE
:
3048 oberon_initialize_type(ctx
, x
-> type
);
3050 case OBERON_CLASS_VAR
:
3051 case OBERON_CLASS_FIELD
:
3052 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3054 if(x
-> type
-> size
== 0)
3056 oberon_error(ctx
, "open array not allowed as variable or field");
3059 oberon_initialize_type(ctx
, x
-> type
);
3060 oberon_generator_init_var(ctx
, x
);
3062 case OBERON_CLASS_PARAM
:
3063 case OBERON_CLASS_VAR_PARAM
:
3064 oberon_initialize_type(ctx
, x
-> type
);
3065 oberon_generator_init_var(ctx
, x
);
3067 case OBERON_CLASS_CONST
:
3068 case OBERON_CLASS_PROC
:
3069 case OBERON_CLASS_MODULE
:
3072 oberon_error(ctx
, "oberon_initialize_object: wat");
3078 oberon_initialize_decl(oberon_context_t
* ctx
)
3080 oberon_object_t
* x
= ctx
-> decl
-> list
;
3084 oberon_initialize_object(ctx
, x
-> next
);
3090 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3092 oberon_object_t
* x
= ctx
-> decl
-> list
;
3096 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3098 if(x
-> next
-> linked
== 0)
3100 oberon_error(ctx
, "unresolved forward declaration");
3108 oberon_decl_seq(oberon_context_t
* ctx
)
3110 if(ctx
-> token
== CONST
)
3112 oberon_assert_token(ctx
, CONST
);
3113 while(ctx
-> token
== IDENT
)
3115 oberon_const_decl(ctx
);
3116 oberon_assert_token(ctx
, SEMICOLON
);
3120 if(ctx
-> token
== TYPE
)
3122 oberon_assert_token(ctx
, TYPE
);
3123 while(ctx
-> token
== IDENT
)
3125 oberon_type_decl(ctx
);
3126 oberon_assert_token(ctx
, SEMICOLON
);
3130 if(ctx
-> token
== VAR
)
3132 oberon_assert_token(ctx
, VAR
);
3133 while(ctx
-> token
== IDENT
)
3135 oberon_var_decl(ctx
);
3136 oberon_assert_token(ctx
, SEMICOLON
);
3140 oberon_prevent_recursive_decl(ctx
);
3141 oberon_initialize_decl(ctx
);
3143 while(ctx
-> token
== PROCEDURE
)
3145 oberon_proc_decl(ctx
);
3146 oberon_assert_token(ctx
, SEMICOLON
);
3149 oberon_prevent_undeclarated_procedures(ctx
);
3152 static oberon_expr_t
*
3153 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3155 oberon_object_t
* x
;
3156 oberon_expr_t
* expr
;
3158 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3161 oberon_generator_init_temp_var(ctx
, x
);
3163 expr
= oberon_new_item(MODE_VAR
, type
, false);
3164 expr
-> item
.var
= x
;
3169 oberon_statement_seq(oberon_context_t
* ctx
);
3172 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3174 if(dst
-> read_only
)
3176 oberon_error(ctx
, "read-only destination");
3179 oberon_check_dst(ctx
, dst
);
3180 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3181 oberon_generate_assign(ctx
, src
, dst
);
3184 static oberon_expr_t
*
3185 oberon_case_labels(oberon_context_t
* ctx
, oberon_expr_t
* val
)
3189 oberon_expr_t
* cond
;
3190 oberon_expr_t
* cond2
;
3192 e1
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3193 oberon_autocast_to(ctx
, e1
, val
-> result
);
3196 if(ctx
-> token
== DOTDOT
)
3198 oberon_assert_token(ctx
, DOTDOT
);
3199 e2
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3200 oberon_autocast_to(ctx
, e2
, val
-> result
);
3206 cond
= oberon_make_bin_op(ctx
, EQUAL
, val
, e1
);
3210 /* val >= e1 && val <= e2 */
3211 cond
= oberon_make_bin_op(ctx
, GEQ
, val
, e1
);
3212 cond2
= oberon_make_bin_op(ctx
, LEQ
, val
, e2
);
3213 cond
= oberon_make_bin_op(ctx
, AND
, cond
, cond2
);
3220 oberon_case(oberon_context_t
* ctx
, oberon_expr_t
* val
, gen_label_t
* end
)
3222 oberon_expr_t
* cond
;
3223 oberon_expr_t
* cond2
;
3224 gen_label_t
* this_end
;
3226 if(ISEXPR(ctx
-> token
))
3228 this_end
= oberon_generator_reserve_label(ctx
);
3230 cond
= oberon_case_labels(ctx
, val
);
3231 while(ctx
-> token
== COMMA
)
3233 oberon_assert_token(ctx
, COMMA
);
3235 cond2
= oberon_case_labels(ctx
, val
);
3236 cond
= oberon_make_bin_op(ctx
, OR
, cond
, cond2
);
3238 oberon_assert_token(ctx
, COLON
);
3240 oberon_generate_branch(ctx
, cond
, false, this_end
);
3241 oberon_statement_seq(ctx
);
3242 oberon_generate_goto(ctx
, end
);
3244 oberon_generate_label(ctx
, this_end
);
3249 oberon_case_statement(oberon_context_t
* ctx
)
3251 oberon_expr_t
* val
;
3252 oberon_expr_t
* expr
;
3255 end
= oberon_generator_reserve_label(ctx
);
3257 oberon_assert_token(ctx
, CASE
);
3258 expr
= oberon_expr(ctx
);
3259 val
= oberon_make_temp_var_item(ctx
, expr
-> result
);
3260 oberon_assign(ctx
, expr
, val
);
3261 oberon_assert_token(ctx
, OF
);
3262 oberon_case(ctx
, val
, end
);
3263 while(ctx
-> token
== BAR
)
3265 oberon_assert_token(ctx
, BAR
);
3266 oberon_case(ctx
, val
, end
);
3269 if(ctx
-> token
== ELSE
)
3271 oberon_assert_token(ctx
, ELSE
);
3272 oberon_statement_seq(ctx
);
3275 oberon_generate_label(ctx
, end
);
3276 oberon_assert_token(ctx
, END
);
3280 oberon_statement(oberon_context_t
* ctx
)
3282 oberon_expr_t
* item1
;
3283 oberon_expr_t
* item2
;
3285 if(ctx
-> token
== IDENT
)
3287 item1
= oberon_designator(ctx
);
3288 if(ctx
-> token
== ASSIGN
)
3290 oberon_assert_token(ctx
, ASSIGN
);
3291 item2
= oberon_expr(ctx
);
3292 oberon_assign(ctx
, item2
, item1
);
3296 oberon_opt_proc_parens(ctx
, item1
);
3299 else if(ctx
-> token
== IF
)
3303 oberon_expr_t
* cond
;
3305 els
= oberon_generator_reserve_label(ctx
);
3306 end
= oberon_generator_reserve_label(ctx
);
3308 oberon_assert_token(ctx
, IF
);
3309 cond
= oberon_expr(ctx
);
3310 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3312 oberon_error(ctx
, "condition must be boolean");
3314 oberon_assert_token(ctx
, THEN
);
3315 oberon_generate_branch(ctx
, cond
, false, els
);
3316 oberon_statement_seq(ctx
);
3317 oberon_generate_goto(ctx
, end
);
3318 oberon_generate_label(ctx
, els
);
3320 while(ctx
-> token
== ELSIF
)
3322 els
= oberon_generator_reserve_label(ctx
);
3324 oberon_assert_token(ctx
, ELSIF
);
3325 cond
= oberon_expr(ctx
);
3326 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3328 oberon_error(ctx
, "condition must be boolean");
3330 oberon_assert_token(ctx
, THEN
);
3331 oberon_generate_branch(ctx
, cond
, false, els
);
3332 oberon_statement_seq(ctx
);
3333 oberon_generate_goto(ctx
, end
);
3334 oberon_generate_label(ctx
, els
);
3337 if(ctx
-> token
== ELSE
)
3339 oberon_assert_token(ctx
, ELSE
);
3340 oberon_statement_seq(ctx
);
3343 oberon_generate_label(ctx
, end
);
3344 oberon_assert_token(ctx
, END
);
3346 else if(ctx
-> token
== WHILE
)
3348 gen_label_t
* begin
;
3350 oberon_expr_t
* cond
;
3352 begin
= oberon_generator_reserve_label(ctx
);
3353 end
= oberon_generator_reserve_label(ctx
);
3355 oberon_assert_token(ctx
, WHILE
);
3356 oberon_generate_label(ctx
, begin
);
3357 cond
= oberon_expr(ctx
);
3358 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3360 oberon_error(ctx
, "condition must be boolean");
3362 oberon_generate_branch(ctx
, cond
, false, end
);
3364 oberon_assert_token(ctx
, DO
);
3365 oberon_statement_seq(ctx
);
3366 oberon_generate_goto(ctx
, begin
);
3368 oberon_assert_token(ctx
, END
);
3369 oberon_generate_label(ctx
, end
);
3371 else if(ctx
-> token
== REPEAT
)
3373 gen_label_t
* begin
;
3374 oberon_expr_t
* cond
;
3376 begin
= oberon_generator_reserve_label(ctx
);
3377 oberon_generate_label(ctx
, begin
);
3378 oberon_assert_token(ctx
, REPEAT
);
3380 oberon_statement_seq(ctx
);
3382 oberon_assert_token(ctx
, UNTIL
);
3384 cond
= oberon_expr(ctx
);
3385 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3387 oberon_error(ctx
, "condition must be boolean");
3390 oberon_generate_branch(ctx
, cond
, true, begin
);
3392 else if(ctx
-> token
== FOR
)
3394 oberon_expr_t
* from
;
3395 oberon_expr_t
* index
;
3397 oberon_expr_t
* bound
;
3399 oberon_expr_t
* cond
;
3400 oberon_expr_t
* count
;
3401 gen_label_t
* begin
;
3406 begin
= oberon_generator_reserve_label(ctx
);
3407 end
= oberon_generator_reserve_label(ctx
);
3409 oberon_assert_token(ctx
, FOR
);
3410 iname
= oberon_assert_ident(ctx
);
3411 index
= oberon_ident_item(ctx
, iname
);
3412 oberon_assert_token(ctx
, ASSIGN
);
3413 from
= oberon_expr(ctx
);
3414 oberon_assign(ctx
, from
, index
);
3415 oberon_assert_token(ctx
, TO
);
3416 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3417 to
= oberon_expr(ctx
);
3418 oberon_assign(ctx
, to
, bound
);
3419 if(ctx
-> token
== BY
)
3421 oberon_assert_token(ctx
, BY
);
3422 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3426 by
= oberon_integer_item(ctx
, 1);
3429 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3431 oberon_error(ctx
, "must be integer");
3434 if(by
-> item
.integer
> 0)
3438 else if(by
-> item
.integer
< 0)
3444 oberon_error(ctx
, "zero step not allowed");
3447 oberon_assert_token(ctx
, DO
);
3448 oberon_generate_label(ctx
, begin
);
3449 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3450 oberon_generate_branch(ctx
, cond
, false, end
);
3451 oberon_statement_seq(ctx
);
3452 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3453 oberon_assign(ctx
, count
, index
);
3454 oberon_generate_goto(ctx
, begin
);
3455 oberon_generate_label(ctx
, end
);
3456 oberon_assert_token(ctx
, END
);
3458 else if(ctx
-> token
== LOOP
)
3460 gen_label_t
* begin
;
3463 begin
= oberon_generator_reserve_label(ctx
);
3464 end
= oberon_generator_reserve_label(ctx
);
3466 oberon_open_scope(ctx
);
3467 oberon_assert_token(ctx
, LOOP
);
3468 oberon_generate_label(ctx
, begin
);
3469 ctx
-> decl
-> exit_label
= end
;
3470 oberon_statement_seq(ctx
);
3471 oberon_generate_goto(ctx
, begin
);
3472 oberon_generate_label(ctx
, end
);
3473 oberon_assert_token(ctx
, END
);
3474 oberon_close_scope(ctx
-> decl
);
3476 else if(ctx
-> token
== EXIT
)
3478 oberon_assert_token(ctx
, EXIT
);
3479 if(ctx
-> decl
-> exit_label
== NULL
)
3481 oberon_error(ctx
, "not in LOOP-END");
3483 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3485 else if(ctx
-> token
== CASE
)
3487 oberon_case_statement(ctx
);
3489 else if(ctx
-> token
== RETURN
)
3491 oberon_assert_token(ctx
, RETURN
);
3492 if(ISEXPR(ctx
-> token
))
3494 oberon_expr_t
* expr
;
3495 expr
= oberon_expr(ctx
);
3496 oberon_make_return(ctx
, expr
);
3500 oberon_make_return(ctx
, NULL
);
3506 oberon_statement_seq(oberon_context_t
* ctx
)
3508 oberon_statement(ctx
);
3509 while(ctx
-> token
== SEMICOLON
)
3511 oberon_assert_token(ctx
, SEMICOLON
);
3512 oberon_statement(ctx
);
3517 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3519 oberon_module_t
* m
= ctx
-> module_list
;
3520 while(m
&& strcmp(m
-> name
, name
) != 0)
3528 code
= ctx
-> import_module(name
);
3531 oberon_error(ctx
, "no such module");
3534 m
= oberon_compile_module(ctx
, code
);
3540 oberon_error(ctx
, "cyclic module import");
3543 oberon_object_t
* ident
;
3544 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3545 ident
-> module
= m
;
3549 oberon_import_decl(oberon_context_t
* ctx
)
3554 alias
= name
= oberon_assert_ident(ctx
);
3555 if(ctx
-> token
== ASSIGN
)
3557 oberon_assert_token(ctx
, ASSIGN
);
3558 name
= oberon_assert_ident(ctx
);
3561 oberon_import_module(ctx
, alias
, name
);
3565 oberon_import_list(oberon_context_t
* ctx
)
3567 oberon_assert_token(ctx
, IMPORT
);
3569 oberon_import_decl(ctx
);
3570 while(ctx
-> token
== COMMA
)
3572 oberon_assert_token(ctx
, COMMA
);
3573 oberon_import_decl(ctx
);
3576 oberon_assert_token(ctx
, SEMICOLON
);
3580 oberon_parse_module(oberon_context_t
* ctx
)
3584 oberon_read_token(ctx
);
3586 oberon_assert_token(ctx
, MODULE
);
3587 name1
= oberon_assert_ident(ctx
);
3588 oberon_assert_token(ctx
, SEMICOLON
);
3589 ctx
-> mod
-> name
= name1
;
3591 oberon_generator_init_module(ctx
, ctx
-> mod
);
3593 if(ctx
-> token
== IMPORT
)
3595 oberon_import_list(ctx
);
3598 oberon_decl_seq(ctx
);
3600 oberon_generate_begin_module(ctx
);
3601 if(ctx
-> token
== BEGIN
)
3603 oberon_assert_token(ctx
, BEGIN
);
3604 oberon_statement_seq(ctx
);
3606 oberon_generate_end_module(ctx
);
3608 oberon_assert_token(ctx
, END
);
3609 name2
= oberon_assert_ident(ctx
);
3610 oberon_assert_token(ctx
, DOT
);
3612 if(strcmp(name1
, name2
) != 0)
3614 oberon_error(ctx
, "module name not matched");
3617 oberon_generator_fini_module(ctx
-> mod
);
3620 // =======================================================================
3622 // =======================================================================
3625 register_default_types(oberon_context_t
* ctx
)
3627 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3628 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3630 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3631 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3632 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3634 ctx
-> string_type
= oberon_new_type_string(1);
3635 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3637 ctx
-> bool_type
= oberon_new_type_boolean();
3638 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3640 ctx
-> byte_type
= oberon_new_type_integer(1);
3641 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3643 ctx
-> shortint_type
= oberon_new_type_integer(2);
3644 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3646 ctx
-> int_type
= oberon_new_type_integer(4);
3647 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3649 ctx
-> longint_type
= oberon_new_type_integer(8);
3650 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3652 ctx
-> real_type
= oberon_new_type_real(4);
3653 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3655 ctx
-> longreal_type
= oberon_new_type_real(8);
3656 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3658 ctx
-> char_type
= oberon_new_type_char(1);
3659 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3661 ctx
-> set_type
= oberon_new_type_set(4);
3662 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3666 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3668 oberon_object_t
* proc
;
3669 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3670 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3671 proc
-> type
-> sysproc
= true;
3672 proc
-> type
-> genfunc
= f
;
3673 proc
-> type
-> genproc
= p
;
3676 static oberon_expr_t
*
3677 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3681 oberon_error(ctx
, "too few arguments");
3686 oberon_error(ctx
, "too mach arguments");
3689 oberon_expr_t
* arg
;
3692 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3694 oberon_error(ctx
, "MIN accept only type");
3697 oberon_expr_t
* expr
;
3698 int bits
= arg
-> result
-> size
* 8;
3699 switch(arg
-> result
-> class)
3701 case OBERON_TYPE_INTEGER
:
3702 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3704 case OBERON_TYPE_SET
:
3705 expr
= oberon_integer_item(ctx
, 0);
3708 oberon_error(ctx
, "allowed only basic types");
3715 static oberon_expr_t
*
3716 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3720 oberon_error(ctx
, "too few arguments");
3725 oberon_error(ctx
, "too mach arguments");
3728 oberon_expr_t
* arg
;
3731 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3733 oberon_error(ctx
, "MAX accept only type");
3736 oberon_expr_t
* expr
;
3737 int bits
= arg
-> result
-> size
* 8;
3738 switch(arg
-> result
-> class)
3740 case OBERON_TYPE_INTEGER
:
3741 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3743 case OBERON_TYPE_SET
:
3744 expr
= oberon_integer_item(ctx
, bits
);
3747 oberon_error(ctx
, "allowed only basic types");
3754 static oberon_expr_t
*
3755 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3759 oberon_error(ctx
, "too few arguments");
3764 oberon_error(ctx
, "too mach arguments");
3767 oberon_expr_t
* arg
;
3770 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3772 oberon_error(ctx
, "SIZE accept only type");
3776 oberon_expr_t
* expr
;
3777 oberon_type_t
* type
= arg
-> result
;
3778 switch(type
-> class)
3780 case OBERON_TYPE_INTEGER
:
3781 case OBERON_TYPE_BOOLEAN
:
3782 case OBERON_TYPE_REAL
:
3783 case OBERON_TYPE_CHAR
:
3784 case OBERON_TYPE_SET
:
3785 size
= type
-> size
;
3788 oberon_error(ctx
, "TODO SIZE");
3792 expr
= oberon_integer_item(ctx
, size
);
3796 static oberon_expr_t
*
3797 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3801 oberon_error(ctx
, "too few arguments");
3806 oberon_error(ctx
, "too mach arguments");
3809 oberon_expr_t
* arg
;
3811 oberon_check_src(ctx
, arg
);
3813 oberon_type_t
* result_type
;
3814 result_type
= arg
-> result
;
3816 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3818 oberon_error(ctx
, "ABS accepts only integers");
3821 oberon_expr_t
* expr
;
3822 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3827 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3831 oberon_error(ctx
, "too few arguments");
3835 oberon_expr_t
* dst
;
3837 oberon_check_dst(ctx
, dst
);
3839 oberon_type_t
* type
;
3840 type
= dst
-> result
;
3842 if(type
-> class != OBERON_TYPE_POINTER
)
3844 oberon_error(ctx
, "not a pointer");
3847 type
= type
-> base
;
3849 oberon_expr_t
* src
;
3850 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3851 src
-> item
.num_args
= 0;
3852 src
-> item
.args
= NULL
;
3855 if(type
-> class == OBERON_TYPE_ARRAY
)
3857 if(type
-> size
== 0)
3859 oberon_type_t
* x
= type
;
3860 while(x
-> class == OBERON_TYPE_ARRAY
)
3870 if(num_args
< max_args
)
3872 oberon_error(ctx
, "too few arguments");
3875 if(num_args
> max_args
)
3877 oberon_error(ctx
, "too mach arguments");
3880 int num_sizes
= max_args
- 1;
3881 oberon_expr_t
* size_list
= list_args
-> next
;
3883 oberon_expr_t
* arg
= size_list
;
3884 for(int i
= 0; i
< max_args
- 1; i
++)
3886 oberon_check_src(ctx
, arg
);
3887 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
3889 oberon_error(ctx
, "size must be integer");
3894 src
-> item
.num_args
= num_sizes
;
3895 src
-> item
.args
= size_list
;
3897 else if(type
-> class != OBERON_TYPE_RECORD
)
3899 oberon_error(ctx
, "oberon_make_new_call: wat");
3902 if(num_args
> max_args
)
3904 oberon_error(ctx
, "too mach arguments");
3907 oberon_assign(ctx
, src
, dst
);
3911 oberon_create_context(ModuleImportCallback import_module
)
3913 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
3915 oberon_scope_t
* world_scope
;
3916 world_scope
= oberon_open_scope(ctx
);
3917 ctx
-> world_scope
= world_scope
;
3919 ctx
-> import_module
= import_module
;
3921 oberon_generator_init_context(ctx
);
3923 register_default_types(ctx
);
3926 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
3927 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
3928 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
3929 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
3932 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
3938 oberon_destroy_context(oberon_context_t
* ctx
)
3940 oberon_generator_destroy_context(ctx
);
3945 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
3947 const char * code
= ctx
-> code
;
3948 int code_index
= ctx
-> code_index
;
3950 int token
= ctx
-> token
;
3951 char * string
= ctx
-> string
;
3952 int integer
= ctx
-> integer
;
3953 int real
= ctx
-> real
;
3954 bool longmode
= ctx
-> longmode
;
3955 oberon_scope_t
* decl
= ctx
-> decl
;
3956 oberon_module_t
* mod
= ctx
-> mod
;
3958 oberon_scope_t
* module_scope
;
3959 module_scope
= oberon_open_scope(ctx
);
3961 oberon_module_t
* module
;
3962 module
= calloc(1, sizeof *module
);
3963 module
-> decl
= module_scope
;
3964 module
-> next
= ctx
-> module_list
;
3966 ctx
-> mod
= module
;
3967 ctx
-> module_list
= module
;
3969 oberon_init_scaner(ctx
, newcode
);
3970 oberon_parse_module(ctx
);
3972 module
-> ready
= 1;
3975 ctx
-> code_index
= code_index
;
3977 ctx
-> token
= token
;
3978 ctx
-> string
= string
;
3979 ctx
-> integer
= integer
;
3981 ctx
-> longmode
= longmode
;