Skip to content

Commit

Permalink
Merge pull request #1 from ncave/fixast
Browse files Browse the repository at this point in the history
Removed CharNumber
  • Loading branch information
DunetsNM authored May 20, 2024
2 parents 5f8a9ed + bfc125d commit 8c28beb
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 54 deletions.
3 changes: 0 additions & 3 deletions src/Fable.AST/Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,6 @@ type NumberValue =
| Float32 of System.Single
| Float64 of System.Double
| Decimal of System.Decimal
// a workaround to do char arithmetics, see #2614 TODO: can we do better? TypeCast didn't work
| CharNumber of NumberKind * System.Char

type ValueKind =
// The AST from F# compiler is a bit inconsistent with ThisValue and BaseValue.
Expand Down Expand Up @@ -559,7 +557,6 @@ type ValueKind =
| NumberValue.Float32 _ -> NumberKind.Float32
| NumberValue.Float64 _ -> NumberKind.Float64
| NumberValue.Decimal _ -> NumberKind.Decimal
| NumberValue.CharNumber(kind, _) -> kind
|> fun kind -> Number(kind, info)
| RegexConstant _ -> Regex
| NewOption(_, t, isStruct) -> Option(t, isStruct)
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,6 @@ module Util =

let transformNumberLiteral com (r: Option<SourceLocation>) (v: Fable.NumberValue) =
match v with
| Fable.NumberValue.CharNumber(Dart.Replacements.DartInt, x) -> Expression.integerLiteral (int64 x)
| Fable.NumberValue.Int8 x -> Expression.integerLiteral (int64 x)
| Fable.NumberValue.UInt8 x -> Expression.integerLiteral (int64 x)
| Fable.NumberValue.Int16 x -> Expression.integerLiteral (int64 x)
Expand All @@ -730,6 +729,7 @@ module Util =
| Fable.NumberValue.UInt32 x -> Expression.integerLiteral (int64 x)
| Fable.NumberValue.Int64 x -> Expression.integerLiteral (x)
| Fable.NumberValue.UInt64 x -> Expression.integerLiteral (int64 x)
| Fable.NumberValue.Float16 x -> Expression.doubleLiteral (float x)
| Fable.NumberValue.Float32 x -> Expression.doubleLiteral (float x)
| Fable.NumberValue.Float64 x -> Expression.doubleLiteral (x)
| _ -> $"Numeric literal is not supported: %A{v}" |> addErrorAndReturnNull com r
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Dart/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ let rec getZero (com: ICompiler) (ctx: Context) (t: Type) =
| String -> makeStrConst "" // Using empty string instead of null so Dart doesn't complain
| Number(BigInt, _) as t -> Helper.LibCall(com, "BigInt", "fromInt32", t, [ makeIntConst 0 ])
| Number(Decimal, _) as t -> makeIntConst 0 |> makeDecimalFromExpr com None t
| Number(kind, uom) -> NumberConstant(NumberValue.ZeroOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetZero kind, uom) |> makeValue None
| Builtin(BclTimeSpan | BclTimeOnly) -> getZeroTimeSpan t
| Builtin BclDateTime as t -> Helper.LibCall(com, "Date", "minValue", t, [])
| Builtin BclDateTimeOffset as t -> Helper.LibCall(com, "DateOffset", "minValue", t, [])
Expand All @@ -567,7 +567,7 @@ let getOne (com: ICompiler) (ctx: Context) (t: Type) =
| Boolean -> makeBoolConst true
| Number(BigInt, _) as t -> Helper.LibCall(com, "BigInt", "fromInt32", t, [ makeIntConst 1 ])
| Number(Decimal, _) as t -> makeIntConst 1 |> makeDecimalFromExpr com None t
| Number(kind, uom) -> NumberConstant(NumberValue.OneOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetOne kind, uom) |> makeValue None
| ListSingleton(CustomOp com ctx None t "get_One" [] e) -> e
| _ -> makeIntConst 1

Expand Down
14 changes: 12 additions & 2 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,13 +2147,18 @@ module Util =
let fableMemberFunctionOrValue =
FsMemberFunctionOrValue(memb) :> Fable.MemberFunctionOrValue

let attributeFullNames =
fableMemberFunctionOrValue.Attributes
|> Seq.map (fun attr -> attr.Entity.FullName)
|> List.ofSeq

