Skip to content

Commit

Permalink
Refactor eeeeeveryyyyything into directories that make sense
Browse files Browse the repository at this point in the history
  • Loading branch information
robsimmons committed Nov 17, 2023
1 parent 4752989 commit 60b0a4c
Show file tree
Hide file tree
Showing 41 changed files with 242 additions and 351 deletions.
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
href="https://fonts.googleapis.com/css2?family=Fira+Mono:wght@500&family=Fira+Sans+Condensed&display=swap"
rel="stylesheet"
/>
<link href="/src/reset.css" rel="stylesheet" />
<link href="/src/dusa.css" rel="stylesheet" />
<link href="/src/codemirror.css" rel="stylesheet" />
<link href="/src/web/reset.css" rel="stylesheet" />
<link href="/src/web/dusa.css" rel="stylesheet" />
<link href="/src/web/codemirror.css" rel="stylesheet" />
<link rel="icon" href="/dusa-icon.svg" />
<link rel="mask-icon" href="/dusa-icon.svg" color="#007FBC" />
</head>
Expand All @@ -33,6 +33,6 @@
</div>
</div>
</main>
<script type="module" src="/src/main.tsx"></script>
<script type="module" src="/src/web/main.tsx"></script>
</body>
</html>
20 changes: 0 additions & 20 deletions src/constants.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/datalog/compile.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { binarize, binarizedProgramToString } from './binarize';
import { execute, solutionsToStrings } from './choiceengine';
import { indexToRuleName } from './compile';
import { parse } from './dusa-parser';
import { compile } from '../langauge/compile';
import { parse } from '../langauge/dusa-parser';
import { makeInitialDb } from './forwardengine';
import { indexedProgramToString, indexize } from './indexize';
import { Declaration, check, declToString } from './syntax';
import { check } from '../langauge/syntax';
import { test, expect } from 'vitest';

