Skip to content

Commit

Permalink
Abstract meaning into the Meaning enum
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Jun 4, 2023
1 parent 03e83b2 commit 5f853d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
19 changes: 13 additions & 6 deletions src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ export function bind(m: Module) {
}
}

export function resolve(
locals: Table,
name: string,
meaning: Node.Var | Node.TypeAlias | Node.Let,
) {
export enum Meaning {
Value,
Type,
}

export function resolve(locals: Table, name: string, meaning: Meaning) {
const symbol = locals.get(name);
if (symbol?.declarations.some((d) => d.kind === meaning)) {
if (
symbol?.declarations.some((d) =>
meaning === Meaning.Value
? [Node.Var, Node.Let].includes(d.kind)
: d.kind === Node.TypeAlias,
)
) {
return symbol;
}
}
29 changes: 12 additions & 17 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TypeAlias,
} from './types';
import { error } from './error';
import { resolve } from './bind';
import { Meaning, resolve } from './bind';

const stringType: Type = { id: 'string' };
const numberType: Type = { id: 'number' };
Expand Down Expand Up @@ -51,23 +51,14 @@ export function check(module: Module) {
function checkExpression(expression: Expression): Type {
switch (expression.kind) {
case Node.Identifier:
const resolveExpression = (kind: Node.Var | Node.Let) =>
resolve(module.locals, expression.text, kind);
const resolveExpression = (meaning: Meaning) =>
resolve(module.locals, expression.text, meaning);

const varSymbol = resolveExpression(Node.Var);
const symbol = resolveExpression(Meaning.Value);

if (varSymbol) {
return checkStatement(varSymbol.valueDeclaration!);
}

const letSymbol = resolveExpression(Node.Let);

if (letSymbol) {
if (
letSymbol.valueDeclaration &&
letSymbol.valueDeclaration.pos < expression.pos
) {
return checkStatement(letSymbol.valueDeclaration!);
if (symbol?.valueDeclaration?.kind === Node.Let) {
if (symbol.valueDeclaration.pos < expression.pos) {
return checkStatement(symbol.valueDeclaration!);
}

error(
Expand All @@ -78,6 +69,10 @@ export function check(module: Module) {
return errorType;
}

if (symbol?.valueDeclaration?.kind === Node.Var) {
return checkStatement(symbol.valueDeclaration!);
}

error(expression.pos, 'Could not resolve ' + expression.text);
return errorType;
case Node.NumericLiteral:
Expand Down Expand Up @@ -105,7 +100,7 @@ export function check(module: Module) {
case 'number':
return numberType;
default:
const symbol = resolve(module.locals, name.text, Node.TypeAlias);
const symbol = resolve(module.locals, name.text, Meaning.Type);
if (symbol) {
return checkType(
(
Expand Down

0 comments on commit 5f853d7

Please sign in to comment.