Skip to content

Commit

Permalink
Remove Test and use Program; tests are programs
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Dec 9, 2024
1 parent ff4e46f commit 8f0b836
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 245 deletions.
6 changes: 1 addition & 5 deletions compiler/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ pub use self::program::*;
pub mod statement;
pub use self::statement::*;

pub mod tst;
pub use self::tst::*;

pub mod types;
pub use self::types::*;

Expand Down Expand Up @@ -88,11 +85,10 @@ impl Ast {
/// Combines the two ASTs into a single AST.
/// The ASTs are combined by extending the components of the first AST with the components of the second AST.
pub fn combine(&mut self, other: Self) {
let Program { imports, stubs, program_scopes, tests } = other.ast;
let Program { imports, stubs, program_scopes } = other.ast;
self.ast.imports.extend(imports);
self.ast.stubs.extend(stubs);
self.ast.program_scopes.extend(program_scopes);
self.ast.tests.extend(tests);
}

/// Returns a reference to the inner program AST representation.
Expand Down
7 changes: 0 additions & 7 deletions compiler/ast/src/passes/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ pub trait ProgramScopeConsumer {
fn consume_program_scope(&mut self, input: ProgramScope) -> Self::Output;
}

/// A `Consumer` for a `Test`
pub trait TestConsumer {
type Output;

fn consume_test(&mut self, input: Test) -> Self::Output;
}

/// A Consumer trait for the program represented by the AST.
pub trait ProgramConsumer {
type Output;
Expand Down
17 changes: 0 additions & 17 deletions compiler/ast/src/passes/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ pub trait ProgramReconstructor: StatementReconstructor {
.into_iter()
.map(|(id, scope)| (id, self.reconstruct_program_scope(scope)))
.collect(),
tests: input.tests.into_iter().map(|test| self.reconstruct_test(test)).collect(),
}
}

Expand Down Expand Up @@ -488,20 +487,4 @@ pub trait ProgramReconstructor: StatementReconstructor {
fn reconstruct_mapping(&mut self, input: Mapping) -> Mapping {
input
}

fn reconstruct_test(&mut self, input: Test) -> Test {
Test {
consts: input
.consts
.into_iter()
.map(|(i, c)| match self.reconstruct_const(c) {
(Statement::Const(declaration), _) => (i, declaration),
_ => unreachable!("`reconstruct_const` can only return `Statement::Const`"),
})
.collect(),
structs: input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect(),
mappings: input.mappings.into_iter().map(|(i, m)| (i, self.reconstruct_mapping(m))).collect(),
functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function(f))).collect(),
}
}
}
8 changes: 0 additions & 8 deletions compiler/ast/src/passes/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> {
input.imports.values().for_each(|import| self.visit_import(&import.0));
input.stubs.values().for_each(|stub| self.visit_stub(stub));
input.program_scopes.values().for_each(|scope| self.visit_program_scope(scope));
input.tests.iter().for_each(|test| self.visit_test(test));
}

