From 90f3229af595dc7acd596aec405037e74d5247f6 Mon Sep 17 00:00:00 2001 From: Steve Dunham Date: Wed, 11 Sep 2024 16:23:14 -0700 Subject: [PATCH] Don't run switch for single cases --- TODO.md | 5 +++-- src/Lib/Compile.idr | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index d0a6d67..4d88831 100644 --- a/TODO.md +++ b/TODO.md @@ -8,6 +8,7 @@ - [ ] operators - [ ] import - [ ] add {{ }} and solving autos +- [ ] do blocks - [ ] some solution for + (classes? ambiguity?) - [ ] show compiler failure in the editor (exit code != 0) - [ ] write js files into `out` directory @@ -17,8 +18,8 @@ - [ ] type at point in vscode - [ ] repl - [ ] LSP -- [ ] don't match forced constructors at runtime - - maybe do this in codegen if there is only one case. +- [x] don't match forced constructors at runtime + - I think we got this by not switching for single cases - [ ] magic nat (codegen as number with appropriate pattern matching) - [ ] magic tuple? (codegen as array) - [ ] magic newtype? (drop in codegen) diff --git a/src/Lib/Compile.idr b/src/Lib/Compile.idr index 594f417..d09fafe 100644 --- a/src/Lib/Compile.idr +++ b/src/Lib/Compile.idr @@ -119,19 +119,21 @@ termToJS env (CCase t alts) f = -- TODO default case, let's drop the extra field. termToJS env t $ \case - (Var nm) => (JCase (Dot (Var nm) "tag") (map (termToJSAlt nm) alts)) + (Var nm) => maybeCaseStmt nm alts t' => let nm = fresh "sc" env in - JSnoc (JConst nm t') - (JCase (Dot (Var nm) "tag") (map (termToJSAlt nm) alts)) + JSnoc (JConst nm t') (maybeCaseStmt nm alts) + where termToJSAlt : String -> CAlt -> JAlt termToJSAlt nm (CConAlt name args u) = JConAlt name (termToJS (mkEnv nm 0 env args) u f) -- intentionally reusing scrutinee name here termToJSAlt nm (CDefAlt u) = JDefAlt (termToJS (Var nm :: env) u f) - label : JSExp -> (String -> JSStmt e) -> JSStmt e - label (Var nm) f = f nm - label t f = ?label_rhs + + maybeCaseStmt : String -> List CAlt -> JSStmt e + -- If there is a single alt, assume it matched + maybeCaseStmt nm [(CConAlt _ args u)] = (termToJS (mkEnv nm 0 env args) u f) + maybeCaseStmt nm alts = (JCase (Dot (Var nm) "tag") (map (termToJSAlt nm) alts)) -- FIXME escape