Don't run switch for single cases

This commit is contained in:
2024-09-11 16:23:14 -07:00
parent db7d2ce73d
commit 90f3229af5
2 changed files with 11 additions and 8 deletions

View File

@@ -8,6 +8,7 @@
- [ ] operators - [ ] operators
- [ ] import - [ ] import
- [ ] add {{ }} and solving autos - [ ] add {{ }} and solving autos
- [ ] do blocks
- [ ] some solution for + (classes? ambiguity?) - [ ] some solution for + (classes? ambiguity?)
- [ ] show compiler failure in the editor (exit code != 0) - [ ] show compiler failure in the editor (exit code != 0)
- [ ] write js files into `out` directory - [ ] write js files into `out` directory
@@ -17,8 +18,8 @@
- [ ] type at point in vscode - [ ] type at point in vscode
- [ ] repl - [ ] repl
- [ ] LSP - [ ] LSP
- [ ] don't match forced constructors at runtime - [x] don't match forced constructors at runtime
- maybe do this in codegen if there is only one case. - I think we got this by not switching for single cases
- [ ] magic nat (codegen as number with appropriate pattern matching) - [ ] magic nat (codegen as number with appropriate pattern matching)
- [ ] magic tuple? (codegen as array) - [ ] magic tuple? (codegen as array)
- [ ] magic newtype? (drop in codegen) - [ ] magic newtype? (drop in codegen)

View File

@@ -119,19 +119,21 @@ termToJS env (CCase t alts) f =
-- TODO default case, let's drop the extra field. -- TODO default case, let's drop the extra field.
termToJS env t $ \case termToJS env t $ \case
(Var nm) => (JCase (Dot (Var nm) "tag") (map (termToJSAlt nm) alts)) (Var nm) => maybeCaseStmt nm alts
t' => t' =>
let nm = fresh "sc" env in let nm = fresh "sc" env in
JSnoc (JConst nm t') JSnoc (JConst nm t') (maybeCaseStmt nm alts)
(JCase (Dot (Var nm) "tag") (map (termToJSAlt nm) alts))
where where
termToJSAlt : String -> CAlt -> JAlt termToJSAlt : String -> CAlt -> JAlt
termToJSAlt nm (CConAlt name args u) = JConAlt name (termToJS (mkEnv nm 0 env args) u f) termToJSAlt nm (CConAlt name args u) = JConAlt name (termToJS (mkEnv nm 0 env args) u f)
-- intentionally reusing scrutinee name here -- intentionally reusing scrutinee name here
termToJSAlt nm (CDefAlt u) = JDefAlt (termToJS (Var nm :: env) u f) termToJSAlt nm (CDefAlt u) = JDefAlt (termToJS (Var nm :: env) u f)
label : JSExp -> (String -> JSStmt e) -> JSStmt e
label (Var nm) f = f nm maybeCaseStmt : String -> List CAlt -> JSStmt e
label t f = ?label_rhs -- 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 -- FIXME escape