Fable.MemberRef(
FsEnt.Ref(ent),
{
CompiledName = memb.CompiledName
IsInstance = memb.IsInstanceMember
NonCurriedArgTypes = nonCurriedArgTypes
AttributeFullNames = fableMemberFunctionOrValue.Attributes |> Seq.map (fun attr -> attr.Entity.FullName) |> List.ofSeq
AttributeFullNames = attributeFullNames
}
)
| ent ->
Expand Down Expand Up @@ -2182,13 +2187,18 @@ module Util =
let fableMemberFunctionOrValue =
FsMemberFunctionOrValue(memb) :> Fable.MemberFunctionOrValue

let attributeFullNames =
fableMemberFunctionOrValue.Attributes
|> Seq.map (fun attr -> attr.Entity.FullName)
|> List.ofSeq

Fable.MemberRef(
FsEnt.Ref(ent),
{
CompiledName = memb.CompiledName
IsInstance = memb.IsInstanceMember
NonCurriedArgTypes = None
AttributeFullNames = fableMemberFunctionOrValue.Attributes |> Seq.map (fun attr -> attr.Entity.FullName) |> List.ofSeq
AttributeFullNames = attributeFullNames
}
)
| ent ->
Expand Down
19 changes: 9 additions & 10 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,24 +1615,23 @@ module Util =
StringTemplate(tag, List.toArray parts, values, r) |> Literal
| Fable.NumberConstant(v, _) ->
match v with
| Fable.NumberValue.Decimal x -> JS.Replacements.makeDecimal com r value.Type x |> transformAsExpr com ctx
| Fable.NumberValue.BigInt x -> Expression.bigintLiteral (string<bigint> x, ?loc = r)
| Fable.NumberValue.Int64 x -> Expression.bigintLiteral (string<int64> x, ?loc = r)
| Fable.NumberValue.UInt64 x -> Expression.bigintLiteral (string<uint64> x, ?loc = r)
// | Fable.NumberValue.Int128 x -> Expression.bigintLiteral(string x, ?loc=r)
// | Fable.NumberValue.UInt128 x -> Expression.bigintLiteral(string x, ?loc=r)
| Fable.NumberValue.NativeInt x -> Expression.bigintLiteral (string<nativeint> x, ?loc = r)
| Fable.NumberValue.UNativeInt x -> Expression.bigintLiteral (string<unativeint> x, ?loc = r)
| Fable.NumberValue.Int8 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.UInt8 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.Int16 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.UInt16 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.Int32 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.UInt32 x -> Expression.numericLiteral (float x, ?loc = r)
// | Fable.NumberValue.Float16 x -> Expression.numericLiteral(float x, ?loc=r)
| Fable.NumberValue.Int64 x -> Expression.bigintLiteral (string<int64> x, ?loc = r)
| Fable.NumberValue.UInt64 x -> Expression.bigintLiteral (string<uint64> x, ?loc = r)
// | Fable.NumberValue.Int128(u,l) -> Expression.bigintLiteral(string System.Int128(u,l), ?loc=r)
// | Fable.NumberValue.UInt128(u,l) -> Expression.bigintLiteral(string System.UInt128(u,l), ?loc=r)
| Fable.NumberValue.BigInt x -> Expression.bigintLiteral (string<bigint> x, ?loc = r)
| Fable.NumberValue.NativeInt x -> Expression.bigintLiteral (string<nativeint> x, ?loc = r)
| Fable.NumberValue.UNativeInt x -> Expression.bigintLiteral (string<unativeint> x, ?loc = r)
| Fable.NumberValue.Float16 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.Float32 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.Float64 x -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.CharNumber(_, x) -> Expression.numericLiteral (float x, ?loc = r)
| Fable.NumberValue.Decimal x -> JS.Replacements.makeDecimal com r value.Type x |> transformAsExpr com ctx
| _ -> addErrorAndReturnNull com r $"Numeric literal is not supported: %A{v}"
| Fable.RegexConstant(source, flags) -> Expression.regExpLiteral (source, flags, ?loc = r)
| Fable.NewArray(newKind, typ, kind) ->
Expand Down
24 changes: 14 additions & 10 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,6 @@ module Util =
let transformCurry (com: IPythonCompiler) (ctx: Context) expr arity : Expression * Statement list =
com.TransformAsExpr(ctx, Replacements.Api.curryExprAtRuntime com arity expr)


