Add support for Nix Flake

This commit is contained in:
2023-12-14 12:30:12 +01:00
parent 2737e0a34e
commit 54505d4d4d
10 changed files with 178 additions and 10 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

2
.gitignore vendored
View File

@@ -29,3 +29,5 @@ cabal.project.local~
# End of https://www.toptal.com/developers/gitignore/api/haskell
result
.direnv

View File

@@ -40,11 +40,11 @@ executable MVM
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends:
base ^>=4.15.0.0,
bytestring ^>=0.11.0.0,
containers ^>=0.6.4.1,
mtl ^>=2.2.2,
transformers ^>=0.5.6.2
base >=4.15.0.0,
bytestring >=0.11.0.0,
containers >=0.6.4.1,
mtl >=2.2.2,
transformers >=0.5.6.2
hs-source-dirs: app
default-language: Haskell2010
@@ -57,11 +57,11 @@ test-suite spec
test
ghc-options: -Wall
build-depends:
base ^>=4.15.0.0,
bytestring ^>=0.11.0.0,
containers ^>=0.6.4.1,
mtl ^>=2.2.2,
transformers ^>=0.5.6.2,
base >=4.15.0.0,
bytestring >=0.11.0.0,
containers >=0.6.4.1,
mtl >=2.2.2,
transformers >=0.5.6.2,
hspec ==2.*
other-modules:
VirtualMachineSpec

20
examples/example1.S Normal file
View File

@@ -0,0 +1,20 @@
; Evaluate 2*3+5
main: push 2
push 3
call &prd
clr 2
push 5
call &sum
clr 2
halt
sum: lda 0
lda 1
add
ret
prd: lda 0
lda 1
mul
ret

9
examples/example2.S Normal file
View File

@@ -0,0 +1,9 @@
; I/O example - simple echo
read: in
dup
out
push 0x0A
sub
jne &read ; loop until the input != new line (0x0A)
halt

30
examples/example3.S Normal file
View File

@@ -0,0 +1,30 @@
; Evaluate 2^10 - loop variant
push 2
push 10
call &pow
clr 2
halt
pow: lda 1 ; base
lda 0 ; exp
push 1 ; acc
; | Stack:
.loop: ldl 1 ; if exp == 0 | exp
je &.done ; then return | exp
pop ; |
; |
ldl 2 ; Evaluate | acc
ldl 0 ; next power | acc base
mul ; | acc*base
stl 2 ; |
; |
ldl 1 ; Decrement exp | exp
push 1 ; | exp 1
sub ; | exp-1
stl 1 ; |
jmp &.loop ; |
.done: ldl 2 ; | ... acc
ret ; | acc

28
examples/example4.S Normal file
View File

@@ -0,0 +1,28 @@
; Evaluate 2^10 - recursive variant
push 2 ; base
push 10 ; exp
call &pow
clr 2
halt
pow: lda 1 ; base
lda 0 ; exp
ldl 1 ; push exp to top
je &.edge ; the edge case: if exp == 0 then return 1
pop ; pop exp
; | Stack:
ldl 0 ; | base
ldl 1 ; | base exp
push 1 ; | base exp 1
sub ; | base exp-1
call &pow ; | base exp-1 base^(exp-1)]
clr 1 ; | base base^(exp-1)
mul ; | base*base^(exp-1)
ret ; | base*base^(exp-1)
.edge: pop
push 1 ; return 1
ret

33
examples/example5.S Normal file
View File

@@ -0,0 +1,33 @@
; The N-th element of Fibbonaci sequence - recursive variant
push 6
call &fibb
clr 1
halt
fibb: lda 0 ; n | Stack:
ldl 0 ; n == 0 -> return 1 | n
je &.done0 ; | n
pop ; |
ldl 0 ; n == 1 -> return 1 | n
push 1 ; | n 1
sub ; | n-1
je &.done1 ; | n-1
dup ; Evaluate fibb | n-1 n-1
push 1 ; | n-1 n-1 1
sub ; | n-1 n-2
call &fibb ; | n-1 n-2 f(n-2)
clr 1 ; | n-1 f(n-2)
over ; | n-1 f(n-2) n-1
call &fibb ; | n-1 f(n-2) n-1 f(n-1)
clr 1 ; | n-1 f(n-2) f(n-1)
add ; | n-1 f(n-2)+f(n-1)
ret
.done1: pop
push 1
ret
.done0: pop
push 1
ret

27
flake.lock generated Normal file
View File

@@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1702346276,
"narHash": "sha256-eAQgwIWApFQ40ipeOjVSoK4TEHVd6nbSd9fApiHIw5A=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "cf28ee258fd5f9a52de6b9865cdb93a1f96d09b7",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-23.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

18
flake.nix Normal file
View File

@@ -0,0 +1,18 @@
{
description = "MVM development";
inputs = {
nixpkgs.url = github:NixOS/nixpkgs/nixos-23.11;
};
outputs = { self, nixpkgs }:
let
pkgs = import nixpkgs { inherit system; };
haskellPkgs = pkgs.haskellPackages;
system = "x86_64-linux";
in {
packages.${system}.default = pkgs.haskellPackages.developPackage {
root = ./.;
};
};
}