Added table reading, Helper Expressions, etc.

Can now read functions instead of purely numeric values.
This commit is contained in:
66945
2022-03-07 14:36:18 -08:00
committed by GitHub
parent 82d7159d22
commit e4270954ac
@@ -8,16 +8,16 @@
<div id="calculator" style="width: 100wh; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; overflow: hidden;"></div> <div id="calculator" style="width: 100wh; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; overflow: hidden;"></div>
<script> <script>
const EXPRESSION_TYPES = { const EXPRESSION_TYPES = {
'expression': ['integer', 'double', 'point', 'array'], 'expression': ['number', 'point', 'array'],
'note': ['note'], 'note': ['note'],
'table': ['table'] 'table': ['table']
}; };
let elt = document.getElementById('calculator'); let elt = document.getElementById('calculator');
let calculator = Desmos.GraphingCalculator(elt); let calculator = Desmos.GraphingCalculator(elt);
// calculator.setExpression({ id: 'graph1', latex: 'y=x^2' });
let variables = [{'name': 'bob', 'type': 'integer', 'value': '27'}]; let helperExpressions = {};
let variables = [];
let url = prompt("Robot URL", "localhost"); let url = prompt("Robot URL", "localhost");
let port = 8000; let port = 8000;
@@ -28,7 +28,6 @@
http.addEventListener("error", handleError); http.addEventListener("error", handleError);
http.open("POST", `http://${url}:${port}`); http.open("POST", `http://${url}:${port}`);
// debugger;
let json = serverStringify(getVariables()); let json = serverStringify(getVariables());
http.send(json); http.send(json);
} }
@@ -73,15 +72,29 @@
let expressions = calculator.getExpressions(); let expressions = calculator.getExpressions();
for(expression of expressions) { for(expression of expressions) {
if(expression['type'] === 'expression') if(expression.type === 'expression')
vars = readVariable(expression['latex']); vars = readVariable(expression.latex);
else if(expression.type === 'table')
vars = readTable(expression);
else else
console.log('Invalid output type : ' + expression['type']); console.log('Invalid output type : ' + expression.type);
} }
return vars; return vars;
} }
function findExpressionType(lvalue) {
let raw = lvalue.replace('\\left', '')
.replace('\\right', '');
if(raw.match(/\([a-zA-Z0-9._{}\(\)]*,[a-zA-Z0-9._{}\(\)]*\)/))
return 'point';
else if(raw.match(/\[[a-zA-Z0-9.,_{}\(\)]*\]/))
return 'array';
else
return 'number';
}
function readVariable(latex) { function readVariable(latex) {
let vars = []; let vars = [];
let lname = latex.split('=')[0]; let lname = latex.split('=')[0];
@@ -91,12 +104,61 @@
let name = regularName(lname); let name = regularName(lname);
let value = regularValue(lvalue); let value = regularValue(lvalue);
vars.push({"name": name, "type": "expression", "value": value}); let type = findExpressionType(lvalue);
let numericValue, numericValueX, numericValueY, listValue;
switch(type) {
case 'number':
numericValue = getNumericValue(name, lname);
break;
case 'point':
numericValueX = getNumericValue(name + 'x', lname + '.x');
numericValueY = getNumericValue(name + 'y', lname + '.y');
break;
case 'array':
listValue = getListValue(name, lname);
break;
}
if(numericValue)
vars.push({"name": name, "type": "number", "value": numericValue});
if(numericValueX && numericValueY)
vars.push({"name": name, "type": "point", "value": numericValueX + ',' + numericValueY});
if(listValue)
vars.push({"name": name, "type": "array", "value": listValue.toString()});
} }
return vars; return vars;
} }
function readTable(expression) {
let vars = [];
for(let column of expression.columns) {
let name = regularName(column.latex);
let listValue = getListValue(name, column.latex);
if(listValue)
vars.push({"name": name, "type": "array", "value": listValue.toString()});
}
return vars;
}
function getNumericValue(key, lname) {
if(!(key in helperExpressions))
helperExpressions[key] = calculator.HelperExpression({ latex: lname });
else
return helperExpressions[key].numericValue;
}
function getListValue(key, lname) {
if(!(key in helperExpressions))
helperExpressions[key] = calculator.HelperExpression({ latex: lname });
else
return helperExpressions[key].listValue;
}
function latexName(name) { function latexName(name) {
return name.substring(0, 1) + '_{' + name.substring(1, name.length) + '}'; return name.substring(0, 1) + '_{' + name.substring(1, name.length) + '}';
} }