Skip to content

Commit

Permalink
Fix some crap
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Dec 8, 2023
1 parent a694bee commit 59a92f5
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 38 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ run-wasm-calc3:
test-wasm-calc4:
cabal run wasm-calc4:tests

.PHONY: run-wasm-calc4
run-wasm-calc4:
cabal run wasm-calc4

.PHONY: freeze
freeze:
cabal freeze --enable-tests --enable-benchmarks
Expand Down
2 changes: 1 addition & 1 deletion wasm-calc3/src/Calc/Wasm/FromExpr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fromExpr (EIf _ predE thenE elseE) =
WIf <$> fromExpr predE <*> fromExpr thenE <*> fromExpr elseE
fromExpr (EVar _ ident) = WVar <$> lookupIdent ident
fromExpr (EApply _ funcName args) =
WApply <$> lookupFunction funcName <*> (traverse fromExpr args) -- need to look up the function name in some sort of state
WApply <$> lookupFunction funcName <*> traverse fromExpr args -- need to look up the function name in some sort of state

fromFunction :: M.Map FunctionName Natural -> Function ann -> Either FromWasmError WasmFunction
fromFunction funcMap (Function {fnBody, fnArgs, fnFunctionName}) = do
Expand Down
6 changes: 3 additions & 3 deletions wasm-calc4/src/Calc/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ module Calc.Interpreter
)
where

import GHC.Natural
import qualified Data.List.NonEmpty as NE
import Calc.Types
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.State
import Data.Coerce
import qualified Data.List.NonEmpty as NE
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import GHC.Natural

-- | type for interpreter state
newtype InterpreterState ann = InterpreterState
Expand Down Expand Up @@ -146,7 +146,7 @@ interpret (EIf ann predExpr thenExpr elseExpr) = do

interpretTupleAccess :: Expr ann -> Natural -> InterpretM ann (Expr ann)
interpretTupleAccess wholeExpr@(ETuple _ fstExpr restExpr) index = do
let items = zip ([0..] :: [Natural]) (fstExpr : NE.toList restExpr)
let items = zip ([0 ..] :: [Natural]) (fstExpr : NE.toList restExpr)
case lookup (index - 1) items of
Just expr -> pure expr
Nothing -> throwError (AccessOutsideTupleBounds wholeExpr index)
Expand Down
18 changes: 11 additions & 7 deletions wasm-calc4/src/Calc/Typecheck/Elaborate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Calc.Typecheck.Elaborate (elaborate,
elaborateFunction, elaborateModule) where
module Calc.Typecheck.Elaborate
( elaborate,
elaborateFunction,
elaborateModule,
)
where

import qualified Data.List as List
import qualified Data.List.NonEmpty as NE
import Calc.ExprUtils
import Calc.TypeUtils
import Calc.Typecheck.Error
Expand All @@ -20,6 +22,8 @@ import Control.Monad (when, zipWithM)
import Control.Monad.Except
import Data.Bifunctor (second)
import Data.Functor
import qualified Data.List as List
import qualified Data.List.NonEmpty as NE

elaborateModule ::
forall ann.
Expand Down Expand Up @@ -152,9 +156,9 @@ infer (ETupleAccess ann tup index) = do
TTuple _ tyFst tyRest ->
let tyAll = zip ([0 ..] :: [Int]) (tyFst : NE.toList tyRest)
in case List.lookup (fromIntegral $ index - 1) tyAll of
Just ty ->
pure (ETupleAccess ty tyTup index)
Nothing -> throwError $ AccessingOutsideTupleBounds ann (getOuterAnnotation tyTup) index
Just ty ->
pure (ETupleAccess ty tyTup index)
Nothing -> throwError $ AccessingOutsideTupleBounds ann (getOuterAnnotation tyTup) index
otherTy -> throwError $ AccessingNonTuple ann otherTy
infer (EApply ann fnName args) = do
fn <- lookupFunction ann fnName
Expand Down
4 changes: 2 additions & 2 deletions wasm-calc4/src/Calc/Typecheck/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

module Calc.Typecheck.Error (TypeError (..), typeErrorDiagnostic) where

