Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate string constants from other constants to allow interning #27

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::representation::CoreAST;

use super::CompilationUnit;
use crate::compiler::source::Registry;
use code_generator::{CodeGenerator, Target};
use code_generator::CodeGenerator;

#[derive(Debug)]
pub struct Backend {}
Expand All @@ -21,7 +21,7 @@ impl Backend {
ast: &CoreAST,
registry: &Registry,
) -> std::result::Result<CompilationUnit, error::Error> {
let mut code_gen = CodeGenerator::new(Target::TopLevel, None, registry);
Ok(code_gen.generate(ast)?)
let unit = CodeGenerator::generate(registry, ast)?;
Ok(unit)
}
}
16 changes: 10 additions & 6 deletions src/compiler/backend/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ impl Context {
}

impl<'a> CodeGenerator<'a> {
pub fn new(
/// constructor is private since we don't want external callers
/// to hold an instance of `CodeGenerator`. It has internal state
/// that shouldn't be shared across multiple generation invocations.
fn new(
target: Target,
parent_variables: Option<VariablesRef>,
source_registry: &'a Registry,
Expand All @@ -96,16 +99,17 @@ impl<'a> CodeGenerator<'a> {
}
}

pub fn generate(&mut self, ast: &CoreAST) -> Result<CompilationUnit> {
/// Main entry point to generate the VM byte code
pub fn generate(registry: &Registry, ast: &CoreAST) -> Result<CompilationUnit> {
let proc = Self::generate_procedure(
self.source_registry,
registry,
None,
Target::TopLevel,
&Expression::body(ast.expressions.clone()),
&Formals::empty(),
)?;

Ok(CompilationUnit::new(Closure::new(proc, vec![])))
Ok(CompilationUnit::new(Closure::new(proc)))
}

pub fn generate_procedure(
Expand Down Expand Up @@ -215,8 +219,8 @@ impl<'a> CodeGenerator<'a> {
Ok(())
}

// This is the main entry-point that turns Expressions into instructions, which
// are then written to the current chunk.
/// This is the main entry-point that turns Expressions into instructions, which
/// are then written to the current chunk.
fn emit_instructions(&mut self, ast: &Expression, context: &Context) -> Result<()> {
match ast {
Expression::Identifier(id) => self.emit_get_variable(id)?,
Expand Down
22 changes: 22 additions & 0 deletions src/compiler/frontend/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ pub mod quotation;
pub mod result;
pub mod sequence;

/// The `Expression` type encodes scheme core forms.
#[derive(Clone, PartialEq, Debug)]
pub enum Expression {
/// A scheme identifier, which is a special symbol
Identifier(identifier::Identifier),
/// Any scheme literal value
Literal(literal::LiteralExpression),
/// A definition
Define(define::DefinitionExpression),
/// A lambda expression
Lambda(lambda::LambdaExpression),
/// A set! expression
Assign(assignment::SetExpression),
/// An if expression
If(conditional::IfExpression),
/// An expression for function application
Apply(apply::ApplicationExpression),
/// A begin expression to sequence other expressions
Begin(sequence::BeginExpression),
}

Expand Down Expand Up @@ -63,6 +72,19 @@ impl Parser {
}
}

/// Expand and parse `ast` into the `CoreAST` representation.
/// This process interleaves macro expansion and parsing of forms.
///
/// ```
/// use braces::compiler::frontend::parser::Parser;
/// use braces::compiler::representation::SexpAST;
/// use braces::compiler::frontend::reader::datum::Datum;
/// let mut parser = Parser::new();
/// // just a very simple s-expression which will be parsed to a literal
/// let sexps = SexpAST::new(vec![Datum::boolean(true, 0..2)]);
///
/// parser.parse(&sexps).unwrap();
/// ```
pub fn parse(&mut self, ast: &SexpAST) -> Result<CoreAST> {
let expressions: Result<Vec<Expression>> =
ast.to_vec().iter().map(|d| self.do_parse(d)).collect();
Expand Down
1 change: 1 addition & 0 deletions src/compiler/source/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ impl<S: Into<Span>> From<S> for Location {
///
/// Examples of this are `Datum` and various `Expression`s.
pub trait HasSourceLocation {
/// return `Location` information for this value
fn source_location(&self) -> &Location;
}
15 changes: 2 additions & 13 deletions src/vm/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ impl SchemeEqual<Value> for Value {
#[derive(Debug, Clone)]
pub struct Factory {
strings: StringTable,
symbols: StringTable,
true_value: Value,
false_value: Value,
nil_value: Value,
Expand All @@ -168,7 +167,6 @@ impl Default for Factory {
fn default() -> Factory {
Factory {
strings: StringTable::default(),
symbols: StringTable::default(),
true_value: Value::Bool(true),
false_value: Value::Bool(false),
nil_value: Value::ProperList(list::List::Nil),
Expand Down Expand Up @@ -203,7 +201,7 @@ impl Factory {
}

pub fn sym<T: Into<std::string::String>>(&mut self, v: T) -> Symbol {
let k = self.symbols.get_or_intern(v.into());
let k = self.strings.get_or_intern(v.into());
Symbol(k)
}

Expand Down Expand Up @@ -250,7 +248,7 @@ impl Factory {
}

pub fn closure(&mut self, v: procedure::native::Procedure) -> Value {
Value::Closure(closure::Closure::new(v, vec![]))
Value::Closure(closure::Closure::new(v))
}

pub fn from_datum(&mut self, d: &Datum) -> Value {
Expand All @@ -277,15 +275,6 @@ impl Factory {
}
}

pub fn interned_symbols(&self) -> Vec<Symbol> {
self.symbols
.interned_vec()
.iter()
.cloned()
.map(Symbol)
.collect()
}

pub fn interned_strings(&self) -> Vec<InternedString> {
self.strings
.interned_vec()
Expand Down
9 changes: 3 additions & 6 deletions src/vm/value/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ pub struct Closure {
}

impl Closure {
pub fn new(proc: procedure::native::Procedure, up_values: Vec<RefValue>) -> Self {
Self::from_rc(Rc::new(proc), up_values)
pub fn new(proc: procedure::native::Procedure) -> Self {
Self::from_rc(Rc::new(proc), vec![])
}

pub fn from_rc(proc: Rc<procedure::native::Procedure>, up_values: Vec<RefValue>) -> Self {
Self {
proc: proc,
up_values,
}
Self { proc, up_values }
}

pub fn code(&self) -> &Chunk {
Expand Down