Skip to content

Commit 96742be

Browse files
committed
Added ast module
1 parent ad27cf5 commit 96742be

File tree

7 files changed

+264
-10
lines changed

7 files changed

+264
-10
lines changed

src/Ast.fs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
module rec Fable.Python.Ast
2+
3+
open System.Collections.Generic
4+
open Fable.Core
5+
6+
// fsharplint:disable MemberNames,InterfaceNames
7+
8+
type _identifier = string
9+
10+
11+
type AST =
12+
abstract foo : int
13+
14+
type ``mod`` =
15+
inherit AST
16+
17+
type expr =
18+
inherit AST
19+
20+
type Module =
21+
inherit ``mod``
22+
abstract body : stmt array
23+
24+
type Expression =
25+
inherit ``mod``
26+
abstract body : expr
27+
28+
type stmt =
29+
inherit AST
30+
31+
32+
type FunctionDef =
33+
inherit stmt
34+
35+
abstract name : _identifier
36+
abstract args : arguments
37+
abstract body : stmt array
38+
abstract decorator_list : expr array
39+
abstract returns : expr option
40+
41+
type ClassDef =
42+
inherit stmt
43+
abstract name : _identifier
44+
abstract bases : expr array
45+
abstract keywords : keyword array
46+
abstract body : stmt array
47+
abstract decorator_list : expr array
48+
49+
type Return =
50+
inherit stmt
51+
abstract value : expr option
52+
53+
type Delete =
54+
inherit stmt
55+
abstract targets : expr array
56+
57+
type Assign =
58+
inherit stmt
59+
abstract targets : expr array
60+
abstract value : expr
61+
62+
type Import =
63+
inherit stmt
64+
abstract names : alias array
65+
66+
type ImportFrom =
67+
inherit stmt
68+
abstract ``module`` : _identifier option
69+
abstract names : alias array
70+
abstract level : int
71+
72+
type If =
73+
inherit stmt
74+
abstract test : expr
75+
abstract body : stmt array
76+
abstract orelse : stmt array
77+
78+
type arguments =
79+
inherit AST
80+
81+
abstract posonlyargs : arg array
82+
abstract args : arg array
83+
abstract vararg : arg option
84+
abstract kwonlyargs : arg array
85+
abstract kw_defaults : expr option list
86+
abstract kwarg : arg option
87+
abstract defaults : expr array
88+
89+
type arg =
90+
inherit AST
91+
abstract arg : _identifier
92+
abstract annotation : expr option
93+
94+
type keyword =
95+
inherit AST
96+
abstract arg : _identifier option
97+
abstract value : expr
98+
99+
type alias =
100+
inherit AST
101+
abstract name : _identifier
102+
abstract asname : _identifier option
103+
104+
105+
[<StringEnum>]
106+
type Mode =
107+
| Exec
108+
| [<CompiledName("func_mode")>] FuncMode
109+
110+
type IExports =
111+
/// Parse the source into an AST node
112+
abstract parse : string -> AST
113+
abstract parse : string * filename: string -> AST
114+
abstract parse : string * filename: string * mode: Mode-> AST
115+
abstract unparse : astObj: AST -> string
116+
abstract walk : node: AST -> AST array
117+
/// Return a formatted dump of the tree in node.
118+
abstract dump : node : AST -> string
119+
/// Return a formatted dump of the tree in node.
120+
abstract dump : node : AST * annotate_fields : bool -> string
121+
/// Return a formatted dump of the tree in node.
122+
abstract dump : node : AST * annotate_fields : bool * include_attributes: bool -> string
123+
124+
[<ImportDefault("ast")>]
125+
let ast: IExports = nativeOnly

src/Builtins.fs

Lines changed: 113 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,120 @@ open Fable.Core
55

66
// fsharplint:disable MemberNames,InterfaceNames
77

