@@ -87,58 +87,56 @@ const isOperand = (value: InfixToken | undefined): value is InfixOperand =>
8787const isOperator = ( value : InfixToken | undefined ) : value is InfixOperator =>
8888 Array . isArray ( value )
8989
90- const appendToArray = < A > ( b : A , init : readonly A [ ] ) : readonly [ A , ...A [ ] ] =>
91- // Unfortunately TypeScript can't reason through this on its own:
92- [ ...init , b ] as readonly A [ ] as readonly [ A , ...A [ ] ]
93-
9490const infixTokensToExpression = (
9591 operation : InfixOperation ,
9692) : Molecule | Atom => {
9793 const firstToken = operation [ 0 ]
9894 if ( operation . length === 1 && isOperand ( firstToken ) ) {
9995 return firstToken
10096 } else {
101- const rightmostOperationRHS = operation [ operation . length - 1 ]
102- if ( rightmostOperationRHS === undefined ) {
97+ const leftmostOperationLHS = operation [ 0 ]
98+ if ( leftmostOperationLHS === undefined ) {
10399 throw new Error ( 'Infix operation was empty. This is a bug!' )
104100 }
105- if ( ! isOperand ( rightmostOperationRHS ) ) {
101+ if ( ! isOperand ( leftmostOperationLHS ) ) {
106102 throw new Error (
107- 'Rightmost token in infix operation was not an operand. This is a bug!' ,
103+ 'Leftmost token in infix operation was not an operand. This is a bug!' ,
108104 )
109105 }
110- const rightmostOperator = operation [ operation . length - 2 ]
111- if ( ! isOperator ( rightmostOperator ) ) {
106+
107+ const leftmostOperator = operation [ 1 ]
108+ if ( ! isOperator ( leftmostOperator ) ) {
112109 throw new Error (
113- 'Could not find rightmost operator in infix operation. This is a bug!' ,
110+ 'Could not find leftmost operator in infix operation. This is a bug!' ,
114111 )
115112 }
116113
117- const rightmostOperationLHS = operation [ operation . length - 3 ]
118- if ( ! isOperand ( rightmostOperationLHS ) ) {
114+ const leftmostOperationRHS = operation [ 2 ]
115+ if ( ! isOperand ( leftmostOperationRHS ) ) {
119116 throw new Error (
120- 'Missing left -hand side of infix operation. This is a bug!' ,
117+ 'Missing right -hand side of infix operation. This is a bug!' ,
121118 )
122119 }
123120
124- const rightmostFunction = trailingIndexesAndArgumentsToExpression (
125- { 0 : '@lookup' , key : rightmostOperator [ 0 ] } ,
126- rightmostOperator [ 1 ] ,
121+ const leftmostFunction = trailingIndexesAndArgumentsToExpression (
122+ { 0 : '@lookup' , key : leftmostOperator [ 0 ] } ,
123+ leftmostOperator [ 1 ] ,
127124 )
128125
129- const reducedRightmostOperation : Molecule = {
126+ const reducedLeftmostOperation : Molecule = {
130127 0 : '@apply' ,
131128 function : {
132129 0 : '@apply' ,
133- function : rightmostFunction ,
134- argument : rightmostOperationRHS ,
130+ function : leftmostFunction ,
131+ argument : leftmostOperationRHS ,
135132 } ,
136- argument : rightmostOperationLHS ,
133+ argument : leftmostOperationLHS ,
137134 }
138135
139- return infixTokensToExpression (
140- appendToArray ( reducedRightmostOperation , operation . slice ( 0 , - 3 ) ) ,
141- )
136+ return infixTokensToExpression ( [
137+ reducedLeftmostOperation ,
138+ ...operation . slice ( 3 ) ,
139+ ] )
142140 }
143141}
144142
0 commit comments