added exit code

This commit is contained in:
hendrik 2024-08-07 20:17:45 +02:00
parent 5e7c06db8d
commit d7290a3ea5
5 changed files with 26 additions and 10 deletions

View File

@ -2,7 +2,7 @@
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: true
debug: false
# Use this to change the Rust version used to run your code
# on Codecrafters.

View File

@ -1,6 +1,8 @@
use std::env;
use std::fs;
use std::io::{self, Write};
use std::process::exit;
use std::result;
use interpreter_starter_rust::scanner::scanner::Scanner;
use interpreter_starter_rust::scanner::scanner_runner::ScannerRunner;
@ -26,8 +28,11 @@ fn main() {
String::new()
});
let mut foo = Scanner::new(&file_contents);
foo.scan();
let result = foo.scan();
println!("{}", foo.generate_output());
if let Err(res) = result {
exit(res as i32);
}
// Uncomment this block to pass the first stage
// if !file_contents.is_empty() {
// panic!("Scanner not implemented");

View File

@ -17,7 +17,7 @@ impl EvaluationError {
#[derive(Clone, Copy)]
pub enum Severity {
ERROR = 5,
ERROR = 65,
WARN = 4,
INFO = 3,
DEBUG = 2,

View File

@ -1,4 +1,7 @@
use std::fmt::{self};
use std::{
fmt::{self},
result,
};
use crate::token_type::TokenType;
@ -32,16 +35,17 @@ impl<'a> Scanner<'a> {
println!("yay");
}
fn scan_token(&mut self) {
fn scan_token(&mut self) -> usize {
let Some(char) = self.evaluator.advance() else {
panic!("Expected not yet eof");
};
match self.match_token(char) {
Ok(_) => {}
Ok(_) => 0,
Err(x) => {
if x.severity() as usize >= self.log_level as usize {
eprintln!("{}", x.get_msg());
}
x.severity() as usize
}
}
}
@ -193,11 +197,18 @@ impl fmt::Debug for Scanner<'_> {
}
impl ScannerRunner for Scanner<'_> {
fn scan(&mut self) {
fn scan(&mut self) -> Result<(), usize> {
let mut result = 0;
while !self.evaluator.is_at_end() {
self.scan_token();
result = std::cmp::max(result, self.scan_token());
}
self.tokens
.add_token(TokenType::EOF, String::new(), self.evaluator.line());
if result == 0 {
Ok(())
} else {
Err(result)
}
}
}

View File

@ -1,3 +1,3 @@
pub trait ScannerRunner {
fn scan(&mut self);
}
fn scan(&mut self) -> Result<(), usize>;
}