Skip to content

Commit

Permalink
Use trampoline
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed May 4, 2023
1 parent a895404 commit f2c58f3
Show file tree
Hide file tree
Showing 230 changed files with 624 additions and 590 deletions.
3 changes: 3 additions & 0 deletions docs/developers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 📅 Revision history for HelMA

## 0.8.4.10 -- 2023-05-01
* Remove Loop

## 0.8.4.9 -- 2023-05-01
* Add optimize
* Add files to examples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<span class="lineno"> 18 </span>import HelVM.HelMA.Automaton.Types.DumpType
<span class="lineno"> 19 </span>
<span class="lineno"> 20 </span>simpleEval :: BIO m =&gt; (BFType , Source , CellType) -&gt; m ()
<span class="lineno"> 21 </span><span class="decl"><span class="istickedoff">simpleEval (c , s , t) = eval c s t Pretty</span></span> --TODO Add MaybeLimit and use Loop
<span class="lineno"> 21 </span><span class="decl"><span class="istickedoff">simpleEval (c , s , t) = eval c s t Pretty</span></span> --TODO Add MaybeLimit and use Trampoline
<span class="lineno"> 22 </span>
<span class="lineno"> 23 </span>----
<span class="lineno"> 24 </span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<span class="lineno"> 7 </span>import HelVM.HelMA.Automata.ETA.OperandParsers
<span class="lineno"> 8 </span>import HelVM.HelMA.Automata.ETA.Token
<span class="lineno"> 9 </span>
<span class="lineno"> 10 </span>import HelVM.HelMA.Automaton.Loop
<span class="lineno"> 10 </span>import HelVM.HelMA.Automaton.Trampoline as Trampoline
<span class="lineno"> 11 </span>
<span class="lineno"> 12 </span>import HelVM.HelMA.Automaton.IO.AutomatonIO
<span class="lineno"> 13 </span>
Expand All @@ -41,43 +41,43 @@
<span class="lineno"> 22 </span>import Prelude hiding (divMod)
<span class="lineno"> 23 </span>
<span class="lineno"> 24 </span>run :: (SAutomatonIO e s m) =&gt; Maybe Natural -&gt; Memory s -&gt; m $ Memory s
<span class="lineno"> 25 </span><span class="decl"><span class="istickedoff">run = loopMWithLimit nextState</span></span>
<span class="lineno"> 25 </span><span class="decl"><span class="istickedoff">run = trampolineMWithLimit nextState</span></span>
<span class="lineno"> 26 </span>
<span class="lineno"> 27 </span>nextState :: (SAutomatonIO e s m) =&gt; Memory s -&gt; m $ MemorySame s
<span class="lineno"> 28 </span><span class="decl"><span class="istickedoff">nextState (Memory iu s) = build =&lt;&lt; nextIM iu where build (t , iu') = doInstruction t (Memory iu' s)</span></span>
<span class="lineno"> 29 </span>
<span class="lineno"> 30 </span>doInstruction :: (SAutomatonIO e s m) =&gt; Maybe Token -&gt; Memory s -&gt; m $ MemorySame s
<span class="lineno"> 31 </span>-- | IO instructions
<span class="lineno"> 32 </span><span class="decl"><span class="istickedoff">doInstruction (Just O) u = Left . updateStack u &lt;$&gt; doOutputChar2 (memoryStack u)</span>
<span class="lineno"> 33 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just I) u = <span class="nottickedoff">Left . updateStack u &lt;$&gt; doInputChar2 (memoryStack u)</span></span>
<span class="lineno"> 32 </span><span class="decl"><span class="istickedoff">doInstruction (Just O) u = Trampoline.continue . updateStack u &lt;$&gt; doOutputChar2 (memoryStack u)</span>
<span class="lineno"> 33 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just I) u = <span class="nottickedoff">Trampoline.continue . updateStack u &lt;$&gt; doInputChar2 (memoryStack u)</span></span>
<span class="lineno"> 34 </span><span class="spaces"></span><span class="istickedoff"></span>
<span class="lineno"> 35 </span><span class="spaces"></span><span class="istickedoff">-- | Stack instructions</span>
<span class="lineno"> 36 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just N) (Memory iu s) = build &lt;$&gt; parseNumber iu where build (symbol , iu') = Left (Memory iu' (push1 symbol s))</span>
<span class="lineno"> 37 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just H) u = Left . updateStack u &lt;$&gt; halibut (memoryStack u)</span>
<span class="lineno"> 36 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just N) (Memory iu s) = build &lt;$&gt; parseNumber iu where build (symbol , iu') = Trampoline.continue (Memory iu' (push1 symbol s))</span>
<span class="lineno"> 37 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just H) u = Trampoline.continue . updateStack u &lt;$&gt; halibut (memoryStack u)</span>
<span class="lineno"> 38 </span><span class="spaces"></span><span class="istickedoff"></span>
<span class="lineno"> 39 </span><span class="spaces"></span><span class="istickedoff">-- | Arithmetic</span>
<span class="lineno"> 40 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just S) u = Left . updateStack u &lt;$&gt; sub (memoryStack u)</span>
<span class="lineno"> 41 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just E) u = <span class="nottickedoff">Left . updateStack u &lt;$&gt; divMod (memoryStack u)</span></span>
<span class="lineno"> 40 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just S) u = Trampoline.continue . updateStack u &lt;$&gt; sub (memoryStack u)</span>
<span class="lineno"> 41 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just E) u = <span class="nottickedoff">Trampoline.continue . updateStack u &lt;$&gt; divMod (memoryStack u)</span></span>
<span class="lineno"> 42 </span><span class="spaces"></span><span class="istickedoff"></span>
<span class="lineno"> 43 </span><span class="spaces"></span><span class="istickedoff">-- | Control</span>
<span class="lineno"> 44 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just R) u = pure $ Left u</span>
<span class="lineno"> 45 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just A) (Memory iu@(IM il ic) s) = pure $ Left ((Memory iu . flipPush1 s . genericNextLabel il) ic)</span>
<span class="lineno"> 44 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just R) u = pure $ Trampoline.continue u</span>
<span class="lineno"> 45 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just A) (Memory iu@(IM il ic) s) = pure $ Trampoline.continue ((Memory iu . flipPush1 s . genericNextLabel il) ic)</span>
<span class="lineno"> 46 </span><span class="spaces"></span><span class="istickedoff">doInstruction (Just T) u = transfer u</span>
<span class="lineno"> 47 </span><span class="spaces"></span><span class="istickedoff">doInstruction Nothing u = end u</span></span>
<span class="lineno"> 48 </span>
<span class="lineno"> 49 </span>transfer :: (SAutomatonIO e s m) =&gt; Memory s -&gt; m $ MemorySame s
<span class="lineno"> 50 </span><span class="decl"><span class="istickedoff">transfer = branch &lt;=&lt; pop2ForStack where</span>
<span class="lineno"> 51 </span><span class="spaces"> </span><span class="istickedoff">branch (_ , 0 , u) = pure $ Left u</span>
<span class="lineno"> 51 </span><span class="spaces"> </span><span class="istickedoff">branch (_ , 0 , u) = pure $ Trampoline.continue u</span>
<span class="lineno"> 52 </span><span class="spaces"> </span><span class="istickedoff">branch (0 , _ , u) = end u</span>
<span class="lineno"> 53 </span><span class="spaces"> </span><span class="istickedoff">branch (l , _ , u) = Left . updateAddress u &lt;$&gt; genericFindAddress (memoryProgram u) l</span></span>
<span class="lineno"> 53 </span><span class="spaces"> </span><span class="istickedoff">branch (l , _ , u) = Trampoline.continue . updateAddress u &lt;$&gt; genericFindAddress (memoryProgram u) l</span></span>
<span class="lineno"> 54 </span>
<span class="lineno"> 55 </span>pop2ForStack :: (SAutomatonIO e s m) =&gt; Memory s -&gt; m (e , e , Memory s)
<span class="lineno"> 56 </span><span class="decl"><span class="istickedoff">pop2ForStack u = build &lt;$&gt; pop2 (memoryStack u) where</span>
<span class="lineno"> 57 </span><span class="spaces"> </span><span class="istickedoff">build (s1 , s2 , s') = (s1 , s2 , updateStack u s')</span></span>
<span class="lineno"> 58 </span>
<span class="lineno"> 59 </span>-- | Terminate instruction
<span class="lineno"> 60 </span>end :: (SAutomatonIO e s m) =&gt; Memory s -&gt; m $ MemorySame s
<span class="lineno"> 61 </span><span class="decl"><span class="istickedoff">end = pure . Right</span></span>
<span class="lineno"> 61 </span><span class="decl"><span class="istickedoff">end = pure . Trampoline.break</span></span>
<span class="lineno"> 62 </span>
<span class="lineno"> 63 </span>-- | Memory methods
<span class="lineno"> 64 </span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
<span class="lineno"> 6 </span>import HelVM.HelMA.Automaton.IO.AutomatonIO
<span class="lineno"> 7 </span>import HelVM.HelMA.Automaton.IO.BusinessIO
<span class="lineno"> 8 </span>
<span class="lineno"> 9 </span>import HelVM.HelMA.Automaton.Loop as Loop
<span class="lineno"> 9 </span>import HelVM.HelMA.Automaton.Trampoline as Trampoline
<span class="lineno"> 10 </span>
<span class="lineno"> 11 </span>import HelVM.HelMA.Automaton.Combiner.RAM as RAM
<span class="lineno"> 12 </span>
<span class="lineno"> 13 </span>import Control.Type.Operator
<span class="lineno"> 14 </span>
<span class="lineno"> 15 </span>run :: (RAutomatonIO e r m) =&gt; Maybe Natural -&gt; Automaton e r -&gt; m $ Automaton e r
<span class="lineno"> 16 </span><span class="decl"><span class="istickedoff">run = loopMWithLimit nextState</span></span>
<span class="lineno"> 16 </span><span class="decl"><span class="istickedoff">run = trampolineMWithLimit nextState</span></span>
<span class="lineno"> 17 </span>
<span class="lineno"> 18 </span>nextState :: RAutomatonIO e r m =&gt; Automaton e r -&gt; m $ AutomatonSame e r
<span class="lineno"> 19 </span><span class="decl"><span class="istickedoff">nextState a@(Automaton ic ram)</span>
Expand All @@ -46,17 +46,17 @@
<span class="lineno"> 27 </span>
<span class="lineno"> 28 </span>-- | IO instructions
<span class="lineno"> 29 </span>doOutputChar :: RAutomatonIO e r m =&gt; e -&gt; Automaton e r -&gt; m $ AutomatonSame e r
<span class="lineno"> 30 </span><span class="decl"><span class="istickedoff">doOutputChar address (Automaton ic ram) = wPutAsChar (genericLoad ram address) $&gt; Loop.continue (next3Automaton ic ram)</span></span>
<span class="lineno"> 30 </span><span class="decl"><span class="istickedoff">doOutputChar address (Automaton ic ram) = wPutAsChar (genericLoad ram address) $&gt; Trampoline.continue (next3Automaton ic ram)</span></span>
<span class="lineno"> 31 </span>
<span class="lineno"> 32 </span>doInputChar :: RAutomatonIO e r m =&gt; e -&gt; Automaton e r -&gt; m $ AutomatonSame e r
<span class="lineno"> 33 </span><span class="decl"><span class="nottickedoff">doInputChar address (Automaton ic ram) = Loop.continue . next3Automaton ic . flippedStoreChar address ram &lt;$&gt; wGetChar</span></span>
<span class="lineno"> 33 </span><span class="decl"><span class="nottickedoff">doInputChar address (Automaton ic ram) = Trampoline.continue . next3Automaton ic . flippedStoreChar address ram &lt;$&gt; wGetChar</span></span>
<span class="lineno"> 34 </span>
<span class="lineno"> 35 </span>-- | Terminate instruction
<span class="lineno"> 36 </span>doEnd :: RAutomatonIO e r m =&gt; Automaton e r -&gt; m $ AutomatonSame e r
<span class="lineno"> 37 </span><span class="decl"><span class="istickedoff">doEnd = pure . Loop.break</span></span>
<span class="lineno"> 37 </span><span class="decl"><span class="istickedoff">doEnd = pure . Trampoline.break</span></span>
<span class="lineno"> 38 </span>
<span class="lineno"> 39 </span>doInstruction :: RAutomatonIO e r m =&gt; e -&gt; e -&gt; Automaton e r -&gt; m $ AutomatonSame e r
<span class="lineno"> 40 </span><span class="decl"><span class="istickedoff">doInstruction src dst (Automaton ic ram) = pure $ Loop.continue $ Automaton ic' $ store dst diff ram where</span>
<span class="lineno"> 40 </span><span class="decl"><span class="istickedoff">doInstruction src dst (Automaton ic ram) = pure $ Trampoline.continue $ Automaton ic' $ store dst diff ram where</span>
<span class="lineno"> 41 </span><span class="spaces"> </span><span class="istickedoff">diff = genericLoad ram dst - genericLoad ram src</span>
<span class="lineno"> 42 </span><span class="spaces"> </span><span class="istickedoff">ic'</span>
<span class="lineno"> 43 </span><span class="spaces"> </span><span class="istickedoff">| diff &lt;= 0 = genericLoad ram $ ic + 2</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<span class="lineno"> 12 </span>import HelVM.HelMA.Automaton.IO.AutomatonIO
<span class="lineno"> 13 </span>import HelVM.HelMA.Automaton.IO.BusinessIO
<span class="lineno"> 14 </span>
<span class="lineno"> 15 </span>import HelVM.HelMA.Automaton.Loop
<span class="lineno"> 15 </span>import HelVM.HelMA.Automaton.Trampoline
<span class="lineno"> 16 </span>
<span class="lineno"> 17 </span>import HelVM.HelMA.Automaton.Types.DumpType
<span class="lineno"> 18 </span>import HelVM.HelMA.Automaton.Types.RAMType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<span class="lineno"> 1 </span>module HelVM.HelMA.Automaton.API.AutoOptions where
<span class="lineno"> 2 </span>
<span class="lineno"> 3 </span>import HelVM.HelMA.Automaton.API.OptimizationLevel
<span class="lineno"> 4 </span>import HelVM.HelMA.Automaton.Loop
<span class="lineno"> 4 </span>import HelVM.HelMA.Automaton.Trampoline
<span class="lineno"> 5 </span>import HelVM.HelMA.Automaton.Types.DumpType
<span class="lineno"> 6 </span>
<span class="lineno"> 7 </span>simpleAutoParams :: AutoOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<span class="lineno"> 12 </span>import HelVM.HelMA.Automaton.IO.AutomatonIO
<span class="lineno"> 13 </span>import HelVM.HelMA.Automaton.IO.BusinessIO
<span class="lineno"> 14 </span>
<span class="lineno"> 15 </span>import HelVM.HelMA.Automaton.Loop as Loop
<span class="lineno"> 16 </span>import HelVM.HelMA.Automaton.Optimizer
<span class="lineno"> 17 </span>import HelVM.HelMA.Automaton.Symbol
<span class="lineno"> 15 </span>import HelVM.HelMA.Automaton.Optimizer
<span class="lineno"> 16 </span>import HelVM.HelMA.Automaton.Symbol
<span class="lineno"> 17 </span>import HelVM.HelMA.Automaton.Trampoline as Trampoline
<span class="lineno"> 18 </span>
<span class="lineno"> 19 </span>import HelVM.HelMA.Automaton.Types.DumpType
<span class="lineno"> 20 </span>import HelVM.HelMA.Automaton.Types.RAMType
Expand Down Expand Up @@ -76,7 +76,7 @@
<span class="lineno"> 57 </span><span class="decl"><span class="istickedoff">runAndDumpLogs p = logDump (dumpType p) &lt;=&lt; run (limit p)</span></span>
<span class="lineno"> 58 </span>
<span class="lineno"> 59 </span>run :: (SRAutomatonIO Symbol s r m) =&gt; LimitMaybe -&gt; F s r m
<span class="lineno"> 60 </span><span class="decl"><span class="istickedoff">run = loopMWithLimit nextState</span></span>
<span class="lineno"> 60 </span><span class="decl"><span class="istickedoff">run = trampolineMWithLimit nextState</span></span>
<span class="lineno"> 61 </span>
<span class="lineno"> 62 </span>nextState :: (SRAutomatonIO Symbol s r m) =&gt; SF s r m
<span class="lineno"> 63 </span><span class="decl"><span class="istickedoff">nextState a = nextStateForInstruction =&lt;&lt; currentInstruction (memoryCM a) where</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<span class="lineno"> 4 </span>
<span class="lineno"> 5 </span>import HelVM.HelMA.Automaton.Instruction
<span class="lineno"> 6 </span>
<span class="lineno"> 7 </span>import HelVM.HelMA.Automaton.Loop as Loop
<span class="lineno"> 7 </span>import HelVM.HelMA.Automaton.Trampoline as Trampoline
<span class="lineno"> 8 </span>
<span class="lineno"> 9 </span>import HelVM.HelMA.Automaton.Symbol
<span class="lineno"> 10 </span>
Expand All @@ -38,9 +38,9 @@
<span class="lineno"> 19 </span>-- | Core of Combiner
<span class="lineno"> 20 </span>
<span class="lineno"> 21 </span>runInstruction :: (SRAutomatonIO Symbol s r m) =&gt; Instruction -&gt; SF s r m
<span class="lineno"> 22 </span><span class="decl"><span class="istickedoff">runInstruction (ISM i) a = Loop.continue . updateStack a &lt;$&gt; runALI i (memoryStack a)</span>
<span class="lineno"> 23 </span><span class="spaces"></span><span class="istickedoff">runInstruction (ILS i) a = Loop.continue . updateFromLSM a &lt;$&gt; runSLI i (toLSM a)</span>
<span class="lineno"> 24 </span><span class="spaces"></span><span class="istickedoff">runInstruction (ICF i) a = Loop.continue . updateFromCPM a &lt;$&gt; runCFI i (toCPM a)</span>
<span class="lineno"> 22 </span><span class="decl"><span class="istickedoff">runInstruction (ISM i) a = Trampoline.continue . updateStack a &lt;$&gt; runALI i (memoryStack a)</span>
<span class="lineno"> 23 </span><span class="spaces"></span><span class="istickedoff">runInstruction (ILS i) a = Trampoline.continue . updateFromLSM a &lt;$&gt; runSLI i (toLSM a)</span>
<span class="lineno"> 24 </span><span class="spaces"></span><span class="istickedoff">runInstruction (ICF i) a = Trampoline.continue . updateFromCPM a &lt;$&gt; runCFI i (toCPM a)</span>
<span class="lineno"> 25 </span><span class="spaces"></span><span class="istickedoff">runInstruction End a = end a</span></span>
<span class="lineno"> 26 </span>
<span class="lineno"> 27 </span>pop2ForStack :: (SRAutomatonIO Symbol s r m) =&gt; Memory s r -&gt; m (Symbol , Symbol , Memory s r)
Expand All @@ -51,7 +51,7 @@
<span class="lineno"> 32 </span><span class="decl"><span class="nottickedoff">push1ForStack e a = a { memoryStack = push1 e (memoryStack a) }</span></span>
<span class="lineno"> 33 </span>
<span class="lineno"> 34 </span>end :: (SRAutomatonIO Symbol s r m) =&gt; SF s r m
<span class="lineno"> 35 </span><span class="decl"><span class="istickedoff">end = pure . Loop.break</span></span>
<span class="lineno"> 35 </span><span class="decl"><span class="istickedoff">end = pure . Trampoline.break</span></span>
<span class="lineno"> 36 </span>
<span class="lineno"> 37 </span>-- | Constructors
<span class="lineno"> 38 </span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<span class="decl"><span class="nottickedoff">never executed</span> <span class="tickonlytrue">always true</span> <span class="tickonlyfalse">always false</span></span>
</pre>
<pre>
<span class="lineno"> 1 </span>module HelVM.HelMA.Automaton.Loop where
<span class="lineno"> 1 </span>module HelVM.HelMA.Automaton.Trampoline where
<span class="lineno"> 2 </span>
<span class="lineno"> 3 </span>import Control.Monad.Extra
<span class="lineno"> 4 </span>import Control.Type.Operator
Expand Down
Loading

0 comments on commit f2c58f3

Please sign in to comment.