Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions crates/fmt/src/lint_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,19 @@ impl Visitor for ShadowedBinding {
self.register_pattern(pattern, line, col);
}

fn visit_expression(&mut self, expr: &Expression) {

}
todo!(); /// Task 8

// fn visit_expression(&mut self, expr: &Expression) {
// if let Expression::Function { parameter, body, .. } = expr {
// self.scope_stack.push(HashMap::new());
// for param in parameter {
// if self.scope_stack.contains(param.name) {

// }
// self.register_pattern(param.name, line, col);
// }
// }
// }
}

impl LintRule for ShadowedBinding {
Expand Down Expand Up @@ -407,7 +417,8 @@ impl Visitor for DeadCode {
self.visit_statement(s);
}

self.returned = saved;
let block_returned = self.returned;
self.returned = saved || block_returned;
}

walk_statement(self, stmt);
Expand Down
16 changes: 16 additions & 0 deletions src/evaluator/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ const MAX_CALL_DEPTH: usize = 500;
pub struct Evaluator {
pub loop_depth: usize,
pub call_depth: usize,
pub call_stack: Vec<CallFrame>,
pub module_cache: HashMap<String, Object>,
}

pub struct CallFrame {
pub name:String,
pub call_line: usize,
pub call_column: usize
}

type Env = Rc<RefCell<Environment>>;

impl Evaluator {
pub fn new() -> Self {
let mut e = Evaluator {
loop_depth: 0,
call_depth: 0,
call_stack: Vec::new(),
module_cache: HashMap::new(),
};
e.preload_stdlib();
Expand Down Expand Up @@ -1696,9 +1704,17 @@ impl Evaluator {
extended.borrow_mut().set(param.name.clone(), val)
}

self.call_stack.push(CallFrame{
name:parameters[0].name.clone(),
call_line:line,
call_column:column
});

self.call_depth += 1;
let result = self.eval_statement(&body, &extended);
self.call_depth -= 1;
self.call_stack.pop();

match result {
Object::Return(v) => *v,
other => other,
Expand Down
14 changes: 12 additions & 2 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ impl Parser {
}

// cur_token is ';' or last token of value expression
if !self.expect_peak(TokenType::Semicolon){
return None;
}
let end_line = self.cur_token.line;
let end_column = self.cur_token.column + 1;
Some(Statement::Const {
Expand Down Expand Up @@ -1362,7 +1365,9 @@ impl Parser {
fn parse_continue_statement(&mut self) -> Option<Statement> {
let line = self.cur_token.line;
let column = self.cur_token.column;
self.expect_peak(TokenType::Semicolon);
if !self.expect_peak(TokenType::Semicolon){
return None;
}
// cur_token is ';' if found, otherwise 'continue'
let end_line = self.cur_token.line;
let end_column = self.cur_token.column + 1;
Expand All @@ -1377,7 +1382,9 @@ impl Parser {
fn parse_break_statement(&mut self) -> Option<Statement> {
let line = self.cur_token.line;
let column = self.cur_token.column;
self.expect_peak(TokenType::Semicolon);
if !self.expect_peak(TokenType::Semicolon){
return None;
}
// cur_token is ';' if found, otherwise 'break'
let end_line = self.cur_token.line;
let end_column = self.cur_token.column + 1;
Expand Down Expand Up @@ -1648,6 +1655,9 @@ impl Parser {
};

// cur_token is ';' or last token of value
if !self.expect_peak(TokenType::Semicolon){
return None;
}
let end_line = self.cur_token.line;
let end_column = self.cur_token.column + 1;
Some(Statement::Let {
Expand Down
11 changes: 11 additions & 0 deletions src/repl/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,16 @@ pub fn run_repl() {
if !parser.errors.is_empty() {
for err in &parser.errors {
show_error(&input, &err.message, err.line, err.column);
if !evaluator.call_stack.is_empty() {
for err in evaluator.call_stack.iter().clone() {
show_error(&input, &err.name, err.call_line, err.call_column);
}
}
}
continue;
}


let result = evaluator.eval(&program, &env);
match result {
Object::Error {
Expand Down Expand Up @@ -149,6 +155,11 @@ pub fn execute(input: String) {
column,
} => {
show_error(&input, message, line, column);
if !evaluator.call_stack.is_empty(){
for e in evaluator.call_stack.iter().clone() {
show_error(&input, &e.name, e.call_line, e.call_column);
}
}
std::process::exit(1);
}
Object::Null => {}
Expand Down
Loading