From 9cbe546e8a220bee464f87f6c8792c404ceb8ea1 Mon Sep 17 00:00:00 2001 From: DiamondNether90 Date: Fri, 12 Sep 2025 11:57:45 +1000 Subject: [PATCH] Error function --- docs/highlight.py | 2 +- src/main.cpp | 22 +++++++++++++++++++++- tests/error.grnd | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/error.grnd diff --git a/docs/highlight.py b/docs/highlight.py index bce796c..85f2aaa 100644 --- a/docs/highlight.py +++ b/docs/highlight.py @@ -15,7 +15,7 @@ def isnumber(num): allstr = "" color = "" -keywords = ["if", "jump", "end", "stdin", "stdout", "stdlnout", "set", "gettype", "exists", "setlist", "setlistat", "getlistat", "getlistsize", "listappend", "getstrsize", "getstrcharat", "add", "subtract", "multiply", "divide", "equal", "inequal", "not", "greater", "lesser", "stoi", "stod", "tostring", "fun", "return", "endfun", "pusharg", "call", "use", "extern"] +keywords = ["if", "jump", "end", "stdin", "stdout", "stdlnout", "set", "gettype", "exists", "setlist", "setlistat", "getlistat", "getlistsize", "listappend", "getstrsize", "getstrcharat", "add", "subtract", "multiply", "divide", "equal", "inequal", "not", "greater", "lesser", "stoi", "stod", "tostring", "fun", "return", "endfun", "pusharg", "call", "use", "extern", "error"] for line in thefile: allstr += line + "
" diff --git a/src/main.cpp b/src/main.cpp index d9bd7c8..053af1a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,7 +78,8 @@ enum class Instructions { Getstrcharat, Getstrsize, Stoi, Stod, Tostring, Fun, Return, Endfun, Pusharg, Call, Local, - Use, Extern + Use, Extern, + Error }; /* @@ -636,6 +637,24 @@ Literal exec(vector in, bool executingFunction) { error("Argument of stdlnout must be a value (literal or a value reference)"); } break; + /* + error instruction + This instruction outputs a custom error message. + */ + + case Instructions::Error: + if (l.args.size() < 1) { + error("Could not find argument for Error inbuilt"); + } + if (holds_alternative(l.args[0])) { + if (holds_alternative(get(l.args[0]).val)) { + error(get(get(l.args[0]).val)); + } else { + error("Argument of error must be a string"); + } + } else { + } + break; /* set instruction This instruction sets a variable to a provided value. @@ -2232,6 +2251,7 @@ vector parser(vector> in) { else if (i == "stdin") newInst.inst = Instructions::Stdin; else if (i == "stdout") newInst.inst = Instructions::Stdout; else if (i == "stdlnout") newInst.inst = Instructions::Stdlnout; + else if (i == "error") newInst.inst = Instructions::Error; else if (i == "jump") newInst.inst = Instructions::Jump; else if (i == "if") newInst.inst = Instructions::If; else if (i == "add") newInst.inst = Instructions::Add; diff --git a/tests/error.grnd b/tests/error.grnd new file mode 100644 index 0000000..bd32ab4 --- /dev/null +++ b/tests/error.grnd @@ -0,0 +1 @@ +error "Hello, world!" \ No newline at end of file