-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
675 lines (616 loc) · 19.4 KB
/
lib.rs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#![crate_id="aheui"]
#![crate_type="lib"]
#![license="BSD simplified"]
#![feature(macro_rules)]
extern crate hangeul;
#[macro_export]
macro_rules! printerr(
($fmt:expr $($arg:tt)*) => (
let _ = writeln!(&mut std::io::stderr(), $fmt $($arg)*);
);
)
pub enum InterpreterDirection {
Down,
Up,
Right,
Left,
}
pub enum InstructionData {
Hangeul(hangeul::ConcreteSyllable),
Character(char),
Virtual,
}
impl InstructionData {
pub fn from_char(c: char) -> InstructionData {
match hangeul::ConcreteSyllable::from_char(c) {
Some(syllable) => Hangeul(syllable),
None => Character(c),
}
}
}
pub enum InstructionMovement {
RegularMovement(InterpreterDirection, int, int),
AllowHorizontalMovement,
AllowVerticalMovement,
DisallowMovement,
KeepCurrentMovement,
WallMovement(InterpreterDirection, int),
}
pub enum InstructionOperation {
NoOperation,
PushConstantOperation(int),
PushDuplicationOperation,
PushIntegerInputOperation,
PushCharInputOperation,
BinaryOperation(fn(int, int) -> int),
PopOperation,
PrintIntegerOperation,
PrintCharOperation,
SwapOperation,
ChangeStorageOperation(uint),
MoveToStorageOperation(uint),
CompareOperation,
BranchOperation,
HaltOperation,
}
fn operation_digeut(v1: int, v2: int) -> int { v2 + v1 }
fn operation_ssang_digeut(v1: int, v2: int) -> int { v2 * v1 }
fn operation_tieut(v1: int, v2: int) -> int { v2 - v1 }
fn operation_nieun(v1: int, v2: int) -> int { v2 / v1 }
fn operation_rieul(v1: int, v2: int) -> int { v2 % v1 }
pub struct Instruction {
data: InstructionData,
operation: InstructionOperation,
move: InstructionMovement,
}
static right_wall_instruction: Instruction
= Instruction { data: Virtual, operation: NoOperation, move: WallMovement(Right, 2) };
static down_wall_instruction: Instruction
= Instruction { data: Virtual, operation: NoOperation, move: WallMovement(Down, 2) };
impl Instruction {
pub fn from_data(i: InstructionData) -> Instruction {
let operation = match i {
Hangeul(c) => match c.initial() {
hangeul::InitialBieup => match c.final0() {
hangeul::FinalIeung => PushIntegerInputOperation,
hangeul::FinalHieut => PushCharInputOperation,
final => PushConstantOperation(final_draw_counts[final as uint]),
},
hangeul::InitialSsangBieup => PushDuplicationOperation,
hangeul::InitialPieup => SwapOperation,
hangeul::InitialDigeut => BinaryOperation(operation_digeut),
hangeul::InitialSsangDigeut => BinaryOperation(operation_ssang_digeut),
hangeul::InitialTieut => BinaryOperation(operation_tieut),
hangeul::InitialNieun => BinaryOperation(operation_nieun),
hangeul::InitialRieul => BinaryOperation(operation_rieul),
hangeul::InitialIeung => { NoOperation },
hangeul::InitialMieum => match c.final0() {
hangeul::FinalIeung => PrintIntegerOperation,
hangeul::FinalHieut => PrintCharOperation,
_ => PopOperation,
},
hangeul::InitialSiot => ChangeStorageOperation(c.final0() as uint),
hangeul::InitialSsangSiot => MoveToStorageOperation(c.final0() as uint),
hangeul::InitialJieut => CompareOperation,
hangeul::InitialChieut => BranchOperation,
hangeul::InitialHieut => HaltOperation,
_ => NoOperation,
},
_ => NoOperation,
};
let move = match i {
Hangeul(c) => match c.peak() {
hangeul::A => { RegularMovement(Right, 0, 1) }
hangeul::Ya => { RegularMovement(Right, 0, 2) }
hangeul::Eo => { RegularMovement(Left, 0, -1) }
hangeul::Yeo => { RegularMovement(Left, 0, -2) }
hangeul::O => { RegularMovement(Up, -1, 0) }
hangeul::Yo => { RegularMovement(Up, -2, 0) }
hangeul::U => { RegularMovement(Down, 1, 0) }
hangeul::Yu => { RegularMovement(Down, 2, 0) }
hangeul::Eu => { AllowHorizontalMovement }
hangeul::I => { AllowVerticalMovement }
hangeul::Ui => { DisallowMovement }
_ => { KeepCurrentMovement }
},
_ => KeepCurrentMovement,
};
Instruction { data: i, operation: operation, move: move }
}
pub fn from_wall_data(direction: InterpreterDirection, value: int) -> Instruction {
Instruction {
data: Virtual,
operation: NoOperation,
move: WallMovement(direction, value)
}
}
pub fn from_char(c: char) -> Instruction {
return Instruction::from_data(InstructionData::from_char(c));
}
pub fn hangeul(&self) -> hangeul::ConcreteSyllable {
match self.data {
Hangeul(syllable) => syllable,
_ => { assert!(false); fail!("") }
}
}
}
pub struct Source {
map: Vec<Vec<Instruction>>,
}
impl Source {
pub fn from_str(s: &str) -> Source {
let mut obj = Source {
map: Vec::new(),
};
obj._parse(s);
obj
}
pub fn _parse(&mut self, s: &str) {
self.map.push(Vec::new());
for c in s.as_slice().chars() {
if c == '\n' {
self.map.push(Vec::new());
} else {
let inst = Instruction::from_data(match hangeul::ConcreteSyllable::from_char(c) {
Some(syllable) => Hangeul(syllable),
None => Character(c),
});
self.map.mut_last().unwrap().push(inst);
}
}
let mut max_col_len = 0;
let row_len = self.map.len();
for row in self.map.mut_iter() {
let len = row.len();
max_col_len = std::cmp::max(max_col_len, len);
row.insert(0, Instruction::from_wall_data(Left, len as int + 1));
row.insert(0, Instruction::from_wall_data(Left, len as int + 1));
}
{
let mut h1 = Vec::new();
let mut h2 = Vec::new();
let mut t1 = Vec::new();
let mut t2 = Vec::new();
for _ in range(0, max_col_len + 2) {
h1.push(Instruction::from_wall_data(Up, row_len as int + 1));
h2.push(Instruction::from_wall_data(Up, row_len as int + 1));
t1.push(down_wall_instruction);
t2.push(down_wall_instruction);
}
self.map.insert(0, h1);
self.map.insert(0, h2);
self.map.push(t1);
self.map.push(t2);
}
}
fn _get(&self, pos: (int, int)) -> Instruction {
match pos {
(ridx, cidx) => {
let row = self.map.get(ridx as uint);
let inst = if cidx >= row.len() as int {
right_wall_instruction
} else {
*row.get(cidx as uint)
};
// println!("{},{} inst: {:?}", ridx, cidx, inst);
inst
}
}
}
pub fn get(&self, pos: (int, int)) -> Instruction {
match pos {
(ridx, cidx) => self._get((ridx + 2, cidx + 2))
}
}
}
pub trait Storage {
fn len(&self) -> uint;
fn put(&mut self, data: int);
fn rput(&mut self, data: int);
fn pick(&mut self) -> Option<int>;
fn peek(&self) -> Option<int>;
fn swap(&mut self) -> bool {
if self.len() >= 2 {
let v1 = self.pick().unwrap();
let v2 = self.pick().unwrap();
self.rput(v1);
self.rput(v2);
true
} else {
false
}
}
}
pub struct TempStorage {
// temp impl to avoid trait - box problem
vec: Vec<int>,
is_queue: bool,
}
impl TempStorage {
pub fn new(is_queue: bool) -> TempStorage {
TempStorage { vec: Vec::new(), is_queue: is_queue }
}
}
impl Storage for TempStorage {
fn len(&self) -> uint {
self.vec.len()
}
fn put(&mut self, data: int) {
self.vec.push(data);
}
fn rput(&mut self, data: int) {
if self.is_queue {
self.vec.unshift(data);
} else {
self.vec.push(data);
}
}
fn pick(&mut self) -> Option<int> {
if self.is_queue {
self.vec.shift()
} else {
self.vec.pop()
}
}
fn peek(&self) -> Option<int> {
if self.is_queue {
if !self.vec.is_empty() {
Some(*self.vec.get(0))
} else {
None
}
} else {
match self.vec.last() {
Some(v) => Some(*v),
None => None,
}
}
}
}
/*
pub struct Stack {
vec: Vec<int>,
}
impl Storage for Stack {
fn put(&mut self, data: int) {
self.vec.push(data);
}
fn rput(&mut self, data: int) {
self.vec.push(data);
}
fn pick(&mut self) -> int {
self.vec.pop().unwrap()
}
fn peek(&self) -> int {
*self.vec.last().unwrap()
}
}
#[test]
fn test_stack() {
let mut stack = Stack { vec: Vec::new() };
stack.put(1);
stack.put(2);
stack.put(3);
assert_eq!(3, stack.pick());
assert_eq!(2, stack.pick());
assert_eq!(1, stack.pick());
}
pub struct Queue {
vec: Vec<int>,
}
impl Storage for Queue {
fn put(&mut self, data: int) {
self.vec.push(data);
}
fn rput(&mut self, data: int) {
self.vec.insert(0, data);
}
fn pick(&mut self) -> int {
self.vec.remove(0).unwrap()
}
fn peek(&self) -> int {
*self.vec.get(0)
}
}
#[test]
fn test_queue() {
let mut queue = Queue { vec: Vec::new() };
queue.put(1);
queue.put(2);
queue.put(3);
assert_eq!(1, queue.pick());
assert_eq!(2, queue.pick());
queue.put(4);
queue.put(5);
assert_eq!(3, queue.pick());
assert_eq!(4, queue.pick());
assert_eq!(5, queue.pick());
}
pub struct Extension {
vec: Vec<int>,
}
impl Storage for Extension {
fn put(&mut self, data: int) {
}
fn rput(&mut self, data: int) {
}
fn pick(&mut self) -> int {
0
}
fn peek(&self) -> int {
0
}
}
*/
pub struct Interpreter {
source: Source,
storages: Vec<TempStorage>, // must be array - fixed size
storage_index: uint,
counter: (int, int),
last_move: (int, int),
direction: InterpreterDirection,
out: std::io::LineBufferedWriter<std::io::stdio::StdWriter>,
}
pub static final_draw_counts: [int, ..28] = [0, 2, 4, 4, 2, 5, 5, 3, 5, 7, 9, 9, 7, 9, 9, 8, 4, 4, 6, 2, 4, -1, 3, 4, 3, 4, 4, -1];
impl Interpreter {
pub fn new(source: Source) -> Interpreter {
let mut obj = Interpreter {
source: source,
storages: Vec::new(),
storage_index: 0,
counter: (2, 2),
last_move: (1, 0),
direction: Down,
out: std::io::stdio::stdout(),
};
for x in range(0, hangeul::final0_count) {
let storage = match x {
21 => {
TempStorage::new(true)
}
27 => {
TempStorage::new(true)
}
_ => {
TempStorage::new(false)
}
};
obj.storages.push(storage);
}
return obj;
}
pub fn counter(&self) -> (int, int) {
match self.counter {
(row, col) => (row - 2, col - 2)
}
}
pub fn storage<'a>(&'a mut self) -> &'a mut Storage {
let storage: &mut Storage = self.storages.get_mut(self.storage_index);
storage
}
pub fn instruct(&mut self, instruction: &Instruction) -> bool {
let mut branch: bool = false;
match instruction.operation {
PushConstantOperation(v) => {
let s = self.storage();
s.put(v);
}
BinaryOperation(op) => {
let s = self.storage();
if s.len() >= 2 {
let v1 = s.pick().unwrap();
let v2 = s.pick().unwrap();
let r = op(v1, v2);
s.put(r);
} else {
branch = true;
}
}
PrintIntegerOperation => {
let v = self.storage().pick();
match v {
Some(v) => {
let _ = self.out.write_int(v);
}
None => {
branch = true;
}
}
}
PrintCharOperation => {
let v = self.storage().pick();
match v {
Some(v) => {
let c = std::char::from_u32(v as u32);
let _ = self.out.write_char(c.unwrap());
}
None => {
branch = true;
}
}
}
PopOperation => {
let v = self.storage().pick();
match v {
None => {
branch = true;
}
_ => { }
}
}
PushDuplicationOperation => {
let s = self.storage();
let v = s.peek();
match v {
Some(v) => {
s.put(v);
}
None => {
branch = true;
}
}
}
SwapOperation => {
let s = self.storage();
if !s.swap() {
branch = true;
}
}
MoveToStorageOperation(index) => {
let v = self.storage().pick();
match v {
Some(v) => {
self.storage_index = index;
self.storage().put(v);
}
None => {
branch = true;
}
}
}
ChangeStorageOperation(index) => {
self.storage_index = index;
}
CompareOperation => {
let s = self.storage();
if s.len() >= 2 {
let v1 = s.pick().unwrap();
let v2 = s.pick().unwrap();
s.put(if v2 >= v1 { 1 } else { 0 });
} else {
branch = true;
}
}
BranchOperation => {
match self.storage().pick() {
Some(v) if v == 0 => {
branch = true;
}
Some(_) => { }
None => {
branch = true;
}
}
}
NoOperation => { }
PushIntegerInputOperation => {
let mut reader = std::io::stdin();
let line = reader.read_line().unwrap();
let num: int = from_str(line.as_slice()).unwrap();
self.storage().put(num);
}
PushCharInputOperation => {
let mut reader = std::io::stdin();
let chr = reader.read_char().unwrap();
self.storage().put(chr as int);
}
HaltOperation => {
//pringln!("halt! {:?}", syllable);
return true;
}
};
let mut direction_move = match instruction.move {
RegularMovement(new_direction, row, col) => {
(new_direction, (row, col))
},
AllowHorizontalMovement => match self.direction {
Right | Left => (self.direction, self.last_move),
Up => (Down, (1, 0)),
Down => (Up, (-1, 0)),
},
AllowVerticalMovement => match self.direction {
Up | Down => (self.direction, self.last_move),
Right => (Left, (0, -1)),
Left => (Right, (1, 0)),
},
DisallowMovement => {
(
match self.direction {
Right => Left,
Left => Right,
Up => Down,
Down => Up,
},
match self.last_move {
(row, col) => (-row, -col)
}
)
},
KeepCurrentMovement => {
(self.direction, self.last_move)
},
WallMovement(direction, value) => {
match (self.direction, direction) {
(Up, Up) => {
self.counter = match self.counter {
(_, col) => (value + 1, col),
}
}
(Down, Down) => {
self.counter = match self.counter {
(_, col) => (value - 1, col),
}
}
(Right, Right) => {
self.counter = match self.counter {
(row, _) => (row, value - 1),
}
}
(Left, Left) => {
self.counter = match self.counter {
(row, _) => (row, value + 1),
}
}
_ => { }
}
(self.direction, self.last_move)
}
};
if branch {
direction_move = match direction_move {
(direction, (row, col)) => (
match direction {
Right => Left,
Left => Right,
Up => Down,
Down => Up,
},
(-row, -col)
)
}
}
match direction_move {
(direction, (row_diff, col_diff)) => {
self.direction = direction;
self.counter = match self.counter {
(row, col) => (row + row_diff, col + col_diff)
};
self.last_move = (row_diff, col_diff);
}
};
false
}
pub fn step(&mut self) -> bool {
let syllable = match self.counter {
(row, col) => self.source._get((row, col))
};
self.instruct(&syllable)
}
pub fn execute(&mut self) {
while !self.step() { }
}
}
#[test]
pub fn test_interpreter() {
let mut interpreter = Interpreter::new(Source::new(""));
assert_eq!(interpreter.counter, (0, 0));
interpreter.instruct(&InstructionData::from_char('아'));
assert_eq!(interpreter.counter, (0, 1));
interpreter.instruct(&InstructionData::from_char('희'));
assert_eq!(interpreter.counter, (0, 1));
{
let source = Source::new("아희");
let mut interpreter = Interpreter::new(source);
interpreter.execute();
}
}