function testExecution(program: string, debug = false) {
const parsed = parse(program);
function testExecution(source: string, debug = false) {
const parsed = parse(source);
if (parsed.errors !== null) {
throw parsed.errors;
}
Expand All @@ -18,25 +16,8 @@ function testExecution(program: string, debug = false) {
throw checked.errors;
}

const named = checked.decls.map<[string, Declaration]>((decl, i) => [indexToRuleName(i), decl]);
if (debug) {
console.log(`Form 1: checked program with named declarations
${named.map(([name, decl]) => `${name}: ${declToString(decl)}`).join('\n')}`);
}

const binarized = binarize(named);
if (debug) {
console.log(`\nForm 2: Binarized program
${binarizedProgramToString(binarized)}`);
}

const indexed = indexize(binarized);
if (debug) {
console.log(`\nForm 3: Index-aware program
${indexedProgramToString(indexed)}`);
}

return execute(indexed, makeInitialDb(indexed), debug);
const program = compile(checked.decls, debug);
return execute(program, makeInitialDb(program), debug);
}

test('Multi-step declaration, basic nat (in)equality', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/datalog/choiceengine.ts → src/engine/choiceengine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Data, TRIV_DATA, dataToString, hide } from './data';
import { DataMap } from './datamap';
import { Data, TRIV_DATA, dataToString, hide } from '../datastructures/data';
import { DataMap } from '../datastructures/datamap';
import { Database, dbToString, insertFact, listFacts, stepDb } from './forwardengine';
import { IndexedProgram } from './indexize';
import { equal } from './terms';
import { IndexedProgram } from '../langauge/indexize';
import { equal } from './dataterm';

export interface ChoiceTreeLeaf {
type: 'leaf';
Expand Down
230 changes: 2 additions & 228 deletions src/datalog/terms.ts → src/engine/dataterm.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
import { SourceLocation } from './parsing/source-location';
import { SPECIAL_DEFAULTS } from './dusa-builtins';
import { Data, expose, hide } from './data';

export type Pattern =
| { type: 'triv' }
| { type: 'int'; value: number }
| { type: 'nat'; value: number }
| { type: 'string'; value: string }
| { type: 'const'; name: string; args: Pattern[] }
| {
type: 'special';
name: keyof typeof SPECIAL_DEFAULTS;
symbol: string;
args: Pattern[];
nonground?: number;
}
| { type: 'wildcard'; name: null | string }
| { type: 'var'; name: string };

export type ParsedPattern =
| { type: 'triv'; loc: SourceLocation }
| { type: 'int'; value: number; loc: SourceLocation }
| { type: 'nat'; value: number; loc: SourceLocation }
| { type: 'string'; value: string; loc: SourceLocation }
| { type: 'const'; name: string; args: ParsedPattern[]; loc: SourceLocation }
| {
type: 'special';
name: keyof typeof SPECIAL_DEFAULTS;
symbol: string;
args: ParsedPattern[];
loc: SourceLocation;
}
| { type: 'wildcard'; name: null | string; loc: SourceLocation }
| { type: 'var'; name: string; loc: SourceLocation };
import { Data, expose, hide } from "../datastructures/data";
import { Pattern } from "../langauge/terms";

export type Substitution = { [varName: string]: Data };

Expand Down Expand Up @@ -281,196 +248,3 @@ export function apply(substitution: Substitution, pattern: Pattern): Data {
export function equal(t: Data, s: Data): boolean {
return t === s;
}

export function termToString(t: Pattern, needsParens = true): string {
switch (t.type) {
case 'triv':
return `()`;
case 'wildcard':
return `_`;
case 'int':
case 'nat':
return `${t.value}`;
case 'string':
return `"${t.value}"`;
case 'const':
case 'special': {
const name = t.type === 'const' ? t.name : `${t.symbol}<${t.name}>`;
return t.args.length === 0
? name
: needsParens
? `(${name} ${t.args.map((arg) => termToString(arg)).join(' ')})`
: `${name} ${t.args.map((arg) => termToString(arg)).join(' ')}`;
}
case 'var':
return t.name;
}
}

export function assertData(p: Pattern): Data {
return apply({}, p);
}

export function parseTerm(s: string): { data: Pattern; rest: string } | null {
if (s[0] === '"') {
const end = s.slice(1).indexOf('"');
if (end === -1) {
throw new Error('Unmatched quote');
}
return {
data: { type: 'string', value: s.slice(1, end + 1) },
rest: s.slice(end + 2).trimStart(),
};
}

if (s[0] === '(') {
if (s[1] === ')') {
return { data: { type: 'triv' }, rest: s.slice(2).trimStart() };
}
const next = parseTerm(s.slice(1));
if (next === null) {
throw new Error('No term following an open parenthesis');
}
if (next.rest[0] !== ')') {
throw new Error('Did not find expected matching parenthesis');
}
return { data: next.data, rest: next.rest.slice(1).trimStart() };
}

const constMatch = s.match(/^-?[0-9a-zA-Z]+/);
if (constMatch) {
if (constMatch[0].match(/^[A-Z]/)) {
return {
data: { type: 'var', name: constMatch[0] },
rest: s.slice(constMatch[0].length).trimStart(),
};
}
if (constMatch[0].match(/^-?[0-9]/)) {
if (`${parseInt(constMatch[0])}` !== constMatch[0]) {
throw new Error(`Bad number: '${constMatch[0]}'`);
}
const value = parseInt(constMatch[0]);
return {
data: { type: value < 0 ? 'int' : 'nat', value },
rest: s.slice(constMatch[0].length).trimStart(),
};
}
let rest = s.slice(constMatch[0].length).trimStart();
const args = [];
let next = parseTerm(rest);
while (next !== null) {
args.push(next.data);
rest = next.rest;
next = parseTerm(next.rest);
}
return { data: { type: 'const', name: constMatch[0], args }, rest };
}

return null;
}

export function parsePattern(s: string): Pattern {
const result = parseTerm(s);
if (result === null) {
throw new Error(`Could not parse '${s}' as a pattern`);
}
if (result.rest !== '') {
throw new Error(`Unexpected parsing '${s}' as a pattern: '${result.rest[0]}'`);
}
return result.data;
}

export function parseData(s: string): Data {
return assertData(parsePattern(s));
}

function repeatedWildcardsAccum(
wildcards: Set<string>,
repeatedWildcards: Map<string, SourceLocation>,
p: ParsedPattern,
) {
switch (p.type) {
case 'wildcard':
if (p.name !== null && wildcards.has(p.name)) {
repeatedWildcards.set(p.name, p.loc);
}
wildcards.add(p.name ?? '_');
return;
case 'int':
case 'string':
case 'triv':
case 'var':
return;
case 'const':
case 'special':
for (const arg of p.args) {
repeatedWildcardsAccum(wildcards, repeatedWildcards, arg);
}
return;
}
}

export function repeatedWildcards(
knownWildcards: Set<string>,
...patterns: ParsedPattern[]
): [string, SourceLocation][] {
const repeatedWildcards = new Map<string, SourceLocation>();
for (const pattern of patterns) {
repeatedWildcardsAccum(knownWildcards, repeatedWildcards, pattern);
}
return [...repeatedWildcards.entries()];
}

function freeVarsAccum(s: Set<string>, p: Pattern) {
switch (p.type) {
case 'var':
s.add(p.name);
return;
case 'int':
case 'string':
case 'triv':
case 'wildcard':
return;
case 'const':
case 'special':
for (const arg of p.args) {
freeVarsAccum(s, arg);
}
return;
}
}

export function freeVars(...patterns: Pattern[]): Set<string> {
const s = new Set<string>();
for (const pattern of patterns) {
freeVarsAccum(s, pattern);
}
return s;
}

function freeParsedVarsAccum(s: Map<string, SourceLocation>, p: ParsedPattern) {
switch (p.type) {
case 'var':
s.set(p.name, p.loc);
return;
case 'int':
case 'string':
case 'triv':
case 'wildcard':
return;
case 'const':
case 'special':
for (const arg of p.args) {
freeParsedVarsAccum(s, arg);
}
return;
}
}

export function freeParsedVars(...patterns: ParsedPattern[]): Map<string, SourceLocation> {
const s = new Map<string, SourceLocation>();
for (const pattern of patterns) {
freeParsedVarsAccum(s, pattern);
}
return s;
}
4 changes: 2 additions & 2 deletions src/datalog/terms.test.ts → src/engine/dataterms.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hide } from './data';
import { Substitution, match } from './terms';
import { hide } from '../datastructures/data';
import { Substitution, match } from './dataterm';
import { test, expect } from 'vitest';

test('Match patterns with data', () => {
Expand Down
10 changes: 5 additions & 5 deletions src/datalog/forwardengine.ts → src/engine/forwardengine.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AttributeMap } from './attributemap';
import PQ from './binqueue';
import { Data, dataToString } from './data';
import { AttributeMap } from '../datastructures/attributemap';
import PQ from '../datastructures/binqueue';
import { Data, dataToString } from '../datastructures/data';
import {
IndexInsertionRule,
IndexedBinaryRule,
IndexedConclusion,
IndexedProgram,
} from './indexize';
import { Substitution, apply, equal, match } from './terms';
} from '../langauge/indexize';
import { Substitution, apply, equal, match } from './dataterm';

type Prefix = { type: 'prefix'; name: string; shared: Data[]; passed: Data[] };
type NewFact = { type: 'fact'; name: string; args: Data[]; value: Data };
Expand Down
File renamed without changes.
Loading

0 comments on commit 60b0a4c

Please sign in to comment.