🤷 The initial version of the compiler, both static and JIT.

This implements a full compiler, with both static compilation and JIT
support, for the world's simplest and silliest programming language. You
can do math, and print variables. That's it. On the bright side, it
implements every part of the compiler, from the lexer and parser;
through analysis and simplification; and into a reasonable code
generator. This should be a good jumping off point for adding more
advanced features.

Tests, including proptests, are included to help avoid regressions.
This commit is contained in:
2020-08-01 20:45:33 -07:00
commit b2f6b12ced
30 changed files with 2178 additions and 0 deletions

56
src/syntax/location.rs Normal file
View File

@@ -0,0 +1,56 @@
use codespan_reporting::diagnostic::{Diagnostic, Label};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Location {
file_idx: usize,
offset: usize,
}
impl Location {
pub fn new(file_idx: usize, offset: usize) -> Self {
Location { file_idx, offset }
}
pub fn manufactured() -> Self {
Location {
file_idx: 0,
offset: 0,
}
}
pub fn primary_label(&self) -> Label<usize> {
Label::primary(self.file_idx, self.offset..self.offset)
}
pub fn secondary_label(&self) -> Label<usize> {
Label::secondary(self.file_idx, self.offset..self.offset)
}
pub fn range_label(&self, end: &Location) -> Vec<Label<usize>> {
if self.file_idx == end.file_idx {
vec![Label::primary(self.file_idx, self.offset..end.offset)]
} else if self.file_idx == 0 {
// if this is a manufactured item, then ... just try the other one
vec![Label::primary(end.file_idx, end.offset..end.offset)]
} else {
// we'll just pick the first location if this is in two different
// files
vec![Label::primary(self.file_idx, self.offset..self.offset)]
}
}
pub fn error(&self) -> Diagnostic<usize> {
Diagnostic::error().with_labels(vec![Label::primary(
self.file_idx,
self.offset..self.offset,
)])
}
pub fn labelled_error(&self, msg: &str) -> Diagnostic<usize> {
Diagnostic::error().with_labels(vec![Label::primary(
self.file_idx,
self.offset..self.offset,
)
.with_message(msg)])
}
}