aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler.rs
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-02-05 21:01:41 +0100
committerGitHub <noreply@github.com>2021-02-05 21:01:41 +0100
commitb277ecb9912f231ae96ad40020163ea2a003846b (patch)
treeb2abcfd0906e27b143b1f73a479445d8941388f9 /src/compiler.rs
parent3b8eec0cbb5d7355c6d4aaa89d53ee981aa9fba0 (diff)
parent415b772d4ae5ff9308efab45449f931de811e167 (diff)
downloadsylt-b277ecb9912f231ae96ad40020163ea2a003846b.tar.gz
Merge pull request #51 from FredTheDino/document/internal
document/internal
Diffstat (limited to 'src/compiler.rs')
-rw-r--r--src/compiler.rs28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 4988c4d..dcd0317 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -161,12 +161,11 @@ 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())
}
-
-
impl Compiler {
pub fn new(current_file: &Path, tokens: TokenStream) -> Self {
Self {
@@ -209,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;
@@ -257,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,
@@ -279,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),
@@ -305,7 +306,6 @@ impl Compiler {
return true;
}
-
fn infix(&mut self, token: Token, block: &mut Block) -> bool {
match token {
Token::Minus
@@ -430,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),
@@ -516,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.");
@@ -809,6 +811,7 @@ impl Compiler {
fn parse_type(&mut self) -> Result<Type, ()> {
match self.peek() {
+
Token::Fn => {
self.eat();
let mut params = Vec::new();
@@ -841,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();
@@ -857,6 +861,7 @@ impl Compiler {
}
}
}
+
Token::Identifier(x) => {
self.eat();
match x.as_str() {
@@ -868,7 +873,6 @@ impl Compiler {
}
}
_ => Err(()),
-
}
}