fn visit_program_scope(&mut self, input: &'a ProgramScope) {
Expand All @@ -251,11 +250,4 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> {
fn visit_function_stub(&mut self, _input: &'a FunctionStub) {}

fn visit_struct_stub(&mut self, _input: &'a Composite) {}

fn visit_test(&mut self, input: &'a Test) {
input.consts.iter().for_each(|(_, c)| (self.visit_const(c)));
input.structs.iter().for_each(|(_, c)| (self.visit_struct(c)));
input.mappings.iter().for_each(|(_, c)| (self.visit_mapping(c)));
input.functions.iter().for_each(|(_, c)| (self.visit_function(c)));
}
}
7 changes: 2 additions & 5 deletions compiler/ast/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use program_scope::*;

use leo_span::{Span, Symbol};

use crate::{Stub, Test};
use crate::Stub;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt;
Expand All @@ -38,9 +38,6 @@ pub struct Program {
pub stubs: IndexMap<Symbol, Stub>,
/// A map from program names to program scopes.
pub program_scopes: IndexMap<Symbol, ProgramScope>,
/// A map from test file names to test defintions.
// TODO: This is a temporary way to store tests in the AST, without requiring an overhaul of the compiler.
pub tests: Vec<Test>,
}

impl fmt::Display for Program {
Expand All @@ -63,6 +60,6 @@ impl fmt::Display for Program {
impl Default for Program {
/// Constructs an empty program node.
fn default() -> Self {
Self { imports: IndexMap::new(), stubs: IndexMap::new(), program_scopes: IndexMap::new(), tests: Vec::new() }
Self { imports: IndexMap::new(), stubs: IndexMap::new(), program_scopes: IndexMap::new() }
}
}
55 changes: 0 additions & 55 deletions compiler/ast/src/tst/mod.rs

This file was deleted.

27 changes: 1 addition & 26 deletions compiler/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,6 @@ impl<'a, N: Network> Compiler<'a, N> {
Ok(())
}

/// Parses and stores the test source , constructs the AST, and optionally outputs it.
pub fn parse_test(&mut self) -> Result<()> {
// Initialize the AST.
let mut ast = Ast::default();
// Parse the sources.
for (name, program_string) in &self.sources {
// Register the source (`program_string`) in the source map.
let prg_sf = with_session_globals(|s| s.source_map.new_source(program_string, name.clone()));
// Use the parser to construct the abstract syntax tree (ast).
ast.combine(leo_parser::parse_test_ast::<N>(
self.handler,
&self.node_builder,
&prg_sf.src,
prg_sf.start_pos,
)?);
}
// Store the AST.
self.ast = ast;
// Write the AST to a JSON file.
if self.compiler_options.output.initial_ast {
self.write_ast_to_json("initial_ast.json")?;
}
Ok(())
}

/// Runs the symbol table pass.
pub fn symbol_table_pass(&self) -> Result<SymbolTable> {
let symbol_table = SymbolTableCreator::do_pass((&self.ast, self.handler))?;
Expand Down Expand Up @@ -496,7 +471,7 @@ impl<'a, N: Network> Compiler<'a, N> {
/// Returns the compiled Leo tests.
pub fn compile_tests(&mut self) -> Result<String> {
// Parse the program.
self.parse_test()?;
self.parse()?;
// Copy the dependencies specified in `program.json` into the AST.
self.add_import_stubs()?;
// Run the intermediate compiler stages.
Expand Down
15 changes: 1 addition & 14 deletions compiler/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) use tokenizer::*;
pub mod parser;
pub use parser::*;

use leo_ast::{Ast, NodeBuilder, Test};
use leo_ast::{Ast, NodeBuilder};
use leo_errors::{Result, emitter::Handler};

use snarkvm::prelude::Network;
Expand All @@ -48,16 +48,3 @@ pub fn parse_ast<N: Network>(
) -> Result<Ast> {
Ok(Ast::new(parse::<N>(handler, node_builder, source, start_pos)?))
}

/// Creates a new test AST from a given file path and source code text.
pub fn parse_test_ast<N: Network>(
handler: &Handler,
node_builder: &NodeBuilder,
source: &str,
start_pos: BytePos,
) -> Result<Ast> {
let test = parse_test::<N>(handler, node_builder, source, start_pos)?;
let mut program = leo_ast::Program::default();
program.tests.push(test);
Ok(Ast::new(program))
}
2 changes: 1 addition & 1 deletion compiler/parser/src/parser/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<N: Network> ParserContext<'_, N> {
return Err(ParserError::missing_program_scope(self.token.span).into());
}

Ok(Program { imports, stubs: IndexMap::new(), program_scopes, tests: Vec::new() })
Ok(Program { imports, stubs: IndexMap::new(), program_scopes })
}

pub(super) fn unexpected_item(token: &SpannedToken, expected: &[Token]) -> ParserError {
Expand Down
13 changes: 0 additions & 13 deletions compiler/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub(super) use context::ParserContext;
mod expression;
mod file;
mod statement;
mod test;
pub(super) mod type_;

/// Creates a new program from a given file path and source code text.
Expand All @@ -50,15 +49,3 @@ pub fn parse<N: Network>(

tokens.parse_program()
}

/// Creates a new test from a given file path and source code text.
pub fn parse_test<N: Network>(
handler: &Handler,
node_builder: &NodeBuilder,
source: &str,
start_pos: BytePos,
) -> Result<Test> {
let mut tokens = ParserContext::<N>::new(handler, node_builder, crate::tokenize(source, start_pos)?);

tokens.parse_test()
}
68 changes: 0 additions & 68 deletions compiler/parser/src/parser/test.rs

This file was deleted.

16 changes: 0 additions & 16 deletions compiler/passes/src/static_single_assignment/rename_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ use leo_ast::{
ProgramScopeConsumer,
StatementConsumer,
StructConsumer,
Test,
TestConsumer,
};
use leo_span::{Symbol, sym};

Expand Down Expand Up @@ -111,19 +109,6 @@ impl ProgramScopeConsumer for StaticSingleAssigner<'_> {
}
}

impl TestConsumer for StaticSingleAssigner<'_> {
type Output = Test;

fn consume_test(&mut self, input: Test) -> Self::Output {
Test {
structs: input.structs.into_iter().map(|(i, s)| (i, self.consume_struct(s))).collect(),
mappings: input.mappings,
functions: input.functions.into_iter().map(|(i, f)| (i, self.consume_function(f))).collect(),
consts: input.consts,
}
}
}

impl ProgramConsumer for StaticSingleAssigner<'_> {
type Output = Program;

Expand All @@ -140,7 +125,6 @@ impl ProgramConsumer for StaticSingleAssigner<'_> {
.into_iter()
.map(|(name, scope)| (name, self.consume_program_scope(scope)))
.collect(),
tests: input.tests.into_iter().map(|test| self.consume_test(test)).collect(),
}
}
}
2 changes: 1 addition & 1 deletion compiler/passes/src/type_checking/check_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'a, N: Network> ProgramVisitor<'a> for TypeChecker<'a, N> {

fn visit_function(&mut self, function: &'a Function) {
// Check that the function's annotations are valid.
let valid_annotations = [sym::should_fail, sym::native_test, sym::interpreted_test];
let valid_annotations = [sym::should_fail, sym::compiled_test, sym::interpreted_test];
for annotation in function.annotations.iter() {
// All Leo annotations currently apply only to test code.
if !self.build_tests || !valid_annotations.contains(&annotation.identifier.name) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ symbols! {

// annotations
should_fail,
native_test,
compiled_test,
interpreted_test,

// general keywords
Expand Down
Loading

0 comments on commit 8f0b836

Please sign in to comment.