Skip to content

Commit

Permalink
fixed had composition check
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Mar 21, 2024
1 parent bfa06d0 commit 0358d0c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
4 changes: 1 addition & 3 deletions source/compiler/codegen/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ function CompileDeclare(ctx: Context, syntax: Syntax.Term_Declare) {


Assign(ctx, variable.type, resolveType, syntax.ref);
variable.markDefined();
}

function CompileAssign(ctx: Context, syntax: Syntax.Term_Assign) {
Expand Down Expand Up @@ -257,8 +256,7 @@ export function Assign(ctx: Context, target: LinearType, expr: OperandType, ref:
ctx.block.push(Instruction.copy(0, 0));

// Duplicate the struct's linear state over
target.infuse(target);
target.markDefined();
target.infuse(expr);

// Clean up the expr generated struct
expr.dispose();
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/postfix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function CompileCall(ctx: Context, syntax: Syntax.Term_Expr_call, operand: Opera

// Special post-processing for linear types
if (!(res instanceof LinearType)) continue;
ResolveLinearType(ctx, res, args[i].ref);
ResolveLinearType(ctx, res, args[i].ref, true);
}


Expand Down
25 changes: 16 additions & 9 deletions source/compiler/codegen/expression/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,18 @@ export class LinearType {
// was already composed, so cascading will make no change
if (this.composable) return;

// check all children are composable
this.composable = true;
for (const [_, child] of this.attributes) {
if (!child.composable) {
this.composable = false;
return; // stop cascade
if (this.type instanceof Structure) {

// Not all attributes are present
if (this.attributes.size < this.type.attributes.length) return;

// check all children are composable
this.composable = true;
for (const [_, child] of this.attributes) {
if (!child.composable) {
this.composable = false;
return; // stop cascade
}
}
}

Expand All @@ -144,7 +150,6 @@ export class LinearType {

private cascadeDecompose() {
if (this.composable) this.parent?.cascadeDecompose();
this.composable = false;
}

getCompositionErrors(reasons: ReferenceRange[] = []) {
Expand All @@ -169,14 +174,16 @@ export class LinearType {
}

markDefined() {
this.attributes.clear();
this.consumedAt = undefined;
this.composable = true;
this.attributes.clear();

this.parent?.cascadeCompose();
}

markConsumed(ref: ReferenceRange) {
this.composable = false;
this.consumedAt = ref;
this.composable = false;
this.attributes.clear();

this.parent?.cascadeDecompose();
Expand Down
44 changes: 31 additions & 13 deletions source/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";

import { ParseError, ReferenceRange, Reference } from "~/bnf/shared.js";
import * as Instance from "~/bnf/syntax.js";
import * as Syntax from "~/bnf/syntax.d.ts";
import { ParseError, ReferenceRange, Reference, SyntaxNode } from "~/bnf/shared.js";
import { Panic } from "~/helper.ts";

await Instance.ready;
Expand All @@ -12,22 +12,40 @@ export function Parse(data: string, path: string, name: string): Syntax.Term_Pro

if (res instanceof ParseError) Panic(`${colors.red("FATAL ERROR")}: Syntax Parser Completely crashed`);

if (res.isPartial) {
console.error(res);
Panic(
colors.red("Syntax Error") + "\n",
{
path, name,
ref: res.reach
? new ReferenceRange(res.root.ref.end, res.reach)
: ReferenceRange.blank()
}
)
};
if (res.isPartial) Panic(
colors.red("Syntax Error") + "\n",
{
path, name,
ref: res.reach
? new ReferenceRange(res.root.ref.end, res.reach)
: ReferenceRange.blank()
}
);

// Fix changes which are too long due to `omit`ed syntax
RemapRefRange(res.root);

return res.root as Syntax.Term_Program;
}

function RemapRefRange(syntax: SyntaxNode) {
if (!Array.isArray(syntax.value)) return;

const lastI = syntax.value.length - 1;
if (lastI < 0) return;

for (const child of syntax.value) {
RemapRefRange(child);
}

syntax.ref.end = syntax.value[lastI].ref.end;

return;
}




export function SourceView(path: string, name: string, range: ReferenceRange, compact?: boolean) {
const source = ReadByteRange(path, range.start.index-200, range.end.index+200);
if (source === null) return `${name}: ${range.toString()}\n`;
Expand Down

0 comments on commit 0358d0c

Please sign in to comment.