@@ -339,6 +339,50 @@ export function evaluateBinaryExpression(code: string, command: ExprNS.Expr, con
339339 // handleRuntimeError
340340 return { type : 'error' , message : 'Unsupported complex operation' } ;
341341 } return { type : 'complex' , value : result } ;
342+ }
343+ // bool and numeric operations
344+ else if ( ( left . type === 'bool' && ( right . type === 'number' || right . type === 'bigint' || right . type === 'bool' ) ) ||
345+ ( right . type === 'bool' && ( left . type === 'number' || left . type === 'bigint' || left . type === 'bool' ) ) ) {
346+
347+ const leftNum = left . type === 'bool' ? ( left . value ? 1 : 0 ) : Number ( left . value ) ;
348+ const rightNum = right . type === 'bool' ? ( right . value ? 1 : 0 ) : Number ( right . value ) ;
349+ let result : number | boolean ;
350+
351+ // Arithmetic
352+ if ( typeof operator === 'string' ) {
353+ if ( operator === '__py_adder' ) result = leftNum + rightNum ;
354+ else if ( operator === '__py_minuser' ) result = leftNum - rightNum ;
355+ else if ( operator === '__py_multiplier' ) result = leftNum * rightNum ;
356+ else if ( operator === '__py_divider' ) {
357+ if ( rightNum === 0 ) {
358+ // handleRuntimeError(context, new ZeroDivisionError(code, command, context));
359+ return { type : 'error' , message : 'Division by zero' } ;
360+ }
361+ result = leftNum / rightNum ;
362+ }
363+ else if ( operator === '__py_powerer' ) result = leftNum ** rightNum ;
364+ else if ( operator === '__py_modder' ) result = pythonMod ( leftNum , rightNum ) ;
365+ else {
366+ // handleRuntimeError(context, new UnsupportedOperandTypeError(code, command, originalLeftType, originalRightType, operand));
367+ return { type : 'error' , message : 'Unsupported boolean/numeric operation' } ;
368+ }
369+ const resultType = ( left . type === 'number' || right . type === 'number' ) ? 'number' : 'bigint' ;
370+ return { type : resultType , value : resultType === 'bigint' ? BigInt ( result ) : result } ;
371+ }
372+ // Comparisons
373+ else {
374+ if ( operator === TokenType . GREATER ) result = leftNum > rightNum ;
375+ else if ( operator === TokenType . GREATEREQUAL ) result = leftNum >= rightNum ;
376+ else if ( operator === TokenType . LESS ) result = leftNum < rightNum ;
377+ else if ( operator === TokenType . LESSEQUAL ) result = leftNum <= rightNum ;
378+ else if ( operator === TokenType . DOUBLEEQUAL ) result = leftNum === rightNum ;
379+ else if ( operator === TokenType . NOTEQUAL ) result = leftNum !== rightNum ;
380+ else {
381+ // handleRuntimeError(context, new UnsupportedOperandTypeError(code, command, originalLeftType, originalRightType, operand));
382+ return { type : 'error' , message : 'Unsupported boolean/numeric comparison' } ;
383+ }
384+ return { type : 'bool' , value : result } ;
385+ }
342386 }
343387 // Float and or Int Operations
344388 else if ( ( left . type === 'number' || left . type === 'bigint' ) && ( right . type === 'number' || right . type === 'bigint' ) ) {
@@ -360,10 +404,15 @@ export function evaluateBinaryExpression(code: string, command: ExprNS.Expr, con
360404 result = leftFloat / rightFloat ;
361405 }
362406 else if ( operator === '__py_powerer' ) result = leftFloat ** rightFloat ;
363- else if ( operator === '__py_modder' ) result = pythonMod ( leftFloat , rightFloat ) ;
364- else {
407+ else if ( operator === '__py_modder' ) {
408+ if ( rightFloat === 0 ) {
409+ // handleRuntimeError(context, new UnsupportedOperandTypeError(code, command, originalLeftType, originalRightType, operand));
410+ return { type : 'error' , message : 'Division by zero' } ;
411+ }
412+ result = pythonMod ( leftFloat , rightFloat ) ;
413+ } else {
365414 // handleRuntimeError(context, new UnsupportedOperandTypeError(code, command, originalLeftType, originalRightType, operand));
366- return { type : 'error' , message : 'Unsupported float operation ' } ;
415+ return { type : 'error' , message : 'Unsupported float comparison ' } ;
367416 }
368417 return { type : 'number' , value : result } ;
369418 }
@@ -381,10 +430,10 @@ export function evaluateBinaryExpression(code: string, command: ExprNS.Expr, con
381430 return { type : 'error' , message : 'Unsupported float comparison' } ;
382431 }
383432 return { type : 'bool' , value : result } ;
384- }
433+ }
385434 }
386435 // Same type Integer Operations
387- else {
436+ else if ( left . type === 'bigint' && right . type === 'bigint' ) {
388437 const leftBigInt = left . value as bigint ;
389438 const rightBigInt = right . value as bigint ;
390439 let result : bigint | boolean ;
@@ -404,13 +453,17 @@ export function evaluateBinaryExpression(code: string, command: ExprNS.Expr, con
404453 } else if ( operator === '__py_powerer' ) {
405454 if ( leftBigInt === 0n && rightBigInt < 0 ) {
406455 // handleRunTimeError, zerodivision error
407- return { type : 'error' , message : '0.0 cannot be raised to a negative pwoer ' }
456+ return { type : 'error' , message : '0.0 cannot be raised to a negative power ' }
408457 }
409458 if ( rightBigInt < 0 ) {
410459 return { type : 'number' , value : Number ( leftBigInt ) ** Number ( rightBigInt ) } ;
411460 }
412461 return { type : 'bigint' , value : leftBigInt ** rightBigInt } ;
413462 } else if ( operator === '__py_modder' ) {
463+ if ( rightBigInt === 0n ) {
464+ // handleRunTimeError - ZeroDivisionError
465+ return { type : 'error' , message : 'integer modulo by zero' } ;
466+ }
414467 return { type : 'bigint' , value : pythonMod ( leftBigInt , rightBigInt ) } ;
415468 } else if ( operator === TokenType . GREATER ) {
416469 return { type : 'bool' , value : leftBigInt > rightBigInt } ;
@@ -424,10 +477,9 @@ export function evaluateBinaryExpression(code: string, command: ExprNS.Expr, con
424477 return { type : 'bool' , value : leftBigInt === rightBigInt } ;
425478 } else if ( operator === TokenType . NOTEQUAL ) {
426479 return { type : 'bool' , value : leftBigInt !== rightBigInt } ;
427- } else {
428- // handleRuntimeError
429- return { type : 'error' , message : 'Unsupported integer operation' } ;
480+ }
481+ // handleRuntimeError
482+ return { type : 'error' , message : 'Unsupported operation' } ;
430483 }
431484 }
432- }
433- }
485+ }
0 commit comments