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

Switches from "encoding module" to "default module" #873

Merged
merged 4 commits into from
Dec 6, 2024
Merged
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
9 changes: 7 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub(crate) mod v1_1 {
use crate::types::SymbolAddress;
use phf::phf_map;

pub mod constants {
pub const DEFAULT_MODULE_NAME: &str = "_";
}
pub static SYSTEM_SYMBOLS: &[&str] = &[
// <unknown text> $0
"$ion", // $1
Expand All @@ -59,7 +62,7 @@ pub(crate) mod v1_1 {
"symbols", // $7
"max_id", // $8
"$ion_shared_symbol_table", // $9
"$ion_encoding", // $10
"encoding", // $10
"$ion_literal", // $11
"$ion_shared_module", // $12
"macro", // $13
Expand Down Expand Up @@ -120,8 +123,10 @@ pub(crate) mod v1_1 {
pub mod system_symbols {
use crate::raw_symbol_ref::SystemSymbol_1_1;

pub const ION_ENCODING: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(10);
pub const ION: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(1);
pub const ENCODING: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(10);
pub const SYMBOL_TABLE: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(15);
pub const MODULE: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(16);
pub const EMPTY_TEXT: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(21);
pub const ADD_SYMBOLS: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(45);
pub const ADD_MACROS: SystemSymbol_1_1 = SystemSymbol_1_1::new_unchecked(47);
Expand Down
9 changes: 4 additions & 5 deletions src/lazy/binary/raw/v1_1/immutable_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,15 +1269,15 @@ mod tests {
RawSymbolRef::SymbolId(5)
])]
#[case::one_flex_syms_with_system_symbol(AnnotationsEncoding::FlexSym, &[0xE7, 0x01, 0x6A], 1, 2, &[
RawSymbolRef::Text("$ion_encoding"),
RawSymbolRef::Text("encoding"),
])]
#[case::two_flex_syms_with_system_symbols(AnnotationsEncoding::FlexSym, &[0xE8, 0x01, 0x60, 0x01, 0x6A], 1, 4, &[
RawSymbolRef::SymbolId(0),
RawSymbolRef::Text("$ion_encoding"),
RawSymbolRef::Text("encoding"),
])]
#[case::three_flex_syms_with_system_symbols(AnnotationsEncoding::FlexSym, &[0xE9, 0x0D, 0x01, 0x60, 0x01, 0x6A, 0x01, 0xA1], 2, 6, &[
RawSymbolRef::SymbolId(0),
RawSymbolRef::Text("$ion_encoding"),
RawSymbolRef::Text("encoding"),
RawSymbolRef::Text("make_field"),
])]
fn read_annotations_sequence(
Expand Down Expand Up @@ -1623,8 +1623,7 @@ mod tests {

// Construct an encoding directive that defines this number of macros. Each macro will expand
// to its own address.
let mut macro_definitions =
String::from("$ion_encoding::(\n (macro_table $ion_encoding\n");
let mut macro_definitions = String::from("$ion::\n(module _\n (macro_table _\n");
for address in MacroTable::FIRST_USER_MACRO_ID..MAX_TEST_MACRO_ADDRESS {
writeln!(macro_definitions, " (macro m{address} () {address})")?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lazy/binary/raw/v1_1/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ mod tests {
0xE3, 0x01, 0x00, 0x00,

// System symbols
0xEE, 0x0A, // $ion_encoding
0xEE, 0x0A, // encoding
0xEE, 0x0E, // macro_table
0xEE, 0x15, // empty text
0xEE, 0x41, // make_field
Expand All @@ -364,7 +364,7 @@ mod tests {
RawSymbolRef::SymbolId(1),
RawSymbolRef::SymbolId(257),
RawSymbolRef::SymbolId(65_793),
RawSymbolRef::Text("$ion_encoding"),
RawSymbolRef::Text("encoding"),
RawSymbolRef::Text("macro_table"),
RawSymbolRef::Text(""),
RawSymbolRef::Text("make_field"),
Expand Down
7 changes: 4 additions & 3 deletions src/lazy/binary/raw/v1_1/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,11 @@ mod tests {
let mut writer = Writer::new(v1_1::Binary, Vec::new())?;
let encoding_directive = Element::read_one(
r#"
$ion_encoding::(
(symbol_table $ion_encoding)
$ion::
(module _
(symbol_table _)
(macro_table
$ion_encoding
_
(macro greet (name) (.make_string "hello, " (%name)))
)
)
Expand Down
8 changes: 6 additions & 2 deletions src/lazy/encoder/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ impl<E: Encoding, Output: Write> Writer<E, Output> {

let mut directive = directive_writer
.value_writer()
.with_annotations(v1_1::system_symbols::ION_ENCODING)?
.with_annotations(v1_1::system_symbols::ION)?
.sexp_writer()?;

directive
.write_symbol(v1_1::system_symbols::MODULE)?
.write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?;

let pending_symbols = context
.symbol_table
.symbols_tail(context.num_pending_symbols)
Expand All @@ -177,7 +181,7 @@ impl<E: Encoding, Output: Write> Writer<E, Output> {
let mut symbol_table = directive.sexp_writer()?;
symbol_table
.write_symbol(v1_1::system_symbols::SYMBOL_TABLE)?
.write_symbol(v1_1::system_symbols::ION_ENCODING)?
.write_symbol(v1_1::constants::DEFAULT_MODULE_NAME)?
.write_list(pending_symbols)?;
symbol_table.close()?;
directive.close()
Expand Down
16 changes: 9 additions & 7 deletions src/lazy/expanded/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Compiles template definition language (TDL) expressions into a form suitable for fast incremental
//! evaluation.
use crate::constants::v1_1;
use crate::element::iterators::SymbolsIterator;
use crate::lazy::decoder::Decoder;
use crate::lazy::expanded::macro_table::ION_1_1_SYSTEM_MACROS;
Expand Down Expand Up @@ -460,11 +461,11 @@ impl TemplateCompiler {
match module_name {
// If the module is `$ion`, this refers to the system module.
"$ion" => ION_1_1_SYSTEM_MACROS.clone_macro_with_id(macro_id),
// If the module is `$ion_encoding`, this refers to the active encoding module.
"$ion_encoding" => context.macro_table().clone_macro_with_id(macro_id),
_ => todo!(
"qualified references to modules other than $ion_encoding (found {module_name}"
),
// If the module is `_`, this refers to the active encoding module.
v1_1::constants::DEFAULT_MODULE_NAME => {
context.macro_table().clone_macro_with_id(macro_id)
}
_ => todo!("qualified references to modules other than `_` (found `{module_name}`"),
}
}

Expand Down Expand Up @@ -1725,9 +1726,10 @@ mod tests {

let ion = r#"
$ion_1_1
$ion_encoding::(
$ion::
(module _
(macro_table
$ion_encoding
_
(macro hello (name) (.make_string "hello " (%name)))
(macro hello_world () (.hello "world")) // Depends on macro 'hello'
)
Expand Down
35 changes: 22 additions & 13 deletions src/lazy/expanded/macro_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,8 @@ mod tests {
stream_eq(
r#"
// Define macro `double`
$ion_encoding::(
$ion::
(module _
(macro_table
$ion
(macro double (x) (.$ion::values (%x) (%x)))
Expand All @@ -1359,15 +1360,16 @@ mod tests {
// `double` exists until the *end* of the encoding directive below. Define a new
// macro that depends on `double`.
$ion_encoding::(
$ion::
(module _
(macro_table
(macro quadruple (y)
(.$ion::values
// Because `double` is in the active encoding module, we can refer
// to it without qualification.
(.double (%y))
// We could also refer to it with a qualification.
(.$ion_encoding::double (%y))))
(._::double (%y))))
)
)
Expand All @@ -1388,23 +1390,25 @@ mod tests {
stream_eq(
r#"
// Define macro `double`
$ion_encoding::(
$ion::
(module _
(macro_table
$ion_encoding
_
(macro double (x) (.values (%x) (%x)))
)
)
$ion_encoding::(
$ion::
(module _
(macro_table
$ion_encoding // Re-export the active encoding module's macros
_ // Re-export the active encoding module's macros
(macro quadruple (y)
(.$ion::values
// Because `double` has been added to the local namespace,
// we can refer to it without a qualified reference.
(.double (%y))
// However, we can also refer to it using a qualified reference.
(.$ion_encoding::double (%y))))
(._::double (%y))))
)
)
Expand Down Expand Up @@ -1456,7 +1460,8 @@ mod tests {
fn multiple_arg_expr_groups() -> IonResult<()> {
stream_eq(
r#"
$ion_encoding::(
$ion::
(module _
(macro_table
(macro foo (x+ y* z+)
(.make_string (.. (%x) "-" (%y) "-" (%z))))
Expand Down Expand Up @@ -1540,7 +1545,8 @@ mod tests {
stream_eq(
r#"
// Define some symbols
$ion_encoding::(
$ion::
(module _
(symbol_table ["foo", "bar"]) // $1, $2
)
// Use them
Expand Down Expand Up @@ -1608,7 +1614,8 @@ mod tests {
// TODO: update symbol IDs when reading and writing system symbols are implemented
stream_eq(
r#"
$ion_encoding::(
$ion::
(module _
(symbol_table ["foo", "bar", "baz"]) // $1, $2, $3
)
$1
Expand Down Expand Up @@ -1686,7 +1693,8 @@ mod tests {
// TODO: update symbol IDs when reading and writing system symbols are implemented
stream_eq(
r#"
$ion_encoding::(
$ion::
(module _
(symbol_table ["foo", "bar", "baz"]) // $1, $2, $3
)
$1
Expand Down Expand Up @@ -1727,7 +1735,8 @@ mod tests {
eval_template_invocation(
r#"
(macro def_macros (macros*)
$ion_encoding::(
$ion::
(module _
(macro_table (%macros))
)
)"#,
Expand Down
24 changes: 14 additions & 10 deletions src/lazy/expanded/macro_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,33 +292,36 @@ impl MacroTable {
template(
r#"
(macro set_symbols (symbols*)
$ion_encoding::(
$ion::
(module _
// Set a new symbol table
(symbol_table [(%symbols)])
// Include the active encoding module macros
(macro_table $ion_encoding)
(macro_table _)
)
)
"#,
),
template(
r#"
(macro add_symbols (symbols*)
$ion_encoding::(
$ion::
(module _
// Set a new symbol table
(symbol_table $ion_encoding [(%symbols)])
(symbol_table _ [(%symbols)])
// Include the active encoding module macros
(macro_table $ion_encoding)
(macro_table _)
)
)
"#,
),
template(
r#"
(macro set_macros (macro_definitions*)
$ion_encoding::(
$ion::
(module _
// Include the active encoding module symbols
(symbol_table $ion_encoding)
(symbol_table _)
// Set a new macro table
(macro_table (%macro_definitions))
)
Expand All @@ -328,11 +331,12 @@ impl MacroTable {
template(
r#"
(macro add_macros (macro_definitions*)
$ion_encoding::(
$ion::
(module _
// Include the active encoding module symbols
(symbol_table $ion_encoding)
(symbol_table _)
// Set a new macro table
(macro_table $ion_encoding (%macro_definitions))
(macro_table _ (%macro_definitions))
)
)
"#,
Expand Down
Loading
Loading