11 Commits

Author SHA1 Message Date
9e329968d1 Error types, print lists, better fn calling 2025-09-13 15:47:24 +10:00
e56c560514 Merge pull request 'Add Error Function' (#18) from DiamondNether90/ground_fork:master into master
Reviewed-on: max/ground#18
2025-09-12 12:05:05 +10:00
9cbe546e8a Error function 2025-09-12 11:57:45 +10:00
d9790711c6 Merge pull request 'Merge pull request 'Update docs/highlight.py' (#15) from DiamondNether90/ground_fork:master into master' (#1) from max/ground:master into master
Reviewed-on: https://git.maxwellj.xyz/DiamondNether90/ground_fork/pulls/1
2025-09-12 11:15:42 +10:00
310fede3ec Merge pull request 'Update docs/highlight.py' (#15) from DiamondNether90/ground_fork:master into master
Reviewed-on: https://git.maxwellj.xyz/max/ground/pulls/15
2025-09-02 20:42:39 +10:00
a4eba4ae47 Merge branch 'master' into master 2025-09-02 20:42:32 +10:00
e2a037befc Update docs/highlight.py
Fixed mishighlight for negative numbers
2025-09-02 08:38:54 +10:00
872392c1c5 Simple escape sequences 2025-09-01 13:10:46 +10:00
2e1e2e727b Merge pull request 'Update docs/highlight.py' (#13) from DiamondNether90/ground_fork:master into master
Reviewed-on: https://git.maxwellj.xyz/max/ground/pulls/13
2025-09-01 08:32:08 +10:00
38681f72d7 Merge branch 'master' into master 2025-09-01 08:31:46 +10:00
074b473bb1 Merge pull request 'Add Python Highlighting Script' (#12) from DiamondNether90/ground_fork:master into master
Reviewed-on: max/ground#12
2025-08-31 21:05:03 +10:00
6 changed files with 247 additions and 101 deletions

View File

@@ -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 + " <br> "
@@ -35,6 +35,8 @@ for i in range(lines):
color = "\033[32m"
elif tempword in keywords:
color = "\033[95m"
elif isnumber(tempword) or tempword == "true" or tempword == "false":
color = "\033[96m"
elif tempword[0] == "#":
incom = True
color = "\033[37m"
@@ -55,8 +57,6 @@ for i in range(lines):
instr = not instr
elif tempword[0] == "\'" and tempword[len(tempword)-1] == "\'":
color = "\033[92m"
elif isnumber(tempword) or tempword == "true" or tempword == "false":
color = "\033[96m"
elif instr:
color = "\033[92m"
elif incom:

View File

@@ -78,7 +78,7 @@ enum class Instructions {
Getstrcharat, Getstrsize,
Stoi, Stod, Tostring,
Fun, Return, Endfun, Pusharg, Call, Local,
Use, Extern
Use, Extern, Error, Catch, Try, Exception
};
/*
@@ -369,8 +369,8 @@ Literal groundValueToLiteral(const GroundValue& gv) {
Takes a string (which is a debug message) and prints it to the console, letting the
user know what went wrong with the program.
*/
void error(string in, int exitc = 1) {
cout << "\033[31mError: \033[39m" << in << endl;
void error(string in, string errCode = "syntaxError", int exitc = 1) {
cout << "\033[31m" + errCode + ": \033[39m" << in << endl;
exit(exitc);
}
@@ -484,6 +484,7 @@ Types getLitType(Literal in) {
if (holds_alternative<bool>(in.val)) return Types::Bool;
if (holds_alternative<string>(in.val)) return Types::String;
if (holds_alternative<char>(in.val)) return Types::Char;
if (holds_alternative<List>(in.val)) return Types::List;
error("Literal for some reason has a weird type. This is not an issue with your program, but an issue with the Ground interpreter.");
return Types::Int;
}
@@ -594,7 +595,42 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
cout << get<char>(get<Literal>(l.args[0]).val);
}
else {
error("Couldn't print that");
error("Couldn't print that", "printError");
}
} else if (holds_alternative<ListRef>(l.args[0])) {
if (variables.find(get<ListRef>(l.args[0]).listName) != variables.end()) {
List list = get<List>(variables[get<ListRef>(l.args[0]).listName].val);
cout << "[";
for (int l = 0; l < list.val.size(); l++) {
if (holds_alternative<string>(list.val[l].val)) {
cout << '"' << get<string>(list.val[l].val) << '"';
}
else if (holds_alternative<int>(list.val[l].val)) {
cout << get<int>(list.val[l].val);
}
else if (holds_alternative<double>(list.val[l].val)) {
cout << get<double>(list.val[l].val);
}
else if (holds_alternative<bool>(list.val[l].val)) {
if (get<bool>(list.val[l].val) == true) {
cout << "true";
} else {
cout << "false";
}
}
else if (holds_alternative<char>(list.val[l].val)) {
cout << '\'' << get<char>(list.val[l].val) << '\'';
}
else {
error("Couldn't print that", "printError");
}
if (l != list.val.size() - 1) {
cout << ", ";
}
}
cout << "]";
} else {
error("Couldn't find list named " + get<ListRef>(l.args[0]).listName);
}
} else {
error("Argument of stdlnout must be a value (literal or a value reference)");
@@ -630,12 +666,78 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
cout << get<char>(get<Literal>(l.args[0]).val) << endl;
}
else {
error("Couldn't print that");
error("Couldn't print that", "printError");
}
} else if (holds_alternative<ListRef>(l.args[0])) {
if (variables.find(get<ListRef>(l.args[0]).listName) != variables.end()) {
List list = get<List>(variables[get<ListRef>(l.args[0]).listName].val);
cout << "[";
for (int l = 0; l < list.val.size(); l++) {
if (holds_alternative<string>(list.val[l].val)) {
cout << '"' << get<string>(list.val[l].val) << '"';
}
else if (holds_alternative<int>(list.val[l].val)) {
cout << get<int>(list.val[l].val);
}
else if (holds_alternative<double>(list.val[l].val)) {
cout << get<double>(list.val[l].val);
}
else if (holds_alternative<bool>(list.val[l].val)) {
if (get<bool>(list.val[l].val) == true) {
cout << "true";
} else {
cout << "false";
}
}
else if (holds_alternative<char>(list.val[l].val)) {
cout << '\'' << get<char>(list.val[l].val) << '\'';
}
else {
error("Couldn't print that", "printError");
}
if (l != list.val.size() - 1) {
cout << ", ";
}
}
cout << "]" << endl;
} else {
error("Couldn't find list named " + get<ListRef>(l.args[0]).listName);
}
} else {
error("Argument of stdlnout must be a value (literal or a value reference)");
error("Argument of stdlnout must be a value (literal or a value reference) or a list reference");
}
break;
/*
error instruction
This instruction outputs a custom error message.
*/
case Instructions::Error:
if (l.args.size() < 2) {
error("Could not find arguments for Error inbuilt");
}
if (holds_alternative<Literal>(l.args[0])) {
if (holds_alternative<string>(get<Literal>(l.args[0]).val) && holds_alternative<string>(get<Literal>(l.args[1]).val)) {
error(get<string>(get<Literal>(l.args[0]).val), get<string>(get<Literal>(l.args[1]).val));
} else {
error("Argument of error must be a string");
}
} else {
error("Argument of error must be a string");
}
break;
/*
catch instruction
This instruction ensures that errors in programs are caught and dealt with appropriately.
*/
case Instructions::Catch:
if (l.args.size() < 2) {
error("Could not find all arguments for Catch inbuilt");
}
if (holds_alternative<Literal>(l.args[0])) {
if (holds_alternative<string>(get<Literal>(l.args[0]).val)) {
}
}
/*
set instruction
This instruction sets a variable to a provided value.
@@ -737,7 +839,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
bool existed = variables.count(var.varName) > 0;
variables[var.varName] = get<List>(variables[listref.listName].val).val[ref];
} else {
error("Index " + to_string(ref) + " out of range of list " + listref.listName);
error("Index " + to_string(ref) + " out of range of list " + listref.listName, "rangeError");
}
} else {
error("Found a normal variable in place of a list");
@@ -793,7 +895,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
bool existed = variables.count(var.varName) > 0;
variables[var.varName] = newLit;
} else {
error("Index " + to_string(ref) + " out of range of string " + instr);
error("Index " + to_string(ref) + " out of range of string " + instr, "rangeError");
}
}
break;
@@ -838,7 +940,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
tmpList.val[ref] = value;
variables[listref.listName].val = tmpList;
} else {
error("Index " + to_string(ref) + " out of range of list " + listref.listName);
error("Index " + to_string(ref) + " out of range of list " + listref.listName, "rangeError");
}
}
} else {
@@ -987,7 +1089,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
bool existed = variables.count(ref.varName) > 0;
variables[ref.varName] = newLit;
} else {
error("Cannot convert the value " + toConv + " to an int");
error("Cannot convert the value " + toConv + " to an int", "conversionError");
}
}
break;
@@ -1026,7 +1128,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
bool existed = variables.count(ref.varName) > 0;
variables[ref.varName] = newLit;
} else {
error("Cannot convert the value " + toConv + " to a decimal");
error("Cannot convert the value " + toConv + " to a decimal", "conversionError");
}
}
break;
@@ -1148,7 +1250,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<char>(left.val) && holds_alternative<char>(right.val)) {
final.val = string().append(&get<char>(left.val)).append(&get<char>(right.val));
} else {
error("Cannot add those two values");
error("Cannot add those two values", "mathError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1198,7 +1300,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<double>(left.val) && holds_alternative<int>(right.val)) {
final.val = get<double>(left.val) - double(get<int>(right.val));
} else {
error("Cannot subtract those two values");
error("Cannot subtract those two values", "mathError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1248,7 +1350,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<double>(left.val) && holds_alternative<int>(right.val)) {
final.val = get<double>(left.val) * double(get<int>(right.val));
} else {
error("Cannot multiply those two values");
error("Cannot multiply those two values", "mathError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1291,12 +1393,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
// Ensure we aren't dividing by zero
if (holds_alternative<int>(right.val)) {
if (get<int>(right.val) == 0) {
error("Division by zero is not allowed");
error("Division by zero is not allowed", "divisionByZeroError");
}
}
if (holds_alternative<double>(right.val)) {
if (get<double>(right.val) == 0) {
error("Division by zero is not allowed");
error("Division by zero is not allowed", "divisionByZeroError");
}
}
@@ -1310,7 +1412,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<double>(left.val) && holds_alternative<int>(right.val)) {
final.val = get<double>(left.val) / double(get<int>(right.val));
} else {
error("Cannot divide those two values");
error("Cannot divide those two values", "mathError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1366,7 +1468,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<bool>(left.val) && holds_alternative<bool>(right.val)) {
final.val = get<bool>(left.val) == get<bool>(right.val);
} else {
error("Cannot equal those two values");
error("Cannot equal those two values", "comparisonError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1422,7 +1524,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<bool>(left.val) && holds_alternative<bool>(right.val)) {
final.val = get<bool>(left.val) != get<bool>(right.val);
} else {
error("Cannot inequal those two values");
error("Cannot inequal those two values", "comparisonError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1511,7 +1613,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<bool>(left.val) && holds_alternative<bool>(right.val)) {
final.val = get<bool>(left.val) > get<bool>(right.val);
} else {
error("Cannot greater those two values");
error("Cannot greater those two values", "comparisonError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1567,7 +1669,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else if (holds_alternative<bool>(left.val) && holds_alternative<bool>(right.val)) {
final.val = get<bool>(left.val) < get<bool>(right.val);
} else {
error("Cannot lesser those two values");
error("Cannot lesser those two values", "comparisonError");
}
bool existed = variables.count(varRef.varName) > 0;
@@ -1674,7 +1776,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
variables[ref.varName].val = "char";
break;
default:
error("Could not get type?? This should never be reached. Please report this issue");
error("Could not get type?? This should never be reached. Please report this issue", "undefinedError");
break;
}
@@ -1894,7 +1996,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
// Type checking - now with error reporting
if (arg.type != getLitType(fnArgs[m])) {
error("Function " + ref.fnName + " argument " + to_string(m + 1) +
" type mismatch. Expected type does not match provided argument type.");
" type mismatch. Expected type does not match provided argument type.", "typeError");
}
// Create the variable
@@ -1957,11 +2059,11 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
string groundLibsDir = getenv("GROUND_LIBS");
if (filesystem::exists(useName)) {
} else if (groundLibsDir != "" && filesystem::exists(groundLibsDir + useName)) {
useName = groundLibsDir + useName;
// no need to do anything here
} else if (groundLibsDir != "" && filesystem::exists(groundLibsDir + useName + ".grnd")) {
useName = groundLibsDir + useName + ".grnd";
} else {
error("Could not find external Ground library in $GROUND_LIBS or current directory.");
error("Could not find external Ground library in $GROUND_LIBS (currently set to " + groundLibsDir +") or current directory.", "libraryError");
}
ifstream file(useName);
@@ -2036,13 +2138,13 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
if (!found) {
error("Could not find external library: " + fullLibName +
" (searched current directory and GROUND_LIBS)");
" (searched current directory and GROUND_LIBS)", "libraryError");
}
// Try to open the library
void* handle = DLOPEN(libPath.c_str());
if (!handle) {
error("Failed to load library " + libPath + ": " + string(DLERROR()));
error("Failed to load library " + libPath + ": " + string(DLERROR()), "libraryError");
}
// Store handle for cleanup later
@@ -2056,7 +2158,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
GetFunctionFunc getFunction = (GetFunctionFunc)DLSYM(handle, "ground_get_function");
if (!getFunctions || !getFunction) {
error("Library " + libName + " is not Ground-compatible (missing required functions: ground_get_functions or ground_get_function)");
error("Library " + libName + " is not Ground-compatible (missing required functions: ground_get_functions or ground_get_function)", "libraryError");
}
// Optional initialization
@@ -2079,12 +2181,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
externalFunctions[libName + ":" + string(functionNames[i])] = funcPtr;
functionCount++;
} else {
error("Failed to get function pointer for: " + string(functionNames[i]));
error("Failed to get function pointer for: " + string(functionNames[i]), "libraryError");
}
}
if (functionCount == 0) {
error("No functions were loaded from library: " + libName);
error("No functions were loaded from library: " + libName, "libraryError");
}
}
break;
@@ -2098,6 +2200,37 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return retLiteral;
}
string interpretEscapeSequences(string input) {
string output;
for (size_t i = 0; i < input.length(); ++i) {
if (input[i] == '\\' && i + 1 < input.length()) {
char next = input[i + 1];
switch (next) {
case 'n': output += '\n'; break;
case 't': output += '\t'; break;
case 'r': output += '\r'; break;
case 'b': output += '\b'; break;
case 'f': output += '\f'; break;
case 'a': output += '\a'; break;
case 'v': output += '\v'; break;
case '\\': output += '\\'; break;
case '\'': output += '\''; break;
case '\"': output += '\"'; break;
case '0': output += '\0'; break;
default:
output += '\\';
output += next;
break;
}
++i;
} else {
output += input[i];
}
}
return output;
}
/*
lexer function
This function takes a string (the user's program) and splits it into smaller chunks
@@ -2198,9 +2331,19 @@ vector<Instruction> parser(vector<vector<string>> in) {
newInst.isLabel = true;
newInst.label.id = i.substr(1);
}
else if (isFunction(i)){
newInst.inst = Instructions::Call;
FunctionRef newFnRef;
newFnRef.fnName = i.substr(1);
newInst.args.push_back(newFnRef);
}
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 == "try") newInst.inst = Instructions::Try;
else if (i == "catch") newInst.inst = Instructions::Catch;
else if (i == "exception") newInst.inst = Instructions::Exception;
else if (i == "jump") newInst.inst = Instructions::Jump;
else if (i == "if") newInst.inst = Instructions::If;
else if (i == "add") newInst.inst = Instructions::Add;
@@ -2264,7 +2407,7 @@ vector<Instruction> parser(vector<vector<string>> in) {
else if (type == "int") newType.type = Types::Int;
else if (type == "bool") newType.type = Types::Bool;
else if (type == "list") newType.type = Types::List;
else error("Ground could not find type. This is an error with the interpreter, not your code. This line of code should never be reached.");
else error("Ground could not find type. This is an error with the interpreter, not your code. This line of code should never be reached.", "undefinedError");
newInst.args.push_back(newType);
}
break;
@@ -2297,7 +2440,7 @@ vector<Instruction> parser(vector<vector<string>> in) {
case Types::String:
{
Literal newLiteral;
string str = i.substr(1, i.size() - 2);
string str = interpretEscapeSequences(i.substr(1, i.size() - 2));
newLiteral.val = str;
newInst.args.push_back(newLiteral);
}
@@ -2335,7 +2478,7 @@ vector<Instruction> parser(vector<vector<string>> in) {
}
break;
default:
error("This type should not be obtained in normal execution");
error("This type should not be obtained in normal execution", "undefinedError");
break;
}
}

1
tests/error.grnd Normal file
View File

@@ -0,0 +1 @@
error "Hello, world!" "sillyError"

View File

@@ -14,7 +14,7 @@ fun -bool !jumpy
return true
endfun
call !jumpy &tmp
!jumpy &tmp
stdlnout "I called a function"

View File

@@ -1,5 +1,6 @@
# A cool list
setlist *favWords "hello" "there" "general" "kenobi"
stdlnout *favWords
set &count 0
set &passedThrough true

View File

@@ -1,5 +1,6 @@
set &var 0
@jump
add 1 $var &var
stdlnout $var
greater 1000 $var &cond
if $cond %2
greater 10000 $var &cond
if $cond %jump