Вернулся на number

This commit is contained in:
2023-11-27 14:45:43 +03:00
parent 3a85bf532d
commit 760be42773
4 changed files with 68 additions and 17 deletions

View File

@@ -6,23 +6,44 @@ export const App = () => {
} }
const testText = ` const testText = `
# Welcome to calculator (написать норм приветствие и описать принцип работы калькулятора)
# Lines starting with # are ignored
# Basic math
2 + 2
6 - 3
10 / 4
5 * 1.5
2 + 2 * 2 2 + 2 * 2
sqrt(3^2 + 4^2) (2 + 2) * 2
5cm + 0.2 m in inch
cos(45 deg)
0.1 + 0.2 0.1 + 0.2
# Units
10 inch in cm
2 feet to m
15 degC to degF
3 tbsp to tsp
9 km/h to m/s
# Functions
sqrt(3^2 + 4^2)
cos(pi rad)
sin(30 deg)
# Variables
a = 25 a = 25
b = a * 2 b = a * 2
pow2(x) = x ^ 2
pow2(6)
2 * 2 2 * 2
last + 1 last + 1
# Function declaration
pow2(x) = x ^ 2
pow2(6)
f(x) = (sin(x) + cos(x/2)) * 5 f(x) = (sin(x) + cos(x/2)) * 5
# Matrices
a = [1, 2, 3; 2+2, 5, 6] a = [1, 2, 3; 2+2, 5, 6]
a[2, 3] a[2, 3]
a[1:2, 2] a[1:2, 2]

View File

@@ -1,12 +1,14 @@
html { html {
line-height: 1.5; font-size: 16px;
line-height: 1.4;
tab-size: 4; tab-size: 4;
font-feature-settings: normal; font-feature-settings: normal;
font-variation-settings: normal; font-variation-settings: normal;
} }
body { body {
margin: 0; max-width: 800px;
margin: auto;
height: 100vh; height: 100vh;
} }
@@ -16,6 +18,10 @@ body {
height: 100%; height: 100%;
} }
#root .cm-editor-mount .cm-editor {
outline: none;
}
.math-result { .math-result {
color: rgb(80, 80, 80); color: rgb(80, 80, 80);
margin-left: 8px; margin-left: 8px;

View File

@@ -1,3 +1,3 @@
import { create, all } from 'mathjs' import { create, all } from 'mathjs'
export const math = create(all, { number: 'BigNumber', precision: 16 }) export const math = create(all)

View File

@@ -33,17 +33,34 @@ const createDecorations = (view: EditorView) => {
try { try {
const result = parser.evaluate(text) const result = parser.evaluate(text)
if (result?.isBigNumber || result?.type === 'Unit') { if (result === undefined) {
continue
}
console.log(result)
if (typeof result === 'number') {
parser.set('last', result) parser.set('last', result)
parser.set('prev', result)
parser.set('previous', result)
decoration = Decoration.widget({
widget: new MathResult(math.format(result, { precision: 12 })),
side: 1,
})
} else if (result?.type === 'Unit') {
parser.set('last', result)
parser.set('prev', result)
parser.set('previous', result)
decoration = Decoration.widget({ decoration = Decoration.widget({
widget: new MathResult(result.toString()), widget: new MathResult(result.toString()),
side: 1, side: 1,
}) })
} } else if (result?.type === 'DenseMatrix') {
if (result?.type === 'DenseMatrix') {
parser.set('last', result) parser.set('last', result)
parser.set('prev', result)
parser.set('previous', result)
const size = result.size() const size = result.size()
@@ -58,15 +75,22 @@ const createDecorations = (view: EditorView) => {
side: 1, side: 1,
}) })
} }
} } else if (typeof result === 'function') {
if (typeof result === 'function') {
decoration = Decoration.widget({ decoration = Decoration.widget({
widget: new MathResult(result.syntax), widget: new MathResult(result.syntax),
side: 1, side: 1,
}) })
} else if (result.isResultSet) {
decoration = Decoration.widget({
widget: new MathResult(result.entries[0].toString()),
side: 1,
})
} else {
console.log(result)
} }
} catch (e) {} } catch (e) {
console.error(e)
}
if (decoration) { if (decoration) {
widgets.push(decoration.range(to)) widgets.push(decoration.range(to))