Compare commits
46 Commits
changelog-update
...
lima
| Author | SHA1 | Date | |
|---|---|---|---|
| fd6cde535e | |||
| b25339bb8f | |||
| cf793667ca | |||
| c54a0cec3d | |||
| bc47c25138 | |||
| 3b2ce63fa7 | |||
| 8b4a1aaf0d | |||
| c6e7db2381 | |||
| 4e60d019ca | |||
| b8002029cf | |||
| 4630f8a09b | |||
| 1a14602f36 | |||
| e2ef95d6fd | |||
| 22aaeceba8 | |||
| f1d95484a5 | |||
| 7e65f26211 | |||
| 0b83422189 | |||
| 1a5017ce2b | |||
| 25fa365c29 | |||
| bb728db51b | |||
| 1fee306daa | |||
| c4eaf2249a | |||
| 6c23fd0d41 | |||
| 3d73e52d48 | |||
| 89b3ec3d17 | |||
| 7e32ef57c2 | |||
| ed5447e430 | |||
| db4de5d926 | |||
| 6b60fde2df | |||
| dd8eea0d55 | |||
| eb71abc665 | |||
| eff1ad4688 | |||
| cb2588fae2 | |||
| 08261a319b | |||
| f21717a9c0 | |||
| 9753f90034 | |||
| 8f240a7ddf | |||
| 54e43764ca | |||
| 962ddf5303 | |||
| 85b151aa65 | |||
| 93341dc13b | |||
| 98036eff65 | |||
| dc977f7354 | |||
| 4f554acee6 | |||
| 48b52cb501 | |||
| 515838e2f9 |
+25
-5
@@ -9,6 +9,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Changed
|
### Changed
|
||||||
### Removed
|
### Removed
|
||||||
|
|
||||||
|
## [4.3.0]
|
||||||
|
### Added
|
||||||
|
- Added documentation (moved from `protocol`)
|
||||||
|
- `Frac.aes` – library for rational numbers
|
||||||
|
- Added some more meaningful error messages
|
||||||
|
- Exported several parsing functionalities
|
||||||
|
- With option `keep_included` it is possible to see which files were included during the parse
|
||||||
|
- There is a function `run_parser` that be used to evaluate any parsing rule
|
||||||
|
- Exported parsers: `body`, `type` and `decl`
|
||||||
|
### Changed
|
||||||
|
- Performance improvements in the standard library
|
||||||
|
- Fixed ACI encoder to handle `-` unary operator
|
||||||
|
- Fixed including by absolute path
|
||||||
|
- Fixed variant type printing in the ACI error messages
|
||||||
|
- Fixed pretty printing of combined function clauses
|
||||||
|
### Removed
|
||||||
|
- `let` definitions are no longer supported in the toplevel of the contract
|
||||||
|
- type declarations are no longer supported
|
||||||
|
|
||||||
## [4.2.0] - 2020-01-15
|
## [4.2.0] - 2020-01-15
|
||||||
### Added
|
### Added
|
||||||
- Allow separate entrypoint/function type signature and definition, and pattern
|
- Allow separate entrypoint/function type signature and definition, and pattern
|
||||||
@@ -53,7 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
(de-)construct byte arrays.
|
(de-)construct byte arrays.
|
||||||
- `[a..b]` language construct, returning the list of numbers between
|
- `[a..b]` language construct, returning the list of numbers between
|
||||||
`a` and `b` (inclusive). Returns the empty list if `a` > `b`.
|
`a` and `b` (inclusive). Returns the empty list if `a` > `b`.
|
||||||
- [Standard libraries] (https://github.com/aeternity/protocol/blob/master/contracts/sophia_stdlib.md)
|
- [Standard libraries](https://github.com/aeternity/aesophia/blob/master/docs/sophia_stdlib.md)
|
||||||
- Checks that `init` is not called from other functions.
|
- Checks that `init` is not called from other functions.
|
||||||
- FATE backend - the compiler is able to produce VM code for both `AEVM` and `FATE`. Many
|
- FATE backend - the compiler is able to produce VM code for both `AEVM` and `FATE`. Many
|
||||||
of the APIs now take `{backend, aevm | fate}` to decide wich backend to produce artifacts
|
of the APIs now take `{backend, aevm | fate}` to decide wich backend to produce artifacts
|
||||||
@@ -192,10 +211,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- Simplify calldata creation - instead of passing a compiled contract, simply
|
- Simplify calldata creation - instead of passing a compiled contract, simply
|
||||||
pass a (stubbed) contract string.
|
pass a (stubbed) contract string.
|
||||||
|
|
||||||
[Unreleased]: https://github.com/aeternity/aesophia/compare/v4.2.0...HEAD
|
[Unreleased]: https://github.com/aeternity/aesophia/compare/v4.3.0...HEAD
|
||||||
[4.2.0]: https://github.com/aeternity/aesophia/compare/v4.2.0...v4.1.0
|
[4.3.0]: https://github.com/aeternity/aesophia/compare/v4.2.0...v4.3.0
|
||||||
[4.1.0]: https://github.com/aeternity/aesophia/compare/v4.1.0...v4.0.0
|
[4.2.0]: https://github.com/aeternity/aesophia/compare/v4.1.0...v4.2.0
|
||||||
[4.0.0]: https://github.com/aeternity/aesophia/compare/v4.0.0...v3.2.0
|
[4.1.0]: https://github.com/aeternity/aesophia/compare/v4.0.0...v4.1.0
|
||||||
|
[4.0.0]: https://github.com/aeternity/aesophia/compare/v3.2.0...v4.0.0
|
||||||
[3.2.0]: https://github.com/aeternity/aesophia/compare/v3.1.0...v3.2.0
|
[3.2.0]: https://github.com/aeternity/aesophia/compare/v3.1.0...v3.2.0
|
||||||
[3.1.0]: https://github.com/aeternity/aesophia/compare/v3.0.0...v3.1.0
|
[3.1.0]: https://github.com/aeternity/aesophia/compare/v3.0.0...v3.1.0
|
||||||
[3.0.0]: https://github.com/aeternity/aesophia/compare/v2.1.0...v3.0.0
|
[3.0.0]: https://github.com/aeternity/aesophia/compare/v2.1.0...v3.0.0
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
# aesophia
|
# aesophia
|
||||||
|
|
||||||
This is the __sophia__ compiler for the æternity system which compiles contracts written in __sophia__ code to the æternity VM code.
|
This is the __sophia__ compiler for the æternity system which compiles contracts written in __sophia__ to [FATE](https://github.com/aeternity/protocol/blob/master/contracts/fate.md) instructions.
|
||||||
|
|
||||||
It is an OTP application written in Erlang and is by default included in
|
|
||||||
[the æternity node](https://github.com/aeternity/epoch). However, it can
|
|
||||||
also be included in other systems to compile contracts coded in sophia which
|
|
||||||
can then be loaded into the æternity system.
|
|
||||||
|
|
||||||
|
The compiler is currently being used three places
|
||||||
|
- [The command line compiler](https://github.com/aeternity/aesophia_cli)
|
||||||
|
- [The HTTP compiler](https://github.com/aeternity/aesophia_http)
|
||||||
|
- In [Aeternity node](https://github.com/aeternity/aeternity) tests
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
@@ -14,7 +13,6 @@ can then be loaded into the æternity system.
|
|||||||
* [Sophia Documentation](docs/sophia.md).
|
* [Sophia Documentation](docs/sophia.md).
|
||||||
* [Sophia Standard Library](docs/sophia_stdlib.md).
|
* [Sophia Standard Library](docs/sophia_stdlib.md).
|
||||||
|
|
||||||
|
|
||||||
## Versioning
|
## Versioning
|
||||||
|
|
||||||
`aesophia` has a version that is only loosely connected to the version of the
|
`aesophia` has a version that is only loosely connected to the version of the
|
||||||
@@ -23,7 +21,6 @@ minor/patch version. The `aesophia` compiler version MUST be bumped whenever
|
|||||||
there is a change in how byte code is generated, but it MAY also be bumped upon
|
there is a change in how byte code is generated, but it MAY also be bumped upon
|
||||||
API changes etc.
|
API changes etc.
|
||||||
|
|
||||||
|
|
||||||
## Interface Modules
|
## Interface Modules
|
||||||
|
|
||||||
The basic modules for interfacing the compiler:
|
The basic modules for interfacing the compiler:
|
||||||
|
|||||||
+28
-21
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
**Table of Contents**
|
**Table of Contents**
|
||||||
|
|
||||||
- [-](#-)
|
|
||||||
- [Language Features](#language-features)
|
- [Language Features](#language-features)
|
||||||
- [Contracts](#contracts)
|
- [Contracts](#contracts)
|
||||||
- [Calling other contracts](#calling-other-contracts)
|
- [Calling other contracts](#calling-other-contracts)
|
||||||
@@ -53,8 +52,7 @@
|
|||||||
- [Operators types](#operators-types)
|
- [Operators types](#operators-types)
|
||||||
- [Operator precendences](#operator-precendences)
|
- [Operator precendences](#operator-precendences)
|
||||||
- [Examples](#examples)
|
- [Examples](#examples)
|
||||||
- [The lifetime of a contract](#the-lifetime-of-a-contract)
|
|
||||||
- [Killing a contract](#killing-a-contract)
|
|
||||||
|
|
||||||
## The Sophia Language
|
## The Sophia Language
|
||||||
An Æternity BlockChain Language
|
An Æternity BlockChain Language
|
||||||
@@ -80,7 +78,7 @@ The main unit of code in Sophia is the *contract*.
|
|||||||
- A contract may define a type `state` encapsulating its local
|
- A contract may define a type `state` encapsulating its local
|
||||||
state. When creating a new contract the `init` entrypoint is executed and the
|
state. When creating a new contract the `init` entrypoint is executed and the
|
||||||
state is initialized to its return value.
|
state is initialized to its return value.
|
||||||
|
|
||||||
The language offers some primitive functions to interact with the blockchain and contracts.
|
The language offers some primitive functions to interact with the blockchain and contracts.
|
||||||
Please refer to the [Chain](sophia_stdlib.md#Chain), [Contract](sophia_stdlib.md#Contract)
|
Please refer to the [Chain](sophia_stdlib.md#Chain), [Contract](sophia_stdlib.md#Contract)
|
||||||
and the [Call](sophia_stdlib.md#Call) namespaces in the documentation.
|
and the [Call](sophia_stdlib.md#Call) namespaces in the documentation.
|
||||||
@@ -281,7 +279,7 @@ so even cyclic includes should be working without any special tinkering.
|
|||||||
|
|
||||||
### Standard library
|
### Standard library
|
||||||
|
|
||||||
Sophia offers [standard library](sophia_stdlib.md) which exposes some
|
Sophia offers [standard library](sophia_stdlib.md) which exposes some
|
||||||
primitive operations and some higher level utilities. The builtin
|
primitive operations and some higher level utilities. The builtin
|
||||||
namespaces like `Chain`, `Contract`, `Map`
|
namespaces like `Chain`, `Contract`, `Map`
|
||||||
are included by default and are supported internally by the compiler.
|
are included by default and are supported internally by the compiler.
|
||||||
@@ -446,6 +444,13 @@ Example syntax:
|
|||||||
// yields [12,13,14,20,21,22,30,31,32]
|
// yields [12,13,14,20,21,22,30,31,32]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Lists can be constructed using the range syntax using special `..` operator:
|
||||||
|
```
|
||||||
|
[1..4] == [1,2,3,4]
|
||||||
|
```
|
||||||
|
The ranges are always ascending and have step equal to 1.
|
||||||
|
|
||||||
|
|
||||||
Please refer to the [standard library](sophia_stdlib.md#List) for the predefined functionalities.
|
Please refer to the [standard library](sophia_stdlib.md#List) for the predefined functionalities.
|
||||||
|
|
||||||
### Maps and records
|
### Maps and records
|
||||||
@@ -546,7 +551,7 @@ Please refer to the `Bytes` [library documentation](sophia_stdlib.md#Bytes).
|
|||||||
|
|
||||||
### Cryptographic builins
|
### Cryptographic builins
|
||||||
|
|
||||||
Libraries [Crypto](sophia_stdlib.md#Crypto) and [String](sophia_stdlib.md#String) provide functions to
|
Libraries [Crypto](sophia_stdlib.md#Crypto) and [String](sophia_stdlib.md#String) provide functions to
|
||||||
hash objects, verify signatures etc. The `hash` is a type alias for `bytes(32)`.
|
hash objects, verify signatures etc. The `hash` is a type alias for `bytes(32)`.
|
||||||
|
|
||||||
#### AEVM note
|
#### AEVM note
|
||||||
@@ -582,7 +587,7 @@ Example for an oracle answering questions of type `string` with answers of type
|
|||||||
contract Oracles =
|
contract Oracles =
|
||||||
|
|
||||||
stateful entrypoint registerOracle(acct : address,
|
stateful entrypoint registerOracle(acct : address,
|
||||||
sign : signature, // Signed oracle address + contract address
|
sign : signature, // Signed network id + oracle address + contract address
|
||||||
qfee : int,
|
qfee : int,
|
||||||
ttl : Chain.ttl) : oracle(string, int) =
|
ttl : Chain.ttl) : oracle(string, int) =
|
||||||
Oracle.register(acct, signature = sign, qfee, ttl)
|
Oracle.register(acct, signature = sign, qfee, ttl)
|
||||||
@@ -603,13 +608,13 @@ contract Oracles =
|
|||||||
Oracle.extend(o, ttl)
|
Oracle.extend(o, ttl)
|
||||||
|
|
||||||
stateful entrypoint signExtendOracle(o : oracle(string, int),
|
stateful entrypoint signExtendOracle(o : oracle(string, int),
|
||||||
sign : signature, // Signed oracle address + contract address
|
sign : signature, // Signed network id + oracle address + contract address
|
||||||
ttl : Chain.ttl) : unit =
|
ttl : Chain.ttl) : unit =
|
||||||
Oracle.extend(o, signature = sign, ttl)
|
Oracle.extend(o, signature = sign, ttl)
|
||||||
|
|
||||||
stateful entrypoint respond(o : oracle(string, int),
|
stateful entrypoint respond(o : oracle(string, int),
|
||||||
q : oracle_query(string, int),
|
q : oracle_query(string, int),
|
||||||
sign : signature, // Signed oracle query id + contract address
|
sign : signature, // Signed network id + oracle query id + contract address
|
||||||
r : int) =
|
r : int) =
|
||||||
Oracle.respond(o, q, signature = sign, r)
|
Oracle.respond(o, q, signature = sign, r)
|
||||||
|
|
||||||
@@ -630,7 +635,7 @@ contract Oracles =
|
|||||||
|
|
||||||
#### Sanity checks
|
#### Sanity checks
|
||||||
|
|
||||||
When an Oracle literal is passed to a contract, no deep checks are performed.
|
When an Oracle literal is passed to a contract, no deep checks are performed.
|
||||||
For extra safety [Oracle.check](sophia_stdlib.md#check) and [Oracle.check_query](sophia_stdlib.md#check_query)
|
For extra safety [Oracle.check](sophia_stdlib.md#check) and [Oracle.check_query](sophia_stdlib.md#check_query)
|
||||||
functions are provided.
|
functions are provided.
|
||||||
|
|
||||||
@@ -653,7 +658,7 @@ To use events a contract must declare a datatype `event`, and events are then
|
|||||||
logged using the `Chain.event` function:
|
logged using the `Chain.event` function:
|
||||||
|
|
||||||
```
|
```
|
||||||
datatype event
|
datatype event
|
||||||
= Event1(int, int, string)
|
= Event1(int, int, string)
|
||||||
| Event2(string, address)
|
| Event2(string, address)
|
||||||
|
|
||||||
@@ -681,8 +686,8 @@ will emit one Event of each kind in the example.
|
|||||||
|
|
||||||
```
|
```
|
||||||
entrypoint emit_events() : () =
|
entrypoint emit_events() : () =
|
||||||
Chain.event(TheFirstEvent(42))
|
Chain.event(Event1(42, 34, "foo"))
|
||||||
Chain.event(AnotherEvent(Contract.address, "This is not indexed"))
|
Chain.event(Event2("This is not indexed", Contract.address))
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Argument order
|
#### Argument order
|
||||||
@@ -825,15 +830,17 @@ In describing the syntax below, we use the following conventions:
|
|||||||
A Sophia file consists of a sequence of *declarations* in a layout block.
|
A Sophia file consists of a sequence of *declarations* in a layout block.
|
||||||
|
|
||||||
```c
|
```c
|
||||||
File ::= Block(Decl)
|
File ::= Block(TopDecl)
|
||||||
Decl ::= ['payable'] 'contract' Con '=' Block(Decl)
|
|
||||||
|
TopDecl ::= ['payable'] 'contract' Con '=' Block(Decl)
|
||||||
| 'namespace' Con '=' Block(Decl)
|
| 'namespace' Con '=' Block(Decl)
|
||||||
| '@compiler' PragmaOp Version
|
| '@compiler' PragmaOp Version
|
||||||
| 'include' String
|
| 'include' String
|
||||||
| 'type' Id ['(' TVar* ')'] ['=' TypeAlias]
|
|
||||||
|
Decl ::= 'type' Id ['(' TVar* ')'] '=' TypeAlias
|
||||||
| 'record' Id ['(' TVar* ')'] '=' RecordType
|
| 'record' Id ['(' TVar* ')'] '=' RecordType
|
||||||
| 'datatype' Id ['(' TVar* ')'] '=' DataType
|
| 'datatype' Id ['(' TVar* ')'] '=' DataType
|
||||||
| EModifier* ('entrypoint' | 'function') Block(FunDecl)
|
| (EModifier* 'entrypoint' | FModifier* 'function') Block(FunDecl)
|
||||||
|
|
||||||
FunDecl ::= Id ':' Type // Type signature
|
FunDecl ::= Id ':' Type // Type signature
|
||||||
| Id Args [':' Type] '=' Block(Stmt) // Definition
|
| Id Args [':' Type] '=' Block(Stmt) // Definition
|
||||||
@@ -945,6 +952,7 @@ Expr ::= '(' LamArgs ')' '=>' Block(Stmt) // Anonymous function (x) => x +
|
|||||||
| '[' Sep(Expr, ',') ']' // List [1, 2, 3]
|
| '[' Sep(Expr, ',') ']' // List [1, 2, 3]
|
||||||
| '[' Expr '|' Sep(Generator, ',') ']'
|
| '[' Expr '|' Sep(Generator, ',') ']'
|
||||||
// List comprehension [k | x <- [1], if (f(x)), let k = x+1]
|
// List comprehension [k | x <- [1], if (f(x)), let k = x+1]
|
||||||
|
| '[' Expr '..' Expr ']' // List range [1..n]
|
||||||
| '{' Sep(FieldUpdate, ',') '}' // Record or map value {x = 0, y = 1}, {[key] = val}
|
| '{' Sep(FieldUpdate, ',') '}' // Record or map value {x = 0, y = 1}, {[key] = val}
|
||||||
| '(' Expr ')' // Parens (1 + 2) * 3
|
| '(' Expr ')' // Parens (1 + 2) * 3
|
||||||
| Id | Con | QId | QCon // Identifiers x, None, Map.member, AELib.Token
|
| Id | Con | QId | QCon // Identifiers x, None, Map.member, AELib.Token
|
||||||
@@ -1012,8 +1020,8 @@ contract FundMe =
|
|||||||
deadline : int,
|
deadline : int,
|
||||||
goal : int }
|
goal : int }
|
||||||
|
|
||||||
function spend(args : spend_args) =
|
stateful function spend(args : spend_args) =
|
||||||
raw_spend(args.recipient, args.amount)
|
Chain.spend(args.recipient, args.amount)
|
||||||
|
|
||||||
entrypoint init(beneficiary, deadline, goal) : state =
|
entrypoint init(beneficiary, deadline, goal) : state =
|
||||||
{ contributions = {},
|
{ contributions = {},
|
||||||
@@ -1052,7 +1060,6 @@ contract FundMe =
|
|||||||
require(state.total >= state.goal, "Project was not funded")
|
require(state.total >= state.goal, "Project was not funded")
|
||||||
spend({recipient = state.beneficiary,
|
spend({recipient = state.beneficiary,
|
||||||
amount = Contract.balance })
|
amount = Contract.balance })
|
||||||
put(state{ beneficiary = #0 })
|
|
||||||
|
|
||||||
stateful function withdraw_contributor() =
|
stateful function withdraw_contributor() =
|
||||||
if(state.total >= state.goal)
|
if(state.total >= state.goal)
|
||||||
|
|||||||
+43
-17
@@ -369,7 +369,7 @@ Registers new oracle answering questions of type `'a` with answers of type `'b`.
|
|||||||
|
|
||||||
* The `acct` is the address of the oracle to register (can be the same as the contract).
|
* The `acct` is the address of the oracle to register (can be the same as the contract).
|
||||||
* `signature` is a signature proving that the contract is allowed to register the account -
|
* `signature` is a signature proving that the contract is allowed to register the account -
|
||||||
the account address + the contract address (concatenated as byte arrays) is
|
the `network id` + `account address` + `contract address` (concatenated as byte arrays) is
|
||||||
signed with the
|
signed with the
|
||||||
private key of the account, proving you have the private key of the oracle to be. If the
|
private key of the account, proving you have the private key of the oracle to be. If the
|
||||||
address is the same as the contract `sign` is ignored and can be left out entirely.
|
address is the same as the contract `sign` is ignored and can be left out entirely.
|
||||||
@@ -403,7 +403,7 @@ Responds to the question `q` on `o`.
|
|||||||
Unless the contract address is the same as the oracle address the `signature`
|
Unless the contract address is the same as the oracle address the `signature`
|
||||||
(which is an optional, named argument)
|
(which is an optional, named argument)
|
||||||
needs to be provided. Proving that we have the private key of the oracle by
|
needs to be provided. Proving that we have the private key of the oracle by
|
||||||
signing the oracle query id + contract address
|
signing the `network id` + `oracle query id` + `contract address`
|
||||||
|
|
||||||
|
|
||||||
### extend
|
### extend
|
||||||
@@ -468,7 +468,8 @@ Naming System (AENS).
|
|||||||
If `owner` is equal to `Contract.address` the signature `signature` is
|
If `owner` is equal to `Contract.address` the signature `signature` is
|
||||||
ignored, and can be left out since it is a named argument. Otherwise we need
|
ignored, and can be left out since it is a named argument. Otherwise we need
|
||||||
a signature to prove that we are allowed to do AENS operations on behalf of
|
a signature to prove that we are allowed to do AENS operations on behalf of
|
||||||
`owner`
|
`owner`. The [signature is tied to a network id](https://github.com/aeternity/protocol/blob/iris/consensus/consensus.md#transaction-signature),
|
||||||
|
i.e. the signature material should be prefixed by the network id.
|
||||||
|
|
||||||
### resolve
|
### resolve
|
||||||
```
|
```
|
||||||
@@ -486,7 +487,7 @@ type checked against this type at run time.
|
|||||||
AENS.preclaim(owner : address, commitment_hash : hash, <signature : signature>) : unit
|
AENS.preclaim(owner : address, commitment_hash : hash, <signature : signature>) : unit
|
||||||
```
|
```
|
||||||
|
|
||||||
The signature should be over `owner address` + `Contract.address`
|
The signature should be over `network id` + `owner address` + `Contract.address`
|
||||||
(concatenated as byte arrays).
|
(concatenated as byte arrays).
|
||||||
|
|
||||||
|
|
||||||
@@ -495,29 +496,29 @@ The signature should be over `owner address` + `Contract.address`
|
|||||||
AENS.claim(owner : address, name : string, salt : int, name_fee : int, <signature : signature>) : unit
|
AENS.claim(owner : address, name : string, salt : int, name_fee : int, <signature : signature>) : unit
|
||||||
```
|
```
|
||||||
|
|
||||||
The signature should be over `owner address` + `name_hash` + `Contract.address`
|
The signature should be over `network id` + `owner address` + `name_hash` + `Contract.address`
|
||||||
using the private key of the `owner` account for signing.
|
using the private key of the `owner` account for signing.
|
||||||
|
|
||||||
|
|
||||||
### transfer
|
### transfer
|
||||||
```
|
```
|
||||||
AENS.transfer(owner : address, new_owner : address, name_hash : hash, <signature : signature>) : unit
|
AENS.transfer(owner : address, new_owner : address, name : string, <signature : signature>) : unit
|
||||||
```
|
```
|
||||||
|
|
||||||
Transfers name to the new owner.
|
Transfers name to the new owner.
|
||||||
|
|
||||||
The signature should be over `owner address` + `name_hash` + `Contract.address`
|
The signature should be over `network id` + `owner address` + `name_hash` + `Contract.address`
|
||||||
using the private key of the `owner` account for signing.
|
using the private key of the `owner` account for signing.
|
||||||
|
|
||||||
|
|
||||||
### revoke
|
### revoke
|
||||||
```
|
```
|
||||||
AENS.revoke(owner : address, name_hash : hash, <signature : signature>) : unit
|
AENS.revoke(owner : address, name : string, <signature : signature>) : unit
|
||||||
```
|
```
|
||||||
|
|
||||||
Revokes the name to extend the ownership time.
|
Revokes the name to extend the ownership time.
|
||||||
|
|
||||||
The signature should be over `owner address` + `name_hash` + `Contract.address`
|
The signature should be over `network id` + `owner address` + `name_hash` + `Contract.address`
|
||||||
using the private key of the `owner` account for signing.
|
using the private key of the `owner` account for signing.
|
||||||
|
|
||||||
|
|
||||||
@@ -568,12 +569,15 @@ Call.caller : address
|
|||||||
|
|
||||||
The address of the entity (possibly another contract) calling the contract.
|
The address of the entity (possibly another contract) calling the contract.
|
||||||
|
|
||||||
|
|
||||||
### value
|
### value
|
||||||
```
|
```
|
||||||
Call.value : int
|
Call.value : int
|
||||||
```
|
```
|
||||||
|
|
||||||
The amount of coins transferred to the contract in the call.
|
The amount of coins transferred to the contract in the call. Note that in the `init`
|
||||||
|
entrypoint this value will be always `0` – in order to get the contract creation value
|
||||||
|
one needs to inspect `Contract.balance`.
|
||||||
|
|
||||||
|
|
||||||
### gas
|
### gas
|
||||||
@@ -699,6 +703,13 @@ List.last(l : list('a)) : option('a)
|
|||||||
Returns `Some` of the last element of a list or `None` if the list is empty.
|
Returns `Some` of the last element of a list or `None` if the list is empty.
|
||||||
|
|
||||||
|
|
||||||
|
### contains
|
||||||
|
```
|
||||||
|
List.contains(e : 'a, l : list('a)) : bool
|
||||||
|
```
|
||||||
|
Checks if list `l` contains element `e`. Equivalent to `List.find(x => x == e, l) != None`.
|
||||||
|
|
||||||
|
|
||||||
### find
|
### find
|
||||||
```
|
```
|
||||||
List.find(p : 'a => bool, l : list('a)) : option('a)
|
List.find(p : 'a => bool, l : list('a)) : option('a)
|
||||||
@@ -967,12 +978,20 @@ List.unzip(l : list('a * 'b)) : list('a) * list('b)
|
|||||||
Opposite to the `zip` operation. Takes a list of pairs and returns pair of lists with respective elements on same indices.
|
Opposite to the `zip` operation. Takes a list of pairs and returns pair of lists with respective elements on same indices.
|
||||||
|
|
||||||
|
|
||||||
|
### merge
|
||||||
|
```
|
||||||
|
List.merge(lesser_cmp : ('a, 'a) => bool, l1 : list('a), l2 : list('a)) : list('a)
|
||||||
|
```
|
||||||
|
|
||||||
|
Merges two sorted lists into a single sorted list. O(length(l1) + length(l2))
|
||||||
|
|
||||||
|
|
||||||
### sort
|
### sort
|
||||||
```
|
```
|
||||||
List.sort(lesser_cmp : ('a, 'a) => bool, l : list('a)) : list('a)
|
List.sort(lesser_cmp : ('a, 'a) => bool, l : list('a)) : list('a)
|
||||||
```
|
```
|
||||||
|
|
||||||
Sorts a list using given comparator. `lesser_cmp(x, y)` should return `true` iff `x < y`. If `lesser_cmp` is not transitive or there exists an element `x` such that `lesser_cmp(x, x)` or there exists a pair of elements `x` and `y` such that `lesser_cmp(x, y) && lesser_cmp(y, x)` then the result is undefined. Currently O(n^2).
|
Sorts a list using given comparator. `lesser_cmp(x, y)` should return `true` iff `x < y`. If `lesser_cmp` is not transitive or there exists an element `x` such that `lesser_cmp(x, x)` or there exists a pair of elements `x` and `y` such that `lesser_cmp(x, y) && lesser_cmp(y, x)` then the result is undefined. O(length(l) * log_2(length(l))).
|
||||||
|
|
||||||
|
|
||||||
### intersperse
|
### intersperse
|
||||||
@@ -1039,6 +1058,13 @@ Option.force(o : option('a)) : 'a
|
|||||||
Forcefully escapes `option` wrapping assuming it is `Some`. Throws error on `None`.
|
Forcefully escapes `option` wrapping assuming it is `Some`. Throws error on `None`.
|
||||||
|
|
||||||
|
|
||||||
|
### contains
|
||||||
|
```
|
||||||
|
Option.contains(e : 'a, o : option('a)) : bool
|
||||||
|
```
|
||||||
|
Returns `true` if and only if `o` contains element equal to `e`. Equivalent to `Option.match(false, x => x == e, o)`.
|
||||||
|
|
||||||
|
|
||||||
### on_elem
|
### on_elem
|
||||||
```
|
```
|
||||||
Option.on_elem(o : option('a), f : 'a => unit) : unit
|
Option.on_elem(o : option('a), f : 'a => unit) : unit
|
||||||
@@ -1149,7 +1175,7 @@ will yield `None`.
|
|||||||
|
|
||||||
### choose
|
### choose
|
||||||
```
|
```
|
||||||
Option.choose(o1 : option('a), o2 : option('a)) : option('a)
|
Option.choose(o1 : option('a), o2 : option('a)) : option('a)
|
||||||
```
|
```
|
||||||
|
|
||||||
Out of two `option`s choose the one that is `Some`, or `None` if both are `None`s.
|
Out of two `option`s choose the one that is `Some`, or `None` if both are `None`s.
|
||||||
@@ -1261,7 +1287,7 @@ Func.curry2(f : ('a, 'b) => 'c) : 'a => ('b => 'c)
|
|||||||
Func.curry3(f : ('a, 'b, 'c) => 'd) : 'a => ('b => ('c => 'd))
|
Func.curry3(f : ('a, 'b, 'c) => 'd) : 'a => ('b => ('c => 'd))
|
||||||
```
|
```
|
||||||
|
|
||||||
Turns a function that takes n arguments into a curried function that takes
|
Turns a function that takes n arguments into a curried function that takes
|
||||||
one argument and returns a function that waits for the rest in the same
|
one argument and returns a function that waits for the rest in the same
|
||||||
manner. For instance `curry2((a, b) => a + b)(1)(2) == 3`.
|
manner. For instance `curry2((a, b) => a + b)(1)(2) == 3`.
|
||||||
|
|
||||||
@@ -1631,7 +1657,7 @@ This namespace provides operations on rational numbers. A rational number is rep
|
|||||||
as a fraction of two integers which are stored internally in the `frac` datatype.
|
as a fraction of two integers which are stored internally in the `frac` datatype.
|
||||||
|
|
||||||
The datatype consists of three constructors `Neg/2`, `Zero/0` and `Pos/2` which determine the
|
The datatype consists of three constructors `Neg/2`, `Zero/0` and `Pos/2` which determine the
|
||||||
sign of the number. Both values stored in `Neg` and `Pos` need to be strictly positive
|
sign of the number. Both values stored in `Neg` and `Pos` need to be strictly positive
|
||||||
integers. However, when creating a `frac` you should never use the constructors explicitly.
|
integers. However, when creating a `frac` you should never use the constructors explicitly.
|
||||||
Instead of that, always use provided functions like `make_frac` or `from_int`. This helps
|
Instead of that, always use provided functions like `make_frac` or `from_int`. This helps
|
||||||
keeping the internal representation well defined.
|
keeping the internal representation well defined.
|
||||||
@@ -1767,14 +1793,14 @@ Rounds a fraction to the nearest greater or equal integer.
|
|||||||
### round_to_zero
|
### round_to_zero
|
||||||
`Frac.round_to_zero(f : frac) : int`
|
`Frac.round_to_zero(f : frac) : int`
|
||||||
|
|
||||||
Rounds a fraction towards zero.
|
Rounds a fraction towards zero.
|
||||||
Effectively `ceil` if lesser than zero and `floor` if greater.
|
Effectively `ceil` if lesser than zero and `floor` if greater.
|
||||||
|
|
||||||
|
|
||||||
### round_from_zero
|
### round_from_zero
|
||||||
`Frac.round_from_zero(f : frac) : int`
|
`Frac.round_from_zero(f : frac) : int`
|
||||||
|
|
||||||
Rounds a fraction from zero.
|
Rounds a fraction from zero.
|
||||||
Effectively `ceil` if greater than zero and `floor` if lesser.
|
Effectively `ceil` if greater than zero and `floor` if lesser.
|
||||||
|
|
||||||
|
|
||||||
@@ -1806,7 +1832,7 @@ Subtraction of two fractions.
|
|||||||
### inv
|
### inv
|
||||||
`Frac.inv(a : frac) : frac`
|
`Frac.inv(a : frac) : frac`
|
||||||
|
|
||||||
Inverts a fraction. Throws error if `a` is zero.
|
Inverts a fraction. Throws error if `a` is zero.
|
||||||
|
|
||||||
|
|
||||||
### mul
|
### mul
|
||||||
|
|||||||
+14
-7
@@ -56,11 +56,14 @@ namespace Frac =
|
|||||||
/** Integer to rational division
|
/** Integer to rational division
|
||||||
*/
|
*/
|
||||||
function make_frac(n : int, d : int) : frac =
|
function make_frac(n : int, d : int) : frac =
|
||||||
if (d == 0) abort("Division by zero")
|
if (d == 0) abort("Zero denominator")
|
||||||
elif (n == 0) Zero
|
elif (n == 0) Zero
|
||||||
elif ((n < 0) == (d < 0)) simplify(Pos(abs_int(n), abs_int(d)))
|
elif ((n < 0) == (d < 0)) simplify(Pos(abs_int(n), abs_int(d)))
|
||||||
else simplify(Neg(abs_int(n), abs_int(d)))
|
else simplify(Neg(abs_int(n), abs_int(d)))
|
||||||
|
|
||||||
|
function one() : frac = Pos(1, 1)
|
||||||
|
function zero() : frac = Zero
|
||||||
|
|
||||||
function eq(a : frac, b : frac) : bool =
|
function eq(a : frac, b : frac) : bool =
|
||||||
let (na, da) = to_pair(a)
|
let (na, da) = to_pair(a)
|
||||||
let (nb, db) = to_pair(b)
|
let (nb, db) = to_pair(b)
|
||||||
@@ -146,7 +149,10 @@ namespace Frac =
|
|||||||
|
|
||||||
function mul(a : frac, b : frac) : frac = make_frac(num(a) * num(b), den(a) * den(b))
|
function mul(a : frac, b : frac) : frac = make_frac(num(a) * num(b), den(a) * den(b))
|
||||||
|
|
||||||
function div(a : frac, b : frac) : frac = mul(a, inv(b))
|
function div(a : frac, b : frac) : frac = switch(b)
|
||||||
|
Neg(n, d) => mul(a, Neg(d, n))
|
||||||
|
Zero => abort("Division by zero")
|
||||||
|
Pos(n, d) => mul(a, Pos(d, n))
|
||||||
|
|
||||||
/** `b` to the power of `e`
|
/** `b` to the power of `e`
|
||||||
*/
|
*/
|
||||||
@@ -168,9 +174,10 @@ namespace Frac =
|
|||||||
function optimize(f : frac, loss : frac) : frac =
|
function optimize(f : frac, loss : frac) : frac =
|
||||||
require(geq(loss, Zero), "negative loss optimize")
|
require(geq(loss, Zero), "negative loss optimize")
|
||||||
let s = sign(f)
|
let s = sign(f)
|
||||||
mul(from_int(s), run_optimize(abs(f), loss))
|
mul(from_int(s), run_optimize(abs(f), abs(f), loss))
|
||||||
private function run_optimize(f : frac, loss : frac) : frac =
|
private function run_optimize(orig : frac, f : frac, loss : frac) : frac =
|
||||||
let t = make_frac((num(f) + 1) / 2, (den(f) + 1)/2)
|
let (n, d) = to_pair(f)
|
||||||
if(gt(abs(sub(t, f)), loss)) f
|
let t = make_frac((n+1)/2, (d+1)/2)
|
||||||
|
if(gt(abs(sub(t, orig)), loss)) f
|
||||||
elif (eq(t, f)) f
|
elif (eq(t, f)) f
|
||||||
else run_optimize(t, loss)
|
else run_optimize(orig, t, loss)
|
||||||
|
|||||||
+67
-7
@@ -15,10 +15,14 @@ namespace List =
|
|||||||
_::t => Some(t)
|
_::t => Some(t)
|
||||||
|
|
||||||
function last(l : list('a)) : option('a) = switch(l)
|
function last(l : list('a)) : option('a) = switch(l)
|
||||||
[] => None
|
[] => None
|
||||||
[x] => Some(x)
|
[x] => Some(x)
|
||||||
_::t => last(t)
|
_::t => last(t)
|
||||||
|
|
||||||
|
function contains(e : 'a, l : list('a)) = switch(l)
|
||||||
|
[] => false
|
||||||
|
h::t => h == e || contains(e, t)
|
||||||
|
|
||||||
/** Finds first element of `l` fulfilling predicate `p` as `Some` or `None`
|
/** Finds first element of `l` fulfilling predicate `p` as `Some` or `None`
|
||||||
* if no such element exists.
|
* if no such element exists.
|
||||||
*/
|
*/
|
||||||
@@ -223,11 +227,67 @@ namespace List =
|
|||||||
(left, right)::t => unzip_(t, left::acc_l, right::acc_r)
|
(left, right)::t => unzip_(t, left::acc_l, right::acc_r)
|
||||||
|
|
||||||
|
|
||||||
// TODO: Improve?
|
/** Merges two sorted lists using `lt` comparator
|
||||||
function sort(lesser_cmp : ('a, 'a) => bool, l : list('a)) : list('a) = switch(l)
|
*/
|
||||||
[] => []
|
function
|
||||||
h::t => switch (partition((x) => lesser_cmp(x, h), t))
|
merge : (('a, 'a) => bool, list('a), list('a)) => list('a)
|
||||||
(lesser, bigger) => sort(lesser_cmp, lesser) ++ h::sort(lesser_cmp, bigger)
|
merge(lt, x::xs, y::ys) =
|
||||||
|
if(lt(x, y)) x::merge(lt, xs, y::ys)
|
||||||
|
else y::merge(lt, x::xs, ys)
|
||||||
|
merge(_, [], ys) = ys
|
||||||
|
merge(_, xs, []) = xs
|
||||||
|
|
||||||
|
|
||||||
|
/** Mergesort inspired by
|
||||||
|
* https://hackage.haskell.org/package/base-4.14.1.0/docs/src/Data.OldList.html#sort
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
sort : (('a, 'a) => bool, list('a)) => list('a)
|
||||||
|
sort(_, []) = []
|
||||||
|
sort(lt, l) =
|
||||||
|
merge_all(lt, monotonic_subs(lt, l))
|
||||||
|
|
||||||
|
/** Splits list into compound increasing sublists
|
||||||
|
*/
|
||||||
|
private function
|
||||||
|
monotonic_subs : (('a, 'a) => bool, list('a)) => list(list('a))
|
||||||
|
monotonic_subs(lt, x::y::rest) =
|
||||||
|
if(lt(y, x)) desc(lt, y, [x], rest)
|
||||||
|
else asc(lt, y, [x], rest)
|
||||||
|
monotonic_subs(_, l) = [l]
|
||||||
|
|
||||||
|
/** Extracts the longest descending prefix and proceeds with monotonic split
|
||||||
|
*/
|
||||||
|
private function
|
||||||
|
desc : (('a, 'a) => bool, 'a, list('a), list('a)) => list(list('a))
|
||||||
|
desc(lt, x, acc, h::t) =
|
||||||
|
if(lt(x, h)) (x::acc) :: monotonic_subs(lt, h::t)
|
||||||
|
else desc(lt, h, x::acc, t)
|
||||||
|
desc(_, x, acc, []) = [x::acc]
|
||||||
|
|
||||||
|
/** Extracts the longest ascending prefix and proceeds with monotonic split
|
||||||
|
*/
|
||||||
|
private function
|
||||||
|
asc : (('a, 'a) => bool, 'a, list('a), list('a)) => list(list('a))
|
||||||
|
asc(lt, x, acc, h::t) =
|
||||||
|
if(lt(h, x)) List.reverse(x::acc) :: monotonic_subs(lt, h::t)
|
||||||
|
else asc(lt, h, x::acc, t)
|
||||||
|
asc(_, x, acc, []) = [List.reverse(x::acc)]
|
||||||
|
|
||||||
|
/** Merges list of sorted lists
|
||||||
|
*/
|
||||||
|
private function
|
||||||
|
merge_all : (('a, 'a) => bool, list(list('a))) => list('a)
|
||||||
|
merge_all(_, [part]) = part
|
||||||
|
merge_all(lt, parts) = merge_all(lt, merge_pairs(lt, parts))
|
||||||
|
|
||||||
|
/** Single round of `merge_all` – pairs of lists in a list of list
|
||||||
|
*/
|
||||||
|
private function
|
||||||
|
merge_pairs : (('a, 'a) => bool, list(list('a))) => list(list('a))
|
||||||
|
merge_pairs(lt, x::y::rest) = merge(lt, x, y) :: merge_pairs(lt, rest)
|
||||||
|
merge_pairs(_, l) = l
|
||||||
|
|
||||||
|
|
||||||
/** Puts `delim` between every two members of the list
|
/** Puts `delim` between every two members of the list
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -22,7 +22,11 @@ namespace Option =
|
|||||||
|
|
||||||
/** Assume it is `Some`
|
/** Assume it is `Some`
|
||||||
*/
|
*/
|
||||||
function force(o : option('a)) : 'a = default(abort("Forced None value"), o)
|
function force(o : option('a)) : 'a = switch(o)
|
||||||
|
None => abort("Forced None value")
|
||||||
|
Some(x) => x
|
||||||
|
|
||||||
|
function contains(e : 'a, o : option('a)) = o == Some(e)
|
||||||
|
|
||||||
function on_elem(o : option('a), f : 'a => unit) : unit = match((), f, o)
|
function on_elem(o : option('a), f : 'a => unit) : unit = match((), f, o)
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@
|
|||||||
{base_plt_apps, [erts, kernel, stdlib, crypto, mnesia]}
|
{base_plt_apps, [erts, kernel, stdlib, crypto, mnesia]}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{relx, [{release, {aesophia, "4.2.0"},
|
{relx, [{release, {aesophia, "4.3.0"},
|
||||||
[aesophia, aebytecode, getopt]},
|
[aesophia, aebytecode, getopt]},
|
||||||
|
|
||||||
{dev_mode, true},
|
{dev_mode, true},
|
||||||
|
|||||||
+15
-7
@@ -14,6 +14,8 @@
|
|||||||
, contract_interface/2
|
, contract_interface/2
|
||||||
, contract_interface/3
|
, contract_interface/3
|
||||||
|
|
||||||
|
, from_typed_ast/2
|
||||||
|
|
||||||
, render_aci_json/1
|
, render_aci_json/1
|
||||||
|
|
||||||
, json_encode_expr/1
|
, json_encode_expr/1
|
||||||
@@ -23,6 +25,8 @@
|
|||||||
-type json() :: jsx:json_term().
|
-type json() :: jsx:json_term().
|
||||||
-type json_text() :: binary().
|
-type json_text() :: binary().
|
||||||
|
|
||||||
|
-export_type([aci_type/0]).
|
||||||
|
|
||||||
%% External API
|
%% External API
|
||||||
-spec file(aci_type(), string()) -> {ok, json() | string()} | {error, term()}.
|
-spec file(aci_type(), string()) -> {ok, json() | string()} | {error, term()}.
|
||||||
file(Type, File) ->
|
file(Type, File) ->
|
||||||
@@ -65,18 +69,20 @@ do_contract_interface(Type, ContractString, Options) ->
|
|||||||
try
|
try
|
||||||
Ast = aeso_compiler:parse(ContractString, Options),
|
Ast = aeso_compiler:parse(ContractString, Options),
|
||||||
%% io:format("~p\n", [Ast]),
|
%% io:format("~p\n", [Ast]),
|
||||||
TypedAst = aeso_ast_infer_types:infer(Ast, [dont_unfold]),
|
{TypedAst, _} = aeso_ast_infer_types:infer(Ast, [dont_unfold | Options]),
|
||||||
%% io:format("~p\n", [TypedAst]),
|
%% io:format("~p\n", [TypedAst]),
|
||||||
JArray = [ encode_contract(C) || C <- TypedAst ],
|
from_typed_ast(Type, TypedAst)
|
||||||
|
|
||||||
case Type of
|
|
||||||
json -> {ok, JArray};
|
|
||||||
string -> do_render_aci_json(JArray)
|
|
||||||
end
|
|
||||||
catch
|
catch
|
||||||
throw:{error, Errors} -> {error, Errors}
|
throw:{error, Errors} -> {error, Errors}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
from_typed_ast(Type, TypedAst) ->
|
||||||
|
JArray = [ encode_contract(C) || C <- TypedAst ],
|
||||||
|
case Type of
|
||||||
|
json -> {ok, JArray};
|
||||||
|
string -> do_render_aci_json(JArray)
|
||||||
|
end.
|
||||||
|
|
||||||
encode_contract(Contract = {contract, _, {con, _, Name}, _}) ->
|
encode_contract(Contract = {contract, _, {con, _, Name}, _}) ->
|
||||||
C0 = #{name => encode_name(Name)},
|
C0 = #{name => encode_name(Name)},
|
||||||
|
|
||||||
@@ -194,6 +200,8 @@ encode_expr({bytes, _, B}) ->
|
|||||||
encode_expr({Lit, _, L}) when Lit == oracle_pubkey; Lit == oracle_query_id;
|
encode_expr({Lit, _, L}) when Lit == oracle_pubkey; Lit == oracle_query_id;
|
||||||
Lit == contract_pubkey; Lit == account_pubkey ->
|
Lit == contract_pubkey; Lit == account_pubkey ->
|
||||||
aeser_api_encoder:encode(Lit, L);
|
aeser_api_encoder:encode(Lit, L);
|
||||||
|
encode_expr({app, _, {'-', _}, [{int, _, N}]}) ->
|
||||||
|
encode_expr({int, [], -N});
|
||||||
encode_expr({app, _, F, As}) ->
|
encode_expr({app, _, F, As}) ->
|
||||||
Ef = encode_expr(F),
|
Ef = encode_expr(F),
|
||||||
Eas = encode_exprs(As),
|
Eas = encode_exprs(As),
|
||||||
|
|||||||
+125
-42
@@ -12,7 +12,11 @@
|
|||||||
|
|
||||||
-module(aeso_ast_infer_types).
|
-module(aeso_ast_infer_types).
|
||||||
|
|
||||||
-export([infer/1, infer/2, unfold_types_in_type/3]).
|
-export([ infer/1
|
||||||
|
, infer/2
|
||||||
|
, unfold_types_in_type/3
|
||||||
|
, pp_type/2
|
||||||
|
]).
|
||||||
|
|
||||||
-type utype() :: {fun_t, aeso_syntax:ann(), named_args_t(), [utype()], utype()}
|
-type utype() :: {fun_t, aeso_syntax:ann(), named_args_t(), [utype()], utype()}
|
||||||
| {app_t, aeso_syntax:ann(), utype(), [utype()]}
|
| {app_t, aeso_syntax:ann(), utype(), [utype()]}
|
||||||
@@ -549,17 +553,21 @@ global_env() ->
|
|||||||
option_t(As, T) -> {app_t, As, {id, As, "option"}, [T]}.
|
option_t(As, T) -> {app_t, As, {id, As, "option"}, [T]}.
|
||||||
map_t(As, K, V) -> {app_t, As, {id, As, "map"}, [K, V]}.
|
map_t(As, K, V) -> {app_t, As, {id, As, "map"}, [K, V]}.
|
||||||
|
|
||||||
-spec infer(aeso_syntax:ast()) -> aeso_syntax:ast() | {env(), aeso_syntax:ast()}.
|
-spec infer(aeso_syntax:ast()) -> {aeso_syntax:ast(), aeso_syntax:ast()} | {env(), aeso_syntax:ast(), aeso_syntax:ast()}.
|
||||||
infer(Contracts) ->
|
infer(Contracts) ->
|
||||||
infer(Contracts, []).
|
infer(Contracts, []).
|
||||||
|
|
||||||
-type option() :: return_env | dont_unfold | no_code | term().
|
-type option() :: return_env | dont_unfold | no_code | debug_mode | term().
|
||||||
|
|
||||||
-spec init_env(list(option())) -> env().
|
-spec init_env(list(option())) -> env().
|
||||||
init_env(_Options) -> global_env().
|
init_env(_Options) -> global_env().
|
||||||
|
|
||||||
-spec infer(aeso_syntax:ast(), list(option())) ->
|
-spec infer(aeso_syntax:ast(), list(option())) ->
|
||||||
aeso_syntax:ast() | {env(), aeso_syntax:ast()}.
|
{aeso_syntax:ast(), aeso_syntax:ast()} | {env(), aeso_syntax:ast(), aeso_syntax:ast()}.
|
||||||
|
infer([], Options) ->
|
||||||
|
create_type_errors(),
|
||||||
|
type_error({no_decls, proplists:get_value(src_file, Options, no_file)}),
|
||||||
|
destroy_and_report_type_errors(init_env(Options));
|
||||||
infer(Contracts, Options) ->
|
infer(Contracts, Options) ->
|
||||||
ets_init(), %% Init the ETS table state
|
ets_init(), %% Init the ETS table state
|
||||||
try
|
try
|
||||||
@@ -568,15 +576,15 @@ infer(Contracts, Options) ->
|
|||||||
ets_new(type_vars, [set]),
|
ets_new(type_vars, [set]),
|
||||||
check_modifiers(Env, Contracts),
|
check_modifiers(Env, Contracts),
|
||||||
{Env1, Decls} = infer1(Env, Contracts, [], Options),
|
{Env1, Decls} = infer1(Env, Contracts, [], Options),
|
||||||
{Env2, Decls2} =
|
{Env2, DeclsFolded, DeclsUnfolded} =
|
||||||
case proplists:get_value(dont_unfold, Options, false) of
|
case proplists:get_value(dont_unfold, Options, false) of
|
||||||
true -> {Env1, Decls};
|
true -> {Env1, Decls, Decls};
|
||||||
false -> E = on_scopes(Env1, fun(Scope) -> unfold_record_types(Env1, Scope) end),
|
false -> E = on_scopes(Env1, fun(Scope) -> unfold_record_types(Env1, Scope) end),
|
||||||
{E, unfold_record_types(E, Decls)}
|
{E, Decls, unfold_record_types(E, Decls)}
|
||||||
end,
|
end,
|
||||||
case proplists:get_value(return_env, Options, false) of
|
case proplists:get_value(return_env, Options, false) of
|
||||||
false -> Decls2;
|
false -> {DeclsFolded, DeclsUnfolded};
|
||||||
true -> {Env2, Decls2}
|
true -> {Env2, DeclsFolded, DeclsUnfolded}
|
||||||
end
|
end
|
||||||
after
|
after
|
||||||
clean_up_ets()
|
clean_up_ets()
|
||||||
@@ -614,15 +622,23 @@ check_scope_name_clash(Env, Kind, Name) ->
|
|||||||
|
|
||||||
-spec infer_contract_top(env(), main_contract | contract | namespace, [aeso_syntax:decl()], list(option())) ->
|
-spec infer_contract_top(env(), main_contract | contract | namespace, [aeso_syntax:decl()], list(option())) ->
|
||||||
{env(), [aeso_syntax:decl()]}.
|
{env(), [aeso_syntax:decl()]}.
|
||||||
infer_contract_top(Env, Kind, Defs0, _Options) ->
|
infer_contract_top(Env, Kind, Defs0, Options) ->
|
||||||
|
create_type_errors(),
|
||||||
Defs = desugar(Defs0),
|
Defs = desugar(Defs0),
|
||||||
infer_contract(Env, Kind, Defs).
|
destroy_and_report_type_errors(Env),
|
||||||
|
infer_contract(Env, Kind, Defs, Options).
|
||||||
|
|
||||||
%% infer_contract takes a proplist mapping global names to types, and
|
%% infer_contract takes a proplist mapping global names to types, and
|
||||||
%% a list of definitions.
|
%% a list of definitions.
|
||||||
-spec infer_contract(env(), main_contract | contract | namespace, [aeso_syntax:decl()]) -> {env(), [aeso_syntax:decl()]}.
|
-spec infer_contract(env(), main_contract | contract | namespace, [aeso_syntax:decl()], list(option())) -> {env(), [aeso_syntax:decl()]}.
|
||||||
infer_contract(Env0, What, Defs0) ->
|
infer_contract(Env0, What, Defs0, Options) ->
|
||||||
Defs = process_blocks(Defs0),
|
create_type_errors(),
|
||||||
|
Defs01 = process_blocks(Defs0),
|
||||||
|
Defs = case lists:member(debug_mode, Options) of
|
||||||
|
true -> expose_internals(Defs01, What);
|
||||||
|
false -> Defs01
|
||||||
|
end,
|
||||||
|
destroy_and_report_type_errors(Env0),
|
||||||
Env = Env0#env{ what = What },
|
Env = Env0#env{ what = What },
|
||||||
Kind = fun({type_def, _, _, _, _}) -> type;
|
Kind = fun({type_def, _, _, _, _}) -> type;
|
||||||
({letfun, _, _, _, _, _}) -> function;
|
({letfun, _, _, _, _, _}) -> function;
|
||||||
@@ -669,20 +685,39 @@ process_blocks(Decls) ->
|
|||||||
-spec process_block(aeso_syntax:ann(), [aeso_syntax:decl()]) -> [aeso_syntax:decl()].
|
-spec process_block(aeso_syntax:ann(), [aeso_syntax:decl()]) -> [aeso_syntax:decl()].
|
||||||
process_block(_, []) -> [];
|
process_block(_, []) -> [];
|
||||||
process_block(_, [Decl]) -> [Decl];
|
process_block(_, [Decl]) -> [Decl];
|
||||||
process_block(Ann, [Decl | Decls]) ->
|
process_block(_Ann, [Decl | Decls]) ->
|
||||||
IsThis = fun(Name) -> fun({letfun, _, {id, _, Name1}, _, _, _}) -> Name == Name1;
|
IsThis = fun(Name) -> fun({letfun, _, {id, _, Name1}, _, _, _}) -> Name == Name1;
|
||||||
(_) -> false end end,
|
(_) -> false end end,
|
||||||
case Decl of
|
case Decl of
|
||||||
{fun_decl, Ann1, Id = {id, _, Name}, Type} ->
|
{fun_decl, Ann1, Id = {id, _, Name}, Type} ->
|
||||||
{Clauses, Rest} = lists:splitwith(IsThis(Name), Decls),
|
{Clauses, Rest} = lists:splitwith(IsThis(Name), Decls),
|
||||||
[{fun_clauses, Ann1, Id, Type, Clauses} |
|
[type_error({mismatched_decl_in_funblock, Name, D1}) || D1 <- Rest],
|
||||||
process_block(Ann, Rest)];
|
[{fun_clauses, Ann1, Id, Type, Clauses}];
|
||||||
{letfun, Ann1, Id = {id, _, Name}, _, _, _} ->
|
{letfun, Ann1, Id = {id, _, Name}, _, _, _} ->
|
||||||
{Clauses, Rest} = lists:splitwith(IsThis(Name), [Decl | Decls]),
|
{Clauses, Rest} = lists:splitwith(IsThis(Name), [Decl | Decls]),
|
||||||
[{fun_clauses, Ann1, Id, {id, [{origin, system} | Ann1], "_"}, Clauses} |
|
[type_error({mismatched_decl_in_funblock, Name, D1}) || D1 <- Rest],
|
||||||
process_block(Ann, Rest)]
|
[{fun_clauses, Ann1, Id, {id, [{origin, system} | Ann1], "_"}, Clauses}]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% Turns private stuff into public stuff
|
||||||
|
expose_internals(Defs, What) ->
|
||||||
|
[ begin
|
||||||
|
Ann = element(2, Def),
|
||||||
|
NewAnn = case What of
|
||||||
|
namespace -> [A ||A <- Ann, A /= {private, true}, A /= private];
|
||||||
|
main_contract -> [{entrypoint, true}|Ann]; % minor duplication
|
||||||
|
contract -> Ann
|
||||||
|
end,
|
||||||
|
Def1 = setelement(2, Def, NewAnn),
|
||||||
|
case Def1 of % fix inner clauses
|
||||||
|
{fun_clauses, Ans, Id, T, Clauses} ->
|
||||||
|
{fun_clauses, Ans, Id, T, expose_internals(Clauses, What)};
|
||||||
|
_ -> Def1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|| Def <- Defs
|
||||||
|
].
|
||||||
|
|
||||||
-spec check_typedefs(env(), [aeso_syntax:decl()]) -> {env(), [aeso_syntax:decl()]}.
|
-spec check_typedefs(env(), [aeso_syntax:decl()]) -> {env(), [aeso_syntax:decl()]}.
|
||||||
check_typedefs(Env = #env{ namespace = Ns }, Defs) ->
|
check_typedefs(Env = #env{ namespace = Ns }, Defs) ->
|
||||||
create_type_errors(),
|
create_type_errors(),
|
||||||
@@ -1522,6 +1557,15 @@ free_vars(L) when is_list(L) ->
|
|||||||
[V || Elem <- L,
|
[V || Elem <- L,
|
||||||
V <- free_vars(Elem)].
|
V <- free_vars(Elem)].
|
||||||
|
|
||||||
|
next_count() ->
|
||||||
|
V = case get(counter) of
|
||||||
|
undefined ->
|
||||||
|
0;
|
||||||
|
X -> X
|
||||||
|
end,
|
||||||
|
put(counter, V + 1),
|
||||||
|
V.
|
||||||
|
|
||||||
%% Clean up all the ets tables (in case of an exception)
|
%% Clean up all the ets tables (in case of an exception)
|
||||||
|
|
||||||
ets_tables() ->
|
ets_tables() ->
|
||||||
@@ -1567,6 +1611,18 @@ ets_tab2list(Name) ->
|
|||||||
TabId = ets_tabid(Name),
|
TabId = ets_tabid(Name),
|
||||||
ets:tab2list(TabId).
|
ets:tab2list(TabId).
|
||||||
|
|
||||||
|
ets_insert_ordered(_, []) -> true;
|
||||||
|
ets_insert_ordered(Name, [H|T]) ->
|
||||||
|
ets_insert_ordered(Name, H),
|
||||||
|
ets_insert_ordered(Name, T);
|
||||||
|
ets_insert_ordered(Name, Object) ->
|
||||||
|
Count = next_count(),
|
||||||
|
TabId = ets_tabid(Name),
|
||||||
|
ets:insert(TabId, {Count, Object}).
|
||||||
|
|
||||||
|
ets_tab2list_ordered(Name) ->
|
||||||
|
[E || {_, E} <- ets_tab2list(Name)].
|
||||||
|
|
||||||
%% Options
|
%% Options
|
||||||
|
|
||||||
create_options(Options) ->
|
create_options(Options) ->
|
||||||
@@ -1602,17 +1658,17 @@ destroy_and_report_unsolved_constraints(Env) ->
|
|||||||
%% -- Named argument constraints --
|
%% -- Named argument constraints --
|
||||||
|
|
||||||
create_named_argument_constraints() ->
|
create_named_argument_constraints() ->
|
||||||
ets_new(named_argument_constraints, [bag]).
|
ets_new(named_argument_constraints, [ordered_set]).
|
||||||
|
|
||||||
destroy_named_argument_constraints() ->
|
destroy_named_argument_constraints() ->
|
||||||
ets_delete(named_argument_constraints).
|
ets_delete(named_argument_constraints).
|
||||||
|
|
||||||
get_named_argument_constraints() ->
|
get_named_argument_constraints() ->
|
||||||
ets_tab2list(named_argument_constraints).
|
ets_tab2list_ordered(named_argument_constraints).
|
||||||
|
|
||||||
-spec add_named_argument_constraint(named_argument_constraint()) -> ok.
|
-spec add_named_argument_constraint(named_argument_constraint()) -> ok.
|
||||||
add_named_argument_constraint(Constraint) ->
|
add_named_argument_constraint(Constraint) ->
|
||||||
ets_insert(named_argument_constraints, Constraint),
|
ets_insert_ordered(named_argument_constraints, Constraint),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
solve_named_argument_constraints(Env) ->
|
solve_named_argument_constraints(Env) ->
|
||||||
@@ -1651,14 +1707,14 @@ destroy_and_report_unsolved_named_argument_constraints(Env) ->
|
|||||||
| {add_bytes, aeso_syntax:ann(), concat | split, utype(), utype(), utype()}.
|
| {add_bytes, aeso_syntax:ann(), concat | split, utype(), utype(), utype()}.
|
||||||
|
|
||||||
create_bytes_constraints() ->
|
create_bytes_constraints() ->
|
||||||
ets_new(bytes_constraints, [bag]).
|
ets_new(bytes_constraints, [ordered_set]).
|
||||||
|
|
||||||
get_bytes_constraints() ->
|
get_bytes_constraints() ->
|
||||||
ets_tab2list(bytes_constraints).
|
ets_tab2list_ordered(bytes_constraints).
|
||||||
|
|
||||||
-spec add_bytes_constraint(byte_constraint()) -> true.
|
-spec add_bytes_constraint(byte_constraint()) -> true.
|
||||||
add_bytes_constraint(Constraint) ->
|
add_bytes_constraint(Constraint) ->
|
||||||
ets_insert(bytes_constraints, Constraint).
|
ets_insert_ordered(bytes_constraints, Constraint).
|
||||||
|
|
||||||
solve_bytes_constraints(Env) ->
|
solve_bytes_constraints(Env) ->
|
||||||
[ solve_bytes_constraint(Env, C) || C <- get_bytes_constraints() ],
|
[ solve_bytes_constraint(Env, C) || C <- get_bytes_constraints() ],
|
||||||
@@ -1712,18 +1768,18 @@ check_bytes_constraint(Env, {add_bytes, Ann, Fun, A0, B0, C0}) ->
|
|||||||
|
|
||||||
create_field_constraints() ->
|
create_field_constraints() ->
|
||||||
%% A relation from uvars to constraints
|
%% A relation from uvars to constraints
|
||||||
ets_new(field_constraints, [bag]).
|
ets_new(field_constraints, [ordered_set]).
|
||||||
|
|
||||||
destroy_field_constraints() ->
|
destroy_field_constraints() ->
|
||||||
ets_delete(field_constraints).
|
ets_delete(field_constraints).
|
||||||
|
|
||||||
-spec constrain([field_constraint()]) -> true.
|
-spec constrain([field_constraint()]) -> true.
|
||||||
constrain(FieldConstraints) ->
|
constrain(FieldConstraints) ->
|
||||||
ets_insert(field_constraints, FieldConstraints).
|
ets_insert_ordered(field_constraints, FieldConstraints).
|
||||||
|
|
||||||
-spec get_field_constraints() -> [field_constraint()].
|
-spec get_field_constraints() -> [field_constraint()].
|
||||||
get_field_constraints() ->
|
get_field_constraints() ->
|
||||||
ets_tab2list(field_constraints).
|
ets_tab2list_ordered(field_constraints).
|
||||||
|
|
||||||
solve_field_constraints(Env) ->
|
solve_field_constraints(Env) ->
|
||||||
FieldCs =
|
FieldCs =
|
||||||
@@ -2262,6 +2318,30 @@ mk_t_err(Pos, Msg) ->
|
|||||||
mk_t_err(Pos, Msg, Ctxt) ->
|
mk_t_err(Pos, Msg, Ctxt) ->
|
||||||
aeso_errors:new(type_error, Pos, lists:flatten(Msg), lists:flatten(Ctxt)).
|
aeso_errors:new(type_error, Pos, lists:flatten(Msg), lists:flatten(Ctxt)).
|
||||||
|
|
||||||
|
mk_error({no_decls, File}) ->
|
||||||
|
Pos = aeso_errors:pos(File, 0, 0),
|
||||||
|
mk_t_err(Pos, "Empty contract\n");
|
||||||
|
mk_error({mismatched_decl_in_funblock, Name, Decl}) ->
|
||||||
|
Msg = io_lib:format("Mismatch in the function block. Expected implementation/type declaration of ~s function\n", [Name]),
|
||||||
|
mk_t_err(pos(Decl), Msg);
|
||||||
|
mk_error({higher_kinded_typevar, T}) ->
|
||||||
|
Msg = io_lib:format("Type ~s is a higher kinded type variable\n"
|
||||||
|
"(takes another type as an argument)\n", [pp(instantiate(T))]
|
||||||
|
),
|
||||||
|
mk_t_err(pos(T), Msg);
|
||||||
|
mk_error({wrong_type_arguments, X, ArityGiven, ArityReal}) ->
|
||||||
|
Msg = io_lib:format("Arity for ~s doesn't match. Expected ~p, got ~p\n"
|
||||||
|
, [pp(instantiate(X)), ArityReal, ArityGiven]
|
||||||
|
),
|
||||||
|
mk_t_err(pos(X), Msg);
|
||||||
|
mk_error({unnamed_map_update_with_default, Upd}) ->
|
||||||
|
Msg = "Invalid map update with default\n",
|
||||||
|
mk_t_err(pos(Upd), Msg);
|
||||||
|
mk_error({fundecl_must_have_funtype, _Ann, Id, Type}) ->
|
||||||
|
Msg = io_lib:format("~s at ~s was declared with an invalid type ~s.\n"
|
||||||
|
"Entrypoints and functions must have functional types"
|
||||||
|
, [pp(Id), pp_loc(Id), pp(instantiate(Type))]),
|
||||||
|
mk_t_err(pos(Id), Msg);
|
||||||
mk_error({cannot_unify, A, B, When}) ->
|
mk_error({cannot_unify, A, B, When}) ->
|
||||||
Msg = io_lib:format("Cannot unify ~s\n and ~s\n",
|
Msg = io_lib:format("Cannot unify ~s\n and ~s\n",
|
||||||
[pp(instantiate(A)), pp(instantiate(B))]),
|
[pp(instantiate(A)), pp(instantiate(B))]),
|
||||||
@@ -2344,14 +2424,6 @@ mk_error({indexed_type_must_be_word, Type, Type1}) ->
|
|||||||
Msg = io_lib:format("The indexed type ~s (at ~s) equals ~s which is not a word type\n",
|
Msg = io_lib:format("The indexed type ~s (at ~s) equals ~s which is not a word type\n",
|
||||||
[pp_type("", Type), pp_loc(Type), pp_type("", Type1)]),
|
[pp_type("", Type), pp_loc(Type), pp_type("", Type1)]),
|
||||||
mk_t_err(pos(Type), Msg);
|
mk_t_err(pos(Type), Msg);
|
||||||
mk_error({payload_type_must_be_string, Type, Type}) ->
|
|
||||||
Msg = io_lib:format("The payload type ~s (at ~s) should be string\n",
|
|
||||||
[pp_type("", Type), pp_loc(Type)]),
|
|
||||||
mk_t_err(pos(Type), Msg);
|
|
||||||
mk_error({payload_type_must_be_string, Type, Type1}) ->
|
|
||||||
Msg = io_lib:format("The payload type ~s (at ~s) equals ~s but it should be string\n",
|
|
||||||
[pp_type("", Type), pp_loc(Type), pp_type("", Type1)]),
|
|
||||||
mk_t_err(pos(Type), Msg);
|
|
||||||
mk_error({event_0_to_3_indexed_values, Constr}) ->
|
mk_error({event_0_to_3_indexed_values, Constr}) ->
|
||||||
Msg = io_lib:format("The event constructor ~s (at ~s) has too many indexed values (max 3)\n",
|
Msg = io_lib:format("The event constructor ~s (at ~s) has too many indexed values (max 3)\n",
|
||||||
[name(Constr), pp_loc(Constr)]),
|
[name(Constr), pp_loc(Constr)]),
|
||||||
@@ -2395,13 +2467,21 @@ mk_error({include, _, {string, Pos, Name}}) ->
|
|||||||
[binary_to_list(Name), pp_loc(Pos)]),
|
[binary_to_list(Name), pp_loc(Pos)]),
|
||||||
mk_t_err(pos(Pos), Msg);
|
mk_t_err(pos(Pos), Msg);
|
||||||
mk_error({namespace, _Pos, {con, Pos, Name}, _Def}) ->
|
mk_error({namespace, _Pos, {con, Pos, Name}, _Def}) ->
|
||||||
Msg = io_lib:format("Nested namespace not allowed\nNamespace '~s' at ~s not defined at top level.\n",
|
Msg = io_lib:format("Nested namespaces are not allowed\nNamespace '~s' at ~s not defined at top level.\n",
|
||||||
|
[Name, pp_loc(Pos)]),
|
||||||
|
mk_t_err(pos(Pos), Msg);
|
||||||
|
mk_error({contract, _Pos, {con, Pos, Name}, _Def}) ->
|
||||||
|
Msg = io_lib:format("Nested contracts are not allowed\nContract '~s' at ~s not defined at top level.\n",
|
||||||
|
[Name, pp_loc(Pos)]),
|
||||||
|
mk_t_err(pos(Pos), Msg);
|
||||||
|
mk_error({type_decl, _, {id, Pos, Name}, _}) ->
|
||||||
|
Msg = io_lib:format("Empty type declarations are not supported\nType ~s at ~s lacks a definition\n",
|
||||||
|
[Name, pp_loc(Pos)]),
|
||||||
|
mk_t_err(pos(Pos), Msg);
|
||||||
|
mk_error({letval, _Pos, {id, Pos, Name}, _Def}) ->
|
||||||
|
Msg = io_lib:format("Toplevel \"let\" definitions are not supported\nValue ~s at ~s could be replaced by 0-argument function\n",
|
||||||
[Name, pp_loc(Pos)]),
|
[Name, pp_loc(Pos)]),
|
||||||
mk_t_err(pos(Pos), Msg);
|
mk_t_err(pos(Pos), Msg);
|
||||||
mk_error({repeated_arg, Fun, Arg}) ->
|
|
||||||
Msg = io_lib:format("Repeated argument ~s to function ~s (at ~s).\n",
|
|
||||||
[Arg, pp(Fun), pp_loc(Fun)]),
|
|
||||||
mk_t_err(pos(Fun), Msg);
|
|
||||||
mk_error({stateful_not_allowed, Id, Fun}) ->
|
mk_error({stateful_not_allowed, Id, Fun}) ->
|
||||||
Msg = io_lib:format("Cannot reference stateful function ~s (at ~s)\nin the definition of non-stateful function ~s.\n",
|
Msg = io_lib:format("Cannot reference stateful function ~s (at ~s)\nin the definition of non-stateful function ~s.\n",
|
||||||
[pp(Id), pp_loc(Id), pp(Fun)]),
|
[pp(Id), pp_loc(Id), pp(Fun)]),
|
||||||
@@ -2529,6 +2609,9 @@ mk_error({mixed_record_and_map, Expr}) ->
|
|||||||
Msg = io_lib:format("Mixed record fields and map keys in\n~s",
|
Msg = io_lib:format("Mixed record fields and map keys in\n~s",
|
||||||
[pp_expr(" ", Expr)]),
|
[pp_expr(" ", Expr)]),
|
||||||
mk_t_err(pos(Expr), Msg);
|
mk_t_err(pos(Expr), Msg);
|
||||||
|
mk_error({conflicting_updates_for_field, Upd, Key}) ->
|
||||||
|
Msg = io_lib:format("Conflicting updates for field '~s'\n", [Key]),
|
||||||
|
mk_t_err(pos(Upd), Msg);
|
||||||
mk_error(Err) ->
|
mk_error(Err) ->
|
||||||
Msg = io_lib:format("Unknown error: ~p\n", [Err]),
|
Msg = io_lib:format("Unknown error: ~p\n", [Err]),
|
||||||
mk_t_err(pos(0, 0), Msg).
|
mk_t_err(pos(0, 0), Msg).
|
||||||
@@ -2751,7 +2834,7 @@ desugar_updates([Upd | Updates]) ->
|
|||||||
{More, Updates1} = updates_key(Key, Updates),
|
{More, Updates1} = updates_key(Key, Updates),
|
||||||
%% Check conflicts
|
%% Check conflicts
|
||||||
case length([ [] || [] <- [Rest | More] ]) of
|
case length([ [] || [] <- [Rest | More] ]) of
|
||||||
N when N > 1 -> error({conflicting_updates_for_field, Upd, Key});
|
N when N > 1 -> type_error({conflicting_updates_for_field, Upd, Key});
|
||||||
_ -> ok
|
_ -> ok
|
||||||
end,
|
end,
|
||||||
[MakeField(lists:append([Rest | More])) | desugar_updates(Updates1)].
|
[MakeField(lists:append([Rest | More])) | desugar_updates(Updates1)].
|
||||||
|
|||||||
@@ -291,7 +291,6 @@ decls_to_fcode(Env, Decls) ->
|
|||||||
end, Env1, Decls).
|
end, Env1, Decls).
|
||||||
|
|
||||||
-spec decl_to_fcode(env(), aeso_syntax:decl()) -> env().
|
-spec decl_to_fcode(env(), aeso_syntax:decl()) -> env().
|
||||||
decl_to_fcode(Env, {type_decl, _, _, _}) -> Env;
|
|
||||||
decl_to_fcode(Env = #{context := {main_contract, _}}, {fun_decl, _, Id, _}) ->
|
decl_to_fcode(Env = #{context := {main_contract, _}}, {fun_decl, _, Id, _}) ->
|
||||||
case is_no_code(Env) of
|
case is_no_code(Env) of
|
||||||
false -> fcode_error({missing_definition, Id});
|
false -> fcode_error({missing_definition, Id});
|
||||||
|
|||||||
+63
-42
@@ -2,7 +2,7 @@
|
|||||||
%%% @author Happi (Erik Stenman)
|
%%% @author Happi (Erik Stenman)
|
||||||
%%% @copyright (C) 2017, Aeternity Anstalt
|
%%% @copyright (C) 2017, Aeternity Anstalt
|
||||||
%%% @doc
|
%%% @doc
|
||||||
%%% Compiler from Aeterinty Sophia language to the Aeternity VM, aevm.
|
%%% Compiler from Aeterinty Sophia language to both AEVM and FATE VM.
|
||||||
%%% @end
|
%%% @end
|
||||||
%%% Created : 12 Dec 2017
|
%%% Created : 12 Dec 2017
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
@@ -38,10 +38,13 @@
|
|||||||
| pp_assembler
|
| pp_assembler
|
||||||
| pp_bytecode
|
| pp_bytecode
|
||||||
| no_code
|
| no_code
|
||||||
|
| keep_included
|
||||||
|
| debug_mode
|
||||||
| {backend, aevm | fate}
|
| {backend, aevm | fate}
|
||||||
| {include, {file_system, [string()]} |
|
| {include, {file_system, [string()]} |
|
||||||
{explicit_files, #{string() => binary()}}}
|
{explicit_files, #{string() => binary()}}}
|
||||||
| {src_file, string()}.
|
| {src_file, string()}
|
||||||
|
| {aci, aeso_aci:aci_type()}.
|
||||||
-type options() :: [option()].
|
-type options() :: [option()].
|
||||||
|
|
||||||
-export_type([ option/0
|
-export_type([ option/0
|
||||||
@@ -115,7 +118,8 @@ from_string(Backend, ContractString, Options) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
from_string1(aevm, ContractString, Options) ->
|
from_string1(aevm, ContractString, Options) ->
|
||||||
#{icode := Icode} = string_to_code(ContractString, Options),
|
#{ icode := Icode
|
||||||
|
, folded_typed_ast := FoldedTypedAst } = string_to_code(ContractString, Options),
|
||||||
TypeInfo = extract_type_info(Icode),
|
TypeInfo = extract_type_info(Icode),
|
||||||
Assembler = assemble(Icode, Options),
|
Assembler = assemble(Icode, Options),
|
||||||
pp_assembler(aevm, Assembler, Options),
|
pp_assembler(aevm, Assembler, Options),
|
||||||
@@ -123,47 +127,63 @@ from_string1(aevm, ContractString, Options) ->
|
|||||||
ByteCode = << << B:8 >> || B <- ByteCodeList >>,
|
ByteCode = << << B:8 >> || B <- ByteCodeList >>,
|
||||||
pp_bytecode(ByteCode, Options),
|
pp_bytecode(ByteCode, Options),
|
||||||
{ok, Version} = version(),
|
{ok, Version} = version(),
|
||||||
{ok, #{byte_code => ByteCode,
|
Res = #{byte_code => ByteCode,
|
||||||
compiler_version => Version,
|
compiler_version => Version,
|
||||||
contract_source => ContractString,
|
contract_source => ContractString,
|
||||||
type_info => TypeInfo,
|
type_info => TypeInfo,
|
||||||
abi_version => aeb_aevm_abi:abi_version(),
|
abi_version => aeb_aevm_abi:abi_version(),
|
||||||
payable => maps:get(payable, Icode)
|
payable => maps:get(payable, Icode)
|
||||||
}};
|
},
|
||||||
|
{ok, maybe_generate_aci(Res, FoldedTypedAst, Options)};
|
||||||
from_string1(fate, ContractString, Options) ->
|
from_string1(fate, ContractString, Options) ->
|
||||||
#{fcode := FCode} = string_to_code(ContractString, Options),
|
#{ fcode := FCode
|
||||||
|
, folded_typed_ast := FoldedTypedAst } = string_to_code(ContractString, Options),
|
||||||
FateCode = aeso_fcode_to_fate:compile(FCode, Options),
|
FateCode = aeso_fcode_to_fate:compile(FCode, Options),
|
||||||
pp_assembler(fate, FateCode, Options),
|
pp_assembler(fate, FateCode, Options),
|
||||||
ByteCode = aeb_fate_code:serialize(FateCode, []),
|
ByteCode = aeb_fate_code:serialize(FateCode, []),
|
||||||
{ok, Version} = version(),
|
{ok, Version} = version(),
|
||||||
{ok, #{byte_code => ByteCode,
|
Res = #{byte_code => ByteCode,
|
||||||
compiler_version => Version,
|
compiler_version => Version,
|
||||||
contract_source => ContractString,
|
contract_source => ContractString,
|
||||||
type_info => [],
|
type_info => [],
|
||||||
fate_code => FateCode,
|
fate_code => FateCode,
|
||||||
abi_version => aeb_fate_abi:abi_version(),
|
abi_version => aeb_fate_abi:abi_version(),
|
||||||
payable => maps:get(payable, FCode)
|
payable => maps:get(payable, FCode)
|
||||||
}}.
|
},
|
||||||
|
{ok, maybe_generate_aci(Res, FoldedTypedAst, Options)}.
|
||||||
|
|
||||||
|
maybe_generate_aci(Result, FoldedTypedAst, Options) ->
|
||||||
|
case proplists:get_value(aci, Options) of
|
||||||
|
undefined ->
|
||||||
|
Result;
|
||||||
|
Type ->
|
||||||
|
{ok, Aci} = aeso_aci:from_typed_ast(Type, FoldedTypedAst),
|
||||||
|
maps:put(aci, Aci, Result)
|
||||||
|
end.
|
||||||
|
|
||||||
-spec string_to_code(string(), options()) -> map().
|
-spec string_to_code(string(), options()) -> map().
|
||||||
string_to_code(ContractString, Options) ->
|
string_to_code(ContractString, Options) ->
|
||||||
Ast = parse(ContractString, Options),
|
Ast = parse(ContractString, Options),
|
||||||
pp_sophia_code(Ast, Options),
|
pp_sophia_code(Ast, Options),
|
||||||
pp_ast(Ast, Options),
|
pp_ast(Ast, Options),
|
||||||
{TypeEnv, TypedAst} = aeso_ast_infer_types:infer(Ast, [return_env | Options]),
|
{TypeEnv, FoldedTypedAst, UnfoldedTypedAst} = aeso_ast_infer_types:infer(Ast, [return_env | Options]),
|
||||||
pp_typed_ast(TypedAst, Options),
|
pp_typed_ast(UnfoldedTypedAst, Options),
|
||||||
case proplists:get_value(backend, Options, aevm) of
|
case proplists:get_value(backend, Options, aevm) of
|
||||||
aevm ->
|
aevm ->
|
||||||
Icode = ast_to_icode(TypedAst, Options),
|
Icode = ast_to_icode(UnfoldedTypedAst, Options),
|
||||||
pp_icode(Icode, Options),
|
pp_icode(Icode, Options),
|
||||||
#{ icode => Icode,
|
#{ icode => Icode
|
||||||
typed_ast => TypedAst,
|
, unfolded_typed_ast => UnfoldedTypedAst
|
||||||
type_env => TypeEnv};
|
, folded_typed_ast => FoldedTypedAst
|
||||||
|
, type_env => TypeEnv
|
||||||
|
, ast => Ast };
|
||||||
fate ->
|
fate ->
|
||||||
Fcode = aeso_ast_to_fcode:ast_to_fcode(TypedAst, Options),
|
Fcode = aeso_ast_to_fcode:ast_to_fcode(UnfoldedTypedAst, Options),
|
||||||
#{ fcode => Fcode,
|
#{ fcode => Fcode
|
||||||
typed_ast => TypedAst,
|
, unfolded_typed_ast => UnfoldedTypedAst
|
||||||
type_env => TypeEnv}
|
, folded_typed_ast => FoldedTypedAst
|
||||||
|
, type_env => TypeEnv
|
||||||
|
, ast => Ast }
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-define(CALL_NAME, "__call").
|
-define(CALL_NAME, "__call").
|
||||||
@@ -198,9 +218,9 @@ check_call1(ContractString0, FunName, Args, Options) ->
|
|||||||
case proplists:get_value(backend, Options, aevm) of
|
case proplists:get_value(backend, Options, aevm) of
|
||||||
aevm ->
|
aevm ->
|
||||||
%% First check the contract without the __call function
|
%% First check the contract without the __call function
|
||||||
#{} = string_to_code(ContractString0, Options),
|
#{ast := Ast} = string_to_code(ContractString0, Options),
|
||||||
ContractString = insert_call_function(ContractString0, ?CALL_NAME, FunName, Args, Options),
|
ContractString = insert_call_function(Ast, ContractString0, ?CALL_NAME, FunName, Args),
|
||||||
#{typed_ast := TypedAst,
|
#{unfolded_typed_ast := TypedAst,
|
||||||
icode := Icode} = string_to_code(ContractString, Options),
|
icode := Icode} = string_to_code(ContractString, Options),
|
||||||
{ok, {FunName, {fun_t, _, _, ArgTypes, RetType}}} = get_call_type(TypedAst),
|
{ok, {FunName, {fun_t, _, _, ArgTypes, RetType}}} = get_call_type(TypedAst),
|
||||||
ArgVMTypes = [ aeso_ast_to_icode:ast_typerep(T, Icode) || T <- ArgTypes ],
|
ArgVMTypes = [ aeso_ast_to_icode:ast_typerep(T, Icode) || T <- ArgTypes ],
|
||||||
@@ -220,13 +240,14 @@ check_call1(ContractString0, FunName, Args, Options) ->
|
|||||||
{ok, FunName, {ArgVMTypes, RetVMType1}, ArgTerms};
|
{ok, FunName, {ArgVMTypes, RetVMType1}, ArgTerms};
|
||||||
fate ->
|
fate ->
|
||||||
%% First check the contract without the __call function
|
%% First check the contract without the __call function
|
||||||
#{fcode := OrgFcode} = string_to_code(ContractString0, Options),
|
#{ fcode := OrgFcode
|
||||||
|
, ast := Ast } = string_to_code(ContractString0, Options),
|
||||||
FateCode = aeso_fcode_to_fate:compile(OrgFcode, []),
|
FateCode = aeso_fcode_to_fate:compile(OrgFcode, []),
|
||||||
%% collect all hashes and compute the first name without hash collision to
|
%% collect all hashes and compute the first name without hash collision to
|
||||||
SymbolHashes = maps:keys(aeb_fate_code:symbols(FateCode)),
|
SymbolHashes = maps:keys(aeb_fate_code:symbols(FateCode)),
|
||||||
CallName = first_none_match(?CALL_NAME, SymbolHashes,
|
CallName = first_none_match(?CALL_NAME, SymbolHashes,
|
||||||
lists:seq($1, $9) ++ lists:seq($A, $Z) ++ lists:seq($a, $z)),
|
lists:seq($1, $9) ++ lists:seq($A, $Z) ++ lists:seq($a, $z)),
|
||||||
ContractString = insert_call_function(ContractString0, CallName, FunName, Args, Options),
|
ContractString = insert_call_function(Ast, ContractString0, CallName, FunName, Args),
|
||||||
#{fcode := Fcode} = string_to_code(ContractString, Options),
|
#{fcode := Fcode} = string_to_code(ContractString, Options),
|
||||||
CallArgs = arguments_of_body(CallName, FunName, Fcode),
|
CallArgs = arguments_of_body(CallName, FunName, Fcode),
|
||||||
{ok, FunName, CallArgs}
|
{ok, FunName, CallArgs}
|
||||||
@@ -252,9 +273,8 @@ first_none_match(CallName, Hashes, [Char|Chars]) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
%% Add the __call function to a contract.
|
%% Add the __call function to a contract.
|
||||||
-spec insert_call_function(string(), string(), string(), [string()], options()) -> string().
|
-spec insert_call_function(aeso_syntax:ast(), string(), string(), string(), [string()]) -> string().
|
||||||
insert_call_function(Code, Call, FunName, Args, Options) ->
|
insert_call_function(Ast, Code, Call, FunName, Args) ->
|
||||||
Ast = parse(Code, Options),
|
|
||||||
Ind = last_contract_indent(Ast),
|
Ind = last_contract_indent(Ast),
|
||||||
lists:flatten(
|
lists:flatten(
|
||||||
[ Code,
|
[ Code,
|
||||||
@@ -310,7 +330,7 @@ to_sophia_value(ContractString, FunName, ok, Data, Options0) ->
|
|||||||
Options = [no_code | Options0],
|
Options = [no_code | Options0],
|
||||||
try
|
try
|
||||||
Code = string_to_code(ContractString, Options),
|
Code = string_to_code(ContractString, Options),
|
||||||
#{ typed_ast := TypedAst, type_env := TypeEnv} = Code,
|
#{ unfolded_typed_ast := TypedAst, type_env := TypeEnv} = Code,
|
||||||
{ok, _, Type0} = get_decode_type(FunName, TypedAst),
|
{ok, _, Type0} = get_decode_type(FunName, TypedAst),
|
||||||
Type = aeso_ast_infer_types:unfold_types_in_type(TypeEnv, Type0, [unfold_record_types, unfold_variant_types]),
|
Type = aeso_ast_infer_types:unfold_types_in_type(TypeEnv, Type0, [unfold_record_types, unfold_variant_types]),
|
||||||
|
|
||||||
@@ -336,12 +356,12 @@ to_sophia_value(ContractString, FunName, ok, Data, Options0) ->
|
|||||||
try
|
try
|
||||||
{ok, aeso_vm_decode:from_fate(Type, aeb_fate_encoding:deserialize(Data))}
|
{ok, aeso_vm_decode:from_fate(Type, aeb_fate_encoding:deserialize(Data))}
|
||||||
catch throw:cannot_translate_to_sophia ->
|
catch throw:cannot_translate_to_sophia ->
|
||||||
Type1 = prettypr:format(aeso_pretty:type(Type)),
|
Type1 = prettypr:format(aeso_pretty:type(Type0)),
|
||||||
Msg = io_lib:format("Cannot translate FATE value ~p\n of Sophia type ~s\n",
|
Msg = io_lib:format("Cannot translate FATE value ~p\n of Sophia type ~s\n",
|
||||||
[aeb_fate_encoding:deserialize(Data), Type1]),
|
[aeb_fate_encoding:deserialize(Data), Type1]),
|
||||||
{error, [aeso_errors:new(data_error, Msg)]};
|
{error, [aeso_errors:new(data_error, Msg)]};
|
||||||
_:_ ->
|
_:_ ->
|
||||||
Type1 = prettypr:format(aeso_pretty:type(Type)),
|
Type1 = prettypr:format(aeso_pretty:type(Type0)),
|
||||||
Msg = io_lib:format("Failed to decode binary as type ~s\n", [Type1]),
|
Msg = io_lib:format("Failed to decode binary as type ~s\n", [Type1]),
|
||||||
{error, [aeso_errors:new(data_error, Msg)]}
|
{error, [aeso_errors:new(data_error, Msg)]}
|
||||||
end
|
end
|
||||||
@@ -386,7 +406,7 @@ decode_calldata(ContractString, FunName, Calldata, Options0) ->
|
|||||||
Options = [no_code | Options0],
|
Options = [no_code | Options0],
|
||||||
try
|
try
|
||||||
Code = string_to_code(ContractString, Options),
|
Code = string_to_code(ContractString, Options),
|
||||||
#{ typed_ast := TypedAst, type_env := TypeEnv} = Code,
|
#{ unfolded_typed_ast := TypedAst, type_env := TypeEnv} = Code,
|
||||||
|
|
||||||
{ok, Args, _} = get_decode_type(FunName, TypedAst),
|
{ok, Args, _} = get_decode_type(FunName, TypedAst),
|
||||||
GetType = fun({typed, _, _, T}) -> T; (T) -> T end,
|
GetType = fun({typed, _, _, T}) -> T; (T) -> T end,
|
||||||
@@ -650,8 +670,9 @@ pp_fate_type(T) -> io_lib:format("~w", [T]).
|
|||||||
|
|
||||||
%% -------------------------------------------------------------------
|
%% -------------------------------------------------------------------
|
||||||
|
|
||||||
|
-spec sophia_type_to_typerep(string()) -> {error, bad_type} | {ok, aeb_aevm_data:type()}.
|
||||||
sophia_type_to_typerep(String) ->
|
sophia_type_to_typerep(String) ->
|
||||||
{ok, Ast} = aeso_parser:type(String),
|
Ast = aeso_parser:run_parser(aeso_parser:type(), String),
|
||||||
try aeso_ast_to_icode:ast_typerep(Ast) of
|
try aeso_ast_to_icode:ast_typerep(Ast) of
|
||||||
Type -> {ok, Type}
|
Type -> {ok, Type}
|
||||||
catch _:_ -> {error, bad_type}
|
catch _:_ -> {error, bad_type}
|
||||||
|
|||||||
+15
-7
@@ -74,25 +74,31 @@
|
|||||||
%% first argument. I.e. no backtracking to the second argument if the first
|
%% first argument. I.e. no backtracking to the second argument if the first
|
||||||
%% fails.
|
%% fails.
|
||||||
|
|
||||||
|
trampoline({bounce, Cont}) when is_function(Cont, 0) ->
|
||||||
|
trampoline(Cont());
|
||||||
|
trampoline(Res) ->
|
||||||
|
Res.
|
||||||
|
-define(BOUNCE(X), {bounce, fun() -> X end}).
|
||||||
|
|
||||||
%% Apply a parser to its continuation. This compiles a parser to its low-level representation.
|
%% Apply a parser to its continuation. This compiles a parser to its low-level representation.
|
||||||
-spec apply_p(parser(A), fun((A) -> parser1(B))) -> parser1(B).
|
-spec apply_p(parser(A), fun((A) -> parser1(B))) -> parser1(B).
|
||||||
apply_p(?lazy(F), K) -> apply_p(F(), K);
|
apply_p(?lazy(F), K) -> apply_p(F(), K);
|
||||||
apply_p(?fail(Err), _) -> {fail, Err};
|
apply_p(?fail(Err), _) -> {fail, Err};
|
||||||
apply_p(?choice([P | Ps]), K) -> lists:foldl(fun(Q, R) -> choice1(apply_p(Q, K), R) end,
|
apply_p(?choice([P | Ps]), K) -> lists:foldl(fun(Q, R) -> choice1(trampoline(apply_p(Q, K)), R) end,
|
||||||
apply_p(P, K), Ps);
|
trampoline(apply_p(P, K)), Ps);
|
||||||
apply_p(?bind(P, F), K) -> apply_p(P, fun(X) -> apply_p(F(X), K) end);
|
apply_p(?bind(P, F), K) -> apply_p(P, fun(X) -> apply_p(F(X), K) end);
|
||||||
apply_p(?right(P, Q), K) -> apply_p(P, fun(_) -> apply_p(Q, K) end);
|
apply_p(?right(P, Q), K) -> apply_p(P, fun(_) -> apply_p(Q, K) end);
|
||||||
apply_p(?left(P, Q), K) -> apply_p(P, fun(X) -> apply_p(Q, fun(_) -> K(X) end) end);
|
apply_p(?left(P, Q), K) -> apply_p(P, fun(X) -> apply_p(Q, fun(_) -> K(X) end) end);
|
||||||
apply_p(?map(F, P), K) -> apply_p(P, fun(X) -> K(F(X)) end);
|
apply_p(?map(F, P), K) -> apply_p(P, fun(X) -> K(F(X)) end);
|
||||||
apply_p(?layout, K) -> {layout, K, {fail, {expected, layout_block}}};
|
apply_p(?layout, K) -> {layout, K, {fail, {expected, layout_block}}};
|
||||||
apply_p(?tok(Atom), K) -> {tok_bind, #{Atom => K}};
|
apply_p(?tok(Atom), K) -> {tok_bind, #{Atom => K}};
|
||||||
apply_p(?return(X), K) -> K(X);
|
apply_p(?return(X), K) -> ?BOUNCE(K(X));
|
||||||
apply_p([P | Q], K) -> apply_p(P, fun(H) -> apply_p(Q, fun(T) -> K([H | T]) end) end);
|
apply_p([P | Q], K) -> apply_p(P, fun(H) -> apply_p(Q, fun(T) -> K([H | T]) end) end);
|
||||||
apply_p(T, K) when is_tuple(T) -> apply_p(tuple_to_list(T), fun(Xs) -> K(list_to_tuple(Xs)) end);
|
apply_p(T, K) when is_tuple(T) -> apply_p(tuple_to_list(T), fun(Xs) -> K(list_to_tuple(Xs)) end);
|
||||||
apply_p(M, K) when is_map(M) ->
|
apply_p(M, K) when is_map(M) ->
|
||||||
{Keys, Ps} = lists:unzip(maps:to_list(M)),
|
{Keys, Ps} = lists:unzip(maps:to_list(M)),
|
||||||
apply_p(Ps, fun(Vals) -> K(maps:from_list(lists:zip(Keys, Vals))) end);
|
apply_p(Ps, fun(Vals) -> K(maps:from_list(lists:zip(Keys, Vals))) end);
|
||||||
apply_p(X, K) -> K(X).
|
apply_p(X, K) -> ?BOUNCE(K(X)).
|
||||||
|
|
||||||
%% -- Primitive combinators --------------------------------------------------
|
%% -- Primitive combinators --------------------------------------------------
|
||||||
|
|
||||||
@@ -160,7 +166,7 @@ layout() -> ?layout.
|
|||||||
%% @doc Parse a sequence of tokens using a parser. Fails if the parse is ambiguous.
|
%% @doc Parse a sequence of tokens using a parser. Fails if the parse is ambiguous.
|
||||||
-spec parse(parser(A), tokens()) -> {ok, A} | {error, term()}.
|
-spec parse(parser(A), tokens()) -> {ok, A} | {error, term()}.
|
||||||
parse(P, S) ->
|
parse(P, S) ->
|
||||||
case parse1(apply_p(P, fun(X) -> {return_plus, X, {fail, no_error}} end), S) of
|
case parse1(trampoline(apply_p(P, fun(X) -> {return_plus, X, {fail, no_error}} end)), S) of
|
||||||
{[], {Pos, Err}} -> {error, {add_current_file(Pos), parse_error, flatten_error(Err)}};
|
{[], {Pos, Err}} -> {error, {add_current_file(Pos), parse_error, flatten_error(Err)}};
|
||||||
{[A], _} -> {ok, A};
|
{[A], _} -> {ok, A};
|
||||||
{As, _} -> {error, {{1, 1}, ambiguous_parse, As}}
|
{As, _} -> {error, {{1, 1}, ambiguous_parse, As}}
|
||||||
@@ -241,7 +247,7 @@ col(T) when is_tuple(T) -> element(2, pos(T)).
|
|||||||
|
|
||||||
%% If both parsers want the next token we grab it and merge the continuations.
|
%% If both parsers want the next token we grab it and merge the continuations.
|
||||||
choice1({tok_bind, Map1}, {tok_bind, Map2}) ->
|
choice1({tok_bind, Map1}, {tok_bind, Map2}) ->
|
||||||
{tok_bind, merge_with(fun(F, G) -> fun(T) -> choice1(F(T), G(T)) end end, Map1, Map2)};
|
{tok_bind, merge_with(fun(F, G) -> fun(T) -> choice1(trampoline(F(T)), trampoline(G(T))) end end, Map1, Map2)};
|
||||||
|
|
||||||
%% If both parsers fail we combine the error messages. If only one fails we discard it.
|
%% If both parsers fail we combine the error messages. If only one fails we discard it.
|
||||||
choice1({fail, E1}, {fail, E2}) -> {fail, add_error(E1, E2)};
|
choice1({fail, E1}, {fail, E2}) -> {fail, add_error(E1, E2)};
|
||||||
@@ -255,7 +261,7 @@ choice1(P, {return_plus, X, Q}) -> {return_plus, X, choice1(P, Q)};
|
|||||||
%% If both sides want a layout block we combine them. If only one side wants a layout block we
|
%% If both sides want a layout block we combine them. If only one side wants a layout block we
|
||||||
%% will commit to a layout block is there is one.
|
%% will commit to a layout block is there is one.
|
||||||
choice1({layout, F, P}, {layout, G, Q}) ->
|
choice1({layout, F, P}, {layout, G, Q}) ->
|
||||||
{layout, fun(N) -> choice1(F(N), G(N)) end, choice1(P, Q)};
|
{layout, fun(N) -> choice1(trampoline(F(N)), trampoline(G(N))) end, choice1(P, Q)};
|
||||||
choice1({layout, F, P}, Q) -> {layout, F, choice1(P, Q)};
|
choice1({layout, F, P}, Q) -> {layout, F, choice1(P, Q)};
|
||||||
choice1(P, {layout, G, Q}) -> {layout, G, choice1(P, Q)}.
|
choice1(P, {layout, G, Q}) -> {layout, G, choice1(P, Q)}.
|
||||||
|
|
||||||
@@ -278,6 +284,8 @@ parse1(P, S) ->
|
|||||||
%% The main work horse. Returns a list of possible parses and an error message in case parsing
|
%% The main work horse. Returns a list of possible parses and an error message in case parsing
|
||||||
%% fails.
|
%% fails.
|
||||||
-spec parse1(parser1(A), #ts{}, [A], term()) -> {[A], error()}.
|
-spec parse1(parser1(A), #ts{}, [A], term()) -> {[A], error()}.
|
||||||
|
parse1({bounce, F}, Ts, Acc, Err) ->
|
||||||
|
parse1(F(), Ts, Acc, Err);
|
||||||
parse1({tok_bind, Map}, Ts, Acc, Err) ->
|
parse1({tok_bind, Map}, Ts, Acc, Err) ->
|
||||||
case next_token(Ts) of
|
case next_token(Ts) of
|
||||||
{T, Ts1} ->
|
{T, Ts1} ->
|
||||||
|
|||||||
+40
-20
@@ -3,20 +3,33 @@
|
|||||||
%%% Description :
|
%%% Description :
|
||||||
%%% Created : 1 Mar 2018 by Ulf Norell
|
%%% Created : 1 Mar 2018 by Ulf Norell
|
||||||
-module(aeso_parser).
|
-module(aeso_parser).
|
||||||
|
-compile({no_auto_import,[map_get/2]}).
|
||||||
|
|
||||||
-export([string/1,
|
-export([string/1,
|
||||||
string/2,
|
string/2,
|
||||||
string/3,
|
string/3,
|
||||||
|
auto_imports/1,
|
||||||
hash_include/2,
|
hash_include/2,
|
||||||
type/1]).
|
decl/0,
|
||||||
|
type/0,
|
||||||
|
body/0,
|
||||||
|
maybe_block/1,
|
||||||
|
run_parser/2,
|
||||||
|
run_parser/3]).
|
||||||
|
|
||||||
-include("aeso_parse_lib.hrl").
|
-include("aeso_parse_lib.hrl").
|
||||||
-import(aeso_parse_lib, [current_file/0, set_current_file/1]).
|
-import(aeso_parse_lib, [current_file/0, set_current_file/1]).
|
||||||
|
|
||||||
-type parse_result() :: aeso_syntax:ast() | none().
|
-type parse_result() :: aeso_syntax:ast() | {aeso_syntax:ast(), sets:set(include_hash())} | none().
|
||||||
|
|
||||||
-type include_hash() :: {string(), binary()}.
|
-type include_hash() :: {string(), binary()}.
|
||||||
|
|
||||||
|
|
||||||
|
escape_errors({ok, Ok}) ->
|
||||||
|
Ok;
|
||||||
|
escape_errors({error, Err}) ->
|
||||||
|
parse_error(Err).
|
||||||
|
|
||||||
-spec string(string()) -> parse_result().
|
-spec string(string()) -> parse_result().
|
||||||
string(String) ->
|
string(String) ->
|
||||||
string(String, sets:new(), []).
|
string(String, sets:new(), []).
|
||||||
@@ -30,21 +43,17 @@ string(String, Opts) ->
|
|||||||
|
|
||||||
-spec string(string(), sets:set(include_hash()), aeso_compiler:options()) -> parse_result().
|
-spec string(string(), sets:set(include_hash()), aeso_compiler:options()) -> parse_result().
|
||||||
string(String, Included, Opts) ->
|
string(String, Included, Opts) ->
|
||||||
case parse_and_scan(file(), String, Opts) of
|
AST = run_parser(file(), String, Opts),
|
||||||
{ok, AST} ->
|
case expand_includes(AST, Included, Opts) of
|
||||||
case expand_includes(AST, Included, Opts) of
|
{ok, AST1} -> AST1;
|
||||||
{ok, AST1} -> AST1;
|
{error, Err} -> parse_error(Err)
|
||||||
{error, Err} -> parse_error(Err)
|
|
||||||
end;
|
|
||||||
{error, Err} ->
|
|
||||||
parse_error(Err)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
type(String) ->
|
|
||||||
case parse_and_scan(type(), String, []) of
|
run_parser(P, Inp) ->
|
||||||
{ok, AST} -> {ok, AST};
|
escape_errors(parse_and_scan(P, Inp, [])).
|
||||||
{error, Err} -> {error, [mk_error(Err)]}
|
run_parser(P, Inp, Opts) ->
|
||||||
end.
|
escape_errors(parse_and_scan(P, Inp, Opts)).
|
||||||
|
|
||||||
parse_and_scan(P, S, Opts) ->
|
parse_and_scan(P, S, Opts) ->
|
||||||
set_current_file(proplists:get_value(src_file, Opts, no_file)),
|
set_current_file(proplists:get_value(src_file, Opts, no_file)),
|
||||||
@@ -102,7 +111,7 @@ decl() ->
|
|||||||
|
|
||||||
%% Function declarations
|
%% Function declarations
|
||||||
, ?RULE(modifiers(), fun_or_entry(), maybe_block(fundef_or_decl()), fun_block(_1, _2, _3))
|
, ?RULE(modifiers(), fun_or_entry(), maybe_block(fundef_or_decl()), fun_block(_1, _2, _3))
|
||||||
, ?RULE(keyword('let'), valdef(),set_pos(get_pos(_1), _2))
|
, ?RULE(keyword('let'), valdef(), set_pos(get_pos(_1), _2))
|
||||||
])).
|
])).
|
||||||
|
|
||||||
fun_block(Mods, Kind, [Decl]) ->
|
fun_block(Mods, Kind, [Decl]) ->
|
||||||
@@ -596,8 +605,13 @@ expand_includes(AST, Included, Opts) ->
|
|||||||
|| File <- lists:usort(auto_imports(AST)) ] ++ AST,
|
|| File <- lists:usort(auto_imports(AST)) ] ++ AST,
|
||||||
expand_includes(AST1, Included, [], Opts).
|
expand_includes(AST1, Included, [], Opts).
|
||||||
|
|
||||||
expand_includes([], _Included, Acc, _Opts) ->
|
expand_includes([], Included, Acc, Opts) ->
|
||||||
{ok, lists:reverse(Acc)};
|
case lists:member(keep_included, Opts) of
|
||||||
|
false ->
|
||||||
|
{ok, lists:reverse(Acc)};
|
||||||
|
true ->
|
||||||
|
{ok, {lists:reverse(Acc), Included}}
|
||||||
|
end;
|
||||||
expand_includes([{include, Ann, {string, _SAnn, File}} | AST], Included, Acc, Opts) ->
|
expand_includes([{include, Ann, {string, _SAnn, File}} | AST], Included, Acc, Opts) ->
|
||||||
case get_include_code(File, Ann, Opts) of
|
case get_include_code(File, Ann, Opts) of
|
||||||
{ok, Code} ->
|
{ok, Code} ->
|
||||||
@@ -656,8 +670,14 @@ stdlib_options() ->
|
|||||||
|
|
||||||
get_include_code(File, Ann, Opts) ->
|
get_include_code(File, Ann, Opts) ->
|
||||||
case {read_file(File, Opts), read_file(File, stdlib_options())} of
|
case {read_file(File, Opts), read_file(File, stdlib_options())} of
|
||||||
{{ok, _}, {ok,_ }} ->
|
{{ok, Bin}, {ok, _}} ->
|
||||||
fail(ann_pos(Ann), "Illegal redefinition of standard library " ++ File);
|
case filename:basename(File) == File of
|
||||||
|
true -> { error
|
||||||
|
, fail( ann_pos(Ann)
|
||||||
|
, "Illegal redefinition of standard library " ++ binary_to_list(File))};
|
||||||
|
%% If a path is provided then the stdlib takes lower priority
|
||||||
|
false -> {ok, binary_to_list(Bin)}
|
||||||
|
end;
|
||||||
{_, {ok, Bin}} ->
|
{_, {ok, Bin}} ->
|
||||||
{ok, binary_to_list(Bin)};
|
{ok, binary_to_list(Bin)};
|
||||||
{{ok, Bin}, _} ->
|
{{ok, Bin}, _} ->
|
||||||
|
|||||||
+13
-8
@@ -145,8 +145,12 @@ decl(D, Options) ->
|
|||||||
with_options(Options, fun() -> decl(D) end).
|
with_options(Options, fun() -> decl(D) end).
|
||||||
|
|
||||||
-spec decl(aeso_syntax:decl()) -> doc().
|
-spec decl(aeso_syntax:decl()) -> doc().
|
||||||
decl({contract, _, C, Ds}) ->
|
decl({contract, Attrs, C, Ds}) ->
|
||||||
block(follow(text("contract"), hsep(name(C), text("="))), decls(Ds));
|
Mod = fun({Mod, true}) when Mod == payable ->
|
||||||
|
text(atom_to_list(Mod));
|
||||||
|
(_) -> empty() end,
|
||||||
|
block(follow( hsep(lists:map(Mod, Attrs) ++ [text("contract")])
|
||||||
|
, hsep(name(C), text("="))), decls(Ds));
|
||||||
decl({namespace, _, C, Ds}) ->
|
decl({namespace, _, C, Ds}) ->
|
||||||
block(follow(text("namespace"), hsep(name(C), text("="))), decls(Ds));
|
block(follow(text("namespace"), hsep(name(C), text("="))), decls(Ds));
|
||||||
decl({pragma, _, Pragma}) -> pragma(Pragma);
|
decl({pragma, _, Pragma}) -> pragma(Pragma);
|
||||||
@@ -155,13 +159,16 @@ decl({type_def, _, T, Vars, Def}) ->
|
|||||||
Kind = element(1, Def),
|
Kind = element(1, Def),
|
||||||
equals(typedecl(Kind, T, Vars), typedef(Def));
|
equals(typedecl(Kind, T, Vars), typedef(Def));
|
||||||
decl({fun_decl, Ann, F, T}) ->
|
decl({fun_decl, Ann, F, T}) ->
|
||||||
|
Mod = fun({Mod, true}) when Mod == private; Mod == stateful; Mod == payable ->
|
||||||
|
text(atom_to_list(Mod));
|
||||||
|
(_) -> empty() end,
|
||||||
Fun = case aeso_syntax:get_ann(entrypoint, Ann, false) of
|
Fun = case aeso_syntax:get_ann(entrypoint, Ann, false) of
|
||||||
true -> text("entrypoint");
|
true -> text("entrypoint");
|
||||||
false -> text("function")
|
false -> text("function")
|
||||||
end,
|
end,
|
||||||
hsep(Fun, typed(name(F), T));
|
hsep(lists:map(Mod, Ann) ++ [Fun, typed(name(F), T)]);
|
||||||
decl(D = {letfun, Attrs, _, _, _, _}) ->
|
decl(D = {letfun, Attrs, _, _, _, _}) ->
|
||||||
Mod = fun({Mod, true}) when Mod == private; Mod == stateful ->
|
Mod = fun({Mod, true}) when Mod == private; Mod == stateful; Mod == payable ->
|
||||||
text(atom_to_list(Mod));
|
text(atom_to_list(Mod));
|
||||||
(_) -> empty() end,
|
(_) -> empty() end,
|
||||||
Fun = case aeso_syntax:get_ann(entrypoint, Attrs, false) of
|
Fun = case aeso_syntax:get_ann(entrypoint, Attrs, false) of
|
||||||
@@ -364,7 +371,8 @@ expr_p(_, {Type, _, Bin})
|
|||||||
Type == oracle_query_id ->
|
Type == oracle_query_id ->
|
||||||
text(binary_to_list(aeser_api_encoder:encode(Type, Bin)));
|
text(binary_to_list(aeser_api_encoder:encode(Type, Bin)));
|
||||||
expr_p(_, {string, _, <<>>}) -> text("\"\"");
|
expr_p(_, {string, _, <<>>}) -> text("\"\"");
|
||||||
expr_p(_, {string, _, S}) -> term(binary_to_list(S));
|
expr_p(_, {string, _, S}) ->
|
||||||
|
text(io_lib:format("\"~s\"", [binary_to_list(S)]));
|
||||||
expr_p(_, {char, _, C}) ->
|
expr_p(_, {char, _, C}) ->
|
||||||
case C of
|
case C of
|
||||||
$' -> text("'\\''");
|
$' -> text("'\\''");
|
||||||
@@ -486,6 +494,3 @@ get_elifs(If = {'if', Ann, Cond, Then, Else}, Elifs) ->
|
|||||||
end;
|
end;
|
||||||
get_elifs(Else, Elifs) -> {lists:reverse(Elifs), {else, Else}}.
|
get_elifs(Else, Elifs) -> {lists:reverse(Elifs), {else, Else}}.
|
||||||
|
|
||||||
fmt(Fmt, Args) -> text(lists:flatten(io_lib:format(Fmt, Args))).
|
|
||||||
term(X) -> fmt("~p", [X]).
|
|
||||||
|
|
||||||
|
|||||||
+12
-6
@@ -37,20 +37,26 @@
|
|||||||
-type decl() :: {contract, ann(), con(), [decl()]}
|
-type decl() :: {contract, ann(), con(), [decl()]}
|
||||||
| {namespace, ann(), con(), [decl()]}
|
| {namespace, ann(), con(), [decl()]}
|
||||||
| {pragma, ann(), pragma()}
|
| {pragma, ann(), pragma()}
|
||||||
| {type_decl, ann(), id(), [tvar()]}
|
| {type_decl, ann(), id(), [tvar()]} % Only for error msgs
|
||||||
| {type_def, ann(), id(), [tvar()], typedef()}
|
| {type_def, ann(), id(), [tvar()], typedef()}
|
||||||
| {fun_decl, ann(), id(), type()}
|
| {fun_clauses, ann(), id(), type(), [letfun() | fundecl()]}
|
||||||
| {fun_clauses, ann(), id(), type(), [letbind()]}
|
|
||||||
| {block, ann(), [decl()]}
|
| {block, ann(), [decl()]}
|
||||||
| letbind().
|
| fundecl()
|
||||||
|
| letfun()
|
||||||
|
| letval(). % Only for error msgs
|
||||||
|
|
||||||
-type compiler_version() :: [non_neg_integer()].
|
-type compiler_version() :: [non_neg_integer()].
|
||||||
|
|
||||||
-type pragma() :: {compiler, '==' | '<' | '>' | '=<' | '>=', compiler_version()}.
|
-type pragma() :: {compiler, '==' | '<' | '>' | '=<' | '>=', compiler_version()}.
|
||||||
|
|
||||||
|
|
||||||
|
-type letval() :: {letval, ann(), pat(), expr()}.
|
||||||
|
-type letfun() :: {letfun, ann(), id(), [pat()], type(), expr()}.
|
||||||
|
-type fundecl() :: {fun_decl, ann(), id(), type()}.
|
||||||
|
|
||||||
-type letbind()
|
-type letbind()
|
||||||
:: {letval, ann(), pat(), expr()}
|
:: letfun()
|
||||||
| {letfun, ann(), id(), [pat()], type(), expr()}.
|
| letval().
|
||||||
|
|
||||||
-type arg() :: {arg, ann(), id(), type()}.
|
-type arg() :: {arg, ann(), id(), type()}.
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ fold(Alg = #alg{zero = Zero, plus = Plus, scoped = Scoped}, Fun, K, X) ->
|
|||||||
%% decl()
|
%% decl()
|
||||||
{contract, _, _, Ds} -> Decl(Ds);
|
{contract, _, _, Ds} -> Decl(Ds);
|
||||||
{namespace, _, _, Ds} -> Decl(Ds);
|
{namespace, _, _, Ds} -> Decl(Ds);
|
||||||
{type_decl, _, I, _} -> BindType(I);
|
|
||||||
{type_def, _, I, _, D} -> Plus(BindType(I), Decl(D));
|
{type_def, _, I, _, D} -> Plus(BindType(I), Decl(D));
|
||||||
{fun_decl, _, _, T} -> Type(T);
|
{fun_decl, _, _, T} -> Type(T);
|
||||||
{letval, _, P, E} -> Scoped(BindExpr(P), Expr(E));
|
{letval, _, P, E} -> Scoped(BindExpr(P), Expr(E));
|
||||||
|
|||||||
@@ -95,6 +95,8 @@ from_fate({tuple_t, _, Types}, ?FATE_TUPLE(Val))
|
|||||||
when length(Types) == tuple_size(Val) ->
|
when length(Types) == tuple_size(Val) ->
|
||||||
{tuple, [], [from_fate(Type, X)
|
{tuple, [], [from_fate(Type, X)
|
||||||
|| {Type, X} <- lists:zip(Types, tuple_to_list(Val))]};
|
|| {Type, X} <- lists:zip(Types, tuple_to_list(Val))]};
|
||||||
|
from_fate({record_t, [{field_t, _, FName, FType}]}, Val) ->
|
||||||
|
{record, [], [{field, [], [{proj, [], FName}], from_fate(FType, Val)}]};
|
||||||
from_fate({record_t, Fields}, ?FATE_TUPLE(Val))
|
from_fate({record_t, Fields}, ?FATE_TUPLE(Val))
|
||||||
when length(Fields) == tuple_size(Val) ->
|
when length(Fields) == tuple_size(Val) ->
|
||||||
{record, [], [ {field, [], [{proj, [], FName}], from_fate(FType, X)}
|
{record, [], [ {field, [], [{proj, [], FName}], from_fate(FType, X)}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{application, aesophia,
|
{application, aesophia,
|
||||||
[{description, "Contract Language for aeternity"},
|
[{description, "Contract Language for aeternity"},
|
||||||
{vsn, "4.2.0"},
|
{vsn, "4.3.0"},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications,
|
{applications,
|
||||||
[kernel,
|
[kernel,
|
||||||
|
|||||||
+13
-6
@@ -11,7 +11,10 @@ test_contract(N) ->
|
|||||||
{Contract,MapACI,DecACI} = test_cases(N),
|
{Contract,MapACI,DecACI} = test_cases(N),
|
||||||
{ok,JSON} = aeso_aci:contract_interface(json, Contract),
|
{ok,JSON} = aeso_aci:contract_interface(json, Contract),
|
||||||
?assertEqual([MapACI], JSON),
|
?assertEqual([MapACI], JSON),
|
||||||
?assertEqual({ok, DecACI}, aeso_aci:render_aci_json(JSON)).
|
?assertEqual({ok, DecACI}, aeso_aci:render_aci_json(JSON)),
|
||||||
|
%% Check if the compiler provides correct aci
|
||||||
|
{ok,#{aci := JSON2}} = aeso_compiler:from_string(Contract, [{aci, json}]),
|
||||||
|
?assertEqual(JSON, JSON2).
|
||||||
|
|
||||||
test_cases(1) ->
|
test_cases(1) ->
|
||||||
Contract = <<"payable contract C =\n"
|
Contract = <<"payable contract C =\n"
|
||||||
@@ -80,11 +83,11 @@ test_cases(3) ->
|
|||||||
DecACI = <<"contract C =\n"
|
DecACI = <<"contract C =\n"
|
||||||
" type state = unit\n"
|
" type state = unit\n"
|
||||||
" datatype event = SingleEventDefined\n"
|
" datatype event = SingleEventDefined\n"
|
||||||
" datatype bert('a) = Bin('a)\n"
|
" datatype bert('a) = Bin('a)\n"
|
||||||
" entrypoint a : (C.bert(string)) => int\n">>,
|
" entrypoint a : (C.bert(string)) => int\n">>,
|
||||||
{Contract,MapACI,DecACI}.
|
{Contract,MapACI,DecACI}.
|
||||||
|
|
||||||
%% Rounttrip
|
%% Roundtrip
|
||||||
aci_test_() ->
|
aci_test_() ->
|
||||||
[{"Testing ACI generation for " ++ ContractName,
|
[{"Testing ACI generation for " ++ ContractName,
|
||||||
fun() -> aci_test_contract(ContractName) end}
|
fun() -> aci_test_contract(ContractName) end}
|
||||||
@@ -94,8 +97,13 @@ all_contracts() -> aeso_compiler_tests:compilable_contracts().
|
|||||||
|
|
||||||
aci_test_contract(Name) ->
|
aci_test_contract(Name) ->
|
||||||
String = aeso_test_utils:read_contract(Name),
|
String = aeso_test_utils:read_contract(Name),
|
||||||
Opts = [{include, {file_system, [aeso_test_utils:contract_path()]}}],
|
Opts = case lists:member(Name, aeso_compiler_tests:debug_mode_contracts()) of
|
||||||
|
true -> [debug_mode];
|
||||||
|
false -> []
|
||||||
|
end ++ [{include, {file_system, [aeso_test_utils:contract_path()]}}],
|
||||||
{ok, JSON} = aeso_aci:contract_interface(json, String, Opts),
|
{ok, JSON} = aeso_aci:contract_interface(json, String, Opts),
|
||||||
|
{ok, #{aci := JSON1}} = aeso_compiler:from_string(String, [{aci, json}, {backend, fate} | Opts]),
|
||||||
|
?assertEqual(JSON, JSON1),
|
||||||
|
|
||||||
io:format("JSON:\n~p\n", [JSON]),
|
io:format("JSON:\n~p\n", [JSON]),
|
||||||
{ok, ContractStub} = aeso_aci:render_aci_json(JSON),
|
{ok, ContractStub} = aeso_aci:render_aci_json(JSON),
|
||||||
@@ -122,4 +130,3 @@ check_stub(Stub, Options) ->
|
|||||||
_ = [ io:format("~s\n", [aeso_errors:pp(E)]) || E <- Errs ],
|
_ = [ io:format("~s\n", [aeso_errors:pp(E)]) || E <- Errs ],
|
||||||
error({parse_errors, Errs})
|
error({parse_errors, Errs})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ compilable_contracts() ->
|
|||||||
{"funargs", "traffic_light", ["Pantone(12)"]},
|
{"funargs", "traffic_light", ["Pantone(12)"]},
|
||||||
{"funargs", "tuples", ["()"]},
|
{"funargs", "tuples", ["()"]},
|
||||||
%% TODO {"funargs", "due", ["FixedTTL(1020)"]},
|
%% TODO {"funargs", "due", ["FixedTTL(1020)"]},
|
||||||
|
{"funargs", "singleton_rec", ["{x = 1000}"]},
|
||||||
{"variant_types", "init", []},
|
{"variant_types", "init", []},
|
||||||
{"basic_auth", "init", []},
|
{"basic_auth", "init", []},
|
||||||
{"address_literals", "init", []},
|
{"address_literals", "init", []},
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ simple_compile_test_() ->
|
|||||||
error(ErrBin)
|
error(ErrBin)
|
||||||
end
|
end
|
||||||
end} || ContractName <- compilable_contracts(), Backend <- [aevm, fate],
|
end} || ContractName <- compilable_contracts(), Backend <- [aevm, fate],
|
||||||
not lists:member(ContractName, not_yet_compilable(Backend))] ++
|
not lists:member(ContractName, not_compilable_on(Backend))] ++
|
||||||
[ {"Test file not found error",
|
[ {"Test file not found error",
|
||||||
fun() ->
|
fun() ->
|
||||||
{error, Errors} = aeso_compiler:file("does_not_exist.aes"),
|
{error, Errors} = aeso_compiler:file("does_not_exist.aes"),
|
||||||
@@ -110,7 +110,15 @@ compile(Backend, Name) ->
|
|||||||
|
|
||||||
compile(Backend, Name, Options) ->
|
compile(Backend, Name, Options) ->
|
||||||
String = aeso_test_utils:read_contract(Name),
|
String = aeso_test_utils:read_contract(Name),
|
||||||
case aeso_compiler:from_string(String, [{src_file, Name ++ ".aes"}, {backend, Backend} | Options]) of
|
Options1 =
|
||||||
|
case lists:member(Name, debug_mode_contracts()) of
|
||||||
|
true -> [debug_mode];
|
||||||
|
false -> []
|
||||||
|
end ++
|
||||||
|
[ {src_file, Name ++ ".aes"}, {backend, Backend}
|
||||||
|
, {include, {file_system, [aeso_test_utils:contract_path()]}}
|
||||||
|
] ++ Options,
|
||||||
|
case aeso_compiler:from_string(String, Options1) of
|
||||||
{ok, Map} -> Map;
|
{ok, Map} -> Map;
|
||||||
{error, ErrorString} when is_binary(ErrorString) -> ErrorString;
|
{error, ErrorString} when is_binary(ErrorString) -> ErrorString;
|
||||||
{error, Errors} -> Errors
|
{error, Errors} -> Errors
|
||||||
@@ -165,11 +173,19 @@ compilable_contracts() ->
|
|||||||
"underscore_number_literals",
|
"underscore_number_literals",
|
||||||
"qualified_constructor",
|
"qualified_constructor",
|
||||||
"let_patterns",
|
"let_patterns",
|
||||||
"lhs_matching"
|
"lhs_matching",
|
||||||
|
"hermetization_turnoff"
|
||||||
].
|
].
|
||||||
|
|
||||||
not_yet_compilable(fate) -> [];
|
not_compilable_on(fate) -> [];
|
||||||
not_yet_compilable(aevm) -> [].
|
not_compilable_on(aevm) ->
|
||||||
|
["stdlib_include",
|
||||||
|
"manual_stdlib_include",
|
||||||
|
"hermetization_turnoff"
|
||||||
|
].
|
||||||
|
|
||||||
|
debug_mode_contracts() ->
|
||||||
|
["hermetization_turnoff"].
|
||||||
|
|
||||||
%% Contracts that should produce type errors
|
%% Contracts that should produce type errors
|
||||||
|
|
||||||
@@ -375,11 +391,15 @@ failing_contracts() ->
|
|||||||
" r.foo : (gas : int, value : int) => Remote.themap\n"
|
" r.foo : (gas : int, value : int) => Remote.themap\n"
|
||||||
"against the expected type\n"
|
"against the expected type\n"
|
||||||
" (gas : int, value : int) => map(string, int)">>])
|
" (gas : int, value : int) => map(string, int)">>])
|
||||||
, ?TYPE_ERROR(bad_include_and_ns,
|
, ?TYPE_ERROR(not_toplevel_include,
|
||||||
[<<?Pos(2, 11)
|
[<<?Pos(2, 11)
|
||||||
"Include of 'included.aes' at line 2, column 11\nnot allowed, include only allowed at top level.">>,
|
"Include of 'included.aes' at line 2, column 11\nnot allowed, include only allowed at top level.">>])
|
||||||
<<?Pos(3, 13)
|
, ?TYPE_ERROR(not_toplevel_namespace,
|
||||||
"Nested namespace not allowed\nNamespace 'Foo' at line 3, column 13 not defined at top level.">>])
|
[<<?Pos(2, 13)
|
||||||
|
"Nested namespaces are not allowed\nNamespace 'Foo' at line 2, column 13 not defined at top level.">>])
|
||||||
|
, ?TYPE_ERROR(not_toplevel_contract,
|
||||||
|
[<<?Pos(2, 12)
|
||||||
|
"Nested contracts are not allowed\nContract 'Con' at line 2, column 12 not defined at top level.">>])
|
||||||
, ?TYPE_ERROR(bad_address_literals,
|
, ?TYPE_ERROR(bad_address_literals,
|
||||||
[<<?Pos(11, 5)
|
[<<?Pos(11, 5)
|
||||||
"Cannot unify address\n"
|
"Cannot unify address\n"
|
||||||
@@ -612,6 +632,44 @@ failing_contracts() ->
|
|||||||
[<<?Pos(5, 28)
|
[<<?Pos(5, 28)
|
||||||
"Invalid call to contract entrypoint 'Foo.foo'.\n"
|
"Invalid call to contract entrypoint 'Foo.foo'.\n"
|
||||||
"It must be called as 'c.foo' for some c : Foo.">>])
|
"It must be called as 'c.foo' for some c : Foo.">>])
|
||||||
|
, ?TYPE_ERROR(toplevel_let,
|
||||||
|
[<<?Pos(2, 7)
|
||||||
|
"Toplevel \"let\" definitions are not supported\n"
|
||||||
|
"Value this_is_illegal at line 2, column 7 could be replaced by 0-argument function">>])
|
||||||
|
, ?TYPE_ERROR(empty_typedecl,
|
||||||
|
[<<?Pos(2, 8)
|
||||||
|
"Empty type declarations are not supported\n"
|
||||||
|
"Type t at line 2, column 8 lacks a definition">>])
|
||||||
|
, ?TYPE_ERROR(higher_kinded_type,
|
||||||
|
[<<?Pos(2, 35)
|
||||||
|
"Type 'm is a higher kinded type variable\n"
|
||||||
|
"(takes another type as an argument)">>])
|
||||||
|
, ?TYPE_ERROR(bad_arity,
|
||||||
|
[<<?Pos(3, 20)
|
||||||
|
"Arity for id doesn't match. Expected 1, got 0">>,
|
||||||
|
<<?Pos(3, 25)
|
||||||
|
"Cannot unify int\n"
|
||||||
|
" and id\n"
|
||||||
|
"when checking the type of the expression at line 3, column 25\n"
|
||||||
|
" 123 : int\n"
|
||||||
|
"against the expected type\n"
|
||||||
|
" id">>,
|
||||||
|
<<?Pos(4, 20)
|
||||||
|
"Arity for id doesn't match. Expected 1, got 2">>,
|
||||||
|
<<?Pos(4, 35)
|
||||||
|
"Cannot unify int\n"
|
||||||
|
" and id(int, int)\n"
|
||||||
|
"when checking the type of the expression at line 4, column 35\n"
|
||||||
|
" 123 : int\n"
|
||||||
|
"against the expected type\n"
|
||||||
|
" id(int, int)">>])
|
||||||
|
, ?TYPE_ERROR(bad_unnamed_map_update_default,
|
||||||
|
[<<?Pos(4, 17)
|
||||||
|
"Invalid map update with default">>])
|
||||||
|
, ?TYPE_ERROR(non_functional_entrypoint,
|
||||||
|
[<<?Pos(2, 14)
|
||||||
|
"f at line 2, column 14 was declared with an invalid type int.\n"
|
||||||
|
"Entrypoints and functions must have functional types">>])
|
||||||
, ?TYPE_ERROR(bad_records,
|
, ?TYPE_ERROR(bad_records,
|
||||||
[<<?Pos(3, 16)
|
[<<?Pos(3, 16)
|
||||||
"Mixed record fields and map keys in\n"
|
"Mixed record fields and map keys in\n"
|
||||||
@@ -623,6 +681,16 @@ failing_contracts() ->
|
|||||||
"Empty record/map update\n"
|
"Empty record/map update\n"
|
||||||
" r {}">>
|
" r {}">>
|
||||||
])
|
])
|
||||||
|
, ?TYPE_ERROR(bad_function_block,
|
||||||
|
[<<?Pos(4, 5)
|
||||||
|
"Mismatch in the function block. Expected implementation/type declaration of g function">>,
|
||||||
|
<<?Pos(5, 5)
|
||||||
|
"Mismatch in the function block. Expected implementation/type declaration of g function">>
|
||||||
|
])
|
||||||
|
, ?TYPE_ERROR(just_an_empty_file,
|
||||||
|
[<<?Pos(0, 0)
|
||||||
|
"Empty contract">>
|
||||||
|
])
|
||||||
, ?TYPE_ERROR(bad_number_of_args,
|
, ?TYPE_ERROR(bad_number_of_args,
|
||||||
[<<?Pos(3, 39)
|
[<<?Pos(3, 39)
|
||||||
"Cannot unify () => unit\n"
|
"Cannot unify () => unit\n"
|
||||||
@@ -645,6 +713,9 @@ failing_contracts() ->
|
|||||||
" g : (int, string) => 'c\nto arguments\n"
|
" g : (int, string) => 'c\nto arguments\n"
|
||||||
" \"Litwo, ojczyzno moja\" : string">>
|
" \"Litwo, ojczyzno moja\" : string">>
|
||||||
])
|
])
|
||||||
|
, ?TYPE_ERROR(bad_state,
|
||||||
|
[<<?Pos(4, 16)
|
||||||
|
"Conflicting updates for field 'foo'">>])
|
||||||
].
|
].
|
||||||
|
|
||||||
-define(Path(File), "code_errors/" ??File).
|
-define(Path(File), "code_errors/" ??File).
|
||||||
@@ -798,6 +869,11 @@ validate(Contract1, Contract2) ->
|
|||||||
ByteCode = #{ fate_code := FCode } = compile(fate, Contract1),
|
ByteCode = #{ fate_code := FCode } = compile(fate, Contract1),
|
||||||
FCode1 = aeb_fate_code:serialize(aeb_fate_code:strip_init_function(FCode)),
|
FCode1 = aeb_fate_code:serialize(aeb_fate_code:strip_init_function(FCode)),
|
||||||
Source = aeso_test_utils:read_contract(Contract2),
|
Source = aeso_test_utils:read_contract(Contract2),
|
||||||
aeso_compiler:validate_byte_code(ByteCode#{ byte_code := FCode1 }, Source,
|
aeso_compiler:validate_byte_code(
|
||||||
[{backend, fate}, {include, {file_system, [aeso_test_utils:contract_path()]}}]).
|
ByteCode#{ byte_code := FCode1 }, Source,
|
||||||
|
case lists:member(Contract2, debug_mode_contracts()) of
|
||||||
|
true -> [debug_mode];
|
||||||
|
false -> []
|
||||||
|
end ++
|
||||||
|
[{backend, fate}, {include, {file_system, [aeso_test_utils:contract_path()]}}]).
|
||||||
|
|
||||||
|
|||||||
@@ -58,8 +58,7 @@ contract Greeter =
|
|||||||
|
|
||||||
let state = { greeting = "Hello" }
|
let state = { greeting = "Hello" }
|
||||||
|
|
||||||
let setGreeting =
|
function setGreeting(greeting: string) =
|
||||||
(greeting: string) =>
|
|
||||||
state{ greeting = greeting }
|
state{ greeting = greeting }
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +1,78 @@
|
|||||||
// Try to cover all syntactic constructs.
|
// Try to cover all syntactic constructs.
|
||||||
|
@compiler > 0
|
||||||
|
@compiler =< 10.1.1.1.1.1.2.3.4
|
||||||
|
|
||||||
contract AllSyntaxType =
|
|
||||||
type typeDecl /* bla */
|
|
||||||
type paramTypeDecl('a, 'b)
|
|
||||||
|
|
||||||
|
namespace Ns =
|
||||||
|
datatype d('a) = D | S(int) | M('a, list('a), int)
|
||||||
|
private function fff() = 123
|
||||||
|
|
||||||
|
stateful entrypoint
|
||||||
|
f (1, x) = (_) => x
|
||||||
|
|
||||||
|
payable contract AllSyntaxType =
|
||||||
/** Multi-
|
/** Multi-
|
||||||
* line
|
* line
|
||||||
* comment
|
* comment
|
||||||
*/
|
*/
|
||||||
function foo : _
|
stateful function foo : _
|
||||||
|
entrypoint bar : int => (int * 'a)
|
||||||
|
|
||||||
|
|
||||||
contract AllSyntax =
|
contract AllSyntax =
|
||||||
|
|
||||||
type typeDecl = int
|
datatype mickiewicz = Adam | Mickiewicz
|
||||||
type paramTypeDecl('a, 'b) = (('a, 'b) => 'b) => list('a) => 'b => 'b
|
record goethe('a, 'b) = {
|
||||||
|
johann : int,
|
||||||
|
wolfgang : 'a,
|
||||||
|
von : 'a * 'b * int,
|
||||||
|
goethe : unit
|
||||||
|
}
|
||||||
|
type dante = Ns.d(int)
|
||||||
|
type shakespeare('a) = goethe('a, 'a)
|
||||||
|
|
||||||
record nestedRecord = { x : int }
|
type state = shakespeare(int)
|
||||||
record recordType = { z : nestedRecord, y : int }
|
|
||||||
datatype variantType('a) = None | Some('a)
|
|
||||||
|
|
||||||
let valWithType : map(int, int) => option(int) = (m) => Map.get(m, 42)
|
entrypoint init() = {
|
||||||
let valNoType =
|
johann = 1000,
|
||||||
if(valWithType(Map.empty) == None)
|
wolfgang = -10,
|
||||||
print(42 mod 10 * 5 / 3)
|
von = (2 + 2, 0, List.sum([x | k <- [1,2,3]
|
||||||
|
, let l = k + 1
|
||||||
|
, if(l < 10)
|
||||||
|
, let f(x) = x + 100
|
||||||
|
, Adam <- [Adam, Mickiewicz]
|
||||||
|
, let x = f(l)
|
||||||
|
])),
|
||||||
|
goethe = () }
|
||||||
|
|
||||||
function funWithType(x : int, y) : int * list(int) = (x, 0 :: [y] ++ [])
|
function f() =
|
||||||
function funNoType() =
|
let kp = "nietzsche"
|
||||||
let foo = (x, y : bool) =>
|
let p = "Пушкин"
|
||||||
if (! (y && x =< 0x0b || true)) [x]
|
let k(x : bytes(8)) : bytes(8) = Bytes.to_int(#fedcba9876543210)
|
||||||
else [11..20]
|
|
||||||
let setY(r : recordType) : unit = r{ y = 5 }
|
|
||||||
let setX(r : recordType, x : int) : recordType = r { z.x = x } // nested record update
|
|
||||||
let getY(r) = switch(r) {y = y} => y
|
|
||||||
switch (funWithType(1, -2))
|
|
||||||
(x, [y, z]) => bar({x = z, y = -y + - -z * (-1)})
|
|
||||||
(x, y :: _) => ()
|
|
||||||
|
|
||||||
let hash : address = #01ab0fff11
|
let f : () => address = () => ak_2gx9MEFxKvY9vMG5YnqnXWv1hCsX7rgnfvBLJS4aQurustR1rt
|
||||||
let b = false
|
if(Bits.test(Bits.all, 10))
|
||||||
let qcon = Mod.Con
|
abort("ohno")
|
||||||
let str = "blabla\nfoo"
|
if(true && false)
|
||||||
let chr = '"'
|
require(true, "ohyes")
|
||||||
|
elif(false || 2 == 2)
|
||||||
|
()
|
||||||
|
else
|
||||||
|
()
|
||||||
|
if(true) f(1,2)((1,2))
|
||||||
|
else switch(1::[1,2,3])
|
||||||
|
[] => 1
|
||||||
|
a::b => 123
|
||||||
|
1::2::3 => 123123
|
||||||
|
[2,3,4] => 1
|
||||||
|
_ => 13
|
||||||
|
1::[2] => 2138
|
||||||
|
put(state{johann = 1})
|
||||||
|
|
||||||
|
let m = {["foo"] = 19, /*hey wanna talk about inlined comments?*/ ["bar"] = 42}
|
||||||
|
let n = {}
|
||||||
|
m{ ["x" = 0] @ z = z + state.johann }
|
||||||
|
|
||||||
|
let sh : shakespeare(shakespeare(int)) =
|
||||||
|
{wolfgang = state}
|
||||||
|
sh{wolfgang.wolfgang = sh.wolfgang} // comment
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
contract C =
|
||||||
|
type id('a) = 'a
|
||||||
|
entrypoint f() : id = 123
|
||||||
|
entrypoint g() : id(int, int) = 123
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
contract C =
|
||||||
|
function
|
||||||
|
g(1) = 2
|
||||||
|
f(2) = 3
|
||||||
|
h(1) = 123
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
contract C =
|
||||||
|
record state = { foo : int }
|
||||||
|
entrypoint init(i : int) =
|
||||||
|
state{ foo = i,
|
||||||
|
foo = 42 }
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
contract C =
|
||||||
|
entrypoint f() =
|
||||||
|
let z = 123
|
||||||
|
{}{ [1 = 0] = z + 1 }
|
||||||
|
2
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
contract C =
|
||||||
|
type t
|
||||||
|
entrypoint f() = 123
|
||||||
@@ -45,3 +45,8 @@ contract FunctionArguments =
|
|||||||
|
|
||||||
entrypoint due(t : Chain.ttl) =
|
entrypoint due(t : Chain.ttl) =
|
||||||
true
|
true
|
||||||
|
|
||||||
|
record singleton_r = { x : int }
|
||||||
|
|
||||||
|
entrypoint singleton_rec(r : singleton_r) =
|
||||||
|
r.x
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace M =
|
||||||
|
function mf() = mg()
|
||||||
|
function mg() = mf()
|
||||||
|
|
||||||
|
namespace N =
|
||||||
|
function nf() = ng() + M.mf() + M.mg()
|
||||||
|
private function ng() = nf() + M.mf() + M.mg()
|
||||||
|
|
||||||
|
contract C =
|
||||||
|
entrypoint f() = N.ng() + N.nf() + g()
|
||||||
|
function g() = N.ng() + N.nf() + f()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
contract IWantToBelieve =
|
||||||
|
type stateT('s, 'm, 'a) = 's => 'm('a * 's)
|
||||||
|
entrypoint s() = 123
|
||||||
@@ -15,7 +15,7 @@ contract MultiSig =
|
|||||||
| OwnerRemoved (address) // of { .removedOwner : Address }
|
| OwnerRemoved (address) // of { .removedOwner : Address }
|
||||||
| ReqChanged (int) // of { .newReq : int }
|
| ReqChanged (int) // of { .newReq : int }
|
||||||
|
|
||||||
let maxOwners : int = 250
|
function maxOwners() : int = 250
|
||||||
|
|
||||||
record state = { nRequired : int
|
record state = { nRequired : int
|
||||||
, nOwners : int
|
, nOwners : int
|
||||||
@@ -68,7 +68,7 @@ contract MultiSig =
|
|||||||
switch(check_pending(callhash()))
|
switch(check_pending(callhash()))
|
||||||
CheckFail(state') => { state = state' }
|
CheckFail(state') => { state = state' }
|
||||||
CheckOk(state') =>
|
CheckOk(state') =>
|
||||||
if(state.nOwners >= maxOwners) () /* TODO */
|
if(state.nOwners >= maxOwners()) () /* TODO */
|
||||||
else
|
else
|
||||||
let nOwners' = state'.nOwners + 1
|
let nOwners' = state'.nOwners + 1
|
||||||
{ state = state' { owners = Map.insert(nOwners', newOwner, state'.owners)
|
{ state = state' { owners = Map.insert(nOwners', newOwner, state'.owners)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
contract C1 =
|
||||||
|
entrypoint f : int
|
||||||
|
|
||||||
|
contract C =
|
||||||
|
entrypoint f() = 123
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace BadNs =
|
||||||
|
contract Con =
|
||||||
|
entrypoint e : () => int
|
||||||
|
|
||||||
|
contract Con =
|
||||||
|
entrypoint foo() = 43
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace BadNs =
|
||||||
|
include "included.aes"
|
||||||
|
|
||||||
|
contract Con =
|
||||||
|
entrypoint foo() = 43
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
contract Bad =
|
contract BadCon =
|
||||||
include "included.aes"
|
|
||||||
namespace Foo =
|
namespace Foo =
|
||||||
function foo() = 42
|
function foo() = 42
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
contract C =
|
||||||
|
let this_is_illegal = 2/0
|
||||||
|
entrypoint this_is_legal() = 2/0
|
||||||
@@ -1,16 +1,3 @@
|
|||||||
|
|
||||||
/* Contract type */
|
|
||||||
contract VotingType =
|
|
||||||
type state
|
|
||||||
function init : list(string) => state
|
|
||||||
|
|
||||||
function giveRightToVote : address => unit
|
|
||||||
function delegate : address => unit
|
|
||||||
function vote : int => unit
|
|
||||||
function winnerName : unit => string
|
|
||||||
function currentTally : unit => list(string * int)
|
|
||||||
|
|
||||||
/* Contract implementation */
|
|
||||||
contract Voting =
|
contract Voting =
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
|||||||
Reference in New Issue
Block a user