Skip to content

Commit

Permalink
More builtins and extended core
Browse files Browse the repository at this point in the history
* implement set for isTuple

* Add default impl: isChild(isParent) => true

* Implement list interfaces for isListOf

* Add RegExp

* Update README.md
  • Loading branch information
divs1210 authored Nov 1, 2023
1 parent 4ef320b commit bcbed0c
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 66 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ PredScript is an attempt to make a flexible dependently-typed functional languag
with familiar (easy / mainstream / JS-like) syntax that lies
somewhere on the spectrum of `Dynamically Typed → Statically Typed`.

## Use Case
## Code Example

```typescript
let isUser: isPred = gen_isRecord({
interface isEmailString extends isString;
function isEmailString(s: isString): isBool {
/^\S+@\S+\.\S+$/
.test($this, s)
}

let isUser: isPred = gen_isRecord({
"id": isInt,
"username": isString
"username": isString,
"email": isEmailString
});

// this can be type checked at compile time
async function fetchUserById(id: isInt): isUser {
(baseURL + "/users/" + id)
.fetch
.await
.get($this, "body")
.parseJSON
// validate and cast at runtime
.as(isUser, $this)
}

fetchUserById(5).then($this, println);
let u: isUser = as(isUser, {
"id": 1,
"username": "johndoe",
"email": "[email protected]"
});
```
## Design goals
Expand Down
Binary file modified examples/core.ps
Binary file not shown.
Binary file added examples/regex.ps
Binary file not shown.
67 changes: 66 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { val, isNull: isJSNull } = require('./util');
// reify JS
// ========
const _String = globalThis.String;
const _RegExp = globalThis.RegExp;


// objects and types
Expand Down Expand Up @@ -257,6 +258,12 @@ Implement(
isBool,
_ => TRUE
);
Implement(
isChild,
List(isParent),
isBool,
_ => TRUE
);
return NULL;
}
);
Expand Down Expand Up @@ -637,6 +644,48 @@ Implement(
);


// RegExps
// =======
const isRegExp = MultiFn("isRegExp", isAny);

const RegExp = MultiFn("RegExp");
Implement(
RegExp,
List(isString),
isRegExp,
src => {
let jsSrc = val(src);
let jsRegExp = new _RegExp(jsSrc);
return Obj(jsRegExp, isRegExp);
}
);
Implement(
RegExp,
List(isString, isString),
isRegExp,
(src, flags) => {
let jsSrc = val(src);
let jsFlags = val(flags);
let jsRegExp = new _RegExp(jsSrc, jsFlags);
return Obj(jsRegExp, isRegExp);
}
);

const newRegExp = r => Obj(r, isRegExp);

const test = MultiFn('test');
Implement(
test,
List(isRegExp, isString),
isBool,
(r, s) => {
let jsS = val(s);
let jsR = val(r);
return Bool(jsR.test(jsS));
}
);


// toList
// ======
const toList = MultiFn("toList");
Expand Down Expand Up @@ -964,6 +1013,17 @@ const assertError = _Lambda((checkFn, msg) => {
});


// errors
const error = _Lambda((msg, obj) => {
obj = obj || NULL;

let e = new Error(val(msg));
e.__psData__ = obj;

throw e;
});


module.exports = {
MultiFn,
isFn,
Expand Down Expand Up @@ -1020,6 +1080,10 @@ module.exports = {
String,
isString,
toString,
isRegExp,
RegExp,
newRegExp,
test,
isRef,
Ref,
println,
Expand All @@ -1035,5 +1099,6 @@ module.exports = {
assert,
assertError,
loop,
recur
recur,
error
};
3 changes: 3 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function compileLiteral(node) {
return `Char('${val}')`;
case 'string':
return `String(\`${val}\`)`;
case 'regex':
return `newRegExp(${val})`;
case 'null':
return `NULL`;
default: {
Expand Down Expand Up @@ -170,6 +172,7 @@ function compileAST(ast) {
case 'bool':
case 'char':
case 'string':
case 'regex':
case 'null':
return compileLiteral(ast);
case 'symbol':
Expand Down
Loading

0 comments on commit bcbed0c

Please sign in to comment.