diff options
| author | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-02-11 18:39:05 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-11 18:39:05 +0100 |
| commit | b09c97154886e1ca9e0a418f8969870a31f39077 (patch) | |
| tree | 88273971103ff6a49295346a5415a1acd646e075 /src | |
| parent | 6bd482f05c24f0aa1475c02d529f33ece40dee24 (diff) | |
| parent | d87c62959a784d15521a86508f2d63acb2206053 (diff) | |
| download | sylt-b09c97154886e1ca9e0a418f8969870a31f39077.tar.gz | |
Merge pull request #64 from FredTheDino/constants
constants
Diffstat (limited to 'src')
| -rw-r--r-- | src/compiler.rs | 47 | ||||
| -rw-r--r-- | src/lib.rs | 23 |
2 files changed, 69 insertions, 1 deletions
diff --git a/src/compiler.rs b/src/compiler.rs index 128b4a8..eb6058a 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -126,6 +126,7 @@ struct Variable { active: bool, upvalue: bool, captured: bool, + mutable: bool, } struct Frame { @@ -798,6 +799,32 @@ impl Compiler { scope, active: false, upvalue: false, + mutable: true, + }); + Ok(slot) + } + + fn define_constant(&mut self, name: &str, typ: Type, _block: &mut Block) -> Result<usize, ()> { + if let Some(var) = self.find_variable(&name) { + if var.scope == self.frame().scope { + error!(self, format!("Multiple definitions of {} in this block.", name)); + return Err(()); + } + } + + let slot = self.stack().len(); + let scope = self.frame().scope; + self.stack_mut().push(Variable { + name: String::from(name), + captured: false, + outer_upvalue: false, + outer_slot: 0, + slot, + typ, + scope, + active: false, + upvalue: false, + mutable: false, }); Ok(slot) } @@ -813,6 +840,15 @@ impl Compiler { } } + fn constant_statement(&mut self, name: &str, typ: Type, block: &mut Block) { + let slot = self.define_constant(name, typ.clone(), block); + self.expression(block); + + if let Ok(slot) = slot { + self.stack_mut()[slot].active = true; + } + } + fn assign(&mut self, block: &mut Block) { let name = match self.eat() { Token::Identifier(name) => name, @@ -837,6 +873,10 @@ impl Compiler { }; if let Some(var) = self.find_variable(&name) { + if !var.mutable { + // TODO(ed): Maybe a better error than "SyntaxError". + error!(self, format!("Cannot assign to constant '{}'", var.name)); + } if let Some(op) = op { if var.upvalue { add_op(self, block, Op::ReadUpvalue(var.slot)); @@ -1165,6 +1205,12 @@ impl Compiler { self.definition_statement(&name, Type::Unknown, block); } + (Token::Identifier(name), Token::ColonColon, ..) => { + self.eat(); + self.eat(); + self.constant_statement(&name, Type::Unknown, block); + } + (Token::Blob, Token::Identifier(_), ..) => { self.blob_statement(block); } @@ -1219,6 +1265,7 @@ impl Compiler { active: false, captured: false, upvalue: false, + mutable: true, }); let mut block = Block::new(name, file, 0); @@ -805,6 +805,16 @@ mod tests { assert_errs!(run_string("<!>\n", true, Vec::new()), [ErrorKind::Unreachable]); } + #[test] + fn assign_to_constant() { + assert_errs!(run_string("a :: 2\na = 2", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); + } + + #[test] + fn assign_to_constant_upvalue() { + assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\n", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); + } + macro_rules! test_multiple { ($mod:ident, $( $fn:ident : $prog:literal ),+ $( , )? ) => { mod $mod { @@ -848,7 +858,7 @@ mod tests { test_multiple!( if_, compare_constants_equality: "if 1 == 2 { - <!> + <!> }", compare_constants_unequality: "if 1 != 1 { <!> @@ -1060,6 +1070,17 @@ a.a <=> 0" ); test_multiple!( + read_constants, + simple: " +a :: 1 +a <=> 1 +b := 2 +{ + a <=> 1 +}", + ); + + test_multiple!( assignment_op_regression, simple_add: " a := 0 |
