Skip to content

Commit af4426a

Browse files
committed
replace some environment constants with functions
1 parent 142978c commit af4426a

File tree

9 files changed

+184
-53
lines changed

9 files changed

+184
-53
lines changed

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ This version is not yet released. If you are reading this on the website, then t
2424
- Add the [`pretty`](https://uiua.org/docs/pretty) function, which gives the string representation of an array's pretty-printed output
2525
- Add numeric subscripts for [`length ⧻`](https://uiua.org/docs/length) to get the length of a specific axis
2626
- Add numeric subscripts for [`shape △`](https://uiua.org/docs/shape) to get the shape of the first few axes
27+
- Deprecate various environment-relate constants and replace them with noadic functions
28+
- This prevents the details of the compiling environment being used instead of the details from the actual running environment
29+
- Deprecrated: `Os`, `Family`, `Arch`, `ExeExt`, `DllExt`, `Sep`, `NumProcs`
30+
- Replacements: `os`, `osfamily`, `arch`, `exeext`, `dllext`, `pathsep`, `numprocs`
2731
- Add experimental [`progressive indexof ⊘`](https://uiua.org/docs/progressiveindexof) for finding sequential indices of each row of an array in another
2832
- Add experimental [lexical ordering](https://uiua.org/docs/experimental#lexical-ordering) syntax
2933
- This enables some function packs and array notation to execute in the order they are normally read

site/primitives.json

+56-14
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,12 @@
358358
"class": "InversionModifier",
359359
"description": "Invert the behavior of a function, treating its first argument as a constant"
360360
},
361+
"arch": {
362+
"args": 0,
363+
"outputs": 1,
364+
"class": "Environment",
365+
"description": "Get the current architecture"
366+
},
361367
"assert": {
362368
"glyph": "",
363369
"args": 2,
@@ -368,7 +374,7 @@
368374
"astar": {
369375
"outputs": 2,
370376
"modifier_args": 3,
371-
"class": "Misc",
377+
"class": "Algorithm",
372378
"description": "Find shortest paths in a graph",
373379
"deprecated": true
374380
},
@@ -452,7 +458,7 @@
452458
"bytes": {
453459
"args": 2,
454460
"outputs": 1,
455-
"class": "Misc",
461+
"class": "Encoding",
456462
"description": "Convert a value to a byte representation",
457463
"experimental": true
458464
},
@@ -513,7 +519,7 @@
513519
"datetime": {
514520
"args": 1,
515521
"outputs": 1,
516-
"class": "Misc",
522+
"class": "Time",
517523
"description": "Get the date and time information from a time"
518524
},
519525
"deduplicate": {
@@ -527,7 +533,7 @@
527533
"glyph": "",
528534
"outputs": 1,
529535
"modifier_args": 1,
530-
"class": "Misc",
536+
"class": "Algorithm",
531537
"description": "Calculate the derivative of a mathematical expression",
532538
"experimental": true
533539
},
@@ -553,6 +559,12 @@
553559
"class": "DyadicPervasive",
554560
"description": "Divide values"
555561
},
562+
"dllext": {
563+
"args": 0,
564+
"outputs": 1,
565+
"class": "Environment",
566+
"description": "Get the DLL extension for the current platform"
567+
},
556568
"do": {
557569
"glyph": "",
558570
"outputs": 1,
@@ -603,6 +615,12 @@
603615
"class": "Constant",
604616
"description": "The number of radians in a quarter circle"
605617
},
618+
"exeext": {
619+
"args": 0,
620+
"outputs": 1,
621+
"class": "Environment",
622+
"description": "Get the executable extension for the current platform"
623+
},
606624
"fall": {
607625
"glyph": "",
608626
"args": 1,
@@ -613,7 +631,7 @@
613631
"fft": {
614632
"args": 1,
615633
"outputs": 1,
616-
"class": "Misc",
634+
"class": "Algorithm",
617635
"description": "Run the Fast Fourier Transform on an array",
618636
"experimental": true
619637
},
@@ -684,7 +702,7 @@
684702
"gen": {
685703
"args": 2,
686704
"outputs": 1,
687-
"class": "Misc",
705+
"class": "RNG",
688706
"description": "Generate an array of random numbers with a seed"
689707
},
690708
"get": {
@@ -771,7 +789,7 @@
771789
"glyph": "",
772790
"outputs": 1,
773791
"modifier_args": 1,
774-
"class": "Misc",
792+
"class": "Algorithm",
775793
"description": "Calculate an antiderivative of a mathematical expression",
776794
"experimental": true
777795
},
@@ -813,7 +831,7 @@
813831
"args": 2,
814832
"outputs": 1,
815833
"class": "Encoding",
816-
"description": "Render text into an image array",
834+
"description": "Get the current Render text into an image array",
817835
"experimental": true
818836
},
819837
"length": {
@@ -933,9 +951,15 @@
933951
"now": {
934952
"args": 0,
935953
"outputs": 1,
936-
"class": "Misc",
954+
"class": "Time",
937955
"description": "Get the current time in seconds"
938956
},
957+
"numprocs": {
958+
"args": 0,
959+
"outputs": 1,
960+
"class": "Environment",
961+
"description": "Get the number of processors on the current system"
962+
},
939963
"obverse": {
940964
"glyph": "",
941965
"outputs": 1,
@@ -980,6 +1004,18 @@
9801004
"class": "DyadicArray",
9811005
"description": "Change the order of the axes of an array"
9821006
},
1007+
"os": {
1008+
"args": 0,
1009+
"outputs": 1,
1010+
"class": "Environment",
1011+
"description": "Get the current operating system"
1012+
},
1013+
"osfamily": {
1014+
"args": 0,
1015+
"outputs": 1,
1016+
"class": "Environment",
1017+
"description": "Get the current operating system family"
1018+
},
9831019
"over": {
9841020
"glyph": ",",
9851021
"args": 2,
@@ -992,7 +1028,7 @@
9921028
"glyph": "",
9931029
"args": 1,
9941030
"outputs": 1,
995-
"class": "Misc",
1031+
"class": "Encoding",
9961032
"description": "Parse a string as a number"
9971033
},
9981034
"partition": {
@@ -1006,9 +1042,15 @@
10061042
"path": {
10071043
"outputs": 2,
10081044
"modifier_args": 2,
1009-
"class": "Misc",
1045+
"class": "Algorithm",
10101046
"description": "Find the shortest path between two things"
10111047
},
1048+
"pathsep": {
1049+
"args": 0,
1050+
"outputs": 1,
1051+
"class": "Environment",
1052+
"description": "Get the primary path separator for the current platform"
1053+
},
10121054
"pi": {
10131055
"glyph": "π",
10141056
"args": 0,
@@ -1069,7 +1111,7 @@
10691111
"glyph": "",
10701112
"args": 0,
10711113
"outputs": 1,
1072-
"class": "Misc",
1114+
"class": "RNG",
10731115
"description": "Generate a random number in the range [0, 1)"
10741116
},
10751117
"range": {
@@ -1103,7 +1145,7 @@
11031145
"regex": {
11041146
"args": 2,
11051147
"outputs": 1,
1106-
"class": "Misc",
1148+
"class": "Algorithm",
11071149
"description": "Match a regex pattern"
11081150
},
11091151
"remove": {
@@ -1322,7 +1364,7 @@
13221364
"timezone": {
13231365
"args": 0,
13241366
"outputs": 1,
1325-
"class": "Misc",
1367+
"class": "Time",
13261368
"description": "Get the local timezone offset"
13271369
},
13281370
"trace": {

site/src/docs.rs

+8
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ impl Allowed {
482482
PrimClass::Thread => "threads",
483483
PrimClass::Map => "map-functions",
484484
PrimClass::Encoding => "encoding",
485+
PrimClass::Algorithm => "algorithms",
486+
PrimClass::Rng => "rng",
487+
PrimClass::Time => "time",
488+
PrimClass::Environment => "environment",
485489
PrimClass::Misc => "misc-functions",
486490
PrimClass::Sys(_) => "system-functions",
487491
};
@@ -560,6 +564,10 @@ impl Allowed {
560564
PrimClass::Thread => ("Thread".into_view(), "Work with OS threads"),
561565
PrimClass::Map => ("Map".into_view(), "Use arrays as hash maps"),
562566
PrimClass::Encoding => ("Encoding".into_view(), "Convert to and from different encodings"),
567+
PrimClass::Algorithm => ("Algorithms".into_view(), "Useful, specific algorithms"),
568+
PrimClass::Rng => ("Random Number Generation".into_view(), "Generate random numbers"),
569+
PrimClass::Time => ("Time".into_view(), "Work with time"),
570+
PrimClass::Environment => ("Environment".into_view(), "Get information about the environment"),
563571
PrimClass::Misc => ("Miscellaneous".into_view(), ""),
564572
PrimClass::Sys(class) => {
565573
match class {

site/src/other.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::BTreeMap;
2-
31
use leptos::*;
42
use leptos_meta::*;
53
use uiua::{ConstClass, Primitive, SysOp, CONSTANTS};
@@ -151,17 +149,6 @@ pub fn RightToLeft() -> impl IntoView {
151149
pub fn Constants() -> impl IntoView {
152150
use ConstClass::*;
153151
use Primitive::*;
154-
let mut by_class: BTreeMap<ConstClass, Vec<View>> = BTreeMap::new();
155-
for con in CONSTANTS.iter().filter(|con| !con.doc.trim().is_empty()) {
156-
let view = view!(<tr>
157-
<td><Const con=con/></td>
158-
<td><div class="const-desc">{
159-
doc_line_fragments_to_view(&con.doc_frags())
160-
}</div></td>
161-
</tr>)
162-
.into_view();
163-
by_class.entry(con.class).or_default().push(view);
164-
}
165152
let class_cols = [
166153
vec![Math, Time, Color, Flags],
167154
vec![External, Media, Spatial, System, Fun],
@@ -171,8 +158,13 @@ pub fn Constants() -> impl IntoView {
171158
for class in col_classes {
172159
let mut rows = Vec::new();
173160
for con in CONSTANTS.iter().filter(|con| con.class == class) {
161+
let style = if con.is_deprecated() {
162+
"text-decoration: line-through;"
163+
} else {
164+
""
165+
};
174166
let view = view!(<tr>
175-
<td><Const con=con/></td>
167+
<td><div style=style><Const con=con/></div></td>
176168
<td><div class="const-desc">{
177169
doc_line_fragments_to_view(&con.doc_frags())
178170
}</div></td>

src/compile/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ pub struct Compiler {
7474
errors: Vec<UiuaError>,
7575
/// Primitives that have emitted errors because they are deprecated
7676
deprecated_prim_errors: HashSet<Primitive>,
77+
/// Constants that have emitted errors because they are deprecated
78+
deprecated_const_errors: HashSet<&'static str>,
7779
/// Accumulated diagnostics
7880
diagnostics: BTreeSet<Diagnostic>,
7981
/// Print diagnostics as they are encountered
@@ -107,6 +109,7 @@ impl Default for Compiler {
107109
in_try: false,
108110
errors: Vec::new(),
109111
deprecated_prim_errors: HashSet::new(),
112+
deprecated_const_errors: HashSet::new(),
110113
diagnostics: BTreeSet::new(),
111114
print_diagnostics: false,
112115
comptime: true,
@@ -1825,6 +1828,20 @@ code:
18251828
Ok(self.global_index(local.index, true, span))
18261829
} else if let Some(constant) = CONSTANTS.iter().find(|c| c.name == ident) {
18271830
// Name is a built-in constant
1831+
if let Some(suggestion) = constant.deprecation {
1832+
if self.deprecated_const_errors.insert(constant.name) {
1833+
let suggestion = if suggestion.is_empty() {
1834+
String::new()
1835+
} else {
1836+
format!(". {suggestion}")
1837+
};
1838+
let message = format!(
1839+
"{} is deprecated and will be removed in a future version{}",
1840+
constant.name, suggestion
1841+
);
1842+
self.emit_diagnostic(message, DiagnosticKind::Warning, span.clone());
1843+
}
1844+
}
18281845
self.code_meta
18291846
.constant_references
18301847
.insert(span.clone().sp(ident));

src/constant.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct ConstantDef {
2222
pub value: Lazy<ConstantValue>,
2323
/// The constant's documentation
2424
pub doc: Lazy<String>,
25+
/// The suggested replacement because of deprecation
26+
pub deprecation: Option<&'static str>,
2527
}
2628

2729
impl ConstantDef {
@@ -33,6 +35,10 @@ impl ConstantDef {
3335
pub fn doc_frags(&self) -> Vec<PrimDocFragment> {
3436
parse_doc_line_fragments(self.doc())
3537
}
38+
/// Check if the constant is deprecated
39+
pub fn is_deprecated(&self) -> bool {
40+
self.deprecation.is_some()
41+
}
3642
}
3743

3844
/// The value of a shadowable constant
@@ -101,7 +107,16 @@ where
101107
}
102108

103109
macro_rules! constant {
104-
($($(#[doc = $doc:literal])+ ($(#[$attr:meta])* $name:literal, $class:ident, $value:expr)),* $(,)?) => {
110+
($(
111+
$(#[doc = $doc:literal])+
112+
(
113+
$(#[$attr:meta])*
114+
$name:literal,
115+
$class:ident,
116+
$value:expr
117+
$(, deprecated($deprecation:literal))?
118+
)
119+
),* $(,)?) => {
105120
const COUNT: usize = {
106121
let mut count = 0;
107122
$(
@@ -114,6 +129,7 @@ macro_rules! constant {
114129
count
115130
};
116131
/// The list of all shadowable constants
132+
#[allow(path_statements)]
117133
pub static CONSTANTS: [ConstantDef; COUNT] =
118134
[$(
119135
$(#[$attr])*
@@ -130,6 +146,7 @@ macro_rules! constant {
130146
s.pop();
131147
s
132148
}),
149+
deprecation: { None::<&'static str> $(; Some($deprecation))? }
133150
},
134151
)*];
135152
};
@@ -185,17 +202,17 @@ constant!(
185202
[0, 1, -1], [1, 0, -1], [0, -1, -1], [-1, 0, -1]
186203
]),
187204
/// A string identifying the operating system
188-
("Os", System, std::env::consts::OS),
205+
("Os", System, std::env::consts::OS, deprecated("Use the os function instead")),
189206
/// A string identifying family of the operating system
190-
("Family", System, std::env::consts::FAMILY),
207+
("Family", System, std::env::consts::FAMILY, deprecated("Use the osfamily function instead")),
191208
/// A string identifying the architecture of the CPU
192-
("Arch", System, std::env::consts::ARCH),
209+
("Arch", System, std::env::consts::ARCH, deprecated("Use the arch function instead")),
193210
/// The executable file extension
194-
("ExeExt", System, std::env::consts::EXE_EXTENSION),
211+
("ExeExt", System, std::env::consts::EXE_EXTENSION, deprecated("Use the exeext function instead")),
195212
/// The file extension for shared libraries
196-
("DllExt", System, std::env::consts::DLL_EXTENSION),
213+
("DllExt", System, std::env::consts::DLL_EXTENSION, deprecated("Use the dllext function instead")),
197214
/// The primary path separator character
198-
("Sep", System, std::path::MAIN_SEPARATOR),
215+
("Sep", System, std::path::MAIN_SEPARATOR, deprecated("Use the pathsep function instead")),
199216
/// The path of the current source file relative to `WorkingDir`
200217
("ThisFile", System, ConstantValue::ThisFile),
201218
/// The name of the current source file
@@ -205,7 +222,7 @@ constant!(
205222
/// The compile-time working directory
206223
("WorkingDir", System, ConstantValue::WorkingDir),
207224
/// The number of processors available
208-
("NumProcs", System, num_cpus::get() as f64),
225+
("NumProcs", System, num_cpus::get() as f64, deprecated("Use the numprocs function instead")),
209226
/// A boolean `true` value for use in `json`
210227
("True", External, Array::json_bool(true)),
211228
/// A boolean `false` value for use in `json`

0 commit comments

Comments
 (0)