let makeInteger (com: IPythonCompiler) (ctx: Context) r _t intName (x: obj) =
let cons = libValue com ctx "types" intName
let value = Expression.intConstant (x, ?loc = r)
Expand All @@ -1679,7 +1678,6 @@ module Util =
let value = Expression.floatConstant (x, ?loc = r)
Expression.call (cons, [ value ], ?loc = r), []


let transformValue (com: IPythonCompiler) (ctx: Context) r value : Expression * Statement list =
match value with
| Fable.BaseValue(None, _) -> Expression.identifier "super()", []
Expand Down Expand Up @@ -1710,28 +1708,34 @@ module Util =
makeBinOp None Fable.String acc (makeStrConst part) BinaryPlus
)
|> transformAsExpr com ctx
| Fable.NumberConstant(x, _) ->
match x with
| Fable.NumberValue.Decimal x -> Py.Replacements.makeDecimal com r value.Type x |> transformAsExpr com ctx
| Fable.NumberValue.Int64 x -> makeInteger com ctx r value.Type "int64" x
| Fable.NumberValue.UInt64 x -> makeInteger com ctx r value.Type "uint64" x
| Fable.NumberConstant(v, _) ->
match v with
| Fable.NumberValue.Int8 x -> makeInteger com ctx r value.Type "int8" x
| Fable.NumberValue.UInt8 x -> makeInteger com ctx r value.Type "uint8" x
| Fable.NumberValue.Int16 x -> makeInteger com ctx r value.Type "int16" x
| Fable.NumberValue.UInt16 x -> makeInteger com ctx r value.Type "uint16" x
| Fable.NumberValue.Int32 x -> Expression.intConstant (x, ?loc = r), []
| Fable.NumberValue.UInt32 x -> makeInteger com ctx r value.Type "uint32" x
// | Fable.NumberValue.CharNumber _ -> makeNumber com ctx r value.Type "char" x
| Fable.NumberValue.Int64 x -> makeInteger com ctx r value.Type "int64" x
| Fable.NumberValue.UInt64 x -> makeInteger com ctx r value.Type "uint64" x
// | Fable.NumberValue.Int128(u,l) -> Expression.intConstant (System.Int128(u,l), ?loc = r), []
// | Fable.NumberValue.UInt128(u,l) -> Expression.intConstant (System.UInt128(u,l), ?loc = r), []
| Fable.NumberValue.BigInt x -> Expression.intConstant (x, ?loc = r), []
| Fable.NumberValue.NativeInt x -> Expression.intConstant (x, ?loc = r), []
| Fable.NumberValue.UNativeInt x -> Expression.intConstant (x, ?loc = r), []
// TODO: special consts also need attention
| Fable.NumberValue.Float64 x when x = infinity -> Expression.name "float('inf')", []
| Fable.NumberValue.Float64 x when x = -infinity -> Expression.name "float('-inf')", []
| Fable.NumberValue.Float64 x when Double.IsNaN(x) -> Expression.name "float('nan')", []
| Fable.NumberValue.Float32 x when Single.IsNaN(x) ->
libCall com ctx r "types" "float32" [ Expression.stringConstant "nan" ], []
| Fable.NumberValue.Float16 x when Single.IsNaN(x) ->
libCall com ctx r "types" "float32" [ Expression.stringConstant "nan" ], []
| Fable.NumberValue.Float16 x -> makeFloat com ctx r value.Type "float32" (float x)
| Fable.NumberValue.Float32 x -> makeFloat com ctx r value.Type "float32" (float x)
| Fable.NumberValue.Float64 x -> Expression.floatConstant (x, ?loc = r), []
// TODO: get rid of wildcard and fix, intConstant can't be catch-all, it includes Float16 and CharNumber at very least
| _ -> Expression.intConstant (x, ?loc = r), []
| Fable.NumberValue.Decimal x -> Py.Replacements.makeDecimal com r value.Type x |> transformAsExpr com ctx
| _ -> addErrorAndReturnNull com r $"Numeric literal is not supported: %A{v}", []
| Fable.NewArray(newKind, typ, kind) ->
match newKind with
| Fable.ArrayValues values -> makeArray com ctx values kind typ
Expand Down
13 changes: 4 additions & 9 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ let toInt com (ctx: Context) r targetType (args: Expr list) =
| _ -> FableError $"Unexpected non-integer type %A{typeTo}" |> raise

