Indexing vectors by ⟦⟧

This commit is contained in:
PaddiM8 2022-01-05 23:30:54 +01:00
parent c0cc4060a4
commit 2cf8f41124
4 changed files with 43 additions and 8 deletions

View File

@ -108,7 +108,7 @@ impl Highlighter for LineHighlighter {
r"(?x)
(?P<op>([+\-/*%^!×÷⋅]|if|otherwise|load|exit|clear|help)) |
(?P<radix>0[box][a-zA-Z0-9]+) |
(?P<identifier>[^!-@\s_|^\[\]\{\}¹²³]+(_\d+)?)",
(?P<identifier>[^!-@\s_|^\[\]\{\}¹²³]+(_\d+)?)",
)
.unwrap();
@ -154,6 +154,7 @@ lazy_static! {
m.insert("sqrt", "");
m.insert("tau", "τ");
m.insert("(", "()");
m.insert("[[", "⟦⟧");
m.insert("!=", "");
m.insert(">=", "");
m.insert("<=", "");
@ -212,7 +213,7 @@ impl Completer for RLHelper {
fn update(&self, line: &mut rustyline::line_buffer::LineBuffer, start: usize, elected: &str) {
line.backspace(line.pos() - start);
line.insert_str(line.pos(), elected);
line.move_forward(if elected.ends_with(")") {
line.move_forward(if elected.ends_with(")") || elected.ends_with("") {
elected.chars().count() - 1
} else {
elected.chars().count()

View File

@ -37,7 +37,9 @@ pub enum TokenKind {
OpenParenthesis,
ClosedParenthesis,
OpenBracket,
OpenDoubleBracket,
ClosedBracket,
ClosedDoubleBracket,
OpenBrace,
ClosedBrace,
Comma,
@ -134,6 +136,8 @@ impl<'a> Lexer<'a> {
')' => build(TokenKind::ClosedParenthesis, "", span),
'[' => build(TokenKind::OpenBracket, "", span),
']' => build(TokenKind::ClosedBracket, "", span),
'⟦' => build(TokenKind::OpenDoubleBracket, "", span),
'⟧' => build(TokenKind::ClosedDoubleBracket, "", span),
'{' => build(TokenKind::OpenBrace, "", span),
'}' => build(TokenKind::ClosedBrace, "", span),
'!' => build(TokenKind::Exclamation, "", span),
@ -167,6 +171,14 @@ impl<'a> Lexer<'a> {
self.advance();
return build(TokenKind::Power, "", span);
}
(TokenKind::OpenBracket, Some('[')) => {
self.advance();
return build(TokenKind::OpenDoubleBracket, "", span);
}
(TokenKind::ClosedBracket, Some(']')) => {
self.advance();
return build(TokenKind::ClosedDoubleBracket, "", span);
}
(TokenKind::Exclamation, Some('=')) => {
self.advance();
return build(TokenKind::NotEquals, "", span);
@ -370,7 +382,7 @@ fn is_valid_identifier(c: Option<&char>) -> bool {
match c {
'+' | '-' | '/' | '*' | '%' | '^' | '!' | '(' | ')' | '=' | '.' | ',' | ';' | '|'
| '⌊' | '⌋' | '⌈' | '⌉' | '[' | ']' | '{' | '}' | 'π' | '√' | 'τ' | 'ϕ' | 'Γ' | '<'
| '>' | '≠' | '≥' | '≤' | '×' | '÷' | '⋅' | '\n' => false,
| '>' | '≠' | '≥' | '≤' | '×' | '÷' | '⋅' | '⟦' | '⟧' | '\n' => false,
_ => !c.is_digit(10) || is_superscript(c) || is_subscript(c),
}
} else {

View File

@ -549,7 +549,7 @@ fn parse_unary(context: &mut Context) -> Result<Expr, CalcError> {
}
fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
let left = parse_factorial(context)?;
let left = parse_indexer(context)?;
if match_token(context, TokenKind::Power) {
let op = advance(context).kind;
@ -560,6 +560,19 @@ fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
Ok(left)
}
fn parse_indexer(context: &mut Context) -> Result<Expr, CalcError> {
let left = parse_factorial(context)?;
if match_token(context, TokenKind::OpenDoubleBracket) {
advance(context);
let right = Box::new(parse_expr(context)?);
consume(context, TokenKind::ClosedDoubleBracket)?;
return Ok(Expr::Indexer(Box::new(left), right));
}
Ok(left)
}
fn parse_factorial(context: &mut Context) -> Result<Expr, CalcError> {
let expr = parse_primary(context)?;

View File

@ -282,18 +282,27 @@
let result = input;
let offset = 0;
result = result.replace(
/(?<radix>0[box][a-zA-Z0-9]+)|(?<comparison>(!=|[<>]=?))|(?<html>[<>&]|(\n\s*\}?|\s+))|(?<op>([+\-/*%^!≈×÷⋅]|if|otherwise)|(?<identifier>[^!-@\s_|^⌊⌋⌈⌉≈\[\]\{\}≠≥≤⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎]+(_\d+)?)\(?)/g,
/(?<brackets>\[\[)|(?<radix>0[box][a-zA-Z0-9]+)|(?<comparison>(!=|[<>]=?))|(?<html>[<>&]|(\n\s*\}?|\s+))|(?<op>([+\-/*%^!≈×÷⋅]|if|otherwise)|(?<identifier>[^!-@\s_|^⌊⌋⌈⌉≈\[\]\{\}⟦⟧≠≥≤⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎]+(_\d+)?)\(?)/g,
(
substring,
brackets,
_radix,
_,
comparison,
_2,
html,
comparison,
_3,
html,
_4,
op,
identifier
) => {
if (brackets) {
if (substring == "[[") {
offset -= 1;
return "⟦⟧";
}
}
if (comparison) {
if (substring == "<=") return "≤";
if (substring == ">=") return "≥";