From 31cc30ee48cc87ae29cbfb38031731f4f16c1fda Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sun, 23 Nov 2025 16:32:54 +1100 Subject: [PATCH] Do some more stuff --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ src/parser.c | 11 +++++------ src/types.h | 4 ++-- tests/test.grnd | 10 +++++++++- 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a9b099b --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Ground, but written in C this time + +This repo houses the new Ground interpreter, which will replace the old C++ interpreter. + +Features of this interpreter: + +* Written in C instead of C++ +* Somewhat organised and readable codebase +* Not super buggy (yet) +* Uses standard, portable C* + +Now that Ground's features have mostly been finalised, this interpreter can be built with care to many features not initially planned, like functions and data structures + +*so far, only tested on Linux, but hopefully should work on other platforms as well + +Progress marker: + +- [x] Lexer +- [x] Parser + - [x] Labels + - [x] Instructions + - [x] Values + - [x] References +- [ ] Interpreter + - [ ] Labels + - [ ] Console I/O + - [ ] Control flow + - [ ] Data + - [ ] Variable creation + - [ ] Variable access + - [ ] Lists + - [ ] Creation + - [ ] Access + - [ ] String operations + - [ ] Maths + - [ ] Comparisions + - [ ] Type conversions + - [ ] Functions + - [ ] Custom data structures + - [ ] Working with external libraries diff --git a/src/parser.c b/src/parser.c index 3a79fc7..d408645 100644 --- a/src/parser.c +++ b/src/parser.c @@ -110,9 +110,8 @@ static GroundArg parseArgument(const char* token) { // Could be type reference or negative number if (strlen(token) > 1 && !isdigit(token[1])) { // Type reference (e.g., -int, -string) - return createRefGroundArg(LABEL, token + 1); // Using LABEL for type refs + return createRefGroundArg(TYPEREF, token + 1); } - // Fall through to number parsing } // Try to parse as number @@ -193,10 +192,10 @@ GroundProgram parseFile(const char* file) { // Check if first token is a label size_t tokenStart = 0; if (line.tokens[0].text[0] == '@') { - // TODO: Handle labels - you might want to store them separately - // For now, skip to next token - tokenStart = 1; - if (tokenStart >= line.count) continue; + GroundInstruction inst = createGroundInstruction(CREATELABEL); + addArgToInstruction(&inst, createRefGroundArg(LABEL, line.tokens[0].text + 1)); + addInstructionToProgram(&program, inst); + continue; } // First non-label token is the instruction diff --git a/src/types.h b/src/types.h index b98077d..97ba2ef 100644 --- a/src/types.h +++ b/src/types.h @@ -8,7 +8,7 @@ #include typedef enum GroundInstType { - IF, JUMP, END, INPUT, PRINT, PRINTLN, 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, STRUCT, ENDSTRUCT, INIT, USE, EXTERN + IF, JUMP, END, INPUT, PRINT, PRINTLN, 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, STRUCT, ENDSTRUCT, INIT, USE, EXTERN, CREATELABEL } GroundInstType; typedef enum GroundValueType { @@ -16,7 +16,7 @@ typedef enum GroundValueType { } GroundValueType; typedef enum GroundArgType { - VALUE, VALREF, DIRREF, LINEREF, LABEL, FNREF + VALUE, VALREF, DIRREF, LINEREF, LABEL, FNREF, TYPEREF } GroundArgType; struct GroundValue; diff --git a/tests/test.grnd b/tests/test.grnd index 170d049..8bdb53c 100644 --- a/tests/test.grnd +++ b/tests/test.grnd @@ -1 +1,9 @@ -println "dingus" +@begin +print "Do you like cheese? " +input &userIn +equal $userIn "yes" &cond +if $cond %end +println "That's sad" +jump %begin +@end +println "Yay! I do too"