00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <assert.h>
00034 #include "../bigint.h"
00035 #include "../builtin.h"
00036 #include "../list.h"
00037 #include "../mytypes.h"
00038 #include "../run.h"
00039 #include "../stree.h"
00040 #include "../strtab.h"
00041 #include "../symbol.h"
00042
00043 #include "bi_console.h"
00044
00045 static void bi_console_write(run_t *run);
00046 static void bi_console_writeline(run_t *run);
00047
00052 void bi_console_declare(builtin_t *bi)
00053 {
00054 stree_modm_t *modm;
00055 stree_csi_t *csi;
00056 stree_ident_t *ident;
00057 stree_symbol_t *symbol;
00058 stree_symbol_t *fun_sym;
00059
00060
00061
00062 ident = stree_ident_new();
00063 ident->sid = strtab_get_sid("Console");
00064
00065 csi = stree_csi_new(csi_class);
00066 csi->name = ident;
00067 list_init(&csi->targ);
00068 list_init(&csi->members);
00069
00070 modm = stree_modm_new(mc_csi);
00071 modm->u.csi = csi;
00072
00073 symbol = stree_symbol_new(sc_csi);
00074 symbol->u.csi = csi;
00075 symbol->outer_csi = NULL;
00076 csi->symbol = symbol;
00077
00078 list_append(&bi->program->module->members, modm);
00079
00080
00081
00082 fun_sym = builtin_declare_fun(csi, "Write");
00083 builtin_fun_add_arg(fun_sym, "arg");
00084
00085
00086
00087 fun_sym = builtin_declare_fun(csi, "WriteLine");
00088 builtin_fun_add_arg(fun_sym, "arg");
00089 }
00090
00095 void bi_console_bind(builtin_t *bi)
00096 {
00097 builtin_fun_bind(bi, "Console", "Write", bi_console_write);
00098 builtin_fun_bind(bi, "Console", "WriteLine", bi_console_writeline);
00099 }
00100
00105 static void bi_console_write(run_t *run)
00106 {
00107 rdata_var_t *var;
00108 int char_val;
00109 int rc;
00110
00111 #ifdef DEBUG_RUN_TRACE
00112 printf("Called Console.Write()\n");
00113 #endif
00114 var = run_local_vars_lookup(run, strtab_get_sid("arg"));
00115 assert(var);
00116
00117 switch (var->vc) {
00118 case vc_bool:
00119 printf("%s", var->u.bool_v->value ? "true" : "false");
00120 break;
00121 case vc_char:
00122 rc = bigint_get_value_int(&var->u.char_v->value, &char_val);
00123 if (rc == EOK)
00124 printf("%lc", char_val);
00125 else
00126 printf("???");
00127 break;
00128 case vc_int:
00129 bigint_print(&var->u.int_v->value);
00130 break;
00131 case vc_string:
00132 printf("%s", var->u.string_v->value);
00133 break;
00134 default:
00135 printf("Unimplemented: Write() with unsupported type.\n");
00136 exit(1);
00137 }
00138 }
00139
00144 static void bi_console_writeline(run_t *run)
00145 {
00146 #ifdef DEBUG_RUN_TRACE
00147 printf("Called Console.WriteLine()\n");
00148 #endif
00149 bi_console_write(run);
00150 putchar('\n');
00151 }