Repository navigation
Expand file tree
/
Copy pathwalc.rs
More file actions
175 lines (145 loc) · 3.22 KB
/
Copy pathwalc.rs
File metadata and controls
175 lines (145 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![allow(dead_code)]
#![allow(unused)]
#![macro_use]
#![no_std]
pub extern crate alloc;
pub use alloc::{
borrow::ToOwned,
boxed::Box,
format,
string::{String, ToString},
vec::Vec,
};
mod walc {
#[link(wasm_import_module = "walc")]
unsafe extern "C" {
pub safe fn output(c: u8);
pub safe fn input() -> u32;
pub safe fn exit() -> !;
}
}
#[unsafe(no_mangle)]
extern "C" fn main() {
crate::main()
}
#[unsafe(no_mangle)]
extern "C" fn handle_trap(trap_code: u32) {
print_string("\nTRAP ");
match trap_code {
0 => print_string("reached unreachable"),
1 => print_string("division error"),
2 => print_string("floats unsupported"),
_ => {}
}
}
// The target_family attribute suppresses rust-analyzer errors
#[cfg(target_family = "wasm")]
#[global_allocator]
static ALLOCATOR: talc::wasm::WasmDynamicTalc = talc::wasm::new_wasm_dynamic_allocator();
// The target_family attribute suppresses rust-analyzer errors
#[cfg(target_family = "wasm")]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
print_string("\nPANIC ");
if let Some(msg) = info.message().as_str() {
print_string(msg);
}
if let Some(location) = info.location() {
print_string(" at ");
print_string(location.file());
print_string(":");
print_u32(location.line());
}
exit()
}
pub fn print_byte(c: u8) {
walc::output(c)
}
pub fn read_byte() -> Option<u8> {
match walc::input() {
0xFFFFFFFF => None,
byte => Some(byte as u8),
}
}
pub fn exit() -> ! {
walc::exit()
}
pub fn read_buffer(buffer: &mut [u8]) -> usize {
let mut count = 0;
for byte in buffer.iter_mut() {
match read_byte() {
Some(b) => {
*byte = b;
count += 1;
}
None => break,
}
}
count
}
pub fn read_all() -> Vec<u8> {
let mut buffer = Vec::new();
while let Some(byte) = read_byte() {
buffer.push(byte);
}
buffer
}
pub fn print_string(s: &str) {
for &byte in s.as_bytes() {
print_byte(byte);
}
}
pub fn read_string() -> String {
String::from_utf8(read_all()).expect("Invalid UTF-8 input")
}
pub fn read_line() -> String {
let mut buffer = Vec::new();
while let Some(byte) = read_byte() {
if byte == b'\n' {
break;
}
buffer.push(byte);
}
String::from_utf8(buffer).expect("Invalid UTF-8 input")
}
pub fn print_u32(mut n: u32) {
let mut buffer = [0u8; 10];
let mut i = 0;
loop {
buffer[i] = b'0' + (n % 10) as u8;
n /= 10;
i += 1;
if n == 0 {
break;
}
}
for j in (0..i).rev() {
print_byte(buffer[j]);
}
}
macro_rules! print {
($($arg:tt)*) => ({
let s = format!($($arg)*);
print_string(&s);
});
}
macro_rules! println {
() => (print_string("\n"));
($($arg:tt)*) => ({
print!($($arg)*);
println!();
});
}
macro_rules! eprint {
($($arg:tt)*) => ({
println!($($arg)*);
});
}
macro_rules! eprintln {
() => {
println!()
};
($($arg:tt)*) => {{
println!($($arg)*);
}};
}