From 01cb243b5146c28118546f40b312893dd15ceba7 Mon Sep 17 00:00:00 2001 From: marihachi Date: Fri, 19 Jul 2024 22:40:21 +0000 Subject: [PATCH 1/3] wip --- src/main.ts | 7 +++++ src/semantics/analyze.ts | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/semantics/analyze.ts diff --git a/src/main.ts b/src/main.ts index d07d251..9b77fe0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ 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" }); @@ -8,3 +9,9 @@ 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); diff --git a/src/semantics/analyze.ts b/src/semantics/analyze.ts new file mode 100644 index 0000000..ebf98e8 --- /dev/null +++ b/src/semantics/analyze.ts @@ -0,0 +1,57 @@ +import { SyntaxNode, Unit } from "../syntax/syntax-node"; + +type SemanticNode = TypeSymbol; + +export class TypeSymbol { + kind = "TypeSymbol" as const; + constructor( + /** Primitive type or Type symbol */ + public baseType: string | TypeSymbol | undefined, + ) {} +} + +export class SemanticContext { + nameToSymbol: Map = new Map(); + nodeToSymbol: Map = new Map(); + + constructor() {} +} + +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(undefined); + ctx.nameToSymbol.set(node.name, symbol); + ctx.nodeToSymbol.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; + } + } +} From de23150300c335b5073886699dead4a4d75fc70e Mon Sep 17 00:00:00 2001 From: marihachi Date: Fri, 19 Jul 2024 22:55:29 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E3=81=B5=E3=82=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/semantics/analyze.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/semantics/analyze.ts b/src/semantics/analyze.ts index ebf98e8..ad08085 100644 --- a/src/semantics/analyze.ts +++ b/src/semantics/analyze.ts @@ -1,6 +1,6 @@ import { SyntaxNode, Unit } from "../syntax/syntax-node"; -type SemanticNode = TypeSymbol; +type SemanticSymbol = TypeSymbol; export class TypeSymbol { kind = "TypeSymbol" as const; @@ -11,8 +11,8 @@ export class TypeSymbol { } export class SemanticContext { - nameToSymbol: Map = new Map(); - nodeToSymbol: Map = new Map(); + nodes: Map = new Map(); + symbols: Map = new Map(); constructor() {} } @@ -32,8 +32,8 @@ function collectNames(ctx: SemanticContext, node: SyntaxNode) { } case "TypeDecl": { const symbol = new TypeSymbol(undefined); - ctx.nameToSymbol.set(node.name, symbol); - ctx.nodeToSymbol.set(node, symbol); + ctx.nodes.set(node.name, node); + ctx.symbols.set(node, symbol); break; } } From 6c41a3008f09258b4907cf08082cafca42bdac9b Mon Sep 17 00:00:00 2001 From: marihachi Date: Fri, 19 Jul 2024 23:03:48 +0000 Subject: [PATCH 3/3] wip --- src/semantics/analyze.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/semantics/analyze.ts b/src/semantics/analyze.ts index ad08085..d726914 100644 --- a/src/semantics/analyze.ts +++ b/src/semantics/analyze.ts @@ -5,16 +5,21 @@ type SemanticSymbol = TypeSymbol; export class TypeSymbol { kind = "TypeSymbol" as const; constructor( - /** Primitive type or Type symbol */ - public baseType: string | TypeSymbol | undefined, + public baseType: TypeSymbol | 'not-resolved' | 'none', ) {} } export class SemanticContext { nodes: Map = new Map(); symbols: Map = new Map(); + builtinTypes: Map = new Map(); - constructor() {} + 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) { @@ -31,7 +36,7 @@ function collectNames(ctx: SemanticContext, node: SyntaxNode) { break; } case "TypeDecl": { - const symbol = new TypeSymbol(undefined); + const symbol = new TypeSymbol('not-resolved'); ctx.nodes.set(node.name, node); ctx.symbols.set(node, symbol); break;