Skip to content

Commit

Permalink
Merge branch 'main' into fix-webui-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyballentine authored Jul 28, 2023
2 parents 478e5eb + 4485a8f commit 9f718e5
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,25 @@ def compareCondition(b: uint): bool {
SideSelection::Width => if compareCondition(w) { same } else {
Size {
width: target,
height: max(int & round((target / w) * h), 1)
height: max(round((target / w) * h), 1)
}
},
SideSelection::Height => if compareCondition(h) { same } else {
Size {
width: max(int & round((target / h) * w), 1),
width: max(round((target / h) * w), 1),
height: target
}
},
SideSelection::ShorterSide => if compareCondition(min(h, w)) { same } else {
Size {
width: max(int & round((target / min(h, w)) * w), 1),
height: max(int & round((target / min(h, w)) * h), 1)
width: max(round((target / min(h, w)) * w), 1),
height: max(round((target / min(h, w)) * h), 1)
}
},
SideSelection::LongerSide => if compareCondition(max(h, w)) { same } else {
Size {
width: max(int & round((target / max(h, w)) * w), 1),
height: max(int & round((target / max(h, w)) * h), 1)
width: max(round((target / max(h, w)) * w), 1),
height: max(round((target / max(h, w)) * h), 1)
}
},
};
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
},
"dependencies": {
"@babel/plugin-transform-react-jsx": "^7.17.12",
"@chainner/navi": "^0.5.0",
"@chainner/navi": "^0.6.0",
"@chakra-ui/icons": "^2.0.11",
"@chakra-ui/react": "^2.3.5",
"@emotion/react": "^11.9.0",
Expand Down
5 changes: 3 additions & 2 deletions src/common/nodes/TypeState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { EvaluationError, NonNeverType, StructType, Type, isSameType } from '@chainner/navi';
import { EvaluationError, NonNeverType, Type, isSameType } from '@chainner/navi';
import { EdgeData, InputId, NodeData, OutputId, SchemaId } from '../common-types';
import { log } from '../log';
import { FunctionDefinition, FunctionInstance } from '../types/function';
import { nullType } from '../types/util';
import { EMPTY_MAP } from '../util';
import { EdgeState } from './EdgeState';
import type { Edge, Node } from 'reactflow';
Expand Down Expand Up @@ -95,7 +96,7 @@ export class TypeState {
}

if (inputValue === undefined && definition.inputNullable.has(id)) {
return new StructType('null');
return nullType;
}

return undefined;
Expand Down
85 changes: 42 additions & 43 deletions src/common/types/chainner-builtin.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {
Arg,
Int,
IntIntervalType,
Intrinsic,
NeverType,
NumberPrimitive,
StringLiteralType,
StringPrimitive,
StringType,
StructInstanceType,
StructType,
StructTypeField,
createInstance,
getStructDescriptor,
handleNumberLiterals,
intersect,
literal,
wrapQuaternary,
wrapScopedUnary,
wrapTernary,
wrapUnary,
} from '@chainner/navi';
import path from 'path';
import { ColorJson } from '../common-types';
Expand Down Expand Up @@ -386,48 +387,46 @@ export const padCenter = wrapTernary<StringPrimitive, Int, StringPrimitive, Stri
}
);

