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
;
695 static void oberon_read_token(oberon_context_t
* ctx
);
698 oberon_read_symbol(oberon_context_t
* ctx
)
707 ctx
-> token
= SEMICOLON
;
708 oberon_get_char(ctx
);
711 ctx
-> token
= COLON
;
712 oberon_get_char(ctx
);
715 ctx
-> token
= ASSIGN
;
716 oberon_get_char(ctx
);
721 oberon_get_char(ctx
);
724 ctx
-> token
= DOTDOT
;
725 oberon_get_char(ctx
);
729 ctx
-> token
= LPAREN
;
730 oberon_get_char(ctx
);
733 oberon_get_char(ctx
);
734 oberon_read_comment(ctx
);
735 oberon_read_token(ctx
);
739 ctx
-> token
= RPAREN
;
740 oberon_get_char(ctx
);
743 ctx
-> token
= EQUAL
;
744 oberon_get_char(ctx
);
748 oberon_get_char(ctx
);
752 oberon_get_char(ctx
);
756 oberon_get_char(ctx
);
760 ctx
-> token
= GREAT
;
761 oberon_get_char(ctx
);
765 oberon_get_char(ctx
);
770 oberon_get_char(ctx
);
773 ctx
-> token
= MINUS
;
774 oberon_get_char(ctx
);
778 oberon_get_char(ctx
);
781 oberon_get_char(ctx
);
782 oberon_error(ctx
, "unstarted comment");
786 ctx
-> token
= SLASH
;
787 oberon_get_char(ctx
);
791 oberon_get_char(ctx
);
795 oberon_get_char(ctx
);
798 ctx
-> token
= COMMA
;
799 oberon_get_char(ctx
);
802 ctx
-> token
= LBRACK
;
803 oberon_get_char(ctx
);
806 ctx
-> token
= RBRACK
;
807 oberon_get_char(ctx
);
810 ctx
-> token
= UPARROW
;
811 oberon_get_char(ctx
);
814 oberon_read_string(ctx
);
817 oberon_read_string(ctx
);
820 ctx
-> token
= LBRACE
;
821 oberon_get_char(ctx
);
824 ctx
-> token
= RBRACE
;
825 oberon_get_char(ctx
);
829 oberon_get_char(ctx
);
832 oberon_error(ctx
, "invalid char %c", ctx
-> c
);
838 oberon_read_token(oberon_context_t
* ctx
)
840 oberon_skip_space(ctx
);
845 oberon_read_ident(ctx
);
849 oberon_read_number(ctx
);
853 oberon_read_symbol(ctx
);
857 // =======================================================================
859 // =======================================================================
861 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
862 static oberon_expr_t
* oberon_expr(oberon_context_t
* ctx
);
863 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
864 static char * oberon_assert_ident(oberon_context_t
* ctx
);
865 static void oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
);
866 static oberon_item_t
* oberon_const_expr(oberon_context_t
* ctx
);
867 static oberon_expr_t
* oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
869 static oberon_expr_t
*
870 oberon_new_operator(int op
, oberon_type_t
* result
, oberon_expr_t
* left
, oberon_expr_t
* right
)
872 oberon_oper_t
* operator;
873 operator = malloc(sizeof *operator);
874 memset(operator, 0, sizeof *operator);
876 operator -> is_item
= 0;
877 operator -> result
= result
;
878 operator -> read_only
= 1;
880 operator -> left
= left
;
881 operator -> right
= right
;
883 return (oberon_expr_t
*) operator;
886 static oberon_expr_t
*
887 oberon_new_item(int mode
, oberon_type_t
* result
, int read_only
)
889 oberon_item_t
* item
;
890 item
= malloc(sizeof *item
);
891 memset(item
, 0, sizeof *item
);
894 item
-> result
= result
;
895 item
-> read_only
= read_only
;
898 return (oberon_expr_t
*)item
;
901 static oberon_expr_t
*
902 oberon_make_unary_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
)
904 oberon_expr_t
* expr
;
905 oberon_type_t
* result
;
907 result
= a
-> result
;
911 if(result
-> class == OBERON_TYPE_SET
)
913 expr
= oberon_new_operator(OP_COMPLEMENTATION
, result
, a
, NULL
);
915 else if(result
-> class == OBERON_TYPE_INTEGER
)
917 expr
= oberon_new_operator(OP_UNARY_MINUS
, result
, a
, NULL
);
921 oberon_error(ctx
, "incompatible operator type");
924 else if(token
== NOT
)
926 if(result
-> class != OBERON_TYPE_BOOLEAN
)
928 oberon_error(ctx
, "incompatible operator type");
931 expr
= oberon_new_operator(OP_LOGIC_NOT
, result
, a
, NULL
);
935 oberon_error(ctx
, "oberon_make_unary_op: wat");
942 oberon_expr_list(oberon_context_t
* ctx
, int * num_expr
, oberon_expr_t
** first
, int const_expr
)
944 oberon_expr_t
* last
;
949 *first
= last
= (oberon_expr_t
*) oberon_const_expr(ctx
);
953 *first
= last
= oberon_expr(ctx
);
955 while(ctx
-> token
== COMMA
)
957 oberon_assert_token(ctx
, COMMA
);
958 oberon_expr_t
* current
;
962 current
= (oberon_expr_t
*) oberon_const_expr(ctx
);
966 current
= oberon_expr(ctx
);
969 last
-> next
= current
;
975 static oberon_expr_t
*
976 oberon_cast_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
978 return oberon_new_operator(OP_CAST
, pref
, expr
, NULL
);
981 static oberon_expr_t
*
982 oberno_make_record_cast(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* rec
)
984 oberon_type_t
* from
= expr
-> result
;
985 oberon_type_t
* to
= rec
;
987 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
993 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
995 oberon_error(ctx
, "must be record type");
998 return oberon_cast_expr(ctx
, expr
, rec
);
1001 static oberon_type_t
*
1002 oberon_get_equal_expr_type(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
1004 oberon_type_t
* result
;
1005 if(a
-> class == OBERON_TYPE_REAL
&& b
-> class == OBERON_TYPE_INTEGER
)
1009 else if(b
-> class == OBERON_TYPE_REAL
&& a
-> class == OBERON_TYPE_INTEGER
)
1013 else if(a
-> class != b
-> class)
1015 oberon_error(ctx
, "oberon_get_equal_expr_type: incompatible types");
1017 else if(a
-> size
> b
-> size
)
1030 oberon_check_record_compatibility(oberon_context_t
* ctx
, oberon_type_t
* from
, oberon_type_t
* to
)
1032 if(from
-> class == OBERON_TYPE_POINTER
&& to
-> class == OBERON_TYPE_POINTER
)
1034 from
= from
-> base
;
1038 if(from
-> class != OBERON_TYPE_RECORD
|| to
-> class != OBERON_TYPE_RECORD
)
1040 oberon_error(ctx
, "not a record");
1043 oberon_type_t
* t
= from
;
1044 while(t
!= NULL
&& t
!= to
)
1051 oberon_error(ctx
, "incompatible record types");
1056 oberon_check_dst(oberon_context_t
* ctx
, oberon_expr_t
* dst
)
1058 if(dst
-> is_item
== false)
1060 oberon_error(ctx
, "not variable");
1063 switch(dst
-> item
.mode
)
1074 oberon_error(ctx
, "not variable");
1080 oberon_check_src(oberon_context_t
* ctx
, oberon_expr_t
* src
)
1084 if(src
-> item
.mode
== MODE_TYPE
)
1086 oberon_error(ctx
, "not variable");
1091 static oberon_expr_t
*
1092 oberon_autocast_to(oberon_context_t
* ctx
, oberon_expr_t
* expr
, oberon_type_t
* pref
)
1095 // Если классы типов равны
1096 // Если INTEGER переводится в REAL
1097 // Если STRING переводится в CHAR
1098 // Если STRING переводится в ARRAY OF CHAR
1099 // Если NIL переводится в POINTER
1100 // Если NIL переводится в PROCEDURE
1102 oberon_check_src(ctx
, expr
);
1105 if(pref
-> class != expr
-> result
-> class)
1107 if(expr
-> result
-> class == OBERON_TYPE_NIL
)
1109 if(pref
-> class != OBERON_TYPE_POINTER
1110 && pref
-> class != OBERON_TYPE_PROCEDURE
)
1115 else if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1117 if(pref
-> class == OBERON_TYPE_CHAR
)
1119 if(expr
-> is_item
&& expr
-> item
.mode
== MODE_STRING
)
1121 if(strlen(expr
-> item
.string
) != 1)
1131 else if(pref
-> class == OBERON_TYPE_ARRAY
)
1133 if(pref
-> base
-> class != OBERON_TYPE_CHAR
)
1143 else if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1145 if(pref
-> class != OBERON_TYPE_REAL
)
1158 oberon_error(ctx
, "oberon_autocast_to: incompatible types");
1161 if(pref
-> class == OBERON_TYPE_CHAR
)
1163 if(expr
-> result
-> class == OBERON_TYPE_STRING
)
1165 int c
= expr
-> item
.string
[0];
1166 expr
= oberon_new_item(MODE_CHAR
, ctx
-> char_type
, true);
1167 expr
-> item
.integer
= c
;
1170 else if(pref
-> class == OBERON_TYPE_INTEGER
|| pref
-> class == OBERON_TYPE_REAL
)
1172 if(expr
-> result
-> size
> pref
-> size
)
1174 oberon_error(ctx
, "incompatible size");
1178 expr
= oberon_cast_expr(ctx
, expr
, pref
);
1181 else if(pref
-> class == OBERON_TYPE_RECORD
)
1183 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1184 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1186 else if(pref
-> class == OBERON_TYPE_POINTER
)
1188 assert(pref
-> base
);
1189 if(expr
-> result
-> class == OBERON_TYPE_NIL
)
1193 else if(expr
-> result
-> base
-> class == OBERON_TYPE_RECORD
)
1195 oberon_check_record_compatibility(ctx
, expr
-> result
, pref
);
1196 expr
= oberno_make_record_cast(ctx
, expr
, pref
);
1198 else if(expr
-> result
-> base
!= pref
-> base
)
1200 oberon_error(ctx
, "incompatible pointer types");
1208 oberon_autocast_binary_op(oberon_context_t
* ctx
, oberon_expr_t
** ea
, oberon_expr_t
** eb
)
1210 oberon_type_t
* a
= (*ea
) -> result
;
1211 oberon_type_t
* b
= (*eb
) -> result
;
1212 oberon_type_t
* preq
= oberon_get_equal_expr_type(ctx
, a
, b
);
1213 *ea
= oberon_autocast_to(ctx
, *ea
, preq
);
1214 *eb
= oberon_autocast_to(ctx
, *eb
, preq
);
1218 oberon_autocast_call(oberon_context_t
* ctx
, oberon_item_t
* desig
)
1220 if(desig
-> mode
!= MODE_CALL
)
1222 oberon_error(ctx
, "expected mode CALL");
1225 oberon_type_t
* fn
= desig
-> parent
-> result
;
1226 int num_args
= desig
-> num_args
;
1227 int num_decl
= fn
-> num_decl
;
1229 if(num_args
< num_decl
)
1231 oberon_error(ctx
, "too few arguments");
1233 else if(num_args
> num_decl
)
1235 oberon_error(ctx
, "too many arguments");
1238 /* Делаем проверку на запись и делаем автокаст */
1239 oberon_expr_t
* casted
[num_args
];
1240 oberon_expr_t
* arg
= desig
-> args
;
1241 oberon_object_t
* param
= fn
-> decl
;
1242 for(int i
= 0; i
< num_args
; i
++)
1244 if(param
-> class == OBERON_CLASS_VAR_PARAM
)
1246 if(arg
-> result
!= param
-> type
)
1248 oberon_error(ctx
, "incompatible type");
1250 if(arg
-> read_only
)
1252 oberon_error(ctx
, "assign to read-only var");
1258 casted
[i
] = oberon_autocast_to(ctx
, arg
, param
-> type
);
1262 param
= param
-> next
;
1265 /* Создаём новый список выражений */
1269 for(int i
= 0; i
< num_args
- 1; i
++)
1271 casted
[i
] -> next
= casted
[i
+ 1];
1273 desig
-> args
= arg
;
1277 static oberon_expr_t
*
1278 oberon_make_call_func(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1280 oberon_type_t
* signature
= item
-> result
;
1281 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1283 oberon_error(ctx
, "not a procedure");
1286 oberon_expr_t
* call
;
1288 if(signature
-> sysproc
)
1290 if(signature
-> genfunc
== NULL
)
1292 oberon_error(ctx
, "not a function-procedure");
1295 call
= signature
-> genfunc(ctx
, num_args
, list_args
);
1299 if(signature
-> base
-> class == OBERON_TYPE_NOTYPE
)
1301 oberon_error(ctx
, "attempt to call procedure in expression");
1304 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1305 call
-> item
.parent
= item
;
1306 call
-> item
.num_args
= num_args
;
1307 call
-> item
.args
= list_args
;
1308 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1315 oberon_make_call_proc(oberon_context_t
* ctx
, oberon_item_t
* item
, int num_args
, oberon_expr_t
* list_args
)
1317 oberon_type_t
* signature
= item
-> result
;
1318 if(signature
-> class != OBERON_TYPE_PROCEDURE
)
1320 oberon_error(ctx
, "not a procedure");
1323 oberon_expr_t
* call
;
1325 if(signature
-> sysproc
)
1327 if(signature
-> genproc
== NULL
)
1329 oberon_error(ctx
, "not a procedure");
1332 signature
-> genproc(ctx
, num_args
, list_args
);
1336 if(signature
-> base
-> class != OBERON_TYPE_NOTYPE
)
1338 oberon_error(ctx
, "attempt to call function as non-typed procedure");
1341 call
= oberon_new_item(MODE_CALL
, signature
-> base
, true);
1342 call
-> item
.parent
= item
;
1343 call
-> item
.num_args
= num_args
;
1344 call
-> item
.args
= list_args
;
1345 oberon_autocast_call(ctx
, (oberon_item_t
*) call
);
1346 oberon_generate_call_proc(ctx
, call
);
1354 || ((x) == INTEGER) \
1357 || ((x) == STRING) \
1359 || ((x) == LPAREN) \
1362 static oberon_expr_t
*
1363 oberno_make_dereferencing(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1365 if(expr
-> result
-> class != OBERON_TYPE_POINTER
)
1367 oberon_error(ctx
, "not a pointer");
1370 assert(expr
-> is_item
);
1372 oberon_expr_t
* selector
;
1373 selector
= oberon_new_item(MODE_DEREF
, expr
-> result
-> base
, expr
-> read_only
);
1374 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1379 static oberon_expr_t
*
1380 oberon_make_array_selector(oberon_context_t
* ctx
, oberon_expr_t
* desig
, oberon_expr_t
* index
)
1382 if(desig
-> result
-> class == OBERON_TYPE_POINTER
)
1384 desig
= oberno_make_dereferencing(ctx
, desig
);
1387 assert(desig
-> is_item
);
1389 if(desig
-> result
-> class != OBERON_TYPE_ARRAY
)
1391 oberon_error(ctx
, "not array");
1394 oberon_type_t
* base
;
1395 base
= desig
-> result
-> base
;
1397 if(index
-> result
-> class != OBERON_TYPE_INTEGER
)
1399 oberon_error(ctx
, "index must be integer");
1402 // Статическая проверка границ массива
1403 if(desig
-> result
-> size
!= 0)
1405 if(index
-> is_item
)
1407 if(index
-> item
.mode
== MODE_INTEGER
)
1409 int arr_size
= desig
-> result
-> size
;
1410 int index_int
= index
-> item
.integer
;
1411 if(index_int
< 0 || index_int
> arr_size
- 1)
1413 oberon_error(ctx
, "not in range (dimension size 0..%i)", arr_size
- 1);
1419 oberon_expr_t
* selector
;
1420 selector
= oberon_new_item(MODE_INDEX
, base
, desig
-> read_only
);
1421 selector
-> item
.parent
= (oberon_item_t
*) desig
;
1422 selector
-> item
.num_args
= 1;
1423 selector
-> item
.args
= index
;
1428 static oberon_expr_t
*
1429 oberon_make_record_selector(oberon_context_t
* ctx
, oberon_expr_t
* expr
, char * name
)
1431 if(expr
-> result
-> class == OBERON_TYPE_POINTER
)
1433 expr
= oberno_make_dereferencing(ctx
, expr
);
1436 assert(expr
-> is_item
);
1438 if(expr
-> result
-> class != OBERON_TYPE_RECORD
)
1440 oberon_error(ctx
, "not record");
1443 oberon_type_t
* rec
= expr
-> result
;
1445 oberon_object_t
* field
;
1446 field
= oberon_find_object(rec
-> scope
, name
, true);
1448 if(field
-> export
== 0)
1450 if(field
-> module
!= ctx
-> mod
)
1452 oberon_error(ctx
, "field not exported");
1457 if(field
-> read_only
)
1459 if(field
-> module
!= ctx
-> mod
)
1465 oberon_expr_t
* selector
;
1466 selector
= oberon_new_item(MODE_FIELD
, field
-> type
, read_only
);
1467 selector
-> item
.var
= field
;
1468 selector
-> item
.parent
= (oberon_item_t
*) expr
;
1473 #define ISSELECTOR(x) \
1476 || ((x) == UPARROW) \
1479 static oberon_object_t
*
1480 oberon_qualident(oberon_context_t
* ctx
, char ** xname
, int check
)
1483 oberon_object_t
* x
;
1485 name
= oberon_assert_ident(ctx
);
1486 x
= oberon_find_object(ctx
-> decl
, name
, check
);
1490 if(x
-> class == OBERON_CLASS_MODULE
)
1492 oberon_assert_token(ctx
, DOT
);
1493 name
= oberon_assert_ident(ctx
);
1494 /* Наличие объектов в левых модулях всегда проверяется */
1495 x
= oberon_find_object(x
-> module
-> decl
, name
, 1);
1497 if(x
-> export
== 0)
1499 oberon_error(ctx
, "not exported");
1512 static oberon_expr_t
*
1513 oberon_ident_item(oberon_context_t
* ctx
, char * name
)
1516 oberon_object_t
* x
;
1517 oberon_expr_t
* expr
;
1519 x
= oberon_find_object(ctx
-> decl
, name
, true);
1522 if(x
-> class == OBERON_CLASS_CONST
|| x
-> class == OBERON_CLASS_PROC
)
1527 expr
= oberon_new_item(MODE_VAR
, x
-> type
, read_only
);
1528 expr
-> item
.var
= x
;
1532 static oberon_expr_t
*
1533 oberon_qualident_expr(oberon_context_t
* ctx
)
1535 oberon_object_t
* var
;
1536 oberon_expr_t
* expr
;
1538 var
= oberon_qualident(ctx
, NULL
, 1);
1541 if(var
-> read_only
)
1543 if(var
-> module
!= ctx
-> mod
)
1549 switch(var
-> class)
1551 case OBERON_CLASS_CONST
:
1553 expr
= (oberon_expr_t
*) var
-> value
;
1555 case OBERON_CLASS_TYPE
:
1556 expr
= oberon_new_item(MODE_TYPE
, var
-> type
, read_only
);
1558 case OBERON_CLASS_VAR
:
1559 case OBERON_CLASS_VAR_PARAM
:
1560 case OBERON_CLASS_PARAM
:
1561 expr
= oberon_new_item(MODE_VAR
, var
-> type
, read_only
);
1563 case OBERON_CLASS_PROC
:
1564 expr
= oberon_new_item(MODE_VAR
, var
-> type
, true);
1567 oberon_error(ctx
, "invalid designator");
1571 expr
-> item
.var
= var
;
1576 static oberon_expr_t
*
1577 oberon_designator(oberon_context_t
* ctx
)
1580 oberon_expr_t
* expr
;
1582 expr
= oberon_qualident_expr(ctx
);
1584 while(expr
-> result
-> class != OBERON_TYPE_PROCEDURE
&& ISSELECTOR(ctx
-> token
))
1586 switch(ctx
-> token
)
1589 oberon_assert_token(ctx
, DOT
);
1590 name
= oberon_assert_ident(ctx
);
1591 expr
= oberon_make_record_selector(ctx
, expr
, name
);
1594 oberon_assert_token(ctx
, LBRACK
);
1595 int num_indexes
= 0;
1596 oberon_expr_t
* indexes
= NULL
;
1597 oberon_expr_list(ctx
, &num_indexes
, &indexes
, 0);
1598 oberon_assert_token(ctx
, RBRACK
);
1600 for(int i
= 0; i
< num_indexes
; i
++)
1602 expr
= oberon_make_array_selector(ctx
, expr
, indexes
);
1603 indexes
= indexes
-> next
;
1607 oberon_assert_token(ctx
, UPARROW
);
1608 expr
= oberno_make_dereferencing(ctx
, expr
);
1611 oberon_assert_token(ctx
, LPAREN
);
1612 oberon_object_t
* objtype
= oberon_qualident(ctx
, NULL
, 1);
1613 if(objtype
-> class != OBERON_CLASS_TYPE
)
1615 oberon_error(ctx
, "must be type");
1617 oberon_assert_token(ctx
, RPAREN
);
1618 expr
= oberno_make_record_cast(ctx
, expr
, objtype
-> type
);
1621 oberon_error(ctx
, "oberon_designator: wat");
1629 static oberon_expr_t
*
1630 oberon_opt_func_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1632 /* Если есть скобки - значит вызов. Если нет, то передаём указатель. */
1633 if(ctx
-> token
== LPAREN
)
1635 oberon_assert_token(ctx
, LPAREN
);
1638 oberon_expr_t
* arguments
= NULL
;
1640 if(ISEXPR(ctx
-> token
))
1642 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1645 assert(expr
-> is_item
== 1);
1646 expr
= oberon_make_call_func(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1648 oberon_assert_token(ctx
, RPAREN
);
1655 oberon_opt_proc_parens(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
1657 assert(expr
-> is_item
);
1660 oberon_expr_t
* arguments
= NULL
;
1662 if(ctx
-> token
== LPAREN
)
1664 oberon_assert_token(ctx
, LPAREN
);
1666 if(ISEXPR(ctx
-> token
))
1668 oberon_expr_list(ctx
, &num_args
, &arguments
, 0);
1671 oberon_assert_token(ctx
, RPAREN
);
1674 /* Вызов происходит даже без скобок */
1675 oberon_make_call_proc(ctx
, (oberon_item_t
*) expr
, num_args
, arguments
);
1678 static oberon_type_t
*
1679 oberon_get_type_of_int_value(oberon_context_t
* ctx
, int64_t i
)
1681 if(i
>= -128 && i
<= 127)
1683 return ctx
-> byte_type
;
1685 else if(i
>= -32768 && i
<= 32767)
1687 return ctx
-> shortint_type
;
1689 else if(i
>= -2147483648 && i
<= 2147483647)
1691 return ctx
-> int_type
;
1695 return ctx
-> longint_type
;
1699 static oberon_expr_t
*
1700 oberon_integer_item(oberon_context_t
* ctx
, int64_t i
)
1702 oberon_expr_t
* expr
;
1703 oberon_type_t
* result
;
1704 result
= oberon_get_type_of_int_value(ctx
, i
);
1705 expr
= oberon_new_item(MODE_INTEGER
, result
, true);
1706 expr
-> item
.integer
= i
;
1710 static oberon_expr_t
*
1711 oberon_element(oberon_context_t
* ctx
)
1716 e1
= oberon_expr(ctx
);
1717 if(e1
-> result
-> class != OBERON_TYPE_INTEGER
)
1719 oberon_error(ctx
, "expected integer");
1723 if(ctx
-> token
== DOTDOT
)
1725 oberon_assert_token(ctx
, DOTDOT
);
1726 e2
= oberon_expr(ctx
);
1727 if(e2
-> result
-> class != OBERON_TYPE_INTEGER
)
1729 oberon_error(ctx
, "expected integer");
1733 oberon_expr_t
* set
;
1734 set
= oberon_new_operator(OP_RANGE
, ctx
-> set_type
, e1
, e2
);
1738 static oberon_expr_t
*
1739 oberon_set(oberon_context_t
* ctx
)
1741 oberon_expr_t
* set
;
1742 oberon_expr_t
* elements
;
1743 set
= oberon_new_item(MODE_SET
, ctx
-> set_type
, true);
1744 set
-> item
.integer
= 0;
1746 oberon_assert_token(ctx
, LBRACE
);
1747 if(ISEXPR(ctx
-> token
))
1749 elements
= oberon_element(ctx
);
1750 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1751 while(ctx
-> token
== COMMA
)
1753 oberon_assert_token(ctx
, COMMA
);
1754 elements
= oberon_element(ctx
);
1755 set
= oberon_new_operator(OP_UNION
, ctx
-> set_type
, set
, elements
);
1758 oberon_assert_token(ctx
, RBRACE
);
1763 static oberon_expr_t
*
1764 oberon_make_boolean(oberon_context_t
* ctx
, bool cond
)
1766 oberon_expr_t
* expr
;
1767 expr
= oberon_new_item(MODE_BOOLEAN
, ctx
-> bool_type
, true);
1768 expr
-> item
.integer
= cond
;
1772 static oberon_expr_t
*
1773 oberon_factor(oberon_context_t
* ctx
)
1775 oberon_expr_t
* expr
;
1776 oberon_type_t
* result
;
1778 switch(ctx
-> token
)
1781 expr
= oberon_designator(ctx
);
1782 expr
= oberon_opt_func_parens(ctx
, expr
);
1785 expr
= oberon_integer_item(ctx
, ctx
-> integer
);
1786 oberon_assert_token(ctx
, INTEGER
);
1789 result
= ctx
-> char_type
;
1790 expr
= oberon_new_item(MODE_CHAR
, result
, true);
1791 expr
-> item
.integer
= ctx
-> integer
;
1792 oberon_assert_token(ctx
, CHAR
);
1795 result
= ctx
-> string_type
;
1796 expr
= oberon_new_item(MODE_STRING
, result
, true);
1797 expr
-> item
.string
= ctx
-> string
;
1798 oberon_assert_token(ctx
, STRING
);
1801 result
= (ctx
-> longmode
) ? (ctx
-> longreal_type
) : (ctx
-> real_type
);
1802 expr
= oberon_new_item(MODE_REAL
, result
, 1);
1803 expr
-> item
.real
= ctx
-> real
;
1804 oberon_assert_token(ctx
, REAL
);
1807 expr
= oberon_set(ctx
);
1810 oberon_assert_token(ctx
, LPAREN
);
1811 expr
= oberon_expr(ctx
);
1812 oberon_assert_token(ctx
, RPAREN
);
1815 oberon_assert_token(ctx
, NOT
);
1816 expr
= oberon_factor(ctx
);
1817 expr
= oberon_make_unary_op(ctx
, NOT
, expr
);
1820 oberon_assert_token(ctx
, NIL
);
1821 expr
= oberon_new_item(MODE_NIL
, ctx
-> nil_type
, true);
1824 oberon_error(ctx
, "invalid expression");
1830 #define ITMAKESBOOLEAN(x) \
1831 (((x) >= EQUAL && (x) <= GEQ) || ((x) == OR) || ((x) == AND))
1833 #define ITUSEONLYINTEGER(x) \
1834 ((x) >= LESS && (x) <= GEQ)
1836 #define ITUSEONLYBOOLEAN(x) \
1837 (((x) == OR) || ((x) == AND))
1840 oberon_autocast_to_real(oberon_context_t
* ctx
, oberon_expr_t
** e
)
1842 oberon_expr_t
* expr
= *e
;
1843 if(expr
-> result
-> class == OBERON_TYPE_INTEGER
)
1845 if(expr
-> result
-> size
<= ctx
-> real_type
-> size
)
1847 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> real_type
);
1851 *e
= oberon_cast_expr(ctx
, expr
, ctx
-> longreal_type
);
1854 else if(expr
-> result
-> class != OBERON_TYPE_REAL
)
1856 oberon_error(ctx
, "required numeric type");
1860 static oberon_expr_t
*
1861 oberon_make_bin_op(oberon_context_t
* ctx
, int token
, oberon_expr_t
* a
, oberon_expr_t
* b
)
1863 oberon_expr_t
* expr
;
1864 oberon_type_t
* result
;
1869 if(a
-> result
-> class != OBERON_TYPE_INTEGER
)
1871 oberon_error(ctx
, "must be integer");
1874 if(b
-> result
-> class != OBERON_TYPE_SET
)
1876 oberon_error(ctx
, "must be set");
1879 result
= ctx
-> bool_type
;
1880 expr
= oberon_new_operator(OP_IN
, result
, a
, b
);
1882 else if(token
== IS
)
1884 oberon_type_t
* v
= a
-> result
;
1885 if(v
-> class == OBERON_TYPE_POINTER
)
1888 if(v
-> class != OBERON_TYPE_RECORD
)
1890 oberon_error(ctx
, "must be record");
1893 else if(v
-> class != OBERON_TYPE_RECORD
)
1895 oberon_error(ctx
, "must be record");
1898 if(b
-> is_item
== false || b
-> item
.mode
!= MODE_TYPE
)
1900 oberon_error(ctx
, "requires type");
1903 oberon_type_t
* t
= b
-> result
;
1904 if(t
-> class == OBERON_TYPE_POINTER
)
1907 if(t
-> class != OBERON_TYPE_RECORD
)
1909 oberon_error(ctx
, "must be record");
1912 else if(t
-> class != OBERON_TYPE_RECORD
)
1914 oberon_error(ctx
, "must be record");
1917 result
= ctx
-> bool_type
;
1918 expr
= oberon_new_operator(OP_IS
, result
, a
, b
);
1920 else if(ITMAKESBOOLEAN(token
))
1922 if(ITUSEONLYINTEGER(token
))
1924 if(a
-> result
-> class == OBERON_TYPE_INTEGER
1925 || b
-> result
-> class == OBERON_TYPE_INTEGER
1926 || a
-> result
-> class == OBERON_TYPE_REAL
1927 || b
-> result
-> class == OBERON_TYPE_REAL
)
1933 oberon_error(ctx
, "used only with numeric types");
1936 else if(ITUSEONLYBOOLEAN(token
))
1938 if(a
-> result
-> class != OBERON_TYPE_BOOLEAN
1939 || b
-> result
-> class != OBERON_TYPE_BOOLEAN
)
1941 oberon_error(ctx
, "used only with boolean type");
1945 oberon_autocast_binary_op(ctx
, &a
, &b
);
1946 result
= ctx
-> bool_type
;
1950 expr
= oberon_new_operator(OP_EQ
, result
, a
, b
);
1952 else if(token
== NEQ
)
1954 expr
= oberon_new_operator(OP_NEQ
, result
, a
, b
);
1956 else if(token
== LESS
)
1958 expr
= oberon_new_operator(OP_LSS
, result
, a
, b
);
1960 else if(token
== LEQ
)
1962 expr
= oberon_new_operator(OP_LEQ
, result
, a
, b
);
1964 else if(token
== GREAT
)
1966 expr
= oberon_new_operator(OP_GRT
, result
, a
, b
);
1968 else if(token
== GEQ
)
1970 expr
= oberon_new_operator(OP_GEQ
, result
, a
, b
);
1972 else if(token
== OR
)
1974 expr
= oberon_new_operator(OP_LOGIC_OR
, result
, a
, b
);
1976 else if(token
== AND
)
1978 expr
= oberon_new_operator(OP_LOGIC_AND
, result
, a
, b
);
1982 oberon_error(ctx
, "oberon_make_bin_op: bool wat");
1985 else if(token
== SLASH
)
1987 if(a
-> result
-> class == OBERON_TYPE_SET
1988 || b
-> result
-> class == OBERON_TYPE_SET
)
1990 oberon_autocast_binary_op(ctx
, &a
, &b
);
1991 result
= a
-> result
;
1992 expr
= oberon_new_operator(OP_SYM_DIFFERENCE
, result
, a
, b
);
1996 oberon_autocast_to_real(ctx
, &a
);
1997 oberon_autocast_to_real(ctx
, &b
);
1998 oberon_autocast_binary_op(ctx
, &a
, &b
);
1999 result
= a
-> result
;
2000 expr
= oberon_new_operator(OP_DIV
, result
, a
, b
);
2003 else if(token
== DIV
)
2005 if(a
-> result
-> class != OBERON_TYPE_INTEGER
2006 || b
-> result
-> class != OBERON_TYPE_INTEGER
)
2008 oberon_error(ctx
, "operator DIV requires integer type");
2011 oberon_autocast_binary_op(ctx
, &a
, &b
);
2012 expr
= oberon_new_operator(OP_DIV
, a
-> result
, a
, b
);
2016 oberon_autocast_binary_op(ctx
, &a
, &b
);
2017 result
= a
-> result
;
2018 if(result
-> class == OBERON_TYPE_SET
)
2023 expr
= oberon_new_operator(OP_UNION
, result
, a
, b
);
2026 expr
= oberon_new_operator(OP_DIFFERENCE
, result
, a
, b
);
2029 expr
= oberon_new_operator(OP_INTERSECTION
, result
, a
, b
);
2036 else if(result
-> class == OBERON_TYPE_INTEGER
2037 || result
-> class == OBERON_TYPE_REAL
)
2042 expr
= oberon_new_operator(OP_ADD
, result
, a
, b
);
2045 expr
= oberon_new_operator(OP_SUB
, result
, a
, b
);
2048 expr
= oberon_new_operator(OP_MUL
, result
, a
, b
);
2051 expr
= oberon_new_operator(OP_MOD
, result
, a
, b
);
2066 oberon_error(ctx
, "invalid operation");
2072 #define ISMULOP(x) \
2073 ((x) >= STAR && (x) <= AND)
2075 static oberon_expr_t
*
2076 oberon_term_expr(oberon_context_t
* ctx
)
2078 oberon_expr_t
* expr
;
2080 expr
= oberon_factor(ctx
);
2081 while(ISMULOP(ctx
-> token
))
2083 int token
= ctx
-> token
;
2084 oberon_read_token(ctx
);
2086 oberon_expr_t
* inter
= oberon_factor(ctx
);
2087 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2093 #define ISADDOP(x) \
2094 ((x) >= PLUS && (x) <= OR)
2096 static oberon_expr_t
*
2097 oberon_simple_expr(oberon_context_t
* ctx
)
2099 oberon_expr_t
* expr
;
2102 if(ctx
-> token
== PLUS
)
2105 oberon_assert_token(ctx
, PLUS
);
2107 else if(ctx
-> token
== MINUS
)
2110 oberon_assert_token(ctx
, MINUS
);
2113 expr
= oberon_term_expr(ctx
);
2117 expr
= oberon_make_unary_op(ctx
, MINUS
, expr
);
2120 while(ISADDOP(ctx
-> token
))
2122 int token
= ctx
-> token
;
2123 oberon_read_token(ctx
);
2125 oberon_expr_t
* inter
= oberon_term_expr(ctx
);
2126 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2132 #define ISRELATION(x) \
2133 ((x) >= EQUAL && (x) <= IS)
2135 static oberon_expr_t
*
2136 oberon_expr(oberon_context_t
* ctx
)
2138 oberon_expr_t
* expr
;
2140 expr
= oberon_simple_expr(ctx
);
2141 while(ISRELATION(ctx
-> token
))
2143 int token
= ctx
-> token
;
2144 oberon_read_token(ctx
);
2146 oberon_expr_t
* inter
= oberon_simple_expr(ctx
);
2147 expr
= oberon_make_bin_op(ctx
, token
, expr
, inter
);
2154 oberon_check_const(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2156 if(expr
-> is_item
== 0)
2158 oberon_error(ctx
, "const expression are required");
2161 switch(expr
-> item
.mode
)
2173 oberon_error(ctx
, "const expression are required");
2178 static oberon_item_t
*
2179 oberon_const_expr(oberon_context_t
* ctx
)
2181 oberon_expr_t
* expr
;
2182 expr
= oberon_expr(ctx
);
2183 oberon_check_const(ctx
, expr
);
2184 return (oberon_item_t
*) expr
;
2187 // =======================================================================
2189 // =======================================================================
2191 static void oberon_decl_seq(oberon_context_t
* ctx
);
2192 static void oberon_statement_seq(oberon_context_t
* ctx
);
2193 static void oberon_initialize_decl(oberon_context_t
* ctx
);
2196 oberon_expect_token(oberon_context_t
* ctx
, int token
)
2198 if(ctx
-> token
!= token
)
2200 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
2205 oberon_assert_token(oberon_context_t
* ctx
, int token
)
2207 oberon_expect_token(ctx
, token
);
2208 oberon_read_token(ctx
);
2212 oberon_assert_ident(oberon_context_t
* ctx
)
2214 oberon_expect_token(ctx
, IDENT
);
2215 char * ident
= ctx
-> string
;
2216 oberon_read_token(ctx
);
2221 oberon_def(oberon_context_t
* ctx
, int * export
, int * read_only
)
2223 switch(ctx
-> token
)
2226 oberon_assert_token(ctx
, STAR
);
2231 oberon_assert_token(ctx
, MINUS
);
2242 static oberon_object_t
*
2243 oberon_ident_def(oberon_context_t
* ctx
, int class, bool check_upscope
)
2248 oberon_object_t
* x
;
2250 name
= oberon_assert_ident(ctx
);
2251 oberon_def(ctx
, &export
, &read_only
);
2253 x
= oberon_define_object(ctx
-> decl
, name
, class, export
, read_only
, check_upscope
);
2258 oberon_ident_list(oberon_context_t
* ctx
, int class, bool check_upscope
, int * num
, oberon_object_t
** list
)
2261 *list
= oberon_ident_def(ctx
, class, check_upscope
);
2262 while(ctx
-> token
== COMMA
)
2264 oberon_assert_token(ctx
, COMMA
);
2265 oberon_ident_def(ctx
, class, check_upscope
);
2271 oberon_var_decl(oberon_context_t
* ctx
)
2274 oberon_object_t
* list
;
2275 oberon_type_t
* type
;
2276 type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2278 oberon_ident_list(ctx
, OBERON_CLASS_VAR
, false, &num
, &list
);
2279 oberon_assert_token(ctx
, COLON
);
2280 oberon_type(ctx
, &type
);
2282 oberon_object_t
* var
= list
;
2283 for(int i
= 0; i
< num
; i
++)
2290 static oberon_object_t
*
2291 oberon_fp_section(oberon_context_t
* ctx
, int * num_decl
)
2293 int class = OBERON_CLASS_PARAM
;
2294 if(ctx
-> token
== VAR
)
2296 oberon_read_token(ctx
);
2297 class = OBERON_CLASS_VAR_PARAM
;
2301 oberon_object_t
* list
;
2302 oberon_ident_list(ctx
, class, false, &num
, &list
);
2304 oberon_assert_token(ctx
, COLON
);
2306 oberon_type_t
* type
;
2307 type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2308 oberon_type(ctx
, &type
);
2310 oberon_object_t
* param
= list
;
2311 for(int i
= 0; i
< num
; i
++)
2313 param
-> type
= type
;
2314 param
= param
-> next
;
2321 #define ISFPSECTION \
2322 ((ctx -> token == VAR) || (ctx -> token == IDENT))
2325 oberon_formal_pars(oberon_context_t
* ctx
, oberon_type_t
* signature
)
2327 oberon_assert_token(ctx
, LPAREN
);
2331 signature
-> decl
= oberon_fp_section(ctx
, &signature
-> num_decl
);
2332 while(ctx
-> token
== SEMICOLON
)
2334 oberon_assert_token(ctx
, SEMICOLON
);
2335 oberon_fp_section(ctx
, &signature
-> num_decl
);
2339 oberon_assert_token(ctx
, RPAREN
);
2341 if(ctx
-> token
== COLON
)
2343 oberon_assert_token(ctx
, COLON
);
2345 oberon_object_t
* typeobj
;
2346 typeobj
= oberon_qualident(ctx
, NULL
, 1);
2347 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2349 oberon_error(ctx
, "function result is not type");
2351 signature
-> base
= typeobj
-> type
;
2356 oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
)
2358 oberon_type_t
* signature
;
2360 signature
-> class = OBERON_TYPE_PROCEDURE
;
2361 signature
-> num_decl
= 0;
2362 signature
-> base
= ctx
-> notype_type
;
2363 signature
-> decl
= NULL
;
2365 if(ctx
-> token
== LPAREN
)
2367 oberon_formal_pars(ctx
, signature
);
2372 oberon_compare_signatures(oberon_context_t
* ctx
, oberon_type_t
* a
, oberon_type_t
* b
)
2374 if(a
-> num_decl
!= b
-> num_decl
)
2376 oberon_error(ctx
, "number parameters not matched");
2379 int num_param
= a
-> num_decl
;
2380 oberon_object_t
* param_a
= a
-> decl
;
2381 oberon_object_t
* param_b
= b
-> decl
;
2382 for(int i
= 0; i
< num_param
; i
++)
2384 if(strcmp(param_a
-> name
, param_b
-> name
) != 0)
2386 oberon_error(ctx
, "param %i name not matched", i
+ 1);
2389 if(param_a
-> type
!= param_b
-> type
)
2391 oberon_error(ctx
, "param %i type not matched", i
+ 1);
2394 param_a
= param_a
-> next
;
2395 param_b
= param_b
-> next
;
2400 oberon_make_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
2402 oberon_object_t
* proc
= ctx
-> decl
-> parent
;
2403 oberon_type_t
* result_type
= proc
-> type
-> base
;
2405 if(result_type
-> class == OBERON_TYPE_NOTYPE
)
2409 oberon_error(ctx
, "procedure has no result type");
2416 oberon_error(ctx
, "procedure requires expression on result");
2419 expr
= oberon_autocast_to(ctx
, expr
, result_type
);
2422 proc
-> has_return
= 1;
2424 oberon_generate_return(ctx
, expr
);
2428 oberon_proc_decl_body(oberon_context_t
* ctx
, oberon_object_t
* proc
)
2430 oberon_assert_token(ctx
, SEMICOLON
);
2432 ctx
-> decl
= proc
-> scope
;
2434 oberon_decl_seq(ctx
);
2436 oberon_generate_begin_proc(ctx
, proc
);
2438 if(ctx
-> token
== BEGIN
)
2440 oberon_assert_token(ctx
, BEGIN
);
2441 oberon_statement_seq(ctx
);
2444 oberon_assert_token(ctx
, END
);
2445 char * name
= oberon_assert_ident(ctx
);
2446 if(strcmp(name
, proc
-> name
) != 0)
2448 oberon_error(ctx
, "procedure name not matched");
2451 if(proc
-> type
-> base
-> class == OBERON_TYPE_NOTYPE
2452 && proc
-> has_return
== 0)
2454 oberon_make_return(ctx
, NULL
);
2457 if(proc
-> has_return
== 0)
2459 oberon_error(ctx
, "procedure requires return");
2462 oberon_generate_end_proc(ctx
);
2463 oberon_close_scope(ctx
-> decl
);
2467 oberon_proc_decl(oberon_context_t
* ctx
)
2469 oberon_assert_token(ctx
, PROCEDURE
);
2472 if(ctx
-> token
== UPARROW
)
2474 oberon_assert_token(ctx
, UPARROW
);
2481 name
= oberon_assert_ident(ctx
);
2482 oberon_def(ctx
, &export
, &read_only
);
2484 oberon_scope_t
* proc_scope
;
2485 proc_scope
= oberon_open_scope(ctx
);
2486 ctx
-> decl
-> local
= 1;
2488 oberon_type_t
* signature
;
2489 signature
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2490 oberon_opt_formal_pars(ctx
, &signature
);
2492 //oberon_initialize_decl(ctx);
2493 oberon_generator_init_type(ctx
, signature
);
2494 oberon_close_scope(ctx
-> decl
);
2496 oberon_object_t
* proc
;
2497 proc
= oberon_find_object(ctx
-> decl
, name
, 0);
2500 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, export
, read_only
, false);
2501 proc
-> type
= signature
;
2502 proc
-> scope
= proc_scope
;
2503 oberon_generator_init_proc(ctx
, proc
);
2507 if(proc
-> class != OBERON_CLASS_PROC
)
2509 oberon_error(ctx
, "mult definition");
2516 oberon_error(ctx
, "mult procedure definition");
2520 if(proc
-> export
!= export
|| proc
-> read_only
!= read_only
)
2522 oberon_error(ctx
, "export type not matched");
2525 oberon_compare_signatures(ctx
, proc
-> type
, signature
);
2528 proc_scope
-> parent
= proc
;
2529 oberon_object_t
* param
= proc_scope
-> list
-> next
;
2532 param
-> parent
= proc
;
2533 param
= param
-> next
;
2539 oberon_proc_decl_body(ctx
, proc
);
2544 oberon_const_decl(oberon_context_t
* ctx
)
2546 oberon_item_t
* value
;
2547 oberon_object_t
* constant
;
2549 constant
= oberon_ident_def(ctx
, OBERON_CLASS_CONST
, false);
2550 oberon_assert_token(ctx
, EQUAL
);
2551 value
= oberon_const_expr(ctx
);
2552 constant
-> value
= value
;
2556 oberon_make_array_type(oberon_context_t
* ctx
, oberon_expr_t
* size
, oberon_type_t
* base
, oberon_type_t
** type
)
2558 if(size
-> is_item
== 0)
2560 oberon_error(ctx
, "requires constant");
2563 if(size
-> item
.mode
!= MODE_INTEGER
)
2565 oberon_error(ctx
, "requires integer constant");
2568 oberon_type_t
* arr
;
2570 arr
-> class = OBERON_TYPE_ARRAY
;
2571 arr
-> size
= size
-> item
.integer
;
2576 oberon_qualident_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2579 oberon_object_t
* to
;
2581 to
= oberon_qualident(ctx
, &name
, 0);
2583 //name = oberon_assert_ident(ctx);
2584 //to = oberon_find_object(ctx -> decl, name, 0);
2588 if(to
-> class != OBERON_CLASS_TYPE
)
2590 oberon_error(ctx
, "not a type");
2595 to
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, false, false, false);
2596 to
-> type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2602 static void oberon_opt_formal_pars(oberon_context_t
* ctx
, oberon_type_t
** type
);
2605 * Правило граматики "type". Указатель type должен указывать на существующий объект!
2609 oberon_make_multiarray(oberon_context_t
* ctx
, oberon_expr_t
* sizes
, oberon_type_t
* base
, oberon_type_t
** type
)
2617 oberon_type_t
* dim
;
2618 dim
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2620 oberon_make_multiarray(ctx
, sizes
-> next
, base
, &dim
);
2622 oberon_make_array_type(ctx
, sizes
, dim
, type
);
2626 oberon_make_open_array(oberon_context_t
* ctx
, oberon_type_t
* base
, oberon_type_t
* type
)
2628 type
-> class = OBERON_TYPE_ARRAY
;
2630 type
-> base
= base
;
2634 oberon_field_list(oberon_context_t
* ctx
, oberon_type_t
* rec
, oberon_scope_t
* modscope
)
2636 if(ctx
-> token
== IDENT
)
2639 oberon_object_t
* list
;
2640 oberon_type_t
* type
;
2641 type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2643 oberon_ident_list(ctx
, OBERON_CLASS_FIELD
, true, &num
, &list
);
2644 oberon_assert_token(ctx
, COLON
);
2646 oberon_scope_t
* current
= ctx
-> decl
;
2647 ctx
-> decl
= modscope
;
2648 oberon_type(ctx
, &type
);
2649 ctx
-> decl
= current
;
2651 oberon_object_t
* field
= list
;
2652 for(int i
= 0; i
< num
; i
++)
2654 field
-> type
= type
;
2655 field
= field
-> next
;
2658 rec
-> num_decl
+= num
;
2663 oberon_type_record_body(oberon_context_t
* ctx
, oberon_type_t
* rec
)
2665 oberon_scope_t
* modscope
= ctx
-> mod
-> decl
;
2666 oberon_scope_t
* oldscope
= ctx
-> decl
;
2667 ctx
-> decl
= modscope
;
2669 if(ctx
-> token
== LPAREN
)
2671 oberon_assert_token(ctx
, LPAREN
);
2673 oberon_object_t
* typeobj
;
2674 typeobj
= oberon_qualident(ctx
, NULL
, true);
2676 if(typeobj
-> class != OBERON_CLASS_TYPE
)
2678 oberon_error(ctx
, "base must be type");
2681 oberon_type_t
* base
= typeobj
-> type
;
2682 if(base
-> class == OBERON_TYPE_POINTER
)
2684 base
= base
-> base
;
2687 if(base
-> class != OBERON_TYPE_RECORD
)
2689 oberon_error(ctx
, "base must be record type");
2693 ctx
-> decl
= base
-> scope
;
2695 oberon_assert_token(ctx
, RPAREN
);
2702 oberon_scope_t
* this_scope
;
2703 this_scope
= oberon_open_scope(ctx
);
2704 this_scope
-> local
= true;
2705 this_scope
-> parent
= NULL
;
2706 this_scope
-> parent_type
= rec
;
2708 oberon_field_list(ctx
, rec
, modscope
);
2709 while(ctx
-> token
== SEMICOLON
)
2711 oberon_assert_token(ctx
, SEMICOLON
);
2712 oberon_field_list(ctx
, rec
, modscope
);
2715 rec
-> scope
= this_scope
;
2716 rec
-> decl
= this_scope
-> list
-> next
;
2717 ctx
-> decl
= oldscope
;
2721 oberon_type(oberon_context_t
* ctx
, oberon_type_t
** type
)
2723 if(ctx
-> token
== IDENT
)
2725 oberon_qualident_type(ctx
, type
);
2727 else if(ctx
-> token
== ARRAY
)
2729 oberon_assert_token(ctx
, ARRAY
);
2732 oberon_expr_t
* sizes
;
2734 if(ISEXPR(ctx
-> token
))
2736 oberon_expr_list(ctx
, &num_sizes
, &sizes
, 1);
2739 oberon_assert_token(ctx
, OF
);
2741 oberon_type_t
* base
;
2742 base
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2743 oberon_type(ctx
, &base
);
2747 oberon_make_open_array(ctx
, base
, *type
);
2751 oberon_make_multiarray(ctx
, sizes
, base
, type
);
2754 else if(ctx
-> token
== RECORD
)
2756 oberon_type_t
* rec
;
2758 rec
-> class = OBERON_TYPE_RECORD
;
2759 rec
-> module
= ctx
-> mod
;
2761 oberon_assert_token(ctx
, RECORD
);
2762 oberon_type_record_body(ctx
, rec
);
2763 oberon_assert_token(ctx
, END
);
2767 else if(ctx
-> token
== POINTER
)
2769 oberon_assert_token(ctx
, POINTER
);
2770 oberon_assert_token(ctx
, TO
);
2772 oberon_type_t
* base
;
2773 base
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2774 oberon_type(ctx
, &base
);
2776 oberon_type_t
* ptr
;
2778 ptr
-> class = OBERON_TYPE_POINTER
;
2781 else if(ctx
-> token
== PROCEDURE
)
2783 oberon_open_scope(ctx
);
2784 oberon_assert_token(ctx
, PROCEDURE
);
2785 oberon_opt_formal_pars(ctx
, type
);
2786 oberon_close_scope(ctx
-> decl
);
2790 oberon_error(ctx
, "invalid type declaration");
2795 oberon_type_decl(oberon_context_t
* ctx
)
2798 oberon_object_t
* newtype
;
2799 oberon_type_t
* type
;
2803 name
= oberon_assert_ident(ctx
);
2804 oberon_def(ctx
, &export
, &read_only
);
2806 newtype
= oberon_find_object(ctx
-> decl
, name
, 0);
2809 newtype
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_TYPE
, export
, read_only
, false);
2810 newtype
-> type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
2811 assert(newtype
-> type
);
2815 if(newtype
-> class != OBERON_CLASS_TYPE
)
2817 oberon_error(ctx
, "mult definition");
2820 if(newtype
-> linked
)
2822 oberon_error(ctx
, "mult definition - already linked");
2825 newtype
-> export
= export
;
2826 newtype
-> read_only
= read_only
;
2829 oberon_assert_token(ctx
, EQUAL
);
2831 type
= newtype
-> type
;
2832 oberon_type(ctx
, &type
);
2834 if(type
-> class == OBERON_TYPE_NOTYPE
)
2836 oberon_error(ctx
, "recursive alias declaration");
2839 newtype
-> type
= type
;
2840 newtype
-> linked
= 1;
2843 static void oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
2844 static void oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
2847 oberon_prevent_recursive_pointer(oberon_context_t
* ctx
, oberon_type_t
* type
)
2849 if(type
-> class != OBERON_TYPE_POINTER
2850 && type
-> class != OBERON_TYPE_ARRAY
)
2855 if(type
-> recursive
)
2857 oberon_error(ctx
, "recursive pointer declaration");
2860 if(type
-> class == OBERON_TYPE_POINTER
2861 && type
-> base
-> class == OBERON_TYPE_POINTER
)
2863 oberon_error(ctx
, "attempt to make pointer to pointer");
2866 type
-> recursive
= 1;
2868 oberon_prevent_recursive_pointer(ctx
, type
-> base
);
2870 type
-> recursive
= 0;
2874 oberon_prevent_recursive_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
2876 if(type
-> class != OBERON_TYPE_RECORD
)
2881 if(type
-> recursive
)
2883 oberon_error(ctx
, "recursive record declaration");
2886 type
-> recursive
= 1;
2890 oberon_prevent_recursive_record(ctx
, type
-> base
);
2893 int num_fields
= type
-> num_decl
;
2894 oberon_object_t
* field
= type
-> decl
;
2895 for(int i
= 0; i
< num_fields
; i
++)
2897 oberon_prevent_recursive_object(ctx
, field
);
2898 field
= field
-> next
;
2901 type
-> recursive
= 0;
2904 oberon_prevent_recursive_procedure(oberon_context_t
* ctx
, oberon_type_t
* type
)
2906 if(type
-> class != OBERON_TYPE_PROCEDURE
)
2911 if(type
-> recursive
)
2913 oberon_error(ctx
, "recursive procedure declaration");
2916 type
-> recursive
= 1;
2918 int num_fields
= type
-> num_decl
;
2919 oberon_object_t
* field
= type
-> decl
;
2920 for(int i
= 0; i
< num_fields
; i
++)
2922 oberon_prevent_recursive_object(ctx
, field
);
2923 field
= field
-> next
;
2926 type
-> recursive
= 0;
2930 oberon_prevent_recursive_array(oberon_context_t
* ctx
, oberon_type_t
* type
)
2932 if(type
-> class != OBERON_TYPE_ARRAY
)
2937 if(type
-> recursive
)
2939 oberon_error(ctx
, "recursive array declaration");
2942 type
-> recursive
= 1;
2944 oberon_prevent_recursive_type(ctx
, type
-> base
);
2946 type
-> recursive
= 0;
2950 oberon_prevent_recursive_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
2952 if(type
-> class == OBERON_TYPE_POINTER
)
2954 oberon_prevent_recursive_pointer(ctx
, type
);
2956 else if(type
-> class == OBERON_TYPE_RECORD
)
2958 oberon_prevent_recursive_record(ctx
, type
);
2960 else if(type
-> class == OBERON_TYPE_ARRAY
)
2962 oberon_prevent_recursive_array(ctx
, type
);
2964 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
2966 oberon_prevent_recursive_procedure(ctx
, type
);
2971 oberon_prevent_recursive_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
2975 case OBERON_CLASS_VAR
:
2976 case OBERON_CLASS_TYPE
:
2977 case OBERON_CLASS_PARAM
:
2978 case OBERON_CLASS_VAR_PARAM
:
2979 case OBERON_CLASS_FIELD
:
2980 oberon_prevent_recursive_type(ctx
, x
-> type
);
2982 case OBERON_CLASS_CONST
:
2983 case OBERON_CLASS_PROC
:
2984 case OBERON_CLASS_MODULE
:
2987 oberon_error(ctx
, "oberon_prevent_recursive_object: wat");
2993 oberon_prevent_recursive_decl(oberon_context_t
* ctx
)
2995 oberon_object_t
* x
= ctx
-> decl
-> list
-> next
;
2999 oberon_prevent_recursive_object(ctx
, x
);
3004 static void oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
);
3005 static void oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
);
3008 oberon_initialize_record_fields(oberon_context_t
* ctx
, oberon_type_t
* type
)
3010 if(type
-> class != OBERON_TYPE_RECORD
)
3015 int num_fields
= type
-> num_decl
;
3016 oberon_object_t
* field
= type
-> decl
;
3017 for(int i
= 0; i
< num_fields
; i
++)
3019 if(field
-> type
-> class == OBERON_TYPE_POINTER
)
3021 oberon_initialize_type(ctx
, field
-> type
);
3024 oberon_initialize_object(ctx
, field
);
3025 field
= field
-> next
;
3028 oberon_generator_init_record(ctx
, type
);
3032 oberon_initialize_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
3034 if(type
-> class == OBERON_TYPE_NOTYPE
)
3036 oberon_error(ctx
, "undeclarated type");
3039 if(type
-> initialized
)
3044 type
-> initialized
= 1;
3046 if(type
-> class == OBERON_TYPE_POINTER
)
3048 oberon_initialize_type(ctx
, type
-> base
);
3049 oberon_generator_init_type(ctx
, type
);
3051 else if(type
-> class == OBERON_TYPE_ARRAY
)
3053 if(type
-> size
!= 0)
3055 if(type
-> base
-> class == OBERON_TYPE_ARRAY
)
3057 if(type
-> base
-> size
== 0)
3059 oberon_error(ctx
, "open array not allowed as array element");
3064 oberon_initialize_type(ctx
, type
-> base
);
3065 oberon_generator_init_type(ctx
, type
);
3067 else if(type
-> class == OBERON_TYPE_RECORD
)
3069 oberon_generator_init_type(ctx
, type
);
3070 oberon_initialize_record_fields(ctx
, type
);
3072 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
3074 int num_fields
= type
-> num_decl
;
3075 oberon_object_t
* field
= type
-> decl
;
3076 for(int i
= 0; i
< num_fields
; i
++)
3078 oberon_initialize_object(ctx
, field
);
3079 field
= field
-> next
;
3082 oberon_generator_init_type(ctx
, type
);
3086 oberon_generator_init_type(ctx
, type
);
3091 oberon_initialize_object(oberon_context_t
* ctx
, oberon_object_t
* x
)
3093 if(x
-> initialized
)
3098 x
-> initialized
= 1;
3102 case OBERON_CLASS_TYPE
:
3103 oberon_initialize_type(ctx
, x
-> type
);
3105 case OBERON_CLASS_VAR
:
3106 case OBERON_CLASS_FIELD
:
3107 if(x
-> type
-> class == OBERON_TYPE_ARRAY
)
3109 if(x
-> type
-> size
== 0)
3111 oberon_error(ctx
, "open array not allowed as variable or field");
3114 oberon_initialize_type(ctx
, x
-> type
);
3115 oberon_generator_init_var(ctx
, x
);
3117 case OBERON_CLASS_PARAM
:
3118 case OBERON_CLASS_VAR_PARAM
:
3119 oberon_initialize_type(ctx
, x
-> type
);
3120 oberon_generator_init_var(ctx
, x
);
3122 case OBERON_CLASS_CONST
:
3123 case OBERON_CLASS_PROC
:
3124 case OBERON_CLASS_MODULE
:
3127 oberon_error(ctx
, "oberon_initialize_object: wat");
3133 oberon_initialize_decl(oberon_context_t
* ctx
)
3135 oberon_object_t
* x
= ctx
-> decl
-> list
;
3139 oberon_initialize_object(ctx
, x
-> next
);
3145 oberon_prevent_undeclarated_procedures(oberon_context_t
* ctx
)
3147 oberon_object_t
* x
= ctx
-> decl
-> list
;
3151 if(x
-> next
-> class == OBERON_CLASS_PROC
)
3153 if(x
-> next
-> linked
== 0)
3155 oberon_error(ctx
, "unresolved forward declaration");
3163 oberon_decl_seq(oberon_context_t
* ctx
)
3165 if(ctx
-> token
== CONST
)
3167 oberon_assert_token(ctx
, CONST
);
3168 while(ctx
-> token
== IDENT
)
3170 oberon_const_decl(ctx
);
3171 oberon_assert_token(ctx
, SEMICOLON
);
3175 if(ctx
-> token
== TYPE
)
3177 oberon_assert_token(ctx
, TYPE
);
3178 while(ctx
-> token
== IDENT
)
3180 oberon_type_decl(ctx
);
3181 oberon_assert_token(ctx
, SEMICOLON
);
3185 if(ctx
-> token
== VAR
)
3187 oberon_assert_token(ctx
, VAR
);
3188 while(ctx
-> token
== IDENT
)
3190 oberon_var_decl(ctx
);
3191 oberon_assert_token(ctx
, SEMICOLON
);
3195 oberon_prevent_recursive_decl(ctx
);
3196 oberon_initialize_decl(ctx
);
3198 while(ctx
-> token
== PROCEDURE
)
3200 oberon_proc_decl(ctx
);
3201 oberon_assert_token(ctx
, SEMICOLON
);
3204 oberon_prevent_undeclarated_procedures(ctx
);
3207 static oberon_expr_t
*
3208 oberon_make_temp_var_item(oberon_context_t
* ctx
, oberon_type_t
* type
)
3210 oberon_object_t
* x
;
3211 oberon_expr_t
* expr
;
3213 x
= oberon_create_object(ctx
-> decl
, "TEMP", OBERON_CLASS_VAR
, false, false);
3216 oberon_generator_init_temp_var(ctx
, x
);
3218 expr
= oberon_new_item(MODE_VAR
, type
, false);
3219 expr
-> item
.var
= x
;
3224 oberon_statement_seq(oberon_context_t
* ctx
);
3227 oberon_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
3229 if(dst
-> read_only
)
3231 oberon_error(ctx
, "read-only destination");
3234 oberon_check_dst(ctx
, dst
);
3235 src
= oberon_autocast_to(ctx
, src
, dst
-> result
);
3236 oberon_generate_assign(ctx
, src
, dst
);
3239 static oberon_expr_t
*
3240 oberon_case_labels(oberon_context_t
* ctx
, oberon_expr_t
* val
)
3244 oberon_expr_t
* cond
;
3245 oberon_expr_t
* cond2
;
3247 e1
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3248 oberon_autocast_to(ctx
, e1
, val
-> result
);
3251 if(ctx
-> token
== DOTDOT
)
3253 oberon_assert_token(ctx
, DOTDOT
);
3254 e2
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3255 oberon_autocast_to(ctx
, e2
, val
-> result
);
3261 cond
= oberon_make_bin_op(ctx
, EQUAL
, val
, e1
);
3265 /* val >= e1 && val <= e2 */
3266 cond
= oberon_make_bin_op(ctx
, GEQ
, val
, e1
);
3267 cond2
= oberon_make_bin_op(ctx
, LEQ
, val
, e2
);
3268 cond
= oberon_make_bin_op(ctx
, AND
, cond
, cond2
);
3275 oberon_case(oberon_context_t
* ctx
, oberon_expr_t
* val
, gen_label_t
* end
)
3277 oberon_expr_t
* cond
;
3278 oberon_expr_t
* cond2
;
3279 gen_label_t
* this_end
;
3281 if(ISEXPR(ctx
-> token
))
3283 this_end
= oberon_generator_reserve_label(ctx
);
3285 cond
= oberon_case_labels(ctx
, val
);
3286 while(ctx
-> token
== COMMA
)
3288 oberon_assert_token(ctx
, COMMA
);
3290 cond2
= oberon_case_labels(ctx
, val
);
3291 cond
= oberon_make_bin_op(ctx
, OR
, cond
, cond2
);
3293 oberon_assert_token(ctx
, COLON
);
3295 oberon_generate_branch(ctx
, cond
, false, this_end
);
3296 oberon_statement_seq(ctx
);
3297 oberon_generate_goto(ctx
, end
);
3299 oberon_generate_label(ctx
, this_end
);
3304 oberon_case_statement(oberon_context_t
* ctx
)
3306 oberon_expr_t
* val
;
3307 oberon_expr_t
* expr
;
3310 end
= oberon_generator_reserve_label(ctx
);
3312 oberon_assert_token(ctx
, CASE
);
3313 expr
= oberon_expr(ctx
);
3314 val
= oberon_make_temp_var_item(ctx
, expr
-> result
);
3315 oberon_assign(ctx
, expr
, val
);
3316 oberon_assert_token(ctx
, OF
);
3317 oberon_case(ctx
, val
, end
);
3318 while(ctx
-> token
== BAR
)
3320 oberon_assert_token(ctx
, BAR
);
3321 oberon_case(ctx
, val
, end
);
3324 if(ctx
-> token
== ELSE
)
3326 oberon_assert_token(ctx
, ELSE
);
3327 oberon_statement_seq(ctx
);
3330 oberon_generate_label(ctx
, end
);
3331 oberon_assert_token(ctx
, END
);
3335 oberon_with_guard_do(oberon_context_t
* ctx
, gen_label_t
* end
)
3337 oberon_expr_t
* val
;
3338 oberon_expr_t
* var
;
3339 oberon_expr_t
* type
;
3340 oberon_expr_t
* cond
;
3341 oberon_expr_t
* cast
;
3342 oberon_type_t
* old_type
;
3343 gen_var_t
* old_var
;
3344 gen_label_t
* this_end
;
3346 this_end
= oberon_generator_reserve_label(ctx
);
3348 var
= oberon_qualident_expr(ctx
);
3349 oberon_assert_token(ctx
, COLON
);
3350 type
= oberon_qualident_expr(ctx
);
3351 cond
= oberon_make_bin_op(ctx
, IS
, var
, type
);
3353 oberon_assert_token(ctx
, DO
);
3354 oberon_generate_branch(ctx
, cond
, false, this_end
);
3356 /* Сохраняем ссылку во временной переменной */
3357 val
= oberon_make_temp_var_item(ctx
, type
-> result
);
3358 cast
= oberno_make_record_cast(ctx
, var
, type
-> result
);
3359 oberon_assign(ctx
, cast
, val
);
3360 /* Подменяем тип у оригинальной переменной */
3361 old_type
= var
-> item
.var
-> type
;
3362 var
-> item
.var
-> type
= type
-> result
;
3363 /* Подменяем ссылку на переменную */
3364 old_var
= var
-> item
.var
-> gen_var
;
3365 var
-> item
.var
-> gen_var
= val
-> item
.var
-> gen_var
;
3367 oberon_statement_seq(ctx
);
3368 oberon_generate_goto(ctx
, end
);
3369 oberon_generate_label(ctx
, this_end
);
3371 /* Возвращаем исходное состояние */
3372 var
-> item
.var
-> gen_var
= old_var
;
3373 var
-> item
.var
-> type
= old_type
;
3377 oberon_with_statement(oberon_context_t
* ctx
)
3380 end
= oberon_generator_reserve_label(ctx
);
3382 oberon_assert_token(ctx
, WITH
);
3383 oberon_with_guard_do(ctx
, end
);
3384 while(ctx
-> token
== BAR
)
3386 oberon_assert_token(ctx
, BAR
);
3387 oberon_with_guard_do(ctx
, end
);
3390 if(ctx
-> token
== ELSE
)
3392 oberon_assert_token(ctx
, ELSE
);
3393 oberon_statement_seq(ctx
);
3396 oberon_generate_label(ctx
, end
);
3397 oberon_assert_token(ctx
, END
);
3401 oberon_statement(oberon_context_t
* ctx
)
3403 oberon_expr_t
* item1
;
3404 oberon_expr_t
* item2
;
3406 if(ctx
-> token
== IDENT
)
3408 item1
= oberon_designator(ctx
);
3409 if(ctx
-> token
== ASSIGN
)
3411 oberon_assert_token(ctx
, ASSIGN
);
3412 item2
= oberon_expr(ctx
);
3413 oberon_assign(ctx
, item2
, item1
);
3417 oberon_opt_proc_parens(ctx
, item1
);
3420 else if(ctx
-> token
== IF
)
3424 oberon_expr_t
* cond
;
3426 els
= oberon_generator_reserve_label(ctx
);
3427 end
= oberon_generator_reserve_label(ctx
);
3429 oberon_assert_token(ctx
, IF
);
3430 cond
= oberon_expr(ctx
);
3431 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3433 oberon_error(ctx
, "condition must be boolean");
3435 oberon_assert_token(ctx
, THEN
);
3436 oberon_generate_branch(ctx
, cond
, false, els
);
3437 oberon_statement_seq(ctx
);
3438 oberon_generate_goto(ctx
, end
);
3439 oberon_generate_label(ctx
, els
);
3441 while(ctx
-> token
== ELSIF
)
3443 els
= oberon_generator_reserve_label(ctx
);
3445 oberon_assert_token(ctx
, ELSIF
);
3446 cond
= oberon_expr(ctx
);
3447 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3449 oberon_error(ctx
, "condition must be boolean");
3451 oberon_assert_token(ctx
, THEN
);
3452 oberon_generate_branch(ctx
, cond
, false, els
);
3453 oberon_statement_seq(ctx
);
3454 oberon_generate_goto(ctx
, end
);
3455 oberon_generate_label(ctx
, els
);
3458 if(ctx
-> token
== ELSE
)
3460 oberon_assert_token(ctx
, ELSE
);
3461 oberon_statement_seq(ctx
);
3464 oberon_generate_label(ctx
, end
);
3465 oberon_assert_token(ctx
, END
);
3467 else if(ctx
-> token
== WHILE
)
3469 gen_label_t
* begin
;
3471 oberon_expr_t
* cond
;
3473 begin
= oberon_generator_reserve_label(ctx
);
3474 end
= oberon_generator_reserve_label(ctx
);
3476 oberon_assert_token(ctx
, WHILE
);
3477 oberon_generate_label(ctx
, begin
);
3478 cond
= oberon_expr(ctx
);
3479 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3481 oberon_error(ctx
, "condition must be boolean");
3483 oberon_generate_branch(ctx
, cond
, false, end
);
3485 oberon_assert_token(ctx
, DO
);
3486 oberon_statement_seq(ctx
);
3487 oberon_generate_goto(ctx
, begin
);
3489 oberon_assert_token(ctx
, END
);
3490 oberon_generate_label(ctx
, end
);
3492 else if(ctx
-> token
== REPEAT
)
3494 gen_label_t
* begin
;
3495 oberon_expr_t
* cond
;
3497 begin
= oberon_generator_reserve_label(ctx
);
3498 oberon_generate_label(ctx
, begin
);
3499 oberon_assert_token(ctx
, REPEAT
);
3501 oberon_statement_seq(ctx
);
3503 oberon_assert_token(ctx
, UNTIL
);
3505 cond
= oberon_expr(ctx
);
3506 if(cond
-> result
-> class != OBERON_TYPE_BOOLEAN
)
3508 oberon_error(ctx
, "condition must be boolean");
3511 oberon_generate_branch(ctx
, cond
, true, begin
);
3513 else if(ctx
-> token
== FOR
)
3515 oberon_expr_t
* from
;
3516 oberon_expr_t
* index
;
3518 oberon_expr_t
* bound
;
3520 oberon_expr_t
* cond
;
3521 oberon_expr_t
* count
;
3522 gen_label_t
* begin
;
3527 begin
= oberon_generator_reserve_label(ctx
);
3528 end
= oberon_generator_reserve_label(ctx
);
3530 oberon_assert_token(ctx
, FOR
);
3531 iname
= oberon_assert_ident(ctx
);
3532 index
= oberon_ident_item(ctx
, iname
);
3533 oberon_assert_token(ctx
, ASSIGN
);
3534 from
= oberon_expr(ctx
);
3535 oberon_assign(ctx
, from
, index
);
3536 oberon_assert_token(ctx
, TO
);
3537 bound
= oberon_make_temp_var_item(ctx
, index
-> result
);
3538 to
= oberon_expr(ctx
);
3539 oberon_assign(ctx
, to
, bound
);
3540 if(ctx
-> token
== BY
)
3542 oberon_assert_token(ctx
, BY
);
3543 by
= (oberon_expr_t
*) oberon_const_expr(ctx
);
3547 by
= oberon_integer_item(ctx
, 1);
3550 if(by
-> result
-> class != OBERON_TYPE_INTEGER
)
3552 oberon_error(ctx
, "must be integer");
3555 if(by
-> item
.integer
> 0)
3559 else if(by
-> item
.integer
< 0)
3565 oberon_error(ctx
, "zero step not allowed");
3568 oberon_assert_token(ctx
, DO
);
3569 oberon_generate_label(ctx
, begin
);
3570 cond
= oberon_make_bin_op(ctx
, op
, index
, bound
);
3571 oberon_generate_branch(ctx
, cond
, false, end
);
3572 oberon_statement_seq(ctx
);
3573 count
= oberon_make_bin_op(ctx
, PLUS
, index
, by
);
3574 oberon_assign(ctx
, count
, index
);
3575 oberon_generate_goto(ctx
, begin
);
3576 oberon_generate_label(ctx
, end
);
3577 oberon_assert_token(ctx
, END
);
3579 else if(ctx
-> token
== LOOP
)
3581 gen_label_t
* begin
;
3584 begin
= oberon_generator_reserve_label(ctx
);
3585 end
= oberon_generator_reserve_label(ctx
);
3587 oberon_open_scope(ctx
);
3588 oberon_assert_token(ctx
, LOOP
);
3589 oberon_generate_label(ctx
, begin
);
3590 ctx
-> decl
-> exit_label
= end
;
3591 oberon_statement_seq(ctx
);
3592 oberon_generate_goto(ctx
, begin
);
3593 oberon_generate_label(ctx
, end
);
3594 oberon_assert_token(ctx
, END
);
3595 oberon_close_scope(ctx
-> decl
);
3597 else if(ctx
-> token
== EXIT
)
3599 oberon_assert_token(ctx
, EXIT
);
3600 if(ctx
-> decl
-> exit_label
== NULL
)
3602 oberon_error(ctx
, "not in LOOP-END");
3604 oberon_generate_goto(ctx
, ctx
-> decl
-> exit_label
);
3606 else if(ctx
-> token
== CASE
)
3608 oberon_case_statement(ctx
);
3610 else if(ctx
-> token
== WITH
)
3612 oberon_with_statement(ctx
);
3614 else if(ctx
-> token
== RETURN
)
3616 oberon_assert_token(ctx
, RETURN
);
3617 if(ISEXPR(ctx
-> token
))
3619 oberon_expr_t
* expr
;
3620 expr
= oberon_expr(ctx
);
3621 oberon_make_return(ctx
, expr
);
3625 oberon_make_return(ctx
, NULL
);
3631 oberon_statement_seq(oberon_context_t
* ctx
)
3633 oberon_statement(ctx
);
3634 while(ctx
-> token
== SEMICOLON
)
3636 oberon_assert_token(ctx
, SEMICOLON
);
3637 oberon_statement(ctx
);
3642 oberon_import_module(oberon_context_t
* ctx
, char * alias
, char * name
)
3644 oberon_module_t
* m
= ctx
-> module_list
;
3645 while(m
&& strcmp(m
-> name
, name
) != 0)
3653 code
= ctx
-> import_module(name
);
3656 oberon_error(ctx
, "no such module");
3659 m
= oberon_compile_module(ctx
, code
);
3665 oberon_error(ctx
, "cyclic module import");
3668 oberon_object_t
* ident
;
3669 ident
= oberon_define_object(ctx
-> decl
, alias
, OBERON_CLASS_MODULE
, false, false, false);
3670 ident
-> module
= m
;
3674 oberon_import_decl(oberon_context_t
* ctx
)
3679 alias
= name
= oberon_assert_ident(ctx
);
3680 if(ctx
-> token
== ASSIGN
)
3682 oberon_assert_token(ctx
, ASSIGN
);
3683 name
= oberon_assert_ident(ctx
);
3686 oberon_import_module(ctx
, alias
, name
);
3690 oberon_import_list(oberon_context_t
* ctx
)
3692 oberon_assert_token(ctx
, IMPORT
);
3694 oberon_import_decl(ctx
);
3695 while(ctx
-> token
== COMMA
)
3697 oberon_assert_token(ctx
, COMMA
);
3698 oberon_import_decl(ctx
);
3701 oberon_assert_token(ctx
, SEMICOLON
);
3705 oberon_parse_module(oberon_context_t
* ctx
)
3709 oberon_read_token(ctx
);
3711 oberon_assert_token(ctx
, MODULE
);
3712 name1
= oberon_assert_ident(ctx
);
3713 oberon_assert_token(ctx
, SEMICOLON
);
3714 ctx
-> mod
-> name
= name1
;
3716 oberon_generator_init_module(ctx
, ctx
-> mod
);
3718 if(ctx
-> token
== IMPORT
)
3720 oberon_import_list(ctx
);
3723 oberon_decl_seq(ctx
);
3725 oberon_generate_begin_module(ctx
);
3726 if(ctx
-> token
== BEGIN
)
3728 oberon_assert_token(ctx
, BEGIN
);
3729 oberon_statement_seq(ctx
);
3731 oberon_generate_end_module(ctx
);
3733 oberon_assert_token(ctx
, END
);
3734 name2
= oberon_assert_ident(ctx
);
3735 oberon_expect_token(ctx
, DOT
);
3737 if(strcmp(name1
, name2
) != 0)
3739 oberon_error(ctx
, "module name not matched");
3742 oberon_generator_fini_module(ctx
-> mod
);
3745 // =======================================================================
3747 // =======================================================================
3750 register_default_types(oberon_context_t
* ctx
)
3752 ctx
-> notype_type
= oberon_new_type_ptr(OBERON_TYPE_NOTYPE
);
3753 oberon_generator_init_type(ctx
, ctx
-> notype_type
);
3755 ctx
-> nil_type
= oberon_new_type_ptr(OBERON_TYPE_NIL
);
3756 oberon_generator_init_type(ctx
, ctx
-> nil_type
);
3758 ctx
-> string_type
= oberon_new_type_string(1);
3759 oberon_generator_init_type(ctx
, ctx
-> string_type
);
3761 ctx
-> bool_type
= oberon_new_type_boolean();
3762 oberon_define_type(ctx
-> world_scope
, "BOOLEAN", ctx
-> bool_type
, 1);
3764 ctx
-> char_type
= oberon_new_type_char(1);
3765 oberon_define_type(ctx
-> world_scope
, "CHAR", ctx
-> char_type
, 1);
3767 ctx
-> byte_type
= oberon_new_type_integer(1);
3768 oberon_define_type(ctx
-> world_scope
, "SHORTINT", ctx
-> byte_type
, 1);
3770 ctx
-> shortint_type
= oberon_new_type_integer(2);
3771 oberon_define_type(ctx
-> world_scope
, "INTEGER", ctx
-> shortint_type
, 1);
3773 ctx
-> int_type
= oberon_new_type_integer(4);
3774 oberon_define_type(ctx
-> world_scope
, "LONGINT", ctx
-> int_type
, 1);
3776 ctx
-> longint_type
= oberon_new_type_integer(8);
3777 oberon_define_type(ctx
-> world_scope
, "HUGEINT", ctx
-> longint_type
, 1);
3779 ctx
-> real_type
= oberon_new_type_real(4);
3780 oberon_define_type(ctx
-> world_scope
, "REAL", ctx
-> real_type
, 1);
3782 ctx
-> longreal_type
= oberon_new_type_real(8);
3783 oberon_define_type(ctx
-> world_scope
, "LONGREAL", ctx
-> longreal_type
, 1);
3785 ctx
-> set_type
= oberon_new_type_set(4);
3786 oberon_define_type(ctx
-> world_scope
, "SET", ctx
-> set_type
, 1);
3790 oberon_new_intrinsic(oberon_context_t
* ctx
, char * name
, GenerateFuncCallback f
, GenerateProcCallback p
)
3792 oberon_object_t
* proc
;
3793 proc
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_PROC
, true, false, false);
3794 proc
-> type
= oberon_new_type_ptr(OBERON_TYPE_PROCEDURE
);
3795 proc
-> type
-> sysproc
= true;
3796 proc
-> type
-> genfunc
= f
;
3797 proc
-> type
-> genproc
= p
;
3800 static oberon_expr_t
*
3801 oberon_make_min_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3805 oberon_error(ctx
, "too few arguments");
3810 oberon_error(ctx
, "too mach arguments");
3813 oberon_expr_t
* arg
;
3816 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3818 oberon_error(ctx
, "MIN accept only type");
3821 oberon_expr_t
* expr
;
3822 int bits
= arg
-> result
-> size
* 8;
3823 switch(arg
-> result
-> class)
3825 case OBERON_TYPE_INTEGER
:
3826 expr
= oberon_integer_item(ctx
, -powl(2, bits
- 1));
3828 case OBERON_TYPE_SET
:
3829 expr
= oberon_integer_item(ctx
, 0);
3832 oberon_error(ctx
, "allowed only basic types");
3839 static oberon_expr_t
*
3840 oberon_make_max_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3844 oberon_error(ctx
, "too few arguments");
3849 oberon_error(ctx
, "too mach arguments");
3852 oberon_expr_t
* arg
;
3855 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3857 oberon_error(ctx
, "MAX accept only type");
3860 oberon_expr_t
* expr
;
3861 int bits
= arg
-> result
-> size
* 8;
3862 switch(arg
-> result
-> class)
3864 case OBERON_TYPE_INTEGER
:
3865 expr
= oberon_integer_item(ctx
, powl(2, bits
- 1) - 1);
3867 case OBERON_TYPE_SET
:
3868 expr
= oberon_integer_item(ctx
, bits
);
3871 oberon_error(ctx
, "allowed only basic types");
3878 static oberon_expr_t
*
3879 oberon_make_size_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3883 oberon_error(ctx
, "too few arguments");
3888 oberon_error(ctx
, "too mach arguments");
3891 oberon_expr_t
* arg
;
3894 if(!arg
-> is_item
|| arg
-> item
.mode
!= MODE_TYPE
)
3896 oberon_error(ctx
, "SIZE accept only type");
3900 oberon_expr_t
* expr
;
3901 oberon_type_t
* type
= arg
-> result
;
3902 switch(type
-> class)
3904 case OBERON_TYPE_INTEGER
:
3905 case OBERON_TYPE_BOOLEAN
:
3906 case OBERON_TYPE_REAL
:
3907 case OBERON_TYPE_CHAR
:
3908 case OBERON_TYPE_SET
:
3909 size
= type
-> size
;
3912 oberon_error(ctx
, "TODO SIZE");
3916 expr
= oberon_integer_item(ctx
, size
);
3920 static oberon_expr_t
*
3921 oberon_make_abs_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3925 oberon_error(ctx
, "too few arguments");
3930 oberon_error(ctx
, "too mach arguments");
3933 oberon_expr_t
* arg
;
3935 oberon_check_src(ctx
, arg
);
3937 oberon_type_t
* result_type
;
3938 result_type
= arg
-> result
;
3940 if(result_type
-> class != OBERON_TYPE_INTEGER
)
3942 oberon_error(ctx
, "ABS accepts only integers");
3945 oberon_expr_t
* expr
;
3946 expr
= oberon_new_operator(OP_ABS
, result_type
, arg
, NULL
);
3951 oberon_make_new_call(oberon_context_t
* ctx
, int num_args
, oberon_expr_t
* list_args
)
3955 oberon_error(ctx
, "too few arguments");
3959 oberon_expr_t
* dst
;
3961 oberon_check_dst(ctx
, dst
);
3963 oberon_type_t
* type
;
3964 type
= dst
-> result
;
3966 if(type
-> class != OBERON_TYPE_POINTER
)
3968 oberon_error(ctx
, "not a pointer");
3971 type
= type
-> base
;
3973 oberon_expr_t
* src
;
3974 src
= oberon_new_item(MODE_NEW
, dst
-> result
, 0);
3975 src
-> item
.num_args
= 0;
3976 src
-> item
.args
= NULL
;
3979 if(type
-> class == OBERON_TYPE_ARRAY
)
3981 if(type
-> size
== 0)
3983 oberon_type_t
* x
= type
;
3984 while(x
-> class == OBERON_TYPE_ARRAY
)
3994 if(num_args
< max_args
)
3996 oberon_error(ctx
, "too few arguments");
3999 if(num_args
> max_args
)
4001 oberon_error(ctx
, "too mach arguments");
4004 int num_sizes
= max_args
- 1;
4005 oberon_expr_t
* size_list
= list_args
-> next
;
4007 oberon_expr_t
* arg
= size_list
;
4008 for(int i
= 0; i
< max_args
- 1; i
++)
4010 oberon_check_src(ctx
, arg
);
4011 if(arg
-> result
-> class != OBERON_TYPE_INTEGER
)
4013 oberon_error(ctx
, "size must be integer");
4018 src
-> item
.num_args
= num_sizes
;
4019 src
-> item
.args
= size_list
;
4021 else if(type
-> class != OBERON_TYPE_RECORD
)
4023 oberon_error(ctx
, "oberon_make_new_call: wat");
4026 if(num_args
> max_args
)
4028 oberon_error(ctx
, "too mach arguments");
4031 oberon_assign(ctx
, src
, dst
);
4035 oberon_new_const(oberon_context_t
* ctx
, char * name
, oberon_expr_t
* expr
)
4037 oberon_object_t
* constant
;
4038 constant
= oberon_define_object(ctx
-> decl
, name
, OBERON_CLASS_CONST
, true, false, false);
4039 oberon_check_const(ctx
, expr
);
4040 constant
-> value
= (oberon_item_t
*) expr
;
4044 oberon_create_context(ModuleImportCallback import_module
)
4046 oberon_context_t
* ctx
= calloc(1, sizeof *ctx
);
4048 oberon_scope_t
* world_scope
;
4049 world_scope
= oberon_open_scope(ctx
);
4050 ctx
-> world_scope
= world_scope
;
4052 ctx
-> import_module
= import_module
;
4054 oberon_generator_init_context(ctx
);
4056 register_default_types(ctx
);
4059 oberon_new_const(ctx
, "TRUE", oberon_make_boolean(ctx
, true));
4060 oberon_new_const(ctx
, "FALSE", oberon_make_boolean(ctx
, false));
4063 oberon_new_intrinsic(ctx
, "ABS", oberon_make_abs_call
, NULL
);
4064 oberon_new_intrinsic(ctx
, "MIN", oberon_make_min_call
, NULL
);
4065 oberon_new_intrinsic(ctx
, "MAX", oberon_make_max_call
, NULL
);
4066 oberon_new_intrinsic(ctx
, "SIZE", oberon_make_size_call
, NULL
);
4069 oberon_new_intrinsic(ctx
, "NEW", NULL
, oberon_make_new_call
);
4075 oberon_destroy_context(oberon_context_t
* ctx
)
4077 oberon_generator_destroy_context(ctx
);
4082 oberon_compile_module(oberon_context_t
* ctx
, const char * newcode
)
4084 const char * code
= ctx
-> code
;
4085 int code_index
= ctx
-> code_index
;
4087 int token
= ctx
-> token
;
4088 char * string
= ctx
-> string
;
4089 int integer
= ctx
-> integer
;
4090 int real
= ctx
-> real
;
4091 bool longmode
= ctx
-> longmode
;
4092 oberon_scope_t
* decl
= ctx
-> decl
;
4093 oberon_module_t
* mod
= ctx
-> mod
;
4095 oberon_scope_t
* module_scope
;
4096 module_scope
= oberon_open_scope(ctx
);
4098 oberon_module_t
* module
;
4099 module
= calloc(1, sizeof *module
);
4100 module
-> decl
= module_scope
;
4101 module
-> next
= ctx
-> module_list
;
4103 ctx
-> mod
= module
;
4104 ctx
-> module_list
= module
;
4106 oberon_init_scaner(ctx
, newcode
);
4107 oberon_parse_module(ctx
);
4109 module
-> ready
= 1;
4112 ctx
-> code_index
= code_index
;
4114 ctx
-> token
= token
;
4115 ctx
-> string
= string
;
4116 ctx
-> integer
= integer
;
4118 ctx
-> longmode
= longmode
;