primop in compiler

This commit is contained in:
2025-04-07 14:29:55 -07:00
parent eeb790f1b2
commit c51d368e90
4 changed files with 9 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ data JSExp : U where
Apply : JSExp -> List JSExp -> JSExp
Var : String -> JSExp
JLam : List String -> JSStmt Return -> JSExp
JPrimOp : String JSExp JSExp JSExp
JUndefined : JSExp
Index : JSExp -> JSExp -> JSExp
Dot : JSExp -> String -> JSExp
@@ -134,6 +135,7 @@ termToJS env (CFun nms t) f =
let (nms', env') = freshNames nms env
in f $ JLam nms' (termToJS env' t JReturn)
termToJS env (CRef nm) f = f $ Var (show nm)
termToJS env (CPrimOp op t u) f = termToJS env t $ \ t => termToJS env u $ \ u => f $ JPrimOp op t u
termToJS env (CMeta k) f = f $ LitString "META \{show k}"
termToJS env (CLit lit) f = f (litToJS lit)
-- if it's a var, just use the original
@@ -258,6 +260,7 @@ expToDoc (JLam nms body) = text "(" <+> commaSep (map jsIdent nms) <+> text ") =
expToDoc JUndefined = text "null"
expToDoc (Index obj ix) = expToDoc obj ++ text "(" ++ expToDoc ix ++ text " :: Nil)"
expToDoc (Dot obj nm) = expToDoc obj ++ text "." ++ jsIdent nm
expToDoc (JPrimOp op t u) = text "(" ++ expToDoc t <+> text op <+> expToDoc u ++ text ")"
caseBody : e. JSStmt e -> Doc
caseBody stmt@(JReturn x) = nest 2 (line ++ stmtToDoc stmt)
@@ -357,6 +360,7 @@ sortedNames defs qn = go Nil Nil qn
getNames acc (CMeta _) = acc
getNames acc (CBnd _) = acc
getNames acc CErased = acc
getNames acc (CPrimOp op t u) = getNames (getNames acc t) u
go : List QName → List QName → QName → List QName
go loop acc qn =

View File

@@ -42,6 +42,9 @@ data CExp : U where
CConstr : Name -> List CExp -> CExp
-- Raw javascript for `pfunc`
CRaw : String -> List QName -> CExp
-- Need this for magic Nat
-- TODO - use for primitives too
CPrimOp : String CExp CExp -> CExp
-- I'm counting Lam in the term for arity. This matches what I need in
-- code gen.

View File

@@ -39,6 +39,7 @@ tailNames CErased = Nil
tailNames (CLit _) = Nil
tailNames (CMeta _) = Nil
tailNames (CRaw _ _) = Nil
tailNames (CPrimOp _ _ _) = Nil
-- rewrite tail calls to return an object
rewriteTailCalls : List QName CExp CExp