match sourceType, targetType with
| Char, _ ->
//Helper.InstanceCall(args.Head, "charCodeAt", targetType, [ makeIntConst 0 ])
| Char, Number(typeTo, _) ->
Helper.LibCall(com, "char", "char_code_at", targetType, [ args.Head; makeIntConst 0 ])
|> emitCast typeTo
| String, _ -> stringToInt com ctx r targetType args
| Number(BigInt, _), _ -> Helper.LibCall(com, "big_int", castBigIntMethod targetType, targetType, args)
| Number(typeFrom, _), Number(typeTo, _) ->
Expand Down Expand Up @@ -696,7 +696,7 @@ let rec getZero (com: ICompiler) ctx (t: Type) =
| Boolean -> makeBoolConst false
| Number(BigInt, _) as t -> Helper.LibCall(com, "big_int", "fromInt32", t, [ makeIntConst 0 ])
| Number(Decimal, _) as t -> makeIntConst 0 |> makeDecimalFromExpr com None t
| Number(kind, uom) -> NumberConstant(NumberValue.ZeroOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetZero kind, uom) |> makeValue None
| Char
| String -> makeStrConst "" // TODO: Use null for string?
| Builtin BclTimeSpan -> Helper.LibCall(com, "time_span", "create", t, [ makeIntConst 0 ])
Expand All @@ -710,16 +710,14 @@ let rec getZero (com: ICompiler) ctx (t: Type) =
let getOne (com: ICompiler) ctx (t: Type) =
match t with
| Boolean -> makeBoolConst true
| Number(kind, uom) -> NumberConstant(NumberValue.OneOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetOne kind, uom) |> makeValue None
| ListSingleton(CustomOp com ctx None t "get_One" [] e) -> e
| _ -> makeIntConst 1

let makeAddFunction (com: ICompiler) ctx t =
let x = makeUniqueIdent ctx t "x"
let y = makeUniqueIdent ctx t "y"

let body = applyOp com ctx None t Operators.addition [ IdentExpr x; IdentExpr y ]

Delegate([ x; y ], body, None, Tags.empty)

let makeGenericAdder (com: ICompiler) ctx t =
Expand All @@ -733,9 +731,7 @@ let makeGenericAverager (com: ICompiler) ctx t =
let divideFn =
let x = makeUniqueIdent ctx t "x"
let i = makeUniqueIdent ctx (Int32.Number) "i"

let body = applyOp com ctx None t Operators.divideByInt [ IdentExpr x; IdentExpr i ]

Delegate([ x; i ], body, None, Tags.empty)

