Skip to content

Instantly share code, notes, and snippets.

@roetlich
Created August 12, 2020 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roetlich/c2b94e569d15447073c7c4cfccdd6f6e to your computer and use it in GitHub Desktop.
Save roetlich/c2b94e569d15447073c7c4cfccdd6f6e to your computer and use it in GitHub Desktop.
IntercalScript web server, with fake type declarations.
{-# language foreign-function-interface #-}
#import {Trait, String, Int, Function, Function2} from "types"
`import http from "http";`;
Response = Trait(funct(r)
String( r.contentType );
Int( r.code );
String( r.msg );
end);
Request = Trait(funct(r)
String( r.url )
end);
HttpResponder = Function(Request,Response);
ok-html = Function(String,Response)(funct(msg)
Response({
contentType: "text/html",
code: 200,
msg: msg
})
end);
link = Function2(String,String,String)(funct(url, text)
"<a href='" +' url +' "'> " +' text +' " </a>"
end);
respond = HttpResponder(
funct(req)
if req.url == "/hello" then
ok-html("Welcome!")
else if req.url == "/" then
ok-html("Main page. Check out " +' link("/hello", "the hello page."))
else
{
contentType: "text/html",
code: 404,
msg: "Error 404, " +' req.url +' " doesn't exist. :("
}
end end
end
);
activate-server = funct(responder, port)
HttpResponder(responder);
Int(port);
create-server = `(f,port) => http.createServer(function (req, res) {
const request = {url: req.url}
const {contentType, code, msg} = f(request)
res.writeHead(code, {'Content-Type': contentType});
res.write(msg);
res.end();
}).listen(port)`;
print("Starting Server on port " +' port +' "..." );
create-server(responder, port);
end;
activate-server(respond, 8080);
{- # language rank1-types # -}
typeof = $funct(var)
funct(x)
if true then
x
else
var
end
end
end;
typegen = $funct(var)
funct(x)
if true then
x
else
var(x)
end
end
end;
Trait = $funct(var)
funct(x)
if true then
x
else
var(x);
x
end
end
end;
Number = funct(num) num +. 0 end;
String = funct(str) str +' "" end;
Int = funct(int) int + 0 end;
Bool = funct(bool) if bool then true else false end end;
Basic = {Number, String, Int, Bool};
Function = $funct(From, To)
funct(f)
if true
then f
else
funct(a) To(f(From(a))) end
end
end
end;
Function2 = $funct(From1, From2, To)
funct(f)
if true
then f
else
funct(a,b) To(f(From1(a), From2(b))) end
end
end
end;
Function3 = $funct(From1, From2, From3, To)
funct(f)
if true
then f
else
funct(a,b,c) To(f(From1(a), From2(b), From3(c))) end
end
end
end;
Tuple = $funct(TypeA, TypeB)
funct(t)
if true then
t
else
let (a,b) = t;
a = TypeA(a);
b = TypeB(b);
(a,b)
end
end
end;
Tuple3 = $funct(TypeA, TypeB, TypeC)
funct(t)
if true then
t
else
let (a,b,c) = t;
a = TypeA(a);
b = TypeB(b);
c = TypeC(c);
(a,b)
end
end
end;
Array = $funct(Type)
funct(array)
if true then
array
else
do for x in array then
x = Type(x)
end;
array
end
end
end;
Types = {typeof, Trait, Number, String, Int, Bool, Function, Function2, Function3, Tuple, Tuple3, Array };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment