From f211f8bb9445dd6930926fe48aab6a69720dd30f Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Sun, 26 Jun 2022 15:43:17 +0200 Subject: [PATCH 1/3] fix: use string types for open --- .config/dotnet-tools.json | 4 +- src/stdlib/Ast.fs | 90 +++++++++++++++--------------- src/stdlib/Builtins.fs | 113 +++++++++++++++++--------------------- src/stdlib/Json.fs | 4 +- src/stdlib/Math.fs | 92 +++++++++++++++---------------- src/stdlib/String.fs | 2 +- 6 files changed, 145 insertions(+), 160 deletions(-) diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 7b4eb8f..14f2616 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -3,13 +3,13 @@ "isRoot": true, "tools": { "paket": { - "version": "6.2.1", + "version": "7.1.5", "commands": [ "paket" ] }, "fantomas-tool": { - "version": "4.5.10", + "version": "4.7.9", "commands": [ "fantomas" ] diff --git a/src/stdlib/Ast.fs b/src/stdlib/Ast.fs index 14e801b..4d2c91e 100644 --- a/src/stdlib/Ast.fs +++ b/src/stdlib/Ast.fs @@ -8,7 +8,7 @@ type _identifier = string type AST = - abstract foo : int + abstract foo: int type ``mod`` = inherit AST @@ -18,11 +18,11 @@ type expr = type Module = inherit ``mod`` - abstract body : stmt array + abstract body: stmt array type Expression = inherit ``mod`` - abstract body : expr + abstract body: expr type stmt = inherit AST @@ -31,74 +31,74 @@ type stmt = type FunctionDef = inherit stmt - abstract name : _identifier - abstract args : arguments - abstract body : stmt array - abstract decorator_list : expr array - abstract returns : expr option + abstract name: _identifier + abstract args: arguments + abstract body: stmt array + abstract decorator_list: expr array + abstract returns: expr option type ClassDef = inherit stmt - abstract name : _identifier - abstract bases : expr array - abstract keywords : keyword array - abstract body : stmt array - abstract decorator_list : expr array + abstract name: _identifier + abstract bases: expr array + abstract keywords: keyword array + abstract body: stmt array + abstract decorator_list: expr array type Return = inherit stmt - abstract value : expr option + abstract value: expr option type Delete = inherit stmt - abstract targets : expr array + abstract targets: expr array type Assign = inherit stmt - abstract targets : expr array - abstract value : expr + abstract targets: expr array + abstract value: expr type Import = inherit stmt - abstract names : alias array + abstract names: alias array type ImportFrom = inherit stmt - abstract ``module`` : _identifier option - abstract names : alias array - abstract level : int + abstract ``module``: _identifier option + abstract names: alias array + abstract level: int type If = inherit stmt - abstract test : expr - abstract body : stmt array - abstract orelse : stmt array + abstract test: expr + abstract body: stmt array + abstract orelse: stmt array type arguments = inherit AST - abstract posonlyargs : arg array - abstract args : arg array - abstract vararg : arg option - abstract kwonlyargs : arg array - abstract kw_defaults : expr option list - abstract kwarg : arg option - abstract defaults : expr array + abstract posonlyargs: arg array + abstract args: arg array + abstract vararg: arg option + abstract kwonlyargs: arg array + abstract kw_defaults: expr option list + abstract kwarg: arg option + abstract defaults: expr array type arg = inherit AST - abstract arg : _identifier - abstract annotation : expr option + abstract arg: _identifier + abstract annotation: expr option type keyword = inherit AST - abstract arg : _identifier option - abstract value : expr + abstract arg: _identifier option + abstract value: expr type alias = inherit AST - abstract name : _identifier - abstract asname : _identifier option + abstract name: _identifier + abstract asname: _identifier option [] @@ -108,17 +108,17 @@ type Mode = type IExports = /// Parse the source into an AST node - abstract parse : string -> AST - abstract parse : string * filename: string -> AST - abstract parse : string * filename: string * mode: Mode -> AST - abstract unparse : astObj: AST -> string - abstract walk : node: AST -> AST array + abstract parse: string -> AST + abstract parse: string * filename: string -> AST + abstract parse: string * filename: string * mode: Mode -> AST + abstract unparse: astObj: AST -> string + abstract walk: node: AST -> AST array /// Return a formatted dump of the tree in node. - abstract dump : node: AST -> string + abstract dump: node: AST -> string /// Return a formatted dump of the tree in node. - abstract dump : node: AST * annotate_fields: bool -> string + abstract dump: node: AST * annotate_fields: bool -> string /// Return a formatted dump of the tree in node. - abstract dump : node: AST * annotate_fields: bool * include_attributes: bool -> string + abstract dump: node: AST * annotate_fields: bool * include_attributes: bool -> string [] let ast: IExports = nativeOnly diff --git a/src/stdlib/Builtins.fs b/src/stdlib/Builtins.fs index 72469d3..a114a14 100644 --- a/src/stdlib/Builtins.fs +++ b/src/stdlib/Builtins.fs @@ -15,70 +15,54 @@ type TextIOWrapper = inherit IDisposable inherit TextIOBase -[] -type OpenTextModeUpdating = - | [] ReadUpdate - | [] UpdateRead - | [] ReadTextUpdate - | [] ReadUpdateText - | [] UpdateReadText - | [] TextReadUpdate - | [] TextUpdateRead - | [] UpdateTextRead - | [] WriteUpdate - | [] UpdateWrite - | [] WriteTextUpdate - | [] WriteUpdateText - | [] UpdateWriteText - | [] TextWriteUpdate - | [] TextUpdateWrite - | [] UpdateTextWrite - | [] AppendUpdate - | [] UpdateAppend - | [] AppendTextUpdate - | [] AppendUpdateText - | [] UpdateAppendText - | [] TextAppendUpdate - | [] TextUpdateAppend - | [] UpdateTextAppend - | [] CreateUpdate - | [] UpdateCreate - | [] CreateTextUpdate - | [] CreateUpdateText - | [] UpdateCreateText - | [] TextCreateUpdate - | [] TextUpdateCreate - | [] UpdateTextCreate +module OpenTextMode = + let [] ReadUpdate = "r+" + let [] UpdateRead = "+r" + let [] ReadTextUpdate = "rt+" + let [] ReadUpdateText = "r+t" + let [] UpdateReadText = "+rt" + let [] TextReadUpdate = "tr+" + let [] TextUpdateRead = "t+r" + let [] UpdateTextRead = "+tr" + let [] WriteUpdate = "w+" + let [] UpdateWrite = "+w" + let [] WriteTextUpdate = "wt+" + let [] WriteUpdateText = "w+t" + let [] UpdateWriteText = "+wt" + let [] TextWriteUpdate = "tw+" + let [] TextUpdateWrite = "t+w" + let [] UpdateTextWrite = "+tw" + let [] AppendUpdate = "a+" + let [] UpdateAppend = "+a" + let [] AppendTextUpdate = "at+" + let [] AppendUpdateText = "a+t" + let [] UpdateAppendText = "+at" + let [] TextAppendUpdate = "ta+" + let [] TextUpdateAppend = "t+a" + let [] UpdateTextAppend = "+ta" + let [] CreateUpdate = "x+" + let [] UpdateCreate = "+x" + let [] CreateTextUpdate = "xt+" + let [] CreateUpdateText = "x+t" + let [] UpdateCreateText = "+xt" + let [] TextCreateUpdate = "tx+" + let [] TextUpdateCreate = "t+x" + let [] UpdateTextCreate = "+tx" + + let [] Read = "rt" + let [] ReadText = "rt" + let [] TextRead = "tr" + + let [] Write = "w" + let [] WriteText = "wt" + let [] TextWrite = "tw" + let [] Append = "a" + let [] AppendText = "at" + let [] TextAppend = "ta" + let [] Create = "x" + let [] CreateText = "xt" + let [] TextCreate = "tx" -[] -type OpenTextModeReading = - | [] Read - | [] ReadText - | [] TextRead - -[] -type OpenTextModeWriting = - | [] Write - | [] WriteText - | [] TextWrite - | [] Append - | [] AppendText - | [] TextAppend - | [] Create - | [] CreateText - | [] TextCreate - -[] -type OpenTextMode = - | OpenTextModeUpdating - | OpenTextModeWriting - | OpenTextModeReading - - -[] -type _OpenFile = - | StringPath of string - | FileDescriptor of int type _Opener = Tuple -> int @@ -120,7 +104,8 @@ type IExports = abstract float : obj -> float abstract print : obj: obj -> unit - [] abstract ``open`` : file: _OpenFile * ?mode: OpenTextMode * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper + [] abstract ``open`` : file: int * ?mode: string * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper + [] abstract ``open`` : file: string * ?mode: string * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper [] let builtins: IExports = nativeOnly diff --git a/src/stdlib/Json.fs b/src/stdlib/Json.fs index ee2003d..f6d3192 100644 --- a/src/stdlib/Json.fs +++ b/src/stdlib/Json.fs @@ -5,8 +5,8 @@ open Fable.Core // fsharplint:disable MemberNames type IExports = - abstract dumps : obj: obj -> string - abstract loads : string -> obj + abstract dumps: obj: obj -> string + abstract loads: string -> obj [] let json: IExports = nativeOnly diff --git a/src/stdlib/Math.fs b/src/stdlib/Math.fs index 6483298..f75db3a 100644 --- a/src/stdlib/Math.fs +++ b/src/stdlib/Math.fs @@ -1,46 +1,46 @@ -module Fable.Python.Math - -open System -open Fable.Core - -// fsharplint:disable MemberNames - -type IExports = - abstract ceil : int -> int - abstract ceil : float -> int - abstract comb : int -> int -> int - abstract copysign : float -> int -> float - abstract fabs : float -> float - abstract factorial : float -> float - abstract floor : int -> int - abstract floor : float -> int - abstract fmod : int -> int -> int - - abstract gcd : [] ints: int[] -> int - abstract isfinite : float -> bool - abstract isfinite : int -> bool - abstract isinf : float -> bool - abstract isinf : int -> bool - abstract isnan : float -> bool - abstract isnan : int -> bool - abstract lcm : [] ints: int[] -> int - - abstract exp : float -> float - abstract expm1 : float -> float - abstract log : float -> float - abstract log1p : float -> float - abstract log2 : float -> float - abstract log10 : float -> float - abstract pow : float -> float -> float - - abstract acos : float -> float - abstract asin : float -> float - abstract atan : float -> float - abstract atan2 : float -> float -> float - abstract cos : float -> float - abstract dist : float -> float -> float - abstract sin : float -> float - abstract tan : float -> float - -[] -let math: IExports = nativeOnly \ No newline at end of file +module Fable.Python.Math + +open System +open Fable.Core + +// fsharplint:disable MemberNames + +type IExports = + abstract ceil: int -> int + abstract ceil: float -> int + abstract comb: int -> int -> int + abstract copysign: float -> int -> float + abstract fabs: float -> float + abstract factorial: float -> float + abstract floor: int -> int + abstract floor: float -> int + abstract fmod: int -> int -> int + + abstract gcd: [] ints: int [] -> int + abstract isfinite: float -> bool + abstract isfinite: int -> bool + abstract isinf: float -> bool + abstract isinf: int -> bool + abstract isnan: float -> bool + abstract isnan: int -> bool + abstract lcm: [] ints: int [] -> int + + abstract exp: float -> float + abstract expm1: float -> float + abstract log: float -> float + abstract log1p: float -> float + abstract log2: float -> float + abstract log10: float -> float + abstract pow: float -> float -> float + + abstract acos: float -> float + abstract asin: float -> float + abstract atan: float -> float + abstract atan2: float -> float -> float + abstract cos: float -> float + abstract dist: float -> float -> float + abstract sin: float -> float + abstract tan: float -> float + +[] +let math: IExports = nativeOnly diff --git a/src/stdlib/String.fs b/src/stdlib/String.fs index 89b324a..31ec4c5 100644 --- a/src/stdlib/String.fs +++ b/src/stdlib/String.fs @@ -7,4 +7,4 @@ open Fable.Core type System.String with [] - member self.format([] args: Object[]) = nativeOnly + member self.format([] args: Object []) = nativeOnly From 334e80089c081bda25633f83dae037fc6c8fe6f5 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Sun, 26 Jun 2022 16:16:46 +0200 Subject: [PATCH 2/3] feat: add write --- src/stdlib/Builtins.fs | 248 ++++++++++++++++++++++++++++++----------- src/stdlib/Html.fs | 28 ++--- src/stdlib/Sys.fs | 8 +- test/TestBuiltins.fs | 5 + 4 files changed, 206 insertions(+), 83 deletions(-) diff --git a/src/stdlib/Builtins.fs b/src/stdlib/Builtins.fs index a114a14..f762c8e 100644 --- a/src/stdlib/Builtins.fs +++ b/src/stdlib/Builtins.fs @@ -8,104 +8,222 @@ open Fable.Core // fsharplint:disable MemberNames,InterfaceNames type TextIOBase = - abstract read : unit -> string - abstract read : __size: int -> string + abstract read: unit -> string + abstract read: __size: int -> string + abstract write: __s: string -> int + abstract writelines: __lines: string seq -> unit + abstract readline: __size: int -> string + abstract readlines: __hint: int -> string list + abstract tell: unit -> int type TextIOWrapper = inherit IDisposable inherit TextIOBase module OpenTextMode = - let [] ReadUpdate = "r+" - let [] UpdateRead = "+r" - let [] ReadTextUpdate = "rt+" - let [] ReadUpdateText = "r+t" - let [] UpdateReadText = "+rt" - let [] TextReadUpdate = "tr+" - let [] TextUpdateRead = "t+r" - let [] UpdateTextRead = "+tr" - let [] WriteUpdate = "w+" - let [] UpdateWrite = "+w" - let [] WriteTextUpdate = "wt+" - let [] WriteUpdateText = "w+t" - let [] UpdateWriteText = "+wt" - let [] TextWriteUpdate = "tw+" - let [] TextUpdateWrite = "t+w" - let [] UpdateTextWrite = "+tw" - let [] AppendUpdate = "a+" - let [] UpdateAppend = "+a" - let [] AppendTextUpdate = "at+" - let [] AppendUpdateText = "a+t" - let [] UpdateAppendText = "+at" - let [] TextAppendUpdate = "ta+" - let [] TextUpdateAppend = "t+a" - let [] UpdateTextAppend = "+ta" - let [] CreateUpdate = "x+" - let [] UpdateCreate = "+x" - let [] CreateTextUpdate = "xt+" - let [] CreateUpdateText = "x+t" - let [] UpdateCreateText = "+xt" - let [] TextCreateUpdate = "tx+" - let [] TextUpdateCreate = "t+x" - let [] UpdateTextCreate = "+tx" - - let [] Read = "rt" - let [] ReadText = "rt" - let [] TextRead = "tr" - - let [] Write = "w" - let [] WriteText = "wt" - let [] TextWrite = "tw" - let [] Append = "a" - let [] AppendText = "at" - let [] TextAppend = "ta" - let [] Create = "x" - let [] CreateText = "xt" - let [] TextCreate = "tx" + [] + let ReadUpdate = "r+" + + [] + let UpdateRead = "+r" + + [] + let ReadTextUpdate = "rt+" + + [] + let ReadUpdateText = "r+t" + + [] + let UpdateReadText = "+rt" + + [] + let TextReadUpdate = "tr+" + + [] + let TextUpdateRead = "t+r" + + [] + let UpdateTextRead = "+tr" + + [] + let WriteUpdate = "w+" + + [] + let UpdateWrite = "+w" + + [] + let WriteTextUpdate = "wt+" + + [] + let WriteUpdateText = "w+t" + + [] + let UpdateWriteText = "+wt" + + [] + let TextWriteUpdate = "tw+" + + [] + let TextUpdateWrite = "t+w" + + [] + let UpdateTextWrite = "+tw" + + [] + let AppendUpdate = "a+" + + [] + let UpdateAppend = "+a" + + [] + let AppendTextUpdate = "at+" + + [] + let AppendUpdateText = "a+t" + + [] + let UpdateAppendText = "+at" + + [] + let TextAppendUpdate = "ta+" + + [] + let TextUpdateAppend = "t+a" + + [] + let UpdateTextAppend = "+ta" + + [] + let CreateUpdate = "x+" + + [] + let UpdateCreate = "+x" + + [] + let CreateTextUpdate = "xt+" + + [] + let CreateUpdateText = "x+t" + + [] + let UpdateCreateText = "+xt" + + [] + let TextCreateUpdate = "tx+" + + [] + let TextUpdateCreate = "t+x" + + [] + let UpdateTextCreate = "+tx" + + [] + let Read = "rt" + + [] + let ReadText = "rt" + + [] + let TextRead = "tr" + + [] + let Write = "w" + + [] + let WriteText = "wt" + + [] + let TextWrite = "tw" + + [] + let Append = "a" + + [] + let AppendText = "at" + + [] + let TextAppend = "ta" + + [] + let Create = "x" + + [] + let CreateText = "xt" + + [] + let TextCreate = "tx" type _Opener = Tuple -> int type IExports = /// Return the absolute value of the argument. - abstract abs : int -> int + abstract abs: int -> int /// Return the absolute value of the argument. - abstract abs : float -> float + abstract abs: float -> float /// Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. - abstract chr : int -> char + abstract chr: int -> char /// Return the names in the current scope. - abstract dir : unit -> string list + abstract dir: unit -> string list + /// Return an alphabetized list of names comprising (some of) the /// attributes of the given object, and of attributes reachable from /// it - abstract dir : obj -> string list + abstract dir: obj -> string list + /// Return the identity of an object. - abstract id : obj -> int + abstract id: obj -> int + ///Return the length (the number of items) of an object. The argument may ///be a sequence (such as a string, bytes, tuple, list, or range) or a ///collection (such as a dictionary, set, or frozen set). - abstract len : obj -> int + abstract len: obj -> int + /// Make an iterator that computes the function using arguments from /// the iterable. Stops when iterable is exhausted. - abstract map : ('T1 -> 'T2) * IEnumerable<'T1> -> IEnumerable<'T2> + abstract map: ('T1 -> 'T2) * IEnumerable<'T1> -> IEnumerable<'T2> + /// Make an iterator that computes the function using arguments from each /// of the iterables. Stops when the shortest iterable is exhausted. - abstract map : ('T1 * 'T2 -> 'T3) * IEnumerable<'T1> * IEnumerable<'T2> -> IEnumerable<'T3> + abstract map: ('T1 * 'T2 -> 'T3) * IEnumerable<'T1> * IEnumerable<'T2> -> IEnumerable<'T3> + /// Make an iterator that computes the function using arguments from each /// of the iterables. Stops when the shortest iterable is exhausted. - abstract map : ('T1 * 'T2 * 'T3 -> 'T4) * IEnumerable<'T1> * IEnumerable<'T2> * IEnumerable<'T3> -> IEnumerable<'T4> + abstract map: ('T1 * 'T2 * 'T3 -> 'T4) * IEnumerable<'T1> * IEnumerable<'T2> * IEnumerable<'T3> -> IEnumerable<'T4> + /// Return the Unicode code point for a one-character string. - abstract ord : char -> int + abstract ord: char -> int /// Object to string - abstract str : obj -> string + abstract str: obj -> string /// Object to int - abstract int : obj -> int + abstract int: obj -> int /// Object to float - abstract float : obj -> float - abstract print : obj: obj -> unit + abstract float: obj -> float + abstract print: obj: obj -> unit + + [] + abstract ``open``: + file: int * + ?mode: string * + ?buffering: int * + ?encoding: string * + ?errors: string * + ?newline: string * + ?closefd: bool * + ?opener: _Opener -> + TextIOWrapper - [] abstract ``open`` : file: int * ?mode: string * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper - [] abstract ``open`` : file: string * ?mode: string * ?buffering: int * ?encoding: string * ?errors: string * ?newline: string * ?closefd: bool * ?opener: _Opener -> TextIOWrapper + [] + abstract ``open``: + file: string * + ?mode: string * + ?buffering: int * + ?encoding: string * + ?errors: string * + ?newline: string * + ?closefd: bool * + ?opener: _Opener -> + TextIOWrapper [] let builtins: IExports = nativeOnly diff --git a/src/stdlib/Html.fs b/src/stdlib/Html.fs index 9d34cb9..f78bbe4 100644 --- a/src/stdlib/Html.fs +++ b/src/stdlib/Html.fs @@ -1,14 +1,14 @@ -module Fable.Python.Html - -open Fable.Core - -// fsharplint:disable MemberNames - -type IExports = - abstract escape : string -> string - abstract escape : string * bool -> string - abstract unescape : string -> string - - /// Miscellaneous operating system interfaces -[] -let html: IExports = nativeOnly +module Fable.Python.Html + +open Fable.Core + +// fsharplint:disable MemberNames + +type IExports = + abstract escape: string -> string + abstract escape: string * bool -> string + abstract unescape: string -> string + +/// Miscellaneous operating system interfaces +[] +let html: IExports = nativeOnly diff --git a/src/stdlib/Sys.fs b/src/stdlib/Sys.fs index 36c16cb..259bdb8 100644 --- a/src/stdlib/Sys.fs +++ b/src/stdlib/Sys.fs @@ -10,20 +10,20 @@ type ByteOrder = | Big type VersionInfo = - abstract major : int + abstract major: int abstract minor: int abstract micro: int abstract releaselevel: string abstract serial: int type IExports = - abstract argv : string array + abstract argv: string array abstract byteorder: ByteOrder abstract hexversion: int abstract maxsize: int abstract maxunicode: int - abstract path : string array - abstract platform : string + abstract path: string array + abstract platform: string abstract prefix: string abstract version: string abstract version_info: VersionInfo diff --git a/test/TestBuiltins.fs b/test/TestBuiltins.fs index f0988e7..5fb195c 100644 --- a/test/TestBuiltins.fs +++ b/test/TestBuiltins.fs @@ -9,3 +9,8 @@ let ``test print works`` () = result |> equal () let ``test __name__ works`` () = __name__ |> equal "test_builtins" + +[] +let ``test write works`` () = + let result = builtins.``open``("test.txt", OpenTextMode.Write) + result.write "ABC" |> equal () \ No newline at end of file From a6341684ac8bb3f1b244f448c22e1fe6f208fbc0 Mon Sep 17 00:00:00 2001 From: Dag Brattli Date: Sun, 26 Jun 2022 16:25:01 +0200 Subject: [PATCH 3/3] fix: handle return type correctly --- test/TestBuiltins.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/TestBuiltins.fs b/test/TestBuiltins.fs index 5fb195c..d06dfbd 100644 --- a/test/TestBuiltins.fs +++ b/test/TestBuiltins.fs @@ -13,4 +13,4 @@ let ``test __name__ works`` () = __name__ |> equal "test_builtins" [] let ``test write works`` () = let result = builtins.``open``("test.txt", OpenTextMode.Write) - result.write "ABC" |> equal () \ No newline at end of file + result.write "ABC" |> equal 3 \ No newline at end of file