objExpr
Expand Down Expand Up @@ -2738,7 +2734,6 @@ let dates (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr optio
| _ -> None
| meth ->
let args = ignoreFormatProvider com ctx r i.DeclaringEntityFullName meth args

let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst

Helper.LibCall(com, moduleName, meth, t, args, i.SignatureArgTypes, ?thisArg = thisArg, ?loc = r)
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Replacements.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ let toArray r t expr =

type NumberValue with

static member ZeroOfKind(kind: NumberKind) : NumberValue =
static member GetZero(kind: NumberKind) : NumberValue =
match kind with
| NumberKind.Int8 -> NumberValue.Int8(0y: int8)
| NumberKind.UInt8 -> NumberValue.UInt8(0uy: uint8)
Expand All @@ -278,7 +278,7 @@ type NumberValue with
| NumberKind.Float64 -> NumberValue.Float64(0.: float)
| NumberKind.Decimal -> NumberValue.Decimal(0M: decimal)

static member OneOfKind(kind: NumberKind) : NumberValue =
static member GetOne(kind: NumberKind) : NumberValue =
match kind with
| NumberKind.Int8 -> NumberValue.Int8(1y: int8)
| NumberKind.UInt8 -> NumberValue.UInt8(1uy: uint8)
Expand Down
14 changes: 5 additions & 9 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ let toLong com (ctx: Context) r targetType (args: Expr list) : Expr =
|> wrapLong com ctx r targetType
| _ ->
addWarning com ctx.InlinePath r "Cannot make conversion because source type is unknown"

TypeCast(args.Head, targetType)

let emitIntCast toKind arg =
Expand All @@ -321,11 +320,9 @@ let toInt com (ctx: Context) r targetType (args: Expr list) =
let sourceType = args.Head.Type

match sourceType, targetType with
| Char, _ ->
match targetType, args with
| Number(kind, _), Value(CharConstant c, r) :: _ ->
Value(NumberConstant(NumberValue.CharNumber(kind, c), NumberInfo.Empty), r)
| _ -> Helper.InstanceCall(args.Head, "charCodeAt", targetType, [ makeIntConst 0 ])
| Char, Number(toKind, _) ->
Helper.InstanceCall(args.Head, "charCodeAt", targetType, [ makeIntConst 0 ])
|> emitIntCast toKind
| String, _ -> stringToInt com ctx r targetType args
| Number(fromKind, _), Number(toKind, _) ->
if needToCast fromKind toKind then
Expand All @@ -340,7 +337,6 @@ let toInt com (ctx: Context) r targetType (args: Expr list) =
TypeCast(args.Head, targetType)
| _ ->
addWarning com ctx.InlinePath r "Cannot make conversion because source type is unknown"

TypeCast(args.Head, targetType)

let round com (args: Expr list) =
Expand Down Expand Up @@ -669,7 +665,7 @@ let rec getZero (com: ICompiler) (ctx: Context) (t: Type) =
| Boolean -> makeBoolConst false
| Char
| String -> makeStrConst "" // TODO: Use null for string?
| Number(kind, uom) -> NumberConstant(NumberValue.ZeroOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetZero kind, uom) |> makeValue None
| Builtin(BclTimeSpan | BclTimeOnly) -> makeIntConst 0 // TODO: Type cast
| Builtin BclDateTime as t -> Helper.LibCall(com, "Date", "minValue", t, [])
| Builtin BclDateTimeOffset as t -> Helper.LibCall(com, "DateOffset", "minValue", t, [])
Expand All @@ -682,7 +678,7 @@ let rec getZero (com: ICompiler) (ctx: Context) (t: Type) =
let getOne (com: ICompiler) (ctx: Context) (t: Type) =
match t with
| Boolean -> makeBoolConst true
| Number(kind, uom) -> NumberConstant(NumberValue.OneOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetOne kind, uom) |> makeValue None
| ListSingleton(CustomOp com ctx None t "get_One" [] e) -> e
| _ -> makeIntConst 1

Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ let rec getZero (com: ICompiler) (ctx: Context) (t: Type) =
| Boolean -> makeBoolConst false
| Number(BigInt, _) -> Helper.LibCall(com, "BigInt", "zero", t, [])
| Number(Decimal, _) -> Helper.LibValue(com, "Decimal", "Zero", t)
| Number(kind, uom) -> NumberConstant(NumberValue.ZeroOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetZero kind, uom) |> makeValue None
| Char -> CharConstant '\u0000' |> makeValue None
| String -> makeStrConst "" // TODO: Use null for string?
| Array(typ, _) -> makeArray typ []
Expand All @@ -632,7 +632,7 @@ let getOne (com: ICompiler) (ctx: Context) (t: Type) =
| Boolean -> makeBoolConst true
| Number(BigInt, _) -> Helper.LibCall(com, "BigInt", "one", t, [])
| Number(Decimal, _) -> Helper.LibValue(com, "Decimal", "One", t)
| Number(kind, uom) -> NumberConstant(NumberValue.OneOfKind kind, uom) |> makeValue None
| Number(kind, uom) -> NumberConstant(NumberValue.GetOne kind, uom) |> makeValue None
| ListSingleton(CustomOp com ctx None t "get_One" [] e) -> e
| _ -> makeIntConst 1

Expand Down
4 changes: 0 additions & 4 deletions src/Fable.Transforms/Transforms.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,10 +1055,6 @@ module AST =
FableError $"Unsupported Number Kind %A{kind} and value %A{x} combination"
|> raise

| _, (:? char as x) ->
// TODO: convert to correct NumberValue based on kind ?
NumberValue.Float64(float x)

| _ ->
FableError $"Unexpected Number Kind %A{kind} and value %A{value} combination"
|> raise
Expand Down
Loading

0 comments on commit 8c28beb

Please sign in to comment.