diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..af26562 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1741352980, + "narHash": "sha256-+u2UunDA4Cl5Fci3m7S643HzKmIDAe+fiXrLqYsR2fs=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "f4330d22f1c5d2ba72d3d22df5597d123fdb60a9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1742889210, + "narHash": "sha256-hw63HnwnqU3ZQfsMclLhMvOezpM7RSB0dMAtD5/sOiw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "698214a32beb4f4c8e3942372c694f40848b360d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1740877520, + "narHash": "sha256-oiwv/ZK/2FhGxrCkQkB83i7GnWXPPLzoqFHpDD3uYpk=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "147dee35aab2193b174e4c0868bd80ead5ce755c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..bfa589d --- /dev/null +++ b/flake.nix @@ -0,0 +1,31 @@ +{ + description = "Lua backend for Idris 2"; + + inputs = { + flake-parts.url = "github:hercules-ci/flake-parts"; + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ]; + perSystem = { config, self', inputs', pkgs, system, ... }: with pkgs; + let + idris2-lua = with idris2Packages; (buildIdris { + src = ./.; + idrisLibraries = [ idris2Api ]; + ipkgName = "idris2-lua"; + version = idris2Api.version; + postInstall = '' + wrapProgram "$out/bin/idris2-lua" \ + --suffix IDRIS2_PACKAGE_PATH ':' ${idris2}/${idris2.name} + ''; + }).executable; + in { + packages = { + inherit idris2-lua; + default = idris2-lua; + }; + }; + }; +} diff --git a/src/LuaCommon.idr b/src/LuaCommon.idr index e455ecf..db96337 100644 --- a/src/LuaCommon.idr +++ b/src/LuaCommon.idr @@ -14,7 +14,7 @@ import Data.String import Data.Vect import Data.Zippable -import Libraries.Utils.Hex +import Protocol.Hex infixl 100 |> diff --git a/src/LuaGen.idr b/src/LuaGen.idr index 0d9c214..4dbd1c9 100644 --- a/src/LuaGen.idr +++ b/src/LuaGen.idr @@ -6,7 +6,9 @@ import Compiler.CompileExpr import Core.Context import Core.Directory -import Libraries.Utils.Hex +import Idris.Syntax + +import Protocol.Hex import Libraries.Utils.Path import Idris.Driver @@ -31,6 +33,9 @@ import LuaCommon import OrderDefs import LuaAst +%hide Libraries.Data.PosMap.infixl.(|>) +%hide Core.Normalise.Eval.Stack + data Stack : Type where data Preamble : Type where @@ -87,7 +92,7 @@ logLine str with (opts |> debugOutput |> get) logLine str | False = pure () export -toMillis : Clock type -> Integer +toMillis : Clock t -> Integer toMillis (MkClock sec nan) = let scale = 1000 in scale * sec + (nan `div` 1000000) @@ -364,7 +369,7 @@ mutual stringify n (LPrimFn (Add ty) [x, y]) = stringifyBinOp n "+" x y stringify n (LPrimFn (Sub ty) [x, y]) = stringifyBinOp n "-" x y stringify n (LPrimFn (Mul ty) [x, y]) = stringifyBinOp n "*" x y - stringify n (LPrimFn (Div IntType) [x, y]) with (copts |> luaVersion |> get >= Lua53) + stringify n (LPrimFn (Div IntType) [x, y]) with ((copts |> luaVersion |> get) >= Lua53) stringify n (LPrimFn (Div IntType) [x, y]) | True = stringifyBinOp n "//" x y stringify n (LPrimFn (Div IntType) [x, y]) | False @@ -511,8 +516,8 @@ pushFrame = s <- get Stack let frame = nextFrame s let index = nextIndex s - put Stack (record{ nextFrame $= (+1) - , nextIndex = indexLowest + put Stack ({ nextFrame $= (+1) + , nextIndex := indexLowest , stack $= (index ::) } s) pure (MkStackFrame frame) @@ -524,7 +529,7 @@ pushLocal = do s <- get Stack let i = nextIndex s - put Stack (record{nextIndex $= (+1)} s) + put Stack ({nextIndex $= (+1)} s) pure (LIndex (LLVar (frameName frame)) (LNumber (show i))) ||| Returns the number of local variables in the popped frame @@ -536,7 +541,7 @@ popFrame = let v = nextIndex s case (i <= frameLowest, stack s) of (False, (nextIndex :: other)) => do - put Stack (record{nextFrame $= (\i => i - 1), nextIndex = nextIndex, stack = other} s) + put Stack ({nextFrame $= (\i => i - 1), nextIndex := nextIndex, stack := other} s) pure (v - 1) (_, _) => throw (UserError "Attempt to pop from an empty stack") @@ -549,7 +554,7 @@ popName = if i <= indexLowest then throw (UserError "attempt to pop from an empty stack frame") else - put Stack (record{nextIndex $= (\i => i - 1)} s) + put Stack ({nextIndex $= (\i => i - 1)} s) getPreamble : @@ -582,7 +587,7 @@ addDefToPreamble name def okIfDefined = do pure () -constantTy : Constant -> Maybe Constant +constantTy : Constant -> Maybe PrimType constantTy (I _) = Just IntType constantTy (BI _) = Just IntegerType constantTy (B8 _) = Just Bits8Type @@ -1310,7 +1315,7 @@ translate defs term = do clock0 <- coreLift $ clockTime Monotonic logLine "Lua compilation started [0/5]" - logLine ("Using " ++ opts |> luaVersion |> get |> show) + logLine ("Using " ++ (opts |> luaVersion |> get |> show)) cdata <- getCompileData False Cases term clock1 <- coreLift $ clockTime Monotonic @@ -1413,7 +1418,7 @@ build defs outputDir term file = do strbuf <- translate defs term let luaFile = file ++ ".lua" Right () <- coreLift $ writeBufferToFile (outputDir luaFile) strbuf.get strbuf.offset - | Left err => throw $ FileErr (outputDir luaFile) err + | Left (err, _) => throw $ FileErr (outputDir luaFile) err luaExe <- coreLift getLuaExe @@ -1426,15 +1431,16 @@ build defs outputDir term file = do pure (outputDir file) compile : Ref Ctxt Defs + -> Ref Syn SyntaxInfo -> String -> String -> ClosedTerm -> String -> Core (Maybe String) -compile defs tmpDir outputDir term file = Just <$> build defs outputDir term file +compile defs _ tmpDir outputDir term file = Just <$> build defs outputDir term file -execute : Ref Ctxt Defs -> String -> ClosedTerm -> Core () -execute defs tmpDir term = do +execute : Ref Ctxt Defs -> Ref Syn SyntaxInfo -> String -> ClosedTerm -> Core () +execute defs _ tmpDir term = do exe <- build defs tmpDir term "generated" coreLift_ $ fflush stdout coreLift_ $ system $ "'" ++ exe ++ "' "