10 #include "../include/oberon.h"
12 #include "oberon-internals.h"
13 #include "generator.h"
84 // =======================================================================
86 // =======================================================================
89 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
93 fprintf(stderr
, "error: ");
94 vfprintf(stderr
, fmt
, ptr
);
95 fprintf(stderr
, "\n");
96 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
97 fprintf(stderr
, " c = %c\n", ctx
-> c
);
98 fprintf(stderr
, " token = %i\n", ctx
-> token
);
103 static oberon_type_t
*
104 oberon_new_type_ptr(int class)
106 oberon_type_t
* x
= malloc(sizeof *x
);
107 memset(x
, 0, sizeof *x
);
112 static oberon_type_t
*
113 oberon_new_type_integer(int size
)
116 x
= oberon_new_type_ptr(OBERON_TYPE_INTEGER
);
121 static oberon_type_t
*
122 oberon_new_type_boolean()
125 x
= oberon_new_type_ptr(OBERON_TYPE_BOOLEAN
);
129 static oberon_type_t
*
130 oberon_new_type_real(int size
)
133 x
= oberon_new_type_ptr(OBERON_TYPE_REAL
);
138 static oberon_type_t
*
139 oberon_new_type_char(int size
)
142 x
= oberon_new_type_ptr(OBERON_TYPE_CHAR
);
147 static oberon_type_t
*
148 oberon_new_type_string(int size
)
151 x
= oberon_new_type_ptr(OBERON_TYPE_STRING
);
156 static oberon_type_t
*
157 oberon_new_type_set(int size
)
160 x
= oberon_new_type_ptr(OBERON_TYPE_SET
);
165 // =======================================================================
167 // =======================================================================
169 static oberon_scope_t
*
170 oberon_open_scope(oberon_context_t
* ctx
)
172 oberon_scope_t
* scope
= calloc(1, sizeof *scope
);
173 oberon_object_t
* list
= calloc(1, sizeof *list
);
176 scope
-> list
= list
;
177 scope
-> up
= ctx
-> decl
;
181 scope
-> local
= scope
-> up
-> local
;
182 scope
-> parent
= scope
-> up
-> parent
;
183 scope
-> parent_type
= scope
-> up
-> parent_type
;
184 scope
-> exit_label
= scope
-> up
-> exit_label
;
192 oberon_close_scope(oberon_scope_t
* scope
)
194 oberon_context_t
* ctx
= scope
-> ctx
;
195 ctx
-> decl
= scope
-> up
;
198 static oberon_object_t
*
199 oberon_find_object_in_list(oberon_object_t
* list
, char * name
)
201 oberon_object_t
* x
= list
;
202 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
209 static oberon_object_t
*
210 oberon_find_object(oberon_scope_t
* scope
, char * name
, bool check_it
)
212 oberon_object_t
* result
= NULL
;
214 oberon_scope_t
* s
= scope
;
215 while(result
== NULL
&& s
!= NULL
)
217 result
= oberon_find_object_in_list(s
-> list
, name
);
221 if(check_it
&& result
== NULL
)
223 oberon_error(scope
-> ctx
, "undefined ident %s", name
);
229 static oberon_object_t
*
230 oberon_create_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
)
232 oberon_object_t
* newvar
= malloc(sizeof *newvar
);
233 memset(newvar
, 0, sizeof *newvar
);
234 newvar
-> name
= name
;
235 newvar
-> class = class;
236 newvar
-> export
= export
;
237 newvar
-> read_only
= read_only
;
238 newvar
-> local
= scope
-> local
;
239 newvar
-> parent
= scope
-> parent
;
240 newvar
-> parent_type
= scope
-> parent_type
;
241 newvar
-> module
= scope
-> ctx
-> mod
;
245 static oberon_object_t
*
246 oberon_define_object(oberon_scope_t
* scope
, char * name
, int class, bool export
, bool read_only
, bool check_upscope
)
250 if(oberon_find_object(scope
-> up
, name
, false))
252 oberon_error(scope
-> ctx
, "already defined");
256 oberon_object_t
* x
= scope
-> list
;
257 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
264 oberon_error(scope
-> ctx
, "already defined");
267 oberon_object_t
* newvar
;
268 newvar
= oberon_create_object(scope
, name
, class, export
, read_only
);
274 static oberon_object_t
*
275 oberon_define_type(oberon_scope_t
* scope
, char * name
, oberon_type_t
* type
, int export
)
277 oberon_object_t
* id
;
278 id
= oberon_define_object(scope
, name
, OBERON_CLASS_TYPE
, export
, false, false);
280 oberon_generator_init_type(scope
-> ctx
, type
);
284 // =======================================================================
286 // =======================================================================
289 oberon_get_char(oberon_context_t
* ctx
)
291 if(ctx
-> code
[ctx
-> code_index
])
293 ctx
-> code_index
+= 1;
294 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
299 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
302 ctx
-> code_index
= 0;
303 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
307 oberon_read_ident(oberon_context_t
* ctx
)
310 int i
= ctx
-> code_index
;
312 int c
= ctx
-> code
[i
];
320 char * ident
= malloc(len
+ 1);
321 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
324 ctx
-> code_index
= i
;
325 ctx
-> c
= ctx
-> code
[i
];
326 ctx
-> string
= ident
;
327 ctx
-> token
= IDENT
;
329 if(strcmp(ident
, "MODULE") == 0)
331 ctx
-> token
= MODULE
;
333 else if(strcmp(ident
, "END") == 0)
337 else if(strcmp(ident
, "VAR") == 0)
341 else if(strcmp(ident
, "BEGIN") == 0)
343 ctx
-> token
= BEGIN
;
345 else if(strcmp(ident
, "OR") == 0)
349 else if(strcmp(ident
, "DIV") == 0)
353 else if(strcmp(ident
, "MOD") == 0)
357 else if(strcmp(ident
, "PROCEDURE") == 0)
359 ctx
-> token
= PROCEDURE
;
361 else if(strcmp(ident
, "RETURN") == 0)
363 ctx
-> token
= RETURN
;
365 else if(strcmp(ident
, "CONST") == 0)
367 ctx
-> token
= CONST
;
369 else if(strcmp(ident
, "TYPE") == 0)
373 else if(strcmp(ident
, "ARRAY") == 0)
375 ctx
-> token
= ARRAY
;
377 else if(strcmp(ident
, "OF") == 0)
381 else if(strcmp(ident
, "RECORD") == 0)
383 ctx
-> token
= RECORD
;
385 else if(strcmp(ident
, "POINTER") == 0)
387 ctx
-> token
= POINTER
;
389 else if(strcmp(ident
, "TO") == 0)
393 else if(strcmp(ident
, "NIL") == 0)
397 else if(strcmp(ident
, "IMPORT") == 0)
399 ctx
-> token
= IMPORT
;
401 else if(strcmp(ident
, "IN") == 0)
405 else if(strcmp(ident
, "IS") == 0)
409 else if(strcmp(ident
, "IF") == 0)
413 else if(strcmp(ident
, "THEN") == 0)
417 else if(strcmp(ident
, "ELSE") == 0)
421 else if(strcmp(ident
, "ELSIF") == 0)
423 ctx
-> token
= ELSIF
;
425 else if(strcmp(ident
, "WHILE") == 0)
427 ctx
-> token
= WHILE
;
429 else if(strcmp(ident
, "DO") == 0)
433 else if(strcmp(ident
, "REPEAT") == 0)
435 ctx
-> token
= REPEAT
;
437 else if(strcmp(ident
, "UNTIL") == 0)
439 ctx
-> token
= UNTIL
;
441 else if(strcmp(ident
, "FOR") == 0)
445 else if(strcmp(ident
, "BY") == 0)
449 else if(strcmp(ident
, "LOOP") == 0)
453 else if(strcmp(ident
, "EXIT") == 0)
457 else if(strcmp(ident
, "CASE") == 0)
461 else if(strcmp(ident
, "WITH") == 0)
467 #define ISHEXDIGIT(x) \
468 (((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F'))
471 oberon_read_number(oberon_context_t
* ctx
)
484 * mode = 3 == LONGREAL
488 start_i
= ctx
-> code_index
;
490 while(isdigit(ctx
-> c
))
492 oberon_get_char(ctx
);
495 end_i
= ctx
-> code_index
;
497 if(ISHEXDIGIT(ctx
-> c
))
500 while(ISHEXDIGIT(ctx
-> c
))
502 oberon_get_char(ctx
);
505 end_i
= ctx
-> code_index
;
510 oberon_get_char(ctx
);
512 else if(ctx
-> c
== 'X')
515 oberon_get_char(ctx
);
519 oberon_error(ctx
, "invalid hex number");
522 else if(ctx
-> c
== '.')
524 oberon_get_char(ctx
);
527 /* Чит: избегаем конфликта с DOTDOT */
528 ctx
-> code_index
-= 1;
534 while(isdigit(ctx
-> c
))
536 oberon_get_char(ctx
);
539 if(ctx
-> c
== 'E' || ctx
-> c
== 'D')
541 exp_i
= ctx
-> code_index
;
548 oberon_get_char(ctx
);
550 if(ctx
-> c
== '+' || ctx
-> c
== '-')
552 oberon_get_char(ctx
);
555 while(isdigit(ctx
-> c
))
557 oberon_get_char(ctx
);
561 end_i
= ctx
-> code_index
;
569 oberon_get_char(ctx
);
571 else if(ctx
-> c
== 'X')
574 oberon_get_char(ctx
);
578 int len
= end_i
- start_i
;
579 ident
= malloc(len
+ 1);
580 memcpy(ident
, &ctx
-> code
[start_i
], len
);
583 ctx
-> longmode
= false;
586 int i
= exp_i
- start_i
;
588 ctx
-> longmode
= true;
594 integer
= atol(ident
);
596 ctx
-> token
= INTEGER
;
599 sscanf(ident
, "%lx", &integer
);
601 ctx
-> token
= INTEGER
;
605 sscanf(ident
, "%lf", &real
);
609 sscanf(ident
, "%lx", &integer
);
614 oberon_error(ctx
, "oberon_read_number: wat");
618 ctx
-> string
= ident
;
619 ctx
-> integer
= integer
;
624 oberon_skip_space(oberon_context_t
* ctx
)
626 while(isspace(ctx
-> c
))
628 oberon_get_char(ctx
);
633 oberon_read_comment(oberon_context_t
* ctx
)
640 oberon_get_char(ctx
);
643 oberon_get_char(ctx
);
647 else if(ctx
-> c
== '*')
649 oberon_get_char(ctx
);
652 oberon_get_char(ctx
);
656 else if(ctx
-> c
== 0)
658 oberon_error(ctx
, "unterminated comment");
662 oberon_get_char(ctx
);
667 static void oberon_read_string(oberon_context_t
* ctx
)
670 oberon_get_char(ctx
);
672 int start
= ctx
-> code_index
;
674 while(ctx
-> c
!= 0 && ctx
-> c
!= c
)
676 oberon_get_char(ctx
);
681 oberon_error(ctx
, "unterminated string");
684 int end
= ctx
-> code_index
;
686 oberon_get_char(ctx
);
688 char * string
= calloc(1, end
- start
+ 1);
689 strncpy(string
, &ctx
-> code
[start
], end
- start
);
691 ctx
-> token
= STRING
;
692 ctx
-> string
= string
;
694 printf("oberon_read_string: string ((%s))\n", string
);
697 static void oberon_read_token(oberon_context_t
* ctx
);
700 oberon_read_symbol(oberon_context_t
* ctx
)
709 ctx
-> token
= SEMICOLON
;
710 oberon_get_char(ctx
);
713 ctx
-> token
= COLON
;
714 oberon_get_char(ctx
);
717 ctx
-> token
= ASSIGN
;
718 oberon_get_char(ctx
);
723 oberon_get_char(ctx
);
726 ctx
-> token
= DOTDOT
;
727 oberon_get_char(ctx
);
731 ctx
-> token
= LPAREN
;
732 oberon_get_char(ctx
);
735 oberon_get_char(ctx
);
736 oberon_read_comment(ctx
);
737 oberon_read_token(ctx
);
741 ctx
-> token
= RPAREN
;
742 oberon_get_char(ctx
);
745 ctx
-> token
= EQUAL
;
746 oberon_get_char(ctx
);
750 oberon_get_char(ctx
);
754 oberon_get_char(ctx
);
758 oberon_get_char(ctx
);
762 ctx
-> token
= GREAT
;
763 oberon_get_char(ctx
);
767 oberon_get_char(ctx
);
772 oberon_get_char(ctx
);
775 ctx
-> token
= MINUS
;
776 oberon_get_char(ctx
);
780 oberon_get_char(ctx
);
783 oberon_get_char(ctx
);
784 oberon_error(ctx
, "unstarted comment");
788 ctx
-> token
= SLASH
;
789 oberon_get_char(ctx
);
793 oberon_get_char(ctx
);
797 oberon_get_char(ctx
);
800 ctx
-> token
= COMMA
;
801 oberon_get_char(ctx
);
804 ctx
-> token
= LBRACK
;
805 oberon_get_char(ctx
);
808 ctx
-> token
= RBRACK
;
809 oberon_get_char(ctx
);
812 ctx
-> token
= UPARROW
;
813 oberon_get_char(ctx
);
816 oberon_read_string(ctx
);
819 oberon_read_string(ctx
);
822 ctx
-> token
= LBRACE
;
823 oberon_get_char(ctx
);
826 ctx
-> token
= RBRACE
;
827 oberon_get_char(ctx
);
831 oberon_get_char(ctx
);
834 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
840 oberon_read_token(oberon_context_t
* ctx
)
842 oberon_skip_space(ctx
);
847 oberon_read_ident(ctx
);
851 oberon_read_number(ctx
);
855 oberon_read_symbol(ctx
);
859 // =======================================================================
861 // =======================================================================
863 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
864 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
865 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
866 static char * oberon_assert_ident(oberon_context_t
* ctx
);
867 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
868 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
869 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
871 static oberon_expr_t
*
872 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
874 oberon_oper_t
* operator;
875 operator = malloc(sizeof *operator);
876 memset(operator, 0, sizeof *operator);
878 operator -> is_item
= 0;
879 operator -> result
= result
;
880 operator -> read_only
= 1;
882 operator -> left
= left
;
883 operator -> right
= right
;
885 return (oberon_expr_t
*) operator;
888 static oberon_expr_t
*
889 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
891 oberon_item_t
* item
;
892 item
= malloc(sizeof *item
);
893 memset(item
, 0, sizeof *item
);
896 item
-> result
= result
;
897 item
-> read_only
= read_only
;
900 return (oberon_expr_t
*)item
;
903 static oberon_expr_t
*
904 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
906 oberon_expr_t
* expr
;
907 oberon_type_t
* result
;
909 result
= a
-> result
;
913 if(result
-> class == OBERON_TYPE_SET
)
915 expr
= oberon_new_operator(OP_COMPLEMENTATION
, result
, a
, NULL
);
917 else if(result
-> class == OBERON_TYPE_INTEGER
)
919 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
923 oberon_error(ctx
, "incompatible operator type");
926 else if(token
== NOT
)
928 if(result
-> class != OBERON_TYPE_BOOLEAN
)
930 oberon_error(ctx
, "incompatible operator type");
933 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
937 oberon_error(ctx
, "oberon_make_unary_op: wat");
944 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
946 oberon_expr_t
* last
;
951 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
955 *first
= last
= oberon_expr(ctx
);
957 while(ctx
-> token
== COMMA
)
959 oberon_assert_token(ctx
, COMMA
);
960 oberon_expr_t
* current
;
964 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
968 current
= oberon_expr(ctx
);
971 last
-> next
= current
;
977 static oberon_expr_t
*
978 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
980 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
983 static oberon_expr_t
*
984 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
986 oberon_type_t
* from
= expr
-> result
;
987 oberon_type_t
* to
= rec
;
989 printf("oberno_make_record_cast: from class %i to class %i\n", from
-> class, to
-> class);
991 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
993 printf("oberno_make_record_cast: pointers\n");
998 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1000 oberon_error(ctx
, "must be record type");
1003 return oberon_cast_expr(ctx
, expr
, rec
);
1006 static oberon_type_t
*
1007 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1009 oberon_type_t
* result
;
1010 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
1014 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
1018 else if(a
-> class != b
-> class)
1020 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
1022 else if(a
-> size
> b
-> size
)
1035 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
1037 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1039 from
= from
-> base
;
1043 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1045 oberon_error(ctx
, "not a record");
1048 oberon_type_t
* t
= from
;
1049 while(t
!= NULL
&& t
!= to
)
1056 oberon_error(ctx
, "incompatible record types");
1061 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1063 if(dst
-> is_item
== false)
1065 oberon_error(ctx
, "not variable");
1068 switch(dst
-> item
.mode
)
1079 oberon_error(ctx
, "not variable");
1085 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1089 if(src
-> item
.mode
== MODE_TYPE
)
1091 oberon_error(ctx
, "not variable");
1096 static oberon_expr_t
*
1097 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1100 // Если классы типов равны
1101 // Если INTEGER переводится в REAL
1102 // Есди STRING переводится в CHAR
1103 // Есди STRING переводится в ARRAY OF CHAR
1105 oberon_check_src(ctx
, expr
);
1108 if(pref
-> class != expr
-> result
-> class)
1110 printf("expr class %i\n", expr
-> result
-> class);
1111 printf("pref class %i\n", pref
-> class);
1113 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1115 if(pref
-> class == OBERON_TYPE_CHAR
)
1117 if(expr
-> is_item
&& expr
-> item
.mode
== MODE_STRING
)
1119 if(strlen(expr
-> item
.string
) != 1)
1129 else if(pref
-> class == OBERON_TYPE_ARRAY
)
1131 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1141 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1143 if(pref
-> class != OBERON_TYPE_REAL
)
1156 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1159 if(pref
-> class == OBERON_TYPE_CHAR
)
1161 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1163 int c
= expr
-> item
.string
[0];
1164 expr
= oberon_new_item(MODE_CHAR
, ctx
-> char_type
, true);
1165 expr
-> item
.integer
= c
;
1168 else if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1170 if(expr
-> result
-> size
> pref
-> size
)
1172 oberon_error(ctx
, "incompatible size");
1176 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1179 else if(pref
-> class == OBERON_TYPE_RECORD
)
1181 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1182 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1184 else if(pref
-> class == OBERON_TYPE_POINTER
)
1186 assert(pref
-> base
);
1187 if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1189 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1190 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1192 else if(expr
-> result
-> base
!= pref
-> base
)
1194 if(expr
-> result
-> base
-> class != OBERON_TYPE_VOID
)
1196 oberon_error(ctx
, "incompatible pointer types");
1205 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1207 oberon_type_t
* a
= (*ea
) -> result
;
1208 oberon_type_t
* b
= (*eb
) -> result
;
1209 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1210 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1211 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1215 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1217 if(desig
-> mode
!= MODE_CALL
)
1219 oberon_error(ctx
, "expected mode CALL");
1222 oberon_type_t
* fn
= desig
-> parent
-> result
;
1223 int num_args
= desig
-> num_args
;
1224 int num_decl
= fn
-> num_decl
;
1226 if(num_args
< num_decl
)
1228 oberon_error(ctx
, "too few arguments");
1230 else if(num_args
> num_decl
)
1232 oberon_error(ctx
, "too many arguments");
1235 /* Делаем проверку на запись и делаем автокаст */
1236 oberon_expr_t
* casted
[num_args
];
1237 oberon_expr_t
* arg
= desig
-> args
;
1238 oberon_object_t
* param
= fn
-> decl
;
1239 for(int i
= 0; i
< num_args
; i
++)
1241 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1243 if(arg
-> result
!= param
-> type
)
1245 oberon_error(ctx
, "incompatible type");
1247 if(arg
-> read_only
)
1249 oberon_error(ctx
, "assign to read-only var");
1255 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1259 param
= param
-> next
;
1262 /* Создаём новый список выражений */
1266 for(int i
= 0; i
< num_args
- 1; i
++)
1268 casted
[i
] -> next
= casted
[i
+ 1];
1270 desig
-> args
= arg
;
1274 static oberon_expr_t
*
1275 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1277 oberon_type_t
* signature
= item
-> result
;
1278 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1280 oberon_error(ctx
, "not a procedure");
1283 oberon_expr_t
* call
;
1285 if(signature
-> sysproc
)
1287 if(signature
-> genfunc
== NULL
)
1289 oberon_error(ctx
, "not a function-procedure");
1292 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1296 if(signature
-> base
-> class == OBERON_TYPE_VOID
)
1298 oberon_error(ctx
, "attempt to call procedure in expression");
1301 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1302 call
-> item
.parent
= item
;
1303 call
-> item
.num_args
= num_args
;
1304 call
-> item
.args
= list_args
;
1305 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1312 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1314 oberon_type_t
* signature
= item
-> result
;
1315 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1317 oberon_error(ctx
, "not a procedure");
1320 oberon_expr_t
* call
;
1322 if(signature
-> sysproc
)
1324 if(signature
-> genproc
== NULL
)
1326 oberon_error(ctx
, "not a procedure");
1329 signature
-> genproc(ctx
, num_args
, list_args
);
1333 if(signature
-> base
-> class != OBERON_TYPE_VOID
)
1335 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1338 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1339 call
-> item
.parent
= item
;
1340 call
-> item
.num_args
= num_args
;
1341 call
-> item
.args
= list_args
;
1342 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1343 oberon_generate_call_proc(ctx
, call
);
1351 || ((x) == INTEGER) \
1354 || ((x) == STRING) \
1356 || ((x) == LPAREN) \
1359 static oberon_expr_t
*
1360 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1362 printf("oberno_make_dereferencing\n");
1363 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1365 oberon_error(ctx
, "not a pointer");
1368 assert(expr
-> is_item
);
1370 oberon_expr_t
* selector
;
1371 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1372 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1377 static oberon_expr_t
*
1378 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1380 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1382 desig
= oberno_make_dereferencing(ctx
, desig
);
1385 assert(desig
-> is_item
);
1387 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1389 oberon_error(ctx
, "not array");
1392 oberon_type_t
* base
;
1393 base
= desig
-> result
-> base
;
1395 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1397 oberon_error(ctx
, "index must be integer");
1400 // Статическая проверка границ массива
1401 if(desig
-> result
-> size
!= 0)
1403 if(index
-> is_item
)
1405 if(index
-> item
.mode
== MODE_INTEGER
)
1407 int arr_size
= desig
-> result
-> size
;
1408 int index_int
= index
-> item
.integer
;
1409 if(index_int
< 0 || index_int
> arr_size
- 1)
1411 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1417 oberon_expr_t
* selector
;
1418 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1419 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1420 selector
-> item
.num_args
= 1;
1421 selector
-> item
.args
= index
;
1426 static oberon_expr_t
*
1427 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1429 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1431 expr
= oberno_make_dereferencing(ctx
, expr
);
1434 assert(expr
-> is_item
);
1436 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1438 oberon_error(ctx
, "not record");
1441 oberon_type_t
* rec
= expr
-> result
;
1443 oberon_object_t
* field
;
1444 field
= oberon_find_object(rec
-> scope
, name
, true);
1446 if(field
-> export
== 0)
1448 if(field
-> module
!= ctx
-> mod
)
1450 oberon_error(ctx
, "field not exported");
1455 if(field
-> read_only
)
1457 if(field
-> module
!= ctx
-> mod
)
1463 oberon_expr_t
* selector
;
1464 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1465 selector
-> item
.var
= field
;
1466 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1471 #define ISSELECTOR(x) \
1474 || ((x) == UPARROW) \
1477 static oberon_object_t
*
1478 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1481 oberon_object_t
* x
;
1483 name
= oberon_assert_ident(ctx
);
1484 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1488 if(x
-> class == OBERON_CLASS_MODULE
)
1490 oberon_assert_token(ctx
, DOT
);
1491 name
= oberon_assert_ident(ctx
);
1492 /* Наличие объектов в левых модулях всегда проверяется */
1493 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1495 if(x
-> export
== 0)
1497 oberon_error(ctx
, "not exported");
1510 static oberon_expr_t
*
1511 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1514 oberon_object_t
* x
;
1515 oberon_expr_t
* expr
;
1517 x
= oberon_find_object(ctx
-> decl
, name
, true);
1520 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1525 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1526 expr
-> item
.var
= x
;
1530 static oberon_expr_t
*
1531 oberon_qualident_expr(oberon_context_t
* ctx
)
1533 oberon_object_t
* var
;
1534 oberon_expr_t
* expr
;
1536 var
= oberon_qualident(ctx
, NULL
, 1);
1539 if(var
-> read_only
)
1541 if(var
-> module
!= ctx
-> mod
)
1547 switch(var
-> class)
1549 case OBERON_CLASS_CONST
:
1551 expr
= (oberon_expr_t
*) var
-> value
;
1553 case OBERON_CLASS_TYPE
:
1554 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1556 case OBERON_CLASS_VAR
:
1557 case OBERON_CLASS_VAR_PARAM
:
1558 case OBERON_CLASS_PARAM
:
1559 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1561 case OBERON_CLASS_PROC
:
1562 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1565 oberon_error(ctx
, "invalid designator");
1569 expr
-> item
.var
= var
;
1574 static oberon_expr_t
*
1575 oberon_designator(oberon_context_t
* ctx
)
1578 oberon_expr_t
* expr
;
1580 expr
= oberon_qualident_expr(ctx
);
1582 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1584 switch(ctx
-> token
)
1587 oberon_assert_token(ctx
, DOT
);
1588 name
= oberon_assert_ident(ctx
);
1589 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1592 oberon_assert_token(ctx
, LBRACK
);
1593 int num_indexes
= 0;
1594 oberon_expr_t
* indexes
= NULL
;
1595 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1596 oberon_assert_token(ctx
, RBRACK
);
1598 for(int i
= 0; i
< num_indexes
; i
++)
1600 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1601 indexes
= indexes
-> next
;
1605 oberon_assert_token(ctx
, UPARROW
);
1606 expr
= oberno_make_dereferencing(ctx
, expr
);
1609 oberon_assert_token(ctx
, LPAREN
);
1610 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1611 if(objtype
-> class != OBERON_CLASS_TYPE
)
1613 oberon_error(ctx
, "must be type");
1615 oberon_assert_token(ctx
, RPAREN
);
1616 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1619 oberon_error(ctx
, "oberon_designator: wat");
1627 static oberon_expr_t
*
1628 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1630 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1631 if(ctx
-> token
== LPAREN
)
1633 oberon_assert_token(ctx
, LPAREN
);
1636 oberon_expr_t
* arguments
= NULL
;
1638 if(ISEXPR(ctx
-> token
))
1640 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1643 assert(expr
-> is_item
== 1);
1644 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1646 oberon_assert_token(ctx
, RPAREN
);
1653 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1655 assert(expr
-> is_item
);
1658 oberon_expr_t
* arguments
= NULL
;
1660 if(ctx
-> token
== LPAREN
)
1662 oberon_assert_token(ctx
, LPAREN
);
1664 if(ISEXPR(ctx
-> token
))
1666 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1669 oberon_assert_token(ctx
, RPAREN
);
1672 /* Вызов происходит даже без скобок */
1673 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1676 static oberon_type_t
*
1677 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1679 if(i
>= -128 && i
<= 127)
1681 return ctx
-> byte_type
;
1683 else if(i
>= -32768 && i
<= 32767)
1685 return ctx
-> shortint_type
;
1687 else if(i
>= -2147483648 && i
<= 2147483647)
1689 return ctx
-> int_type
;
1693 return ctx
-> longint_type
;
1697 static oberon_expr_t
*
1698 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1700 oberon_expr_t
* expr
;
1701 oberon_type_t
* result
;
1702 result
= oberon_get_type_of_int_value(ctx
, i
);
1703 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1704 expr
-> item
.integer
= i
;
1708 static oberon_expr_t
*
1709 oberon_element(oberon_context_t
* ctx
)
1714 e1
= oberon_expr(ctx
);
1715 if(e1
-> result
-> class != OBERON_TYPE_INTEGER
)
1717 oberon_error(ctx
, "expected integer");
1721 if(ctx
-> token
== DOTDOT
)
1723 oberon_assert_token(ctx
, DOTDOT
);
1724 e2
= oberon_expr(ctx
);
1725 if(e2
-> result
-> class != OBERON_TYPE_INTEGER
)
1727 oberon_error(ctx
, "expected integer");
1731 oberon_expr_t
* set
;
1732 set
= oberon_new_operator(OP_RANGE
, ctx
-> set_type
, e1
, e2
);
1736 static oberon_expr_t
*
1737 oberon_set(oberon_context_t
* ctx
)
1739 oberon_expr_t
* set
;
1740 oberon_expr_t
* elements
;
1741 set
= oberon_new_item(MODE_SET
, ctx
-> set_type
, true);
1742 set
-> item
.integer
= 0;
1744 oberon_assert_token(ctx
, LBRACE
);
1745 if(ISEXPR(ctx
-> token
))
1747 elements
= oberon_element(ctx
);
1748 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1749 while(ctx
-> token
== COMMA
)
1751 oberon_assert_token(ctx
, COMMA
);
1752 elements
= oberon_element(ctx
);
1753 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1756 oberon_assert_token(ctx
, RBRACE
);
1761 static oberon_expr_t
*
1762 oberon_make_boolean(oberon_context_t
* ctx
, bool cond
)
1764 oberon_expr_t
* expr
;
1765 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1766 expr
-> item
.integer
= cond
;
1770 static oberon_expr_t
*
1771 oberon_factor(oberon_context_t
* ctx
)
1773 oberon_expr_t
* expr
;
1774 oberon_type_t
* result
;
1776 switch(ctx
-> token
)
1779 expr
= oberon_designator(ctx
);
1780 expr
= oberon_opt_func_parens(ctx
, expr
);
1783 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1784 oberon_assert_token(ctx
, INTEGER
);
1787 result
= ctx
-> char_type
;
1788 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1789 expr
-> item
.integer
= ctx
-> integer
;
1790 oberon_assert_token(ctx
, CHAR
);
1793 result
= ctx
-> string_type
;
1794 expr
= oberon_new_item(MODE_STRING
, result
, true);
1795 expr
-> item
.string
= ctx
-> string
;
1796 oberon_assert_token(ctx
, STRING
);
1799 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1800 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1801 expr
-> item
.real
= ctx
-> real
;
1802 oberon_assert_token(ctx
, REAL
);
1805 expr
= oberon_set(ctx
);
1808 oberon_assert_token(ctx
, LPAREN
);
1809 expr
= oberon_expr(ctx
);
1810 oberon_assert_token(ctx
, RPAREN
);
1813 oberon_assert_token(ctx
, NOT
);
1814 expr
= oberon_factor(ctx
);
1815 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1818 oberon_assert_token(ctx
, NIL
);
1819 expr
= oberon_new_item(MODE_NIL
, ctx
-> void_ptr_type
, true);
1822 oberon_error(ctx
, "invalid expression");
1828 #define ITMAKESBOOLEAN(x) \
1829 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1831 #define ITUSEONLYINTEGER(x) \
1832 ((x) >= LESS && (x) <= GEQ)
1834 #define ITUSEONLYBOOLEAN(x) \
1835 (((x) == OR) || ((x) == AND))
1838 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1840 oberon_expr_t
* expr
= *e
;
1841 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1843 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1845 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1849 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1852 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1854 oberon_error(ctx
, "required numeric type");
1858 static oberon_expr_t
*
1859 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1861 oberon_expr_t
* expr
;
1862 oberon_type_t
* result
;
1867 if(a
-> result
-> class != OBERON_TYPE_INTEGER
)
1869 oberon_error(ctx
, "must be integer");
1872 if(b
-> result
-> class != OBERON_TYPE_SET
)
1874 oberon_error(ctx
, "must be set");
1877 result
= ctx
-> bool_type
;
1878 expr
= oberon_new_operator(OP_IN
, result
, a
, b
);
1880 else if(token
== IS
)
1882 oberon_type_t
* v
= a
-> result
;
1883 if(v
-> class == OBERON_TYPE_POINTER
)
1886 if(v
-> class != OBERON_TYPE_RECORD
)
1888 oberon_error(ctx
, "must be record");
1891 else if(v
-> class != OBERON_TYPE_RECORD
)
1893 oberon_error(ctx
, "must be record");
1896 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1898 oberon_error(ctx
, "requires type");
1901 oberon_type_t
* t
= b
-> result
;
1902 if(t
-> class == OBERON_TYPE_POINTER
)
1905 if(t
-> class != OBERON_TYPE_RECORD
)
1907 oberon_error(ctx
, "must be record");
1910 else if(t
-> class != OBERON_TYPE_RECORD
)
1912 oberon_error(ctx
, "must be record");
1915 result
= ctx
-> bool_type
;
1916 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1918 else if(ITMAKESBOOLEAN(token
))
1920 if(ITUSEONLYINTEGER(token
))
1922 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1923 || b
-> result
-> class == OBERON_TYPE_INTEGER
1924 || a
-> result
-> class == OBERON_TYPE_REAL
1925 || b
-> result
-> class == OBERON_TYPE_REAL
)
1931 oberon_error(ctx
, "used only with numeric types");
1934 else if(ITUSEONLYBOOLEAN(token
))
1936 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1937 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1939 oberon_error(ctx
, "used only with boolean type");
1943 oberon_autocast_binary_op(ctx
, &a
, &b
);
1944 result
= ctx
-> bool_type
;
1948 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1950 else if(token
== NEQ
)
1952 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1954 else if(token
== LESS
)
1956 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1958 else if(token
== LEQ
)
1960 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1962 else if(token
== GREAT
)
1964 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1966 else if(token
== GEQ
)
1968 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1970 else if(token
== OR
)
1972 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1974 else if(token
== AND
)
1976 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1980 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1983 else if(token
== SLASH
)
1985 if(a
-> result
-> class == OBERON_TYPE_SET
1986 || b
-> result
-> class == OBERON_TYPE_SET
)
1988 oberon_autocast_binary_op(ctx
, &a
, &b
);
1989 result
= a
-> result
;
1990 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
1994 oberon_autocast_to_real(ctx
, &a
);
1995 oberon_autocast_to_real(ctx
, &b
);
1996 oberon_autocast_binary_op(ctx
, &a
, &b
);
1997 result
= a
-> result
;
1998 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
2001 else if(token
== DIV
)
2003 if(a
-> result
-> class != OBERON_TYPE_INTEGER
2004 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
2006 oberon_error(ctx
, "operator DIV requires integer type");
2009 oberon_autocast_binary_op(ctx
, &a
, &b
);
2010 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
2014 oberon_autocast_binary_op(ctx
, &a
, &b
);
2015 result
= a
-> result
;
2016 if(result
-> class == OBERON_TYPE_SET
)
2021 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
2024 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
2027 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
2034 else if(result
-> class == OBERON_TYPE_INTEGER
2035 || result
-> class == OBERON_TYPE_REAL
)
2040 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
2043 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
2046 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
2049 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2064 oberon_error(ctx
, "invalid operation");
2070 #define ISMULOP(x) \
2071 ((x) >= STAR && (x) <= AND)
2073 static oberon_expr_t
*
2074 oberon_term_expr(oberon_context_t
* ctx
)
2076 oberon_expr_t
* expr
;
2078 expr
= oberon_factor(ctx
);
2079 while(ISMULOP(ctx
-> token
))
2081 int token
= ctx
-> token
;
2082 oberon_read_token(ctx
);
2084 oberon_expr_t
* inter
= oberon_factor(ctx
);
2085 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2091 #define ISADDOP(x) \
2092 ((x) >= PLUS && (x) <= OR)
2094 static oberon_expr_t
*
2095 oberon_simple_expr(oberon_context_t
* ctx
)
2097 oberon_expr_t
* expr
;
2100 if(ctx
-> token
== PLUS
)
2103 oberon_assert_token(ctx
, PLUS
);
2105 else if(ctx
-> token
== MINUS
)
2108 oberon_assert_token(ctx
, MINUS
);
2111 expr
= oberon_term_expr(ctx
);
2115 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2118 while(ISADDOP(ctx
-> token
))
2120 int token
= ctx
-> token
;
2121 oberon_read_token(ctx
);
2123 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2124 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2130 #define ISRELATION(x) \
2131 ((x) >= EQUAL && (x) <= IS)
2133 static oberon_expr_t
*
2134 oberon_expr(oberon_context_t
* ctx
)
2136 oberon_expr_t
* expr
;
2138 expr
= oberon_simple_expr(ctx
);
2139 while(ISRELATION(ctx
-> token
))
2141 int token
= ctx
-> token
;
2142 oberon_read_token(ctx
);
2144 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2145 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2152 oberon_check_const(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2154 if(expr
-> is_item
== 0)
2156 oberon_error(ctx
, "const expression are required");
2159 switch(expr
-> item
.mode
)
2171 oberon_error(ctx
, "const expression are required");
2176 static oberon_item_t
*
2177 oberon_const_expr(oberon_context_t
* ctx
)
2179 oberon_expr_t
* expr
;
2180 expr
= oberon_expr(ctx
);
2181 oberon_check_const(ctx
, expr
);
2182 return (oberon_item_t
*) expr
;
2185 // =======================================================================
2187 // =======================================================================
2189 static void oberon_decl_seq(oberon_context_t
* ctx
);
2190 static void oberon_statement_seq(oberon_context_t
* ctx
);
2191 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2194 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2196 if(ctx
-> token
!= token
)
2198 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2203 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2205 oberon_expect_token(ctx
, token
);
2206 oberon_read_token(ctx
);
2210 oberon_assert_ident(oberon_context_t
* ctx
)
2212 oberon_expect_token(ctx
, IDENT
);
2213 char * ident
= ctx
-> string
;
2214 oberon_read_token(ctx
);
2219 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2221 switch(ctx
-> token
)
2224 oberon_assert_token(ctx
, STAR
);
2229 oberon_assert_token(ctx
, MINUS
);
2240 static oberon_object_t
*
2241 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2246 oberon_object_t
* x
;
2248 name
= oberon_assert_ident(ctx
);
2249 oberon_def(ctx
, &export
, &read_only
);
2251 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2256 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2259 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2260 while(ctx
-> token
== COMMA
)
2262 oberon_assert_token(ctx
, COMMA
);
2263 oberon_ident_def(ctx
, class, check_upscope
);
2269 oberon_var_decl(oberon_context_t
* ctx
)
2272 oberon_object_t
* list
;
2273 oberon_type_t
* type
;
2274 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2276 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2277 oberon_assert_token(ctx
, COLON
);
2278 oberon_type(ctx
, &type
);
2280 oberon_object_t
* var
= list
;
2281 for(int i
= 0; i
< num
; i
++)
2288 static oberon_object_t
*
2289 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2291 int class = OBERON_CLASS_PARAM
;
2292 if(ctx
-> token
== VAR
)
2294 oberon_read_token(ctx
);
2295 class = OBERON_CLASS_VAR_PARAM
;
2299 oberon_object_t
* list
;
2300 oberon_ident_list(ctx
, class, false, &num
, &list
);
2302 oberon_assert_token(ctx
, COLON
);
2304 oberon_type_t
* type
;
2305 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2306 oberon_type(ctx
, &type
);
2308 oberon_object_t
* param
= list
;
2309 for(int i
= 0; i
< num
; i
++)
2311 param
-> type
= type
;
2312 param
= param
-> next
;
2319 #define ISFPSECTION \
2320 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2323 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2325 oberon_assert_token(ctx
, LPAREN
);
2329 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2330 while(ctx
-> token
== SEMICOLON
)
2332 oberon_assert_token(ctx
, SEMICOLON
);
2333 oberon_fp_section(ctx
, &signature
-> num_decl
);
2337 oberon_assert_token(ctx
, RPAREN
);
2339 if(ctx
-> token
== COLON
)
2341 oberon_assert_token(ctx
, COLON
);
2343 oberon_object_t
* typeobj
;
2344 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2345 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2347 oberon_error(ctx
, "function result is not type");
2349 signature
-> base
= typeobj
-> type
;
2354 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2356 oberon_type_t
* signature
;
2358 signature
-> class = OBERON_TYPE_PROCEDURE
;
2359 signature
-> num_decl
= 0;
2360 signature
-> base
= ctx
-> void_type
;
2361 signature
-> decl
= NULL
;
2363 if(ctx
-> token
== LPAREN
)
2365 oberon_formal_pars(ctx
, signature
);
2370 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2372 if(a
-> num_decl
!= b
-> num_decl
)
2374 oberon_error(ctx
, "number parameters not matched");
2377 int num_param
= a
-> num_decl
;
2378 oberon_object_t
* param_a
= a
-> decl
;
2379 oberon_object_t
* param_b
= b
-> decl
;
2380 for(int i
= 0; i
< num_param
; i
++)
2382 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2384 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2387 if(param_a
-> type
!= param_b
-> type
)
2389 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2392 param_a
= param_a
-> next
;
2393 param_b
= param_b
-> next
;
2398 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2400 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2401 oberon_type_t
* result_type
= proc
-> type
-> base
;
2403 if(result_type
-> class == OBERON_TYPE_VOID
)
2407 oberon_error(ctx
, "procedure has no result type");
2414 oberon_error(ctx
, "procedure requires expression on result");
2417 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2420 proc
-> has_return
= 1;
2422 oberon_generate_return(ctx
, expr
);
2426 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2428 oberon_assert_token(ctx
, SEMICOLON
);
2430 ctx
-> decl
= proc
-> scope
;
2432 oberon_decl_seq(ctx
);
2434 oberon_generate_begin_proc(ctx
, proc
);
2436 if(ctx
-> token
== BEGIN
)
2438 oberon_assert_token(ctx
, BEGIN
);
2439 oberon_statement_seq(ctx
);
2442 oberon_assert_token(ctx
, END
);
2443 char * name
= oberon_assert_ident(ctx
);
2444 if(strcmp(name
, proc
-> name
) != 0)
2446 oberon_error(ctx
, "procedure name not matched");
2449 if(proc
-> type
-> base
-> class == OBERON_TYPE_VOID
2450 && proc
-> has_return
== 0)
2452 oberon_make_return(ctx
, NULL
);
2455 if(proc
-> has_return
== 0)
2457 oberon_error(ctx
, "procedure requires return");
2460 oberon_generate_end_proc(ctx
);
2461 oberon_close_scope(ctx
-> decl
);
2465 oberon_proc_decl(oberon_context_t
* ctx
)
2467 oberon_assert_token(ctx
, PROCEDURE
);
2470 if(ctx
-> token
== UPARROW
)
2472 oberon_assert_token(ctx
, UPARROW
);
2479 name
= oberon_assert_ident(ctx
);
2480 oberon_def(ctx
, &export
, &read_only
);
2482 oberon_scope_t
* proc_scope
;
2483 proc_scope
= oberon_open_scope(ctx
);
2484 ctx
-> decl
-> local
= 1;
2486 oberon_type_t
* signature
;
2487 signature
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2488 oberon_opt_formal_pars(ctx
, &signature
);
2490 //oberon_initialize_decl(ctx);
2491 oberon_generator_init_type(ctx
, signature
);
2492 oberon_close_scope(ctx
-> decl
);
2494 oberon_object_t
* proc
;
2495 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2498 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2499 proc
-> type
= signature
;
2500 proc
-> scope
= proc_scope
;
2501 oberon_generator_init_proc(ctx
, proc
);
2505 if(proc
-> class != OBERON_CLASS_PROC
)
2507 oberon_error(ctx
, "mult definition");
2514 oberon_error(ctx
, "mult procedure definition");
2518 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2520 oberon_error(ctx
, "export type not matched");
2523 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2526 proc_scope
-> parent
= proc
;
2527 oberon_object_t
* param
= proc_scope
-> list
-> next
;
2530 param
-> parent
= proc
;
2531 param
= param
-> next
;
2537 oberon_proc_decl_body(ctx
, proc
);
2542 oberon_const_decl(oberon_context_t
* ctx
)
2544 oberon_item_t
* value
;
2545 oberon_object_t
* constant
;
2547 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2548 oberon_assert_token(ctx
, EQUAL
);
2549 value
= oberon_const_expr(ctx
);
2550 constant
-> value
= value
;
2554 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2556 if(size
-> is_item
== 0)
2558 oberon_error(ctx
, "requires constant");
2561 if(size
-> item
.mode
!= MODE_INTEGER
)
2563 oberon_error(ctx
, "requires integer constant");
2566 oberon_type_t
* arr
;
2568 arr
-> class = OBERON_TYPE_ARRAY
;
2569 arr
-> size
= size
-> item
.integer
;
2574 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2577 oberon_object_t
* to
;
2579 to
= oberon_qualident(ctx
, &name
, 0);
2581 //name = oberon_assert_ident(ctx);
2582 //to = oberon_find_object(ctx -> decl, name, 0);
2586 if(to
-> class != OBERON_CLASS_TYPE
)
2588 oberon_error(ctx
, "not a type");
2593 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2594 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2600 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2603 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2607 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2615 oberon_type_t
* dim
;
2616 dim
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2618 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2620 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2624 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2626 type
-> class = OBERON_TYPE_ARRAY
;
2628 type
-> base
= base
;
2632 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2634 if(ctx
-> token
== IDENT
)
2637 oberon_object_t
* list
;
2638 oberon_type_t
* type
;
2639 type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2641 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2642 oberon_assert_token(ctx
, COLON
);
2644 oberon_scope_t
* current
= ctx
-> decl
;
2645 ctx
-> decl
= modscope
;
2646 oberon_type(ctx
, &type
);
2647 ctx
-> decl
= current
;
2649 oberon_object_t
* field
= list
;
2650 for(int i
= 0; i
< num
; i
++)
2652 field
-> type
= type
;
2653 field
= field
-> next
;
2656 rec
-> num_decl
+= num
;
2661 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2663 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2664 oberon_scope_t
* oldscope
= ctx
-> decl
;
2665 ctx
-> decl
= modscope
;
2667 if(ctx
-> token
== LPAREN
)
2669 oberon_assert_token(ctx
, LPAREN
);
2671 oberon_object_t
* typeobj
;
2672 typeobj
= oberon_qualident(ctx
, NULL
, true);
2674 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2676 oberon_error(ctx
, "base must be type");
2679 oberon_type_t
* base
= typeobj
-> type
;
2680 if(base
-> class == OBERON_TYPE_POINTER
)
2682 base
= base
-> base
;
2685 if(base
-> class != OBERON_TYPE_RECORD
)
2687 oberon_error(ctx
, "base must be record type");
2691 ctx
-> decl
= base
-> scope
;
2693 oberon_assert_token(ctx
, RPAREN
);
2700 oberon_scope_t
* this_scope
;
2701 this_scope
= oberon_open_scope(ctx
);
2702 this_scope
-> local
= true;
2703 this_scope
-> parent
= NULL
;
2704 this_scope
-> parent_type
= rec
;
2706 oberon_field_list(ctx
, rec
, modscope
);
2707 while(ctx
-> token
== SEMICOLON
)
2709 oberon_assert_token(ctx
, SEMICOLON
);
2710 oberon_field_list(ctx
, rec
, modscope
);
2713 rec
-> scope
= this_scope
;
2714 rec
-> decl
= this_scope
-> list
-> next
;
2715 ctx
-> decl
= oldscope
;
2719 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2721 if(ctx
-> token
== IDENT
)
2723 oberon_qualident_type(ctx
, type
);
2725 else if(ctx
-> token
== ARRAY
)
2727 oberon_assert_token(ctx
, ARRAY
);
2730 oberon_expr_t
* sizes
;
2732 if(ISEXPR(ctx
-> token
))
2734 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2737 oberon_assert_token(ctx
, OF
);
2739 oberon_type_t
* base
;
2740 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2741 oberon_type(ctx
, &base
);
2745 oberon_make_open_array(ctx
, base
, *type
);
2749 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2752 else if(ctx
-> token
== RECORD
)
2754 oberon_type_t
* rec
;
2756 rec
-> class = OBERON_TYPE_RECORD
;
2757 rec
-> module
= ctx
-> mod
;
2759 oberon_assert_token(ctx
, RECORD
);
2760 oberon_type_record_body(ctx
, rec
);
2761 oberon_assert_token(ctx
, END
);
2765 else if(ctx
-> token
== POINTER
)
2767 oberon_assert_token(ctx
, POINTER
);
2768 oberon_assert_token(ctx
, TO
);
2770 oberon_type_t
* base
;
2771 base
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2772 oberon_type(ctx
, &base
);
2774 oberon_type_t
* ptr
;
2776 ptr
-> class = OBERON_TYPE_POINTER
;
2779 else if(ctx
-> token
== PROCEDURE
)
2781 oberon_open_scope(ctx
);
2782 oberon_assert_token(ctx
, PROCEDURE
);
2783 oberon_opt_formal_pars(ctx
, type
);
2784 oberon_close_scope(ctx
-> decl
);
2788 oberon_error(ctx
, "invalid type declaration");
2793 oberon_type_decl(oberon_context_t
* ctx
)
2796 oberon_object_t
* newtype
;
2797 oberon_type_t
* type
;
2801 name
= oberon_assert_ident(ctx
);
2802 oberon_def(ctx
, &export
, &read_only
);
2804 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2807 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2808 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
2809 assert(newtype
-> type
);
2813 if(newtype
-> class != OBERON_CLASS_TYPE
)
2815 oberon_error(ctx
, "mult definition");
2818 if(newtype
-> linked
)
2820 oberon_error(ctx
, "mult definition - already linked");
2823 newtype
-> export
= export
;
2824 newtype
-> read_only
= read_only
;
2827 oberon_assert_token(ctx
, EQUAL
);
2829 type
= newtype
-> type
;
2830 oberon_type(ctx
, &type
);
2832 if(type
-> class == OBERON_TYPE_VOID
)
2834 oberon_error(ctx
, "recursive alias declaration");
2837 newtype
-> type
= type
;
2838 newtype
-> linked
= 1;
2841 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2842 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2845 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2847 if(type
-> class != OBERON_TYPE_POINTER
2848 && type
-> class != OBERON_TYPE_ARRAY
)
2853 if(type
-> recursive
)
2855 oberon_error(ctx
, "recursive pointer declaration");
2858 if(type
-> class == OBERON_TYPE_POINTER
2859 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2861 oberon_error(ctx
, "attempt to make pointer to pointer");
2864 type
-> recursive
= 1;
2866 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2868 type
-> recursive
= 0;
2872 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2874 if(type
-> class != OBERON_TYPE_RECORD
)
2879 if(type
-> recursive
)
2881 oberon_error(ctx
, "recursive record declaration");
2884 type
-> recursive
= 1;
2886 int num_fields
= type
-> num_decl
;
2887 oberon_object_t
* field
= type
-> decl
;
2888 for(int i
= 0; i
< num_fields
; i
++)
2890 oberon_prevent_recursive_object(ctx
, field
);
2891 field
= field
-> next
;
2894 type
-> recursive
= 0;
2897 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2899 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2904 if(type
-> recursive
)
2906 oberon_error(ctx
, "recursive procedure declaration");
2909 type
-> recursive
= 1;
2911 int num_fields
= type
-> num_decl
;
2912 oberon_object_t
* field
= type
-> decl
;
2913 for(int i
= 0; i
< num_fields
; i
++)
2915 oberon_prevent_recursive_object(ctx
, field
);
2916 field
= field
-> next
;
2919 type
-> recursive
= 0;
2923 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2925 if(type
-> class != OBERON_TYPE_ARRAY
)
2930 if(type
-> recursive
)
2932 oberon_error(ctx
, "recursive array declaration");
2935 type
-> recursive
= 1;
2937 oberon_prevent_recursive_type(ctx
, type
-> base
);
2939 type
-> recursive
= 0;
2943 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2945 if(type
-> class == OBERON_TYPE_POINTER
)
2947 oberon_prevent_recursive_pointer(ctx
, type
);
2949 else if(type
-> class == OBERON_TYPE_RECORD
)
2951 oberon_prevent_recursive_record(ctx
, type
);
2953 else if(type
-> class == OBERON_TYPE_ARRAY
)
2955 oberon_prevent_recursive_array(ctx
, type
);
2957 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2959 oberon_prevent_recursive_procedure(ctx
, type
);
2964 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2968 case OBERON_CLASS_VAR
:
2969 case OBERON_CLASS_TYPE
:
2970 case OBERON_CLASS_PARAM
:
2971 case OBERON_CLASS_VAR_PARAM
:
2972 case OBERON_CLASS_FIELD
:
2973 oberon_prevent_recursive_type(ctx
, x
-> type
);
2975 case OBERON_CLASS_CONST
:
2976 case OBERON_CLASS_PROC
:
2977 case OBERON_CLASS_MODULE
:
2980 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2986 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2988 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2992 oberon_prevent_recursive_object(ctx
, x
);
2997 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2998 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
3001 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
3003 if(type
-> class != OBERON_TYPE_RECORD
)
3008 int num_fields
= type
-> num_decl
;
3009 oberon_object_t
* field
= type
-> decl
;
3010 for(int i
= 0; i
< num_fields
; i
++)
3012 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
3014 oberon_initialize_type(ctx
, field
-> type
);
3017 oberon_initialize_object(ctx
, field
);
3018 field
= field
-> next
;
3021 oberon_generator_init_record(ctx
, type
);
3025 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
3027 if(type
-> class == OBERON_TYPE_VOID
)
3029 oberon_error(ctx
, "undeclarated type");
3032 if(type
-> initialized
)
3037 type
-> initialized
= 1;
3039 if(type
-> class == OBERON_TYPE_POINTER
)
3041 oberon_initialize_type(ctx
, type
-> base
);
3042 oberon_generator_init_type(ctx
, type
);
3044 else if(type
-> class == OBERON_TYPE_ARRAY
)
3046 if(type
-> size
!= 0)
3048 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
3050 if(type
-> base
-> size
== 0)
3052 oberon_error(ctx
, "open array not allowed as array element");
3057 oberon_initialize_type(ctx
, type
-> base
);
3058 oberon_generator_init_type(ctx
, type
);
3060 else if(type
-> class == OBERON_TYPE_RECORD
)
3062 oberon_generator_init_type(ctx
, type
);
3063 oberon_initialize_record_fields(ctx
, type
);
3065 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3067 int num_fields
= type
-> num_decl
;
3068 oberon_object_t
* field
= type
-> decl
;
3069 for(int i
= 0; i
< num_fields
; i
++)
3071 oberon_initialize_object(ctx
, field
);
3072 field
= field
-> next
;
3075 oberon_generator_init_type(ctx
, type
);
3079 oberon_generator_init_type(ctx
, type
);
3084 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3086 if(x
-> initialized
)
3091 x
-> initialized
= 1;
3095 case OBERON_CLASS_TYPE
:
3096 oberon_initialize_type(ctx
, x
-> type
);
3098 case OBERON_CLASS_VAR
:
3099 case OBERON_CLASS_FIELD
:
3100 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3102 if(x
-> type
-> size
== 0)
3104 oberon_error(ctx
, "open array not allowed as variable or field");
3107 oberon_initialize_type(ctx
, x
-> type
);
3108 oberon_generator_init_var(ctx
, x
);
3110 case OBERON_CLASS_PARAM
:
3111 case OBERON_CLASS_VAR_PARAM
:
3112 oberon_initialize_type(ctx
, x
-> type
);
3113 oberon_generator_init_var(ctx
, x
);
3115 case OBERON_CLASS_CONST
:
3116 case OBERON_CLASS_PROC
:
3117 case OBERON_CLASS_MODULE
:
3120 oberon_error(ctx
, "oberon_initialize_object: wat");
3126 oberon_initialize_decl(oberon_context_t
* ctx
)
3128 oberon_object_t
* x
= ctx
-> decl
-> list
;
3132 oberon_initialize_object(ctx
, x
-> next
);
3138 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3140 oberon_object_t
* x
= ctx
-> decl
-> list
;
3144 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3146 if(x
-> next
-> linked
== 0)
3148 oberon_error(ctx
, "unresolved forward declaration");
3156 oberon_decl_seq(oberon_context_t
* ctx
)
3158 if(ctx
-> token
== CONST
)
3160 oberon_assert_token(ctx
, CONST
);
3161 while(ctx
-> token
== IDENT
)
3163 oberon_const_decl(ctx
);
3164 oberon_assert_token(ctx
, SEMICOLON
);
3168 if(ctx
-> token
== TYPE
)
3170 oberon_assert_token(ctx
, TYPE
);
3171 while(ctx
-> token
== IDENT
)
3173 oberon_type_decl(ctx
);
3174 oberon_assert_token(ctx
, SEMICOLON
);
3178 if(ctx
-> token
== VAR
)
3180 oberon_assert_token(ctx
, VAR
);
3181 while(ctx
-> token
== IDENT
)
3183 oberon_var_decl(ctx
);
3184 oberon_assert_token(ctx
, SEMICOLON
);
3188 oberon_prevent_recursive_decl(ctx
);
3189 oberon_initialize_decl(ctx
);
3191 while(ctx
-> token
== PROCEDURE
)
3193 oberon_proc_decl(ctx
);
3194 oberon_assert_token(ctx
, SEMICOLON
);
3197 oberon_prevent_undeclarated_procedures(ctx
);
3200 static oberon_expr_t
*
3201 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3203 oberon_object_t
* x
;
3204 oberon_expr_t
* expr
;
3206 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3209 oberon_generator_init_temp_var(ctx
, x
);
3211 expr
= oberon_new_item(MODE_VAR
, type
, false);
3212 expr
-> item
.var
= x
;
3217 oberon_statement_seq(oberon_context_t
* ctx
);
3220 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3222 if(dst
-> read_only
)
3224 oberon_error(ctx
, "read-only destination");
3227 oberon_check_dst(ctx
, dst
);
3228 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3229 oberon_generate_assign(ctx
, src
, dst
);
3232 static oberon_expr_t
*
3233 oberon_case_labels(oberon_context_t
* ctx
, oberon_expr_t
* val
)
3237 oberon_expr_t
* cond
;
3238 oberon_expr_t
* cond2
;
3240 e1
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3241 oberon_autocast_to(ctx
, e1
, val
-> result
);
3244 if(ctx
-> token
== DOTDOT
)
3246 oberon_assert_token(ctx
, DOTDOT
);
3247 e2
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3248 oberon_autocast_to(ctx
, e2
, val
-> result
);
3254 cond
= oberon_make_bin_op(ctx
, EQUAL
, val
, e1
);
3258 /* val >= e1 && val <= e2 */
3259 cond
= oberon_make_bin_op(ctx
, GEQ
, val
, e1
);
3260 cond2
= oberon_make_bin_op(ctx
, LEQ
, val
, e2
);
3261 cond
= oberon_make_bin_op(ctx
, AND
, cond
, cond2
);
3268 oberon_case(oberon_context_t
* ctx
, oberon_expr_t
* val
, gen_label_t
* end
)
3270 oberon_expr_t
* cond
;
3271 oberon_expr_t
* cond2
;
3272 gen_label_t
* this_end
;
3274 if(ISEXPR(ctx
-> token
))
3276 this_end
= oberon_generator_reserve_label(ctx
);
3278 cond
= oberon_case_labels(ctx
, val
);
3279 while(ctx
-> token
== COMMA
)
3281 oberon_assert_token(ctx
, COMMA
);
3283 cond2
= oberon_case_labels(ctx
, val
);
3284 cond
= oberon_make_bin_op(ctx
, OR
, cond
, cond2
);
3286 oberon_assert_token(ctx
, COLON
);
3288 oberon_generate_branch(ctx
, cond
, false, this_end
);
3289 oberon_statement_seq(ctx
);
3290 oberon_generate_goto(ctx
, end
);
3292 oberon_generate_label(ctx
, this_end
);
3297 oberon_case_statement(oberon_context_t
* ctx
)
3299 oberon_expr_t
* val
;
3300 oberon_expr_t
* expr
;
3303 end
= oberon_generator_reserve_label(ctx
);
3305 oberon_assert_token(ctx
, CASE
);
3306 expr
= oberon_expr(ctx
);
3307 val
= oberon_make_temp_var_item(ctx
, expr
-> result
);
3308 oberon_assign(ctx
, expr
, val
);
3309 oberon_assert_token(ctx
, OF
);
3310 oberon_case(ctx
, val
, end
);
3311 while(ctx
-> token
== BAR
)
3313 oberon_assert_token(ctx
, BAR
);
3314 oberon_case(ctx
, val
, end
);
3317 if(ctx
-> token
== ELSE
)
3319 oberon_assert_token(ctx
, ELSE
);
3320 oberon_statement_seq(ctx
);
3323 oberon_generate_label(ctx
, end
);
3324 oberon_assert_token(ctx
, END
);
3328 oberon_with_guard_do(oberon_context_t
* ctx
, gen_label_t
* end
)
3330 oberon_expr_t
* val
;
3331 oberon_expr_t
* var
;
3332 oberon_expr_t
* type
;
3333 oberon_expr_t
* cond
;
3334 oberon_expr_t
* cast
;
3335 oberon_type_t
* old_type
;
3336 gen_var_t
* old_var
;
3337 gen_label_t
* this_end
;
3339 this_end
= oberon_generator_reserve_label(ctx
);
3341 var
= oberon_qualident_expr(ctx
);
3342 oberon_assert_token(ctx
, COLON
);
3343 type
= oberon_qualident_expr(ctx
);
3344 cond
= oberon_make_bin_op(ctx
, IS
, var
, type
);
3346 oberon_assert_token(ctx
, DO
);
3347 oberon_generate_branch(ctx
, cond
, false, this_end
);
3349 /* Сохраняем ссылку во временной переменной */
3350 val
= oberon_make_temp_var_item(ctx
, type
-> result
);
3351 cast
= oberno_make_record_cast(ctx
, var
, type
-> result
);
3352 oberon_assign(ctx
, cast
, val
);
3353 /* Подменяем тип у оригинальной переменной */
3354 old_type
= var
-> item
.var
-> type
;
3355 var
-> item
.var
-> type
= type
-> result
;
3356 /* Подменяем ссылку на переменную */
3357 old_var
= var
-> item
.var
-> gen_var
;
3358 var
-> item
.var
-> gen_var
= val
-> item
.var
-> gen_var
;
3360 oberon_statement_seq(ctx
);
3361 oberon_generate_goto(ctx
, end
);
3362 oberon_generate_label(ctx
, this_end
);
3364 /* Возвращаем исходное состояние */
3365 var
-> item
.var
-> gen_var
= old_var
;
3366 var
-> item
.var
-> type
= old_type
;
3370 oberon_with_statement(oberon_context_t
* ctx
)
3373 end
= oberon_generator_reserve_label(ctx
);
3375 oberon_assert_token(ctx
, WITH
);
3376 oberon_with_guard_do(ctx
, end
);
3377 while(ctx
-> token
== BAR
)
3379 oberon_assert_token(ctx
, BAR
);
3380 oberon_with_guard_do(ctx
, end
);
3383 if(ctx
-> token
== ELSE
)
3385 oberon_assert_token(ctx
, ELSE
);
3386 oberon_statement_seq(ctx
);
3389 oberon_generate_label(ctx
, end
);
3390 oberon_assert_token(ctx
, END
);
3394 oberon_statement(oberon_context_t
* ctx
)
3396 oberon_expr_t
* item1
;
3397 oberon_expr_t
* item2
;
3399 if(ctx
-> token
== IDENT
)
3401 item1
= oberon_designator(ctx
);
3402 if(ctx
-> token
== ASSIGN
)
3404 oberon_assert_token(ctx
, ASSIGN
);
3405 item2
= oberon_expr(ctx
);
3406 oberon_assign(ctx
, item2
, item1
);
3410 oberon_opt_proc_parens(ctx
, item1
);
3413 else if(ctx
-> token
== IF
)
3417 oberon_expr_t
* cond
;
3419 els
= oberon_generator_reserve_label(ctx
);
3420 end
= oberon_generator_reserve_label(ctx
);
3422 oberon_assert_token(ctx
, IF
);
3423 cond
= oberon_expr(ctx
);
3424 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3426 oberon_error(ctx
, "condition must be boolean");
3428 oberon_assert_token(ctx
, THEN
);
3429 oberon_generate_branch(ctx
, cond
, false, els
);
3430 oberon_statement_seq(ctx
);
3431 oberon_generate_goto(ctx
, end
);
3432 oberon_generate_label(ctx
, els
);
3434 while(ctx
-> token
== ELSIF
)
3436 els
= oberon_generator_reserve_label(ctx
);
3438 oberon_assert_token(ctx
, ELSIF
);
3439 cond
= oberon_expr(ctx
);
3440 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3442 oberon_error(ctx
, "condition must be boolean");
3444 oberon_assert_token(ctx
, THEN
);
3445 oberon_generate_branch(ctx
, cond
, false, els
);
3446 oberon_statement_seq(ctx
);
3447 oberon_generate_goto(ctx
, end
);
3448 oberon_generate_label(ctx
, els
);
3451 if(ctx
-> token
== ELSE
)
3453 oberon_assert_token(ctx
, ELSE
);
3454 oberon_statement_seq(ctx
);
3457 oberon_generate_label(ctx
, end
);
3458 oberon_assert_token(ctx
, END
);
3460 else if(ctx
-> token
== WHILE
)
3462 gen_label_t
* begin
;
3464 oberon_expr_t
* cond
;
3466 begin
= oberon_generator_reserve_label(ctx
);
3467 end
= oberon_generator_reserve_label(ctx
);
3469 oberon_assert_token(ctx
, WHILE
);
3470 oberon_generate_label(ctx
, begin
);
3471 cond
= oberon_expr(ctx
);
3472 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3474 oberon_error(ctx
, "condition must be boolean");
3476 oberon_generate_branch(ctx
, cond
, false, end
);
3478 oberon_assert_token(ctx
, DO
);
3479 oberon_statement_seq(ctx
);
3480 oberon_generate_goto(ctx
, begin
);
3482 oberon_assert_token(ctx
, END
);
3483 oberon_generate_label(ctx
, end
);
3485 else if(ctx
-> token
== REPEAT
)
3487 gen_label_t
* begin
;
3488 oberon_expr_t
* cond
;
3490 begin
= oberon_generator_reserve_label(ctx
);
3491 oberon_generate_label(ctx
, begin
);
3492 oberon_assert_token(ctx
, REPEAT
);
3494 oberon_statement_seq(ctx
);
3496 oberon_assert_token(ctx
, UNTIL
);
3498 cond
= oberon_expr(ctx
);
3499 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3501 oberon_error(ctx
, "condition must be boolean");
3504 oberon_generate_branch(ctx
, cond
, true, begin
);
3506 else if(ctx
-> token
== FOR
)
3508 oberon_expr_t
* from
;
3509 oberon_expr_t
* index
;
3511 oberon_expr_t
* bound
;
3513 oberon_expr_t
* cond
;
3514 oberon_expr_t
* count
;
3515 gen_label_t
* begin
;
3520 begin
= oberon_generator_reserve_label(ctx
);
3521 end
= oberon_generator_reserve_label(ctx
);
3523 oberon_assert_token(ctx
, FOR
);
3524 iname
= oberon_assert_ident(ctx
);
3525 index
= oberon_ident_item(ctx
, iname
);
3526 oberon_assert_token(ctx
, ASSIGN
);
3527 from
= oberon_expr(ctx
);
3528 oberon_assign(ctx
, from
, index
);
3529 oberon_assert_token(ctx
, TO
);
3530 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3531 to
= oberon_expr(ctx
);
3532 oberon_assign(ctx
, to
, bound
);
3533 if(ctx
-> token
== BY
)
3535 oberon_assert_token(ctx
, BY
);
3536 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3540 by
= oberon_integer_item(ctx
, 1);
3543 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3545 oberon_error(ctx
, "must be integer");
3548 if(by
-> item
.integer
> 0)
3552 else if(by
-> item
.integer
< 0)
3558 oberon_error(ctx
, "zero step not allowed");
3561 oberon_assert_token(ctx
, DO
);
3562 oberon_generate_label(ctx
, begin
);
3563 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3564 oberon_generate_branch(ctx
, cond
, false, end
);
3565 oberon_statement_seq(ctx
);
3566 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3567 oberon_assign(ctx
, count
, index
);
3568 oberon_generate_goto(ctx
, begin
);
3569 oberon_generate_label(ctx
, end
);
3570 oberon_assert_token(ctx
, END
);
3572 else if(ctx
-> token
== LOOP
)
3574 gen_label_t
* begin
;
3577 begin
= oberon_generator_reserve_label(ctx
);
3578 end
= oberon_generator_reserve_label(ctx
);
3580 oberon_open_scope(ctx
);
3581 oberon_assert_token(ctx
, LOOP
);
3582 oberon_generate_label(ctx
, begin
);
3583 ctx
-> decl
-> exit_label
= end
;
3584 oberon_statement_seq(ctx
);
3585 oberon_generate_goto(ctx
, begin
);
3586 oberon_generate_label(ctx
, end
);
3587 oberon_assert_token(ctx
, END
);
3588 oberon_close_scope(ctx
-> decl
);
3590 else if(ctx
-> token
== EXIT
)
3592 oberon_assert_token(ctx
, EXIT
);
3593 if(ctx
-> decl
-> exit_label
== NULL
)
3595 oberon_error(ctx
, "not in LOOP-END");
3597 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3599 else if(ctx
-> token
== CASE
)
3601 oberon_case_statement(ctx
);
3603 else if(ctx
-> token
== WITH
)
3605 oberon_with_statement(ctx
);
3607 else if(ctx
-> token
== RETURN
)
3609 oberon_assert_token(ctx
, RETURN
);
3610 if(ISEXPR(ctx
-> token
))
3612 oberon_expr_t
* expr
;
3613 expr
= oberon_expr(ctx
);
3614 oberon_make_return(ctx
, expr
);
3618 oberon_make_return(ctx
, NULL
);
3624 oberon_statement_seq(oberon_context_t
* ctx
)
3626 oberon_statement(ctx
);
3627 while(ctx
-> token
== SEMICOLON
)
3629 oberon_assert_token(ctx
, SEMICOLON
);
3630 oberon_statement(ctx
);
3635 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3637 oberon_module_t
* m
= ctx
-> module_list
;
3638 while(m
&& strcmp(m
-> name
, name
) != 0)
3646 code
= ctx
-> import_module(name
);
3649 oberon_error(ctx
, "no such module");
3652 m
= oberon_compile_module(ctx
, code
);
3658 oberon_error(ctx
, "cyclic module import");
3661 oberon_object_t
* ident
;
3662 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3663 ident
-> module
= m
;
3667 oberon_import_decl(oberon_context_t
* ctx
)
3672 alias
= name
= oberon_assert_ident(ctx
);
3673 if(ctx
-> token
== ASSIGN
)
3675 oberon_assert_token(ctx
, ASSIGN
);
3676 name
= oberon_assert_ident(ctx
);
3679 oberon_import_module(ctx
, alias
, name
);
3683 oberon_import_list(oberon_context_t
* ctx
)
3685 oberon_assert_token(ctx
, IMPORT
);
3687 oberon_import_decl(ctx
);
3688 while(ctx
-> token
== COMMA
)
3690 oberon_assert_token(ctx
, COMMA
);
3691 oberon_import_decl(ctx
);
3694 oberon_assert_token(ctx
, SEMICOLON
);
3698 oberon_parse_module(oberon_context_t
* ctx
)
3702 oberon_read_token(ctx
);
3704 oberon_assert_token(ctx
, MODULE
);
3705 name1
= oberon_assert_ident(ctx
);
3706 oberon_assert_token(ctx
, SEMICOLON
);
3707 ctx
-> mod
-> name
= name1
;
3709 oberon_generator_init_module(ctx
, ctx
-> mod
);
3711 if(ctx
-> token
== IMPORT
)
3713 oberon_import_list(ctx
);
3716 oberon_decl_seq(ctx
);
3718 oberon_generate_begin_module(ctx
);
3719 if(ctx
-> token
== BEGIN
)
3721 oberon_assert_token(ctx
, BEGIN
);
3722 oberon_statement_seq(ctx
);
3724 oberon_generate_end_module(ctx
);
3726 oberon_assert_token(ctx
, END
);
3727 name2
= oberon_assert_ident(ctx
);
3728 oberon_expect_token(ctx
, DOT
);
3730 if(strcmp(name1
, name2
) != 0)
3732 oberon_error(ctx
, "module name not matched");
3735 oberon_generator_fini_module(ctx
-> mod
);
3738 // =======================================================================
3740 // =======================================================================
3743 register_default_types(oberon_context_t
* ctx
)
3745 ctx
-> void_type
= oberon_new_type_ptr(OBERON_TYPE_VOID
);
3746 oberon_generator_init_type(ctx
, ctx
-> void_type
);
3748 ctx
-> void_ptr_type
= oberon_new_type_ptr(OBERON_TYPE_POINTER
);
3749 ctx
-> void_ptr_type
-> base
= ctx
-> void_type
;
3750 oberon_generator_init_type(ctx
, ctx
-> void_ptr_type
);
3752 ctx
-> string_type
= oberon_new_type_string(1);
3753 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3755 ctx
-> bool_type
= oberon_new_type_boolean();
3756 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3758 ctx
-> byte_type
= oberon_new_type_integer(1);
3759 oberon_define_type(ctx
-> world_scope
, "BYTE", ctx
-> byte_type
, 1);
3761 ctx
-> shortint_type
= oberon_new_type_integer(2);
3762 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> shortint_type
, 1);
3764 ctx
-> int_type
= oberon_new_type_integer(4);
3765 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> int_type
, 1);
3767 ctx
-> longint_type
= oberon_new_type_integer(8);
3768 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> longint_type
, 1);
3770 ctx
-> real_type
= oberon_new_type_real(4);
3771 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3773 ctx
-> longreal_type
= oberon_new_type_real(8);
3774 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3776 ctx
-> char_type
= oberon_new_type_char(1);
3777 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3779 ctx
-> set_type
= oberon_new_type_set(4);
3780 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3784 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3786 oberon_object_t
* proc
;
3787 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3788 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3789 proc
-> type
-> sysproc
= true;
3790 proc
-> type
-> genfunc
= f
;
3791 proc
-> type
-> genproc
= p
;
3794 static oberon_expr_t
*
3795 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3799 oberon_error(ctx
, "too few arguments");
3804 oberon_error(ctx
, "too mach arguments");
3807 oberon_expr_t
* arg
;
3810 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3812 oberon_error(ctx
, "MIN accept only type");
3815 oberon_expr_t
* expr
;
3816 int bits
= arg
-> result
-> size
* 8;
3817 switch(arg
-> result
-> class)
3819 case OBERON_TYPE_INTEGER
:
3820 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3822 case OBERON_TYPE_SET
:
3823 expr
= oberon_integer_item(ctx
, 0);
3826 oberon_error(ctx
, "allowed only basic types");
3833 static oberon_expr_t
*
3834 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3838 oberon_error(ctx
, "too few arguments");
3843 oberon_error(ctx
, "too mach arguments");
3846 oberon_expr_t
* arg
;
3849 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3851 oberon_error(ctx
, "MAX accept only type");
3854 oberon_expr_t
* expr
;
3855 int bits
= arg
-> result
-> size
* 8;
3856 switch(arg
-> result
-> class)
3858 case OBERON_TYPE_INTEGER
:
3859 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3861 case OBERON_TYPE_SET
:
3862 expr
= oberon_integer_item(ctx
, bits
);
3865 oberon_error(ctx
, "allowed only basic types");
3872 static oberon_expr_t
*
3873 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3877 oberon_error(ctx
, "too few arguments");
3882 oberon_error(ctx
, "too mach arguments");
3885 oberon_expr_t
* arg
;
3888 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3890 oberon_error(ctx
, "SIZE accept only type");
3894 oberon_expr_t
* expr
;
3895 oberon_type_t
* type
= arg
-> result
;
3896 switch(type
-> class)
3898 case OBERON_TYPE_INTEGER
:
3899 case OBERON_TYPE_BOOLEAN
:
3900 case OBERON_TYPE_REAL
:
3901 case OBERON_TYPE_CHAR
:
3902 case OBERON_TYPE_SET
:
3903 size
= type
-> size
;
3906 oberon_error(ctx
, "TODO SIZE");
3910 expr
= oberon_integer_item(ctx
, size
);
3914 static oberon_expr_t
*
3915 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3919 oberon_error(ctx
, "too few arguments");
3924 oberon_error(ctx
, "too mach arguments");
3927 oberon_expr_t
* arg
;
3929 oberon_check_src(ctx
, arg
);
3931 oberon_type_t
* result_type
;
3932 result_type
= arg
-> result
;
3934 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3936 oberon_error(ctx
, "ABS accepts only integers");
3939 oberon_expr_t
* expr
;
3940 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3945 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3949 oberon_error(ctx
, "too few arguments");
3953 oberon_expr_t
* dst
;
3955 oberon_check_dst(ctx
, dst
);
3957 oberon_type_t
* type
;
3958 type
= dst
-> result
;
3960 if(type
-> class != OBERON_TYPE_POINTER
)
3962 oberon_error(ctx
, "not a pointer");
3965 type
= type
-> base
;
3967 oberon_expr_t
* src
;
3968 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3969 src
-> item
.num_args
= 0;
3970 src
-> item
.args
= NULL
;
3973 if(type
-> class == OBERON_TYPE_ARRAY
)
3975 if(type
-> size
== 0)
3977 oberon_type_t
* x
= type
;
3978 while(x
-> class == OBERON_TYPE_ARRAY
)
3988 if(num_args
< max_args
)
3990 oberon_error(ctx
, "too few arguments");
3993 if(num_args
> max_args
)
3995 oberon_error(ctx
, "too mach arguments");
3998 int num_sizes
= max_args
- 1;
3999 oberon_expr_t
* size_list
= list_args
-> next
;
4001 oberon_expr_t
* arg
= size_list
;
4002 for(int i
= 0; i
< max_args
- 1; i
++)
4004 oberon_check_src(ctx
, arg
);
4005 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
4007 oberon_error(ctx
, "size must be integer");
4012 src
-> item
.num_args
= num_sizes
;
4013 src
-> item
.args
= size_list
;
4015 else if(type
-> class != OBERON_TYPE_RECORD
)
4017 oberon_error(ctx
, "oberon_make_new_call: wat");
4020 if(num_args
> max_args
)
4022 oberon_error(ctx
, "too mach arguments");
4025 oberon_assign(ctx
, src
, dst
);
4029 oberon_new_const(oberon_context_t
* ctx
, char * name
, oberon_expr_t
* expr
)
4031 oberon_object_t
* constant
;
4032 constant
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_CONST
, true, false, false);
4033 oberon_check_const(ctx
, expr
);
4034 constant
-> value
= (oberon_item_t
*) expr
;
4038 oberon_create_context(ModuleImportCallback import_module
)
4040 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
4042 oberon_scope_t
* world_scope
;
4043 world_scope
= oberon_open_scope(ctx
);
4044 ctx
-> world_scope
= world_scope
;
4046 ctx
-> import_module
= import_module
;
4048 oberon_generator_init_context(ctx
);
4050 register_default_types(ctx
);
4053 oberon_new_const(ctx
, "TRUE", oberon_make_boolean(ctx
, true));
4054 oberon_new_const(ctx
, "FALSE", oberon_make_boolean(ctx
, false));
4057 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
4058 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
4059 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
4060 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
4063 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
4069 oberon_destroy_context(oberon_context_t
* ctx
)
4071 oberon_generator_destroy_context(ctx
);
4076 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
4078 const char * code
= ctx
-> code
;
4079 int code_index
= ctx
-> code_index
;
4081 int token
= ctx
-> token
;
4082 char * string
= ctx
-> string
;
4083 int integer
= ctx
-> integer
;
4084 int real
= ctx
-> real
;
4085 bool longmode
= ctx
-> longmode
;
4086 oberon_scope_t
* decl
= ctx
-> decl
;
4087 oberon_module_t
* mod
= ctx
-> mod
;
4089 oberon_scope_t
* module_scope
;
4090 module_scope
= oberon_open_scope(ctx
);
4092 oberon_module_t
* module
;
4093 module
= calloc(1, sizeof *module
);
4094 module
-> decl
= module_scope
;
4095 module
-> next
= ctx
-> module_list
;
4097 ctx
-> mod
= module
;
4098 ctx
-> module_list
= module
;
4100 oberon_init_scaner(ctx
, newcode
);
4101 oberon_parse_module(ctx
);
4103 module
-> ready
= 1;
4106 ctx
-> code_index
= code_index
;
4108 ctx
-> token
= token
;
4109 ctx
-> string
= string
;
4110 ctx
-> integer
= integer
;
4112 ctx
-> longmode
= longmode
;