Skip to content

Commit

Permalink
working test
Browse files Browse the repository at this point in the history
  • Loading branch information
stargazing-dino committed May 18, 2024
1 parent 49cc1a2 commit 3c4d42a
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 62 deletions.
2 changes: 1 addition & 1 deletion crates/compiler/src/compilation_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod register_strings;
mod resolve_deferred_type_diagnostic;
mod validate_unique_node_names;

pub(crate) use self::{
pub use self::{
add_initial_value_registrations::*, add_tracking_declarations::*, check_types::*,
clean_up_diagnostics::*, create_declarations_for_tracking_nodes::*, early_breaks::*,
find_tracking_nodes::*, generate_code::*, get_declarations::*, parse_files::*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use yarnspinner_core::prelude::*;
use yarnspinner_core::types::{Type, TypeFormat};

pub(crate) fn add_initial_value_registrations(
pub fn add_initial_value_registrations(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
// Last step: take every variable declaration we found in all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use crate::prelude::*;
use yarnspinner_core::prelude::*;
use yarnspinner_core::types::Type;

pub(crate) fn add_tracking_declarations(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
pub fn add_tracking_declarations(mut state: CompilationIntermediate) -> CompilationIntermediate {
let tracking_declarations: Vec<_> = state
.tracking_nodes
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/compilation_steps/check_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use crate::visitors::TypeCheckVisitor;
use antlr_rust::tree::ParseTreeVisitorCompat;

pub(crate) fn check_types(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn check_types(mut state: CompilationIntermediate) -> CompilationIntermediate {
for file in &state.parsed_files {
let mut visitor =
TypeCheckVisitor::new(state.known_variable_declarations.clone(), file.clone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::listeners::DiagnosticVec;
use crate::prelude::*;
use std::collections::HashSet;

pub(crate) fn clean_up_diagnostics(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn clean_up_diagnostics(mut state: CompilationIntermediate) -> CompilationIntermediate {
let total_diagnostics = if let Some(Ok(compilation)) = state.result.as_ref() {
compilation
.warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use yarnspinner_core::prelude::Library;
use yarnspinner_core::types::Type;

pub(crate) fn create_declarations_for_tracking_nodes(
pub fn create_declarations_for_tracking_nodes(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
let tracking_declarations = state.tracking_nodes.iter().map(|node| {
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/src/compilation_steps/early_breaks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

pub(crate) fn break_on_job_with_only_strings(
pub fn break_on_job_with_only_strings(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
if state.job.compilation_type == CompilationType::StringsOnly {
Expand All @@ -15,7 +15,7 @@ pub(crate) fn break_on_job_with_only_strings(
state
}

pub(crate) fn break_on_job_with_only_declarations(
pub fn break_on_job_with_only_declarations(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
if state.job.compilation_type == CompilationType::DeclarationsOnly {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::visitors::NodeTrackingVisitor;
use antlr_rust::tree::ParseTreeVisitorCompat;
use std::collections::HashSet;

pub(crate) fn find_tracking_nodes(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn find_tracking_nodes(mut state: CompilationIntermediate) -> CompilationIntermediate {
// determining the nodes we need to track visits on
// this needs to be done before we finish up with declarations
// so that any tracking variables are included in the compiled declarations
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/compilation_steps/generate_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::visitors::KnownTypes;
use crate::Result;
use std::collections::{HashMap, HashSet};

pub(crate) fn generate_code(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn generate_code(mut state: CompilationIntermediate) -> CompilationIntermediate {
let has_errors = state.diagnostics.has_errors();
let results: Vec<_> = if has_errors {
// We have errors, so we can't safely generate code.
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/compilation_steps/get_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use crate::visitors::DeclarationVisitor;
use antlr_rust::tree::ParseTreeVisitorCompat;

pub(crate) fn get_declarations(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn get_declarations(mut state: CompilationIntermediate) -> CompilationIntermediate {
// Find the variable declarations in these files.
for file in &state.parsed_files {
let mut variable_declaration_visitor =
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/compilation_steps/parse_files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

pub(crate) fn parse_files(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn parse_files(mut state: CompilationIntermediate) -> CompilationIntermediate {
for (file, chars) in state.job.files.iter().zip(state.file_chars.iter()) {
let parse_result = parse_syntax_tree(file, chars, &mut state.diagnostics);
state.parsed_files.push(parse_result);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::prelude::*;
use yarnspinner_core::prelude::*;

pub(crate) fn register_initial_variables(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
pub fn register_initial_variables(mut state: CompilationIntermediate) -> CompilationIntermediate {
let variables = &mut state.known_variable_declarations;
let job_variable_declarations = state.job.variable_declarations.clone();
variables.extend(job_variable_declarations);
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/compilation_steps/register_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;
use crate::visitors::{LastLineBeforeOptionsVisitor, StringTableGeneratorVisitor};
use antlr_rust::tree::ParseTreeVisitorCompat;

pub(crate) fn register_strings(mut state: CompilationIntermediate) -> CompilationIntermediate {
pub fn register_strings(mut state: CompilationIntermediate) -> CompilationIntermediate {
// First pass: parse all files, generate their syntax trees,
// and figure out what variables they've declared
for file in &state.parsed_files {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use std::collections::HashSet;

pub(crate) fn resolve_deferred_type_diagnostic(
pub fn resolve_deferred_type_diagnostic(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
let known_declarations: HashSet<_> = state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use crate::prelude::*;
use antlr_rust::token::Token;
use std::collections::HashMap;

pub(crate) fn validate_unique_node_names(
mut state: CompilationIntermediate,
) -> CompilationIntermediate {
pub fn validate_unique_node_names(mut state: CompilationIntermediate) -> CompilationIntermediate {
// Ensure that all nodes names in this compilation are unique. Node
// name uniqueness is important for several processes, so we do this
// check here.
Expand Down
34 changes: 31 additions & 3 deletions crates/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner.Compiler/Compiler.cs>
//! and <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner.Compiler/CompilationJob.cs>
use crate::prelude::*;
use crate::{compilation_steps::*, prelude::*};
use std::path::Path;
use yarnspinner_core::prelude::*;

mod add_tags_to_lines;
pub(crate) mod antlr_rust_ext;
pub(crate) mod run_compilation;
pub mod run_compilation;
pub(crate) mod utils;

#[allow(missing_docs)]
Expand Down Expand Up @@ -99,7 +99,35 @@ impl Compiler {

/// Compiles the Yarn files previously added into a [`Compilation`].
pub fn compile(&self) -> Result<Compilation> {
run_compilation::compile(self)
run_compilation::compile(self, Self::default_compiler_steps())
}

/// Compiles the Yarn files previously added into a [`Compilation`] with custom compilation steps.
pub fn compile_with_custom_steps(
&self,
custom_steps: Vec<&CompilationStep>,
) -> Result<Compilation> {
run_compilation::compile(self, custom_steps)
}

/// The default compilation steps that the compiler will use.
pub fn default_compiler_steps() -> Vec<&'static CompilationStep> {
vec![
&register_initial_variables,
&parse_files,
&register_strings,
&validate_unique_node_names,
&break_on_job_with_only_strings,
&get_declarations,
&check_types,
&find_tracking_nodes,
&create_declarations_for_tracking_nodes,
&add_tracking_declarations,
&resolve_deferred_type_diagnostic,
&break_on_job_with_only_declarations,
&generate_code,
&add_initial_value_registrations,
]
}
}

Expand Down
54 changes: 20 additions & 34 deletions crates/compiler/src/compiler/run_compilation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! The main entry point for running a compilation job.
use crate::compilation_steps::*;
use crate::output::*;
use crate::prelude::*;
Expand All @@ -7,24 +9,7 @@ use crate::Result;
use std::collections::{HashMap, HashSet};

/// Compile Yarn code, as specified by a compilation job.
pub(crate) fn compile(compiler: &Compiler) -> Result<Compilation> {
let compiler_steps: Vec<&CompilationStep> = vec![
&register_initial_variables,
&parse_files,
&register_strings,
&validate_unique_node_names,
&break_on_job_with_only_strings,
&get_declarations,
&check_types,
&find_tracking_nodes,
&create_declarations_for_tracking_nodes,
&add_tracking_declarations,
&resolve_deferred_type_diagnostic,
&break_on_job_with_only_declarations,
&generate_code,
&add_initial_value_registrations,
];

pub(crate) fn compile(compiler: &Compiler, steps: Vec<&CompilationStep>) -> Result<Compilation> {
let chars: Vec<Vec<u32>> = compiler
.files
.iter()
Expand All @@ -41,7 +26,7 @@ pub(crate) fn compile(compiler: &Compiler) -> Result<Compilation> {
.collect();
let chars: Vec<_> = chars.iter().map(|c| c.as_slice()).collect();
let initial = CompilationIntermediate::from_job(compiler, chars);
let intermediate = compiler_steps.into_iter().fold(initial, |state, step| {
let intermediate = steps.into_iter().fold(initial, |state, step| {
if state.early_break {
state
} else {
Expand All @@ -55,24 +40,25 @@ pub(crate) fn compile(compiler: &Compiler) -> Result<Compilation> {
result
}

type CompilationStep = dyn Fn(CompilationIntermediate) -> CompilationIntermediate;
pub type CompilationStep = dyn Fn(CompilationIntermediate) -> CompilationIntermediate;

pub(crate) struct CompilationIntermediate<'input> {
pub(crate) job: &'input Compiler,
pub(crate) file_chars: Vec<&'input [u32]>,
pub(crate) result: Option<Result<Compilation>>,
#[derive(Debug)]
pub struct CompilationIntermediate<'input> {
pub job: &'input Compiler,
pub file_chars: Vec<&'input [u32]>,
pub result: Option<Result<Compilation>>,
/// All variable declarations that we've encountered, PLUS the ones we knew about before
pub(crate) known_variable_declarations: Vec<Declaration>,
pub known_variable_declarations: Vec<Declaration>,
/// All variable declarations that we've encountered during this compilation job
pub(crate) derived_variable_declarations: Vec<Declaration>,
pub(crate) potential_issues: Vec<DeferredTypeDiagnostic>,
pub(crate) parsed_files: Vec<FileParseResult<'input>>,
pub(crate) tracking_nodes: HashSet<String>,
pub(crate) string_table: StringTableManager,
pub(crate) diagnostics: Vec<Diagnostic>,
pub(crate) file_tags: HashMap<String, Vec<String>>,
pub(crate) known_types: KnownTypes,
pub(crate) early_break: bool,
pub derived_variable_declarations: Vec<Declaration>,
pub potential_issues: Vec<DeferredTypeDiagnostic>,
pub parsed_files: Vec<FileParseResult<'input>>,
pub tracking_nodes: HashSet<String>,
pub string_table: StringTableManager,
pub diagnostics: Vec<Diagnostic>,
pub file_tags: HashMap<String, Vec<String>>,
pub known_types: KnownTypes,
pub early_break: bool,
}

impl<'input> CompilationIntermediate<'input> {
Expand Down
11 changes: 10 additions & 1 deletion crates/compiler/src/file_parse_result.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner/blob/da39c7195107d8211f21c263e4084f773b84eaff/YarnSpinner.Compiler/FileParseResult.cs>
use crate::prelude::{generated::yarnspinnerparser::*, *};
use std::rc::Rc;
use std::{fmt::Formatter, rc::Rc};

/// Contains the result of parsing a single file of source code.
///
Expand Down Expand Up @@ -34,3 +34,12 @@ impl<'input> FileParseResult<'input> {
&self.parser.input
}
}

impl<'input> std::fmt::Debug for FileParseResult<'input> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FileParseResult")
.field("name", &self.name)
.field("tree", &self.tree)
.finish()
}
}
3 changes: 2 additions & 1 deletion crates/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#![warn(missing_docs, missing_debug_implementations)]

mod collections;
pub(crate) mod compilation_steps;
pub mod compilation_steps;
pub(crate) mod compiler;
pub(crate) mod error_strategy;
mod file_parse_result;
Expand All @@ -21,6 +21,7 @@ mod string_table_manager;
pub(crate) mod token_ext;
pub(crate) mod visitors;

pub use crate::compiler::run_compilation;
pub use crate::compiler::Result;

pub mod prelude {
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/src/string_table_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{Deref, DerefMut};
use yarnspinner_core::prelude::*;

#[derive(Debug, Clone, Default)]
pub(crate) struct StringTableManager(pub HashMap<LineId, StringInfo>);
pub struct StringTableManager(pub HashMap<LineId, StringInfo>);

impl StringTableManager {
pub(crate) fn contains_implicit_string_tags(&self) -> bool {
Expand Down
Loading

0 comments on commit 3c4d42a

Please sign in to comment.