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

feat/arity - refactor to allow for variable number of arguments #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"lint": "eslint",
"test": "vitest",
"plumber": "node ./dist/main.js",
"prepublish": "npm run build"
"prepack": "npm run build"
},
"devDependencies": {
"@types/node": "^20.11.5",
Expand Down
4 changes: 2 additions & 2 deletions src/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ export class Interpreter
throw new RuntimeError('Can only call functions and classes', expr.paren)
}

if (args.length !== callee.arity()) {
if (!callee.arity(args.length)) {
throw new RuntimeError(
`Expected ${callee.arity()} arguments but got ${args.length}.`,
`Incorrect number of arguments provided: ${args.length}.`,
expr.paren,
)
}
Expand Down
69 changes: 69 additions & 0 deletions src/__tests__/class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { beforeAll, describe, expect, it } from 'vitest'

import { PlumberScript } from '../PlumberScript'
import { beforeEach } from 'node:test'

const plumber = new PlumberScript()

describe('Class', () => {
beforeAll(() => {
plumber.evaluate(`
class Person {
init(first, last) {
this.first = first;
this.last = last;
}

fullName() {
return this.first + " " + this.last;
}

setFirst(first) {
this.first = first;
}

setLast(last) {
this.last = last;
}
}
`)

plumber.evaluate(`
class Employee extends Person {
init(first, last, age) {
super.init(first, last);
this.age = age;
}

isRetirementAge() {
return this.age >= 65;
}
}
`)
})
it('allows for methods in the base class to work correctly', () => {
let fullName = plumber.evaluate(`
let p = Person("John","Doe");
p.fullName()
`)
expect(fullName).toEqual('John Doe')

fullName = plumber.evaluate(`p.setFirst("Jane"); p.fullName()`)
expect(fullName).toEqual('Jane Doe')

fullName = plumber.evaluate(`p.first = "John"; p.fullName()`)
expect(fullName).toEqual('John Doe')
})

it('allows for methods in the superclass to work correctly', () => {
plumber.evaluate(`
let e = Employee("Chu Kang","Phua",65);
`)

let fullName = plumber.evaluate(`e.fullName()`) // test superclass method
expect(fullName).toEqual('Chu Kang Phua')

let isRetirementAge = plumber.evaluate(`e.isRetirementAge()`)
expect(isRetirementAge).toEqual(true)
})
})
6 changes: 3 additions & 3 deletions src/ast/PlumberClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export class PlumberClass extends PlumberCallable {
return instance
}

arity(): number {
arity(argLength: number): boolean {
const initializer = this.findMethod('init')
if (initializer === null) return 0
return initializer.arity()
if (initializer === null) return true
return initializer.arity(argLength)
}
}
6 changes: 3 additions & 3 deletions src/ast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PlumberInstance } from './PlumberInstance'
import { FunctionStmt } from './Stmt'

export abstract class PlumberCallable {
abstract arity(): number
abstract arity(argLength: number): boolean
abstract call(interpreter: Interpreter, args: Array<PlumberObject>): PlumberObject
}

Expand Down Expand Up @@ -34,8 +34,8 @@ export class PlumberFunction extends PlumberCallable {
return `<fn ${this.declaration.name.lexeme}>`
}

arity(): number {
return this.declaration.params.length
arity(argLength: number): boolean {
return argLength === this.declaration.params.length
}

call(interpreter: Interpreter, args: Array<PlumberObject>): PlumberObject {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/abs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Interpreter } from '../Interpreter'
import { PlumberCallable, PlumberObject } from '../ast/types'

export class AbsFunction extends PlumberCallable {
arity(): number {
return 1
arity(argLength: number): boolean {
return argLength === 1
}

call(_: Interpreter, args: Array<PlumberObject>): PlumberObject {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/power.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Interpreter } from '../Interpreter'
import { PlumberCallable, PlumberObject } from '../ast/types'

export class PowerFunction extends PlumberCallable {
arity(): number {
return 2
arity(argLength: number): boolean {
return argLength === 2
}

call(_: Interpreter, args: Array<PlumberObject>): PlumberObject {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/str-replace-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Interpreter } from '../Interpreter'
import { PlumberCallable, PlumberObject } from '../ast/types'

export class StrReplaceAllFunction extends PlumberCallable {
arity(): number {
return 3
arity(argLength: number): boolean {
return argLength === 3
}

call(_: Interpreter, args: Array<PlumberObject>): PlumberObject {
Expand Down