1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
use super::chunk::Chunk;
use super::errors::{Error, ErrorKind, LoxResult};
use super::opcode::OpCode;
use crate::scanner;
struct Compiler<T: Iterator<Item = scanner::Token>> {
// panic: bool,
errors: Vec<Error>,
tokens: T,
chunk: Chunk,
// TODO(tazjin): Restructure so that these don't need to be Option?
current: Option<scanner::Token>,
previous: Option<scanner::Token>,
}
impl<T: Iterator<Item = scanner::Token>> Compiler<T> {
fn compile(&mut self) -> LoxResult<()> {
self.advance();
self.expression();
self.consume(
&scanner::TokenKind::Eof,
ErrorKind::ExpectedToken("Expected end of expression"),
)?;
self.end_compiler()
}
fn advance(&mut self) {
self.previous = self.current.take();
self.current = self.tokens.next();
}
fn expression(&mut self) {
unimplemented!()
}
fn consume(
&mut self,
expected: &scanner::TokenKind,
err: ErrorKind,
) -> LoxResult<()> {
unimplemented!()
}
fn current_chunk(&mut self) -> &mut Chunk {
&mut self.chunk
}
fn end_compiler(&mut self) -> LoxResult<()> {
let line = self.previous().line;
self.current_chunk().add_op(OpCode::OpReturn, line);
Ok(())
}
fn previous(&self) -> &scanner::Token {
self.previous
.as_ref()
.expect("invalid internal compiler state: missing previous token")
}
}
pub fn compile(code: &str) -> Result<Chunk, Vec<Error>> {
let chars = code.chars().collect::<Vec<char>>();
let tokens = scanner::scan(&chars).map_err(|errors| {
errors.into_iter().map(Into::into).collect::<Vec<Error>>()
})?;
let mut compiler = Compiler {
tokens: tokens.into_iter().peekable(),
errors: vec![],
current: None,
previous: None,
chunk: Default::default(),
};
compiler.compile()?;
if compiler.errors.is_empty() {
Ok(unimplemented!())
} else {
Err(compiler.errors)
}
}
|