Skip to content

Commit

Permalink
fix signature stringification
Browse files Browse the repository at this point in the history
+ signature stringification reworked
+ fix truncation error for functions with no parameters
  • Loading branch information
michal-kapala committed Mar 29, 2024
1 parent 05d21e3 commit bec9b76
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jitterbit-script",
"version": "1.0.13",
"version": "1.0.14",
"description": "Static typechecker and interpreter for Jitterbit Script",
"main": "build/index.js",
"scripts": {
Expand Down
56 changes: 37 additions & 19 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,22 @@ export abstract class Func {
abstract analyzeCall(args: TypedExpr[], env: TypeEnv): TypeInfo;

/**
* The function's signature string.
* Returns the function's signature.
*
* For polymorphic functions the specified or default (first) is returned.
* @param sigIdx
* @returns
*/
toString() {
if(this.signature) {
let signature = `${this.name}(`;
let optionals = false;
const params = this.signature.params;
for(let idx = 0; idx < this.signature.params.length; idx++) {
if(!optionals && !params[idx].required) {
optionals = true;
signature += "[";
}
signature += `${params[idx].type} ${params[idx].name}, `;
}
signature = signature.substring(0, signature.length - 2);
signature += optionals ? "])" : ")";
signature += `: ${this.signature.returnType}`;
return signature;
}
else return `${this.name} (polymorphic)`;
toString(sigIdx?: number) {
// single signature
if(this.signature)
return this.signature.toString(this.name);
// specified
else if(sigIdx && 0 <= sigIdx && sigIdx < this.signatures.length)
return this.signatures[sigIdx].toString(this.name);
// first (default)
else
return this.signatures[0].toString(this.name);
}
}

Expand Down Expand Up @@ -140,6 +135,29 @@ export class Signature {
this.returnType = returnType;
this.params = params;
}

/**
* Returns the signature's string representation.
* @param funcName
* @returns
*/
toString(funcName: string) {
let result = `${funcName}(`;
let optionals = false;
const params = this.params;
for(let idx = 0; idx < this.params.length; idx++) {
if(!optionals && !params[idx].required) {
optionals = true;
result += "[";
}
result += `${params[idx].type} ${params[idx].name}, `;
}
if(this.params.length > 0)
result = result.substring(0, result.length - 2);
result += optionals ? "])" : ")";
result += `: ${this.returnType}`;
return result;
}
}

/**
Expand Down

0 comments on commit bec9b76

Please sign in to comment.