Skip to content

Commit

Permalink
Add ⎕P and ⎕E and ⎕ fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerg committed Dec 1, 2024
1 parent bea6df6 commit 0d07ce7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
Binary file modified docs/interpreters/latest/tinyapl-js.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions examples/sum.tinyapl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
⎕←⎕+⎕
3 changes: 1 addition & 2 deletions src/TinyAPL/ArrayFunctionOperator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import qualified Data.IORef as IORef
import Data.IORef (IORef)
import Control.DeepSeq
import GHC.Generics
import GHC.Stack (HasCallStack)

-- * Arrays

Expand Down Expand Up @@ -108,7 +107,7 @@ majorCells (Array (_:sh) cs) = mapMaybe (arrayOf sh) $ chunk (product sh) cs whe
chunk l xs = genericTake l xs : chunk l (genericDrop l xs)
majorCells (Dictionary ks vs) = zipWith (\a b -> vector [a, b]) ks vs

fromMajorCells :: HasCallStack => [Noun] -> Noun
fromMajorCells :: [Noun] -> Noun
fromMajorCells [] = Array [0] []
fromMajorCells (c:cs) = let
impl = fromJust $ arrayReshaped (1 + genericLength cs : arrayShape c) $ concatMap arrayContents $ c : cs
Expand Down
16 changes: 15 additions & 1 deletion src/TinyAPL/CoreQuads.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ type_ = PrimitiveFunction (Just $ \(Array sh cs) -> return $ Array sh $ (\case
AdverbWrap _ -> Number 4
ConjunctionWrap _ -> Number 5
Struct _ -> Number 6) <$> cs) Nothing (G.quad : "Type") Nothing
print_ = PrimitiveFunction (Just $ \y -> do
let err = DomainError "Print argument must be a string or vector of strings"
ss <- asStrings err y
out <- getsContext contextOut
out $ intercalate "\n" ss ++ "\n"
pure $ vector []
) Nothing (G.quad : "P") Nothing
errorPrint = PrimitiveFunction (Just $ \y -> do
let err = DomainError "Print argument must be a string or vector of strings"
ss <- asStrings err y
err <- getsContext contextErr
err $ intercalate "\n" ss ++ "\n"
pure $ vector []
) Nothing (G.quad : "E") Nothing
measure = PrimitiveAdverb Nothing (Just $ \f -> pure $ DerivedFunctionFunction (Just $ \y -> do
start <- realToFrac <$> liftToSt getPOSIXTime
_ <- callMonad f y
Expand All @@ -78,7 +92,7 @@ measure = PrimitiveAdverb Nothing (Just $ \f -> pure $ DerivedFunctionFunction (
end <- realToFrac <$> liftToSt getPOSIXTime
pure $ scalar $ Number $ (end - start) :+ 0) Nothing measure f) (G.quad : "_Measure") Nothing

core = quadsFromReprs [ io, ct, u, l, d, seed, unix, ts, math ] [ exists, repr, delay, type_, unicode ] [ measure ] []
core = quadsFromReprs [ io, ct, u, l, d, seed, unix, ts, math ] [ exists, repr, delay, type_, unicode, print_, errorPrint ] [ measure ] []

makeImport :: (FilePath -> St String) -> Maybe ([String] -> St String) -> Function
makeImport read readStd = PrimitiveFunction (Just $ \x -> do
Expand Down
4 changes: 2 additions & 2 deletions src/TinyAPL/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ evalLeaf (TokenPrimConjunction n _) =
lift $ except $ maybeToEither (SyntaxError $ "Unknown primitive conjunction " ++ [n]) $ VConjunction <$> lookup n P.conjunctions
evalLeaf (TokenArrayName name _)
| name == [G.quad] = do
out <- gets contextOut
err <- gets contextErr
input <- gets contextIn
out $ G.quad : ": "
err $ G.quad : ": "
code <- input
context <- get
(res, context') <- lift $ run [G.quad] code context
Expand Down

0 comments on commit 0d07ce7

Please sign in to comment.