import { SyntaxNode } from '@lezer/common' ; import { AggregateExpr , BinaryExpr , Expr , FunctionCall , MatrixSelector , NumberLiteral , OffsetExpr , ParenExpr , StepInvariantExpr , StringLiteral , SubqueryExpr , UnaryExpr , VectorSelector , } from '../grammar/parser.terms' ; import { walkThrough } from './path-finder' ; import { getFunction , ValueType } from '../types' ; export function getType ( node ) { if ( ! node ) { return ValueType . none ; } switch ( node . type . id ) { case Expr : return getType ( node . firstChild ) ; case AggregateExpr : return ValueType . vector ; case VectorSelector : return ValueType . vector ; case OffsetExpr : return getType ( node . firstChild ) ; case StringLiteral : return ValueType . string ; case NumberLiteral : return ValueType . scalar ; case MatrixSelector : return ValueType . matrix ; case SubqueryExpr : return ValueType . matrix ; case ParenExpr : return getType ( walkThrough ( node , Expr ) ) ; case UnaryExpr : return getType ( walkThrough ( node , Expr ) ) ; case BinaryExpr : const lt = getType ( node . firstChild ) ; const rt = getType ( node . lastChild ) ; if ( lt === ValueType . scalar && rt === ValueType . scalar ) { return ValueType . scalar ; } return ValueType . vector ; case FunctionCall : const funcNode = node . firstChild ?. firstChild ; if ( ! funcNode ) { return ValueType . none ; } return getFunction ( funcNode . type . id ) . returnType ; case StepInvariantExpr : return getType ( walkThrough ( node , Expr ) ) ; default : return ValueType . none ; } }