import GHC.Natural
import Calc.SourceSpan
import Calc.TypeUtils
import Calc.Types.Annotation
Expand All @@ -18,6 +17,7 @@ import Data.Maybe (catMaybes, mapMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Error.Diagnose as Diag
import GHC.Natural
import qualified Prettyprinter as PP
import qualified Prettyprinter.Render.Text as PP

Expand Down Expand Up @@ -167,7 +167,7 @@ typeErrorDiagnostic input e =
( mapMaybe makeThis pairs
)
[]
(AccessingNonTuple ann ty ) ->
(AccessingNonTuple ann ty) ->
Diag.Err
Nothing
"Accessing non-tuple"
Expand Down
6 changes: 3 additions & 3 deletions wasm-calc4/src/Calc/Types/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import Calc.Types.FunctionName
import Calc.Types.Identifier
import Calc.Types.Prim
import qualified Data.List.NonEmpty as NE
import GHC.Natural
import Prettyprinter ((<+>))
import qualified Prettyprinter as PP
import GHC.Natural

data Expr ann
= EPrim ann Prim
Expand Down Expand Up @@ -44,8 +44,8 @@ instance PP.Pretty (Expr ann) where
where
tupleItems :: a -> NE.NonEmpty a -> [a]
tupleItems b bs = b : NE.toList bs
pretty (ETupleAccess _ tup nat)
= PP.pretty tup <> "." <> PP.pretty nat
pretty (ETupleAccess _ tup nat) =
PP.pretty tup <> "." <> PP.pretty nat

data Op
= OpAdd
Expand Down
11 changes: 6 additions & 5 deletions wasm-calc4/src/Calc/Wasm/FromExpr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ addLocal ::
WasmType ->
m Natural
addLocal maybeIdent ty = do
len <- fromIntegral <$> gets (\fes -> length (fesIdentifiers fes) + length (fesItems fes))
len <- gets (fromIntegral . (\fes -> length (fesIdentifiers fes) + length (fesItems fes)))
modify (\fes -> fes {fesItems = fesItems fes <> [ty]})
case maybeIdent of
Just ident -> modify (\fes -> fes { fesIdentifiers = fesIdentifiers fes <> M.singleton ident len })
Just ident -> modify (\fes -> fes {fesIdentifiers = fesIdentifiers fes <> M.singleton ident len})
Nothing -> pure ()
pure len

Expand Down Expand Up @@ -88,7 +88,8 @@ fromExpr (EIf _ predE thenE elseE) =
fromExpr (EVar _ ident) =
WVar <$> lookupIdent ident
fromExpr (EApply _ funcName args) =
WApply <$> lookupFunction funcName
WApply
<$> lookupFunction funcName
<*> traverse fromExpr args -- need to look up the function name in some sort of state
fromExpr (ETuple ty a as) = do
wasmType <- liftEither $ scalarFromType ty
Expand All @@ -103,8 +104,8 @@ fromExpr (ETuple ty a as) = do
(,) (i * size) <$> fromExpr item
)
allItems
fromExpr (ETupleAccess _ tup nat)
= WTupleAccess <$> fromExpr tup <*> pure nat
fromExpr (ETupleAccess _ tup nat) =
WTupleAccess <$> fromExpr tup <*> pure nat

memorySizeForType :: Type ann -> Natural
memorySizeForType (TPrim _ TInt) =
Expand Down
25 changes: 12 additions & 13 deletions wasm-calc4/src/Calc/Wasm/ToWasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,22 @@ fromExpr (WSet index container items) =
<> foldMap fromItem items
<> [Wasm.GetLocal index]
fromExpr (WTupleAccess tup _index) =
let offset = 0 in
fromExpr tup <> [Wasm.I32Load $ Wasm.MemArg offset 0]
let offset = 0
in fromExpr tup <> [Wasm.I32Load $ Wasm.MemArg offset 0]

-- | we load the bump allocator module and build on top of it
moduleToWasm :: WasmModule -> Wasm.Module
moduleToWasm (WasmModule {wmFunctions}) =
let functions = mapWithIndex (uncurry fromFunction) wmFunctions
types = typeFromFunction <$> wmFunctions
exports = catMaybes $ mapWithIndex (uncurry exportFromFunction) wmFunctions
in
moduleWithAllocator
{ Wasm.types = (Wasm.types moduleWithAllocator !! 0) : types,
Wasm.functions = (head (Wasm.functions moduleWithAllocator)) : functions,
Wasm.tables = mempty,
Wasm.elems = mempty,
Wasm.datas = mempty,
Wasm.start = Nothing,
Wasm.imports = mempty,
Wasm.exports = exports
}
in moduleWithAllocator
{ Wasm.types = head (Wasm.types moduleWithAllocator) : types,
Wasm.functions = head (Wasm.functions moduleWithAllocator) : functions,
Wasm.tables = mempty,
Wasm.elems = mempty,
Wasm.datas = mempty,
Wasm.start = Nothing,
Wasm.imports = mempty,
Wasm.exports = exports
}
3 changes: 1 addition & 2 deletions wasm-calc4/test/Test/Parser/ParserSpec.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Test.Parser.ParserSpec (spec) where
Expand Down Expand Up @@ -94,7 +93,7 @@ spec = do
("a + 1", EInfix () OpAdd (var "a") (int 1)),
("add(1,2)", EApply () "add" [int 1, int 2]),
("go()", EApply () "go" []),
("tuple.1",ETupleAccess () (var "tuple") 1)
("tuple.1", ETupleAccess () (var "tuple") 1)
]
traverse_
( \(str, expr) -> it (T.unpack str) $ do
Expand Down
9 changes: 7 additions & 2 deletions wasm-calc4/test/Test/Wasm/WasmSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ spec = do
Wasm.VI32 1
),
( joinLines
[
"(1,True).1"
[ "(1,True).1"
],
Wasm.VI32 1 -- note we cannot make polymorphic versions of these functions yet, although we will
),
Expand All @@ -68,6 +67,12 @@ spec = do
"fst(swapIntAndBool((1,True)))"
],
Wasm.VI32 1 -- note we cannot make polymorphic versions of these functions yet, although we will
),
( joinLines
[ "function fst(pair: (Integer,Integer)) { pair.1 }",
"fst(((10,2),(3,4)).1)"
],
Wasm.VI32 10
)
]

Expand Down

0 comments on commit 59a92f5

Please sign in to comment.