export const splitFilePath = wrapUnary<StringPrimitive, StructType>((filePath: StringPrimitive) => {
if (filePath.type === 'literal') {
const base = path.basename(filePath.value);
const ext = path.extname(base);
const basename = ext ? base.slice(0, -ext.length) : base;
return new StructType('SplitFilePath', [
new StructTypeField(
'dir',
new StructType('Directory', [
new StructTypeField('path', literal(path.dirname(filePath.value))),
])
),
new StructTypeField('basename', literal(basename)),
new StructTypeField('ext', literal(ext)),
]);
export const splitFilePath = wrapScopedUnary(
(scope, filePath: StringPrimitive): StructInstanceType => {
const splitFilePathDesc = getStructDescriptor(scope, 'SplitFilePath');
const directoryDesc = getStructDescriptor(scope, 'Directory');

if (filePath.type === 'literal') {
const base = path.basename(filePath.value);
const ext = path.extname(base);
const basename = ext ? base.slice(0, -ext.length) : base;

return createInstance(splitFilePathDesc, {
dir: createInstance(directoryDesc, {
path: literal(path.dirname(filePath.value)),
}),
basename: literal(basename),
ext: literal(ext),
});
}
return createInstance(splitFilePathDesc);
}
return new StructType('SplitFilePath', [
new StructTypeField(
'dir',
new StructType('Directory', [new StructTypeField('path', StringType.instance)])
),
new StructTypeField('basename', StringType.instance),
new StructTypeField('ext', StringType.instance),
]);
});
);

export const parseColorJson = wrapUnary<StringPrimitive, StructType>((json) => {
if (json.type === 'literal') {
try {
const value = JSON.parse(json.value) as unknown;
if (value && typeof value === 'object' && 'kind' in value && 'values' in value) {
const color = value as ColorJson;
return new StructType('Color', [
new StructTypeField('channels', literal(color.values.length)),
]);
export const parseColorJson = wrapScopedUnary(
(scope, json: StringPrimitive): Arg<StructInstanceType> => {
const colorDesc = getStructDescriptor(scope, 'Color');

if (json.type === 'literal') {
try {
const value = JSON.parse(json.value) as unknown;
if (value && typeof value === 'object' && 'kind' in value && 'values' in value) {
const color = value as ColorJson;
return createInstance(colorDesc, {
channels: literal(color.values.length),
});
}
} catch {
// noop
}
} catch {
// noop
return NeverType.instance;
}
return NeverType.instance;
return createInstance(colorDesc);
}
return new StructType('Color', [
new StructTypeField('channels', new IntIntervalType(1, Infinity)),
]);
});
);
15 changes: 8 additions & 7 deletions src/common/types/chainner-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SourceDocument,
Type,
globalScope,
makeScoped,
parseDefinitions,
} from '@chainner/navi';
import { lazy } from '../util';
Expand Down Expand Up @@ -151,12 +152,12 @@ intrinsic def parseColorJson(json: string): Color;
export const getChainnerScope = lazy((): Scope => {
const builder = new ScopeBuilder('Chainner scope', globalScope);

const intrinsic: Record<string, (...args: NeverType[]) => Type> = {
formatPattern: formatTextPattern,
regexReplace,
padStart,
padEnd,
padCenter,
const intrinsic: Record<string, (scope: Scope, ...args: NeverType[]) => Type> = {
formatPattern: makeScoped(formatTextPattern),
regexReplace: makeScoped(regexReplace),
padStart: makeScoped(padStart),
padEnd: makeScoped(padEnd),
padCenter: makeScoped(padCenter),
splitFilePath,
parseColorJson,
};
Expand All @@ -167,7 +168,7 @@ export const getChainnerScope = lazy((): Scope => {
if (!(d.name in intrinsic)) {
throw new Error(`Unable to find definition for intrinsic ${d.name}`);
}
const fn = intrinsic[d.name] as (...args: Type[]) => Type;
const fn = intrinsic[d.name] as (scope: Scope, ...args: Type[]) => Type;
builder.add(IntrinsicFunctionDefinition.from(d, fn));
} else {
builder.add(d);
Expand Down
19 changes: 8 additions & 11 deletions src/common/types/explain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import {
NumberPrimitive,
NumericLiteralType,
StringPrimitive,
StructType,
StructValueType,
Type,
UnionType,
ValueType,
isStructInstance,
} from '@chainner/navi';
import { joinEnglish } from '../util';
import { IntNumberType, isColor, isDirectory, isImage } from './util';
import { IntNumberType, getFields, isColor, isDirectory, isImage } from './util';

const isInt = (n: Type, min = -Infinity, max = Infinity): n is IntIntervalType => {
return n.underlying === 'number' && n.type === 'int-interval' && n.min === min && n.max === max;
Expand Down Expand Up @@ -77,36 +78,32 @@ const explainString = (s: StringPrimitive): string | undefined => {
if (s.excluded.size === 1 && s.excluded.has('')) return 'a non-empty string';
};

const explainStruct = (s: StructType, options: ExplainOptions): string | undefined => {
const explainStruct = (s: StructValueType, options: ExplainOptions): string | undefined => {
const detailed = (base: string | undefined, detail: string): string | undefined => {
if (options.detailed && base) return `${base} ${detail}`;
return base;
};

if (isImage(s)) {
const width = s.fields[0].type;
const height = s.fields[1].type;
const channels = s.fields[2].type;

const { width, height, channels } = getFields(s);
if (isInt(width, 1) && isInt(height, 1)) {
if (isInt(channels, 1)) return detailed('an image', 'of any size and any colorspace');
return detailed(formatChannelNumber(channels, 'image'), 'of any size');
}
}

if (isColor(s)) {
const channels = s.fields[0].type;

const { channels } = getFields(s);
if (isInt(channels, 1)) return detailed('a color', 'of any colorspace');
return formatChannelNumber(channels, 'color');
}

if (isDirectory(s)) {
const path = s.fields[0].type;
const { path } = getFields(s);
if (path.type === 'string') return 'a directory path';
}

if (s.name === 'Seed') {
if (isStructInstance(s) && s.descriptor.name === 'Seed') {
return 'a seed (for randomness)';
}
};
Expand Down
67 changes: 2 additions & 65 deletions src/common/types/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Bounds,
Expression,
FieldAccessExpression,
FunctionCallExpression,
Expand Down Expand Up @@ -94,77 +95,13 @@ export interface MatchExpressionJson {
arms: MatchArmJson[];
}

const toNumberJson = (number: number): NumberJson => {
if (Number.isNaN(number)) return 'NaN';
if (number === Infinity) return 'inf';
if (number === -Infinity) return '-inf';
return number;
};
const fromNumberJson = (number: NumberJson): number => {
if (number === 'NaN') return NaN;
if (number === 'inf') return Infinity;
if (number === '-inf') return -Infinity;
return number;
};

export const toJson = (e: Expression): ExpressionJson => {
switch (e.type) {
case 'any':
return 'any';
case 'never':
return 'never';
case 'number':
return 'number';
case 'string':
return 'string';
case 'interval':
return { type: 'interval', min: toNumberJson(e.min), max: toNumberJson(e.max) };
case 'int-interval':
return { type: 'int-interval', min: toNumberJson(e.min), max: toNumberJson(e.max) };
case 'literal':
if (e.underlying === 'number') {
return { type: 'numeric-literal', value: toNumberJson(e.value) };
}
return { type: 'string-literal', value: e.value };
case 'union':
return { type: 'union', items: e.items.map(toJson) };
case 'intersection':
return { type: 'intersection', items: e.items.map(toJson) };
case 'struct':
return {
type: 'named',
name: e.name,
fields: Object.fromEntries(e.fields.map((f) => [e.name, toJson(f.type)])),
};
case 'named':
return {
type: 'named',
name: e.name,
};
case 'field-access':
return { type: 'field-access', of: toJson(e.of), field: e.field };
case 'function-call':
return { type: 'function-call', name: e.functionName, args: e.args.map(toJson) };
case 'match': {
return {
type: 'match',
of: toJson(e.of),
arms: e.arms.map((a) => ({
pattern: toJson(a.pattern),
binding: a.binding,
to: toJson(a.to),
})),
};
}
case 'scope':
throw new Error('Converting scoped expressions to JSON is currently not supported.');
case 'inverted-set':
throw new Error('Converting scoped expressions to JSON is currently not supported.');
default:
return assertNever(e);
}
};

export const fromJson = (e: ExpressionJson): Expression => {
if (typeof e === 'boolean') {
return new NamedExpression(e ? 'true' : 'false');
Expand All @@ -186,7 +123,7 @@ export const fromJson = (e: ExpressionJson): Expression => {
case 'string-literal':
return new StringLiteralType(e.value);
case 'interval':
return new IntervalType(fromNumberJson(e.min), fromNumberJson(e.max));
return new IntervalType(fromNumberJson(e.min), fromNumberJson(e.max), Bounds.Inclusive);
case 'int-interval':
return new IntIntervalType(fromNumberJson(e.min), fromNumberJson(e.max));
case 'union':
Expand Down
Loading

0 comments on commit 9f718e5

Please sign in to comment.