support for arrays and a shorthand for vector
This commit is contained in:
parent
83bbc5edab
commit
3651b7fe18
5 changed files with 37 additions and 4 deletions
|
|
@ -21,7 +21,11 @@ The repl has the following syntax (It's work in progress, new capabilities will
|
||||||
```a = vector(12, 1)```
|
```a = vector(12, 1)```
|
||||||
> > vector@0{x0:0, y0: 2, x:12, y:1}
|
> > vector@0{x0:0, y0: 2, x:12, y:1}
|
||||||
* ```vector(1 2)``` works as well. The start is now the origin.
|
* ```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
|
* properties
|
||||||
* ```a = vector(12, 1)```
|
* ```a = vector(12, 1)```
|
||||||
|
|
|
||||||
|
|
@ -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 = {
|
const vector = {
|
||||||
id: index_sequence++,
|
id: index_sequence++,
|
||||||
x0: x0,
|
x0: x0,
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import {parse} from './parser';
|
||||||
import {add_vector_arrow, add_vector_arrow_to_svg, update_vector_arrow} from "./svg_functions";
|
import {add_vector_arrow, add_vector_arrow_to_svg, update_vector_arrow} from "./svg_functions";
|
||||||
import {
|
import {
|
||||||
addition,
|
addition,
|
||||||
|
create_vector,
|
||||||
division,
|
division,
|
||||||
functions,
|
functions,
|
||||||
label,
|
label,
|
||||||
|
|
@ -274,6 +275,15 @@ const visit = function (expr) {
|
||||||
case 'reference': {
|
case 'reference': {
|
||||||
return references[expr.name];
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,14 +103,13 @@ export const parse = function (tokens) {
|
||||||
} else {
|
} else {
|
||||||
throw {message: "Expect ')' after arguments."};
|
throw {message: "Expect ')' after arguments."};
|
||||||
}
|
}
|
||||||
match([token_types.COMMA]);
|
match([token_types.COMMA, token_types.SPACE]);
|
||||||
} while (!match([token_types.RIGHT_PAREN]));
|
} while (!match([token_types.RIGHT_PAREN]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return {type: 'call', name: callee, arguments: arguments_list};
|
return {type: 'call', name: callee, arguments: arguments_list};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function primary() {
|
function primary() {
|
||||||
if (match([token_types.NUMERIC, token_types.STRING])) {
|
if (match([token_types.NUMERIC, token_types.STRING])) {
|
||||||
return {type: 'literal', value: previous_token().value, value_type: previous_token().type};
|
return {type: 'literal', value: previous_token().value, value_type: previous_token().type};
|
||||||
|
|
@ -147,6 +146,23 @@ export const parse = function (tokens) {
|
||||||
};
|
};
|
||||||
advance();
|
advance();
|
||||||
return result;
|
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};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@ export const scan = function (command) {
|
||||||
return token_types.RIGHT_BRACKET;
|
return token_types.RIGHT_BRACKET;
|
||||||
case ',':
|
case ',':
|
||||||
return token_types.COMMA;
|
return token_types.COMMA;
|
||||||
|
case ' ':
|
||||||
|
return token_types.SPACE;
|
||||||
case '.':
|
case '.':
|
||||||
return token_types.DOT;
|
return token_types.DOT;
|
||||||
case '-':
|
case '-':
|
||||||
|
|
@ -192,6 +194,7 @@ export const token_types = {
|
||||||
LEFT_BRACKET: {type: 'left_bracket'},
|
LEFT_BRACKET: {type: 'left_bracket'},
|
||||||
RIGHT_BRACKET: {type: 'right_bracket'},
|
RIGHT_BRACKET: {type: 'right_bracket'},
|
||||||
COMMA: {type: 'comma'},
|
COMMA: {type: 'comma'},
|
||||||
|
SPACE: {type: 'space'},
|
||||||
DOT: {type: 'dot'},
|
DOT: {type: 'dot'},
|
||||||
MINUS: {type: 'minus'},
|
MINUS: {type: 'minus'},
|
||||||
PLUS: {type: 'plus'},
|
PLUS: {type: 'plus'},
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue