...
 
Commits (1)
......@@ -6,12 +6,14 @@
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"Gizra/elm-keyboard-event": "1.0.1",
"SwiftsNamesake/proper-keyboard": "4.0.0",
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0"
"elm/html": "1.0.0",
"elm/json": "1.1.3"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2"
......
module Main exposing (main)
import Browser
import Browser.Events exposing (onKeyDown)
import Html exposing (Html, button, div, h1, span, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import Json.Decode as D
import Keyboard.Event as KE exposing (KeyboardEvent, decodeKeyboardEvent)
import Keyboard.Key as KK
......@@ -18,13 +22,24 @@ type alias Model =
}
initialModel : Model
initialModel =
{ stack = []
, currentNum = "0"
, error = Nothing
, dirty = False
}
init : ( Model, Cmd Msg )
init =
( { stack = []
, currentNum = "0"
, error = Nothing
, dirty = False
}
, Cmd.none
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
onKeyDown (D.map HandleKeyboardEvent decodeKeyboardEvent)
......@@ -79,47 +94,50 @@ type Msg
| Enter
| SetDecimal
| SetSign
| HandleKeyboardEvent KeyboardEvent
update : Msg -> Model -> Model
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetDecimal ->
if String.contains "." model.currentNum then
model
( model, Cmd.none )
else
{ model | currentNum = model.currentNum ++ "." }
( { model | currentNum = model.currentNum ++ "." }, Cmd.none )
SetSign ->
if model.currentNum == "0" then
model
( model, Cmd.none )
else if String.startsWith "-" model.currentNum then
{ model | currentNum = String.dropLeft 1 model.currentNum }
( { model | currentNum = String.dropLeft 1 model.currentNum }, Cmd.none )
else
{ model | currentNum = "-" ++ model.currentNum }
( { model | currentNum = "-" ++ model.currentNum }, Cmd.none )
ClearAll ->
initialModel
init
Clear ->
{ model | currentNum = "0", dirty = False }
( { model | currentNum = "0", dirty = False }, Cmd.none )
Back ->
let
newNum =
String.dropRight 1 model.currentNum
in
{ model
( { model
| currentNum =
if String.isEmpty newNum then
"0"
else
newNum
}
}
, Cmd.none
)
Enter ->
let
......@@ -128,15 +146,15 @@ update msg model =
in
case maybeNumber of
Nothing ->
{ model | error = Just "PARSE ERR" }
( { model | error = Just "PARSE ERR" }, Cmd.none )
Just num ->
{ model | stack = num :: model.stack, dirty = True }
( { model | stack = num :: model.stack, dirty = True }, Cmd.none )
InputOperator operator ->
case model.stack of
[] ->
model
( model, Cmd.none )
x :: xs ->
let
......@@ -148,28 +166,90 @@ update msg model =
in
case maybeNumber of
Nothing ->
{ model | error = Just "PARSE ERR" }
( { model | error = Just "PARSE ERR" }, Cmd.none )
Just num ->
let
newNum =
op num x
in
{ model
( { model
| stack = xs
, currentNum = String.fromFloat newNum
, dirty = True
}
}
, Cmd.none
)
InputNumber num ->
if model.currentNum == "0" then
{ model | currentNum = String.fromFloat num }
let
newModel =
if model.currentNum == "0" then
{ model | currentNum = String.fromFloat num }
else if model.dirty then
{ model | currentNum = String.fromFloat num, dirty = False }
else if model.dirty then
{ model | currentNum = String.fromFloat num, dirty = False }
else
{ model | currentNum = model.currentNum ++ String.fromFloat num }
else
{ model | currentNum = model.currentNum ++ String.fromFloat num }
in
( newModel, Cmd.none )
HandleKeyboardEvent event ->
case event.keyCode of
KK.Multiply ->
update (InputOperator Mult) model
KK.Divide ->
update (InputOperator Div) model
KK.Add ->
update (InputOperator Add) model
KK.Subtract ->
update (InputOperator Sub) model
KK.Decimal ->
update SetDecimal model
KK.Enter ->
update Enter model
KK.Backspace ->
update Back model
KK.NumpadZero ->
update (InputNumber 0) model
KK.NumpadOne ->
update (InputNumber 1) model
KK.NumpadTwo ->
update (InputNumber 2) model
KK.NumpadThree ->
update (InputNumber 3) model
KK.NumpadFour ->
update (InputNumber 4) model
KK.NumpadFive ->
update (InputNumber 5) model
KK.NumpadSix ->
update (InputNumber 6) model
KK.NumpadSeven ->
update (InputNumber 7) model
KK.NumpadEight ->
update (InputNumber 8) model
KK.NumpadNine ->
update (InputNumber 9) model
_ ->
( model, Cmd.none )
......@@ -302,13 +382,14 @@ view model =
-- PROGRAM
---- PROGRAM ----
main : Program () Model Msg
main =
Browser.sandbox
{ init = initialModel
, view = view
Browser.element
{ view = view
, init = \_ -> init
, update = update
, subscriptions = subscriptions
}