bi_console.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         /* Declare class Builtin */
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         /* Declare Builtin.Write(). */
00081 
00082         fun_sym = builtin_declare_fun(csi, "Write");
00083         builtin_fun_add_arg(fun_sym, "arg");
00084 
00085         /* Declare Builtin.WriteLine(). */
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 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7