22# Provides extended to_julia functionality with vector/complex/fraction support
33#
44# Part of feature 029-output-handling, 030-to-julia-bool-conversion
5+ # Updated for 041-scoped-type-enum: Uses GenTypes.T enum instead of GIAC_* constants
6+
7+ using . GenTypes: T, INT, DOUBLE, ZINT, REAL, CPLX, VECT, SYMB, IDNT, FRAC, STRNG, FUNC
58
69# ============================================================================
710# Extended to_julia Conversion
@@ -16,14 +19,14 @@ Recursively convert a GIAC expression to native Julia types.
1619| GIAC Type | Julia Return Type |
1720|-----------|-------------------|
1821| Boolean (`true`/`false`) | `Bool` |
19- | `GIAC_INT ` | `Int64` |
20- | `GIAC_ZINT ` | `BigInt` |
21- | `GIAC_DOUBLE `, `GIAC_REAL ` | `Float64` |
22- | `GIAC_FRAC ` | `Rational{Int64}` or `Rational{BigInt}` |
23- | `GIAC_CPLX ` | `Complex{T}` (T promoted from parts) |
24- | `GIAC_VECT ` | `Vector{T}` (T narrowed from elements) |
25- | `GIAC_STRNG ` | `String` |
26- | `GIAC_SYMB `, `GIAC_IDNT `, `GIAC_FUNC ` | `GiacExpr` (unchanged) |
22+ | `INT ` | `Int64` |
23+ | `ZINT ` | `BigInt` |
24+ | `DOUBLE `, `REAL ` | `Float64` |
25+ | `FRAC ` | `Rational{Int64}` or `Rational{BigInt}` |
26+ | `CPLX ` | `Complex{T}` (T promoted from parts) |
27+ | `VECT ` | `Vector{T}` (T narrowed from elements) |
28+ | `STRNG ` | `String` |
29+ | `SYMB `, `IDNT `, `FUNC ` | `GiacExpr` (unchanged) |
2730
2831Note: GIAC represents booleans as integers internally, but `to_julia` detects them
2932via their string representation ("true"/"false") and returns Julia `Bool` values.
@@ -69,27 +72,27 @@ function to_julia(g::GiacExpr)
6972end
7073
7174# Internal dispatcher based on type constant
72- function _convert_by_type (g:: GiacExpr , t:: Int32 )
73- if t == GIAC_INT
75+ function _convert_by_type (g:: GiacExpr , t:: T )
76+ if t == INT
7477 # Check for boolean before integer conversion (030-to-julia-bool-conversion)
7578 if is_boolean (g)
7679 return _convert_to_bool (g)
7780 end
7881 return _convert_to_int64 (g)
79- elseif t == GIAC_DOUBLE || t == GIAC_REAL
82+ elseif t == DOUBLE || t == REAL
8083 return _convert_to_float64 (g)
81- elseif t == GIAC_ZINT
84+ elseif t == ZINT
8285 return _convert_to_bigint (g)
83- elseif t == GIAC_FRAC
86+ elseif t == FRAC
8487 return _convert_to_rational (g)
85- elseif t == GIAC_CPLX
88+ elseif t == CPLX
8689 return _convert_to_complex (g)
87- elseif t == GIAC_VECT
90+ elseif t == VECT
8891 return _convert_to_vector (g)
89- elseif t == GIAC_STRNG
92+ elseif t == STRNG
9093 return _convert_to_string (g)
9194 else
92- # Symbolic types (GIAC_SYMB, GIAC_IDNT, GIAC_FUNC ) - return unchanged
95+ # Symbolic types (SYMB, IDNT, FUNC ) - return unchanged
9396 return g
9497 end
9598end
@@ -143,7 +146,7 @@ function _convert_to_rational(g::GiacExpr)
143146 den_type = giac_type (den_expr)
144147
145148 # Check if either is BigInt (ZINT)
146- if num_type == GIAC_ZINT || den_type == GIAC_ZINT
149+ if num_type == ZINT || den_type == ZINT
147150 num = to_julia (num_expr):: Union{Int64, BigInt}
148151 den = to_julia (den_expr):: Union{Int64, BigInt}
149152 return Rational {BigInt} (BigInt (num), BigInt (den))
@@ -210,7 +213,7 @@ Check if a GiacExpr can be fully converted to native Julia types
210213"""
211214function _can_convert_fully (g:: GiacExpr ):: Bool
212215 t = giac_type (g)
213- if t == GIAC_VECT
216+ if t == VECT
214217 # For vectors, recursively check all elements
215218 n = _vector_length (g)
216219 for i in 1 : n
@@ -220,11 +223,11 @@ function _can_convert_fully(g::GiacExpr)::Bool
220223 end
221224 end
222225 return true
223- elseif t in (GIAC_INT, GIAC_DOUBLE, GIAC_REAL, GIAC_ZINT, GIAC_FRAC, GIAC_CPLX, GIAC_STRNG )
226+ elseif t in (INT, DOUBLE, REAL, ZINT, FRAC, CPLX, STRNG )
224227 # Numeric and string types can be fully converted
225228 return true
226229 else
227- # GIAC_SYMB, GIAC_IDNT, GIAC_FUNC - symbolic, cannot fully convert
230+ # SYMB, IDNT, FUNC - symbolic, cannot fully convert
228231 return false
229232 end
230233end
@@ -302,9 +305,9 @@ Convert an integer GiacExpr to Int64.
302305"""
303306function Base. convert (:: Type{Int64} , g:: GiacExpr ):: Int64
304307 t = giac_type (g)
305- if t == GIAC_INT
308+ if t == INT
306309 return _convert_to_int64 (g)
307- elseif t == GIAC_ZINT
310+ elseif t == ZINT
308311 big = _convert_to_bigint (g)
309312 if big > typemax (Int64) || big < typemin (Int64)
310313 throw (InexactError (:convert , Int64, big))
@@ -322,13 +325,13 @@ Convert a numeric GiacExpr to Float64.
322325"""
323326function Base. convert (:: Type{Float64} , g:: GiacExpr ):: Float64
324327 t = giac_type (g)
325- if t == GIAC_DOUBLE || t == GIAC_REAL
328+ if t == DOUBLE || t == REAL
326329 return _convert_to_float64 (g)
327- elseif t == GIAC_INT
330+ elseif t == INT
328331 return Float64 (_convert_to_int64 (g))
329- elseif t == GIAC_ZINT
332+ elseif t == ZINT
330333 return Float64 (_convert_to_bigint (g))
331- elseif t == GIAC_FRAC
334+ elseif t == FRAC
332335 r = _convert_to_rational (g)
333336 return Float64 (r)
334337 else
@@ -355,11 +358,11 @@ Convert a fraction or integer GiacExpr to a Rational.
355358"""
356359function Base. convert (:: Type{Rational} , g:: GiacExpr ):: Rational
357360 t = giac_type (g)
358- if t == GIAC_FRAC
361+ if t == FRAC
359362 return _convert_to_rational (g)
360- elseif t == GIAC_INT
363+ elseif t == INT
361364 return Rational (_convert_to_int64 (g))
362- elseif t == GIAC_ZINT
365+ elseif t == ZINT
363366 return Rational (_convert_to_bigint (g))
364367 else
365368 throw (MethodError (convert, (Rational, g)))
@@ -373,7 +376,7 @@ Convert a complex GiacExpr to a Julia Complex.
373376"""
374377function Base. convert (:: Type{Complex} , g:: GiacExpr ):: Complex
375378 t = giac_type (g)
376- if t == GIAC_CPLX
379+ if t == CPLX
377380 return _convert_to_complex (g)
378381 elseif is_numeric (g)
379382 # Numeric but not complex - treat as real
@@ -415,7 +418,7 @@ function Base.convert(::Type{Bool}, g::GiacExpr)::Bool
415418
416419 # Allow integer 0/1 to convert to Bool (standard Julia behavior)
417420 t = giac_type (g)
418- if t == GIAC_INT
421+ if t == INT
419422 val = _convert_to_int64 (g)
420423 if val == 0
421424 return false
0 commit comments