Skip to content

Commit

Permalink
+data syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Oct 12, 2024
1 parent ab5af22 commit 38dd424
Show file tree
Hide file tree
Showing 26 changed files with 193 additions and 28 deletions.
5 changes: 2 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"build:all": "deno task build:syntax & deno task build:compiler",
"build:syntax": "deno run -A npm:bnf-parser ./source/bnf/",
"build:compiler": "deno compile --output salient.exe -A ./source/cli.ts",
"test": "deno test && deno task test:salient",
"test:all": "deno test && deno task test:salient",
"test:deno": "deno test",
"test:salient": "deno run -A ./source/cli.ts test ./tests",
"cli": "deno run -A ./source/cli.ts",
Expand All @@ -18,8 +18,7 @@
"imports": {
"~/": "./source/",
"bnf-parser": "npm:bnf-parser@^4.1.3",
"npm-run-all": "npm:npm-run-all@^4.1.5",
"chalk": "npm:chalk@^5.3.0"
"fmt/colors": "https://deno.land/[email protected]/fmt/colors.ts"
},
"lint": {
"include": ["source/"],
Expand Down
2 changes: 1 addition & 1 deletion source/bnf/syntax.bnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
program ::= %w* ( stmt_top %w* )* ;
stmt_top ::= function | structure | external | test ;
stmt_top ::= function | structure | data | external | test ;


#=============================
Expand Down
2 changes: 1 addition & 1 deletion source/bnf/syntax.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type Term_Stmt_top = {
count: number,
ref: _Shared.ReferenceRange,
value: [
(Term_Function | Term_Structure | Term_External | Term_Test)
(Term_Function | Term_Structure | Term_Data | Term_External | Term_Test)
]
}
export declare function Parse_Stmt_top (i: string, refMapping?: boolean): _Shared.ParseError | {
Expand Down
2 changes: 1 addition & 1 deletion source/bnf/syntax.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion source/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import { Compile } from "~/compile.ts";
import { Panic } from "~/compiler/helper.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve, join, relative, dirname } from "https://deno.land/[email protected]/path/mod.ts";
import { existsSync } from "https://deno.land/[email protected]/fs/mod.ts";
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import Function from "~/compiler/function.ts";
import Package from "~/compiler/package.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import type { Scope } from "~/compiler/codegen/scope.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import { IntrinsicType, bool, u8, i8, u16, i16, u32, i32, u64, i64, f32, f64 } from "~/compiler/intrinsic.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import Structure from "~/compiler/structure.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/flow-control.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import { LinearType, SolidType, OperandType } from "~/compiler/codegen/expression/type.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as Syntax from "~/bnf/syntax.d.ts";
import { red } from "https://deno.land/[email protected]/fmt/colors.ts";
import { red } from "fmt/colors";

import * as WasmTypes from "~/wasm/type.ts";
import { AssertUnreachable, LatentOffset } from "~/helper.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/infix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import * as WasmTypes from "~/wasm/type.ts";
import Structure from "~/compiler/structure.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/operand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import Structure from "~/compiler/structure.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/postfix/call.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import Function from "~/compiler/function.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/postfix/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import { AssertUnreachable } from "~/helper.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/codegen/expression/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type * as Syntax from "~/bnf/syntax.d.ts";
import { IntrinsicValue, f32, f64, i16, i32, i64, i8, u16, u32, u64, u8 } from "~/compiler/intrinsic.ts";
Expand Down
152 changes: 152 additions & 0 deletions source/compiler/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import * as colors from "fmt/colors";

import type { Term_Data } from "~/bnf/syntax.d.ts";
import type { File, Namespace } from "~/compiler/file.ts";
import Structure from "~/compiler/structure.ts";
import { IsSolidType, SolidType } from "~/compiler/codegen/expression/type.ts";
import { AssertUnreachable } from "~/helper.ts";
import { ReferenceRange } from "~/bnf/shared.d.ts";
import { SourceView } from "~/parser.ts";
import { Panic } from "~/compiler/helper.ts";

export default class Data {
owner: File;
name: string;
ast: Term_Data;
ref: ReferenceRange;

attributes: Array<{
offset: number,
name: string,
type: SolidType,
}>;
linked: boolean;

constructor(owner: File, ast: Term_Data) {
this.owner = owner;
this.name = ast.value[0].value;
this.ast = ast;
this.ref = ast.ref;

this.attributes = [];
this.linked = false;
}

link(chain: Data[] = []) {
if (this.linked) return;
if (chain.includes(this)) Panic(
`${colors.red("Error")}: This structure is attempting to compose itself\n`
+ chain.map(x => `${x.name}${x.ref.start.toString()}`).join(' -> ')
+ ` -> ${this.name}${this.ref.start.toString()}`
);
chain.push(this);

const attrs = Array<{
name: string,
type: SolidType
ref: ReferenceRange
}>();

for (const stmt of this.ast.value[1].value) {
const line = stmt.value[0];
switch (line.type) {
case "struct_attr": {
const name = line.value[0].value;
const existing = attrs.find(x => x.name === name);
if (existing) {
console.error(
`${colors.red("Error")}: Duplicate attribute name found in struct\n`
+ SourceView(this.owner.path, this.owner.name, existing.ref)
+ SourceView(this.owner.path, this.owner.name, line.value[0].ref)
);
this.owner.markFailure();
continue;
}

const scope = this.owner;
const type = scope.get(line.value[1]);
if (!type) {
console.error(
`${colors.red("Error")}: Unable to resolve type\n`
+ SourceView(this.owner.path, this.owner.name, line.value[1].ref)
);
this.owner.markFailure();
continue;
}

if (!IsSolidType(type)) {
console.error(
`${colors.red("Error")}: Expected a solid type\n`
+ SourceView(this.owner.path, this.owner.name, line.value[1].ref)
);
console.error(type);
this.owner.markFailure();
continue;
}

if (type instanceof Structure) {
console.error(
`${colors.red("Error")}: Cannot have a struct as element of data\n`
+ SourceView(this.owner.path, this.owner.name, line.value[1].ref)
);
console.error(type);
this.owner.markFailure();
continue;
}
if (type instanceof Data) type.link(chain);

attrs.push({ name, type, ref: line.ref });
} break;
case "struct_spread": {
const other = this.owner.get(line.value[0]);
if (other === null) {
console.error(
`${colors.red("Error")}: Unable to resolve type\n`
+ SourceView(this.owner.path, this.owner.name, line.value[0].ref)
);
this.owner.markFailure();
continue;
}

if (other instanceof Structure) other.link();
else if (other instanceof Data) other.link(chain);
else {
console.error(
`${colors.red("Error")}: Resolved type must be another structure\n`
+ SourceView(this.owner.path, this.owner.name, line.value[0].ref)
);
this.owner.markFailure();
continue;
}
} break;
default: AssertUnreachable(line);
}
}

this.linked = true;
}

get(name: string) {
this.link(); // ensure struct is linked
const out = this.attributes.find(x => x.name == name);
return out;
}

getTypeName() {
return "type " + this.name;
}

declarationView(): string {
return SourceView(this.owner.path, this.owner.name, this.ast.ref);
}

merge(other: Namespace) {
console.error(
`${colors.red("Error")}: Cannot share a name space between these two\n`
+ this.declarationView()
+ other.declarationView()
);

this.owner.markFailure();
}
}
18 changes: 16 additions & 2 deletions source/compiler/file.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type Package from "./package.ts";
import type { Term_Access, Term_External, Term_Function, Term_Program, Term_Structure, Term_Test } from "~/bnf/syntax.d.ts";
import type { Term_Access, Term_Data, Term_External, Term_Function, Term_Program, Term_Structure, Term_Test } from "~/bnf/syntax.d.ts";

import Structure from "~/compiler/structure.ts";
import Function from "~/compiler/function.ts";
import TestCase from "~/compiler/test-case.ts";
import Global from "~/compiler/global.ts";
import Import from "~/compiler/import.ts";
import Data from "~/compiler/data.ts";
import { IntrinsicType, bool, u8, i8, u16, i16, i32, i64, u32, u64, f32, f64, none, never } from "~/compiler/intrinsic.ts";
import { FlatAccess, FlattenAccess } from "~/compiler/helper.ts";
import { AssertUnreachable } from "~/helper.ts";
import { SimplifyString } from "~/compiler/codegen/expression/constant.ts";
import { VirtualType } from "~/compiler/intrinsic.ts";
import { Parse } from "~/parser.ts";
import TestCase from "~/compiler/test-case.ts";

export type Namespace = Function | Import | Global | Structure | IntrinsicType | VirtualType ;

Expand Down Expand Up @@ -99,6 +100,7 @@ function Ingest(file: File, syntax: Term_Program) {
case "structure": IngestStructure(file, inner); break;
case "external": IngestExternal(file, inner); break;
case "test": IngestTest(file, inner); break;
case "data": IngestData(file, inner); break;
default: AssertUnreachable(inner);
}
}
Expand Down Expand Up @@ -128,6 +130,18 @@ function IngestStructure(file: File, syntax: Term_Structure) {
throw new Error(`Structures cannot share a namespace`);
}

function IngestData(file: File, syntax: Term_Data) {
const struct = new Data(file, syntax);

const existing = file.namespace[struct.name];
if (!existing) {
file.namespace[struct.name] = struct;
return;
}

throw new Error(`Structures cannot share a namespace`);
}



function IngestExternal(file: File, syntax: Term_External) {
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type { Term_Access, Term_Function } from "~/bnf/syntax.d.ts";
import type { File, Namespace } from "./file.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type { File, Namespace } from "./file.ts";
import { ReferenceRange, SourceView } from "~/parser.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type { File, Namespace } from "./file.ts";
import { ReferenceRange, SourceView } from "~/parser.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/structure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type { Term_Structure } from "~/bnf/syntax.d.ts";
import type { File, Namespace } from "~/compiler/file.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/test-case.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import type { Term_Block } from "~/bnf/syntax.d.ts";
import type { File } from "./file.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stripAnsiCode } from "https://deno.land/[email protected]/fmt/colors.ts";
import { stripAnsiCode } from "fmt/colors";

export type Byte = number;
export function isByte(value: number): value is Byte {
Expand Down
2 changes: 1 addition & 1 deletion source/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import * as Instance from "~/bnf/syntax.js";
import * as Syntax from "~/bnf/syntax.d.ts";
Expand Down
2 changes: 1 addition & 1 deletion source/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as duration from "https://deno.land/[email protected]/fmt/duration.ts";
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";
import * as colors from "fmt/colors";

import TestCase from "~/compiler/test-case.ts";
import Package from "~/compiler/package.ts";
Expand Down

0 comments on commit 38dd424

Please sign in to comment.