From 0cc629684b8722c4760ad8ad3c6812ce71271a10 Mon Sep 17 00:00:00 2001 From: Nicolas Boulenguez Date: Sun, 20 Feb 2022 10:14:16 +0100 Subject: [PATCH] elm: merge more ideas from #450 Various trivial changes reducing the diff to #450. Dockerfile: npm already depends on nodejs Core.elm: change profile of deepEquals instead of uncurrying before each call. --- impls/elm/Dockerfile | 2 +- impls/elm/Makefile | 20 ++++++++++---------- impls/elm/bootstrap.js | 2 +- impls/elm/elm.json | 2 +- impls/elm/run | 2 +- impls/elm/{ => src}/Core.elm | 12 ++++++------ impls/elm/{ => src}/Env.elm | 5 +++-- impls/elm/{ => src}/Eval.elm | 0 impls/elm/{ => src}/IO.elm | 0 impls/elm/{ => src}/Printer.elm | 0 impls/elm/{ => src}/Reader.elm | 0 impls/elm/{ => src}/Step0_repl.elm | 0 impls/elm/{ => src}/Step1_read_print.elm | 0 impls/elm/{ => src}/Step2_eval.elm | 0 impls/elm/{ => src}/Step3_env.elm | 0 impls/elm/{ => src}/Step4_if_fn_do.elm | 0 impls/elm/{ => src}/Step5_tco.elm | 0 impls/elm/{ => src}/Step6_file.elm | 0 impls/elm/{ => src}/Step7_quote.elm | 0 impls/elm/{ => src}/Step8_macros.elm | 0 impls/elm/{ => src}/Step9_try.elm | 0 impls/elm/{ => src}/StepA_mal.elm | 0 impls/elm/{ => src}/Types.elm | 0 impls/elm/{ => src}/Utils.elm | 13 ++++++++++--- 24 files changed, 33 insertions(+), 25 deletions(-) rename impls/elm/{ => src}/Core.elm (99%) rename impls/elm/{ => src}/Env.elm (98%) rename impls/elm/{ => src}/Eval.elm (100%) rename impls/elm/{ => src}/IO.elm (100%) rename impls/elm/{ => src}/Printer.elm (100%) rename impls/elm/{ => src}/Reader.elm (100%) rename impls/elm/{ => src}/Step0_repl.elm (100%) rename impls/elm/{ => src}/Step1_read_print.elm (100%) rename impls/elm/{ => src}/Step2_eval.elm (100%) rename impls/elm/{ => src}/Step3_env.elm (100%) rename impls/elm/{ => src}/Step4_if_fn_do.elm (100%) rename impls/elm/{ => src}/Step5_tco.elm (100%) rename impls/elm/{ => src}/Step6_file.elm (100%) rename impls/elm/{ => src}/Step7_quote.elm (100%) rename impls/elm/{ => src}/Step8_macros.elm (100%) rename impls/elm/{ => src}/Step9_try.elm (100%) rename impls/elm/{ => src}/StepA_mal.elm (100%) rename impls/elm/{ => src}/Types.elm (100%) rename impls/elm/{ => src}/Utils.elm (91%) diff --git a/impls/elm/Dockerfile b/impls/elm/Dockerfile index c9781fc276..89dd381b1c 100644 --- a/impls/elm/Dockerfile +++ b/impls/elm/Dockerfile @@ -19,7 +19,7 @@ WORKDIR /mal # Specific implementation requirements ########################################################## -RUN apt-get -y install g++ libreadline-dev nodejs npm +RUN apt-get -y install g++ libreadline-dev npm ENV HOME /mal ENV NPM_CONFIG_CACHE /mal/.npm diff --git a/impls/elm/Makefile b/impls/elm/Makefile index 92e5a55ed6..c4c5e3a115 100644 --- a/impls/elm/Makefile +++ b/impls/elm/Makefile @@ -1,24 +1,24 @@ -SOURCES = Step0_repl.elm Step1_read_print.elm Step2_eval.elm \ - Step3_env.elm Step4_if_fn_do.elm Step5_tco.elm Step6_file.elm \ - Step7_quote.elm Step8_macros.elm Step9_try.elm StepA_mal.elm +SOURCES = src/Step0_repl.elm src/Step1_read_print.elm src/Step2_eval.elm src/\ + Step3_env.elm src/Step4_if_fn_do.elm src/Step5_tco.elm src/Step6_file.elm src/\ + Step7_quote.elm src/Step8_macros.elm src/Step9_try.elm src/StepA_mal.elm -BINS = $(SOURCES:S%.elm=s%.js) +BINS = $(SOURCES:src/Step%.elm=step%.js) -ELM_MAKE = node_modules/.bin/elm make +ELM = node_modules/.bin/elm all: node_modules $(BINS) node_modules: npm install -s%.js: S%.elm node_modules - $(ELM_MAKE) $< --output $@ +step%.js: src/Step%.elm node_modules + $(ELM) make $< --output $@ -STEP0_SOURCES = IO.elm -STEP1_SOURCES = $(STEP0_SOURCES) Reader.elm Printer.elm Utils.elm Types.elm Env.elm +STEP0_SOURCES = src/IO.elm +STEP1_SOURCES = $(STEP0_SOURCES) src/Reader.elm src/Printer.elm src/Utils.elm src/Types.elm src/Env.elm STEP2_SOURCES = $(STEP1_SOURCES) STEP3_SOURCES = $(STEP2_SOURCES) -STEP4_SOURCES = $(STEP3_SOURCES) Core.elm Eval.elm +STEP4_SOURCES = $(STEP3_SOURCES) src/Core.elm src/Eval.elm step0_repl.js: $(STEP0_SOURCES) step1_read_print.js: $(STEP1_SOURCES) diff --git a/impls/elm/bootstrap.js b/impls/elm/bootstrap.js index 52d2260606..53c2f60cb0 100644 --- a/impls/elm/bootstrap.js +++ b/impls/elm/bootstrap.js @@ -24,7 +24,7 @@ app.ports.readLine.subscribe(function(prompt) { }); // Read the contents of a file. -if (4 <= args[0][4] || args[0][4] == 'A') { +if ('readFile' in app.ports) { app.ports.readFile.subscribe(function(filename) { try { var contents = fs.readFileSync(filename, 'utf8'); diff --git a/impls/elm/elm.json b/impls/elm/elm.json index 13f7e5b1d4..d3cc54e049 100644 --- a/impls/elm/elm.json +++ b/impls/elm/elm.json @@ -1,7 +1,7 @@ { "type": "application", "source-directories": [ - "." + "src" ], "elm-version": "0.19.1", "dependencies": { diff --git a/impls/elm/run b/impls/elm/run index 9802b002f3..d22e4fbeac 100755 --- a/impls/elm/run +++ b/impls/elm/run @@ -1,2 +1,2 @@ -#!/bin/bash +#!/usr/bin/env bash exec node $(dirname $0)/bootstrap.js ${STEP:-stepA_mal} "${@}" diff --git a/impls/elm/Core.elm b/impls/elm/src/Core.elm similarity index 99% rename from impls/elm/Core.elm rename to impls/elm/src/Core.elm index 09f7ba6b84..4014b8f548 100644 --- a/impls/elm/Core.elm +++ b/impls/elm/src/Core.elm @@ -73,7 +73,7 @@ ns = True ( x :: xs, y :: ys ) -> - if deepEquals x y then + if deepEquals (x, y) then equalLists xs ys else @@ -99,11 +99,11 @@ ns = else zip (Dict.values a) (Dict.values b) - |> List.map (\(x,y) -> deepEquals x y) + |> List.map deepEquals |> List.all identity - deepEquals a b = - case ( a, b ) of + deepEquals c = + case c of ( MalList _ list, MalList _ otherList ) -> equalLists list otherList @@ -131,14 +131,14 @@ ns = ( _, MalMap _ _ ) -> False - _ -> + (a, b) -> a == b {- = -} equals args = case args of [ a, b ] -> - Eval.succeed <| MalBool (deepEquals a b) + Eval.succeed <| MalBool (deepEquals (a, b)) _ -> Eval.fail "unsupported arguments" diff --git a/impls/elm/Env.elm b/impls/elm/src/Env.elm similarity index 98% rename from impls/elm/Env.elm rename to impls/elm/src/Env.elm index 27ee0d0032..6edd412417 100644 --- a/impls/elm/Env.elm +++ b/impls/elm/src/Env.elm @@ -21,6 +21,7 @@ import Array import Dict import Set import Types exposing (Env, Frame, MalExpr(..), MalFunction(..)) +import Utils exposing (flip) debug : Env -> String -> a -> a @@ -358,7 +359,7 @@ gc expr env = value = getAtom atomId env in - countExpr value acc + countExpr value acc _ -> acc @@ -421,7 +422,7 @@ gc expr env = in countFrames initSet initSet |> countExpr expr - |> (\acc -> countList acc env.stack) + |> flip countList env.stack |> loop |> filterFrames env.frames |> makeNewEnv diff --git a/impls/elm/Eval.elm b/impls/elm/src/Eval.elm similarity index 100% rename from impls/elm/Eval.elm rename to impls/elm/src/Eval.elm diff --git a/impls/elm/IO.elm b/impls/elm/src/IO.elm similarity index 100% rename from impls/elm/IO.elm rename to impls/elm/src/IO.elm diff --git a/impls/elm/Printer.elm b/impls/elm/src/Printer.elm similarity index 100% rename from impls/elm/Printer.elm rename to impls/elm/src/Printer.elm diff --git a/impls/elm/Reader.elm b/impls/elm/src/Reader.elm similarity index 100% rename from impls/elm/Reader.elm rename to impls/elm/src/Reader.elm diff --git a/impls/elm/Step0_repl.elm b/impls/elm/src/Step0_repl.elm similarity index 100% rename from impls/elm/Step0_repl.elm rename to impls/elm/src/Step0_repl.elm diff --git a/impls/elm/Step1_read_print.elm b/impls/elm/src/Step1_read_print.elm similarity index 100% rename from impls/elm/Step1_read_print.elm rename to impls/elm/src/Step1_read_print.elm diff --git a/impls/elm/Step2_eval.elm b/impls/elm/src/Step2_eval.elm similarity index 100% rename from impls/elm/Step2_eval.elm rename to impls/elm/src/Step2_eval.elm diff --git a/impls/elm/Step3_env.elm b/impls/elm/src/Step3_env.elm similarity index 100% rename from impls/elm/Step3_env.elm rename to impls/elm/src/Step3_env.elm diff --git a/impls/elm/Step4_if_fn_do.elm b/impls/elm/src/Step4_if_fn_do.elm similarity index 100% rename from impls/elm/Step4_if_fn_do.elm rename to impls/elm/src/Step4_if_fn_do.elm diff --git a/impls/elm/Step5_tco.elm b/impls/elm/src/Step5_tco.elm similarity index 100% rename from impls/elm/Step5_tco.elm rename to impls/elm/src/Step5_tco.elm diff --git a/impls/elm/Step6_file.elm b/impls/elm/src/Step6_file.elm similarity index 100% rename from impls/elm/Step6_file.elm rename to impls/elm/src/Step6_file.elm diff --git a/impls/elm/Step7_quote.elm b/impls/elm/src/Step7_quote.elm similarity index 100% rename from impls/elm/Step7_quote.elm rename to impls/elm/src/Step7_quote.elm diff --git a/impls/elm/Step8_macros.elm b/impls/elm/src/Step8_macros.elm similarity index 100% rename from impls/elm/Step8_macros.elm rename to impls/elm/src/Step8_macros.elm diff --git a/impls/elm/Step9_try.elm b/impls/elm/src/Step9_try.elm similarity index 100% rename from impls/elm/Step9_try.elm rename to impls/elm/src/Step9_try.elm diff --git a/impls/elm/StepA_mal.elm b/impls/elm/src/StepA_mal.elm similarity index 100% rename from impls/elm/StepA_mal.elm rename to impls/elm/src/StepA_mal.elm diff --git a/impls/elm/Types.elm b/impls/elm/src/Types.elm similarity index 100% rename from impls/elm/Types.elm rename to impls/elm/src/Types.elm diff --git a/impls/elm/Utils.elm b/impls/elm/src/Utils.elm similarity index 91% rename from impls/elm/Utils.elm rename to impls/elm/src/Utils.elm index 0c0e79d95c..b6de08c474 100644 --- a/impls/elm/Utils.elm +++ b/impls/elm/src/Utils.elm @@ -1,6 +1,7 @@ module Utils exposing ( decodeString , encodeString + , flip , justValues , last , makeCall @@ -39,9 +40,10 @@ decodeString = regex : String -> Regex.Regex -regex str = case Regex.fromString str of - Nothing -> Debug.todo "invalid regex" - Just r -> r +regex str = + case Regex.fromString str of + Nothing -> Debug.todo "invalid regex" + Just r -> r encodeString : String -> String @@ -122,3 +124,8 @@ justValues list = Nothing :: rest -> justValues rest + + +flip : (a -> b -> c) -> (b -> a -> c) +flip f b a = + f a b