-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple calculator.rs
159 lines (119 loc) · 3.08 KB
/
simple calculator.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
/*
simple rust calculator
add_op := '+' | '-'
mul_op := '*' | '/'
digits := {'+' | '-'} [0..9] {[0..9]}
expr := term {add_op term}
term := factor {mul_op factor}
factor := digits | '(' expr ')'
*/
use std::io;
type CalcInt = i64;
struct ParseState {
line: String,
index: usize,
}
fn token(ps: &ParseState) -> char {
ps.line.as_bytes()[ps.index-1] as char
}
fn fatal(msg: String) {
eprintln!("fatal: {} ", msg);
::std::process::exit(1);
}
fn lex_match(ps: &mut ParseState, expected: char) {
if token(ps) == expected {
ps.index += 1;
return
}
fatal(format!("error matching {} at index {}", expected, ps.index));
std::process::exit(1);
}
fn scan_digits(ps: &mut ParseState) -> CalcInt {
const BASE: CalcInt = 10;
let mut val: CalcInt = 0;
loop {
let digit: CalcInt;
match token(ps) {
'0' => digit = 0,
'1' => digit = 1,
'2' => digit = 2,
'3' => digit = 3,
'4' => digit = 4,
'5' => digit = 5,
'6' => digit = 6,
'7' => digit = 7,
'8' => digit = 8,
'9' => digit = 9,
_ => break
}
if digit >= BASE {
fatal(format!("Digit {} out of range for base {}", digit, BASE));
}
if val > (std::i64::MAX - digit)/BASE {
fatal(format!("Integer overflow"));
}
val = val*BASE + digit;
ps.index += 1;
}
return val;
}
fn factor(ps: &mut ParseState) -> CalcInt {
let value: CalcInt;
if token(ps) == '(' {
lex_match(ps, '(');
value = expr(ps);
lex_match(ps, ')');
} else if token(ps).is_digit(10) || token(ps) == '+' || token(ps) == '-' {
value = scan_digits(ps);
} else {
value = 0;
fatal("bad factor".to_string());
}
return value;
}
fn term(ps: &mut ParseState) -> CalcInt {
let mut value: CalcInt = factor(ps);
while token(ps) == '*' || token(ps) == '/' {
match token(ps) {
'*' => {
lex_match(ps, '*');
value *= factor(ps);
},
'/' => {
lex_match(ps, '/');
value /= factor(ps);
},
_ => {},
}
}
return value;
}
fn expr(ps: &mut ParseState) -> CalcInt {
let mut value: CalcInt = term(ps);
match token(ps) {
'+' => {
lex_match(ps, '+');
value += term(ps);
}
'-' => {
lex_match(ps, '-');
value -= term(ps);
}
_ => {},
}
return value;
}
fn main() {
let mut ps = ParseState{
line: String::new(),
index: 1, // index of NEXT char to read
};
print!("expr:\n\t");
match io::stdin().read_line(&mut ps.line) {
Ok(_n) => {
let result = expr(&mut ps);
println!("result: {}", result);
}
Err(error) => fatal(format!("error: {}", error)),
}
}