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 |
|
---|
29 | /** @file Console builtin binding. */
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <assert.h>
|
---|
34 | #include "../bigint.h"
|
---|
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 |
|
---|
43 | #include "bi_console.h"
|
---|
44 |
|
---|
45 | static void bi_console_write(run_t *run);
|
---|
46 | static void bi_console_writeline(run_t *run);
|
---|
47 |
|
---|
48 | /** Declare Console builtin.
|
---|
49 | *
|
---|
50 | * @param bi Builtin object
|
---|
51 | */
|
---|
52 | void bi_console_declare(builtin_t *bi)
|
---|
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();
|
---|
63 | ident->sid = strtab_get_sid("Console");
|
---|
64 |
|
---|
65 | csi = stree_csi_new(csi_class);
|
---|
66 | csi->name = ident;
|
---|
67 | list_init(&csi->targ);
|
---|
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 |
|
---|
80 | /* Declare Builtin.Write(). */
|
---|
81 |
|
---|
82 | fun_sym = builtin_declare_fun(csi, "Write");
|
---|
83 | builtin_fun_add_arg(fun_sym, "arg");
|
---|
84 |
|
---|
85 | /* Declare Builtin.WriteLine(). */
|
---|
86 |
|
---|
87 | fun_sym = builtin_declare_fun(csi, "WriteLine");
|
---|
88 | builtin_fun_add_arg(fun_sym, "arg");
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Bind builtin functions.
|
---|
92 | *
|
---|
93 | * @param bi Builtin object
|
---|
94 | */
|
---|
95 | void bi_console_bind(builtin_t *bi)
|
---|
96 | {
|
---|
97 | builtin_fun_bind(bi, "Console", "Write", bi_console_write);
|
---|
98 | builtin_fun_bind(bi, "Console", "WriteLine", bi_console_writeline);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** Write to the console.
|
---|
102 | *
|
---|
103 | * @param run Runner object
|
---|
104 | */
|
---|
105 | static void bi_console_write(run_t *run)
|
---|
106 | {
|
---|
107 | rdata_var_t *var;
|
---|
108 | int char_val;
|
---|
109 | errno_t rc;
|
---|
110 |
|
---|
111 | #ifdef DEBUG_RUN_TRACE
|
---|
112 | printf("Called Console.Write()\n");
|
---|
113 | #endif
|
---|
114 | var = run_local_vars_lookup(run, strtab_get_sid("arg"));
|
---|
115 | assert(var);
|
---|
116 |
|
---|
117 | switch (var->vc) {
|
---|
118 | case vc_bool:
|
---|
119 | printf("%s", var->u.bool_v->value ? "true" : "false");
|
---|
120 | break;
|
---|
121 | case vc_char:
|
---|
122 | rc = bigint_get_value_int(&var->u.char_v->value, &char_val);
|
---|
123 | if (rc == EOK)
|
---|
124 | printf("%lc", char_val);
|
---|
125 | else
|
---|
126 | printf("???");
|
---|
127 | break;
|
---|
128 | case vc_int:
|
---|
129 | bigint_print(&var->u.int_v->value);
|
---|
130 | break;
|
---|
131 | case vc_string:
|
---|
132 | printf("%s", var->u.string_v->value);
|
---|
133 | break;
|
---|
134 | default:
|
---|
135 | printf("Unimplemented: Write() with unsupported type.\n");
|
---|
136 | exit(1);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Write a line of output.
|
---|
141 | *
|
---|
142 | * @param run Runner object
|
---|
143 | */
|
---|
144 | static void bi_console_writeline(run_t *run)
|
---|
145 | {
|
---|
146 | #ifdef DEBUG_RUN_TRACE
|
---|
147 | printf("Called Console.WriteLine()\n");
|
---|
148 | #endif
|
---|
149 | bi_console_write(run);
|
---|
150 | putchar('\n');
|
---|
151 | }
|
---|