8-
type Builtins =
9-
abstract print : obj: obj -> unit
8+
type TextIOBase =
9+
abstract read : unit -> string
10+
abstract read : __size: int -> string
11+
12+
type TextIOWrapper =
13+
inherit TextIOBase
14+
15+
[<StringEnum>]
16+
type OpenTextModeUpdating =
17+
| [<CompiledName("r+")>] ReadUpdate
18+
| [<CompiledName("+r")>] UpdateRead
19+
| [<CompiledName("rt+")>] ReadTextUpdate
20+
| [<CompiledName("r+t")>] ReadUpdateText
21+
| [<CompiledName("+rt")>] UpdateReadText
22+
| [<CompiledName("tr+")>] TextReadUpdate
23+
| [<CompiledName("t+r")>] TextUpdateRead
24+
| [<CompiledName("+tr")>] UpdateTextRead
25+
| [<CompiledName("w+")>] WriteUpdate
26+
| [<CompiledName("+w")>] UpdateWrite
27+
| [<CompiledName("wt+")>] WriteTextUpdate
28+
| [<CompiledName("w+t")>] WriteUpdateText
29+
| [<CompiledName("+wt")>] UpdateWriteText
30+
| [<CompiledName("tw+")>] TextWriteUpdate
31+
| [<CompiledName("t+w")>] TextUpdateWrite
32+
| [<CompiledName("+tw")>] UpdateTextWrite
33+
| [<CompiledName("a+")>] AppendUpdate
34+
| [<CompiledName("+a")>] UpdateAppend
35+
| [<CompiledName("at+")>] AppendTextUpdate
36+
| [<CompiledName("a+t")>] AppendUpdateText
37+
| [<CompiledName("+at")>] UpdateAppendText
38+
| [<CompiledName("ta+")>] TextAppendUpdate
39+
| [<CompiledName("t+a")>] TextUpdateAppend
40+
| [<CompiledName("+ta")>] UpdateTextAppend
41+
| [<CompiledName("x+")>] CreateUpdate
42+
| [<CompiledName("+x")>] UpdateCreate
43+
| [<CompiledName("xt+")>] CreateTextUpdate
44+
| [<CompiledName("x+t")>] CreateUpdateText
45+
| [<CompiledName("+xt")>] UpdateCreateText
46+
| [<CompiledName("tx+")>] TextCreateUpdate
47+
| [<CompiledName("t+x")>] TextUpdateCreate
48+
| [<CompiledName("+tx")>] UpdateTextCreate
49+
50+
[<StringEnum>]
51+
type OpenTextModeReading =
52+
| [<CompiledName("rt")>] Read
53+
| [<CompiledName("rt")>] ReadText
54+
| [<CompiledName("tr")>] TextRead
55+
56+
[<StringEnum>]
57+
type OpenTextModeWriting =
58+
| [<CompiledName("w")>] Write
59+
| [<CompiledName("wt")>] WriteText
60+
| [<CompiledName("tw")>] TextWrite
61+
| [<CompiledName("a")>] Append
62+
| [<CompiledName("at")>] AppendText
63+
| [<CompiledName("ta")>] TextAppend
64+
| [<CompiledName("x")>] Create
65+
| [<CompiledName("xt")>] CreateText
66+
| [<CompiledName("tx")>] TextCreate
67+
68+
[<Erase>]
69+
type OpenTextMode =
70+
| OpenTextModeUpdating
71+
| OpenTextModeWriting
72+
| OpenTextModeReading
73+
74+
75+
[<Erase>]
76+
type _OpenFile =
77+
| StrOrBytesPath of string
78+
| FileDescriptor of int
79+
80+
type IExports =
81+
/// Return the absolute value of the argument.
82+
abstract abs : int -> int
83+
/// Return the absolute value of the argument.
84+
abstract abs : float -> float
85+
/// Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
86+
abstract chr : int -> char
87+
/// Return the names in the current scope.
88+
abstract dir : unit -> string list
89+
/// Return an alphabetized list of names comprising (some of) the
90+
/// attributes of the given object, and of attributes reachable from
91+
/// it
92+
abstract dir : obj -> string list
93+
/// Return the identity of an object.
94+
abstract id : obj -> int
95+
///Return the length (the number of items) of an object. The argument may
96+
///be a sequence (such as a string, bytes, tuple, list, or range) or a
97+
///collection (such as a dictionary, set, or frozen set).
98+
abstract len : obj -> int
99+
/// Make an iterator that computes the function using arguments from
100+
/// the iterable. Stops when iterable is exhausted.
10101
abstract map : ('T1 -> 'T2) * IEnumerable<'T1> -> IEnumerable<'T2>
102+
/// Make an iterator that computes the function using arguments from each
103+
/// of the iterables. Stops when the shortest iterable is exhausted.
11104
abstract map : ('T1 * 'T2 -> 'T3) * IEnumerable<'T1> * IEnumerable<'T2> -> IEnumerable<'T3>
105+
/// Make an iterator that computes the function using arguments from each
106+
/// of the iterables. Stops when the shortest iterable is exhausted.
12107
abstract map : ('T1 * 'T2 * 'T3 -> 'T4) * IEnumerable<'T1> * IEnumerable<'T2> * IEnumerable<'T3> -> IEnumerable<'T4>
108+
/// Return the Unicode code point for a one-character string.
109+
abstract ord : char -> int
110+
abstract print : obj: obj -> unit
111+
abstract read : file: _OpenFile -> TextIOWrapper
112+
abstract read : file: _OpenFile * mode: OpenTextMode -> TextIOWrapper
113+
abstract read : file: _OpenFile * mode: OpenTextMode * buffering: int -> TextIOWrapper
114+
abstract read : file: _OpenFile * mode: OpenTextMode * buffering: int * encoding: string -> TextIOWrapper
115+
116+
abstract read :
117+
file: _OpenFile * mode: OpenTextMode * buffering: int * encoding: string * errors: string -> TextIOWrapper
118+
119+
abstract read :
120+
file: _OpenFile * mode: OpenTextMode * buffering: int * encoding: string * errors: string * newline: string ->
121+
TextIOWrapper
13122

14-
let builtins: Builtins = nativeOnly
123+
[<ImportDefault("builtins")>]
124+
let builtins: IExports = nativeOnly

src/Fable.Python.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<Compile Include="Builtins.fs" />
1010
<Compile Include="Json.fs" />
11+
<Compile Include="Ast.fs" />
1112
</ItemGroup>
1213
<ItemGroup>
1314
<ProjectReference Include="../../Fable/src/Fable.Core/Fable.Core.fsproj" />

src/Json.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ open Fable.Core
44

55
// fsharplint:disable MemberNames,InterfaceNames
66

7-
type Json =
7+
type IExports =
88
abstract dumps : obj: obj -> string
99
abstract loads : string -> obj
1010

1111
[<ImportDefault("json")>]
12-
let json: Json = nativeOnly
12+
let json: IExports = nativeOnly

test/Fable.Python.Test.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</ItemGroup>
1414
<ItemGroup>
1515
<Compile Include="Util.fs" />
16+
<Compile Include="TestAst.fs" />
1617
<Compile Include="TestBuiltins.fs" />
1718
<Compile Include="TestJson.fs" />
1819
<Compile Include="Main.fs" />

test/TestAst.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Fable.Python.Tests.Ast
2+
3+
open Util.Testing
4+
open Fable.Python.Ast
5+
6+
[<Fact>]
7+
let ``test ast parse empty module works`` () =
8+
let result = ast.parse ""
9+
result.GetType () |> equal typeof<AST>
10+
11+
[<Fact>]
12+
let ``test ast parse assign works`` () =
13+
let result = ast.parse "a = 10" :?> Module
14+
15+
// Result is a list of statements
16+
result.body.[0].GetType () |> equal typeof<stmt>
17+
18+
// The statement is an assign statement
19+
let assign = (result.body.[0] :?> Assign)
20+
assign.GetType () |> equal typeof<Assign>
21+
22+

test/TestBuiltins.fs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ module Fable.Python.Tests.Builtins
33
open Util.Testing
44
open Fable.Python.Builtins
55

6-
// type MyObject = {
7-
// Foo: int
8-
// Bar: string
9-
// }
10-
116
[<Fact>]
127
let ``test print works`` () =
138
let result = builtins.print "Hello, world!"

0 commit comments

Comments
 (0)