mirror of
https://github.com/enso-org/enso.git
synced 2024-12-31 17:55:35 +03:00
Move Builtin Types and Methods to stdlib (#3363)
This PR replaces hard-coded `@Builtin_Method` and `@Builtin_Type` nodes in Builtins with an automated solution that a) collects metadata from such annotations b) generates `BuiltinTypes` c) registers builtin methods with corresponding constructors. The main differences are: 1) The owner of the builtin method does not necessarily have to be a builtin type 2) You can now mix regular methods and builtin ones in stdlib 3) No need to keep track of builtin methods and types in various places and register them by hand (a source of many typos or omissions as it found during the process of this PR) Related to #181497846 Benchmarks also execute within the margin of error. ### Important Notes The PR got a bit large over time as I was moving various builtin types and finding various corner cases. Most of the changes however are rather simple c&p from Builtins.enso to the corresponding stdlib module. Here is the list of the most crucial updates: - `engine/runtime/src/main/java/org/enso/interpreter/runtime/builtin/Builtins.java` - the core of the changes. We no longer register individual builtin constructors and their methods by hand. Instead, the information about those is read from 2 metadata files generated by annotation processors. When the builtin method is encountered in stdlib, we do not ignore the method. Instead we lookup it up in the list of registered functions (see `getBuiltinFunction` and `IrToTruffle`) - `engine/runtime/src/main/java/org/enso/interpreter/runtime/callable/atom/AtomConstructor.java` has now information whether it corresponds to the builtin type or not. - `engine/runtime/src/main/scala/org/enso/compiler/codegen/RuntimeStubsGenerator.scala` - when runtime stubs generator encounters a builtin type, based on the @Builtin_Type annotation, it looks up an existing constructor for it and registers it in the provided scope, rather than creating a new one. The scope of the constructor is also changed to the one coming from stdlib, while ensuring that synthetic methods (for fields) also get assigned correctly - `engine/runtime/src/main/scala/org/enso/compiler/codegen/IrToTruffle.scala` - when a builtin method is encountered in stdlib we don't generate a new function node for it, instead we look it up in the list of registered builtin methods. Note that Integer and Number present a bit of a challenge because they list a whole bunch of methods that don't have a corresponding method (instead delegating to small/big integer implementations). During the translation new atom constructors get initialized but we don't want to do it for builtins which have gone through the process earlier, hence the exception - `lib/scala/interpreter-dsl/src/main/java/org/enso/interpreter/dsl/MethodProcessor.java` - @Builtin_Method processor not only generates the actual code fpr nodes but also collects and writes the info about them (name, class, params) to a metadata file that is read during builtins initialization - `lib/scala/interpreter-dsl/src/main/java/org/enso/interpreter/dsl/MethodProcessor.java` - @Builtin_Method processor no longer generates only (root) nodes but also collects and writes the info about them (name, class, params) to a metadata file that is read during builtins initialization - `lib/scala/interpreter-dsl/src/main/java/org/enso/interpreter/dsl/TypeProcessor.java` - Similar to MethodProcessor but handles @Builtin_Type annotations. It doesn't, **yet**, generate any builtin objects. It also collects the names, as present in stdlib, if any, so that we can generate the names automatically (see generated `types/ConstantsGen.java`) - `engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin` - various classes annotated with @BuiltinType to ensure that the atom constructor is always properly registered for the builitn. Note that in order to support types fields in those, annotation takes optional `params` parameter (comma separated). - `engine/runtime/src/bench/scala/org/enso/interpreter/bench/fixtures/semantic/AtomFixtures.scala` - drop manual creation of test list which seemed to be a relict of the old design
This commit is contained in:
parent
ce6a97e977
commit
4bbabc00be
@ -191,6 +191,7 @@
|
||||
- [Provide `tagValues` for function arguments in the language server][3422]
|
||||
- [Delay construction of Truffle nodes to speed initialization][3429]
|
||||
- [Frgaal compiler integration to allow for latest Java constructs][3421]
|
||||
- [Move Builtin Types and Methods definitions to stdlib][3363]
|
||||
|
||||
[3227]: https://github.com/enso-org/enso/pull/3227
|
||||
[3248]: https://github.com/enso-org/enso/pull/3248
|
||||
@ -206,6 +207,7 @@
|
||||
[3422]: https://github.com/enso-org/enso/pull/3422
|
||||
[3429]: https://github.com/enso-org/enso/pull/3429
|
||||
[3421]: https://github.com/enso-org/enso/pull/3421
|
||||
[3363]: https://github.com/enso-org/enso/pull/3363
|
||||
|
||||
# Enso 2.0.0-alpha.18 (2021-10-12)
|
||||
|
||||
|
@ -197,11 +197,11 @@ thread_local! {
|
||||
suggestions : vec![
|
||||
Rc::new(
|
||||
Suggestion::new("Text Input","\"\"",&icons.text_input)
|
||||
.with_return_type("Standard.Builtins.Main.Text")
|
||||
.with_return_type("Standard.Base.Data.Text.Text")
|
||||
),
|
||||
Rc::new(
|
||||
Suggestion::new("Number Input","0",&icons.number_input)
|
||||
.with_return_type("Standard.Builtins.Main.Number")
|
||||
.with_return_type("Standard.Base.Data.Numbers.Number")
|
||||
),
|
||||
]
|
||||
},
|
||||
@ -211,8 +211,8 @@ thread_local! {
|
||||
suggestions : vec![
|
||||
Rc::new(
|
||||
Suggestion::new("Text Length","length",&icons.default)
|
||||
.with_this_arg("Standard.Builtins.Main.Text")
|
||||
.with_return_type("Standard.Base.Main.Integer")
|
||||
.with_this_arg("Standard.Base.Data.Text.Text")
|
||||
.with_return_type("Standard.Base.Data.Numbers.Integer")
|
||||
.marked_as_method_call("length","Standard.Base.Data.Text.Extensions")
|
||||
)
|
||||
]
|
||||
@ -231,7 +231,7 @@ thread_local! {
|
||||
Suggestion::new("Fetch Data", "Http.fetch",&icons.default)
|
||||
.with_return_type("Standard.Base.Network.Http.Body.Body")
|
||||
.with_argument_types(vec![
|
||||
"Standard.Builtins.Main.Text",
|
||||
"Standard.Base.Data.Text.Text",
|
||||
"Vector.Vector",
|
||||
])
|
||||
.with_import_added("Standard.Base.Network.Http")
|
||||
|
@ -1,7 +1,7 @@
|
||||
x ->
|
||||
result = Builtins.Ref.new '{ message: ""}'
|
||||
result = Ref.new '{ message: ""}'
|
||||
x.catch err->
|
||||
message = err.to_display_text
|
||||
Builtins.Ref.put result ('{ "kind": "Dataflow", "message": ' + message.to_json.to_text + '}')
|
||||
Builtins.Ref.get result
|
||||
Ref.put result ('{ "kind": "Dataflow", "message": ' + message.to_json.to_text + '}')
|
||||
Ref.get result
|
||||
|
||||
|
@ -950,7 +950,10 @@ lazy val searcher = project
|
||||
lazy val `interpreter-dsl` = (project in file("lib/scala/interpreter-dsl"))
|
||||
.settings(
|
||||
version := "0.1",
|
||||
libraryDependencies += "org.netbeans.api" % "org-openide-util-lookup" % "RELEASE130"
|
||||
libraryDependencies ++= Seq(
|
||||
"org.apache.commons" % "commons-lang3" % commonsLangVersion,
|
||||
"org.netbeans.api" % "org-openide-util-lookup" % "RELEASE130"
|
||||
)
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@ -1130,6 +1133,7 @@ lazy val runtime = (project in file("engine/runtime"))
|
||||
Compile/compile/compilers := FrgaalJavaCompiler.compilers((Compile / dependencyClasspath).value, compilers.value, javaVersion),
|
||||
Test / parallelExecution := false,
|
||||
Test / logBuffered := false,
|
||||
Test / testOptions += Tests.Argument("-oD"), // show timings for individual tests
|
||||
scalacOptions += "-Ymacro-annotations",
|
||||
scalacOptions ++= Seq("-Ypatmat-exhaust-depth", "off"),
|
||||
libraryDependencies ++= jmh ++ jaxb ++ circe ++ Seq(
|
||||
@ -1170,7 +1174,7 @@ lazy val runtime = (project in file("engine/runtime"))
|
||||
s"--upgrade-module-path=${file("engine/runtime/build-cache/truffle-api.jar").absolutePath}"
|
||||
),
|
||||
Test / fork := true,
|
||||
Test / envVars ++= distributionEnvironmentOverrides,
|
||||
Test / envVars ++= distributionEnvironmentOverrides ++ Map("ENSO_TEST_DISABLE_IR_CACHE" -> "false"),
|
||||
bootstrap := CopyTruffleJAR.bootstrapJARs.value,
|
||||
Global / onLoad := EnvironmentCheck.addVersionCheck(
|
||||
graalVersion,
|
||||
|
383
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Any.enso
Normal file
383
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Any.enso
Normal file
@ -0,0 +1,383 @@
|
||||
from Standard.Base import all
|
||||
|
||||
# The type that subsumes all types.
|
||||
type Any
|
||||
|
||||
## Any is the universal top-type, with all other types being subsumed by it.
|
||||
|
||||
If a value of type Any is expected in a given location, _any value_ can
|
||||
be used in that position.
|
||||
@Builtin_Type
|
||||
type Any
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Executes the provided handler on a dataflow error, or executes as
|
||||
identity on a non-error value.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value.
|
||||
catch_primitive : (Error -> Any) -> Any
|
||||
catch_primitive handler = @Builtin_Method "Any.catch"
|
||||
|
||||
## Generic conversion of an arbitrary Enso value to a corresponding textual
|
||||
representation.
|
||||
|
||||
> Example
|
||||
Getting a textual representation of the number 7.
|
||||
|
||||
7.to_text
|
||||
to_text : Text
|
||||
to_text = @Builtin_Method "Any.to_text"
|
||||
|
||||
## Generic conversion of an arbitrary Enso value to a corresponding human-readable
|
||||
representation.
|
||||
|
||||
> Example
|
||||
Getting a human-readable textual representation of the number 7.
|
||||
|
||||
7.to_text
|
||||
to_display_text : Text
|
||||
to_display_text = @Builtin_Method "Any.to_display_text"
|
||||
|
||||
## ALIAS Equality
|
||||
|
||||
Checks if `this` is equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The object to compare `this` with.
|
||||
|
||||
Two values are considered to be equal in Enso when they obey the following
|
||||
recursive properties:
|
||||
- At each level, they have the same structure.
|
||||
- The value of each field in `this` is equal (by this definition) to the
|
||||
corresponding field in `that`.
|
||||
|
||||
! Implementing Your Own Equality
|
||||
Equality in Enso is defined to allow comparison of any two values
|
||||
(universal equality), no matter if they are not directly comparable. When
|
||||
implementing equality for your own types, keep in mind that it needs to
|
||||
work with any Enso value as the `that` argument.
|
||||
|
||||
? Generic Equality and Performance
|
||||
While the generic equality provided here will work for _all_ values in
|
||||
Enso, its performance may often be suboptimal. Many types can implement
|
||||
their own equality operations that will be more efficient than these.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_equality =
|
||||
a = 7 * 21
|
||||
a == 147
|
||||
== : Any -> Boolean
|
||||
== that = if Meta.is_same_object this that then True else
|
||||
this_meta = Meta.meta this
|
||||
that_meta = Meta.meta that
|
||||
case Cons this_meta that_meta of
|
||||
Cons (Meta.Atom _) (Meta.Atom _) ->
|
||||
c_1 = this_meta.constructor
|
||||
c_2 = that_meta.constructor
|
||||
if Meta.is_same_object c_1 c_2 . not then False else
|
||||
f_1 = this_meta.fields
|
||||
f_2 = that_meta.fields
|
||||
0.up_to f_1.length . all i-> (f_1.at i) == (f_2.at i)
|
||||
Cons (Meta.Error _) (Meta.Error _) -> this_meta.payload == that_meta.payload
|
||||
Cons (Meta.Polyglot o_1) (Meta.Polyglot o_2) ->
|
||||
langs_match = (this_meta.get_language == Meta.Java) && (that_meta.get_language == Meta.Java)
|
||||
if langs_match.not then False else o_1.equals o_2
|
||||
Cons (Meta.Unresolved_Symbol _) (Meta.Unresolved_Symbol _) ->
|
||||
(this_meta.name == that_meta.name) && (this_meta.scope == that_meta.scope)
|
||||
## Constructor comparison is covered by the identity equality.
|
||||
Primitive objects should define their own equality.
|
||||
Therefore, there are no more cases to handle in this method.
|
||||
_ -> False
|
||||
|
||||
## ALIAS Inequality
|
||||
|
||||
Checks if `this` is not equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The object to compare `this` against.
|
||||
|
||||
! Implementing Your Own Inequality
|
||||
We recommend that you do not implement your own inequality, instead relying
|
||||
on the default definition given here. If you do, please ensure that you
|
||||
satisfy universal equality, as described in the documentation for `Any.==`.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is not equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_inequality =
|
||||
a = 7 * 21
|
||||
a != 147
|
||||
!= : Any -> Boolean
|
||||
!= that = (this == that).not
|
||||
|
||||
## ALIAS Greater Than
|
||||
|
||||
Checks if `this` is greater than `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `>` defined, a type must define `compare_to`, returning an Ordering.
|
||||
|
||||
! Implementing Greater Than
|
||||
Many types can admit a definition of greater than that is more efficient
|
||||
than the generic one given here. When implementing this for your own types
|
||||
please ensure that it is semantically equivalent to using `.compare_to`.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is greater than `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_greater =
|
||||
a = 7 * 28
|
||||
a > 147
|
||||
> : Any -> Boolean
|
||||
> that = this.compare_to that == Ordering.Greater
|
||||
|
||||
## ALIAS Greater Than or Equal
|
||||
|
||||
Checks if `this` is greater than or equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `>=` defined, a type must define both `>` and `==`.
|
||||
|
||||
! Implementing Greater Than or Equal
|
||||
While it is often possible to implement a more efficient version of this
|
||||
operation for complex types, care must be taken to ensure that your
|
||||
implementation is semantically equivalent to the disjunction of the
|
||||
greater than and equal to operations.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is greater than or equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_greater_eq =
|
||||
a = 6 * 21
|
||||
a >= 147
|
||||
>= : Any -> Boolean
|
||||
>= that = (this > that) || (this == that)
|
||||
|
||||
## ALIAS Less Than
|
||||
|
||||
Checks if `this` is less than `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `<` defined, a type must define `compare_to`, returning an Ordering.
|
||||
|
||||
! Implementing Less Than
|
||||
Many types can admit a definition of less than that is more efficient than
|
||||
the generic one given here. When implementing this for your own types
|
||||
please ensure that it is semantically equivalent to using `.compare_to`.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is less than `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_less =
|
||||
a = 7 * 21
|
||||
a < 147
|
||||
< : Any -> Boolean
|
||||
< that = this.compare_to that == Ordering.Less
|
||||
|
||||
## ALIAS Less Than or Equal
|
||||
|
||||
Checks if `this` is less than or equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `<=` defined, a type must define both `<` and `==`.
|
||||
|
||||
! Implementing Less Than or Equal
|
||||
While it is often possible to implement a more efficient version of this
|
||||
operation for complex types, care must be taken to ensure that your
|
||||
implementation is semantically equivalent to the disjunction of the
|
||||
less than than and equal to operations.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is less than or equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_less_eq =
|
||||
a = 7 * 21
|
||||
a < 147
|
||||
<= : Any -> Boolean
|
||||
<= that = (this < that) || (this == that)
|
||||
|
||||
## Checks if the type is an instance of `Nothing`.
|
||||
|
||||
Nothing in Enso is used as a universal value to indicate the lack of presence
|
||||
of a value. This function is primarily useful in the IDE.
|
||||
|
||||
> Example
|
||||
Checking if the value 1 is nothing.
|
||||
|
||||
1.is_nothing
|
||||
is_nothing : Boolean
|
||||
is_nothing = case this of
|
||||
Nothing -> True
|
||||
_ -> False
|
||||
|
||||
## Executes the provided handler on an error, or returns a non-error value
|
||||
unchanged.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value. By default
|
||||
this is identity.
|
||||
|
||||
> Example
|
||||
Catching an erroneous value and getting the length of its message.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_catch =
|
||||
error = Error.throw "My message"
|
||||
error.catch (err -> err.length)
|
||||
catch : (Error -> Any) -> Any
|
||||
catch (handler = x->x) = this.catch_primitive handler
|
||||
|
||||
## Transforms an error.
|
||||
|
||||
Arguments:
|
||||
- f: The function used to transform the error.
|
||||
|
||||
If `this` is a non-error value it is returned unchanged. However, if `this`
|
||||
is an error, the error is transformed using the provided function.
|
||||
|
||||
> Example
|
||||
Transforming an error value to provide more information.
|
||||
|
||||
from Standard.Base import all
|
||||
from Standard.Examples import Example_Error_Type
|
||||
|
||||
example_map_error =
|
||||
my_map = Map.empty
|
||||
error = my_map.get "x"
|
||||
error.map_error (_ -> Example_Error_Type "x is missing")
|
||||
map_error : (Error -> Error) -> Any
|
||||
map_error _ = this
|
||||
|
||||
## Checks if `this` is an error.
|
||||
|
||||
> Example
|
||||
Checking if the provided value is an error.
|
||||
|
||||
1.is_error
|
||||
is_error : Boolean
|
||||
is_error = False
|
||||
|
||||
## Applies the provided function to `this` unless `this` is `Nothing`, which is
|
||||
returned unchanged.
|
||||
|
||||
Arguments:
|
||||
- f: The function to apply to `this` if `this` is not `Nothing`.
|
||||
|
||||
> Example
|
||||
Applying a function over a value 10.
|
||||
|
||||
10.map_nothing *2
|
||||
map_nothing : (a -> b) -> b | Nothing
|
||||
map_nothing f = case this of
|
||||
Nothing -> Nothing
|
||||
a -> f a
|
||||
|
||||
## Applies the function `this` to the provided argument.
|
||||
|
||||
Arguments:
|
||||
- argument: The argument to apply `this` to.
|
||||
|
||||
? Piping Blocks to Functions
|
||||
This construction is particularly useful for passing a block as an argument
|
||||
to a function. This means that you can compute more sophisticated values
|
||||
in-line, as shown in the example below.
|
||||
|
||||
> Example
|
||||
Applying a function to a block.
|
||||
|
||||
(x -> x + 1) <|
|
||||
y = 1 ^ 3
|
||||
3 + y
|
||||
<| : Any -> Any
|
||||
<| ~argument = this argument
|
||||
|
||||
## Applies the function on the right hand side to the argument on the left.
|
||||
|
||||
Arguments
|
||||
- function: The function to apply to `this`.
|
||||
|
||||
? `|>` or `.`?
|
||||
The eagle-eyed reader will notice that the operator dot (`.`) is very
|
||||
similar to the operator `|>`. In Enso, with the variable precedence of
|
||||
operators, this makes perfect sense. In general, we recommend using `.`.
|
||||
However, there are some contexts where variable precedence might be unclear
|
||||
or confusing, or where the function being applied is not a method. In these
|
||||
contexts we recommend using `|>`.
|
||||
|
||||
> Example
|
||||
Applying multiple functions in a pipeline to compute a number and transform
|
||||
it to text.
|
||||
|
||||
1 |> (* 2) |> (/ 100) |> .to_text
|
||||
|> : (Any -> Any) -> Any
|
||||
|> ~function = function this
|
||||
|
||||
## Composes two functions together, for `f << g` creating the function
|
||||
composition `f ∘ g` (equivalent to `x -> f (g x)`).
|
||||
|
||||
Arguments:
|
||||
- that: The function to compose with `this`.
|
||||
|
||||
> Example
|
||||
Multiply by 2 and then add 1 as a function applied to 2.
|
||||
|
||||
(+1 << *2) 2
|
||||
<< : (Any -> Any) -> (Any -> Any) -> Any -> Any
|
||||
<< ~that = x -> this (that x)
|
||||
|
||||
## Composes two functions together in the forward direction, for `f >> g`
|
||||
creating the function composition `g ∘ f` (equivalent to `x -> g (f (x))`).
|
||||
|
||||
Arguments:
|
||||
- that: The function to compose with `this`.
|
||||
|
||||
> Example
|
||||
Add one and then multiply by two as a function applied to 2.
|
||||
|
||||
(+1 >> *2) 2
|
||||
>> : (Any -> Any) -> (Any -> Any) -> Any -> Any
|
||||
>> ~that = x -> that (this x)
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a Text used to display this value in the IDE.
|
||||
|
||||
The particular representation is left unspecified and subject to change in
|
||||
the future. The current implementation uses JSON serialization as the
|
||||
default.
|
||||
|
||||
Types defining their own versions of this method should ensure that the
|
||||
result is reasonably small and that the operation is quick to compute.
|
||||
|
||||
> Example
|
||||
Converting the number `2` into visualization data.
|
||||
|
||||
2.to_default_visualization_data
|
||||
to_default_visualization_data : Text
|
||||
to_default_visualization_data = this.to_json.to_text
|
@ -1,343 +0,0 @@
|
||||
from Standard.Base import all
|
||||
|
||||
## ALIAS Equality
|
||||
|
||||
Checks if `this` is equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The object to compare `this` with.
|
||||
|
||||
Two values are considered to be equal in Enso when they obey the following
|
||||
recursive properties:
|
||||
- At each level, they have the same structure.
|
||||
- The value of each field in `this` is equal (by this definition) to the
|
||||
corresponding field in `that`.
|
||||
|
||||
! Implementing Your Own Equality
|
||||
Equality in Enso is defined to allow comparison of any two values
|
||||
(universal equality), no matter if they are not directly comparable. When
|
||||
implementing equality for your own types, keep in mind that it needs to
|
||||
work with any Enso value as the `that` argument.
|
||||
|
||||
? Generic Equality and Performance
|
||||
While the generic equality provided here will work for _all_ values in
|
||||
Enso, its performance may often be suboptimal. Many types can implement
|
||||
their own equality operations that will be more efficient than these.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_equality =
|
||||
a = 7 * 21
|
||||
a == 147
|
||||
Any.== : Any -> Boolean
|
||||
Any.== that = if Meta.is_same_object this that then True else
|
||||
this_meta = Meta.meta this
|
||||
that_meta = Meta.meta that
|
||||
case Cons this_meta that_meta of
|
||||
Cons (Meta.Atom _) (Meta.Atom _) ->
|
||||
c_1 = this_meta.constructor
|
||||
c_2 = that_meta.constructor
|
||||
if Meta.is_same_object c_1 c_2 . not then False else
|
||||
f_1 = this_meta.fields
|
||||
f_2 = that_meta.fields
|
||||
0.up_to f_1.length . all i-> (f_1.at i) == (f_2.at i)
|
||||
Cons (Meta.Error _) (Meta.Error _) -> this_meta.payload == that_meta.payload
|
||||
Cons (Meta.Polyglot o_1) (Meta.Polyglot o_2) ->
|
||||
langs_match = (this_meta.get_language == Meta.Java) && (that_meta.get_language == Meta.Java)
|
||||
if langs_match.not then False else o_1.equals o_2
|
||||
Cons (Meta.Unresolved_Symbol _) (Meta.Unresolved_Symbol _) ->
|
||||
(this_meta.name == that_meta.name) && (this_meta.scope == that_meta.scope)
|
||||
## Constructor comparison is covered by the identity equality.
|
||||
Primitive objects should define their own equality.
|
||||
Therefore, there are no more cases to handle in this method.
|
||||
_ -> False
|
||||
|
||||
## ALIAS Inequality
|
||||
|
||||
Checks if `this` is not equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The object to compare `this` against.
|
||||
|
||||
! Implementing Your Own Inequality
|
||||
We recommend that you do not implement your own inequality, instead relying
|
||||
on the default definition given here. If you do, please ensure that you
|
||||
satisfy universal equality, as described in the documentation for `Any.==`.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is not equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_inequality =
|
||||
a = 7 * 21
|
||||
a != 147
|
||||
Any.!= : Any -> Boolean
|
||||
Any.!= that = (this == that).not
|
||||
|
||||
## ALIAS Greater Than
|
||||
|
||||
Checks if `this` is greater than `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `>` defined, a type must define `compare_to`, returning an Ordering.
|
||||
|
||||
! Implementing Greater Than
|
||||
Many types can admit a definition of greater than that is more efficient
|
||||
than the generic one given here. When implementing this for your own types
|
||||
please ensure that it is semantically equivalent to using `.compare_to`.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is greater than `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_greater =
|
||||
a = 7 * 28
|
||||
a > 147
|
||||
Any.> : Any -> Boolean
|
||||
Any.> that = this.compare_to that == Ordering.Greater
|
||||
|
||||
## ALIAS Greater Than or Equal
|
||||
|
||||
Checks if `this` is greater than or equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `>=` defined, a type must define both `>` and `==`.
|
||||
|
||||
! Implementing Greater Than or Equal
|
||||
While it is often possible to implement a more efficient version of this
|
||||
operation for complex types, care must be taken to ensure that your
|
||||
implementation is semantically equivalent to the disjunction of the
|
||||
greater than and equal to operations.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is greater than or equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_greater_eq =
|
||||
a = 6 * 21
|
||||
a >= 147
|
||||
Any.>= : Any -> Boolean
|
||||
Any.>= that = (this > that) || (this == that)
|
||||
|
||||
## ALIAS Less Than
|
||||
|
||||
Checks if `this` is less than `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `<` defined, a type must define `compare_to`, returning an Ordering.
|
||||
|
||||
! Implementing Less Than
|
||||
Many types can admit a definition of less than that is more efficient than
|
||||
the generic one given here. When implementing this for your own types
|
||||
please ensure that it is semantically equivalent to using `.compare_to`.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is less than `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_less =
|
||||
a = 7 * 21
|
||||
a < 147
|
||||
Any.< : Any -> Boolean
|
||||
Any.< that = this.compare_to that == Ordering.Less
|
||||
|
||||
## ALIAS Less Than or Equal
|
||||
|
||||
Checks if `this` is less than or equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The value to compare `this` against.
|
||||
|
||||
To have `<=` defined, a type must define both `<` and `==`.
|
||||
|
||||
! Implementing Less Than or Equal
|
||||
While it is often possible to implement a more efficient version of this
|
||||
operation for complex types, care must be taken to ensure that your
|
||||
implementation is semantically equivalent to the disjunction of the
|
||||
less than than and equal to operations.
|
||||
|
||||
> Example
|
||||
Checking if the variable `a` is less than or equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_less_eq =
|
||||
a = 7 * 21
|
||||
a < 147
|
||||
Any.<= : Any -> Boolean
|
||||
Any.<= that = (this < that) || (this == that)
|
||||
|
||||
## Checks if the type is an instance of `Nothing`.
|
||||
|
||||
Nothing in Enso is used as a universal value to indicate the lack of presence
|
||||
of a value. This function is primarily useful in the IDE.
|
||||
|
||||
> Example
|
||||
Checking if the value 1 is nothing.
|
||||
|
||||
1.is_nothing
|
||||
Any.is_nothing : Boolean
|
||||
Any.is_nothing = case this of
|
||||
Nothing -> True
|
||||
_ -> False
|
||||
|
||||
## Executes the provided handler on an error, or returns a non-error value
|
||||
unchanged.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value. By default
|
||||
this is identity.
|
||||
|
||||
> Example
|
||||
Catching an erroneous value and getting the length of its message.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_catch =
|
||||
error = Error.throw "My message"
|
||||
error.catch (err -> err.length)
|
||||
Any.catch : (Error -> Any) -> Any
|
||||
Any.catch (handler = x->x) = this.catch_primitive handler
|
||||
|
||||
## Transforms an error.
|
||||
|
||||
Arguments:
|
||||
- f: The function used to transform the error.
|
||||
|
||||
If `this` is a non-error value it is returned unchanged. However, if `this`
|
||||
is an error, the error is transformed using the provided function.
|
||||
|
||||
> Example
|
||||
Transforming an error value to provide more information.
|
||||
|
||||
from Standard.Base import all
|
||||
from Standard.Examples import Example_Error_Type
|
||||
|
||||
example_map_error =
|
||||
my_map = Map.empty
|
||||
error = my_map.get "x"
|
||||
error.map_error (_ -> Example_Error_Type "x is missing")
|
||||
Any.map_error : (Error -> Error) -> Any
|
||||
Any.map_error _ = this
|
||||
|
||||
## Checks if `this` is an error.
|
||||
|
||||
> Example
|
||||
Checking if the provided value is an error.
|
||||
|
||||
1.is_error
|
||||
Any.is_error : Boolean
|
||||
Any.is_error = False
|
||||
|
||||
## Applies the provided function to `this` unless `this` is `Nothing`, which is
|
||||
returned unchanged.
|
||||
|
||||
Arguments:
|
||||
- f: The function to apply to `this` if `this` is not `Nothing`.
|
||||
|
||||
> Example
|
||||
Applying a function over a value 10.
|
||||
|
||||
10.map_nothing *2
|
||||
Any.map_nothing : (a -> b) -> b | Nothing
|
||||
Any.map_nothing f = case this of
|
||||
Nothing -> Nothing
|
||||
a -> f a
|
||||
|
||||
## Applies the function `this` to the provided argument.
|
||||
|
||||
Arguments:
|
||||
- argument: The argument to apply `this` to.
|
||||
|
||||
? Piping Blocks to Functions
|
||||
This construction is particularly useful for passing a block as an argument
|
||||
to a function. This means that you can compute more sophisticated values
|
||||
in-line, as shown in the example below.
|
||||
|
||||
> Example
|
||||
Applying a function to a block.
|
||||
|
||||
(x -> x + 1) <|
|
||||
y = 1 ^ 3
|
||||
3 + y
|
||||
Any.<| : Any -> Any
|
||||
Any.<| ~argument = this argument
|
||||
|
||||
## Applies the function on the right hand side to the argument on the left.
|
||||
|
||||
Arguments
|
||||
- function: The function to apply to `this`.
|
||||
|
||||
? `|>` or `.`?
|
||||
The eagle-eyed reader will notice that the operator dot (`.`) is very
|
||||
similar to the operator `|>`. In Enso, with the variable precedence of
|
||||
operators, this makes perfect sense. In general, we recommend using `.`.
|
||||
However, there are some contexts where variable precedence might be unclear
|
||||
or confusing, or where the function being applied is not a method. In these
|
||||
contexts we recommend using `|>`.
|
||||
|
||||
> Example
|
||||
Applying multiple functions in a pipeline to compute a number and transform
|
||||
it to text.
|
||||
|
||||
1 |> (* 2) |> (/ 100) |> .to_text
|
||||
Any.|> : (Any -> Any) -> Any
|
||||
Any.|> ~function = function this
|
||||
|
||||
## Composes two functions together, for `f << g` creating the function
|
||||
composition `f ∘ g` (equivalent to `x -> f (g x)`).
|
||||
|
||||
Arguments:
|
||||
- that: The function to compose with `this`.
|
||||
|
||||
> Example
|
||||
Multiply by 2 and then add 1 as a function applied to 2.
|
||||
|
||||
(+1 << *2) 2
|
||||
Any.<< : (Any -> Any) -> (Any -> Any) -> Any -> Any
|
||||
Any.<< ~that = x -> this (that x)
|
||||
|
||||
## Composes two functions together in the forward direction, for `f >> g`
|
||||
creating the function composition `g ∘ f` (equivalent to `x -> g (f (x))`).
|
||||
|
||||
Arguments:
|
||||
- that: The function to compose with `this`.
|
||||
|
||||
> Example
|
||||
Add one and then multiply by two as a function applied to 2.
|
||||
|
||||
(+1 >> *2) 2
|
||||
Any.>> : (Any -> Any) -> (Any -> Any) -> Any -> Any
|
||||
Any.>> ~that = x -> that (this x)
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a Text used to display this value in the IDE.
|
||||
|
||||
The particular representation is left unspecified and subject to change in
|
||||
the future. The current implementation uses JSON serialization as the
|
||||
default.
|
||||
|
||||
Types defining their own versions of this method should ensure that the
|
||||
result is reasonably small and that the operation is quick to compute.
|
||||
|
||||
> Example
|
||||
Converting the number `2` into visualization data.
|
||||
|
||||
2.to_default_visualization_data
|
||||
Any.to_default_visualization_data : Text
|
||||
Any.to_default_visualization_data = this.to_json.to_text
|
179
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso
Normal file
179
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso
Normal file
@ -0,0 +1,179 @@
|
||||
import Standard.Base.Data.Vector
|
||||
|
||||
## Utilities for working with primitive arrays.
|
||||
type Array
|
||||
|
||||
## The type of primitive mutable arrays.
|
||||
@Builtin_Type
|
||||
type Array
|
||||
|
||||
## Gets the element at index in the array this.
|
||||
|
||||
Arguments:
|
||||
- index: The index to get the element from.
|
||||
|
||||
? Safety
|
||||
If index < 0 or index >= this.length, then this operation will result
|
||||
in an Invalid_Array_Index_Error exception.
|
||||
|
||||
> Example
|
||||
Get the element at index 1.
|
||||
|
||||
[1,2,3].to_array.at 1
|
||||
at : Integer -> Any
|
||||
at index = @Builtin_Method "Array.at"
|
||||
|
||||
## Set the cell at the specified index to the provided value, returning
|
||||
the array.
|
||||
|
||||
Arguments:
|
||||
- index: The position in the array to set.
|
||||
- value: The value to set at position index.
|
||||
|
||||
The array is mutated in place, and only returned to facilitate a natural
|
||||
programming style in Enso.
|
||||
|
||||
? Safety
|
||||
If index < 0 or index >= this.length, then this operation will result
|
||||
in an Invalid_Array_Index_Error exception.
|
||||
set_at : Integer -> Any -> Array
|
||||
set_at index value = @Builtin_Method "Array.set_at"
|
||||
|
||||
## Gets the length of the array this.
|
||||
|
||||
> Example
|
||||
Getting the length of an array.
|
||||
|
||||
[1,2,3].to_array.length
|
||||
length : Integer
|
||||
length = @Builtin_Method "Array.length"
|
||||
|
||||
## Sorts the this array in place.
|
||||
|
||||
Arguments:
|
||||
- comparator: A comparison function that takes two elements and returns
|
||||
an Ordering that describes how the first element is ordered with
|
||||
respect to the second.
|
||||
|
||||
> Example
|
||||
Sorting an array of numbers.
|
||||
|
||||
[1,2,3].to_array.sort
|
||||
sort : (Any -> Any -> Ordering) -> Nothing
|
||||
sort comparator = @Builtin_Method "Array.sort"
|
||||
|
||||
## Identity.
|
||||
|
||||
This method is implemented purely for completeness with the runtime's
|
||||
primitive array protocol.
|
||||
to_array : Array
|
||||
to_array = @Builtin_Method "Array.to_array"
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a Text used to display this value in the IDE.
|
||||
|
||||
The particular representation is left unspecified and subject to change in
|
||||
the future. The current implementation uses JSON serialization as the
|
||||
default.
|
||||
|
||||
> Example
|
||||
Converting an array to its default visualization representation.
|
||||
|
||||
[1, 2, 3, 4].to_array.to_default_visualization_data
|
||||
to_default_visualization_data : Text
|
||||
to_default_visualization_data =
|
||||
Vector.Vector this . to_default_visualization_data
|
||||
|
||||
## Creates an array with length 0.
|
||||
|
||||
> Example
|
||||
Create an empty array.
|
||||
|
||||
Array.empty
|
||||
empty : Array
|
||||
empty = @Builtin_Method "Array.empty"
|
||||
|
||||
## Creates a new array of length size, with all elements uninitialized.
|
||||
|
||||
Arguments:
|
||||
- size: The size of the array to create.
|
||||
|
||||
> Example
|
||||
Create a new array of size 10.
|
||||
|
||||
Array.new 10
|
||||
new : Integer -> Array
|
||||
new size = @Builtin_Method "Array.new"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Create an array with one element provided.
|
||||
|
||||
Arguments:
|
||||
- item_1: The one element in the array.
|
||||
new_1 : Any -> Array
|
||||
new_1 item_1 = @Builtin_Method "Array.new_1"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Create an array with two elements provided.
|
||||
|
||||
Arguments:
|
||||
- item_1: The first element.
|
||||
- item_2: The second element.
|
||||
new_2 : Any -> Any -> Array
|
||||
new_2 item_1 item_2 = @Builtin_Method "Array.new_2"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Create an array with three elements provided.
|
||||
|
||||
Arguments:
|
||||
- item_1: The first element.
|
||||
- item_2: The second element.
|
||||
- item_3: The third element.
|
||||
new_3 : Any -> Any -> Any -> Array
|
||||
new_3 item_1 item_2 item_3 = @Builtin_Method "Array.new_3"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Create an array with four elements provided.
|
||||
|
||||
Arguments:
|
||||
- item_1: The first element.
|
||||
- item_2: The second element.
|
||||
- item_3: The third element.
|
||||
- item_4: The fourth element.
|
||||
new_4 : Any -> Any -> Any -> Any -> Array
|
||||
new_4 item_1 item_2 item_3 item_4 = @Builtin_Method "Array.new_4"
|
||||
|
||||
## Copies from the source array, beginning at the specified position, to the
|
||||
specified position in the destination array.
|
||||
|
||||
Arguments:
|
||||
- src: The source array.
|
||||
- source_index: The start position in the src array.
|
||||
- dest: The desination array.
|
||||
- dest_index: The start position in the that array.
|
||||
|
||||
A subsequence of array elements are copied from the src array to the
|
||||
dest array. The number of components copied is equal to count. The
|
||||
components at positions source_index through source_index + count - 1
|
||||
in the strc array are copied into positions dest_index through
|
||||
dest_index + count - 1, respectively, of the destination array.
|
||||
|
||||
If the src and dest arguments refer to the same array, then the copy
|
||||
is performed as if the components at positions source_index through
|
||||
source_index + count - 1 are first copied to a temporary array with
|
||||
length count, and then the contents of the temporary array are copied
|
||||
into positions dest_index through dest_index + count - 1 of the
|
||||
destination array.
|
||||
|
||||
> Example
|
||||
Copying elements from one array to another.
|
||||
|
||||
Array.copy [1,2,3].to_array 0 (Vector.fill 3 0).to_array 0 3
|
||||
copy : Array -> Integer -> Array -> Integer -> Integer -> Nothing
|
||||
copy src source_index dest dest_index count = @Builtin_Method "Array.copy"
|
@ -1,18 +0,0 @@
|
||||
from Standard.Base import all
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a Text used to display this value in the IDE.
|
||||
|
||||
The particular representation is left unspecified and subject to change in
|
||||
the future. The current implementation uses JSON serialization as the
|
||||
default.
|
||||
|
||||
> Example
|
||||
Converting an array to its default visualization representation.
|
||||
|
||||
[1, 2, 3, 4].to_array.to_default_visualization_data
|
||||
Array.to_default_visualization_data : Text
|
||||
Array.to_default_visualization_data =
|
||||
Vector.Vector this . to_default_visualization_data
|
114
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Boolean.enso
Normal file
114
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Boolean.enso
Normal file
@ -0,0 +1,114 @@
|
||||
## Booleans.
|
||||
type Boolean
|
||||
|
||||
## A type with only two possible values.
|
||||
|
||||
The boolean type represents the two truth values of boolean logic. It is
|
||||
primarily used for control-flow.
|
||||
@Builtin_Type
|
||||
type Boolean
|
||||
|
||||
## Compares two booleans for equality.
|
||||
|
||||
Arguments:
|
||||
- that: The boolean to compare this with.
|
||||
|
||||
> Example
|
||||
Comparing True to False to get False.
|
||||
|
||||
True == False
|
||||
== : Boolean -> Boolean
|
||||
== that = @Builtin_Method "Boolean.=="
|
||||
|
||||
## Computes the logical and (conjunction) of two booleans.
|
||||
|
||||
Arguments:
|
||||
- that: The boolean to compute the conjunction of this with.
|
||||
|
||||
! Short Circuiting
|
||||
This method is not implemented in a short-circuiting manner. This means
|
||||
that even if this is False, it will also evaluate that. This is
|
||||
for performance.
|
||||
|
||||
> Example
|
||||
Computing the conjunction of False and True (to get False).
|
||||
|
||||
False && True
|
||||
&& : Boolean -> Boolean
|
||||
&& that = @Builtin_Method "Boolean.&&"
|
||||
|
||||
## Computes the logical or (disjunction) of two booleans.
|
||||
|
||||
Arguments:
|
||||
- that: The boolean to compute the disjunction of this with.
|
||||
|
||||
! Short Circuiting
|
||||
This methid is not implemented in a short-circuiting manner. This means
|
||||
that even if this is True, it will also evaluate that. This is
|
||||
for performance.
|
||||
|
||||
> Example
|
||||
Computing the disjunction of True and False (to get True).
|
||||
|
||||
True || False
|
||||
|| : Boolean -> Boolean
|
||||
|| that = @Builtin_Method "Boolean.||"
|
||||
|
||||
## Computes the logical negation of this.
|
||||
|
||||
> Example
|
||||
Negating True to get False.
|
||||
|
||||
True.not
|
||||
not : Boolean
|
||||
not = @Builtin_Method "Boolean.not"
|
||||
|
||||
## Generates a human-readable text representation of the boolean.
|
||||
|
||||
> Example
|
||||
Converting the value True to text.
|
||||
|
||||
True.to_text
|
||||
to_text : Text
|
||||
to_text = @Builtin_Method "Boolean.to_text"
|
||||
|
||||
## The if-then-else control flow operator that executes one of two branches
|
||||
based on a conditional.
|
||||
|
||||
Arguments:
|
||||
- on_true: The computation to evaluate if this evaluates to True.
|
||||
- on_false: The computation to evaluate if this evaluates to False.
|
||||
|
||||
Both of the arguments to this method are _lazy_, meaning that they will
|
||||
only be evaluated if they are needed (based on the condition).
|
||||
|
||||
> Example
|
||||
Telling the user if a number 27 is divisible by three.
|
||||
|
||||
if (27 % 3) == 0 then IO.println "Yes" else IO.println "No"
|
||||
if_then_else : Any -> Any -> Any
|
||||
if_then_else ~on_true ~on_false = @Builtin_Method "Boolean.if_then_else"
|
||||
|
||||
## The if-then control flow operator that executes a branch if the condition
|
||||
is true, and otherwise returns Nothing.
|
||||
|
||||
Arguments:
|
||||
- on_true: The computation to evaluate if this evaluates to True.
|
||||
|
||||
The argument to this method is _lazy_, meaning that it will only be
|
||||
evaluated if the this evaluates to True.
|
||||
|
||||
> Example
|
||||
Printing a message to the user only if a number is divisible by three.
|
||||
|
||||
if (27 % 3) == 0 then IO.println "Fizz"
|
||||
if_then : Any -> Any | Nothing
|
||||
if_then ~on_true = @Builtin_Method "Boolean.if_then"
|
||||
|
||||
## The constructor for the value True.
|
||||
@Builtin_Type
|
||||
type True
|
||||
|
||||
## The constructor for the value False.
|
||||
@Builtin_Type
|
||||
type False
|
@ -1,5 +1,7 @@
|
||||
from Standard.Base import all hiding Number, Boolean, Array
|
||||
|
||||
import Standard.Base.Data.Numbers as Base_Number
|
||||
|
||||
from Standard.Base.Data.Json import all
|
||||
|
||||
polyglot java import org.enso.base.json.Parser
|
||||
@ -291,7 +293,7 @@ into_helper fmt json = case fmt of
|
||||
Base.Boolean -> case json of
|
||||
Boolean v -> v
|
||||
_ -> Panic.throw (Type_Mismatch_Error json fmt)
|
||||
Base.Number -> case json of
|
||||
Base_Number.Number -> case json of
|
||||
Number v -> v
|
||||
_ -> Panic.throw (Type_Mismatch_Error json fmt)
|
||||
Base.Text -> case json of
|
||||
|
@ -1,5 +1,8 @@
|
||||
from Standard.Builtins import all
|
||||
from Standard.Builtins export Nil, Cons
|
||||
from Standard.Base.Data.Numbers import all
|
||||
from Standard.Base.Error.Common import Error
|
||||
from Standard.Base.Data.Boolean import True, False
|
||||
import Standard.Base.Nothing
|
||||
import Standard.Base.Runtime.Unsafe
|
||||
|
||||
## The basic cons-list type.
|
||||
|
||||
@ -11,9 +14,18 @@ from Standard.Builtins export Nil, Cons
|
||||
> Example
|
||||
A list containing the elements `1`, `2`, and `3`, in this order is:
|
||||
Cons 1 (Cons 2 (Cons 3 Nil))
|
||||
## Cons lists.
|
||||
type List
|
||||
Nil
|
||||
Cons
|
||||
|
||||
## The type that indicates the end of a cons list.
|
||||
type Nil
|
||||
|
||||
## A cons cell for a cons list.
|
||||
|
||||
Arguments:
|
||||
- x: The element at this position in the list.
|
||||
- xs: The rest of the list.
|
||||
type Cons x xs
|
||||
|
||||
## Computes the number of elements in the list.
|
||||
|
||||
@ -264,7 +276,7 @@ type List
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_head = Examples.list.head
|
||||
example_head = Examples.list.x
|
||||
head : Any ! Empty_Error
|
||||
head = case this of
|
||||
Cons a _ -> a
|
||||
@ -364,4 +376,3 @@ map_helper list cons f = case list of
|
||||
Unsafe.set_atom_field cons 1 res
|
||||
@Tail_Call here.map_helper t res f
|
||||
Nil -> Unsafe.set_atom_field cons 1 Nil
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Interval.Bound
|
||||
import Standard.Base.Error.Extensions
|
||||
|
||||
polyglot java import java.lang.Long
|
||||
polyglot java import java.util.Random
|
||||
|
@ -1,316 +0,0 @@
|
||||
from Standard.Base import all hiding Parse_Error
|
||||
|
||||
polyglot java import java.lang.Long
|
||||
polyglot java import java.lang.Double
|
||||
polyglot java import java.lang.Math
|
||||
polyglot java import java.lang.String
|
||||
polyglot java import java.lang.NumberFormatException
|
||||
|
||||
## ALIAS Inverse Sine
|
||||
|
||||
Computes the inverse of the sine function
|
||||
|
||||
Selects a value in the -pi/2 through pi/2 range.
|
||||
|
||||
> Example
|
||||
Calculate the inverse sine of 1.
|
||||
|
||||
1.asin
|
||||
Number.asin : Decimal
|
||||
Number.asin = Math.asin this.to_decimal
|
||||
|
||||
## ALIAS Inverse Cosine
|
||||
|
||||
Computes the inverse of the cosine function.
|
||||
|
||||
Selects a value in the -pi/2 through pi/2 range.
|
||||
|
||||
> Example
|
||||
Calculate the inverse cosine of 1.
|
||||
|
||||
1.acos
|
||||
Number.acos : Decimal
|
||||
Number.acos = Math.acos this.to_decimal
|
||||
|
||||
## ALIAS Inverse Tangent
|
||||
|
||||
Computes the inverse of the tangent function.
|
||||
|
||||
Selects a value in the -pi/2 through pi/2 range.
|
||||
|
||||
> Example
|
||||
Calculate the inverse tangent of 1.
|
||||
|
||||
1.atan
|
||||
Number.atan : Decimal
|
||||
Number.atan = Math.atan this.to_decimal
|
||||
|
||||
## Computes the argument (angle) in the conversion from cartesian
|
||||
to polar coordinates, taking `this` as the x coordinate.
|
||||
|
||||
Arguments:
|
||||
- y: The y coordinate.
|
||||
|
||||
The returned angle is in the -pi through pi range.
|
||||
|
||||
> Example
|
||||
Convert the coordinates 1 and 2 to polar form.
|
||||
|
||||
1.atan_2 2
|
||||
Number.atan_2 : Number -> Decimal
|
||||
Number.atan_2 y = Math.atan2 this.to_decimal y.to_decimal
|
||||
|
||||
## ALIAS Sine
|
||||
|
||||
Computes the sine function.
|
||||
|
||||
> Example
|
||||
Calculate the sine of 2.
|
||||
|
||||
2.sin
|
||||
Number.sin : Decimal
|
||||
Number.sin = Math.sin this.to_decimal
|
||||
|
||||
## ALIAS Cosine
|
||||
|
||||
Computes the cosine function.
|
||||
|
||||
> Example
|
||||
Calculate the cosine of 2.
|
||||
|
||||
2.cos
|
||||
Number.cos : Decimal
|
||||
Number.cos = Math.cos this.to_decimal
|
||||
|
||||
## ALIAS Tangent
|
||||
|
||||
Computes the tangent function.
|
||||
|
||||
> Example
|
||||
Calculate the tangent of 2.
|
||||
|
||||
2.tan
|
||||
Number.tan : Decimal
|
||||
Number.tan = Math.tan this.to_decimal
|
||||
|
||||
## Computes the hyperbolic sine function.
|
||||
|
||||
> Example
|
||||
Calculate the hyperbolic sine of 1.
|
||||
|
||||
1.sinh
|
||||
Number.sinh : Decimal
|
||||
Number.sinh = Math.sinh this.to_decimal
|
||||
|
||||
## Computes the hyperbolic cosine function.
|
||||
|
||||
> Example
|
||||
Calcualte the hyperbolic cosine of 1.
|
||||
|
||||
1.cosh
|
||||
Number.cosh : Decimal
|
||||
Number.cosh = Math.cosh this.to_decimal
|
||||
|
||||
## Computes the hyperbolic tangent function.
|
||||
|
||||
> Example
|
||||
Calculate the hyperbolic tangent of 1.
|
||||
|
||||
1.tanh
|
||||
Number.tanh : Decimal
|
||||
Number.tanh = Math.tanh this.to_decimal
|
||||
|
||||
## ALIAS Exponential
|
||||
|
||||
Computes the exponential function, raising Euler's number `r` to the power of
|
||||
`this`.
|
||||
|
||||
> Example
|
||||
Calculate e to the 4th power.
|
||||
|
||||
4.exp
|
||||
Number.exp : Decimal
|
||||
Number.exp = Math.exp this.to_decimal
|
||||
|
||||
## ALIAS Natural Logarithm
|
||||
|
||||
Computes the natural logarithm function.
|
||||
|
||||
> Example
|
||||
Calculate the natural logarithm of 2.
|
||||
|
||||
2.ln
|
||||
Number.ln : Decimal
|
||||
Number.ln = Math.log this.to_decimal
|
||||
|
||||
## ALIAS Square Root
|
||||
|
||||
Computes the square root of `this`.
|
||||
|
||||
> Example
|
||||
Calculate the square root of 8.
|
||||
|
||||
8.sqrt
|
||||
Number.sqrt : Decimal
|
||||
Number.sqrt = Math.sqrt this.to_decimal
|
||||
|
||||
## ALIAS Logarithm
|
||||
|
||||
Computes the `base`-log of `this`.
|
||||
|
||||
Arguments:
|
||||
- base: The base for the logarithm.
|
||||
|
||||
> Example
|
||||
Calculate log 2 of 4.
|
||||
|
||||
4.log 2
|
||||
Number.log : Number -> Decimal
|
||||
Number.log base = this.ln / base.ln
|
||||
|
||||
## UNSTABLE This API is not user-friendly and will be improved in the future.
|
||||
|
||||
Converts a numeric value to a string, using the Java string formatting
|
||||
syntax.
|
||||
|
||||
Arguments:
|
||||
- fmt: The java-style formatting specifier.
|
||||
|
||||
> Example
|
||||
Convert the value 5 to a string.
|
||||
|
||||
5.format "%x"
|
||||
Number.format : Text -> Text
|
||||
Number.format fmt = String.format fmt this
|
||||
|
||||
## Checks equality of numbers, using an `epsilon` value.
|
||||
|
||||
Arguments:
|
||||
- that: The number to check equality against.
|
||||
- epsilon: The value by which `this` and `that` can be separated by before
|
||||
counting as not equal.
|
||||
|
||||
> Example
|
||||
Check if 1 is equal to 1.0000001 within 0.001.
|
||||
|
||||
1.equals 1.0000001 epsilon=0.001
|
||||
Number.equals : Number -> Number -> Boolean
|
||||
Number.equals that epsilon=0.0 =
|
||||
(this == that) || ((this - that).abs <= epsilon)
|
||||
|
||||
## Returns the smaller value of `this` and `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The number to compare `this` against.
|
||||
|
||||
? Math.min or Number.min
|
||||
While we provide the min method on `Number`, we find it more intuitive to
|
||||
write `Math.min a b` rather than `a.min b`. To that end, we recommend using
|
||||
the first style.
|
||||
|
||||
> Example
|
||||
Find the minimum of 2 and 5.
|
||||
|
||||
2.min 5
|
||||
Number.min : Number -> Number
|
||||
Number.min that = if this < that then this else that
|
||||
|
||||
## Returns the larger value of `this` and `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The number to compare `this` against.
|
||||
|
||||
? Math.max or Number.max
|
||||
While we provide the max method on `Number`, we find it more intuitive to
|
||||
write `Math.max a b` rather than `a.max b`. To that end, we recommend using
|
||||
the first style.
|
||||
|
||||
> Example
|
||||
Find the maximum of 2 and 5.
|
||||
|
||||
2.max 5
|
||||
Number.max : Number -> Number
|
||||
Number.max that = if this > that then this else that
|
||||
|
||||
## Number to JSON conversion.
|
||||
|
||||
> Example
|
||||
Convert the number 8 to JSON.
|
||||
|
||||
8.to_json
|
||||
Number.to_json : Json.Number
|
||||
Number.to_json = Json.Number this
|
||||
|
||||
## ALIAS From Text
|
||||
|
||||
Parses a textual representation of an integer into an integer number, returning
|
||||
a `Parse_Error` if the text does not represent a valid integer.
|
||||
|
||||
Arguments:
|
||||
- text: The text to parse into a integer.
|
||||
- radix: The number base to use for parsing (defaults to 10).
|
||||
|
||||
> Example
|
||||
Parse the text "20220216" into an integer number.
|
||||
|
||||
Integer.parse "20220216"
|
||||
Integer.parse : Text -> Text -> Integer ! Parse_Error
|
||||
Integer.parse text (radix=10) =
|
||||
Panic.catch NumberFormatException (Long.parseLong text radix) _->
|
||||
Error.throw (Parse_Error text)
|
||||
|
||||
## ALIAS From Text
|
||||
|
||||
Parses a textual representation of a decimal into a decimal number, returning
|
||||
a `Parse_Error` if the text does not represent a valid decimal.
|
||||
|
||||
Arguments:
|
||||
- text: The text to parse into a decimal.
|
||||
|
||||
> Example
|
||||
Parse the text "7.6" into a decimal number.
|
||||
|
||||
Decimal.parse "7.6"
|
||||
Decimal.parse : Text -> Decimal ! Parse_Error
|
||||
Decimal.parse text =
|
||||
Panic.catch NumberFormatException (Double.parseDouble text) _->
|
||||
Error.throw (Parse_Error text)
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
A syntax error when parsing a double.
|
||||
type Parse_Error text
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Pretty print the syntax error.
|
||||
Parse_Error.to_display_text : Text
|
||||
Parse_Error.to_display_text =
|
||||
"Could not parse " + this.text.to_text + " as a double."
|
||||
|
||||
## A constant holding the floating-point positive infinity.
|
||||
Number.positive_infinity : Decimal
|
||||
Number.positive_infinity = Double.POSITIVE_INFINITY
|
||||
|
||||
## A constant holding the floating-point negative infinity.
|
||||
Number.negative_infinity : Decimal
|
||||
Number.negative_infinity = Double.NEGATIVE_INFINITY
|
||||
|
||||
## A constant holding the floating-point Not-a-Number value.
|
||||
Number.nan : Decimal
|
||||
Number.nan = Double.NaN
|
||||
|
||||
## Checks if the given number is the floating-point Not-a-Number value.
|
||||
|
||||
This is needed, because the NaN value will return `False` even when being
|
||||
compared with itself, so `x == Number.nan` would not work.
|
||||
Number.is_nan : Boolean
|
||||
Number.is_nan = case this of
|
||||
Decimal -> Double.isNaN this
|
||||
_ -> False
|
||||
|
||||
## Returns the sign of the number.
|
||||
Number.signum : Integer
|
||||
Number.signum =
|
||||
if this > 0 then 1 else
|
||||
if this < 0 then -1 else 0
|
1035
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso
Normal file
1035
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Numbers.enso
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,9 +1,3 @@
|
||||
from Standard.Base import all
|
||||
|
||||
from Standard.Builtins import Less, Equal, Greater
|
||||
|
||||
from Standard.Builtins export Less, Equal, Greater
|
||||
|
||||
## Converts a sign-based representation of ordering to Enso's native ordering.
|
||||
|
||||
Arguments:
|
||||
@ -25,9 +19,18 @@ from_sign sign = if sign == 0 then Equal else
|
||||
The result should be returned in terms of how `this` orders in comparison to
|
||||
`that`. So, if `this` is greater than `that`, you should return `Greater.`
|
||||
type Ordering
|
||||
Less
|
||||
Equal
|
||||
Greater
|
||||
|
||||
## A representation that the first value orders as less than the second.
|
||||
@Builtin_Type
|
||||
type Less
|
||||
|
||||
## A representation that the first value orders as equal to the second.
|
||||
@Builtin_Type
|
||||
type Equal
|
||||
|
||||
## A representation that the first value orders as greater than the second.
|
||||
@Builtin_Type
|
||||
type Greater
|
||||
|
||||
## Converts the ordering to the signed notion of ordering based on integers.
|
||||
|
||||
|
43
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ref.enso
Normal file
43
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Ref.enso
Normal file
@ -0,0 +1,43 @@
|
||||
## Utilities for working with mutable references.
|
||||
type Ref
|
||||
|
||||
## A mutable reference type.
|
||||
@Builtin_Type
|
||||
type Ref
|
||||
|
||||
## Gets the contents of the mutable reference ref.
|
||||
|
||||
Arguments:
|
||||
- ref: The reference to get the contents of.
|
||||
|
||||
> Example
|
||||
Getting the contents of a reference.
|
||||
|
||||
Ref.get (Ref.new 0)
|
||||
get : Ref -> Any
|
||||
get ref = @Builtin_Method "Ref.get"
|
||||
|
||||
## Puts a new value into the reference, returning the old value.
|
||||
|
||||
Arguments:
|
||||
- ref: The reference in which to store the value.
|
||||
- new_value: The new value to store in ref.
|
||||
|
||||
> Example
|
||||
Storing the value 10 in a reference.
|
||||
|
||||
Ref.put (Ref.new 0) 10
|
||||
put : Ref -> Any -> Any
|
||||
put ref new_value = @Builtin_Method "Ref.put"
|
||||
|
||||
## Creates a new reference containing the provided value.
|
||||
|
||||
Arguments:
|
||||
- value: The value to be contained in the ref.
|
||||
|
||||
> Example
|
||||
Creating a new reference containing the value 7.
|
||||
|
||||
Ref.new 7
|
||||
new : Any -> Ref
|
||||
new value = @Builtin_Method "Ref.new"
|
49
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text.enso
Normal file
49
distribution/lib/Standard/Base/0.0.0-dev/src/Data/Text.enso
Normal file
@ -0,0 +1,49 @@
|
||||
import Standard.Base.Meta
|
||||
polyglot java import org.enso.base.Text_Utils
|
||||
|
||||
## Enso's text type.
|
||||
type Text
|
||||
|
||||
## Enso's text type.
|
||||
|
||||
Enso's text type is natively unicode aware, and will handle arbitrary
|
||||
textual data.
|
||||
|
||||
? Concatenation
|
||||
Enso's text type uses a rope-based structure under the hood to provide
|
||||
users with efficient concatenation operations.
|
||||
@Builtin_Type
|
||||
type Text
|
||||
|
||||
## Concatenates the text that to the right side of this.
|
||||
|
||||
Arguments:
|
||||
- that: The text to concatenate to this.
|
||||
|
||||
> Example
|
||||
Concatenating two texts.
|
||||
|
||||
"Hello" + ", world!"
|
||||
+ : Text -> Text
|
||||
+ that = @Builtin_Method "Text.+"
|
||||
|
||||
## Checks whether `this` is equal to `that`.
|
||||
|
||||
Arguments:
|
||||
- that: The text to compare `this` for equality with.
|
||||
|
||||
! Unicode Equality
|
||||
The definition of equality includes Unicode canonicalization. I.e. two
|
||||
texts are equal if they are identical after canonical decomposition. This
|
||||
ensures that different ways of expressing the same character in the
|
||||
underlying binary representation are considered equal.
|
||||
|
||||
> Example
|
||||
The string 'é' (i.e. the character U+00E9, LATIN SMALL LETTER E WITH ACUTE)
|
||||
is canonically the same as the string 'e\u0301' (i.e. the letter `e`
|
||||
followed by U+0301, COMBINING ACUTE ACCENT). Therefore:
|
||||
|
||||
('é' == 'e\u0301') == True
|
||||
== : Any -> Boolean
|
||||
== that = if Meta.is_same_object this Text then Meta.is_same_object that Text else
|
||||
Text_Utils.equals this that
|
@ -1,7 +1,6 @@
|
||||
## Methods for operating on `Text` in Enso.
|
||||
|
||||
from Standard.Base import all
|
||||
from Standard.Builtins import Text, Prim_Text_Helpers
|
||||
|
||||
import Standard.Base.Data.Text.Regex
|
||||
import Standard.Base.Data.Text.Regex.Mode
|
||||
@ -16,8 +15,6 @@ from Standard.Base.Error.Problem_Behavior as Problem_Behavior_Module import Prob
|
||||
import Standard.Base.Data.Locale
|
||||
import Standard.Base.Meta
|
||||
|
||||
from Standard.Builtins export Text
|
||||
|
||||
export Standard.Base.Data.Text.Matching_Mode
|
||||
export Standard.Base.Data.Text.Case
|
||||
export Standard.Base.Data.Text.Location
|
||||
|
@ -0,0 +1,7 @@
|
||||
## Internal text utilities for inspecting text primitives.
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Forces flattening of a text value.
|
||||
optimize : Text
|
||||
optimize = @Builtin_Method "Prim_Text_Helpers.optimize"
|
@ -14,8 +14,6 @@ import Standard.Base.Data.Text.Regex.Mode
|
||||
import Standard.Base.Data.Text.Regex.Option
|
||||
import Standard.Base.Data.Map
|
||||
|
||||
import Standard.Base.Error.Extensions as Errors
|
||||
|
||||
## Compile the provided `expression` into a regex pattern that can be used for
|
||||
matching.
|
||||
|
||||
|
@ -17,8 +17,7 @@
|
||||
customisable `Engine` and `Pattern` types.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Error.Extensions as Errors
|
||||
import Standard.Base.Error.Common as Errors
|
||||
|
||||
from Standard.Base.Data.Text.Regex.Engine.Default as Default_Engine export Default
|
||||
|
||||
|
@ -40,13 +40,9 @@ import Standard.Base.Data.Text.Regex.Engine
|
||||
import Standard.Base.Data.Text.Regex.Option as Global_Option
|
||||
import Standard.Base.Data.Text.Regex.Mode
|
||||
import Standard.Base.Data.Text.Matching_Mode
|
||||
import Standard.Base.Polyglot.Java as Java_Ext
|
||||
import Standard.Base.Polyglot.Java
|
||||
from Standard.Base.Data.Text.Span as Span_Module import Utf_16_Span
|
||||
|
||||
from Standard.Builtins import Java
|
||||
|
||||
import Standard.Base.Error.Extensions as Errors
|
||||
|
||||
polyglot java import java.lang.IllegalArgumentException
|
||||
polyglot java import java.lang.IndexOutOfBoundsException
|
||||
polyglot java import java.lang.StringBuffer
|
||||
|
@ -1,6 +1,7 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.System
|
||||
|
||||
polyglot java import java.time.Duration as Java_Duration
|
||||
polyglot java import java.time.Period as Java_Period
|
||||
|
@ -1,6 +1,5 @@
|
||||
from Standard.Base import all
|
||||
from Standard.Builtins import Array
|
||||
from Standard.Builtins import Unsupported_Argument_Types
|
||||
import Standard.Base.Runtime.Unsafe
|
||||
|
||||
## Creates a new vector of the given length, initializing elements using
|
||||
the provided constructor function.
|
||||
|
@ -1,4 +1,154 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Base.Data.Json
|
||||
import Standard.Base.Runtime
|
||||
|
||||
## Dataflow errors.
|
||||
type Error
|
||||
|
||||
## A type representing dataflow errors.
|
||||
|
||||
A dataflow error in Enso is one that behaves like a standard value, and
|
||||
hence represents erroneous states in a way that exists _within_ standard
|
||||
control flow.
|
||||
|
||||
? Dataflow Errors or Panics
|
||||
Whilst a Panic is useful for unrecoverable situations, most Enso APIs
|
||||
are designed to use dataflow errors instead. As they exist within the
|
||||
normal program control flow, they are able to be represented on the
|
||||
Enso graph.
|
||||
@Builtin_Type
|
||||
type Error
|
||||
|
||||
## Creates a new dataflow error containing the provided payload.
|
||||
|
||||
Arguments:
|
||||
- payload: The contents of the dataflow error to be created.
|
||||
|
||||
> Example
|
||||
Throw a dataflow error containing the text "Oops".
|
||||
|
||||
Error.throw "Oops"
|
||||
throw : Any -> Error
|
||||
throw payload = @Builtin_Method "Error.throw"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Executes the provided handler on a dataflow error, or executes as
|
||||
identity on a non-error value.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value.
|
||||
catch_primitive : (Error -> Any) -> Any
|
||||
catch_primitive handler = @Builtin_Method "Error.catch_primitive"
|
||||
|
||||
## PRIVATE
|
||||
UNSTABLE
|
||||
|
||||
Returns a textual representation of the stack trace attached to an error.
|
||||
get_stack_trace_text : Text
|
||||
get_stack_trace_text = @Builtin_Method "Error.get_stack_trace_text"
|
||||
|
||||
## Converts an error to a corresponding textual representation.
|
||||
|
||||
> Example
|
||||
Converting a thrown error to text.
|
||||
|
||||
Error.throw "foo" . to_text
|
||||
to_text : Text
|
||||
to_text = @Builtin_Method "Error.to_text"
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Returns a human-readable text representing this error.
|
||||
to_display_text : Text
|
||||
to_display_text = "Error: " + (this.catch .to_display_text)
|
||||
|
||||
## Executes the provided handler on a dataflow error, or returns a non-error
|
||||
value unchanged.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value. By default
|
||||
this is identity.
|
||||
|
||||
> Example
|
||||
Catching an erroneous value and getting the length of its message.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_catch =
|
||||
Examples.throw_error.catch (err -> err.message.length)
|
||||
catch : (Error -> Any) -> Any
|
||||
catch (handler = x->x) = this.catch_primitive handler
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Returns a display representation of the dataflow error on which it is called.
|
||||
|
||||
> Example
|
||||
Displaying a dataflow error.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_display = Examples.throw_error.to_default_visualization_data
|
||||
to_default_visualization_data : Text
|
||||
to_default_visualization_data = this.catch .to_default_visualization_data
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Returns a JSON representation of the dataflow error.
|
||||
|
||||
> Example
|
||||
Converting a dataflow error to JSON.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_to_json = Examples.throw_error.to_json
|
||||
to_json : Json.Object
|
||||
to_json =
|
||||
error_type = ["type", "Error"]
|
||||
error_content = ["content", this.catch .to_json]
|
||||
error_message = ["message", this.catch .to_display_text]
|
||||
Json.from_pairs [error_type, error_content, error_message]
|
||||
|
||||
## Transforms an error.
|
||||
|
||||
Arguments:
|
||||
- f: The function used to transform the error.
|
||||
|
||||
If `this` is a non-error value it is returned unchanged. However, if `this`
|
||||
is an error, the error is transformed using the provided function
|
||||
|
||||
> Example
|
||||
Transforming an error value.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_map_error =
|
||||
map = Examples.map
|
||||
map.get 10 . map_error (_ -> "The element 10 was not found.")
|
||||
map_error : (Error -> Error) -> Any
|
||||
map_error f = this.catch (x -> Error.throw (f x))
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the attached stack trace of the error.
|
||||
|
||||
The ordering of the resulting vector is such that the top stack frame is the
|
||||
first element.
|
||||
stack_trace : Vector.Vector Runtime.Stack_Trace_Element
|
||||
stack_trace =
|
||||
Panic.get_attached_stack_trace this
|
||||
|
||||
## Checks if `this` is an error.
|
||||
|
||||
> Example
|
||||
Checking if the value 1 is an error.
|
||||
|
||||
1.is_error
|
||||
is_error : Boolean
|
||||
is_error = True
|
||||
|
||||
|
||||
type Illegal_State_Error
|
||||
|
||||
@ -33,3 +183,380 @@ type Wrapped_Dataflow_Error payload
|
||||
## PRIVATE
|
||||
Throws the original error.
|
||||
Wrapped_Dataflow_Error.unwrap = Error.throw this.payload
|
||||
|
||||
type Caught_Panic
|
||||
## A wrapper for a caught panic.
|
||||
|
||||
Arguments:
|
||||
- payload: the payload carried by the error.
|
||||
- internal_original_exception (private): the original Java exception that is
|
||||
the source of this panic. Only for internal use. To get the Java exception
|
||||
from polyglot exceptions, match the `payload` on `Polyglot_Error` and
|
||||
extract the Java object from there.
|
||||
@Builtin_Type
|
||||
type Caught_Panic payload internal_original_exception
|
||||
|
||||
## Converts this caught panic into a dataflow error containing the same
|
||||
payload and stack trace.
|
||||
convert_to_dataflow_error : Error
|
||||
convert_to_dataflow_error = @Builtin_Method "Caught_Panic.convert_to_dataflow_error"
|
||||
|
||||
## Returns the stack trace of the caught panic.
|
||||
stack_trace : Vector.Vector Runtime.Stack_Trace_Element
|
||||
stack_trace =
|
||||
Panic.get_attached_stack_trace this
|
||||
|
||||
## Panics.
|
||||
type Panic
|
||||
|
||||
## A panic is an error condition that is based _outside_ of the normal
|
||||
program control flow.
|
||||
|
||||
Panics "bubble up" through the program until they reach either an
|
||||
invocation of Panic.recover Any or the program's main method. An unhandled
|
||||
panic in main will terminate the program.
|
||||
|
||||
? Dataflow Errors or Panics
|
||||
Panics are designed to be used for unrecoverable situations that need
|
||||
to be handled through non-linear control flow mechanisms.
|
||||
@Builtin_Type
|
||||
type Panic
|
||||
|
||||
## Throws a new panic with the provided payload.
|
||||
|
||||
Arguments:
|
||||
- payload: The contents of the panic to be thrown. If the payload is a
|
||||
`Caught_Panic` or a raw Java exception, instead of throwing a new panic
|
||||
with it as a payload, the original exception is rethrown, preserving
|
||||
its stacktrace.
|
||||
|
||||
> Example
|
||||
Throwing a panic containing the text "Oh no!".
|
||||
|
||||
Panic.throw "Oh no!"
|
||||
|
||||
> Example
|
||||
Use together with `Panic.catch` to catch only specific types of errors
|
||||
and rethrow any others, without affecting their stacktraces.
|
||||
|
||||
Panic.catch Any (Panic.throw "foo") caught_panic-> case caught_panic.payload of
|
||||
Illegal_Argument_Error message _ -> "Illegal arguments were provided: "+message
|
||||
other_panic -> Panic.throw other_panic
|
||||
throw : Any -> Panic
|
||||
throw payload = @Builtin_Method "Panic.throw"
|
||||
|
||||
## PRIVATE
|
||||
Executes the provided action and if any panic was thrown, calls the
|
||||
provided callback.
|
||||
|
||||
If action executes successfully, the result of `Panic.catch Any` is the
|
||||
result of that action. Otherwise, it is the result of the provided
|
||||
handler callback, executed with the caught panic as its first argument.
|
||||
|
||||
Arguments:
|
||||
- action: The code to execute that potentially panics.
|
||||
- handler: The callback to handle any panics.
|
||||
catch_primitive : Any -> (Caught_Panic -> Any) -> Any
|
||||
catch_primitive ~action handler = @Builtin_Method "Panic.catch_primitive"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Returns a raw representation of the stack trace attached to the provided
|
||||
throwable. It can be a dataflow error, a panic or a native Java exception.
|
||||
You probably want `Panic.get_attached_stack_trace` instead.
|
||||
primitive_get_attached_stack_trace : Throwable -> Array
|
||||
primitive_get_attached_stack_trace throwable = @Builtin_Method "Panic.primitive_get_attached_stack_trace"
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the attached stack trace of the given throwable. Can be used to get
|
||||
an Enso friendly stack trace from native Java exceptions.
|
||||
|
||||
The ordering of the resulting vector is such that the top stack frame is the
|
||||
first element.
|
||||
get_attached_stack_trace : Caught_Panic | Throwable -> Vector.Vector Runtime.Stack_Trace_Element
|
||||
get_attached_stack_trace error =
|
||||
throwable = case error of
|
||||
Caught_Panic _ internal_original_exception -> internal_original_exception
|
||||
throwable -> throwable
|
||||
prim_stack = Panic.primitive_get_attached_stack_trace throwable
|
||||
stack_with_prims = Vector.Vector prim_stack
|
||||
stack_with_prims.map Runtime.wrap_primitive_stack_trace_element
|
||||
|
||||
## Takes any value, and if it is a dataflow error, throws it as a Panic,
|
||||
otherwise, returns the original value unchanged.
|
||||
|
||||
Arguments:
|
||||
- value: The value to rethrow any errors on as a panic.
|
||||
|
||||
> Example
|
||||
Rethrowing a dataflow error as a panic.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_rethrow = Panic.rethrow Examples.throw_error
|
||||
rethrow : (Any ! Any) -> Any
|
||||
rethrow value = value.catch Panic.throw
|
||||
|
||||
## Executes the provided action and if a panic matching the provided type was
|
||||
thrown, calls the provided callback.
|
||||
|
||||
If action executes successfully, the result of `Panic.catch` is the result of
|
||||
that action. Otherwise, if a matching panic is thrown from within the action,
|
||||
the result is obtained by calling the provided handler callback. Any
|
||||
non-matching panics are forwarded without changes.
|
||||
|
||||
Arguments:
|
||||
- panic_type: The expected panic type. It can either be an Enso type or a
|
||||
Java class. If the Java class is provided, `Polyglot_Error` containing a
|
||||
Java exception of this class will be matched.
|
||||
- action: The code to execute that potentially panics.
|
||||
- handler: The callback to handle the panics. The callback will be provided
|
||||
with a `Caught_Panic` instance encapsulating the `payload` of the caught
|
||||
panic and its stacktrace.
|
||||
|
||||
> Example
|
||||
Handling a specific type of panic.
|
||||
|
||||
Panic.catch Illegal_Argument_Error (Panic.throw (Illegal_Argument_Error "Oh no!" Nothing)) error->
|
||||
"Caught an `Illegal_Argument_Error`: "+error.payload.message
|
||||
|
||||
> Example
|
||||
Handling any panic.
|
||||
|
||||
Panic.catch Any (Panic.throw (Illegal_Argument_Error "Oh no!" Nothing)) error->
|
||||
"Caught some panic!"
|
||||
|
||||
> Example
|
||||
Convert a string to an integer, catching the Java `NumberFormatException`
|
||||
and converting it to a more Enso-friendly dataflow error.
|
||||
|
||||
polyglot java import java.lang.Long
|
||||
polyglot java import java.lang.NumberFormatException
|
||||
parse str =
|
||||
Panic.catch NumberFormatException (Long.parseLong str) caught_panic->
|
||||
Error.throw (Illegal_Argument_Error "The provided string is not a valid number: "+caught_panic.payload.cause.getMessage)
|
||||
catch : Any -> Any -> (Caught_Panic -> Any) -> Any
|
||||
catch panic_type ~action handler =
|
||||
Panic.catch_primitive action caught_panic->
|
||||
case Meta.get_polyglot_language panic_type == "java" of
|
||||
False -> case caught_panic.payload.is_a panic_type of
|
||||
True -> handler caught_panic
|
||||
False -> Panic.throw caught_panic
|
||||
True -> case caught_panic.payload of
|
||||
Polyglot_Error java_exception ->
|
||||
case Java.is_instance java_exception panic_type of
|
||||
True -> handler caught_panic
|
||||
False -> Panic.throw caught_panic
|
||||
_ -> Panic.throw caught_panic
|
||||
|
||||
## Executes the provided action and converts a possible panic matching any of
|
||||
the provided types into a dataflow Error.
|
||||
|
||||
If action executes successfully, the result of `Panic.recover` is the result
|
||||
of that action. Otherwise, if it panicked with a type matching one of the
|
||||
expected error types, that panic is returned as a dataflow error. Unexpected
|
||||
panics are passed through as-is. it is the panic that was thrown after
|
||||
conversion to a dataflow error.
|
||||
|
||||
Arguments:
|
||||
- expected_types: The types of expected panics which should be recovered.
|
||||
This can either be a Vector of types or a single type.
|
||||
- action: The code to execute that potentially panics.
|
||||
|
||||
> Example
|
||||
Converting an expected panic to a dataflow error.
|
||||
|
||||
Panic.recover Illegal_Argument_Error (Panic.throw (Illegal_Argument_Error "Oh!" Nothing))
|
||||
|
||||
> Example
|
||||
Converting one of many expected panic types to a dataflow error.
|
||||
|
||||
Panic.recover [Illegal_Argument_Error, Illegal_State_Error] (Panic.throw (Illegal_Argument_Error "Oh!" Nothing))
|
||||
recover : (Vector.Vector Any | Any) -> Any -> Any
|
||||
recover expected_types ~action =
|
||||
types_to_check = case expected_types of
|
||||
Vector.Vector _ -> expected_types
|
||||
_ -> [expected_types]
|
||||
Panic.catch Any action caught_panic->
|
||||
is_matched = types_to_check.exists typ->
|
||||
caught_panic.payload.is_a typ
|
||||
case is_matched of
|
||||
True -> caught_panic.convert_to_dataflow_error
|
||||
False -> Panic.throw caught_panic
|
||||
|
||||
## The runtime representation of a syntax error.
|
||||
|
||||
Arguments:
|
||||
- message: A description of the erroneous syntax.
|
||||
@Builtin_Type
|
||||
type Syntax_Error message
|
||||
|
||||
## The runtime representation of a type error.
|
||||
|
||||
Arguments:
|
||||
- expected: The expected type at the error location.
|
||||
- actual: The actual type at the error location.
|
||||
- name: The name of the argument whose type is mismatched.
|
||||
@Builtin_Type
|
||||
type Type_Error expected actual name
|
||||
|
||||
## The runtime representation of a compilation error.
|
||||
|
||||
Arguments:
|
||||
- message: A description of the erroneous state.
|
||||
@Builtin_Type
|
||||
type Compile_Error message
|
||||
|
||||
## The error thrown when a there is no pattern to match on the scrutinee.
|
||||
|
||||
Arguments:
|
||||
- scrutinee: The scrutinee that failed to match.
|
||||
@Builtin_Type
|
||||
type Inexhaustive_Pattern_Match_Error scrutinee
|
||||
|
||||
## The error thrown when the number of arguments provided to an operation
|
||||
does not match the expected number of arguments.
|
||||
|
||||
Arguments:
|
||||
- expected_min: the minimum expected number of arguments.
|
||||
- expected_max: the maximum expected number of arguments.
|
||||
- actual: the actual number of arguments passed.
|
||||
@Builtin_Type
|
||||
type Arity_Error expected_min expected_max actual
|
||||
|
||||
## The error thrown when the program attempts to read from a state slot that has
|
||||
not yet been initialized.
|
||||
|
||||
Arguments:
|
||||
- key: The key for the state slot that was not initialized.
|
||||
@Builtin_Type
|
||||
type Uninitialized_State key
|
||||
|
||||
## The error thrown when the specified symbol does not exist as a method on
|
||||
the target.
|
||||
|
||||
Arguments:
|
||||
- target: The target on which the attempted method call was performed.
|
||||
- symbol: The symbol that was attempted to be called on target.
|
||||
@Builtin_Type
|
||||
type No_Such_Method_Error target symbol
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the method name of the method that could not be found.
|
||||
|
||||
> Example
|
||||
Getting the method name from a no such method error.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_method_name =
|
||||
error = Examples.no_such_method
|
||||
error.method_name
|
||||
No_Such_Method_Error.method_name : Text
|
||||
No_Such_Method_Error.method_name =
|
||||
Meta.meta this.symbol . name
|
||||
|
||||
|
||||
## An error that occurred across a polyglot boundary.
|
||||
|
||||
Arguments:
|
||||
- cause: A polyglot object corresponding to the original error.
|
||||
@Builtin_Type
|
||||
type Polyglot_Error cause
|
||||
|
||||
## An error that occurs when the enso_project function is called in a file
|
||||
that is not part of a project.
|
||||
@Builtin_Type
|
||||
type Module_Not_In_Package_Error
|
||||
|
||||
## An error for when an erroneous arithmetic computation takes place.
|
||||
|
||||
Arguments:
|
||||
- message: A description of the error condition.
|
||||
@Builtin_Type
|
||||
type Arithmetic_Error message
|
||||
|
||||
## An error that occurs when a program requests a read from an array index
|
||||
that is out of bounds in the array.
|
||||
|
||||
Arguments:
|
||||
- array: The array in which the index was requested.
|
||||
- index: The index that was out of bounds.
|
||||
@Builtin_Type
|
||||
type Invalid_Array_Index_Error array index
|
||||
|
||||
## An error that occurs when an object is used as a function in a function
|
||||
call, but it cannot be called.
|
||||
|
||||
Arguments:
|
||||
- target: The called object.
|
||||
@Builtin_Type
|
||||
type Not_Invokable_Error target
|
||||
|
||||
## An error that occurs when arguments used in a function call are invalid
|
||||
types for the function.
|
||||
|
||||
Arguments:
|
||||
- arguments: The passed arguments.
|
||||
@Builtin_Type
|
||||
type Unsupported_Argument_Types arguments
|
||||
|
||||
## An error that occurs when the specified module cannot be found.
|
||||
|
||||
Arguments:
|
||||
- name: The module searched for.
|
||||
@Builtin_Type
|
||||
type Module_Does_Not_Exist name
|
||||
|
||||
## An error that occurs when the specified value cannot be converted to a given type
|
||||
## FIXME: please check
|
||||
|
||||
Arguments:
|
||||
- target: ...
|
||||
@Builtin_Type
|
||||
type Invalid_Conversion_Target_Error target
|
||||
|
||||
## An error that occurs when the conversion from one type to another does not exist
|
||||
## FIXME: please check
|
||||
|
||||
Arguments:
|
||||
- target: ...
|
||||
- that: ...
|
||||
- conversion: ...
|
||||
@Builtin_Type
|
||||
type No_Such_Conversion_Error
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
A type used to represent that something has not yet been implemented.
|
||||
|
||||
Arguments:
|
||||
- message: The message describing what implementation is missing.
|
||||
type Unimplemented_Error message
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Converts the unimplemented error to a human-readable error message.
|
||||
Unimplemented_Error.to_display_text : Text
|
||||
Unimplemented_Error.to_display_text = "An implementation is missing: " + this.message
|
||||
|
||||
## ADVANCED
|
||||
|
||||
A function that can be used to indicate that something hasn't been
|
||||
implemented yet.
|
||||
|
||||
Arguments:
|
||||
- message: A description of what implementation is missing.
|
||||
|
||||
> Example
|
||||
Throwing an error to show that something is unimplemented.
|
||||
|
||||
import Standard.Base.Error.Common as Errors
|
||||
|
||||
example_unimplemented = Errors.unimplemented
|
||||
unimplemented : Text -> Void
|
||||
unimplemented message="" = Panic.throw (Unimplemented_Error message)
|
@ -1,268 +0,0 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Builtins
|
||||
from Standard.Base.Runtime.Extensions as Runtime_Extensions import Stack_Trace_Element
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the method name of the method that could not be found.
|
||||
|
||||
> Example
|
||||
Getting the method name from a no such method error.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_method_name =
|
||||
error = Examples.no_such_method
|
||||
error.method_name
|
||||
No_Such_Method_Error.method_name : Text
|
||||
No_Such_Method_Error.method_name =
|
||||
Meta.meta this.symbol . name
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
A type used to represent that something has not yet been implemented.
|
||||
|
||||
Arguments:
|
||||
- message: The message describing what implementation is missing.
|
||||
type Unimplemented_Error message
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Converts the unimplemented error to a human-readable error message.
|
||||
Unimplemented_Error.to_display_text : Text
|
||||
Unimplemented_Error.to_display_text = "An implementation is missing: " + this.message
|
||||
|
||||
## ADVANCED
|
||||
|
||||
A function that can be used to indicate that something hasn't been
|
||||
implemented yet.
|
||||
|
||||
Arguments:
|
||||
- message: A description of what implementation is missing.
|
||||
|
||||
> Example
|
||||
Throwing an error to show that something is unimplemented.
|
||||
|
||||
import Standard.Base.Error.Extensions
|
||||
|
||||
example_unimplemented = Extensions.unimplemented
|
||||
unimplemented : Text -> Void
|
||||
unimplemented message="" = Panic.throw (Unimplemented_Error message)
|
||||
|
||||
## Executes the provided handler on a dataflow error, or returns a non-error
|
||||
value unchanged.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value. By default
|
||||
this is identity.
|
||||
|
||||
> Example
|
||||
Catching an erroneous value and getting the length of its message.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_catch =
|
||||
Examples.throw_error.catch (err -> err.message.length)
|
||||
Error.catch : (Error -> Any) -> Any
|
||||
Error.catch (handler = x->x) = this.catch_primitive handler
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Returns a display representation of the dataflow error on which it is called.
|
||||
|
||||
> Example
|
||||
Displaying a dataflow error.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_display = Examples.throw_error.to_default_visualization_data
|
||||
Error.to_default_visualization_data : Text
|
||||
Error.to_default_visualization_data = this.catch .to_default_visualization_data
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Returns a human-readable text representing this error.
|
||||
Error.to_display_text : Text
|
||||
Error.to_display_text = "Error: " + (this.catch .to_display_text)
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Returns a JSON representation of the dataflow error.
|
||||
|
||||
> Example
|
||||
Converting a dataflow error to JSON.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_to_json = Examples.throw_error.to_json
|
||||
Error.to_json : Json.Object
|
||||
Error.to_json =
|
||||
error_type = ["type", "Error"]
|
||||
error_content = ["content", this.catch .to_json]
|
||||
error_message = ["message", this.catch .to_display_text]
|
||||
Json.from_pairs [error_type, error_content, error_message]
|
||||
|
||||
## Transforms an error.
|
||||
|
||||
Arguments:
|
||||
- f: The function used to transform the error.
|
||||
|
||||
If `this` is a non-error value it is returned unchanged. However, if `this`
|
||||
is an error, the error is transformed using the provided function
|
||||
|
||||
> Example
|
||||
Transforming an error value.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_map_error =
|
||||
map = Examples.map
|
||||
map.get 10 . map_error (_ -> "The element 10 was not found.")
|
||||
Error.map_error : (Error -> Error) -> Any
|
||||
Error.map_error f = this.catch (x -> Error.throw (f x))
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the attached stack trace of the given throwable. Can be used to get
|
||||
an Enso friendly stack trace from native Java exceptions.
|
||||
|
||||
The ordering of the resulting vector is such that the top stack frame is the
|
||||
first element.
|
||||
Panic.get_attached_stack_trace : Caught_Panic | Throwable -> Vector.Vector Stack_Trace_Element
|
||||
Panic.get_attached_stack_trace error =
|
||||
throwable = case error of
|
||||
Caught_Panic _ internal_original_exception -> internal_original_exception
|
||||
throwable -> throwable
|
||||
prim_stack = Panic.primitive_get_attached_stack_trace throwable
|
||||
stack_with_prims = Vector.Vector prim_stack
|
||||
stack_with_prims.map Runtime_Extensions.wrap_primitive_stack_trace_element
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the attached stack trace of the error.
|
||||
|
||||
The ordering of the resulting vector is such that the top stack frame is the
|
||||
first element.
|
||||
Error.stack_trace : Vector.Vector Stack_Trace_Element
|
||||
Error.stack_trace =
|
||||
Panic.get_attached_stack_trace this
|
||||
|
||||
## Checks if `this` is an error.
|
||||
|
||||
> Example
|
||||
Checking if the value 1 is an error.
|
||||
|
||||
1.is_error
|
||||
Error.is_error : Boolean
|
||||
Error.is_error = True
|
||||
|
||||
## Takes any value, and if it is a dataflow error, throws it as a Panic,
|
||||
otherwise, returns the original value unchanged.
|
||||
|
||||
Arguments:
|
||||
- value: The value to rethrow any errors on as a panic.
|
||||
|
||||
> Example
|
||||
Rethrowing a dataflow error as a panic.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_rethrow = Panic.rethrow Examples.throw_error
|
||||
Panic.rethrow : (Any ! Any) -> Any
|
||||
Panic.rethrow value = value.catch Panic.throw
|
||||
|
||||
## Returns the stack trace of the caught panic.
|
||||
Caught_Panic.stack_trace : Vector.Vector Stack_Trace_Element
|
||||
Caught_Panic.stack_trace =
|
||||
Panic.get_attached_stack_trace this
|
||||
|
||||
## Executes the provided action and if a panic matching the provided type was
|
||||
thrown, calls the provided callback.
|
||||
|
||||
If action executes successfully, the result of `Panic.catch` is the result of
|
||||
that action. Otherwise, if a matching panic is thrown from within the action,
|
||||
the result is obtained by calling the provided handler callback. Any
|
||||
non-matching panics are forwarded without changes.
|
||||
|
||||
Arguments:
|
||||
- panic_type: The expected panic type. It can either be an Enso type or a
|
||||
Java class. If the Java class is provided, `Polyglot_Error` containing a
|
||||
Java exception of this class will be matched.
|
||||
- action: The code to execute that potentially panics.
|
||||
- handler: The callback to handle the panics. The callback will be provided
|
||||
with a `Caught_Panic` instance encapsulating the `payload` of the caught
|
||||
panic and its stacktrace.
|
||||
|
||||
> Example
|
||||
Handling a specific type of panic.
|
||||
|
||||
Panic.catch Illegal_Argument_Error (Panic.throw (Illegal_Argument_Error "Oh no!" Nothing)) error->
|
||||
"Caught an `Illegal_Argument_Error`: "+error.payload.message
|
||||
|
||||
> Example
|
||||
Handling any panic.
|
||||
|
||||
Panic.catch Any (Panic.throw (Illegal_Argument_Error "Oh no!" Nothing)) error->
|
||||
"Caught some panic!"
|
||||
|
||||
> Example
|
||||
Convert a string to an integer, catching the Java `NumberFormatException`
|
||||
and converting it to a more Enso-friendly dataflow error.
|
||||
|
||||
polyglot java import java.lang.Long
|
||||
polyglot java import java.lang.NumberFormatException
|
||||
parse str =
|
||||
Panic.catch NumberFormatException (Long.parseLong str) caught_panic->
|
||||
Error.throw (Illegal_Argument_Error "The provided string is not a valid number: "+caught_panic.payload.cause.getMessage)
|
||||
Panic.catch : Any -> Any -> (Caught_Panic -> Any) -> Any
|
||||
Panic.catch panic_type ~action handler =
|
||||
Panic.catch_primitive action caught_panic->
|
||||
case Builtins.Meta.get_polyglot_language panic_type == "java" of
|
||||
False -> case caught_panic.payload.is_a panic_type of
|
||||
True -> handler caught_panic
|
||||
False -> Panic.throw caught_panic
|
||||
True -> case caught_panic.payload of
|
||||
Polyglot_Error java_exception ->
|
||||
case Java.is_instance java_exception panic_type of
|
||||
True -> handler caught_panic
|
||||
False -> Panic.throw caught_panic
|
||||
_ -> Panic.throw caught_panic
|
||||
|
||||
## Executes the provided action and converts a possible panic matching any of
|
||||
the provided types into a dataflow Error.
|
||||
|
||||
If action executes successfully, the result of `Panic.recover` is the result
|
||||
of that action. Otherwise, if it panicked with a type matching one of the
|
||||
expected error types, that panic is returned as a dataflow error. Unexpected
|
||||
panics are passed through as-is. it is the panic that was thrown after
|
||||
conversion to a dataflow error.
|
||||
|
||||
Arguments:
|
||||
- expected_types: The types of expected panics which should be recovered.
|
||||
This can either be a Vector of types or a single type.
|
||||
- action: The code to execute that potentially panics.
|
||||
|
||||
> Example
|
||||
Converting an expected panic to a dataflow error.
|
||||
|
||||
Panic.recover Illegal_Argument_Error (Panic.throw (Illegal_Argument_Error "Oh!" Nothing))
|
||||
|
||||
> Example
|
||||
Converting one of many expected panic types to a dataflow error.
|
||||
|
||||
Panic.recover [Illegal_Argument_Error, Illegal_State_Error] (Panic.throw (Illegal_Argument_Error "Oh!" Nothing))
|
||||
Panic.recover : (Vector.Vector Any | Any) -> Any -> Any
|
||||
Panic.recover expected_types ~action =
|
||||
types_to_check = case expected_types of
|
||||
Vector.Vector _ -> expected_types
|
||||
_ -> [expected_types]
|
||||
Panic.catch Any action caught_panic->
|
||||
is_matched = types_to_check.exists typ->
|
||||
caught_panic.payload.is_a typ
|
||||
case is_matched of
|
||||
True -> caught_panic.convert_to_dataflow_error
|
||||
False -> Panic.throw caught_panic
|
@ -0,0 +1,9 @@
|
||||
# Function types.
|
||||
type Function
|
||||
|
||||
## A function is any type that represents a not-yet evaluated computation.
|
||||
|
||||
Methods are represented as functions with dynamic dispatch semantics on
|
||||
the this argument.
|
||||
@Builtin_Type
|
||||
type Function
|
36
distribution/lib/Standard/Base/0.0.0-dev/src/IO.enso
Normal file
36
distribution/lib/Standard/Base/0.0.0-dev/src/IO.enso
Normal file
@ -0,0 +1,36 @@
|
||||
## Builtin IO operations.
|
||||
|
||||
## Prints the provided message to standard error.
|
||||
|
||||
Arguments:
|
||||
- message: The message to print. It will have to_text called on it to
|
||||
generate a textual representation that is then printed.
|
||||
|
||||
> Example
|
||||
Print the message "Oh no!" to standard error.
|
||||
|
||||
IO.print_err "Oh no!"
|
||||
print_err : Any -> Nothing
|
||||
print_err message = @Builtin_Method "IO.print_err"
|
||||
|
||||
## Prints the provided message to standard output.
|
||||
|
||||
Arguments:
|
||||
- message: The message to print. It will have to_text called on it to
|
||||
generate a textual representation that is then printed.
|
||||
|
||||
> Example
|
||||
Print the message "Oh yes!" to standard output.
|
||||
|
||||
IO.println "Oh yes!"
|
||||
println : Any -> Nothing
|
||||
println message = @Builtin_Method "IO.println"
|
||||
|
||||
## Reads a line from standard input.
|
||||
|
||||
> Example
|
||||
Read a line from standard input.
|
||||
|
||||
IO.readln
|
||||
readln : Text
|
||||
readln = @Builtin_Method "IO.readln"
|
@ -1,5 +1,6 @@
|
||||
import project.Data.Any.Extensions
|
||||
import project.Data.Array.Extensions
|
||||
import project.Data.Any
|
||||
import project.Data.Array
|
||||
import project.Data.Boolean
|
||||
import project.Data.Interval
|
||||
import project.Data.Json
|
||||
import project.Data.List
|
||||
@ -7,28 +8,34 @@ import project.Data.Locale
|
||||
import project.Data.Map
|
||||
import project.Data.Maybe
|
||||
import project.Data.Noise
|
||||
import project.Data.Number.Extensions
|
||||
import project.Data.Numbers
|
||||
import project.Data.Ordering
|
||||
import project.Data.Ordering.Sort_Order
|
||||
import project.Data.Pair
|
||||
import project.Data.Range
|
||||
import project.Data.Ref
|
||||
import project.Data.Text
|
||||
import project.Data.Text.Extensions
|
||||
import project.Data.Text.Matching
|
||||
import project.Data.Vector
|
||||
import project.Error.Common
|
||||
import project.Error.Extensions
|
||||
import project.Function
|
||||
import project.IO
|
||||
import project.Nothing
|
||||
import project.Math
|
||||
import project.Meta
|
||||
import project.Meta.Enso_Project
|
||||
import project.Polyglot
|
||||
import project.Polyglot.Java
|
||||
import project.Runtime
|
||||
import project.Runtime.Extensions
|
||||
import project.Runtime.State
|
||||
import project.Runtime.Resource
|
||||
import project.System.Environment
|
||||
import project.System.File
|
||||
import project.Data.Text.Regex.Mode as Regex_Mode
|
||||
import project.Warning
|
||||
|
||||
from Standard.Builtins import Nothing, Number, Integer, Any, True, False, Cons, Boolean, Arithmetic_Error
|
||||
|
||||
export project.Data.Interval
|
||||
export project.Data.Json
|
||||
export project.Data.Locale
|
||||
@ -36,21 +43,27 @@ export project.Data.Map
|
||||
export project.Data.Maybe
|
||||
export project.Data.Ordering
|
||||
export project.Data.Ordering.Sort_Order
|
||||
export project.Data.Ref
|
||||
export project.Data.Vector
|
||||
export project.IO
|
||||
export project.Math
|
||||
export project.Meta
|
||||
export project.Polyglot.Java
|
||||
export project.Runtime
|
||||
export project.Runtime.State
|
||||
export project.System.Environment
|
||||
export project.System.File
|
||||
export project.Data.Text.Regex.Mode as Regex_Mode
|
||||
export project.Warning
|
||||
|
||||
from project.Data.Any.Extensions export all
|
||||
from project.Data.Array.Extensions export all
|
||||
from project.Data.List export Nil, Cons
|
||||
from project.Data.Number.Extensions export all hiding Math, String, Double
|
||||
from project.Data.Array export Array
|
||||
from project.Data.Any export all
|
||||
from project.Data.Boolean export all
|
||||
from project.Data.List export Nil, Cons, List
|
||||
from project.Data.Numbers export all hiding Math, String, Double, Parse_Error
|
||||
from project.Data.Noise export all hiding Noise
|
||||
from project.Data.Pair export Pair
|
||||
from project.Data.Range export Range
|
||||
from project.Data.Range export all
|
||||
## TODO [RW] Once autoscoping is implemented or automatic imports for ADTs are
|
||||
fixed in the IDE, we should revisit if we want to export ADTs like `Case` by
|
||||
default. It may be unnecessary pollution of scope, but until the issues are
|
||||
@ -60,11 +73,11 @@ from project.Data.Range export Range
|
||||
https://www.pivotaltracker.com/story/show/181309938
|
||||
from project.Data.Text.Extensions export Text, Line_Ending_Style, Case, Location
|
||||
from project.Data.Text.Matching export Case_Insensitive, Text_Matcher, Regex_Matcher
|
||||
from project.Data.Text export all
|
||||
from project.Error.Common export all
|
||||
from project.Error.Extensions export all
|
||||
from project.Function export all
|
||||
from project.Meta.Enso_Project export all
|
||||
from project.Polyglot.Java export all
|
||||
from project.Nothing export all
|
||||
from project.Polyglot export all
|
||||
from project.Runtime.Extensions export all
|
||||
|
||||
from Standard.Builtins export all hiding Meta, Less, Equal, Greater, Ordering
|
||||
|
||||
from project.Runtime.Resource export all
|
||||
|
@ -1,133 +1,5 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Builtins
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a meta-representation of a given runtime entity.
|
||||
|
||||
Arguments:
|
||||
- value: The runtime entity to get the meta representation of.
|
||||
meta : Any -> Meta
|
||||
meta value = if Builtins.Meta.is_atom value then Atom value else
|
||||
if Builtins.Meta.is_constructor value then Constructor value else
|
||||
if Builtins.Meta.is_polyglot value then Polyglot value else
|
||||
if Builtins.Meta.is_unresolved_symbol value then Unresolved_Symbol value else
|
||||
if Builtins.Meta.is_error value then Error value.catch else
|
||||
Primitive value
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks whether two objects are represented by the same underlying reference.
|
||||
|
||||
Arguments:
|
||||
- value_1: The first value.
|
||||
- value_2: The second value.
|
||||
is_same_object : Any -> Any -> Boolean
|
||||
is_same_object value_1 value_2 = Builtins.Meta.is_same_object value_1 value_2
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Any.is_a : Any -> Boolean
|
||||
Any.is_a typ = here.is_a this typ
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Any.is_an : Any -> Boolean
|
||||
Any.is_an typ = here.is_a this typ
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Base.Error.is_a : Any -> Boolean
|
||||
Base.Error.is_a typ = this.is_an typ
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Base.Error.is_an : Any -> Boolean
|
||||
Base.Error.is_an typ = typ == Base.Error
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `value` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check for being an instance of `typ`.
|
||||
- typ: The type to check `this` against.
|
||||
is_a : Any -> Any -> Boolean
|
||||
is_a value typ = if typ == Any then True else
|
||||
if Builtins.Meta.is_error value then typ == Base.Error else
|
||||
case value of
|
||||
Array -> typ == Array
|
||||
Boolean -> if typ == Boolean then True else value == typ
|
||||
Text -> typ == Text
|
||||
Number -> if typ == Number then True else case value of
|
||||
Integer -> typ == Integer
|
||||
Decimal -> typ == Decimal
|
||||
Base.Polyglot -> typ == Base.Polyglot
|
||||
_ ->
|
||||
meta_val = here.meta value
|
||||
case meta_val of
|
||||
Atom _ -> if Builtins.meta.is_atom typ then typ == value else
|
||||
meta_val.constructor == typ
|
||||
Constructor _ ->
|
||||
meta_typ = here.meta typ
|
||||
case meta_typ of
|
||||
Atom _ -> meta_val.constructor == meta_typ.constructor
|
||||
Constructor _ -> meta_val.constructor == meta_typ
|
||||
_ -> False
|
||||
Error _ -> typ == Error
|
||||
Unresolved_Symbol _ -> typ == Unresolved_Symbol
|
||||
_ -> False
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `value` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check for being an instance of `typ`.
|
||||
- typ: The type to check `this` against.
|
||||
is_an : Any -> Any -> Boolean
|
||||
is_an value typ = here.is_a value typ
|
||||
|
||||
## Represents a polyglot language.
|
||||
type Language
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
The Java laguage.
|
||||
type Java
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
An unknown language.
|
||||
type Unknown
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
@ -193,19 +65,87 @@ type Meta
|
||||
- value: The polyglot value contained in the meta representation.
|
||||
type Polyglot value
|
||||
|
||||
|
||||
## Atom methods
|
||||
## PRIVATE
|
||||
|
||||
Gets the atom constructor instance for the provided atom.
|
||||
|
||||
Arguments:
|
||||
- atom: The atom to obtain the constructor for.
|
||||
get_atom_constructor : Atom -> Atom_Constructor
|
||||
get_atom_constructor atom = @Builtin_Method "Meta.get_atom_constructor"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Get the fields for the provided atom.
|
||||
|
||||
Arguments:
|
||||
- atom: The atom to obtain the fields for.
|
||||
get_atom_fields : Atom -> Array
|
||||
get_atom_fields atom = @Builtin_Method "Meta.get_atom_fields"
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a vector of field values of the given atom.
|
||||
Atom.fields : Vector.Vector
|
||||
Atom.fields = Vector.Vector (Builtins.Meta.get_atom_fields this.value)
|
||||
Atom.fields = Vector.Vector (here.get_atom_fields this.value)
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a constructor value of the given atom.
|
||||
Atom.constructor : Any
|
||||
Atom.constructor = Builtins.Meta.get_atom_constructor this.value
|
||||
Atom.constructor : Atom_Constructor
|
||||
Atom.constructor = here.get_atom_constructor this.value
|
||||
|
||||
# Polyglot methods
|
||||
## PRIVATE
|
||||
|
||||
Get a textual representation of the language from which an object comes.
|
||||
|
||||
Arguments:
|
||||
- value: The value to obtain the source language for.
|
||||
get_polyglot_language : Any -> Text
|
||||
get_polyglot_language value = @Builtin_Method "Meta.get_polyglot_language"
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns the language with which a polyglot value is associated.
|
||||
Polyglot.get_language : Language
|
||||
Polyglot.get_language =
|
||||
lang_str = here.get_polyglot_language
|
||||
if lang_str == "java" then Java else Unknown
|
||||
|
||||
# UnresolvedSymbol methods
|
||||
## PRIVATE
|
||||
|
||||
Creates an unresolved symbol for the name name in the scope.
|
||||
|
||||
Arguments:
|
||||
- name: The name of the unresolved symbol.
|
||||
- scope: The scope in which the symbol name is unresolved.
|
||||
create_unresolved_symbol : Text -> Module_Scope -> Unresolved_Symbol
|
||||
create_unresolved_symbol name scope = @Builtin_Method "Meta.create_unresolved_symbol"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Obtains the name of the provided unresolved symbol.
|
||||
|
||||
Arguments:
|
||||
- symbol: The unresolved symbol from which to get the name.
|
||||
get_unresolved_symbol_name : Unresolved_Symbol -> Text
|
||||
get_unresolved_symbol_name symbol = @Builtin_Method "Meta.get_unresolved_symbol_name"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Obtains the scope in which the provided unresolved symbol was created.
|
||||
|
||||
Arguments:
|
||||
- symbol: The unresolved symbol from which to get the scope.
|
||||
get_unresolved_symbol_scope : Unresolved_Symbol -> Module_Scope
|
||||
get_unresolved_symbol_scope symbol = @Builtin_Method "Meta.get_unresolved_symbol_scope"
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
@ -217,35 +157,65 @@ Atom.constructor = Builtins.Meta.get_atom_constructor this.value
|
||||
- new_name: The new name for the unresolved symbol.
|
||||
Unresolved_Symbol.rename : Text -> Any
|
||||
Unresolved_Symbol.rename new_name =
|
||||
Builtins.Meta.create_unresolved_symbol new_name this.scope
|
||||
here.create_unresolved_symbol new_name this.scope
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns the name of an unresolved symbol.
|
||||
Unresolved_Symbol.name : Text
|
||||
Unresolved_Symbol.name = Builtins.Meta.get_unresolved_symbol_name this.value
|
||||
Unresolved_Symbol.name = here.get_unresolved_symbol_name this.value
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns the definition scope of an unresolved symbol.
|
||||
Unresolved_Symbol.scope : Any
|
||||
Unresolved_Symbol.scope = Builtins.Meta.get_unresolved_symbol_scope this.value
|
||||
Unresolved_Symbol.scope = here.get_unresolved_symbol_scope this.value
|
||||
|
||||
|
||||
# Constructor methods
|
||||
## PRIVATE
|
||||
|
||||
Get the fields of an atom constructor.
|
||||
|
||||
Arguments:
|
||||
- atom_constructor: The constructor from which to get the fields.
|
||||
get_constructor_fields : Atom_Constructor -> Array
|
||||
get_constructor_fields atom_constructor = @Builtin_Method "Meta.get_constructor_fields"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Get the name of an atom constructor.
|
||||
|
||||
Arguments:
|
||||
- atom_constructor: The atom constructor from which to obtain the name.
|
||||
get_constructor_name : Atom_Constructor -> Text
|
||||
get_constructor_name atom_constructor = @Builtin_Method "Meta.get_constructor_name"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Constructs a new atom using the provided constructor and fields.
|
||||
|
||||
Arguments:
|
||||
- constructor: The constructor for the atom to create.
|
||||
- fields: The arguments to pass to constructor.
|
||||
new_atom : Atom_Constructor -> Array -> Atom
|
||||
new_atom constructor fields = @Builtin_Method "Meta.new_atom"
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns a vector of field names defined by a constructor.
|
||||
Constructor.fields : Vector.Vector
|
||||
Constructor.fields = Vector.Vector (Builtins.Meta.get_constructor_fields this.value)
|
||||
Constructor.fields = Vector.Vector (here.get_constructor_fields this.value)
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns the name of a constructor.
|
||||
Constructor.name : Text
|
||||
Constructor.name = Builtins.Meta.get_constructor_name this.value
|
||||
Constructor.name = here.get_constructor_name this.value
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
@ -256,16 +226,190 @@ Constructor.name = Builtins.Meta.get_constructor_name this.value
|
||||
- fields: A vector of arguments to pass to the constructor when creating the
|
||||
new atom.
|
||||
Constructor.new : Vector.Vector -> Any
|
||||
Constructor.new fields = Builtins.Meta.new_atom this.value fields.to_array
|
||||
Constructor.new fields = here.new_atom this.value fields.to_array
|
||||
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Returns the language with which a polyglot value is associated.
|
||||
Polyglot.get_language : Language
|
||||
Polyglot.get_language =
|
||||
lang_str = Builtins.Meta.get_polyglot_language
|
||||
if lang_str == "java" then Java else Unknown
|
||||
Returns a meta-representation of a given runtime entity.
|
||||
|
||||
Arguments:
|
||||
- value: The runtime entity to get the meta representation of.
|
||||
meta : Any -> Meta
|
||||
meta value = if here.is_atom value then Atom value else
|
||||
if here.is_atom_constructor value then Constructor value else
|
||||
if here.is_polyglot value then Polyglot value else
|
||||
if here.is_unresolved_symbol value then Unresolved_Symbol value else
|
||||
if here.is_error value then Error value.catch else
|
||||
Primitive value
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks whether two objects are represented by the same underlying reference.
|
||||
|
||||
Arguments:
|
||||
- value_1: The first value.
|
||||
- value_2: The second value.
|
||||
is_same_object : Any -> Any -> Boolean
|
||||
is_same_object value_1 value_2 = @Builtin_Method "Meta.is_same_object"
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Any.is_a : Any -> Boolean
|
||||
Any.is_a typ = here.is_a this typ
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Any.is_an : Any -> Boolean
|
||||
Any.is_an typ = here.is_a this typ
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Base.Error.is_a : Any -> Boolean
|
||||
Base.Error.is_a typ = this.is_an typ
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `this` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- typ: The type to check `this` against.
|
||||
Base.Error.is_an : Any -> Boolean
|
||||
Base.Error.is_an typ = typ == Base.Error
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `value` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check for being an instance of `typ`.
|
||||
- typ: The type to check `this` against.
|
||||
is_a : Any -> Any -> Boolean
|
||||
is_a value typ = if typ == Any then True else
|
||||
if here.is_error value then typ == Base.Error else
|
||||
case value of
|
||||
Array -> typ == Array
|
||||
Boolean -> if typ == Boolean then True else value == typ
|
||||
Text -> typ == Text
|
||||
Number -> if typ == Number then True else case value of
|
||||
Integer -> typ == Integer
|
||||
Decimal -> typ == Decimal
|
||||
Base.Polyglot -> typ == Base.Polyglot
|
||||
_ ->
|
||||
meta_val = here.meta value
|
||||
case meta_val of
|
||||
Atom _ -> if here.is_atom typ then typ == value else
|
||||
meta_val.constructor == typ
|
||||
Constructor _ ->
|
||||
meta_typ = here.meta typ
|
||||
case meta_typ of
|
||||
Atom _ -> meta_val.constructor == meta_typ.constructor
|
||||
Constructor _ -> meta_val.constructor == meta_typ
|
||||
_ -> False
|
||||
Error _ -> typ == Error
|
||||
Unresolved_Symbol _ -> typ == Unresolved_Symbol
|
||||
_ -> False
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
Checks if `value` is an instance of `typ`.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check for being an instance of `typ`.
|
||||
- typ: The type to check `this` against.
|
||||
is_an : Any -> Any -> Boolean
|
||||
is_an value typ = here.is_a value typ
|
||||
|
||||
## Represents a polyglot language.
|
||||
type Language
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
The Java laguage.
|
||||
type Java
|
||||
|
||||
## UNSTABLE
|
||||
ADVANCED
|
||||
|
||||
An unknown language.
|
||||
type Unknown
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Checks if the provided value is an atom constructor.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check.
|
||||
is_atom_constructor : Any -> Boolean
|
||||
is_atom_constructor value = @Builtin_Method "Meta.is_atom_constructor"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Checks if the provided value is an atom.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check.
|
||||
is_atom : Any -> Boolean
|
||||
is_atom value = @Builtin_Method "Meta.is_atom"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Checks if the provided value is a runtime error.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check.
|
||||
is_error : Any -> Boolean
|
||||
is_error value = @Builtin_Method "Meta.is_error"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Checks if the provided value is a polyglot value.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check.
|
||||
is_polyglot : Any -> Boolean
|
||||
is_polyglot value = @Builtin_Method "Meta.is_polyglot"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Checks if the provided value is an unresolved symbol.
|
||||
|
||||
Arguments:
|
||||
- value: The value to check.
|
||||
is_unresolved_symbol : Any -> Boolean
|
||||
is_unresolved_symbol value = @Builtin_Method "Meta.is_unresolved_symbol"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Returns a Text representing the source location of a stack frame above
|
||||
the call.
|
||||
|
||||
Arguments:
|
||||
- frames_to_skip: how many frames on the stack to skip. Called with 0
|
||||
will return exact location of the call.
|
||||
get_source_location_builtin : Integer -> Text
|
||||
get_source_location_builtin frames_to_skip = @Builtin_Method "Meta.get_source_location_builtin"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
@ -282,7 +426,7 @@ Polyglot.get_language =
|
||||
used carefully.
|
||||
get_source_location : Integer -> Text
|
||||
get_source_location skip_frames =
|
||||
Builtins.Meta.get_source_location skip_frames+1
|
||||
here.get_source_location_builtin skip_frames+1
|
||||
|
||||
## PRIVATE
|
||||
|
||||
@ -291,7 +435,7 @@ get_source_location skip_frames =
|
||||
Arguments:
|
||||
- value: The value for which to display the type.
|
||||
get_simple_type_name : Any -> Text
|
||||
get_simple_type_name value = Builtins.Meta.get_simple_type_name value
|
||||
get_simple_type_name value = @Builtin_Method "Meta.get_simple_type_name"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
@ -300,4 +444,4 @@ get_simple_type_name value = Builtins.Meta.get_simple_type_name value
|
||||
Arguments:
|
||||
- value: the value to get the type of.
|
||||
get_qualified_type_name : Any -> Text
|
||||
get_qualified_type_name value = Builtins.Meta.get_qualified_type_name value
|
||||
get_qualified_type_name value = @Builtin_Method "Meta.get_qualified_type_name"
|
||||
|
@ -1,39 +1,49 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Base.System.File
|
||||
|
||||
import Standard.Builtins
|
||||
## Functionality for inspecting the current project.
|
||||
type Project_Description
|
||||
|
||||
## Returns the root directory of the project.
|
||||
## A representation of an Enso project.
|
||||
|
||||
> Example
|
||||
Get the root directory of the project.
|
||||
Arguments:
|
||||
- prim_root_file: The primitive root file of the project.
|
||||
- prim_config: The primitive config of the project.
|
||||
@Builtin_Type
|
||||
type Project_Description prim_root_file prim_config
|
||||
|
||||
Enso_Project.root
|
||||
Builtins.Project_Description.root : File.File
|
||||
Builtins.Project_Description.root = File.new this.prim_root_file.getPath
|
||||
|
||||
## Returns the root data directory of the project.
|
||||
## Returns the root directory of the project.
|
||||
|
||||
> Example
|
||||
Get the data directory of the project.
|
||||
> Example
|
||||
Get the root directory of the project.
|
||||
|
||||
Enso_Project.data
|
||||
Builtins.Project_Description.data : File.File
|
||||
Builtins.Project_Description.data = this.root / "data"
|
||||
Enso_Project.root
|
||||
root : File.File
|
||||
root = File.new this.prim_root_file.getPath
|
||||
|
||||
## Returns the name of the project.
|
||||
## Returns the root data directory of the project.
|
||||
|
||||
> Example
|
||||
Get the name of the project.
|
||||
> Example
|
||||
Get the data directory of the project.
|
||||
|
||||
Enso_Project.name
|
||||
Builtins.Project_Description.name : Text
|
||||
Builtins.Project_Description.name = this.prim_config.name
|
||||
Enso_Project.data
|
||||
data : File.File
|
||||
data = this.root / "data"
|
||||
|
||||
## Returns the namespace of the project.
|
||||
## Returns the name of the project.
|
||||
|
||||
> Example
|
||||
Get the namespace of the project.
|
||||
> Example
|
||||
Get the name of the project.
|
||||
|
||||
Enso_Project.namespace
|
||||
Builtins.Project_Description.namespace : Text
|
||||
Builtins.Project_Description.namespace = this.prim_config.namespace
|
||||
Enso_Project.name
|
||||
name : Text
|
||||
name = this.prim_config.name
|
||||
|
||||
## Returns the namespace of the project.
|
||||
|
||||
> Example
|
||||
Get the namespace of the project.
|
||||
|
||||
Enso_Project.namespace
|
||||
namespace : Text
|
||||
namespace = this.prim_config.namespace
|
||||
|
@ -0,0 +1,7 @@
|
||||
## The type that has only a singleton value.
|
||||
|
||||
It is often used alongside a value of type a to provide a Maybe or
|
||||
Option abstraction. The type a | Nothing is semantically equivalent to
|
||||
Maybe a.
|
||||
@Builtin_Type
|
||||
type Nothing
|
105
distribution/lib/Standard/Base/0.0.0-dev/src/Polyglot.enso
Normal file
105
distribution/lib/Standard/Base/0.0.0-dev/src/Polyglot.enso
Normal file
@ -0,0 +1,105 @@
|
||||
## Generic utilities for interacting with other languages.
|
||||
type Polyglot
|
||||
|
||||
## A type representing interactions with polyglot languages.
|
||||
Polyglot is a term that refers to other languages (such as Java) that are
|
||||
running on the same JVM.
|
||||
|
||||
@Builtin_Type
|
||||
type Polyglot
|
||||
|
||||
## Reads the number of elements in a given polyglot array object.
|
||||
|
||||
Arguments:
|
||||
- array: a polyglot array object, originating in any supported language.
|
||||
get_array_size : Any -> Integer
|
||||
get_array_size array = @Builtin_Method "Polyglot.get_array_size"
|
||||
|
||||
## Executes a polyglot function object (e.g. a lambda).
|
||||
|
||||
Arguments:
|
||||
- callable: The polyglot function object to execute.
|
||||
- arguments: A vector of arguments to callable.
|
||||
execute : Any -> Vector -> Any
|
||||
execute callable arguments = @Builtin_Method "Polyglot.execute"
|
||||
|
||||
## Performs a by-name lookup for a member in a polyglot object.
|
||||
|
||||
Arguments:
|
||||
- object: The polyglot object on which to perform the member lookup.
|
||||
- member_name: The textual name of the member to lookup.
|
||||
|
||||
> Example
|
||||
Look up the field a on an object o.
|
||||
Polyglot.get_member o "a"
|
||||
get_member : Any -> Text
|
||||
get_member object member_name = @Builtin_Method "Polyglot.get_member"
|
||||
|
||||
## Returns a polyglot array of all of the members of the provided object.
|
||||
|
||||
Arguments:
|
||||
- object: The object from which to get a list of member names.
|
||||
|
||||
> Example
|
||||
Get a list of the fields for an object o.
|
||||
Polyglot.get_members o
|
||||
get_members : Any -> Array
|
||||
get_members object = @Builtin_Method "Polyglot.get_members"
|
||||
|
||||
## Instantiates a polyglot object using the provided constructor.
|
||||
|
||||
Arguments:
|
||||
- constructor: The constructor with which to instantiate the object.
|
||||
- arguments: A vector of the arguments to pass to the polyglot
|
||||
constructor.
|
||||
|
||||
> Example
|
||||
Instantiate a new Java Integer with the value 1.
|
||||
Polyglot.new Integer [1]
|
||||
new : Any -> Vector -> Any
|
||||
new constructor arguments = @Builtin_Method "Polyglot.new"
|
||||
|
||||
## Invokes a method on a polyglot object by name.
|
||||
|
||||
Arguments:
|
||||
- target: The polyglot object on which to call the method.
|
||||
- name: The name of the method.
|
||||
- arguments: The arguments to pass to the method given by name.
|
||||
invoke : Any -> Text -> Vector -> Any
|
||||
invoke target name arguments = @Builtin_Method "Polyglot.invoke"
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Checks if `value` defines a source location.
|
||||
|
||||
Source locations are typically exposed by functions, classes, sometimes
|
||||
also other objects to specify their allocation sites.
|
||||
has_source_location : Any -> Boolean
|
||||
has_source_location value = @Builtin_Method "Polyglot.has_source_location"
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Gets the source location of `value`.
|
||||
|
||||
Source locations are typically exposed by functions, classes, sometimes
|
||||
also other objects to specify their allocation sites.
|
||||
This method will throw a polyglot exception if
|
||||
`Polyglot.has_source_location value` returns `False`.
|
||||
get_source_location : Any -> Source_Location
|
||||
get_source_location value = @Builtin_Method "Polyglot.get_source_location"
|
||||
|
||||
## Checks if a polyglot language is installed in the runtime environment.
|
||||
|
||||
Arguments:
|
||||
- langauge_name: The name of the language to test
|
||||
is_language_installed : Text -> Bool
|
||||
is_language_installed language_name = @Builtin_Method "Polyglot.is_language_installed"
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the executable name of a polyglot object.
|
||||
get_executable_name : Any -> Text
|
||||
get_executable_name = @Builtin_Method "Polyglot.get_executable_name"
|
@ -1,6 +1,38 @@
|
||||
from Standard.Base import all
|
||||
## Utilities for working with Java polyglot objects.
|
||||
type Java
|
||||
|
||||
import Standard.Builtins
|
||||
## A type for operations specific to Java polyglot objects.
|
||||
type Java
|
||||
|
||||
## Adds the provided entry to the host class path.
|
||||
|
||||
Arguments:
|
||||
- path: The java classpath entry to add.
|
||||
|
||||
Use of the actual polyglot imports system should be preferred to use of
|
||||
this method.
|
||||
|
||||
> Example
|
||||
Adding Random to the classpath.
|
||||
|
||||
Java.add_to_class_path "java.util.Random"
|
||||
add_to_class_path : Text -> Nothing
|
||||
add_to_class_path path = @Builtin_Method "Java.add_to_class_path"
|
||||
|
||||
## Looks up a java symbol on the classpath by name.
|
||||
|
||||
Arguments:
|
||||
- name: The name of the java symbol to look up.
|
||||
|
||||
Use of the actual polyglot imports system should be preferred to use of
|
||||
this method.
|
||||
|
||||
> Example
|
||||
Look up java's Random class.
|
||||
|
||||
Java.lookup_class "java.util.Random"
|
||||
lookup_class : Text -> Any
|
||||
lookup_class name = @Builtin_Method "Java.lookup_class"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
@ -9,7 +41,7 @@ import Standard.Builtins
|
||||
Arguments:
|
||||
- object: The object to check for class membership.
|
||||
- class: The java class to check for membership in.
|
||||
Builtins.Java.is_instance : Any -> Any -> Boolean
|
||||
Builtins.Java.is_instance object class =
|
||||
is_instance : Any -> Any -> Boolean
|
||||
is_instance object class =
|
||||
class_object = class.class
|
||||
class_object.isInstance object
|
||||
|
92
distribution/lib/Standard/Base/0.0.0-dev/src/Runtime.enso
Normal file
92
distribution/lib/Standard/Base/0.0.0-dev/src/Runtime.enso
Normal file
@ -0,0 +1,92 @@
|
||||
import Standard.Base.Data.Vector
|
||||
import Standard.Base.Polyglot
|
||||
import Standard.Base.Nothing
|
||||
from Standard.Base.Runtime.Extensions import Source_Location
|
||||
|
||||
## Utilities for interacting with the runtime.
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Returns a raw representation of the current execution stack trace.
|
||||
You probably want `Runtime.get_stack_trace` instead.
|
||||
primitive_get_stack_trace : Array
|
||||
primitive_get_stack_trace = @Builtin_Method "Runtime.primitive_get_stack_trace"
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the execution stack trace of its call site. The ordering of the
|
||||
resulting vector is such that the top stack frame is the first element.
|
||||
get_stack_trace : Vector.Vector Stack_Trace_Element
|
||||
get_stack_trace =
|
||||
prim_stack = this.primitive_get_stack_trace
|
||||
stack_with_prims = Vector.Vector prim_stack
|
||||
stack = stack_with_prims.map here.wrap_primitive_stack_trace_element
|
||||
# drop this frame and the one from `Runtime.primitive_get_stack_trace`
|
||||
stack.drop_start 2
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Suggests that the runtime perform garbage collection.
|
||||
|
||||
It is not _guaranteed_ to perform garbage collection, but in practice
|
||||
will _usually_ begin a garbage collection cycle.
|
||||
|
||||
> Example
|
||||
Ask for the runtime to collect garbage.
|
||||
|
||||
Runtime.gc
|
||||
gc : Nothing
|
||||
gc = @Builtin_Method "Runtime.gc"
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Executes the provided action without allowing it to inline.
|
||||
|
||||
Arguments:
|
||||
- action: The computation to be executed.
|
||||
|
||||
This is particularly useful when writing benchmarks and
|
||||
performance-critical code where you need to prevent inlining from
|
||||
occurring.
|
||||
|
||||
> Example
|
||||
Print something to the console without it being inlined.
|
||||
|
||||
Runtime.no_inline <| IO.println "Hi!"
|
||||
no_inline : Any -> Any
|
||||
no_inline ~action = @Builtin_Method "Runtime.no_inline"
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Applies the following function to the given argument, without allowing
|
||||
them to inline.
|
||||
|
||||
Arguments:
|
||||
- function: The one-argument function to call.
|
||||
- arg: The single argument for the function.
|
||||
|
||||
This is particularly useful to avoid constant folding in benchmarks.
|
||||
|
||||
> Example
|
||||
Print something to the console without it being inlined.
|
||||
|
||||
Runtime.no_inline_with_arg IO.println "Hi!"
|
||||
no_inline_with_arg : Any -> Any
|
||||
no_inline_with_arg function arg = @Builtin_Method "Runtime.no_inline_with_arg"
|
||||
|
||||
## PRIVATE
|
||||
Converts a primitive stack trace element into the regular one.
|
||||
wrap_primitive_stack_trace_element el =
|
||||
loc = if Polyglot.has_source_location el then (Source_Location (Polyglot.get_source_location el)) else Nothing
|
||||
name = Polyglot.get_executable_name el
|
||||
Stack_Trace_Element name loc
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Represents a single stack frame in an Enso stack trace.
|
||||
type Stack_Trace_Element
|
||||
## PRIVATE
|
||||
type Stack_Trace_Element name source_location
|
@ -0,0 +1,33 @@
|
||||
## Debug utilities.
|
||||
|
||||
## TEXT_ONLY
|
||||
|
||||
Places a breakpoint in the program's execution, dropping the user into an
|
||||
interactive debugging REPL.
|
||||
|
||||
From the REPL, the user is able to manipulate both the program state and
|
||||
its execution in an interactive fashion.
|
||||
|
||||
> Example
|
||||
Dropping into a debugging REPL during execution.
|
||||
|
||||
Debug.breakpoint
|
||||
breakpoint : Nothing
|
||||
breakpoint = @Builtin_Method "Debug.breakpoint"
|
||||
|
||||
## Evaluates the provided Enso code in the caller frame.
|
||||
|
||||
Arguments:
|
||||
- expression: The enso code to evaluate.
|
||||
|
||||
? Scoping
|
||||
The fact that expression is evaluated in the caller frame means that
|
||||
it has access to variables in the scope enclosing the call to
|
||||
Debug.eval.
|
||||
|
||||
> Example
|
||||
Evaluating the expression 1 + 1 and assigning it to a value.
|
||||
|
||||
result = Debug.eval "1 + 1"
|
||||
eval : Text -> Any
|
||||
eval expression = @Builtin_Method "Debug.eval"
|
@ -65,33 +65,3 @@ type Source_Location
|
||||
Return the source file corresponding to this location.
|
||||
file : File.File
|
||||
file = File.new this.prim_location.getSource.getPath
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Represents a single stack frame in an Enso stack trace.
|
||||
type Stack_Trace_Element
|
||||
## PRIVATE
|
||||
type Stack_Trace_Element name source_location
|
||||
|
||||
## ADVANCED
|
||||
UNSTABLE
|
||||
|
||||
Returns the execution stack trace of its call site. The ordering of the
|
||||
resulting vector is such that the top stack frame is the first element.
|
||||
Runtime.get_stack_trace : Vector.Vector Stack_Trace_Element
|
||||
Runtime.get_stack_trace =
|
||||
prim_stack = this.primitive_get_stack_trace
|
||||
stack_with_prims = Vector.Vector prim_stack
|
||||
stack = stack_with_prims.map here.wrap_primitive_stack_trace_element
|
||||
# drop this frame and the one from `Runtime.primitive_get_stack_trace`
|
||||
stack.drop_start 2
|
||||
|
||||
## PRIVATE
|
||||
Converts a primitive stack trace element into the regular one.
|
||||
wrap_primitive_stack_trace_element el =
|
||||
loc = case Polyglot.has_source_location el of
|
||||
True -> Source_Location (Polyglot.get_source_location el)
|
||||
False -> Nothing
|
||||
name = Polyglot.get_executable_name el
|
||||
Stack_Trace_Element name loc
|
||||
|
@ -0,0 +1,77 @@
|
||||
## An API for manual resource management.
|
||||
|
||||
## Resource provides an API for manual management of computation resources.
|
||||
|
||||
These include things like file handles, network sockets, and so on. This
|
||||
API is intended for use by library developers to provide higher-level and
|
||||
easier to use abstractions.
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Acquires a resource, performs an action on it, and destroys it safely,
|
||||
even in the presence of panics.
|
||||
|
||||
Arguments:
|
||||
- constructor: The computation responsible for acquiring the resource.
|
||||
- destructor: The computation responsible for destroying the resource
|
||||
once it is done being used.
|
||||
- action: The computation to perform on the acquired resource.
|
||||
bracket : Any -> (Any -> Nothing) -> (Any -> Any) -> Any
|
||||
bracket ~constructor ~destructor ~action = @Builtin_Method "Resource.bracket"
|
||||
|
||||
## An API for automatic resource management.
|
||||
type Managed_Resource
|
||||
|
||||
## A managed resource is a special type of resource that is subject to
|
||||
automated cleanup when it is no longer in use.
|
||||
|
||||
This API is intended for use by developers to provide easy-to-use
|
||||
abstractions, and is not expected to be used by end-users.
|
||||
@Builtin_Type
|
||||
type Managed_Resource
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Registers a resource with the resource manager to be cleaned up using
|
||||
function once it is no longer in use.
|
||||
|
||||
Arguments:
|
||||
- resource: The resource to be managed automatically.
|
||||
- function: The action to be executed on resource to clean it up when
|
||||
it is no longer in use.
|
||||
register : Any -> (Any -> Nothing) -> Managed_Resource
|
||||
register resource function = @Builtin_Method "Managed_Resource.register"
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Forces finalization of a managed resource using the registered finalizer,
|
||||
even if the resource is still reachable.
|
||||
|
||||
Arguments:
|
||||
- resource: The resource that should be finalized.
|
||||
finalize : Managed_Resource -> Nothing
|
||||
finalize resource = @Builtin_Method "Managed_Resource.finalize"
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Executes the provided action on the resource managed by the managed
|
||||
resource object.
|
||||
|
||||
Arguments:
|
||||
- resource: The managed resource on which to run the action.
|
||||
- action: The action that will be applied to the resource managed by
|
||||
resource.
|
||||
with : Managed_Resource -> (Any -> Any) -> Any
|
||||
with resource ~action = @Builtin_Method "Managed_Resource.with"
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Takes the value held by the managed resource and unregisters the
|
||||
finalization step for this resource, effectively removing it from the
|
||||
managed resources system.
|
||||
|
||||
Arguments:
|
||||
- resource: The managed resource from which to acquire the underlying
|
||||
resource.
|
||||
take : Managed_Resource -> Any
|
||||
take resource = @Builtin_Method "Managed_Resource.take"
|
@ -0,0 +1,52 @@
|
||||
## The runtime's integrated monadic state management.
|
||||
## A container type for functionality for working with the runtime's
|
||||
integrated state functionality.
|
||||
|
||||
## Executes a stateful computation in a local state environment.
|
||||
|
||||
Arguments:
|
||||
- key: The key to associate your local_state with in the environment.
|
||||
It is recommended that types be used as keys.
|
||||
- local_state: The value to associate with key.
|
||||
- computation: The computation to execute in the local state
|
||||
environment.
|
||||
|
||||
> Example
|
||||
Print a value from the state.
|
||||
|
||||
State.run Integer 0 <| IO.println (State.get Integer)
|
||||
run : Any -> Any -> Any -> Any
|
||||
run key local_state ~computation = @Builtin_Method "State.run"
|
||||
|
||||
## Returns the current value for the provided key contained in the monadic
|
||||
state.
|
||||
|
||||
Arguments:
|
||||
- key: The key into the state to get the associated value for.
|
||||
|
||||
Returns an uninitialized state error if the user tries to read from an
|
||||
uninitialized slot.
|
||||
|
||||
> Example
|
||||
Get the value of state for a key.
|
||||
|
||||
State.get Decimal
|
||||
get : Any -> Any ! Uninitialized_State
|
||||
get key = @Builtin_Method "State.get"
|
||||
|
||||
## Associates a new_state with the provided key in the runtime's monadic
|
||||
state, returning the provided state.
|
||||
|
||||
Arguments:
|
||||
- key: The key with which to associate the new state.
|
||||
- new_state: The new state to store.
|
||||
|
||||
Returns an uninitialized state error if the user tries to read from an
|
||||
uninitialized slot.
|
||||
|
||||
> Example
|
||||
Store a new value in the state for a given key.
|
||||
|
||||
State.put Text 2821
|
||||
put : Any -> Any -> Any ! Uninitialized_State
|
||||
put key new_state = @Builtin_Method "State.put"
|
@ -0,0 +1,19 @@
|
||||
## Utilities for working with threads.
|
||||
## Internal threading utilities used for working with threads.
|
||||
|
||||
## ADVANCED
|
||||
|
||||
Executes an action with a handler for the executing thread being
|
||||
interrupted.
|
||||
|
||||
Arguments:
|
||||
- action: The action to execute.
|
||||
- interrupt_handler: The code to be executed if the thread is
|
||||
interrupted.
|
||||
|
||||
> Example
|
||||
Die on thread interrupts.
|
||||
|
||||
Thread.with_interrupt_handler (1 + 1) <| IO.println "I died!"
|
||||
with_interrupt_handler : Any -> Any -> Any
|
||||
with_interrupt_handler ~action ~interrupt_handler = @Builtin_Method "Thread.with_interrupt_handler"
|
@ -0,0 +1,14 @@
|
||||
## Unsafe operations.
|
||||
A container for unsafe operations that operate based on implementation
|
||||
details of the language.
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Sets the atom field at the provided index to have the provided value.
|
||||
|
||||
Arguments:
|
||||
- atom: The atom to set the field in.
|
||||
- index: The index of the field to set (zero-based).
|
||||
- value: The value to set the field at index to.
|
||||
set_atom_field : Atom -> Integer -> Any -> Atom
|
||||
set_atom_field atom index value = @Builtin_Method "Unsafe.set_atom_field"
|
58
distribution/lib/Standard/Base/0.0.0-dev/src/System.enso
Normal file
58
distribution/lib/Standard/Base/0.0.0-dev/src/System.enso
Normal file
@ -0,0 +1,58 @@
|
||||
## Functionality for interacting with the host system.
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Create a system process, returning the exit code, and the outputs to both
|
||||
standard out and standard error.
|
||||
|
||||
Arguments:
|
||||
- command: The name of the system process.
|
||||
- arguments: An array of arguments to the system process.
|
||||
- input: The input to pass to the process via standard input.
|
||||
- redirect_in: Specifies if the standard input of the program should be
|
||||
redirected to the started process.
|
||||
- redirect_out: Specifies if the standard output of the started process
|
||||
should be redirected to the program's standard output.
|
||||
- redirect_err: Specifies if the standard error output of the started
|
||||
process should be redirected to the program's standard error output.
|
||||
create_process : Text -> Array -> Text -> Boolean -> Boolean -> Boolean -> System_Process_Result
|
||||
create_process command arguments input redirect_in redirect_out redirect_err = @Builtin_Method "System.create_process"
|
||||
|
||||
## Exits the Enso program, returning the provided code to the parent
|
||||
process.
|
||||
|
||||
Arguments:
|
||||
- code: The numerical exit code for the Enso program.
|
||||
|
||||
> Example
|
||||
Exit the enso program with a failure.
|
||||
|
||||
System.exit 42
|
||||
exit : Integer -> Nothing
|
||||
exit code = @Builtin_Method "System.exit"
|
||||
|
||||
## Gets the nanosecond resolution system time at the moment of the call.
|
||||
|
||||
> Example
|
||||
Getting the current value of the nanosecond timer.
|
||||
|
||||
System.nano_time
|
||||
nano_time : Integer
|
||||
nano_time = @Builtin_Method "System.nano_time"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Get the name of the current platform upon which the program is running.
|
||||
os : Text
|
||||
os = @Builtin_Method "System.os"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
The type representing the result of a subprocess exiting.
|
||||
|
||||
Arguments:
|
||||
- exit_code: The exit code of the child process.
|
||||
- stdout: Any values printed to standard out by the child process.
|
||||
- stderr: Any values printed to standard error by the child process.
|
||||
@Builtin_Type
|
||||
type System_Process_Result exit_code stdout stderr
|
@ -5,6 +5,7 @@ import Standard.Base.Data.Text.Matching_Mode
|
||||
import Standard.Base.Data.Text.Text_Sub_Range
|
||||
from Standard.Base.Data.Text.Encoding as Encoding_Module import Encoding
|
||||
from Standard.Base.Error.Problem_Behavior as Problem_Behavior_Module import Problem_Behavior, Report_Warning
|
||||
from Standard.Base.Runtime.Resource import all
|
||||
|
||||
export Standard.Base.System.File.Option
|
||||
|
||||
@ -32,7 +33,7 @@ polyglot java import java.nio.file.Path
|
||||
example_new = File.new Examples.csv_path
|
||||
new : (Text | File) -> File
|
||||
new path = case path of
|
||||
Text -> File (Prim_Io.get_file path)
|
||||
Text -> File (here.get_file path)
|
||||
_ -> path
|
||||
|
||||
## Open and reads all bytes in the file at the provided `path` into a byte vector.
|
||||
@ -129,7 +130,7 @@ write_text path contents (encoding=Encoding.utf_8) =
|
||||
|
||||
example_cwd = File.current_directory
|
||||
current_directory : File
|
||||
current_directory = File (Prim_Io.get_cwd)
|
||||
current_directory = File (here.get_cwd)
|
||||
|
||||
## ALIAS Home Directory
|
||||
|
||||
@ -142,7 +143,7 @@ current_directory = File (Prim_Io.get_cwd)
|
||||
|
||||
example_home = File.home
|
||||
home : File
|
||||
home = here.new (Prim_Io.get_user_home)
|
||||
home = here.new (here.get_user_home)
|
||||
|
||||
## Lists files contained in the provided directory.
|
||||
|
||||
@ -952,3 +953,25 @@ list_descendants file =
|
||||
False -> Nothing
|
||||
go file
|
||||
builder.to_vector
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Gets a file corresponding to the current working directory of the
|
||||
program.
|
||||
get_cwd : File
|
||||
get_cwd = @Builtin_Method "File.get_cwd"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Gets a file corresponding to the provided path.
|
||||
|
||||
Arguments:
|
||||
- path: The path to obtain a file at.
|
||||
get_file : Text -> File
|
||||
get_file path = @Builtin_Method "File.get_file"
|
||||
|
||||
## PRIVATE
|
||||
|
||||
Gets the textual path to the user's system-defined home directory.
|
||||
user_home : Text
|
||||
user_home = @Builtin_Method "File.user_home"
|
||||
|
@ -1,6 +1,4 @@
|
||||
from Standard.Base import all
|
||||
|
||||
from Standard.Builtins import System
|
||||
import Standard.Base.System
|
||||
|
||||
## A representation of the various operating systems on which Enso can run.
|
||||
type Os
|
||||
|
@ -1,9 +1,9 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Base.System
|
||||
|
||||
import Standard.Base.System.Process.Exit_Code
|
||||
|
||||
from Standard.Base.Data.Vector import Vector
|
||||
from Standard.Builtins import Array, System, True, False
|
||||
|
||||
## ALIAS Run a Command
|
||||
UNSTABLE
|
||||
@ -128,4 +128,3 @@ type Builder
|
||||
- stdout: The contents of the process' standard output.
|
||||
- stderr: The contents of the process' standard error.
|
||||
type Result exit_code stdout stderr
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
from Standard.Base import all
|
||||
from Standard.Base.Runtime import Stack_Trace_Element
|
||||
|
||||
## A representation of a dataflow warning attached to a value.
|
||||
type Warning
|
||||
@ -188,3 +189,37 @@ merge_matched_warnings value matcher merger =
|
||||
new_warnings = merger (result.second.map .value)
|
||||
new_warnings.fold result.first acc-> warning->
|
||||
Warning.attach warning acc
|
||||
|
||||
## PRIVATE
|
||||
type Prim_Warning
|
||||
|
||||
## PRIVATE
|
||||
type Prim_Warning
|
||||
|
||||
## PRIVATE
|
||||
attach : Any -> Any -> Any -> Any
|
||||
attach value warning origin = @Builtin_Method "Prim_Warning.attach"
|
||||
|
||||
## PRIVATE
|
||||
create : Any -> Any -> Prim_Warning
|
||||
create payload origin = @Builtin_Method "Prim_Warning.create"
|
||||
|
||||
## PRIVATE
|
||||
get_all : Any -> Array Prim_Warning
|
||||
get_all value = @Builtin_Method "Prim_Warning.get_all"
|
||||
|
||||
## PRIVATE
|
||||
set : Any -> Array Prim_Warning -> Any
|
||||
set value warnings = @Builtin_Method "Prim_Warning.set"
|
||||
|
||||
## PRIVATE
|
||||
get_origin : Prim_Warning -> Any
|
||||
get_origin warn = @Builtin_Method "Prim_Warning.get_origin"
|
||||
|
||||
## PRIVATE
|
||||
get_value : Prim_Warning -> Any
|
||||
get_value warn = @Builtin_Method "Prim_Warning.get_value"
|
||||
|
||||
## PRIVATE
|
||||
get_reassignments : Prim_Warning -> Any
|
||||
get_reassignments warn = @Builtin_Method "Prim_Warning.get_reassignments"
|
||||
|
@ -1,5 +1,7 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Runtime.Resource
|
||||
|
||||
import Standard.Database.Data.Dialect
|
||||
import Standard.Database.Data.Internal.IR
|
||||
import Standard.Database.Data.Sql
|
||||
|
@ -1,6 +1,6 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Error.Extensions as Errors
|
||||
import Standard.Base.Error.Common as Errors
|
||||
import Standard.Table.Data.Aggregate_Column
|
||||
import Standard.Database.Data.Sql
|
||||
import Standard.Database.Data.Dialect.Postgres
|
||||
|
@ -8,8 +8,6 @@
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Error.Extensions as Error
|
||||
|
||||
## ALIAS Text Input
|
||||
|
||||
Creating text in Enso is as simple as adding a node that contains the text
|
||||
|
@ -1,7 +1,7 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Table
|
||||
|
||||
import Standard.Base.Error.Extensions as Errors
|
||||
import Standard.Base.Error.Common as Errors
|
||||
from Standard.Base.Error.Problem_Behavior as Problem_Behavior_Module import Problem_Behavior
|
||||
from Standard.Table.Error as Table_Errors import Invalid_Row, Mismatched_Quote, Parser_Error, Additional_Invalid_Rows
|
||||
from Standard.Base.Data.Text.Encoding as Encoding_Module import Encoding
|
||||
|
@ -1,4 +1,5 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Base.Runtime.Unsafe
|
||||
|
||||
## Creates a new Unique_Name_Strategy instance.
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
from Standard.Base import all
|
||||
|
||||
from Standard.Builtins import Array
|
||||
|
||||
## PRIVATE
|
||||
|
||||
An efficient builder for concatenating vectors.
|
||||
|
@ -1,7 +1,7 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Table
|
||||
|
||||
import Standard.Base.Error.Extensions as Errors
|
||||
import Standard.Base.Error.Common as Errors
|
||||
from Standard.Base.Error.Problem_Behavior as Problem_Behavior_Module import Problem_Behavior
|
||||
from Standard.Base.Data.Text.Encoding as Encoding_Module import Encoding
|
||||
import Standard.Table.Internal.Delimited_Reader
|
||||
|
@ -1,4 +1,5 @@
|
||||
from Standard.Base import all
|
||||
import Standard.Base.System
|
||||
|
||||
## Measure the amount of time it takes to execute a given computation.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Builtins
|
||||
import Standard.Base.Runtime.State
|
||||
import Standard.Base.System
|
||||
|
||||
## Creates a new test group, describing properties of the object
|
||||
described by `this`.
|
||||
|
@ -40,8 +40,8 @@ prepare_visualization x = Helpers.recover_errors <|
|
||||
types it will return `Nothing`.
|
||||
find_expected_enso_type_for_sql : Sql_Type -> Text
|
||||
find_expected_enso_type_for_sql sql_type =
|
||||
if sql_type.is_definitely_integer then "Standard.Builtins.Main.Integer" else
|
||||
if sql_type.is_definitely_double then "Standard.Builtins.Main.Decimal" else
|
||||
if sql_type.is_definitely_text then "Standard.Builtins.Main.Text" else
|
||||
if sql_type.is_definitely_boolean then "Standard.Builtins.Main.Boolean" else
|
||||
if sql_type.is_definitely_integer then "Standard.Base.Data.Numbers.Integer" else
|
||||
if sql_type.is_definitely_double then "Standard.Base.Data.Numbers.Decimal" else
|
||||
if sql_type.is_definitely_text then "Standard.Base.Data.Text.Text" else
|
||||
if sql_type.is_definitely_boolean then "Standard.Base.Boolean.Boolean" else
|
||||
Nothing
|
||||
|
@ -217,7 +217,7 @@ Enso has a concept of _extension methods_. These are methods that are _not_
|
||||
defined "alongside" the type (in the same compilation unit). Currently, we have
|
||||
no way to define methods that are _not_ extensions on builtin types without
|
||||
defining them in Java. This is awkward, and leads to a poor experience for both
|
||||
developers of Enso, and the users (where there is a special case rule rule for
|
||||
developers of Enso, and the users (where there is a special case rule for
|
||||
certain types, and also a hacky form of documentation for these same types).
|
||||
|
||||
For types defined in Java, their methods defined in Enso are extensions and are
|
||||
|
@ -57,9 +57,9 @@ class DocSectionsBuilderTest extends AnyWordSpec with Matchers {
|
||||
| > Example
|
||||
| Throwing an error to show that something is unimplemented.
|
||||
|
|
||||
| import Standard.Base.Error.Extensions
|
||||
| import Standard.Base.Error.Common as Errors
|
||||
|
|
||||
| example_unimplemented = Extensions.unimplemented
|
||||
| example_unimplemented = Errors.unimplemented
|
||||
|""".stripMargin.linesIterator.mkString("\n")
|
||||
val expected = Seq(
|
||||
DocSection.Tag("ADVANCED", ""),
|
||||
@ -73,7 +73,7 @@ class DocSectionsBuilderTest extends AnyWordSpec with Matchers {
|
||||
DocSection.Marked(
|
||||
DocSection.Mark.Example(),
|
||||
Some("Example"),
|
||||
" Throwing an error to show that something is unimplemented. <pre><code>import Standard.Base.Error.Extensions</code><br /><code>example_unimplemented = Extensions.unimplemented</code><br /></pre>"
|
||||
" Throwing an error to show that something is unimplemented. <pre><code>import Standard.Base.Error.Common as Errors</code><br /><code>example_unimplemented = Errors.unimplemented</code><br /></pre>"
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -292,7 +292,7 @@ class UpgradeSpec
|
||||
val script = getTestDirectory / "script.enso"
|
||||
val message = "Hello from test"
|
||||
val content =
|
||||
s"""from Standard.Builtins import all
|
||||
s"""import Standard.Base.IO
|
||||
|main = IO.println "$message"
|
||||
|""".stripMargin
|
||||
FileSystem.writeTextFile(script, content)
|
||||
|
@ -1,26 +1,21 @@
|
||||
package org.enso.interpreter.bench.fixtures.semantic
|
||||
|
||||
import org.enso.interpreter.test.DefaultInterpreterRunner
|
||||
import org.enso.interpreter.runtime.builtin.Builtins
|
||||
import org.graalvm.polyglot.Value
|
||||
|
||||
class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val million: Long = 1000000
|
||||
|
||||
def buildInputList(length: Long): Value = {
|
||||
val builtins =
|
||||
interpreterContext.executionContext.getTopScope
|
||||
.getModule(Builtins.MODULE_NAME)
|
||||
val nil = builtins.getConstructor("Nil")
|
||||
val cons = builtins.getConstructor("Cons")
|
||||
1L.to(length).foldLeft(nil.newInstance()) { case (tail, el) =>
|
||||
cons.newInstance(el.asInstanceOf[Object], tail)
|
||||
}
|
||||
}
|
||||
val millionElementList = buildInputList(million)
|
||||
val millionElementList = eval(
|
||||
s"""|from Standard.Base.Data.List import Cons,Nil
|
||||
|from Standard.Base.Data.Numbers import all
|
||||
|
|
||||
|main =
|
||||
| res = (1.up_to $million).fold Nil (acc -> x -> Cons x acc)
|
||||
| res
|
||||
""".stripMargin)
|
||||
|
||||
val generateListCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|main = length ->
|
||||
| generator = acc -> i -> if i == 0 then acc else @Tail_Call generator (Cons i acc) (i - 1)
|
||||
@ -31,18 +26,18 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val generateList = getMain(generateListCode)
|
||||
|
||||
val generateListQualifiedCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|main = length ->
|
||||
| generator = acc -> i -> if i == 0 then acc else @Tail_Call generator (Builtins.cons i acc) (i - 1)
|
||||
| generator = acc -> i -> if i == 0 then acc else @Tail_Call generator (List.cons i acc) (i - 1)
|
||||
|
|
||||
| res = generator Builtins.nil length
|
||||
| res = generator List.nil length
|
||||
| res
|
||||
""".stripMargin
|
||||
val generateListQualified = getMain(generateListQualifiedCode)
|
||||
|
||||
val reverseListCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|main = list ->
|
||||
| reverser = acc -> list -> case list of
|
||||
@ -55,21 +50,21 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val reverseList = getMain(reverseListCode)
|
||||
|
||||
val reverseListMethodsCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|Cons.reverse = acc -> case this of
|
||||
| Cons h t -> @Tail_Call t.reverse (Cons h acc)
|
||||
|Cons.rev = acc -> case this of
|
||||
| Cons h t -> @Tail_Call t.rev (Cons h acc)
|
||||
|
|
||||
|Nil.reverse = acc -> acc
|
||||
|Nil.rev = acc -> acc
|
||||
|
|
||||
|main = list ->
|
||||
| res = list.reverse Nil
|
||||
| res = list.rev Nil
|
||||
| res
|
||||
|""".stripMargin
|
||||
val reverseListMethods = getMain(reverseListMethodsCode)
|
||||
|
||||
val sumListCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|main = list ->
|
||||
| summator = acc -> list -> case list of
|
||||
@ -82,7 +77,7 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val sumList = getMain(sumListCode)
|
||||
|
||||
val sumListLeftFoldCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|main = list ->
|
||||
| fold = f -> acc -> list -> case list of
|
||||
@ -95,7 +90,7 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val sumListLeftFold = getMain(sumListLeftFoldCode)
|
||||
|
||||
val sumListFallbackCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|main = list ->
|
||||
| summator = acc -> list -> case list of
|
||||
@ -108,7 +103,7 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val sumListFallback = getMain(sumListFallbackCode)
|
||||
|
||||
val sumListMethodsCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|Nil.sum = acc -> acc
|
||||
|Cons.sum = acc -> case this of
|
||||
@ -121,7 +116,7 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val sumListMethods = getMain(sumListMethodsCode)
|
||||
|
||||
val mapReverseListCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|Nil.mapReverse = f -> acc -> acc
|
||||
|Cons.mapReverse = f -> acc -> case this of
|
||||
@ -134,7 +129,7 @@ class AtomFixtures extends DefaultInterpreterRunner {
|
||||
val mapReverseList = getMain(mapReverseListCode)
|
||||
|
||||
val mapReverseListCurryCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.List import all
|
||||
|
|
||||
|Nil.mapReverse = f -> acc -> acc
|
||||
|Cons.mapReverse = f -> acc -> case this of
|
||||
|
@ -7,7 +7,7 @@ class CallableFixtures extends DefaultInterpreterRunner {
|
||||
|
||||
val sumTCOfromCallCode =
|
||||
"""
|
||||
|from Standard.Builtins import all
|
||||
|from Standard.Base.Data.Numbers import all
|
||||
|
|
||||
|type Foo
|
||||
|
|
||||
|
@ -49,7 +49,8 @@ class RecursionFixtures extends DefaultInterpreterRunner {
|
||||
val oversaturatedRecursiveCall = getMain(oversaturatedRecursiveCallTCOCode)
|
||||
|
||||
val sumStateTCOCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.Numbers import Number
|
||||
|import Standard.Base.Runtime.State
|
||||
|
|
||||
|stateSum = n ->
|
||||
| acc = State.get Number
|
||||
@ -63,7 +64,7 @@ class RecursionFixtures extends DefaultInterpreterRunner {
|
||||
val sumStateTCO = getMain(sumStateTCOCode)
|
||||
|
||||
val sumTCOWithEvalCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""import Standard.Base.Runtime.Debug
|
||||
|
|
||||
|main = sumTo ->
|
||||
| summator = acc -> current ->
|
||||
@ -75,7 +76,9 @@ class RecursionFixtures extends DefaultInterpreterRunner {
|
||||
val sumTCOWithEval = getMain(sumTCOWithEvalCode)
|
||||
|
||||
val nestedThunkSumCode =
|
||||
"""from Standard.Builtins import all
|
||||
"""from Standard.Base.Data.Numbers import Number
|
||||
|import Standard.Base.Runtime.State
|
||||
|import Standard.Base.Nothing
|
||||
|
|
||||
|doNTimes = n -> ~block ->
|
||||
| block
|
||||
|
@ -25,7 +25,7 @@ public class BaseResolverNode extends Node {
|
||||
|
||||
@CompilerDirectives.TruffleBoundary
|
||||
Function resolveMethodOnError(UnresolvedSymbol symbol) {
|
||||
return symbol.resolveFor(getContext().getBuiltins().dataflowError().constructor());
|
||||
return symbol.resolveFor(getContext().getBuiltins().dataflowError());
|
||||
}
|
||||
|
||||
@CompilerDirectives.TruffleBoundary
|
||||
|
@ -8,7 +8,6 @@ import com.oracle.truffle.api.interop.InteropLibrary;
|
||||
import com.oracle.truffle.api.library.CachedLibrary;
|
||||
import com.oracle.truffle.api.nodes.NodeInfo;
|
||||
import com.oracle.truffle.api.profiles.ConditionProfile;
|
||||
import org.enso.interpreter.runtime.builtin.Bool;
|
||||
import org.enso.interpreter.runtime.callable.atom.Atom;
|
||||
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
|
||||
|
||||
@ -19,11 +18,15 @@ public abstract class BooleanConstructorBranchNode extends BranchNode {
|
||||
private final AtomConstructor falseCons;
|
||||
private final ConditionProfile profile = ConditionProfile.createCountingProfile();
|
||||
|
||||
BooleanConstructorBranchNode(Bool bool, RootCallTarget branch) {
|
||||
BooleanConstructorBranchNode(
|
||||
AtomConstructor bool,
|
||||
AtomConstructor trueAtom,
|
||||
AtomConstructor falseAtom,
|
||||
RootCallTarget branch) {
|
||||
super(branch);
|
||||
this.boolCons = bool.getBool();
|
||||
this.trueCons = bool.getTrue();
|
||||
this.falseCons = bool.getFalse();
|
||||
this.boolCons = bool;
|
||||
this.trueCons = trueAtom;
|
||||
this.falseCons = falseAtom;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,8 +36,12 @@ public abstract class BooleanConstructorBranchNode extends BranchNode {
|
||||
* @param branch the expression to be executed if (@code matcher} matches
|
||||
* @return a node for matching in a case expression
|
||||
*/
|
||||
public static BooleanConstructorBranchNode build(Bool bool, RootCallTarget branch) {
|
||||
return BooleanConstructorBranchNodeGen.create(bool, branch);
|
||||
public static BooleanConstructorBranchNode build(
|
||||
AtomConstructor bool,
|
||||
AtomConstructor trueAtom,
|
||||
AtomConstructor falseAtom,
|
||||
RootCallTarget branch) {
|
||||
return BooleanConstructorBranchNodeGen.create(bool, trueAtom, falseAtom, branch);
|
||||
}
|
||||
|
||||
@Specialization
|
||||
|
@ -93,12 +93,7 @@ public abstract class CaseNode extends ExpressionNode {
|
||||
}
|
||||
CompilerDirectives.transferToInterpreter();
|
||||
throw new PanicException(
|
||||
Context.get(this)
|
||||
.getBuiltins()
|
||||
.error()
|
||||
.inexhaustivePatternMatchError()
|
||||
.newInstance(object),
|
||||
this);
|
||||
Context.get(this).getBuiltins().error().makeInexhaustivePatternMatchError(object), this);
|
||||
} catch (BranchSelectedException e) {
|
||||
// Note [Branch Selection Control Flow]
|
||||
frame.setObject(getStateFrameSlot(), e.getResult().getState());
|
||||
|
@ -0,0 +1,6 @@
|
||||
package org.enso.interpreter.node.expression.builtin;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
|
||||
@BuiltinType(name = "Standard.Base.Any.Any")
|
||||
public class Any extends Builtin {}
|
@ -0,0 +1,12 @@
|
||||
package org.enso.interpreter.node.expression.builtin;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
|
||||
// Note that Boolean BuiltinType cannot be moved to `.expression.builtin.bool` package along with
|
||||
// True and False
|
||||
// because it currently breaks a lot of code generation for builtin methods.
|
||||
// The name Boolean would clash with java.lang.Boolean.
|
||||
// Before moving this definition to the `bool` package, as we should, one would have to address that
|
||||
// problem first.
|
||||
@BuiltinType(name = "Standard.Base.Data.Boolean.Boolean")
|
||||
public class Boolean extends Builtin {}
|
@ -0,0 +1,4 @@
|
||||
package org.enso.interpreter.node.expression.builtin;
|
||||
|
||||
/** A base class for all classes annotated with @BuiltinType. Temporarily a placeholder. */
|
||||
public abstract class Builtin {}
|
@ -0,0 +1,6 @@
|
||||
package org.enso.interpreter.node.expression.builtin;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
|
||||
@BuiltinType(name = "Standard.Base.Error.Common.Error")
|
||||
public class Error extends Builtin {}
|
@ -0,0 +1,6 @@
|
||||
package org.enso.interpreter.node.expression.builtin;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
|
||||
@BuiltinType(name = "Standard.Base.Nothing.Nothing")
|
||||
public class Nothing extends Builtin {}
|
@ -0,0 +1,6 @@
|
||||
package org.enso.interpreter.node.expression.builtin;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
|
||||
@BuiltinType
|
||||
public class Polyglot extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.bool;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType
|
||||
public class False extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.bool;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType
|
||||
public class True extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.debug;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType
|
||||
public class Debug extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"message"})
|
||||
public class ArithmeticError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"expected_min", "expected_max", "actual"})
|
||||
public class ArityError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"payload", "internal_original_exception"})
|
||||
public class CaughtPanic extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"message"})
|
||||
public class CompileError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"scrutinee"})
|
||||
public class InexhaustivePatternMatchError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"array", "index"})
|
||||
public class InvalidArrayIndexError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"target"})
|
||||
public class InvalidConversionTargetError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"name"})
|
||||
public class ModuleDoesNotExist extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType
|
||||
public class ModuleNotInPackageError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"target", "that", "conversion"})
|
||||
public class NoSuchConversionError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"target", "symbol"})
|
||||
public class NoSuchMethodError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"target"})
|
||||
public class NotInvokableError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(name = "Standard.Base.Error.Common.Panic")
|
||||
public class Panic extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"cause"})
|
||||
public class PolyglotError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"message"})
|
||||
public class SyntaxError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"expected", "actual", "name"})
|
||||
public class TypeError extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"key"})
|
||||
public class UninitializedState extends Builtin {}
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.error;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"arguments"})
|
||||
public class UnsupportedArgumentTypes extends Builtin {}
|
@ -11,7 +11,7 @@ import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
|
||||
import org.enso.interpreter.runtime.data.text.Text;
|
||||
import org.enso.interpreter.runtime.type.TypesGen;
|
||||
|
||||
@BuiltinMethod(type = "No_Such_Method_Error", name = "to_display_text")
|
||||
@BuiltinMethod(type = "No_Such_Conversion_Error", name = "to_display_text")
|
||||
public abstract class NoSuchConversionErrorToDisplayTextNode extends Node {
|
||||
static NoSuchConversionErrorToDisplayTextNode build() {
|
||||
return NoSuchConversionErrorToDisplayTextNodeGen.create();
|
||||
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.function;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(name = "Standard.Base.Function.Function")
|
||||
public class Function extends Builtin {}
|
@ -26,7 +26,7 @@ public class GetArraySizeNode extends Node {
|
||||
err.enter();
|
||||
Builtins builtins = Context.get(this).getBuiltins();
|
||||
throw new PanicException(
|
||||
builtins.error().makeTypeError(builtins.mutable().array(), array, "array"), this);
|
||||
builtins.error().makeTypeError(builtins.array(), array, "array"), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import org.enso.interpreter.runtime.Context;
|
||||
import org.enso.interpreter.runtime.data.EnsoFile;
|
||||
|
||||
@BuiltinMethod(
|
||||
type = "Prim_Io",
|
||||
type = "File",
|
||||
name = "get_cwd",
|
||||
description = "A file corresponding to the current working directory.")
|
||||
public abstract class GetCwdNode extends Node {
|
||||
|
@ -10,7 +10,7 @@ import org.enso.interpreter.runtime.Context;
|
||||
import org.enso.interpreter.runtime.data.EnsoFile;
|
||||
|
||||
@BuiltinMethod(
|
||||
type = "Prim_Io",
|
||||
type = "File",
|
||||
name = "get_file",
|
||||
description =
|
||||
"Takes the text representation of a path and returns a TruffleFile corresponding to it.")
|
||||
|
@ -5,7 +5,7 @@ import org.enso.interpreter.dsl.BuiltinMethod;
|
||||
import org.enso.interpreter.runtime.data.text.Text;
|
||||
|
||||
@BuiltinMethod(
|
||||
type = "Prim_Io",
|
||||
type = "File",
|
||||
name = "user_home",
|
||||
description = "Get the text path to the user home directory.")
|
||||
public final class GetUserHomeNode extends Node {
|
||||
|
@ -10,7 +10,7 @@ import org.enso.interpreter.runtime.data.text.Text;
|
||||
|
||||
@BuiltinMethod(
|
||||
type = "Meta",
|
||||
name = "get_source_location",
|
||||
name = "get_source_location_builtin",
|
||||
description = "Returns a textual representation of the location of the callsite.")
|
||||
public class GetSourceLocationNode extends Node {
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
package org.enso.interpreter.node.expression.builtin.meta;
|
||||
|
||||
import org.enso.interpreter.dsl.BuiltinType;
|
||||
import org.enso.interpreter.node.expression.builtin.Builtin;
|
||||
|
||||
@BuiltinType(params = {"prim_root_file", "prim_config"})
|
||||
public class ProjectDescription extends Builtin {}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user