Skip to content

Commit

Permalink
fix a lexing bug and make utf8 use subscripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Sep 14, 2024
1 parent 24154ac commit 9b0390e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion site/primitives.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@
},
"both": {
"glyph": "",
"args": 2,
"outputs": 1,
"modifier_args": 1,
"class": "Planet",
Expand Down
16 changes: 12 additions & 4 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2049,10 +2049,12 @@ code:
}
#[allow(clippy::match_single_binding)]
fn subscript(&mut self, sub: Subscript, span: CodeSpan, call: bool) -> UiuaResult {
self.experimental_error(&span, || {
"Subscripts are experimental. To use them, add \
`# Experimental!` to the top of the file."
});
if !matches!(sub.word.value, Word::Primitive(Primitive::Utf8)) {
self.experimental_error(&span, || {
"Subscripts are experimental. To use them, add \
`# Experimental!` to the top of the file."
});
}
let n = sub.n.value;
match sub.word.value {
Word::Modified(m) => match m.modifier.value {
Expand Down Expand Up @@ -2106,6 +2108,12 @@ code:
self.primitive(Primitive::Mul, span.clone(), call)?;
self.primitive(Primitive::Floor, span, call)?;
}
Primitive::Utf8 => {
if n != 8 {
self.add_error(span.clone(), "Only UTF-8 is supported");
}
self.primitive(Primitive::Utf8, span, call)?
}
_ => {
self.add_error(
span.clone(),
Expand Down
8 changes: 7 additions & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ impl<'a> Formatter<'a> {
}
self.output.push(')');
}
Word::Primitive(prim) => self.push(&word.span, &prim.to_string()),
Word::Primitive(prim) => self.format_primitive(*prim, &word.span),
Word::Modified(m) => {
self.format_modifier(&m.modifier);
self.format_words(&m.operands, true, depth);
Expand Down Expand Up @@ -1216,6 +1216,12 @@ impl<'a> Formatter<'a> {
}
}
}
fn format_primitive(&mut self, prim: Primitive, span: &CodeSpan) {
match prim {
Primitive::Utf8 => self.push(span, "utf"),
_ => self.push(span, &prim.to_string()),
}
}
fn format_multiline_words(
&mut self,
mut lines: &[Vec<Sp<Word>>],
Expand Down
2 changes: 1 addition & 1 deletion src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ impl<'a> Lexer<'a> {
// Try to parse as primitives
let lowercase_end = ident
.char_indices()
.find(|(_, c)| !c.is_ascii_lowercase())
.find(|(_, c)| !c.is_ascii_lowercase() && *c != '&')
.map_or(ident.len(), |(i, _)| i);
let lowercase = &ident[..lowercase_end];
if let Some(prims) = Primitive::from_format_name_multi(lowercase) {
Expand Down

0 comments on commit 9b0390e

Please sign in to comment.