fixed incorrect behavior after reassignment to variable

This commit is contained in:
Sander Hautvast 2021-02-26 18:06:26 +01:00
parent 152877ac8f
commit 34e58b193a
2 changed files with 19 additions and 19 deletions

View file

@ -1,10 +1,6 @@
import {scan, token_types} from './scanner'; import {scan, token_types} from './scanner';
import {parse} from './parser'; import {parse} from './parser';
import { import {add_vector_arrow_to_svg, remove_child, update_vector_arrow} from "./svg_functions";
add_vector_arrow_to_svg,
remove_child,
update_vector_arrow
} from "./svg_functions";
export let vectors = []; // collection of added vectors // maybe move to console.js export let vectors = []; // collection of added vectors // maybe move to console.js
const state = {}; const state = {};
@ -13,7 +9,7 @@ const command_history_element = document.getElementById('command_history');
command_input_element.value = ''; command_input_element.value = '';
let command_history = ['']; let command_history = [''];
let command_history_index = 0; let command_history_index = 0;
let vectors_index_sequence = 0;
export const remove_vector = function (vector_or_index) { export const remove_vector = function (vector_or_index) {
let index; let index;
@ -49,10 +45,12 @@ export const update_lazy_objects = function () {
state[object.binding].object.y0 = value.object.y0; state[object.binding].object.y0 = value.object.y0;
state[object.binding].object.x = value.object.x; state[object.binding].object.x = value.object.x;
state[object.binding].object.y = value.object.y; state[object.binding].object.y = value.object.y;
state[object.binding].object.id = value.object.id;
let description = state[object.binding].description; let description = state[object.binding].description;
if (!description) { if (!description) {
description = state[object.binding]; description = state[object.binding];
} }
return {description: object.binding + ':' + description}; return {description: object.binding + ':' + description};
}); });
} }
@ -69,7 +67,7 @@ export const adjust_input_element_height = function () {
command_input_element.onkeyup = function handle_key_input(event) { command_input_element.onkeyup = function handle_key_input(event) {
adjust_input_element_height(); adjust_input_element_height();
if (event.key === 'ArrowUp') { if (event.key === 'ArrowUp' && !event.shiftKey) {
if (command_history_index > -1) { if (command_history_index > -1) {
command_input_element.value = command_history[command_history_index]; command_input_element.value = command_history[command_history_index];
if (command_history_index > 0) { if (command_history_index > 0) {
@ -77,7 +75,7 @@ command_input_element.onkeyup = function handle_key_input(event) {
} }
} }
} }
if (event.key === 'ArrowDown') { if (event.key === 'ArrowDown' && !event.shiftKey) {
if (command_history_index < command_history.length - 1) { if (command_history_index < command_history.length - 1) {
command_history_index += 1; command_history_index += 1;
command_input_element.value = command_history[command_history_index]; command_input_element.value = command_history[command_history_index];
@ -90,7 +88,7 @@ command_input_element.onkeyup = function handle_key_input(event) {
} }
}; };
const handle_enter = function(){ const handle_enter = function () {
let commands = command_input_element.value; let commands = command_input_element.value;
command_input_element.value = ''; command_input_element.value = '';
adjust_input_element_height(); adjust_input_element_height();
@ -106,15 +104,15 @@ const handle_enter = function(){
let result; let result;
try { try {
result = visit_expression(statement); result = visit_expression(statement);
let object_wrapper = result.value; let object_wrapper = result.value !== undefined ? result.value : result;
result.value.object.label = object_wrapper.binding; object_wrapper.object.label = object_wrapper.binding;
if (object_wrapper.object.is_vector) { if (object_wrapper.object.is_vector) {
if (object_wrapper.previous) { if (object_wrapper.previous) {
update_vector_arrow(object_wrapper.previous.id, object_wrapper.object); update_vector_arrow(object_wrapper.previous.id, object_wrapper.object);
} else { } else {
vectors.push(result.value.object); vectors.push(object_wrapper.object);
add_vector_arrow_to_svg(result.value.object); add_vector_arrow_to_svg(object_wrapper.object);
} }
} }
@ -136,7 +134,7 @@ const visit_expression = function (expr) {
case 'declaration': { case 'declaration': {
let value = visit_expression(expr.initializer); let value = visit_expression(expr.initializer);
value.binding = expr.var_name.value; value.binding = expr.var_name.value;
if (value.binding in state) { if (state[value.binding]) {
value.previous = state[value.binding].object; value.previous = state[value.binding].object;
} }
state[value.binding] = value; state[value.binding] = value;
@ -144,7 +142,7 @@ const visit_expression = function (expr) {
if (!description) { if (!description) {
description = state[value.binding]; //questionable. use toString instead of message? description = state[value.binding]; //questionable. use toString instead of message?
} }
update_lazy_objects();
return {description: expr.var_name.value + ':' + description, value: value}; return {description: expr.var_name.value + ':' + description, value: value};
} }
case 'group': case 'group':
@ -280,7 +278,7 @@ const addition = function (left, right) {
} }
export const create_vector = function (vector) { //rename to create_vector export const create_vector = function (vector) { //rename to create_vector
vector.id = vectors.length; vector.id = vectors_index_sequence++;
vector.add = (other) => create_vector({ vector.add = (other) => create_vector({
x0: vector.x0 + other.x0, x0: vector.x0 + other.x0,
y0: vector.x0 + other.x0, y0: vector.x0 + other.x0,

View file

@ -188,7 +188,8 @@ export const update_vector_arrow = function (id, vector) {
let arrow = document.getElementById(id.toString()); let arrow = document.getElementById(id.toString());
if (arrow) { if (arrow) {
arrow.setAttribute('d', d); arrow.setAttribute('d', d);
update_label_position(id, calc_screen_x(vector.x) + 5, calc_screen_y(vector.y) + 5); arrow.id = vector.id;
update_label_position(id, vector.id, calc_screen_x(vector.x) + 5, calc_screen_y(vector.y) + 5);
} }
} }
@ -235,10 +236,11 @@ const create_defs = function () {
return defs; return defs;
} }
const update_label_position = function (id, x, y) { const update_label_position = function (id, new_id, x, y) {
let label = document.getElementById('l' + id); let label = document.getElementById('l' + id);
label.setAttribute('x', x.toString()); label.setAttribute('x', x.toString());
label.setAttribute('y', y.toString()); label.setAttribute('y', y.toString());
label.id = 'l' + new_id;
} }
/** /**
@ -252,7 +254,7 @@ const move_vector = function (event) {
vectors[moving_vector.id].x = (current_x - origin_x) / grid_size; vectors[moving_vector.id].x = (current_x - origin_x) / grid_size;
vectors[moving_vector.id].y = (origin_y - current_y) / grid_size; vectors[moving_vector.id].y = (origin_y - current_y) / grid_size;
moving_vector.setAttribute('d', create_d(origin_x, origin_y, current_x, current_y)); moving_vector.setAttribute('d', create_d(origin_x, origin_y, current_x, current_y));
update_label_position(moving_vector.id, current_x + 5, current_y + 5); update_label_position(moving_vector.id,moving_vector.id, current_x + 5, current_y + 5);
update_lazy_objects(); update_lazy_objects();
} }
} }