-
Notifications
You must be signed in to change notification settings - Fork 3
/
tem.txt
662 lines (577 loc) · 17.5 KB
/
tem.txt
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
```
PARAMPARA PRATISHTA ANUSHASHAN
A BOLE TOH 10
C BOLE TOH INPUT LE LE RE BABA
AGAR A > 3 TAB
; Statement
WARNA AGAR B > 3 TAB
; Statement
NHI TOH
; Statement
BAS ITNA HI
JAB TAK HAI JAAN A > 3 TAB TAK
; Statement here!
JAHAN
PRINT BASANTI PRINT A
KHATAM TATA BYE BYE
```
is a toy lang (made using Rust)and
PARAMPARA PRATISHTA ANUSHASHAN - Start of Program
BOLE TOH - Initialization
AGAR - If Statement
TAB - Then Statement
WARNA AGAR - ElseIF Statement
NHI TOH - Else Statement
BAS ITNA HI - End of IF Statement
INPUT LE LE RE BABA - Take Input from Console
JAB TAK HAI JAAN - While Statement
TAB TAK - Then Statement after While Statement
JAHAN - End of While Statement
PRINT BASANTI PRINT - Print Statement
KHATAM TATA BYE BYE - End of Program
I want to make a lexer/tokenizer for the same .
which also includes
Variable (Limit Data Type to f64)
Input (Numeric) /Output
Boolean Expressions parsing
Arithmetic Operations (+, -, /, *, %)
Comments(Starting with '@' Symbol)
/// parser for expressions calculator.
use std::{
cmp::{Ord, Ordering},
convert::TryFrom,
error::Error,
fmt,
io::prelude::*,
iter::Peekable,
slice::Iter,
};
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Token {
Plus,
Dash,
Star,
Slash,
Carrot,
RightParen,
LeftParen,
End,
Number(i64),
}
impl Token {
fn is_binary(&self) -> bool {
match self {
Token::Plus => true,
Token::Dash => true,
Token::Star => true,
Token::Slash => true,
Token::Carrot => true,
_ => false,
}
}
}
#[derive(Debug, PartialEq, Eq)]
enum Operator {
Add,
Multiply,
Divide,
Subtract,
Power,
Negative,
Sentinel,
}
impl Operator {
fn cmp_val(&self) -> usize {
match self {
Operator::Negative => 4,
Operator::Power => 6,
Operator::Multiply => 5,
Operator::Divide => 5,
Operator::Add => 3,
Operator::Subtract => 3,
Operator::Sentinel => 1,
}
}
}
impl TryFrom<Token> for Operator {
type Error = &'static str;
fn try_from(token: Token) -> Result<Self, Self::Error> {
match token {
Token::Plus => Ok(Operator::Add),
Token::Star => Ok(Operator::Multiply),
Token::Dash => Ok(Operator::Subtract),
Token::Carrot => Ok(Operator::Power),
Token::Slash => Ok(Operator::Divide),
_ => Err("Can only convert operators"),
}
}
}
impl PartialOrd for Operator {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp_val().cmp(&other.cmp_val()))
}
}
impl Ord for Operator {
fn cmp(&self, other: &Self) -> Ordering {
self.cmp_val().cmp(&other.cmp_val())
}
}
#[derive(Debug, PartialEq, Eq)]
enum Expression {
Binary(Operator, Box<Expression>, Box<Expression>),
Unary(Operator, Box<Expression>),
Number(i64),
}
impl Expression {
fn eval(&mut self) -> i64 {
match self {
Expression::Number(n) => *n,
Expression::Unary(_negative, expr) => -1 * expr.eval(),
Expression::Binary(Operator::Add, expr1, expr2) => expr1.eval() + expr2.eval(),
Expression::Binary(Operator::Multiply, expr1, expr2) => expr1.eval() * expr2.eval(),
Expression::Binary(Operator::Subtract, expr1, expr2) => expr1.eval() - expr2.eval(),
Expression::Binary(Operator::Power, expr1, expr2) => {
let expr1 = expr1.eval();
let mut expr2 = expr2.eval();
if expr2 < 0 {
expr2 *= -1;
println!("Negative numbers aren't allowed in exponents");
}
match expr1.checked_pow(expr2 as u32) {
Some(v) => v,
None => {
eprintln!("{} ^ {} would overflow", expr1, expr2);
0
}
}
}
Expression::Binary(Operator::Divide, expr1, expr2) => expr1.eval() / expr2.eval(),
_ => {
panic!("Unreachable code: for expr {:?}", self);
}
}
}
}
#[derive(Debug)]
struct SyntaxError {
message: String,
level: String,
}
impl SyntaxError {
fn new_lex_error(message: String) -> Self {
SyntaxError {
message,
level: "Lex".to_string(),
}
}
fn new_parse_error(message: String) -> Self {
SyntaxError {
message,
level: "Parse".to_string(),
}
}
}
impl fmt::Display for SyntaxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} Error {}", self.level, self.message)
}
}
impl Error for SyntaxError {}
struct ClimbingParser<'a> {
iter: &'a mut Peekable<Iter<'a, Token>>,
}
impl<'a> ClimbingParser<'a> {
fn new(iter: &'a mut Peekable<Iter<'a, Token>>) -> Self {
ClimbingParser { iter }
}
fn assert_next(&mut self, token: Token) -> Result<(), SyntaxError> {
let next = self.iter.next();
if let None = next {
return Err(SyntaxError::new_parse_error(
"Unexpected end of input".to_string(),
));
}
if *next.unwrap() != token {
return Err(SyntaxError::new_parse_error(format!(
"Expected {:?} actual {:?}",
token,
next.unwrap(),
)));
}
Ok(())
}
fn primary(&mut self) -> Result<Expression, SyntaxError> {
match self.iter.next().unwrap() {
Token::Dash => {
let op = Operator::Negative;
let expr = self.expression(op.cmp_val())?;
Ok(Expression::Unary(op, Box::new(expr)))
}
Token::RightParen => {
let expr = self.expression(0)?;
self.assert_next(Token::LeftParen)?;
Ok(expr)
}
Token::Number(n) => Ok(Expression::Number(*n)),
tok => Err(SyntaxError::new_parse_error(format!(
"Unexpected token {:?}",
tok
))),
}
}
fn expression(&mut self, precedence: usize) -> Result<Expression, SyntaxError> {
let mut expr = self.primary()?;
while let Some(tok) = self.iter.peek() {
if !tok.is_binary() {
break;
}
let operator = Operator::try_from(**tok).unwrap();
if operator.cmp_val() < precedence {
break;
}
self.iter.next();
let inner_precedence = match operator {
Operator::Power => operator.cmp_val(),
_ => 1 + operator.cmp_val(),
};
let rhs = self.expression(inner_precedence)?;
expr = Expression::Binary(operator, Box::new(expr), Box::new(rhs));
}
Ok(expr)
}
fn parse(&mut self) -> Result<Expression, SyntaxError> {
let ast = self.expression(0)?;
self.assert_next(Token::End)?;
Ok(ast)
}
}
struct DescentParser<'a> {
iter: &'a mut Peekable<Iter<'a, Token>>,
}
impl<'a> DescentParser<'a> {
fn new(iter: &'a mut Peekable<Iter<'a, Token>>) -> Self {
DescentParser { iter }
}
fn assert_next(&mut self, token: Token) -> Result<(), SyntaxError> {
let next = self.iter.next();
if let None = next {
return Err(SyntaxError::new_parse_error(
"Unexpected end of input".to_string(),
));
}
if *next.unwrap() != token {
return Err(SyntaxError::new_parse_error(format!(
"Expected {:?} actual {:?}",
token,
next.unwrap(),
)));
}
Ok(())
}
fn primary(&mut self) -> Result<Expression, SyntaxError> {
let next = self.iter.next().unwrap();
match next {
Token::Number(n) => Ok(Expression::Number(*n)),
Token::RightParen => {
let expr = self.expression()?;
self.assert_next(Token::LeftParen)?;
Ok(expr)
}
Token::Dash => {
let expr = self.factor()?;
Ok(Expression::Unary(Operator::Negative, Box::new(expr)))
}
_ => Err(SyntaxError::new_parse_error(format!(
"Unexpected token {:?}",
next
))),
}
}
fn factor(&mut self) -> Result<Expression, SyntaxError> {
let expr = self.primary()?;
let next = self.iter.peek().unwrap();
if *next == &Token::Carrot {
self.iter.next();
let rhs = self.factor()?;
return Ok(Expression::Binary(
Operator::Power,
Box::new(expr),
Box::new(rhs),
));
}
Ok(expr)
}
fn term(&mut self) -> Result<Expression, SyntaxError> {
let mut expr: Expression = self.factor()?;
loop {
let next = self.iter.peek().unwrap();
match next {
Token::Star => {
self.iter.next();
let rhs = self.factor()?;
expr = Expression::Binary(Operator::Multiply, Box::new(expr), Box::new(rhs));
}
Token::Slash => {
self.iter.next();
let rhs = self.factor()?;
expr = Expression::Binary(Operator::Divide, Box::new(expr), Box::new(rhs));
}
_ => break,
};
}
Ok(expr)
}
fn expression(&mut self) -> Result<Expression, SyntaxError> {
let mut expr: Expression = self.term()?;
loop {
let next = self.iter.peek().unwrap();
match next {
Token::Plus => {
self.iter.next();
let rhs = self.term()?;
expr = Expression::Binary(Operator::Add, Box::new(expr), Box::new(rhs));
}
Token::Dash => {
self.iter.next();
let rhs = self.term()?;
expr = Expression::Binary(Operator::Subtract, Box::new(expr), Box::new(rhs));
}
_ => break,
};
}
Ok(expr)
}
fn parse(&mut self) -> Result<Expression, SyntaxError> {
let ast = self.expression()?;
self.assert_next(Token::End)?;
Ok(ast)
}
}
struct ShuntingYardParser<'a> {
iter: &'a mut Peekable<Iter<'a, Token>>,
operator_stack: Vec<Operator>,
operand_stack: Vec<Expression>,
}
impl<'a> ShuntingYardParser<'a> {
fn new(iter: &'a mut Peekable<Iter<'a, Token>>) -> Self {
ShuntingYardParser {
iter,
operator_stack: vec![Operator::Sentinel],
operand_stack: vec![],
}
}
fn assert_next(&mut self, token: Token) -> Result<(), SyntaxError> {
let next = self.iter.next();
if let None = next {
return Err(SyntaxError::new_parse_error(
"Unexpected end of input".to_string(),
));
}
if *next.unwrap() != token {
return Err(SyntaxError::new_parse_error(format!(
"Expected {:?} actual {:?}",
next.unwrap(),
token
)));
}
Ok(())
}
fn primary(&mut self) -> Result<(), SyntaxError> {
let next = self.iter.peek();
if let None = next {
return Err(SyntaxError::new_parse_error(
"Unexpected end of input".to_string(),
));
}
let next = *self.iter.peek().unwrap();
match next {
Token::Number(num) => {
let _token = self.iter.next();
self.operand_stack.push(Expression::Number(*num));
}
Token::RightParen => {
let _token = self.iter.next();
self.operator_stack.push(Operator::Sentinel);
self.expression()?;
self.assert_next(Token::LeftParen)?;
self.operator_stack.pop();
}
Token::Dash => {
let _token = self.iter.next();
self.push_operator(Operator::Negative);
self.primary()?;
}
_ => {
return Err(SyntaxError::new_parse_error(format!(
"Unexpected token {:?}",
next
)));
}
}
Ok(())
}
fn expression(&mut self) -> Result<(), SyntaxError> {
self.primary()?;
while let Some(tok) = self.iter.peek() {
if tok.is_binary() {
let token = self.iter.next().unwrap();
let op = Operator::try_from(*token).unwrap();
self.push_operator(op);
self.primary()?;
} else {
break;
}
}
while *self.operator_stack.last().unwrap() != Operator::Sentinel {
self.pop_operator();
}
Ok(())
}
fn parse(&mut self) -> Result<(), SyntaxError> {
self.expression()?;
self.assert_next(Token::End)
}
fn pop_operator(&mut self) {
let top = self.operator_stack.pop().unwrap();
let right = self.operand_stack.pop().unwrap();
match top {
Operator::Negative => {
self.operand_stack
.push(Expression::Unary(top, Box::new(right)));
}
_ => {
let left = self.operand_stack.pop().unwrap();
self.operand_stack
.push(Expression::Binary(top, Box::new(left), Box::new(right)));
}
};
}
fn push_operator(&mut self, op: Operator) {
while &op < self.operator_stack.last().unwrap() {
self.pop_operator();
}
self.operator_stack.push(op);
}
}
fn lex(code: String) -> Result<Vec<Token>, SyntaxError> {
let mut iter = code.chars().peekable();
let mut tokens: Vec<Token> = Vec::new();
let mut leftover: Option<char> = None;
loop {
let ch = match leftover {
Some(ch) => ch,
None => match iter.next() {
None => break,
Some(ch) => ch,
},
};
leftover = None;
match ch {
' ' => continue,
'+' => tokens.push(Token::Plus),
'*' => tokens.push(Token::Star),
'/' => tokens.push(Token::Slash),
'^' => tokens.push(Token::Carrot),
')' => tokens.push(Token::LeftParen),
'(' => tokens.push(Token::RightParen),
'-' => tokens.push(Token::Dash),
ch if ch.is_ascii_digit() => {
let number_stream: String = iter
.by_ref()
.take_while(|c| match c.is_ascii_digit() {
true => true,
false => {
leftover = Some(*c);
false
}
})
.collect();
let number: i64 = format!("{}{}", ch, number_stream).parse().unwrap();
tokens.push(Token::Number(number));
}
_ => {
return Err(SyntaxError::new_lex_error(format!(
"Unrecognized character {}",
ch
)))
}
}
}
tokens.push(Token::End);
Ok(tokens)
}
fn get_line() -> String {
print!("> ");
std::io::stdout().flush().unwrap();
let mut input = String::new();
match std::io::stdin().read_line(&mut input) {
Ok(_s) => {}
Err(_e) => {}
};
input.trim().to_string()
}
fn eval_shunting_yard(code: String) -> Result<(), Box<dyn Error>> {
let tokens = lex(code)?;
let mut token_iter = tokens.iter().peekable();
let mut parser = ShuntingYardParser::new(&mut token_iter);
let result = parser.parse();
match result {
Ok(()) => {}
Err(e) => return Err(Box::new(e)),
}
for mut expr in parser.operand_stack {
println!("{}", expr.eval());
}
Ok(())
}
fn eval_climbing(code: String) -> Result<(), Box<dyn Error>> {
let tokens = lex(code)?;
let mut token_iter = tokens.iter().peekable();
let mut parser = ClimbingParser::new(&mut token_iter);
let result = parser.parse();
match result {
Ok(mut ast) => println!("{}", ast.eval()),
Err(e) => return Err(Box::new(e)),
}
Ok(())
}
fn eval_descent(code: String) -> Result<(), Box<dyn Error>> {
let tokens = lex(code)?;
let mut token_iter = tokens.iter().peekable();
let mut parser = DescentParser::new(&mut token_iter);
let result = parser.parse();
match result {
Ok(mut ast) => println!("{}", ast.eval()),
Err(e) => return Err(Box::new(e)),
}
Ok(())
}
fn eval(code: String, shunting_yard: bool, climbing: bool) -> Result<(), Box<dyn Error>> {
if shunting_yard {
return eval_shunting_yard(code);
}
if climbing {
return eval_climbing(code);
}
eval_descent(code)
}
fn run_repl() -> Result<(), Box<dyn Error>> {
loop {
let line = get_line();
if line == "quit" {
break ();
}
if let Err(e) = eval(line, false, true) {
println!("Error: {}", e);
}
}
Ok(())
}
fn run() -> Result<(), Box<dyn Error>> {
run_repl()
}
fn main() {
if let Err(e) = run() {
eprintln!("Error: {}", e);
}
}