Skip to content

Commit

Permalink
Add seed_types +, *, and /.
Browse files Browse the repository at this point in the history
Part of #47.
  • Loading branch information
jkomoros committed Jul 8, 2023
1 parent 78d6121 commit 66df2b0
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 4 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,30 @@ Returns the negation of a
Required parameters:
- `a` - The value to negate
#### +
Returns the sum of a and b
Required parameters:
- `a` - The left hand side
- `b` - The right hand side. If not provided, will be 1.
#### *
Returns the product of a and b
Required parameters:
- `a` - The left hand side
- `b` - The right hand side. If not provided, will be 1.
#### /
Returns the division of a by b
Required parameters:
- `a` - The left hand side
- `b` - The right hand side. If not provided, will be 1.
### Templates
The `render` seed_type takes a template string and some variables and renders a new string.
Expand Down
200 changes: 197 additions & 3 deletions seed-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,200 @@
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
},
"description": {
"$ref": "#/definitions/seedData/anyOf/0/properties/description"
},
"type": {
"type": "string",
"const": "+"
},
"a": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2"
}
],
"description": "The left hand side"
}
]
},
"b": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"not": {}
},
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2"
}
]
}
],
"description": "The right hand side"
}
]
}
},
"required": [
"type",
"a"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
},
"description": {
"$ref": "#/definitions/seedData/anyOf/0/properties/description"
},
"type": {
"type": "string",
"const": "*"
},
"a": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2"
}
],
"description": "The left hand side"
}
]
},
"b": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"not": {}
},
{
"$ref": "#/definitions/seedData/anyOf/15/properties/b/anyOf/2/anyOf/1"
}
],
"description": "The right hand side"
}
]
}
},
"required": [
"type",
"a"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"$ref": "#/definitions/seedData/anyOf/0/properties/id"
},
"description": {
"$ref": "#/definitions/seedData/anyOf/0/properties/description"
},
"type": {
"type": "string",
"const": "/"
},
"a": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2"
}
],
"description": "The left hand side"
}
]
},
"b": {
"anyOf": [
{
"$ref": "#/definitions/seedData"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
},
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/5/properties/value/anyOf/2/anyOf/1/additionalProperties/anyOf/2"
}
],
"description": "The right hand side"
}
]
}
},
"required": [
"type",
"a",
"b"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down Expand Up @@ -1387,7 +1581,7 @@
"items": {
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/20/properties/properties/additionalProperties/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/23/properties/properties/additionalProperties/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
Expand All @@ -1401,7 +1595,7 @@
"return": {
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/20/properties/properties/additionalProperties/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/23/properties/properties/additionalProperties/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/0/properties/prompt/anyOf/1"
Expand Down Expand Up @@ -1507,7 +1701,7 @@
{
"anyOf": [
{
"$ref": "#/definitions/seedData/anyOf/22/properties/name/anyOf/2/anyOf/0"
"$ref": "#/definitions/seedData/anyOf/25/properties/name/anyOf/2/anyOf/0"
},
{
"$ref": "#/definitions/seedData/anyOf/2/properties/memory/anyOf/2/anyOf/1"
Expand Down
42 changes: 41 additions & 1 deletion src/grow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ import {
arrayReturnType,
SeedDataLetMulti,
EnvironmentData,
SeedDataNoop
SeedDataNoop,
SeedDataAdd,
SeedDataMultiply,
SeedDataDivide
} from './types.js';

import {
Expand Down Expand Up @@ -356,6 +359,34 @@ const growGreaterThanOrEqualTo = async (seed : Seed<SeedDataGreaterThanOrEqualTo
return a >= b;
};

const growAdd = async (seed : Seed<SeedDataAdd>, env : Environment) : Promise<number> => {
const data = seed.data;
const a = await getProperty(seed, env, data.a);
const b = await getProperty(seed, env, data.b, 1);
if (typeof a != 'number') throw new Error('a is not a number');
if (typeof b != 'number') throw new Error('b is not a number');
return a + b;
};

const growMultiply = async (seed : Seed<SeedDataMultiply>, env : Environment) : Promise<number> => {
const data = seed.data;
const a = await getProperty(seed, env, data.a);
const b = await getProperty(seed, env, data.b, 1);
if (typeof a != 'number') throw new Error('a is not a number');
if (typeof b != 'number') throw new Error('b is not a number');
return a * b;
};

const growDivide = async (seed : Seed<SeedDataDivide>, env : Environment) : Promise<number> => {
const data = seed.data;
const a = await getProperty(seed, env, data.a);
const b = await getProperty(seed, env, data.b, 1);
if (typeof a != 'number') throw new Error('a is not a number');
if (typeof b != 'number') throw new Error('b is not a number');
//This will throw if b is 0
return a / b;
};

const growNot = async (seed : Seed<SeedDataNot>, env : Environment) : Promise<boolean> => {
const data = seed.data;
const a = await getProperty(seed, env, data.a);
Expand Down Expand Up @@ -610,6 +641,15 @@ export const grow = async (seed : Seed, env : Environment) : Promise<Value> => {
case '!':
result = await growNot(seed as Seed<SeedDataNot>, env);
break;
case '+':
result = await growAdd(seed as Seed<SeedDataAdd>, env);
break;
case '*':
result = await growMultiply(seed as Seed<SeedDataMultiply>, env);
break;
case '/':
result = await growDivide(seed as Seed<SeedDataDivide>, env);
break;
case 'render':
result = await growRender(seed as Seed<SeedDataRender>, env);
break;
Expand Down
45 changes: 45 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,45 @@ const seedDataNot = makeSeedData(seedDataConfigNot);

export type SeedDataNot = z.infer<typeof seedDataNot>;

const seedDataConfigAdd = {
type: z.literal('+'),
properties: {
a: inputNonObjectValue.describe('The left hand side'),
b: inputNonObjectValue.optional().describe('The right hand side')
}
};

const nestedSeedDataAdd = makeNestedSeedData(seedDataConfigAdd);
const seedDataAdd = makeSeedData(seedDataConfigAdd);

export type SeedDataAdd = z.infer<typeof seedDataAdd>;

const seedDataConfigMultiply = {
type: z.literal('*'),
properties: {
a: inputNonObjectValue.describe('The left hand side'),
b: inputNonObjectValue.optional().describe('The right hand side')
}
};

const nestedSeedDataMultiply = makeNestedSeedData(seedDataConfigMultiply);
const seedDataMultiply = makeSeedData(seedDataConfigMultiply);

export type SeedDataMultiply = z.infer<typeof seedDataMultiply>;

const seedDataConfigDivide = {
type: z.literal('/'),
properties: {
a: inputNonObjectValue.describe('The left hand side'),
b: inputNonObjectValue.describe('The right hand side')
}
};

const nestedSeedDataDivide = makeNestedSeedData(seedDataConfigDivide);
const seedDataDivide = makeSeedData(seedDataConfigDivide);

export type SeedDataDivide = z.infer<typeof seedDataDivide>;

const seedDataConfigRender = {
type: z.literal('render'),
properties: {
Expand Down Expand Up @@ -751,6 +790,9 @@ export const expandedSeedData = z.discriminatedUnion('type', [
seedDataLessThanOrEqualTo,
seedDataGreaterThanOrEqualTo,
seedDataNot,
seedDataAdd,
seedDataMultiply,
seedDataDivide,
seedDataRender,
seedDataCompose,
seedDataExtract,
Expand Down Expand Up @@ -784,6 +826,9 @@ export const seedData = z.discriminatedUnion('type', [
nestedSeedDataLessThanOrEqualTo,
nestedSeedDataGreaterThanOrEqaulTo,
nestedSeedDataNot,
nestedSeedDataAdd,
nestedSeedDataMultiply,
nestedSeedDataDivide,
nestedSeedDataRender,
nestedSeedDataCompose,
nestedSeedDataExtract,
Expand Down

0 comments on commit 66df2b0

Please sign in to comment.