feat(tazjin/rlox): Add initial support for strings

... including concatenation.

This diverges significantly from the book, as I'm using std::String
instead of implementing the book's whole heap object management
system.

It's possible that Lox in Rust actually doesn't need a GC and the
ownership model works just fine.

Change-Id: I374a0461d627cfafc26b2b54bfefac8b7c574d00
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2577
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
Vincent Ambo 2021-02-28 22:35:26 +02:00 committed by tazjin
parent 3b33c1bd76
commit 6f600c8300
3 changed files with 40 additions and 1 deletions

View file

@ -144,6 +144,10 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
ParseRule::new(None, Some(Compiler::binary), Precedence::Comparison)
}
TokenKind::String(_) => {
ParseRule::new(Some(Compiler::string), None, Precedence::None)
},
_ => ParseRule::new(None, None, Precedence::None),
}
}
@ -255,6 +259,18 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
Ok(())
}
fn string(&mut self) -> LoxResult<()> {
match &self.previous().kind {
TokenKind::String(s) => {
let s = s.clone();
self.emit_constant(Value::String(s));
}
_ => unreachable!("only called for strings"),
}
Ok(())
}
fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> {
self.advance();
let rule: ParseRule<T> = rule_for(&self.previous().kind);