Work on usable codegen

- escape js names
- executable output
- better FC in parsing
- experiment with IO
This commit is contained in:
2024-09-29 22:17:51 -07:00
parent 76e627825c
commit 151f678f75
7 changed files with 116 additions and 18 deletions

30
newt/IO.newt Normal file
View File

@@ -0,0 +1,30 @@
module IO
import Prelude
data Foo : U where
MkFoo : Nat -> Nat -> Foo
data World : U where
data IORes : U -> U where
MkIORes : {a : U} -> a -> World -> IORes a
IO : U -> U
IO a = World -> IORes a
iobind : {a b : U} -> IO a -> (a -> IO b) -> IO b
iobind ma mab w = case ma w of
(MkIORes a w) => mab a w
_>>=_ : {a b : U} -> IO a -> (a -> IO b) -> IO b
_>>=_ = iobind
infixr 2 _>>=_
infixr 2 _>>_
-- FIXME - bad parse error for lambda without the parens
_>>_ : {a b : U} -> IO a -> IO b -> IO b
_>>_ ma mb = ma >>= (\ _ => mb)

17
newt/JSLib.newt Normal file
View File

@@ -0,0 +1,17 @@
module JSLib
ptype Int
ptype String
infixl 4 _+_
infixl 5 _*_
pfunc _+_ : Int -> Int -> Int := "(x,y) => x + y"
pfunc _*_ : Int -> Int -> Int := "(x,y) => x * y"
ptype JVoid
-- REVIEW - maybe we only have body, use names from the pi-type and generate
-- the arrow (or inline?) ourselves
pfunc log : String -> JVoid := "x => console.log(x)"
pfunc debug : {a : U} -> String -> a -> JVoid := "(_,x,a) => console.log(x,a)"

22
newt/Prelude.newt Normal file
View File

@@ -0,0 +1,22 @@
module Prelude
data Nat : U where
Z : Nat
S : Nat -> Nat
data Maybe : U -> U where
Just : {a : U} -> a -> Maybe a
Nothing : {a : U} -> Maybe a
data Either : U -> U -> U where
Left : {a b : U} -> a -> Either a b
Right : {a b : U} -> b -> Either a b
-- TODO this is special cased in some languages, maybe for easier
-- inference? Figure out why.
infixr 0 _$_
_$_ : {a b : U} -> (a -> b) -> a -> b
f $ a = f a