Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 仮引数をparamにリネーム #829

Merged
merged 3 commits into from
Oct 29, 2024
Merged
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
12 changes: 6 additions & 6 deletions etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ const FALSE: {
};

// @public (undocumented)
const FN: (args: VUserFn["args"], statements: VUserFn["statements"], scope: VUserFn["scope"]) => VUserFn;
const FN: (params: VUserFn["params"], statements: VUserFn["statements"], scope: VUserFn["scope"]) => VUserFn;

// @public (undocumented)
type Fn = NodeBase & {
type: 'fn';
args: {
params: {
dest: Expression;
optional: boolean;
default?: Expression;
Expand All @@ -334,7 +334,7 @@ const FN_NATIVE: (fn: VNativeFn["native"]) => VNativeFn;
// @public (undocumented)
type FnTypeSource = NodeBase & {
type: 'fnTypeSource';
args: TypeSource[];
params: TypeSource[];
result: TypeSource;
};

Expand Down Expand Up @@ -746,7 +746,7 @@ declare namespace values {
VObj,
VFn,
VUserFn,
VFnArg,
VFnParam,
VNativeFn,
VReturn,
VBreak,
Expand Down Expand Up @@ -808,7 +808,7 @@ type VError = {
type VFn = VUserFn | VNativeFn;

// @public (undocumented)
type VFnArg = {
type VFnParam = {
dest: Expression;
type?: Type;
default?: Value;
Expand Down Expand Up @@ -859,7 +859,7 @@ type VStr = {
type VUserFn = VFnBase & {
native?: undefined;
name?: string;
args: VFnArg[];
params: VFnParam[];
statements: Node_2[];
scope: Scope;
};
Expand Down
16 changes: 8 additions & 8 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@
return result ?? NULL;
} else {
const fnScope = fn.scope!.createChildScope();
for (const i of fn.args.keys()) {
const argdef = fn.args[i]!;
if (!argdef.default) expectAny(args[i]);
this.define(fnScope, argdef.dest, args[i] ?? argdef.default!, true);
for (const [i, param] of fn.params.entries()) {
const arg = args[i];
if (!param.default) expectAny(arg);
this.define(fnScope, param.dest, arg ?? param.default!, true);
}

const info: CallInfo = { name: fn.name ?? '<anonymous>', pos };
Expand Down Expand Up @@ -362,7 +362,7 @@

case 'loop': {
// eslint-disable-next-line no-constant-condition
while (true) {

Check warning on line 365 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = await this._run(node.statements, scope.createChildScope(), callStack);
if (v.type === 'break') {
break;
Expand Down Expand Up @@ -542,12 +542,12 @@

case 'fn': {
return FN(
await Promise.all(node.args.map(async (arg) => {
await Promise.all(node.params.map(async (param) => {
return {
dest: arg.dest,
dest: param.dest,
default:
arg.default ? await this._eval(arg.default, scope, callStack) :
arg.optional ? NULL :
param.default ? await this._eval(param.default, scope, callStack) :
param.optional ? NULL :
undefined,
// type: (TODO)
};
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export function reprValue(value: Value, literalLike = false, processedObjects =
// そのうちネイティブ関数の引数も表示できるようにしたいが、ホスト向けの破壊的変更を伴うと思われる
return '@( ?? ) { native code }';
} else {
return `@( ${(value.args.map(v => v.dest.type === 'identifier' ? v.dest.name : '?')).join(', ')} ) { ... }`;
return `@( ${(value.params.map(v => v.dest.type === 'identifier' ? v.dest.name : '?')).join(', ')} ) { ... }`;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/interpreter/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type VFnBase = {
export type VUserFn = VFnBase & {
native?: undefined; // if (vfn.native) で型アサーション出来るように
name?: string;
args: VFnArg[];
params: VFnParam[];
statements: Node[];
scope: Scope;
};
export type VFnArg = {
export type VFnParam = {
dest: Expression;
type?: Type;
default?: Value;
Expand Down Expand Up @@ -128,9 +128,9 @@ export const ARR = (arr: VArr['value']): VArr => ({
value: arr,
});

export const FN = (args: VUserFn['args'], statements: VUserFn['statements'], scope: VUserFn['scope']): VUserFn => ({
export const FN = (params: VUserFn['params'], statements: VUserFn['statements'], scope: VUserFn['scope']): VUserFn => ({
type: 'fn' as const,
args: args,
params: params,
statements: statements,
scope: scope,
});
Expand Down
4 changes: 2 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export type If = NodeBase & {

export type Fn = NodeBase & {
type: 'fn'; // 関数
args: {
params: {
dest: Expression; // 引数名
optional: boolean;
default?: Expression; // 引数の初期値
Expand Down Expand Up @@ -363,6 +363,6 @@ export type NamedTypeSource = NodeBase & {

export type FnTypeSource = NodeBase & {
type: 'fnTypeSource'; // 関数の型
args: TypeSource[]; // 引数の型
params: TypeSource[]; // 引数の型
result: TypeSource; // 戻り値の型
};
4 changes: 2 additions & 2 deletions src/parser/plugins/validate-keyword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ function validateNode(node: Ast.Node): Ast.Node {
break;
}
case 'fn': {
for (const arg of node.args) {
validateDest(arg.dest);
for (const param of node.params) {
validateDest(param.dest);
}
break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser/plugins/validate-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ function validateNode(node: Ast.Node): Ast.Node {
break;
}
case 'fn': {
for (const arg of node.args) {
if (arg.argType != null) {
getTypeBySource(arg.argType);
for (const param of node.params) {
if (param.argType != null) {
getTypeBySource(param.argType);
}
}
if (node.retType != null) {
Expand Down
6 changes: 3 additions & 3 deletions src/parser/syntaxes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export function parseDest(s: ITokenStream): Ast.Expression {
* Params = "(" [Dest [":" Type] *(SEP Dest [":" Type])] ")"
* ```
*/
export function parseParams(s: ITokenStream): Ast.Fn['args'] {
const items: Ast.Fn['args'] = [];
export function parseParams(s: ITokenStream): Ast.Fn['params'] {
const items: Ast.Fn['params'] = [];

s.expect(TokenKind.OpenParen);
s.next();
Expand Down Expand Up @@ -200,7 +200,7 @@ function parseFnType(s: ITokenStream): Ast.TypeSource {

const resultType = parseType(s);

return NODE('fnTypeSource', { args: params, result: resultType }, startPos, s.getPos());
return NODE('fnTypeSource', { params, result: resultType }, startPos, s.getPos());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/syntaxes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ function parseFnExpr(s: ITokenStream): Ast.Fn {

const body = parseBlock(s);

return NODE('fn', { args: params, retType: type, children: body }, startPos, s.getPos());
return NODE('fn', { params: params, retType: type, children: body }, startPos, s.getPos());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parser/syntaxes/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function parseFnDef(s: ITokenStream): Ast.Definition {
return NODE('def', {
dest,
expr: NODE('fn', {
args: params,
params: params,
retType: type,
children: body,
}, startPos, endPos),
Expand Down
6 changes: 3 additions & 3 deletions src/parser/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export function visitNode(node: Ast.Node, fn: (node: Ast.Node) => Ast.Node): Ast
break;
}
case 'fn': {
for (const i of result.args.keys()) {
if (result.args[i]!.default) {
result.args[i]!.default = visitNode(result.args[i]!.default!, fn) as Ast.Fn['args'][number]['default'];
for (const param of result.params) {
if (param.default) {
param.default = visitNode(param.default!, fn) as Ast.Fn['params'][number]['default'];
}
}
for (let i = 0; i < result.children.length; i++) {
Expand Down
24 changes: 12 additions & 12 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export function T_GENERIC<N extends string>(name: N, inners: Type[]): TGeneric<N

export type TFn = {
type: 'fn';
args: Type[];
params: Type[];
result: Type;
};

export function T_FN(args: Type[], result: Type): TFn {
export function T_FN(params: Type[], result: Type): TFn {
return {
type: 'fn',
args,
params,
result,
};
}
Expand Down Expand Up @@ -80,10 +80,10 @@ export function isCompatibleType(a: Type, b: Type): boolean {
assertTFn(b); // NOTE: TypeGuardが効かない
// fn result
if (!isCompatibleType(a.result, b.result)) return false;
// fn args
if (a.args.length !== b.args.length) return false;
for (let i = 0; i < a.args.length; i++) {
if (!isCompatibleType(a.args[i]!, b.args[i]!)) return false;
// fn parameters
if (a.params.length !== b.params.length) return false;
for (let i = 0; i < a.params.length; i++) {
if (!isCompatibleType(a.params[i]!, b.params[i]!)) return false;
}
break;
}
Expand All @@ -101,7 +101,7 @@ export function getTypeName(type: Type): string {
return `${type.name}<${type.inners.map(inner => getTypeName(inner)).join(', ')}>`;
}
case 'fn': {
return `@(${type.args.map(arg => getTypeName(arg)).join(', ')}) { ${getTypeName(type.result)} }`;
return `@(${type.params.map(param => getTypeName(param)).join(', ')}) { ${getTypeName(type.result)} }`;
}
}
}
Expand All @@ -117,9 +117,9 @@ export function getTypeNameBySource(typeSource: Ast.TypeSource): string {
}
}
case 'fnTypeSource': {
const args = typeSource.args.map(arg => getTypeNameBySource(arg)).join(', ');
const params = typeSource.params.map(param => getTypeNameBySource(param)).join(', ');
const result = getTypeNameBySource(typeSource.result);
return `@(${args}) { ${result} }`;
return `@(${params}) { ${result} }`;
}
}
}
Expand Down Expand Up @@ -153,7 +153,7 @@ export function getTypeBySource(typeSource: Ast.TypeSource): Type {
}
throw new AiScriptSyntaxError(`Unknown type: '${getTypeNameBySource(typeSource)}'`, typeSource.loc.start);
} else {
const argTypes = typeSource.args.map(arg => getTypeBySource(arg));
return T_FN(argTypes, getTypeBySource(typeSource.result));
const paramTypes = typeSource.params.map(param => getTypeBySource(param));
return T_FN(paramTypes, getTypeBySource(typeSource.result));
}
}
1 change: 1 addition & 0 deletions unreleased/rename-arg-param.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- For Hosts: いくつかの型で args を params にリネームしました。
Loading