Skip to content

Handling Runtime Error

Lexi edited this page Nov 26, 2024 · 1 revision

The PotatoChip.h defines the structure and utilities for generating and managing runtime errors. Centralized error routines like HANDLERUNTIMEERROR are implemented there.

EmitUnformattedLine(";------------------------------------------------------------");
EmitUnformattedLine("; Issue \"Run-time error #X..X near line #X..X\" to handle run-time errors");
EmitUnformattedLine(";------------------------------------------------------------");
EmitFormattedLine("HANDLERUNTIMEERROR","EQU","*");
EmitFormattedLine("","SVC","#SVC_WRITE_ENDL");
AddDSToStaticData("Run-time error #","",reference);
EmitFormattedLine("","PUSHA",reference);
EmitFormattedLine("","SVC","#SVC_WRITE_STRING");
EmitFormattedLine("","SVC","#SVC_WRITE_INTEGER");
AddDSToStaticData(" near line #","",reference);
EmitFormattedLine("","PUSHA",reference);
EmitFormattedLine("","SVC","#SVC_WRITE_STRING");
EmitFormattedLine("","SVC","#SVC_WRITE_INTEGER");
EmitFormattedLine("","SVC","#SVC_WRITE_ENDL");
EmitFormattedLine("","PUSH","#0D1");
EmitFormattedLine("","SVC","#SVC_TERMINATE");  

Notes:

  • Error #X corresponds to the type of runtime error (e.g., Assertion Failure, Division by Zero).
  • Line #X specifies where the error was triggered in the source program.
  • Error handling terminates execution after displaying relevant details.

Here are some key example uses from PotatoChip.cpp

Example 1: Assertion Failures

Implementation:

void ParseAssertion(TOKEN tokens[]) {
    code.EmitFormattedLine("", "SETT");
    code.EmitFormattedLine("", "JMPT", Elabel);
    code.EmitFormattedLine("", "PUSH", "#0D" + tokens[0].sourceLineNumber);
    code.EmitFormattedLine("", "PUSH", "#0D1");
    code.EmitFormattedLine("", "JMP", "HANDLERUNTIMEERROR");
}

Example Trigger:

{ 5 > 10 }  // Triggers runtime error due to invalid assertion.

Example 2: Division by Zero in Expressions

Implementation:

if (operation == DIVIDE) {
    code.EmitFormattedLine("", "SETNZPI");
    code.EmitFormattedLine("", "JMPNZ", "HANDLERUNTIMEERROR");
}

Example Trigger:

x = 10/0;