Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { parse } from "./syntax/parse";
import fs from "node:fs";
import process from "node:process";
import { analyze, SemanticContext } from "./semantics/analyze";

const sourcePath = process.cwd() + "/debug/main.snow";
const source = fs.readFileSync(sourcePath, { encoding: "utf8" });

const tree = parse(source);

console.log(JSON.stringify(tree, null, " "));

const semCtx = new SemanticContext();
analyze(semCtx, tree);

console.log(semCtx.nameToSymbol);
console.log(semCtx.nodeToSymbol);
62 changes: 62 additions & 0 deletions src/semantics/analyze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { SyntaxNode, Unit } from "../syntax/syntax-node";

type SemanticSymbol = TypeSymbol;

export class TypeSymbol {
kind = "TypeSymbol" as const;
constructor(
public baseType: TypeSymbol | 'not-resolved' | 'none',
) {}
}

export class SemanticContext {
nodes: Map<string, SyntaxNode> = new Map();
symbols: Map<SyntaxNode, TypeSymbol> = new Map();
builtinTypes: Map<string, TypeSymbol> = new Map();

constructor() {
this.builtinTypes.set('string', new TypeSymbol('none'));
this.builtinTypes.set('number', new TypeSymbol('none'));
this.builtinTypes.set('boolean', new TypeSymbol('none'));
this.builtinTypes.set('object', new TypeSymbol('none'));
}
}

export function analyze(ctx: SemanticContext, unit: Unit) {
collectNames(ctx, unit);
bindNames(ctx, unit);
}

function collectNames(ctx: SemanticContext, node: SyntaxNode) {
switch (node.kind) {
case "Unit": {
for (const decl of node.decls) {
collectNames(ctx, decl);
}
break;
}
case "TypeDecl": {
const symbol = new TypeSymbol('not-resolved');
ctx.nodes.set(node.name, node);
ctx.symbols.set(node, symbol);
break;
}
}
}

function bindNames(ctx: SemanticContext, node: SyntaxNode) {
switch (node.kind) {
case "Unit": {
for (const decl of node.decls) {
bindNames(ctx, decl);
}
break;
}
case "TypeDecl": {
break;
}
case "TypeNode": {
break;
}
}
}