Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marihachi committed Aug 22, 2023
1 parent 5a7d338 commit bb56d3e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
46 changes: 22 additions & 24 deletions src/lib/semantics/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import * as builtins from './builtins.js';
import {
ExprSymbol,
FnSymbol,
FuncSymbol,
StructSymbol,
Symbol,
VariableSymbol
Expand Down Expand Up @@ -52,11 +52,13 @@ type StatementResult =
| 'return'
| 'break';

export type AnalyzeResult = {
success: boolean,
errors: string[],
warnings: string[],
};
export class AnalyzeResult {
constructor(
public success: boolean,
public errors: string[],
public warnings: string[],
) { }
}

export function analyze(
source: SourceFile,
Expand Down Expand Up @@ -84,11 +86,7 @@ export function analyze(
a.dispatchWarn('type checking of array elements is not supported yet.');
}

return {
success: (a.error.length == 0),
errors: a.error,
warnings: a.warn,
};
return new AnalyzeResult((a.error.length == 0), a.error, a.warn);
}

function declareTopLevel(node: FileNode, a: AnalyzeContext) {
Expand All @@ -109,7 +107,7 @@ function declareTopLevel(node: FileNode, a: AnalyzeContext) {
const params = node.params.map(x => ({ name: x.name }));

// declare function
const symbol = new FnSymbol(params, pendingType, []);
const symbol = new FuncSymbol(params, pendingType, []);
a.symbolTable.set(node, symbol);
a.env.set(node.name, symbol);
break;
Expand Down Expand Up @@ -152,7 +150,7 @@ function resolveTopLevel(node: FileNode, a: AnalyzeContext) {
}

// expect function symbol
if (symbol.kind != 'FnSymbol') {
if (symbol.kind != 'FuncSymbol') {
a.dispatchError('function expected.', node);
return;
}
Expand Down Expand Up @@ -229,7 +227,7 @@ function analyzeTopLevel(node: FileNode, a: AnalyzeContext) {
}

// expect function symbol
if (symbol.kind != 'FnSymbol') {
if (symbol.kind != 'FuncSymbol') {
a.dispatchError('function expected.', node);
return;
}
Expand Down Expand Up @@ -273,7 +271,7 @@ function analyzeTopLevel(node: FileNode, a: AnalyzeContext) {
/**
* @returns type of the last step of the block
*/
function analyzeBlock(nodes: StepNode[], allowJump: boolean, funcSymbol: FnSymbol, a: AnalyzeContext, before?: () => void): Type {
function analyzeBlock(nodes: StepNode[], allowJump: boolean, funcSymbol: FuncSymbol, a: AnalyzeContext, before?: () => void): Type {
if (isPendingType(funcSymbol.ty)) {
throw new UguisuError('unexpected type');
}
Expand Down Expand Up @@ -331,7 +329,7 @@ function analyzeBlock(nodes: StepNode[], allowJump: boolean, funcSymbol: FnSymbo
return blockTy;
}

function analyzeReferenceExpr(node: ReferenceExpr, allowJump: boolean, funcSymbol: FnSymbol, a: AnalyzeContext): Symbol | undefined {
function analyzeReferenceExpr(node: ReferenceExpr, allowJump: boolean, funcSymbol: FuncSymbol, a: AnalyzeContext): Symbol | undefined {
switch (node.kind) {
case 'Identifier': {
// get symbol
Expand Down Expand Up @@ -430,7 +428,7 @@ function analyzeReferenceExpr(node: ReferenceExpr, allowJump: boolean, funcSymbo
throw new UguisuError('unexpected node');
}

function analyzeStatement(node: StatementNode, allowJump: boolean, funcSymbol: FnSymbol, a: AnalyzeContext): StatementResult {
function analyzeStatement(node: StatementNode, allowJump: boolean, funcSymbol: FuncSymbol, a: AnalyzeContext): StatementResult {
switch (node.kind) {
case 'ExprStatement': {
analyzeExpr(node.expr, allowJump, funcSymbol, a);
Expand Down Expand Up @@ -583,7 +581,7 @@ function analyzeStatement(node: StatementNode, allowJump: boolean, funcSymbol: F
throw new UguisuError('unexpected node');
}

function analyzeExpr(node: ExprNode, allowJump: boolean, funcSymbol: FnSymbol, a: AnalyzeContext): Type {
function analyzeExpr(node: ExprNode, allowJump: boolean, funcSymbol: FuncSymbol, a: AnalyzeContext): Type {
// validate expression
switch (node.kind) {
case 'Identifier':
Expand Down Expand Up @@ -643,8 +641,8 @@ function analyzeExpr(node: ExprNode, allowJump: boolean, funcSymbol: FnSymbol, a
// check callable
let calleeTy;
switch (calleeSymbol.kind) {
case 'FnSymbol':
case 'NativeFnSymbol': {
case 'FuncSymbol':
case 'NativeFuncSymbol': {
calleeTy = calleeSymbol.ty;
break;
}
Expand Down Expand Up @@ -925,8 +923,8 @@ function analyzeExpr(node: ExprNode, allowJump: boolean, funcSymbol: FnSymbol, a

function getTypeFromSymbol(symbol: Symbol, errorNode: SyntaxNode, a: AnalyzeContext): Type {
switch (symbol.kind) {
case 'FnSymbol':
case 'NativeFnSymbol': {
case 'FuncSymbol':
case 'NativeFuncSymbol': {
return symbol.ty;
}
case 'StructSymbol': {
Expand Down Expand Up @@ -964,8 +962,8 @@ function resolveTyLabel(node: TyLabel, a: AnalyzeContext): Type {
case 'StructSymbol': {
return new NamedType(node.name);
}
case 'FnSymbol':
case 'NativeFnSymbol':
case 'FuncSymbol':
case 'NativeFuncSymbol':
case 'VariableSymbol':
case 'ExprSymbol': {
a.dispatchError('invalid type name.', node);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/semantics/builtins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NativeFnSymbol, StructSymbol, Symbol, VariableSymbol } from './symbol.js';
import { NativeFuncSymbol, StructSymbol, Symbol, VariableSymbol } from './symbol.js';
import {
AnalyzeContext
} from './common.js';
Expand All @@ -16,7 +16,7 @@ import {
function setDecl(name: string, paramsTy: ValidType[], returnTy: ValidType, a: AnalyzeContext) {
const params = Array(paramsTy.length).map(() => ({ name: 'x' }));
const ty = new FunctionType(paramsTy, returnTy);
a.env.set(name, new NativeFnSymbol(params, ty));
a.env.set(name, new NativeFuncSymbol(params, ty));
}

function group(name: string, a: AnalyzeContext, handler: (setItem: (name: string, paramsTy: ValidType[], returnTy: ValidType) => void) => void) {
Expand Down
16 changes: 8 additions & 8 deletions src/lib/semantics/symbol.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { BadType, FunctionType, PendingType, Type } from './type.js';

export type Symbol =
| FnSymbol
| NativeFnSymbol
| FuncSymbol
| NativeFuncSymbol
| StructSymbol
| VariableSymbol
| ExprSymbol;

export class FnSymbol {
kind = 'FnSymbol' as const;
export class FuncSymbol {
kind = 'FuncSymbol' as const;
constructor(
public params: { name: string }[],
public ty: FunctionType | PendingType | BadType,
/** for wasm */
public vars: FnVar[]
public vars: FuncVar[]
) { }
}
export type FnVar = { name: string, isParam: boolean, ty: Type };
export type FuncVar = { name: string, isParam: boolean, ty: Type };

export class NativeFnSymbol {
kind = 'NativeFnSymbol' as const;
export class NativeFuncSymbol {
kind = 'NativeFuncSymbol' as const;
constructor(
public params: { name: string }[],
public ty: FunctionType | PendingType | BadType,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/wasm/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function translateFunc(ctx: Context, node: FileNode) {
return;
}
const symbol = ctx.symbolTable.get(node);
if (symbol == null || symbol.kind != 'FnSymbol') {
if (symbol == null || symbol.kind != 'FuncSymbol') {
throw new UguisuError('unknown node');
}

Expand Down Expand Up @@ -297,7 +297,7 @@ function translateExpr(ctx: Context, node: ExprNode, func: FuncInfo): number {
}
case 'Call': {
const calleeSymbol = ctx.symbolTable.get(node.callee);
if (calleeSymbol == null || calleeSymbol.kind != 'FnSymbol') {
if (calleeSymbol == null || calleeSymbol.kind != 'FuncSymbol') {
throw new UguisuError('invalid node');
}
const callee = node.callee as Identifier;
Expand Down

0 comments on commit bb56d3e

Please sign in to comment.