Lengths and angles should be set with |abs| values (#93)

* Lengths and angles should be set with |abs| values

* clean up
This commit is contained in:
Kurt Hutten
2023-04-02 17:20:11 +10:00
committed by GitHub
parent b279daa8e0
commit 01bf3c1049
13 changed files with 159 additions and 55 deletions

View File

@ -1,7 +1,16 @@
import { useEffect, useState, useRef } from 'react'
import { abstractSyntaxTree, Value } from '../lang/abstractSyntaxTree'
import {
abstractSyntaxTree,
BinaryPart,
Value,
} from '../lang/abstractSyntaxTree'
import { executor } from '../lang/executor'
import { findUniqueName } from '../lang/modifyAst'
import {
createIdentifier,
createLiteral,
createUnaryExpression,
findUniqueName,
} from '../lang/modifyAst'
import { findAllPreviousVariables, PrevVariable } from '../lang/queryAst'
import { lexer } from '../lang/tokeniser'
import { useStore } from '../useStore'
@ -238,3 +247,32 @@ export const CreateNewVariable = ({
</>
)
}
export function removeDoubleNegatives(
valueNode: BinaryPart,
sign: number,
variableName?: string
): BinaryPart {
let finValue: BinaryPart = variableName
? createIdentifier(variableName)
: valueNode
if (sign === -1) finValue = createUnaryExpression(finValue)
if (
finValue.type === 'UnaryExpression' &&
finValue.operator === '-' &&
finValue.argument.type === 'UnaryExpression' &&
finValue.argument.operator === '-'
) {
finValue = finValue.argument.argument
}
if (
finValue.type === 'UnaryExpression' &&
finValue.operator === '-' &&
finValue.argument.type === 'Literal' &&
typeof finValue.argument.value === 'number' &&
finValue.argument.value < 0
) {
finValue = createLiteral(-finValue.argument.value)
}
return finValue
}