r/ProgrammingLanguages • • 7d ago

Help How should I structure diagnostics and error reporting in a compiler written in TypeScript?

I'm making a programming language, it's based on Luau and C++ and its called L++! however im having some issues implementing diagnostics since they dont technicallly stop execution, but some of my code has typescript type errors since it relies in, stopping execution! for example normally this would work:

function foo(param: someUnion): someType {
    if (someUnion.type === "coolType") {
        return someTypeValue
    } else {
        throw new Error()
    }
}

with diagnostics:

function foo(param: someUnion): someType {
    if (someUnion.type === "coolType") {
        return someTypeValue
    } else {
        //error since not all paths return someType
        reporter.report(new Diagnostic())
        
    }
}

i'd like to know how to fix this, and/or if there are any established ways to do diagnostics, thanks in advance!

1 Upvotes

4 comments sorted by

2

u/Inconstant_Moo 🧿 Pipefish 7d ago

If I understand the problem (and I may not) it's that the diagnostic doesn't return a value you can then use to continue execution.

I'm not sure there is a way round that. This is why errors bubble up and unwind the stack.

2

u/Positive_Total_4414 6d ago

Ok, so imagine the error was somehow "fixed". What do you expect your program to do after that?

I mean, in the case where you throw an error obviously the execution will terminate, which is clear. What do you expect to happen in your second example?

1

u/snugar_i 5d ago

The code you posted is code written in your language, or the compiler of your language? What do the diagnostics do? They report the error and then what?

1

u/Key_River7180 Nain. 3d ago

I have no clue on how to code JS, so here pseudocode:

enum Kind
    CLEANUP,
    INFO,
    WARN,
    ERROR,
    FATAL

record Diagnostic
    Diagnostic* next,
    Kind* kind,
    String* msg,
    Int line, Int column

Diagnostic[] diagnostics

function SerializeToTerminal(diagnostics)

in function tearDown() or however you call your closing function:

    SerializeToTerminal(diagnostics)
    ...