Skip to content

Commit

Permalink
Merge pull request #109 from Eclalang/issue-#99-refactor
Browse files Browse the repository at this point in the history
refactored the eclatype to go convertion
  • Loading branch information
tot0p authored Dec 13, 2023
2 parents b0ce9eb + 1d244ac commit 378492f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions interpreter/libs/utils/EclaTypeToGo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"github.com/Eclalang/Ecla/interpreter/eclaType"
"reflect"
)

// EclaTypeToGo converts an eclaType to a go type.
Expand All @@ -18,17 +19,24 @@ func EclaTypeToGo(arg eclaType.Type) any {
case eclaType.Char:
return rune(arg.(eclaType.Char))
case *eclaType.List:
var types []any
for _, val := range arg.(*eclaType.List).Value {
types = append(types, EclaTypeToGo(val))
// TODO : base the type of the array on the type of the ecla list using eclaType.List.GetType()
arrType := reflect.SliceOf(reflect.TypeOf(EclaTypeToGo(arg.(*eclaType.List).Value[0])))
arr := reflect.MakeSlice(arrType, len(arg.(*eclaType.List).Value), len(arg.(*eclaType.List).Value))
for i, val := range arg.(*eclaType.List).Value {
arr.Index(i).Set(reflect.ValueOf(EclaTypeToGo(val)))
}
return types
return arr.Interface()
case *eclaType.Map:
var types = make(map[any]any)
for i := 0; i < len(arg.(*eclaType.Map).Keys); i++ {
types[EclaTypeToGo(arg.(*eclaType.Map).Keys[i])] = EclaTypeToGo(arg.(*eclaType.Map).Values[i])
}
return types
mapType := reflect.MapOf(reflect.TypeOf(types), reflect.TypeOf(types))
mapVal := reflect.MakeMap(mapType)
for k, v := range types {
mapVal.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
return mapVal.Interface()
default:
return nil
}
Expand Down

0 comments on commit 378492f

Please sign in to comment.