Skip to content

Commit

Permalink
feat: add location to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shenyih0ng committed Apr 2, 2024
1 parent ce87563 commit ad70fb8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
16 changes: 13 additions & 3 deletions src/go-slang/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RuntimeSourceError } from '../errors/runtimeSourceError'
import { NodeLocation } from './types'

export class UnknownInstructionError extends RuntimeSourceError {
private inst_type: string
Expand All @@ -20,9 +21,12 @@ export class UnknownInstructionError extends RuntimeSourceError {
export class UndefinedError extends RuntimeSourceError {
private identifier: string

constructor(identifier: string) {
public location: NodeLocation

constructor(identifier: string, location: NodeLocation) {
super()
this.identifier = identifier
this.location = location
}

public explain() {
Expand All @@ -35,11 +39,14 @@ export class FuncArityError extends RuntimeSourceError {
private n_actual: number
private n_expected: number

constructor(func_name: string, n_actual: number, n_expected: number) {
public location: NodeLocation

constructor(func_name: string, n_actual: number, n_expected: number, location: NodeLocation) {
super()
this.func_name = func_name
this.n_actual = n_actual
this.n_expected = n_expected
this.location = location
}

public explain() {
Expand All @@ -54,9 +61,12 @@ export class FuncArityError extends RuntimeSourceError {
export class GoExprMustBeFunctionError extends RuntimeSourceError {
private expr: string

constructor(expr: string) {
public location: NodeLocation

constructor(expr: string, location: NodeLocation) {
super()
this.expr = expr
this.location = location
}

public explain() {
Expand Down
16 changes: 8 additions & 8 deletions src/go-slang/goroutine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ const Interpreter: {
C.pushR(...H.allocM(right), ...H.allocM(asgmts.reverse()))
},

GoStatement: ({ call }: GoStatement, { C, H, A }) => {
GoStatement: ({ call, loc }: GoStatement, { C, H, A }) => {
if (call.type !== NodeType.CallExpression) {
return new GoExprMustBeFunctionError(call.type)
return new GoExprMustBeFunctionError(call.type, loc!)
}

const { callee, args } = call as CallExpression
Expand All @@ -198,9 +198,9 @@ const Interpreter: {

Literal: (inst: Literal, { S, H }) => S.push(H.alloc(inst.value)),

Identifier: ({ name }: Identifier, { S, E, H }) => {
Identifier: ({ name, loc }: Identifier, { S, E, H }) => {
const value = E.lookup(name)
return value === null ? new UndefinedError(name) : S.push(H.alloc(value))
return value === null ? new UndefinedError(name, loc!) : S.push(H.alloc(value))
},

UnaryExpression: ({ argument, operator: op }: UnaryExpression, { C, H, A }) =>
Expand All @@ -227,8 +227,8 @@ const Interpreter: {
},

AssignOp: ({ idNodeUid }: AssignOp, { S, E, H, A }) => {
const name = A.get<Identifier>(idNodeUid).name
!E.assign(name, H.resolve(S.pop())) ? new UndefinedError(name) : void {}
const id = A.get<Identifier>(idNodeUid)
!E.assign(id.name, H.resolve(S.pop())) ? new UndefinedError(id.name, id.loc!) : void {}
},

UnaryOp: ({ opNodeId }: UnaryOp, { S, H, A }) => {
Expand Down Expand Up @@ -257,7 +257,7 @@ const Interpreter: {

if (paramNames.length !== values.length) {
const calleeId = A.get<Identifier>(calleeNodeId)
return new FuncArityError(calleeId.name, values.length, params.length)
return new FuncArityError(calleeId.name, values.length, params.length, calleeId.loc!)
}

C.pushR(...H.allocM([body, RetMarker, { type: CommandType.EnvOp, envId: E.id() }]))
Expand All @@ -277,7 +277,7 @@ const Interpreter: {

if (paramNames.length !== values.length) {
const calleeId = A.get<Identifier>(calleeNodeId)
return new FuncArityError(calleeId.name, values.length, params.length)
return new FuncArityError(calleeId.name, values.length, params.length, calleeId.loc!)
}

const _C: Control = new Stack()
Expand Down
2 changes: 1 addition & 1 deletion src/go-slang/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface Position {
column: number
offset: number
}
interface NodeLocation {
export interface NodeLocation {
start: Position
end: Position
offset: number
Expand Down

0 comments on commit ad70fb8

Please sign in to comment.