Решение на Логически изрази от Димитър Тагарев
Към профила на Димитър Тагарев
Резултати
- 0 точки от тестове
- 0 бонус точки
- 0 точки общо
- 0 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::collections::VecDeque;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expr {
Atom(char),
Not(Box<Expr>),
And(Vec<Expr>),
Or(Vec<Expr>),
}
#[derive(Debug, PartialEq, Eq)]
pub enum ParseError {
UnexpectedExpr,
UnexpectedUnaryOp,
UnexpectedBinOp,
UnexpectedParen,
UnexpectedEnd,
}
pub struct SimpleExprParser {
stack: VecDeque<Expr>,
ops: VecDeque<char>,
expect_atom: bool,
}
impl SimpleExprParser {
pub fn new() -> SimpleExprParser {
SimpleExprParser {
stack: VecDeque::new(),
ops: VecDeque::new(),
expect_atom: true,
}
}
pub fn push_atom(&mut self, c: char) -> Result<(), ParseError> {
if !self.expect_atom {
return Err(ParseError::UnexpectedExpr);
}
self.stack.push_back(Expr::Atom(c));
self.expect_atom = false;
Ok(())
}
pub fn push_op(&mut self, op: char) -> Result<(), ParseError> {
match op {
'!' => {
if !self.expect_atom {
return Err(ParseError::UnexpectedUnaryOp);
}
self.ops.push_back('!');
}
'&' | '|' => {
if self.expect_atom {
return Err(ParseError::UnexpectedBinOp);
}
while let Some(&top_op) = self.ops.back() {
if top_op == '!' || (top_op == '&' && op == '|') {
self.apply_op()?;
} else {
break;
}
}
self.ops.push_back(op);
self.expect_atom = true;
}
_ => panic!("Invalid operator"),
}
Ok(())
}
pub fn finish(mut self) -> Result<Expr, ParseError> {
while let Some(&top_op) = self.ops.back() {
if top_op == '!' && self.expect_atom {
return Err(ParseError::UnexpectedEnd);
}
self.apply_op()?;
}
if self.stack.len() != 1 {
return Err(ParseError::UnexpectedEnd);
}
Ok(self.stack.pop_back().unwrap())
}
fn apply_op(&mut self) -> Result<(), ParseError> {
if let Some(op) = self.ops.pop_back() {
match op {
'!' => {
if let Some(expr) = self.stack.pop_back() {
self.stack.push_back(Expr::Not(Box::new(expr)));
} else {
return Err(ParseError::UnexpectedUnaryOp);
}
}
'&' => self.combine_op(Expr::And)?,
'|' => self.combine_op(Expr::Or)?,
_ => unreachable!(),
}
}
Ok(())
}
fn combine_op(&mut self, constructor: fn(Vec<Expr>) -> Expr) -> Result<(), ParseError> {
let mut args = Vec::new();
while let Some(top_op) = self.ops.back() {
if *top_op == '&' || *top_op == '|' {
break;
}
if let Some(expr) = self.stack.pop_back() {
args.push(expr);
} else {
return Err(ParseError::UnexpectedEnd);
}
}
if args.is_empty() {
return Err(ParseError::UnexpectedBinOp);
}
args.reverse();
self.stack.push_back(constructor(args));
Ok(())
}
}
impl Default for SimpleExprParser {
fn default() -> Self {
Self::new()
}
}
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20241224-258381-1ctqn/solution) error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:372:58 | 372 | assert_eq!(eval(&expr!(atom('A')), &['A'], &[]), Value::True); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:373:58 | 373 | assert_eq!(eval(&expr!(atom('A')), &[], &['A']), Value::False); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:375:66 | 375 | assert_eq!(eval(&expr!(not(atom('B'))), &['A'], &['B']), Value::True); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:376:66 | 376 | assert_eq!(eval(&expr!(not(atom('B'))), &['B'], &['A']), Value::False); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:378:79 | 378 | assert_eq!(eval(&expr!(and(atom('A'), atom('B'))), &['A', 'B'], &[]), Value::True); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:379:77 | 379 | assert_eq!(eval(&expr!(and(atom('A'), atom('B'))), &['A'], &['B']), Value::False); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:380:76 | 380 | assert_eq!(eval(&expr!(or(atom('A'), atom('B'))), &['A'], &['B']), Value::True); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:381:78 | 381 | assert_eq!(eval(&expr!(or(atom('A'), atom('B'))), &[], &['A', 'B']), Value::False); | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:390:13 | 390 | Value::False | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:394:13 | 394 | Value::True | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:398:13 | 398 | Value::False | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:402:13 | 402 | Value::True | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:422:13 | 422 | Value::False | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:427:13 | 427 | Value::True | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:440:13 | 440 | Value::False | ^^^^^ use of undeclared type `Value` error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:445:13 | 445 | Value::True | ^^^^^ use of undeclared type `Value` error[E0412]: cannot find type `ExprParser` in this scope --> tests/solution_test.rs:58:27 | 58 | fn feed_full(parser: &mut ExprParser, text: &str) -> Result<(), ParseError> { | ^^^^^^^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:177:26 | 177 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:181:26 | 181 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:185:26 | 185 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:189:26 | 189 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:198:26 | 198 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:205:26 | 205 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:217:26 | 217 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:224:26 | 224 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:236:26 | 236 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:253:26 | 253 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:326:26 | 326 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:329:26 | 329 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:333:26 | 333 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:339:26 | 339 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:344:26 | 344 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:348:26 | 348 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:353:26 | 353 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0433]: failed to resolve: use of undeclared type `ExprParser` --> tests/solution_test.rs:360:26 | 360 | let mut parser = ExprParser::new(); | ^^^^^^^^^^ use of undeclared type `ExprParser` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:372:20 | 372 | assert_eq!(eval(&expr!(atom('A')), &['A'], &[]), Value::True); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:373:20 | 373 | assert_eq!(eval(&expr!(atom('A')), &[], &['A']), Value::False); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:375:20 | 375 | assert_eq!(eval(&expr!(not(atom('B'))), &['A'], &['B']), Value::True); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:376:20 | 376 | assert_eq!(eval(&expr!(not(atom('B'))), &['B'], &['A']), Value::False); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:378:20 | 378 | assert_eq!(eval(&expr!(and(atom('A'), atom('B'))), &['A', 'B'], &[]), Value::True); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:379:20 | 379 | assert_eq!(eval(&expr!(and(atom('A'), atom('B'))), &['A'], &['B']), Value::False); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:380:20 | 380 | assert_eq!(eval(&expr!(or(atom('A'), atom('B'))), &['A'], &['B']), Value::True); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:381:20 | 381 | assert_eq!(eval(&expr!(or(atom('A'), atom('B'))), &[], &['A', 'B']), Value::False); | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:389:13 | 389 | eval(&expr!(not(and(atom('A'), atom('B')))), &['A', 'B'], &[]), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:393:13 | 393 | eval(&expr!(not(and(atom('A'), atom('B')))), &['A'], &['B']), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:397:13 | 397 | eval(&expr!(not(or(atom('A'), atom('B')))), &['A'], &['B']), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:401:13 | 401 | eval(&expr!(not(or(atom('A'), atom('B')))), &[], &['A', 'B']), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:410:20 | 410 | assert_eq!(eval(&expr!(atom('A')), &[], &[]), Value::Expr(expr!(atom('A')))); | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:410:55 | 410 | assert_eq!(eval(&expr!(atom('A')), &[], &[]), Value::Expr(expr!(atom('A')))); | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:412:13 | 412 | eval(&expr!(not(atom('B'))), &[], &[]), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:413:13 | 413 | Value::Expr(expr!(not(atom('B')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:417:13 | 417 | eval(&expr!(and(atom('A'), atom('B'), atom('C'))), &['B'], &[]), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:418:13 | 418 | Value::Expr(expr!(and(atom('A'), atom('C')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:421:13 | 421 | eval(&expr!(and(atom('A'), atom('B'), atom('C'))), &[], &['B']), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:426:13 | 426 | eval(&expr!(or(atom('A'), atom('B'), atom('C'))), &['B'], &[]), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:430:13 | 430 | eval(&expr!(or(atom('A'), atom('B'), atom('C'))), &[], &['B']), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:431:13 | 431 | Value::Expr(expr!(or(atom('A'), atom('C')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:435:13 | 435 | eval(&expr!(and(atom('A'), not(atom('B')), atom('C'))), &[], &['B']), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:436:13 | 436 | Value::Expr(expr!(and(atom('A'), atom('C')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:439:13 | 439 | eval(&expr!(and(atom('A'), not(atom('B')), atom('C'))), &['B'], &[]), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:444:13 | 444 | eval(&expr!(or(atom('A'), not(atom('B')), atom('C'))), &[], &['B']), | ^^^^ not found in this scope error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:448:13 | 448 | eval(&expr!(or(atom('A'), not(atom('B')), atom('C'))), &['B'], &[]), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:449:13 | 449 | Value::Expr(expr!(or(atom('A'), atom('C')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:458:13 | 458 | eval(&expr!(and(atom('A'), atom('B'), atom('C'))), &['A', 'C'], &[]), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:459:13 | 459 | Value::Expr(expr!(atom('B'))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:462:13 | 462 | eval(&expr!(or(atom('A'), atom('B'), atom('C'))), &[], &['A', 'C']), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:463:13 | 463 | Value::Expr(expr!(atom('B'))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:467:13 | 467 | eval(&expr!(and(atom('A'), not(atom('B')), atom('C'))), &['A', 'C'], &[]), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:468:13 | 468 | Value::Expr(expr!(not(atom('B')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:471:13 | 471 | eval(&expr!(or(atom('A'), not(atom('B')), atom('C'))), &[], &['A', 'C']), | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:472:13 | 472 | Value::Expr(expr!(not(atom('B')))) | ^^^^^ use of undeclared type `Value` error[E0425]: cannot find function `eval` in this scope --> tests/solution_test.rs:481:13 | 481 | eval( | ^^^^ not found in this scope error[E0433]: failed to resolve: use of undeclared type `Value` --> tests/solution_test.rs:491:13 | 491 | Value::Expr(expr!(or(atom('X'), atom('B'), not(atom('D')), atom('Y')))) | ^^^^^ use of undeclared type `Value` Some errors have detailed explanations: E0412, E0425, E0433. For more information about an error, try `rustc --explain E0412`. error: could not compile `solution` due to 73 previous errors