mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-03-05 00:31:10 +01:00
Indexing vectors by ⟦⟧
This commit is contained in:
parent
c0cc4060a4
commit
2cf8f41124
@ -108,7 +108,7 @@ impl Highlighter for LineHighlighter {
|
|||||||
r"(?x)
|
r"(?x)
|
||||||
(?P<op>([+\-/*%^!×÷⋅]|if|otherwise|load|exit|clear|help)) |
|
(?P<op>([+\-/*%^!×÷⋅]|if|otherwise|load|exit|clear|help)) |
|
||||||
(?P<radix>0[box][a-zA-Z0-9]+) |
|
(?P<radix>0[box][a-zA-Z0-9]+) |
|
||||||
(?P<identifier>[^!-@\s_|^⌊⌋⌈⌉\[\]\{\}≠≥≤⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎]+(_\d+)?)",
|
(?P<identifier>[^!-@\s_|^⌊⌋⌈⌉\[\]\{\}⟦⟧≠≥≤⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾₀₁₂₃₄₅₆₇₈₉₊₋₌₍₎]+(_\d+)?)",
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
@ -154,6 +154,7 @@ lazy_static! {
|
|||||||
m.insert("sqrt", "√");
|
m.insert("sqrt", "√");
|
||||||
m.insert("tau", "τ");
|
m.insert("tau", "τ");
|
||||||
m.insert("(", "()");
|
m.insert("(", "()");
|
||||||
|
m.insert("[[", "⟦⟧");
|
||||||
m.insert("!=", "≠");
|
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) {
|
fn update(&self, line: &mut rustyline::line_buffer::LineBuffer, start: usize, elected: &str) {
|
||||||
line.backspace(line.pos() - start);
|
line.backspace(line.pos() - start);
|
||||||
line.insert_str(line.pos(), elected);
|
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
|
elected.chars().count() - 1
|
||||||
} else {
|
} else {
|
||||||
elected.chars().count()
|
elected.chars().count()
|
||||||
|
@ -37,7 +37,9 @@ pub enum TokenKind {
|
|||||||
OpenParenthesis,
|
OpenParenthesis,
|
||||||
ClosedParenthesis,
|
ClosedParenthesis,
|
||||||
OpenBracket,
|
OpenBracket,
|
||||||
|
OpenDoubleBracket,
|
||||||
ClosedBracket,
|
ClosedBracket,
|
||||||
|
ClosedDoubleBracket,
|
||||||
OpenBrace,
|
OpenBrace,
|
||||||
ClosedBrace,
|
ClosedBrace,
|
||||||
Comma,
|
Comma,
|
||||||
@ -134,6 +136,8 @@ impl<'a> Lexer<'a> {
|
|||||||
')' => build(TokenKind::ClosedParenthesis, "", span),
|
')' => build(TokenKind::ClosedParenthesis, "", span),
|
||||||
'[' => build(TokenKind::OpenBracket, "", span),
|
'[' => build(TokenKind::OpenBracket, "", span),
|
||||||
']' => build(TokenKind::ClosedBracket, "", span),
|
']' => build(TokenKind::ClosedBracket, "", span),
|
||||||
|
'⟦' => build(TokenKind::OpenDoubleBracket, "", span),
|
||||||
|
'⟧' => build(TokenKind::ClosedDoubleBracket, "", span),
|
||||||
'{' => build(TokenKind::OpenBrace, "", span),
|
'{' => build(TokenKind::OpenBrace, "", span),
|
||||||
'}' => build(TokenKind::ClosedBrace, "", span),
|
'}' => build(TokenKind::ClosedBrace, "", span),
|
||||||
'!' => build(TokenKind::Exclamation, "", span),
|
'!' => build(TokenKind::Exclamation, "", span),
|
||||||
@ -167,6 +171,14 @@ impl<'a> Lexer<'a> {
|
|||||||
self.advance();
|
self.advance();
|
||||||
return build(TokenKind::Power, "", span);
|
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('=')) => {
|
(TokenKind::Exclamation, Some('=')) => {
|
||||||
self.advance();
|
self.advance();
|
||||||
return build(TokenKind::NotEquals, "", span);
|
return build(TokenKind::NotEquals, "", span);
|
||||||
@ -370,7 +382,7 @@ fn is_valid_identifier(c: Option<&char>) -> bool {
|
|||||||
match c {
|
match c {
|
||||||
'+' | '-' | '/' | '*' | '%' | '^' | '!' | '(' | ')' | '=' | '.' | ',' | ';' | '|'
|
'+' | '-' | '/' | '*' | '%' | '^' | '!' | '(' | ')' | '=' | '.' | ',' | ';' | '|'
|
||||||
| '⌊' | '⌋' | '⌈' | '⌉' | '[' | ']' | '{' | '}' | 'π' | '√' | 'τ' | 'ϕ' | 'Γ' | '<'
|
| '⌊' | '⌋' | '⌈' | '⌉' | '[' | ']' | '{' | '}' | 'π' | '√' | 'τ' | 'ϕ' | 'Γ' | '<'
|
||||||
| '>' | '≠' | '≥' | '≤' | '×' | '÷' | '⋅' | '\n' => false,
|
| '>' | '≠' | '≥' | '≤' | '×' | '÷' | '⋅' | '⟦' | '⟧' | '\n' => false,
|
||||||
_ => !c.is_digit(10) || is_superscript(c) || is_subscript(c),
|
_ => !c.is_digit(10) || is_superscript(c) || is_subscript(c),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -549,7 +549,7 @@ fn parse_unary(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_exponent(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) {
|
if match_token(context, TokenKind::Power) {
|
||||||
let op = advance(context).kind;
|
let op = advance(context).kind;
|
||||||
@ -560,6 +560,19 @@ fn parse_exponent(context: &mut Context) -> Result<Expr, CalcError> {
|
|||||||
Ok(left)
|
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> {
|
fn parse_factorial(context: &mut Context) -> Result<Expr, CalcError> {
|
||||||
let expr = parse_primary(context)?;
|
let expr = parse_primary(context)?;
|
||||||
|
|
||||||
|
@ -282,18 +282,27 @@
|
|||||||
let result = input;
|
let result = input;
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
result = result.replace(
|
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,
|
substring,
|
||||||
|
brackets,
|
||||||
_radix,
|
_radix,
|
||||||
_,
|
|
||||||
comparison,
|
|
||||||
_2,
|
_2,
|
||||||
html,
|
comparison,
|
||||||
_3,
|
_3,
|
||||||
|
html,
|
||||||
|
_4,
|
||||||
op,
|
op,
|
||||||
identifier
|
identifier
|
||||||
) => {
|
) => {
|
||||||
|
if (brackets) {
|
||||||
|
if (substring == "[[") {
|
||||||
|
offset -= 1;
|
||||||
|
|
||||||
|
return "⟦⟧";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (comparison) {
|
if (comparison) {
|
||||||
if (substring == "<=") return "≤";
|
if (substring == "<=") return "≤";
|
||||||
if (substring == ">=") return "≥";
|
if (substring == ">=") return "≥";
|
||||||
|
Loading…
Reference in New Issue
Block a user