This is a mini interpreter for Solidity language written in Haskell.
- Standard types
-
int -
bool -
string -
mapping -
array
-
- Standard operators
- Functions
- Declaration
- Recursion
- Access modifiers
-
public -
private -
internal -
external
-
- Variables
- Declaration
- Assignment
-
Constants` -
require- Throws error if condition is false and reverts all changes
- Statements
-
if -
for -
while
-
contract Example {
int public value = 0;
function storeValue(int x) public {
value = x;
require(value < 100, "Huge value");
}
mapping(int => int) private cache;
function privateFib(int arg) private returns (int) {
if (arg == 0 || arg == 1) {
return 1;
}
int current = cache[arg];
if (current == 0){
int result = privateFib(arg - 1) + privateFib(arg - 2);
cache[arg] = result;
return result;
}
else {
return current;
}
}
function fib(int arg) public returns (int) {
return privateFib(arg);
}
int public constant arrSize = 10;
string[arrSize] private arr;
function storeInArr(int start, int stop, string calldata newValue) public {
for (int i = start; i < stop; i = i + 1) {
require(i < arrSize, "Incorrect stop");
arr[i] = newValue;
}
}
function readFromArr(int index) public returns (string) {
require(index < arrSize);
return arr[index];
}
}~> msi Example.sol
Example.sol successfully uploaded!
Available functions:
storeValue(int)
loadValue() returns (int)
fib(int) returns (int)
storeInArr(int, int, string)
readFromArr(int) returns (string)
Available members:
value
arrSize
MSI> storeValue(10)
(void) void
MSI> value
(int) 10
MSI> storeValue(101)
Reverted with: Huge value
MSI> value
(int) 10
MSI> fib(2)
(int) 2
MSI> privateFib(2)
Reverted with: access denied
MSI> unknownFunction()
Reverted with: unknown function
MSI> unknownMember = 3
Reverted with: unknown member
MSI> storeInArr(0, 3, "Haskell > OCaml")
(void) void
MSI> readFromArr(0)
(string) "Haskell > OCaml"
MSI> storeInArr(0, 12, "Haskell < OCaml")
Reverted with: Incorrect stop
MSI> readFromArr(0)
(string) "Haskell > OCaml"
MSI> exit
Bye-bye!
~>
KarasssDev – for the idea of the project and requirements.