4dfc604a5f38159b0d1668b8376b0cc4fa8195e7
28 // =======================================================================
30 // =======================================================================
33 oberon_error(oberon_context_t
* ctx
, const char * fmt
, ...)
37 fprintf(stderr
, "error: ");
38 vfprintf(stderr
, fmt
, ptr
);
39 fprintf(stderr
, "\n");
40 fprintf(stderr
, " code_index = %i\n", ctx
-> code_index
);
41 fprintf(stderr
, " c = %c\n", ctx
-> c
);
42 fprintf(stderr
, " token = %i\n", ctx
-> token
);
48 oberon_item_to_type_class(oberon_context_t
* ctx
, oberon_item_t
* item
)
55 class = OBERON_TYPE_INTEGER
;
58 class = OBERON_TYPE_BOOLEAN
;
61 class = item
-> var
-> type
-> class;
64 oberon_error(ctx
, "oberon_item_to_type_class: wat");
72 oberon_autocast_to(oberon_context_t
* ctx
, oberon_item_t
* from
, oberon_item_t
* to
)
74 int from_class
= oberon_item_to_type_class(ctx
, from
);
75 int to_class
= oberon_item_to_type_class(ctx
, to
);
77 if(from_class
!= to_class
)
79 oberon_error(ctx
, "oberon_autocast_to: types not matched %i -> %i", from_class
, to_class
);
83 // =======================================================================
85 // =======================================================================
87 static oberon_type_t
*
88 oberon_find_type(oberon_context_t
* ctx
, char * name
)
90 oberon_type_t
* x
= ctx
-> types
;
91 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
100 oberon_find_var(oberon_context_t
* ctx
, char * name
)
102 oberon_var_t
* x
= ctx
-> mod
-> vars
;
103 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
112 oberon_define_var(oberon_context_t
* ctx
, char * name
, oberon_type_t
* type
)
114 oberon_var_t
* x
= ctx
-> mod
-> vars
;
115 while(x
-> next
&& strcmp(x
-> next
-> name
, name
) != 0)
122 oberon_error(ctx
, "already defined");
125 oberon_var_t
* newvar
= malloc(sizeof *newvar
);
126 memset(newvar
, 0, sizeof *newvar
);
127 newvar
-> name
= name
;
128 newvar
-> type
= type
;
129 oberon_generator_init_var(ctx
, newvar
);
134 // =======================================================================
136 // =======================================================================
139 oberon_get_char(oberon_context_t
* ctx
)
141 ctx
-> code_index
+= 1;
142 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
146 oberon_init_scaner(oberon_context_t
* ctx
, const char * code
)
149 ctx
-> code_index
= 0;
150 ctx
-> c
= ctx
-> code
[ctx
-> code_index
];
154 oberon_read_ident(oberon_context_t
* ctx
)
157 int i
= ctx
-> code_index
;
159 int c
= ctx
-> code
[i
];
167 char * ident
= malloc(len
+ 1);
168 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
171 ctx
-> code_index
= i
;
172 ctx
-> c
= ctx
-> code
[i
];
173 ctx
-> string
= ident
;
174 ctx
-> token
= IDENT
;
176 if(strcmp(ident
, "MODULE") == 0)
178 ctx
-> token
= MODULE
;
180 else if(strcmp(ident
, "END") == 0)
184 else if(strcmp(ident
, "VAR") == 0)
188 else if(strcmp(ident
, "BEGIN") == 0)
190 ctx
-> token
= BEGIN
;
192 else if(strcmp(ident
, "TRUE") == 0)
196 else if(strcmp(ident
, "FALSE") == 0)
198 ctx
-> token
= FALSE
;
203 oberon_read_integer(oberon_context_t
* ctx
)
206 int i
= ctx
-> code_index
;
208 int c
= ctx
-> code
[i
];
216 char * ident
= malloc(len
+ 2);
217 memcpy(ident
, &ctx
->code
[ctx
->code_index
], len
);
220 ctx
-> code_index
= i
;
221 ctx
-> c
= ctx
-> code
[i
];
222 ctx
-> string
= ident
;
223 ctx
-> integer
= atoi(ident
);
224 ctx
-> token
= INTEGER
;
228 oberon_skip_space(oberon_context_t
* ctx
)
230 while(isspace(ctx
-> c
))
232 oberon_get_char(ctx
);
237 oberon_read_symbol(oberon_context_t
* ctx
)
246 ctx
-> token
= SEMICOLON
;
247 oberon_get_char(ctx
);
250 ctx
-> token
= COLON
;
251 oberon_get_char(ctx
);
254 ctx
-> token
= ASSIGN
;
255 oberon_get_char(ctx
);
260 oberon_get_char(ctx
);
263 ctx
-> token
= LPAREN
;
264 oberon_get_char(ctx
);
267 ctx
-> token
= RPAREN
;
268 oberon_get_char(ctx
);
271 oberon_error(ctx
, "invalid char");
277 oberon_read_token(oberon_context_t
* ctx
)
279 oberon_skip_space(ctx
);
284 oberon_read_ident(ctx
);
288 oberon_read_integer(ctx
);
292 oberon_read_symbol(ctx
);
296 // =======================================================================
298 // =======================================================================
300 static void oberon_expect_token(oberon_context_t
* ctx
, int token
);
301 static oberon_item_t
* oberon_expr(oberon_context_t
* ctx
);
302 static void oberon_assert_token(oberon_context_t
* ctx
, int token
);
303 static char * oberon_assert_ident(oberon_context_t
* ctx
);
305 static oberon_item_t
*
306 oberon_factor(oberon_context_t
* ctx
)
311 oberon_item_t
* item
= malloc(sizeof *item
);
312 memset(item
, 0, sizeof *item
);
317 name
= oberon_assert_ident(ctx
);
318 var
= oberon_find_var(ctx
, name
);
321 oberon_error(ctx
, "undefined variable %s", name
);
323 item
-> mode
= MODE_VAR
;
327 item
-> mode
= MODE_INTEGER
;
328 item
-> integer
= ctx
-> integer
;
329 oberon_assert_token(ctx
, INTEGER
);
332 item
-> mode
= MODE_BOOLEAN
;
334 oberon_assert_token(ctx
, TRUE
);
337 item
-> mode
= MODE_BOOLEAN
;
339 oberon_assert_token(ctx
, FALSE
);
342 oberon_assert_token(ctx
, LPAREN
);
343 item
= oberon_expr(ctx
);
344 oberon_assert_token(ctx
, RPAREN
);
347 oberon_error(ctx
, "invalid expression");
353 static oberon_item_t
*
354 oberon_expr(oberon_context_t
* ctx
)
356 return oberon_factor(ctx
);
359 // =======================================================================
361 // =======================================================================
364 oberon_expect_token(oberon_context_t
* ctx
, int token
)
366 if(ctx
-> token
!= token
)
368 oberon_error(ctx
, "unexpected token %i (%i)", ctx
-> token
, token
);
373 oberon_assert_token(oberon_context_t
* ctx
, int token
)
375 oberon_expect_token(ctx
, token
);
376 oberon_read_token(ctx
);
380 oberon_assert_ident(oberon_context_t
* ctx
)
382 oberon_expect_token(ctx
, IDENT
);
383 char * ident
= ctx
-> string
;
384 oberon_read_token(ctx
);
388 static oberon_type_t
*
389 oberon_type(oberon_context_t
* ctx
)
391 char * name
= oberon_assert_ident(ctx
);
392 oberon_type_t
* type
= oberon_find_type(ctx
, name
);
396 oberon_error(ctx
, "undefined type");
403 oberon_var_decl(oberon_context_t
* ctx
)
405 char * name
= oberon_assert_ident(ctx
);
406 oberon_assert_token(ctx
, COLON
);
407 oberon_type_t
* type
= oberon_type(ctx
);
408 oberon_define_var(ctx
, name
, type
);
412 oberon_decl_seq(oberon_context_t
* ctx
)
414 if(ctx
-> token
== VAR
)
416 oberon_assert_token(ctx
, VAR
);
417 while(ctx
-> token
== IDENT
)
419 oberon_var_decl(ctx
);
420 oberon_assert_token(ctx
, SEMICOLON
);
426 oberon_assign(oberon_context_t
* ctx
, oberon_item_t
* src
, oberon_item_t
* dst
)
428 if(dst
-> mode
== MODE_INTEGER
)
430 oberon_error(ctx
, "invalid assignment");
433 oberon_autocast_to(ctx
, src
, dst
);
435 oberon_generate_assign(ctx
, src
, dst
);
439 oberon_statement(oberon_context_t
* ctx
)
441 oberon_item_t
* item1
;
442 oberon_item_t
* item2
;
444 if(ctx
-> token
== IDENT
)
446 item1
= oberon_expr(ctx
);
447 oberon_assert_token(ctx
, ASSIGN
);
448 item2
= oberon_expr(ctx
);
449 oberon_assign(ctx
, item2
, item1
);
454 oberon_statement_seq(oberon_context_t
* ctx
)
456 oberon_statement(ctx
);
457 while(ctx
-> token
== SEMICOLON
)
459 oberon_assert_token(ctx
, SEMICOLON
);
460 oberon_statement(ctx
);
465 oberon_parse_module(oberon_context_t
* ctx
)
468 oberon_read_token(ctx
);
470 oberon_assert_token(ctx
, MODULE
);
471 name1
= oberon_assert_ident(ctx
);
472 oberon_assert_token(ctx
, SEMICOLON
);
473 ctx
-> mod
-> name
= name1
;
475 oberon_decl_seq(ctx
);
477 if(ctx
-> token
== BEGIN
)
479 oberon_assert_token(ctx
, BEGIN
);
480 oberon_generate_begin_module(ctx
);
481 oberon_statement_seq(ctx
);
482 oberon_generate_end_module(ctx
);
485 oberon_assert_token(ctx
, END
);
486 name2
= oberon_assert_ident(ctx
);
487 oberon_assert_token(ctx
, DOT
);
489 if(strcmp(name1
, name2
) != 0)
491 oberon_error(ctx
, "module name not matched");
495 // =======================================================================
497 // =======================================================================
500 oberon_create_context()
502 oberon_context_t
* ctx
= malloc(sizeof *ctx
);
503 memset(ctx
, 0, sizeof *ctx
);
505 oberon_type_t
* types
= malloc(sizeof *types
);
506 memset(types
, 0, sizeof *types
);
508 oberon_generator_init_context(ctx
);
509 ctx
-> types
= types
;
514 oberon_destroy_context(oberon_context_t
* ctx
)
516 oberon_generator_destroy_context(ctx
);
521 oberon_compile_module(oberon_context_t
* ctx
, const char * code
)
523 oberon_module_t
* mod
= malloc(sizeof *mod
);
524 memset(mod
, 0, sizeof *mod
);
525 oberon_var_t
* vars
= malloc(sizeof *vars
);
526 memset(vars
, 0, sizeof *vars
);
528 ctx
-> mod
-> vars
= vars
;
530 oberon_init_scaner(ctx
, code
);
531 oberon_parse_module(ctx
);
533 oberon_generate_code(ctx
);
538 oberon_register_global_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
540 oberon_type_t
* x
= ctx
-> types
;
541 while(x
-> next
&& strcmp(x
-> next
-> name
, type
-> name
) != 0)
548 oberon_error(ctx
, "already defined");
551 // TODO: copy type name (not a pointer)
552 oberon_type_t
* newtype
= malloc(sizeof *newtype
);
553 memcpy(newtype
, type
, sizeof *newtype
);
554 newtype
-> next
= NULL
;
555 oberon_generator_init_type(ctx
, newtype
);