102 lines
2.7 KiB
Elm
102 lines
2.7 KiB
Elm
module PC.Helpers exposing (..)
|
|
|
|
import Array exposing (Array)
|
|
import Http exposing (..)
|
|
|
|
-- Convert a number from 42 to 0042
|
|
-- number : (Prefered positive) whole number
|
|
-- length : positive whole number
|
|
-- Please note that only 0 are added. The number will not be shortend!
|
|
addLeadingZero : Int -> Int -> String
|
|
addLeadingZero number length =
|
|
if number >= 0 then
|
|
let
|
|
num_str = String.fromInt number
|
|
in
|
|
(String.fromList <| List.repeat (length - String.length num_str) '0'
|
|
)
|
|
++ num_str
|
|
else
|
|
let
|
|
num_str = String.fromInt <| 0-number
|
|
in
|
|
"-" ++
|
|
(String.fromList <| List.repeat (length - String.length num_str) '0'
|
|
)
|
|
++ num_str
|
|
|
|
|
|
-- Get the value at a given position in a List.
|
|
-- For performance reasons it might be better
|
|
-- to use an Array instead.
|
|
valueAt : Int -> List a -> Maybe a
|
|
valueAt pos list = List.head <| List.drop pos list
|
|
|
|
|
|
-- Change the value at a given position in a List.
|
|
-- If your pos is greater than the length of
|
|
-- the list, then the rest is filled with the
|
|
-- default element.
|
|
overwriteAt : a -> Int -> a ->List a -> List a
|
|
overwriteAt default pos newVal list =
|
|
let
|
|
len = List.length list
|
|
in
|
|
if pos > len then
|
|
list ++
|
|
(List.repeat (pos-len) default) ++
|
|
[newVal]
|
|
else
|
|
let
|
|
before = List.take pos list
|
|
after = List.drop (pos+1) list
|
|
in
|
|
before ++ [newVal] ++ after
|
|
|
|
overwriteAt_Arr : a -> Int -> a -> Array a -> Array a
|
|
overwriteAt_Arr default pos newVal list =
|
|
let
|
|
len = Array.length list
|
|
in
|
|
if pos > len then
|
|
Array.append list
|
|
<| Array.append (Array.repeat (pos-len) default)
|
|
<| Array.fromList [newVal]
|
|
else
|
|
Array.set pos newVal list
|
|
|
|
|
|
-- Prints Http Errors into something readable
|
|
printHttpError : Http.Error -> String
|
|
printHttpError err =
|
|
case err of
|
|
Http.BadUrl str -> "Bad Url: " ++ str
|
|
Http.Timeout -> "Timeout"
|
|
Http.NetworkError -> "Network Error"
|
|
Http.BadStatus num -> "Bad Status: " ++ String.fromInt num
|
|
Http.BadBody str -> "Bad Body: " ++ str
|
|
|
|
|
|
seperateInstructionsEntry : Int -> (Int, Int)
|
|
seperateInstructionsEntry i =
|
|
let
|
|
instruction = i // 100000
|
|
address = i - instruction*100000
|
|
in
|
|
(instruction, address)
|
|
|
|
|
|
valueAtRam : Int -> Array (Int, String) -> (Int, String)
|
|
valueAtRam pos arr =
|
|
case Array.get pos arr of
|
|
Just a -> a
|
|
Nothing -> (0, "")
|
|
|
|
|
|
changeAtRam : Int -> Int -> Array (Int, String) -> Array (Int,String)
|
|
changeAtRam pos newVal list =
|
|
let
|
|
(_,comment) = valueAtRam pos list
|
|
in
|
|
overwriteAt_Arr (0, "") pos (newVal, comment) list
|