Skip to content

Commit 01452f3

Browse files
committed
Made rascal exit gracefully on error.
1 parent 4bc0d29 commit 01452f3

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/analyzer/semantic_analyzer.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::fmt;
22
use std::fmt::{Formatter, Display};
3+
use std::io;
4+
use std::io::Write;
5+
use std::process;
36
use parser::ast::{
47
Program,
58
Block,
@@ -46,7 +49,10 @@ impl SemanticAnalyzer {
4649
}
4750

4851
pub fn analyze(&mut self, program: &Program) {
49-
self.visit_program(program).unwrap();
52+
if let Err(error) = self.visit_program(program) {
53+
writeln!(io::stderr(), "{}", error).unwrap();
54+
process::exit(1);
55+
}
5056
}
5157

5258
fn init_built_ins(&mut self) -> Result<(), String> {

src/interpreter/interpreter.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::fmt;
22
use std::fmt::{Display, Formatter};
3+
use std::io;
4+
use std::io::Write;
5+
use std::process;
36
use parser::ast::{
47
Program,
58
Block,
@@ -45,7 +48,10 @@ impl Interpreter {
4548
}
4649

4750
pub fn interpret(&mut self, program: &Program) {
48-
self.visit_program(program).unwrap();
51+
if let Err(error) = self.visit_program(program) {
52+
writeln!(io::stderr(), "{}", error).unwrap();
53+
process::exit(1);
54+
}
4955
}
5056

5157
fn init_built_ins(&mut self) -> Result<(), String> {

src/lexer/lexer.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use std::io;
2+
use std::io::Write;
3+
use std::process;
14
use itertools::Itertools;
25
use super::source::Source;
36
use super::token::{Token, TokenCache};
@@ -33,15 +36,18 @@ impl<'a> Lexer<'a> {
3336
break;
3437
}
3538
Ok(token) => self.token_cache.push(token),
36-
Err(e) => panic!(e)
39+
Err(error) => {
40+
writeln!(io::stderr(), "{}", error).unwrap();
41+
process::exit(1);
42+
}
3743
}
3844
}
3945
}
4046

4147
fn number(&mut self) -> Result<i32, String> {
4248
let start_int: String = match self.source.current_char() {
4349
Some(c) if c.is_digit(10) => Ok(c),
44-
_ => Err("Internal Lexer Error, expected number")
50+
_ => Err("Internal Lexer Error: Expected number")
4551
}?.to_string();
4652

4753
let final_int = self.source.by_ref()
@@ -51,7 +57,7 @@ impl<'a> Lexer<'a> {
5157
return acc;
5258
})
5359
.parse::<i32>()
54-
.or(Err("Internal Lexer Error, failed to parse integer"))?;
60+
.or(Err("Internal Lexer Error: Failed to parse integer"))?;
5561

5662
return Ok(final_int);
5763
}
@@ -64,8 +70,8 @@ impl<'a> Lexer<'a> {
6470

6571
let decimal = match self.source.next() {
6672
Some(c) if c.is_digit(10) => self.number(),
67-
Some(c) => Err(String::from(format!("Lexing Error: Expected floating point number at: {}.{}", integer, c))),
68-
None => Err(String::from(format!("Lexing Error: Expected floating point number at: {}.", integer)))
73+
Some(c) => Err(String::from(format!("Lexer Error: Expected floating point number at: {}.{}", integer, c))),
74+
None => Err(String::from(format!("Lexer Error: Expected floating point number at: {}.", integer)))
6975
}?;
7076

7177
let mut string_real: String = integer.to_string();
@@ -83,7 +89,7 @@ impl<'a> Lexer<'a> {
8389
fn id(&mut self) -> Result<Token, String> {
8490
let start_id: String = match self.source.current_char() {
8591
Some(c) if c.is_alphabetic() => Ok(c),
86-
_ => Err("Internal Lexer Error, expected alphabetic character")
92+
_ => Err("Internal Lexer Error: Expected alphabetic character")
8793
}?.to_string();
8894
let final_id: String = self.source.by_ref()
8995
.peeking_take_while(| c: &char | c.is_alphanumeric() || c == &'_')
@@ -119,7 +125,7 @@ impl<'a> Lexer<'a> {
119125
fn string(&mut self) -> Result<Token, String> {
120126
match self.source.current_char() {
121127
Some('\'') => Ok(()),
122-
_ => Err("Internal Lexer Error, expected '\'' character")
128+
_ => Err("Internal Lexer Error: Expected '\'' character")
123129
}?;
124130

125131
let final_string: String = self.source.by_ref()
@@ -131,7 +137,7 @@ impl<'a> Lexer<'a> {
131137

132138
match self.source.next() {
133139
Some('\'') => Ok(()),
134-
_ => Err("Internal Lexer Error, expected '\'' character")
140+
_ => Err("Internal Lexer Error: Expected '\'' character")
135141
}?;
136142

137143
return Ok(Token::STRING_LITERAL(final_string));
@@ -140,7 +146,7 @@ impl<'a> Lexer<'a> {
140146
fn assign(&mut self) -> Result<Token, String> {
141147
return match (self.source.current_char(), self.source.next()) {
142148
(Some(':'), Some('=')) => Ok(Token::ASSIGN),
143-
_ => Err(String::from("Internal Lexer Error: expected ':=' characters"))
149+
_ => Err(String::from("Internal Lexer Error: Expected ':=' characters"))
144150
};
145151
}
146152

@@ -165,7 +171,7 @@ impl<'a> Lexer<'a> {
165171
_ => Ok(Token::GREATER_THAN)
166172
},
167173
Some('=') => Ok(Token::EQUAL),
168-
_ => Err(String::from("Internal Lexer Error: expected comparison character"))
174+
_ => Err(String::from("Internal Lexer Error: Expected comparison character"))
169175
};
170176
}
171177

@@ -192,7 +198,7 @@ impl<'a> Lexer<'a> {
192198
Some('(') => Ok(Token::LPAREN),
193199
Some(')') => Ok(Token::RPAREN),
194200
None => Ok(Token::EOF),
195-
Some(character) => Err(format!("Unknown Token: '{}'", character)),
201+
Some(character) => Err(format!("Lexer Error: Unknown Token '{}'", character)),
196202
}
197203
}
198204
}

0 commit comments

Comments
 (0)