Skip to content
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
19 changes: 19 additions & 0 deletions examples/conditional-imports-wasm.ilo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Conditional imports example (ILO-399).
-- Syntax: use ?<pred> "true-path" : "false-path"
--
-- Pick different modules at compile time based on the build target.
-- Predicates: wasm (wasm32 target), native (host binary), test (ilo test run).
--
-- Example (not runnable inline — requires companion files):
-- use ?wasm "platform/wasm-io.ilo" : "platform/native-io.ilo"
-- use ?test "stubs/db-stub.ilo" : "db/real-db.ilo"
--
-- The resolver evaluates the predicate against the current build target
-- and imports exactly one of the two modules; the other is never read.
-- Both branches follow the same import rules as unconditional `use`:
-- flat import (all public declarations), selective import ([name1 name2]),
-- or named-module alias (use alias: form) are NOT combined with ?<pred> —
-- conditional imports always import all declarations from the chosen file.

-- Standalone demonstration: a function that shows the active platform.
platform>t;"native"
32 changes: 32 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ pub struct Param {
pub ty: Type,
}

/// Compile-time predicate for conditional `use` — `use ?wasm "a.ilo" : "b.ilo"`.
///
/// `wasm` — true when building for wasm32 (`--target wasm`).
/// `native` — true when building for a native host (`--target native`, default).
/// `test` — true when running under `ilo test` (`--target test`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum UsePredicate {
Wasm,
Native,
Test,
}

impl UsePredicate {
pub fn from_str(s: &str) -> Option<Self> {
match s {
"wasm" => Some(Self::Wasm),
"native" => Some(Self::Native),
"test" => Some(Self::Test),
_ => None,
}
}
}

/// Top-level declarations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Decl {
Expand Down Expand Up @@ -138,6 +161,9 @@ pub enum Decl {
/// `use alias:"path/to/file.ilo"` — import all public declarations, prefixed
/// with `alias-` (e.g. `math-dbl`, `math-half`). Private (`_`-prefixed)
/// symbols are always excluded from named-module imports.
/// `use ?wasm "wasm-mod.ilo" : "native-mod.ilo"` — conditional import:
/// import `path` when the predicate is true for the current build target,
/// otherwise import `alt_path`. Resolved before verification.
/// Resolved before verification; replaced by the imported declarations in
/// the merged program. Stripped by the verifier/codegen as a safety net.
Use {
Expand All @@ -147,6 +173,12 @@ pub enum Decl {
/// Named module alias: `use alias:"path"` sets this to `Some("alias")`.
/// When set, imported public symbols are renamed `alias-<name>`.
alias: Option<String>,
/// Conditional form: `use ?<pred> "true-path" : "false-path"`.
/// When `Some`, `path` is the true-branch and `alt_path` is the
/// false-branch. `only` and `alias` are disallowed in this form.
predicate: Option<UsePredicate>,
/// The false-branch path for conditional imports. `None` for unconditional.
alt_path: Option<String>,
#[serde(skip)]
span: Span,
},
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ mod tests {
path: "x.ilo".into(),
only: None,
alias: None,
predicate: None,
alt_path: None,
span: Span::UNKNOWN,
});
prog.declarations.push(Decl::Error {
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,8 @@ mod tests {
path: "x.ilo".into(),
only: None,
alias: None,
predicate: None,
alt_path: None,
span: Span::UNKNOWN,
};
let s = format_decl(&use_decl, FmtMode::Dense);
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,8 @@ mod tests {
path: "x.ilo".into(),
only: None,
alias: None,
predicate: None,
alt_path: None,
span: Span::UNKNOWN,
});
let py = emit(&prog);
Expand Down
2 changes: 2 additions & 0 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14370,6 +14370,8 @@ mod tests {
path: "x.ilo".to_string(),
only: None,
alias: None,
predicate: None,
alt_path: None,
span: Span { start: 0, end: 0 },
},
);
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.