aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler.rs')
-rw-r--r--src/compiler.rs26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 2caa4e9..d12ee4b 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -161,6 +161,7 @@ macro_rules! push_scope {
};
}
+/// Helper function for adding operations to the given block.
fn add_op(compiler: &Compiler, block: &mut Block, op: Op) -> usize {
block.add(op, compiler.line())
}
@@ -207,6 +208,7 @@ impl Compiler {
&mut self.frame_mut().stack
}
+ /// Used to recover from a panic so the rest of the code can be parsed.
fn clear_panic(&mut self) {
if self.panic {
self.panic = false;
@@ -255,6 +257,15 @@ impl Compiler {
t
}
+ /// The line of the current token.
+ fn line(&self) -> usize {
+ if self.curr < self.tokens.len() {
+ self.tokens[self.curr].1
+ } else {
+ self.tokens[self.tokens.len() - 1].1
+ }
+ }
+
fn precedence(&self, token: Token) -> Prec {
match token {
Token::Star | Token::Slash => Prec::Factor,
@@ -277,14 +288,6 @@ impl Compiler {
}
}
- fn line(&self) -> usize {
- if self.curr < self.tokens.len() {
- self.tokens[self.curr].1
- } else {
- self.tokens[self.tokens.len() - 1].1
- }
- }
-
fn prefix(&mut self, token: Token, block: &mut Block) -> bool {
match token {
Token::Identifier(_) => self.variable_expression(block),
@@ -303,7 +306,6 @@ impl Compiler {
return true;
}
-
fn infix(&mut self, token: Token, block: &mut Block) -> bool {
match token {
Token::Minus
@@ -428,6 +430,7 @@ impl Compiler {
block.add_from(op, self.line());
}
+ /// Entry point for all expression parsing.
fn expression(&mut self, block: &mut Block) {
match self.peek_four() {
(Token::Fn, ..) => self.function(block),
@@ -514,6 +517,7 @@ impl Compiler {
add_op(self, block, Op::Call(arity));
}
+ // TODO(ed): de-complexify
fn function(&mut self, block: &mut Block) {
expect!(self, Token::Fn, "Expected 'fn' at start of function.");
@@ -807,6 +811,7 @@ impl Compiler {
fn parse_type(&mut self) -> Result<Type, ()> {
match self.peek() {
+
Token::Fn => {
self.eat();
let mut params = Vec::new();
@@ -839,6 +844,7 @@ impl Compiler {
let f = Type::Function(params, Box::new(return_type));
Ok(f)
}
+
Token::LeftParen => {
self.eat();
let mut element = Vec::new();
@@ -855,6 +861,7 @@ impl Compiler {
}
}
}
+
Token::Identifier(x) => {
self.eat();
match x.as_str() {
@@ -866,7 +873,6 @@ impl Compiler {
}
}
_ => Err(()),
-
}
}