support for arrays and a shorthand for vector

This commit is contained in:
Sander Hautvast 2021-03-08 17:38:58 +01:00
parent 83bbc5edab
commit 3651b7fe18
5 changed files with 37 additions and 4 deletions

View file

@ -21,7 +21,11 @@ The repl has the following syntax (It's work in progress, new capabilities will
```a = vector(12, 1)```
> > vector@0{x0:0, y0: 2, x:12, y:1}
* ```vector(1 2)``` works as well. The start is now the origin.
commas are not mandatory. I'm planning to add a more mathematical notation for vectors: ```[1 2]```
commas are not mandatory.
* This means spaces are meaningful.
```vector(-1 -1)``` != ```vector(-1-1)``` and the latter would mean ```vector(-2)``` which is not legal.
* arrays: \[i<sub>0</sub> i<sub>1</sub> i<sub>2</sub> ... i<sub>n</sub>] or \[i<sub>0</sub>, i<sub>1</sub>, i<sub>2</sub>, ... i<sub>n</sub>] creates an array.
* \[i<sub>0</sub> i<sub>1</sub>] is special: it is a shorthand for creating a vector. I'll have to think of another way to create an array of length 2...
* properties
* ```a = vector(12, 1)```

View file

@ -65,7 +65,7 @@ const help = function () {
}
}
const create_vector = function (x0, y0, x, y) { //rename to create_vector
export const create_vector = function (x0, y0, x, y) { //rename to create_vector
const vector = {
id: index_sequence++,
x0: x0,

View file

@ -4,6 +4,7 @@ import {parse} from './parser';
import {add_vector_arrow, add_vector_arrow_to_svg, update_vector_arrow} from "./svg_functions";
import {
addition,
create_vector,
division,
functions,
label,
@ -274,6 +275,15 @@ const visit = function (expr) {
case 'reference': {
return references[expr.name];
}
case 'array': { //unsure this is what I want
let array = expr.elements.map(x => x.value);
if (array.length === 2) {
return create_vector(0, 0, array[0], array[1]);
} else {
return array;
}
}
}
}

View file

@ -103,14 +103,13 @@ export const parse = function (tokens) {
} else {
throw {message: "Expect ')' after arguments."};
}
match([token_types.COMMA]);
match([token_types.COMMA, token_types.SPACE]);
} while (!match([token_types.RIGHT_PAREN]));
}
return {type: 'call', name: callee, arguments: arguments_list};
}
function primary() {
if (match([token_types.NUMERIC, token_types.STRING])) {
return {type: 'literal', value: previous_token().value, value_type: previous_token().type};
@ -147,6 +146,23 @@ export const parse = function (tokens) {
};
advance();
return result;
} else if (match([token_types.LEFT_BRACKET])) {
let array = [];
if (!check(token_types.RIGHT_BRACKET, token_index)) {
let result;
do {
result = expression();
if (result) {
array.push(result);
} else {
throw {message: "Expect ']' after array elements."};
}
match([token_types.COMMA, token_types.SPACE]);
} while (!match([token_types.RIGHT_BRACKET]));
}
return {type: 'array', elements: array};
}
}

View file

@ -31,6 +31,8 @@ export const scan = function (command) {
return token_types.RIGHT_BRACKET;
case ',':
return token_types.COMMA;
case ' ':
return token_types.SPACE;
case '.':
return token_types.DOT;
case '-':
@ -192,6 +194,7 @@ export const token_types = {
LEFT_BRACKET: {type: 'left_bracket'},
RIGHT_BRACKET: {type: 'right_bracket'},
COMMA: {type: 'comma'},
SPACE: {type: 'space'},
DOT: {type: 'dot'},
MINUS: {type: 'minus'},
PLUS: {type: 'plus'},