[37f527b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[051b3db8] | 29 | /** @file Console builtin binding. */
|
---|
[37f527b] | 30 |
|
---|
| 31 | #include <stdio.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <assert.h>
|
---|
[23de644] | 34 | #include "../bigint.h"
|
---|
[37f527b] | 35 | #include "../builtin.h"
|
---|
| 36 | #include "../list.h"
|
---|
| 37 | #include "../mytypes.h"
|
---|
| 38 | #include "../run.h"
|
---|
| 39 | #include "../stree.h"
|
---|
| 40 | #include "../strtab.h"
|
---|
| 41 | #include "../symbol.h"
|
---|
| 42 |
|
---|
[051b3db8] | 43 | #include "bi_console.h"
|
---|
[37f527b] | 44 |
|
---|
[051b3db8] | 45 | static void bi_console_write(run_t *run);
|
---|
| 46 | static void bi_console_writeline(run_t *run);
|
---|
[37f527b] | 47 |
|
---|
[051b3db8] | 48 | /** Declare Console builtin.
|
---|
[38aaacc2] | 49 | *
|
---|
| 50 | * @param bi Builtin object
|
---|
| 51 | */
|
---|
[051b3db8] | 52 | void bi_console_declare(builtin_t *bi)
|
---|
[37f527b] | 53 | {
|
---|
| 54 | stree_modm_t *modm;
|
---|
| 55 | stree_csi_t *csi;
|
---|
| 56 | stree_ident_t *ident;
|
---|
| 57 | stree_symbol_t *symbol;
|
---|
| 58 | stree_symbol_t *fun_sym;
|
---|
| 59 |
|
---|
| 60 | /* Declare class Builtin */
|
---|
| 61 |
|
---|
| 62 | ident = stree_ident_new();
|
---|
[051b3db8] | 63 | ident->sid = strtab_get_sid("Console");
|
---|
[37f527b] | 64 |
|
---|
| 65 | csi = stree_csi_new(csi_class);
|
---|
| 66 | csi->name = ident;
|
---|
[38aaacc2] | 67 | list_init(&csi->targ);
|
---|
[37f527b] | 68 | list_init(&csi->members);
|
---|
| 69 |
|
---|
| 70 | modm = stree_modm_new(mc_csi);
|
---|
| 71 | modm->u.csi = csi;
|
---|
| 72 |
|
---|
| 73 | symbol = stree_symbol_new(sc_csi);
|
---|
| 74 | symbol->u.csi = csi;
|
---|
| 75 | symbol->outer_csi = NULL;
|
---|
| 76 | csi->symbol = symbol;
|
---|
| 77 |
|
---|
| 78 | list_append(&bi->program->module->members, modm);
|
---|
| 79 |
|
---|
[38aaacc2] | 80 | /* Declare Builtin.Write(). */
|
---|
| 81 |
|
---|
| 82 | fun_sym = builtin_declare_fun(csi, "Write");
|
---|
| 83 | builtin_fun_add_arg(fun_sym, "arg");
|
---|
| 84 |
|
---|
[37f527b] | 85 | /* Declare Builtin.WriteLine(). */
|
---|
| 86 |
|
---|
| 87 | fun_sym = builtin_declare_fun(csi, "WriteLine");
|
---|
| 88 | builtin_fun_add_arg(fun_sym, "arg");
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[38aaacc2] | 91 | /** Bind builtin functions.
|
---|
| 92 | *
|
---|
| 93 | * @param bi Builtin object
|
---|
| 94 | */
|
---|
[051b3db8] | 95 | void bi_console_bind(builtin_t *bi)
|
---|
[37f527b] | 96 | {
|
---|
[051b3db8] | 97 | builtin_fun_bind(bi, "Console", "Write", bi_console_write);
|
---|
| 98 | builtin_fun_bind(bi, "Console", "WriteLine", bi_console_writeline);
|
---|
[37f527b] | 99 | }
|
---|
| 100 |
|
---|
[38aaacc2] | 101 | /** Write to the console.
|
---|
| 102 | *
|
---|
| 103 | * @param run Runner object
|
---|
| 104 | */
|
---|
[051b3db8] | 105 | static void bi_console_write(run_t *run)
|
---|
[37f527b] | 106 | {
|
---|
| 107 | rdata_var_t *var;
|
---|
[074444f] | 108 | int char_val;
|
---|
[b7fd2a0] | 109 | errno_t rc;
|
---|
[37f527b] | 110 |
|
---|
| 111 | #ifdef DEBUG_RUN_TRACE
|
---|
[051b3db8] | 112 | printf("Called Console.Write()\n");
|
---|
[37f527b] | 113 | #endif
|
---|
| 114 | var = run_local_vars_lookup(run, strtab_get_sid("arg"));
|
---|
| 115 | assert(var);
|
---|
| 116 |
|
---|
| 117 | switch (var->vc) {
|
---|
[38aaacc2] | 118 | case vc_bool:
|
---|
| 119 | printf("%s", var->u.bool_v->value ? "true" : "false");
|
---|
| 120 | break;
|
---|
[074444f] | 121 | case vc_char:
|
---|
| 122 | rc = bigint_get_value_int(&var->u.char_v->value, &char_val);
|
---|
| 123 | if (rc == EOK)
|
---|
[38aaacc2] | 124 | printf("%lc", char_val);
|
---|
[074444f] | 125 | else
|
---|
[38aaacc2] | 126 | printf("???");
|
---|
[074444f] | 127 | break;
|
---|
[37f527b] | 128 | case vc_int:
|
---|
[23de644] | 129 | bigint_print(&var->u.int_v->value);
|
---|
[37f527b] | 130 | break;
|
---|
| 131 | case vc_string:
|
---|
[38aaacc2] | 132 | printf("%s", var->u.string_v->value);
|
---|
[37f527b] | 133 | break;
|
---|
| 134 | default:
|
---|
[38aaacc2] | 135 | printf("Unimplemented: Write() with unsupported type.\n");
|
---|
[37f527b] | 136 | exit(1);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[38aaacc2] | 140 | /** Write a line of output.
|
---|
| 141 | *
|
---|
| 142 | * @param run Runner object
|
---|
| 143 | */
|
---|
[051b3db8] | 144 | static void bi_console_writeline(run_t *run)
|
---|
[38aaacc2] | 145 | {
|
---|
| 146 | #ifdef DEBUG_RUN_TRACE
|
---|
[051b3db8] | 147 | printf("Called Console.WriteLine()\n");
|
---|
[38aaacc2] | 148 | #endif
|
---|
[051b3db8] | 149 | bi_console_write(run);
|
---|
[38aaacc2] | 150 | putchar('\n');
|
---|
| 151 | }
|
---|