-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from froth/global-variables
Implement global variables
- Loading branch information
Showing
12 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ mod gc; | |
mod op; | ||
mod printer; | ||
mod scanner; | ||
mod source_span_extensions; | ||
mod token; | ||
mod types; | ||
mod vm; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,7 @@ pub enum Op { | |
Less, | ||
Print, | ||
Pop, | ||
DefineGlobal(u8), | ||
GetGlobal(u8), | ||
SetGlobal(u8), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::cmp::max; | ||
|
||
use miette::SourceSpan; | ||
|
||
pub trait SourceSpanExtensions { | ||
fn until(&self, right: Self) -> Self; | ||
} | ||
|
||
impl SourceSpanExtensions for SourceSpan { | ||
fn until(&self, right: Self) -> Self { | ||
assert!(self.offset() <= right.offset()); | ||
let len = max(right.offset() - self.offset() + right.len(), self.len()); | ||
(self.offset(), len).into() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod source_span_extension_tests { | ||
use miette::SourceSpan; | ||
|
||
use super::SourceSpanExtensions; | ||
|
||
#[test] | ||
fn non_overlapping() { | ||
let left: SourceSpan = (1, 1).into(); | ||
let right: SourceSpan = (3, 2).into(); | ||
let combined = left.until(right); | ||
assert_eq!(combined.offset(), 1); | ||
assert_eq!(combined.len(), 4); | ||
} | ||
|
||
#[test] | ||
fn overlapping() { | ||
let left: SourceSpan = (1, 10).into(); | ||
let right: SourceSpan = (8, 2).into(); | ||
let combined = left.until(right); | ||
assert_eq!(combined.offset(), 1); | ||
assert_eq!(combined.len(), 10); | ||
} | ||
|
||
#[test] | ||
fn strang() { | ||
let left: SourceSpan = (6, 1).into(); | ||
let right: SourceSpan = (10, 1).into(); | ||
let combined = left.until(right); | ||
assert_eq!(combined.offset(), 6); | ||
assert_eq!(combined.len(), 5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
error | ||
a * b = c + d; | ||
---- | ||
---- | ||
{ | ||
"causes": [], | ||
"filename": "", | ||
"labels": [], | ||
"message": "Parser Error", | ||
"related": [ | ||
{ | ||
"causes": [], | ||
"filename": "tests/invalid_assignment_target.lox", | ||
"labels": [ | ||
{ | ||
"label": "here", | ||
"span": { | ||
"length": 1, | ||
"offset": 6 | ||
} | ||
} | ||
], | ||
"message": "Invalid assignment target", | ||
"related": [], | ||
"severity": "error" | ||
} | ||
], | ||
"severity": "error" | ||
} | ||
---- | ||
---- (no newline) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interpret | ||
var breakfast = "beignets"; | ||
var beverage = "cafe au lait"; | ||
breakfast = "beignets with " + beverage; | ||
|
||
print breakfast; | ||
---- | ||
beignets with cafe au lait |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
interpret | ||
var a = 5; | ||
var b = "123123"; | ||
var c; | ||
print a; | ||
print b; | ||
print c; | ||
---- | ||
5 | ||
123123 | ||
Nil |