forked from ground/ground
Merge pull request 'Unit tests' (#26) from DiamondNether90/cground:master into master
Reviewed-on: ground/ground#26
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ build
|
|||||||
ground
|
ground
|
||||||
groundc
|
groundc
|
||||||
.idea
|
.idea
|
||||||
|
Makefile
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -3,6 +3,7 @@
|
|||||||
#include "compiler.h"
|
#include "compiler.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "serialize.h"
|
#include "serialize.h"
|
||||||
|
#include "repl.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -43,9 +44,8 @@ char* getFileContents(const char* filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc < 2) {
|
if (argc == 1) {
|
||||||
printf("Usage: %s <file> [-c] [--compile] [-h] [--help]\n", argv[0]);
|
exit(repl());
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool compile = false;
|
bool compile = false;
|
||||||
@@ -71,7 +71,7 @@ int main(int argc, char** argv) {
|
|||||||
printf(" -w <output> or --writebytecode <output>: Outputs binary Ground bytecode");
|
printf(" -w <output> or --writebytecode <output>: Outputs binary Ground bytecode");
|
||||||
printf(" -b <output> or --bytecode <output>: Inputs binary Ground bytecode");
|
printf(" -b <output> or --bytecode <output>: Inputs binary Ground bytecode");
|
||||||
exit(0);
|
exit(0);
|
||||||
} else if (strcmp("--writebytecode", argv[i]) == 0 || strcmp("-w", argv[i]) == 0) {
|
} else if (strcmp("--writeBytecode", argv[i]) == 0 || strcmp("-w", argv[i]) == 0) {
|
||||||
if (compile) {
|
if (compile) {
|
||||||
printf("Cannot choose both bytecode and compilation");
|
printf("Cannot choose both bytecode and compilation");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
@@ -176,6 +176,11 @@ static GroundInstType getInstructionType(const char* inst) {
|
|||||||
if (strcmp(inst, "not") == 0) return NOT;
|
if (strcmp(inst, "not") == 0) return NOT;
|
||||||
if (strcmp(inst, "greater") == 0) return GREATER;
|
if (strcmp(inst, "greater") == 0) return GREATER;
|
||||||
if (strcmp(inst, "lesser") == 0) return LESSER;
|
if (strcmp(inst, "lesser") == 0) return LESSER;
|
||||||
|
if (strcmp(inst, "and") == 0) return AND;
|
||||||
|
if (strcmp(inst, "or") == 0) return OR;
|
||||||
|
if (strcmp(inst, "xor") == 0) return XOR;
|
||||||
|
if (strcmp(inst, "neg") == 0) return NEG;
|
||||||
|
if (strcmp(inst, "shift") == 0) return SHIFT;
|
||||||
if (strcmp(inst, "stoi") == 0) return STOI;
|
if (strcmp(inst, "stoi") == 0) return STOI;
|
||||||
if (strcmp(inst, "stod") == 0) return STOD;
|
if (strcmp(inst, "stod") == 0) return STOD;
|
||||||
if (strcmp(inst, "ctoi") == 0) return CTOI;
|
if (strcmp(inst, "ctoi") == 0) return CTOI;
|
||||||
@@ -194,6 +199,7 @@ static GroundInstType getInstructionType(const char* inst) {
|
|||||||
if (strcmp(inst, "use") == 0) return USE;
|
if (strcmp(inst, "use") == 0) return USE;
|
||||||
if (strcmp(inst, "extern") == 0) return EXTERN;
|
if (strcmp(inst, "extern") == 0) return EXTERN;
|
||||||
if (strcmp(inst, "drop") == 0) return DROP;
|
if (strcmp(inst, "drop") == 0) return DROP;
|
||||||
|
if (strcmp(inst, "license") == 0) return LICENSE;
|
||||||
if (strcmp(inst, "PAUSE") == 0) return PAUSE;
|
if (strcmp(inst, "PAUSE") == 0) return PAUSE;
|
||||||
|
|
||||||
fprintf(stderr, "Error: Unknown instruction: %s\n", inst);
|
fprintf(stderr, "Error: Unknown instruction: %s\n", inst);
|
||||||
|
|||||||
60
src/repl.c
Normal file
60
src/repl.c
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Ground REPL (shows when ground is ran without any arguments)
|
||||||
|
* Copyright (C) 2026 DiamondNether90
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "interpreter.h"
|
||||||
|
#include "include/estr.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
int repl() {
|
||||||
|
printf("Ground REPL\n");
|
||||||
|
printf("Copyright (C) DiamondNether90 2026\n");
|
||||||
|
printf("Distributed under the GNU GPL-3.0 license; type \"license\" for more info\n");
|
||||||
|
char input[BUFFER_SIZE];
|
||||||
|
|
||||||
|
GroundVariable* variables = NULL;
|
||||||
|
GroundLabel* labels = NULL;
|
||||||
|
|
||||||
|
GroundScope scope;
|
||||||
|
scope.variables = &variables;
|
||||||
|
scope.labels = &labels;
|
||||||
|
scope.isMainScope = true;
|
||||||
|
while (true) {
|
||||||
|
Estr programString = CREATE_ESTR("");
|
||||||
|
*scope.labels = NULL;
|
||||||
|
|
||||||
|
// Get program
|
||||||
|
printf(">>> ");
|
||||||
|
while (true) {
|
||||||
|
fgets(input, BUFFER_SIZE, stdin);
|
||||||
|
if (strcmp(input, "\n") == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
APPEND_ESTR(programString, input);
|
||||||
|
printf("...");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interpret program
|
||||||
|
GroundProgram program = createGroundProgram();
|
||||||
|
program = parseFile(programString.str);
|
||||||
|
interpretGroundProgram(&program, &scope);
|
||||||
|
freeGroundProgram(&program);
|
||||||
|
|
||||||
|
DESTROY_ESTR(programString);
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/repl.h
Normal file
1
src/repl.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
int repl();
|
||||||
25
src/types.c
25
src/types.c
@@ -486,6 +486,21 @@ void printGroundInstruction(GroundInstruction* gi) {
|
|||||||
case LESSER:
|
case LESSER:
|
||||||
printf("lesser");
|
printf("lesser");
|
||||||
break;
|
break;
|
||||||
|
case AND:
|
||||||
|
printf("and");
|
||||||
|
break;
|
||||||
|
case OR:
|
||||||
|
printf("or");
|
||||||
|
break;
|
||||||
|
case XOR:
|
||||||
|
printf("xor");
|
||||||
|
break;
|
||||||
|
case NEG:
|
||||||
|
printf("neg");
|
||||||
|
break;
|
||||||
|
case SHIFT:
|
||||||
|
printf("shift");
|
||||||
|
break;
|
||||||
case STOI:
|
case STOI:
|
||||||
printf("stoi");
|
printf("stoi");
|
||||||
break;
|
break;
|
||||||
@@ -545,6 +560,7 @@ void printGroundInstruction(GroundInstruction* gi) {
|
|||||||
}
|
}
|
||||||
if (gi->type != CREATELABEL) printf(" ");
|
if (gi->type != CREATELABEL) printf(" ");
|
||||||
for (size_t i = 0; i < gi->args.length; i++) {
|
for (size_t i = 0; i < gi->args.length; i++) {
|
||||||
|
if (i != 0) printf(" ");
|
||||||
if (gi->args.args[i].type == VALUE && gi->args.args[i].value.value.type == STRING) {
|
if (gi->args.args[i].type == VALUE && gi->args.args[i].value.value.type == STRING) {
|
||||||
printf("\"");
|
printf("\"");
|
||||||
printGroundArg(&gi->args.args[i]);
|
printGroundArg(&gi->args.args[i]);
|
||||||
@@ -552,7 +568,6 @@ void printGroundInstruction(GroundInstruction* gi) {
|
|||||||
} else {
|
} else {
|
||||||
printGroundArg(&gi->args.args[i]);
|
printGroundArg(&gi->args.args[i]);
|
||||||
}
|
}
|
||||||
printf(" ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,12 +580,12 @@ List createList() {
|
|||||||
|
|
||||||
void appendToList(List* list, GroundValue value) {
|
void appendToList(List* list, GroundValue value) {
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/ground\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
GroundValue* ptr = realloc(list->values, (list->size + 1) * sizeof(GroundValue));
|
GroundValue* ptr = realloc(list->values, (list->size + 1) * sizeof(GroundValue));
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
printf("There was an error allocating memory for a list.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
printf("There was an error allocating memory for a list.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/ground\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
list->size++;
|
list->size++;
|
||||||
@@ -580,7 +595,7 @@ void appendToList(List* list, GroundValue value) {
|
|||||||
|
|
||||||
ListAccess getListAt(List* list, size_t idx) {
|
ListAccess getListAt(List* list, size_t idx) {
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/ground\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (idx < list->size) {
|
if (idx < list->size) {
|
||||||
@@ -598,7 +613,7 @@ ListAccess getListAt(List* list, size_t idx) {
|
|||||||
|
|
||||||
ListAccessStatus setListAt(List* list, size_t idx, GroundValue value) {
|
ListAccessStatus setListAt(List* list, size_t idx, GroundValue value) {
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/cground\n");
|
printf("Expecting a List ptr, got a null pointer instead.\nThis is likely not an error with your Ground program.\nPlease report this issue to https://chsp.au/ground/ground\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (idx < list->size) {
|
if (idx < list->size) {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ void wasm_print(const char* str);
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum GroundInstType {
|
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, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, ERRORCMD
|
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, AND, OR, XOR, NEG, SHIFT, STOI, STOD, ITOC, CTOI, TOSTRING, FUN, RETURN, ENDFUN, PUSHARG, CALL, STRUCT, ENDSTRUCT, INIT, GETFIELD, SETFIELD, USE, EXTERN, CREATELABEL, PAUSE, DROP, LICENSE, ERRORCMD
|
||||||
} GroundInstType;
|
} GroundInstType;
|
||||||
|
|
||||||
typedef enum GroundValueType {
|
typedef enum GroundValueType {
|
||||||
|
|||||||
@@ -1,27 +1,15 @@
|
|||||||
set &x 5
|
set &x 5
|
||||||
|
|
||||||
PAUSE
|
|
||||||
|
|
||||||
fun !dingle -function -int &a
|
fun !dingle -function -int &a
|
||||||
|
|
||||||
PAUSE
|
|
||||||
|
|
||||||
fun !capture -int -int &b
|
fun !capture -int -int &b
|
||||||
|
|
||||||
PAUSE
|
|
||||||
|
|
||||||
add $a $b &tmp
|
add $a $b &tmp
|
||||||
add $tmp $x &tmp
|
add $tmp $x &tmp
|
||||||
return $tmp
|
return $tmp
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
return $capture
|
return $capture
|
||||||
|
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
|
set &x 10
|
||||||
call !dingle 3 &result
|
call !dingle 3 &result
|
||||||
|
|
||||||
PAUSE
|
|
||||||
|
|
||||||
call !result 5 &y
|
call !result 5 &y
|
||||||
println $y
|
println $y $x
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
set &x "dingus"
|
set &x "dingus"
|
||||||
PAUSE
|
println $x
|
||||||
drop &x
|
drop &x
|
||||||
PAUSE
|
println $x
|
||||||
2
tests/end.grnd
Normal file
2
tests/end.grnd
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
set &x 10
|
||||||
|
end $x
|
||||||
4
tests/lib.grnd
Normal file
4
tests/lib.grnd
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
fun !lib_PrintHello -int
|
||||||
|
println "Hello"
|
||||||
|
return 0
|
||||||
|
endfun
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
# A cool list
|
# A cool list
|
||||||
setlist &favWords "hello" "there" "general" "kenobi"
|
setlist &favWords "hello" "there" "general"
|
||||||
|
listappend &favWords "kenobi"
|
||||||
println $favWords
|
println $favWords
|
||||||
|
|
||||||
set &count 0
|
set &count 0
|
||||||
|
|||||||
@@ -1,11 +1,2 @@
|
|||||||
fun !dingle -int
|
|
||||||
|
|
||||||
endfun
|
|
||||||
|
|
||||||
set &x 5
|
set &x 5
|
||||||
set &y "dingle"
|
|
||||||
PAUSE
|
PAUSE
|
||||||
println "continuing"
|
|
||||||
println "step through here"
|
|
||||||
println "step again"
|
|
||||||
println "and again"
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ fun !fib -int -int &n -function &fib
|
|||||||
endfun
|
endfun
|
||||||
|
|
||||||
# Main program
|
# Main program
|
||||||
println "Computing fib(30) recursively..."
|
println "Computing fib(20) recursively..."
|
||||||
call !fib 30 $fib &answer
|
call !fib 20 $fib &answer
|
||||||
println "Result:" $answer
|
println "Result:" $answer
|
||||||
end
|
end
|
||||||
|
|||||||
82
tests/unit.sh
Executable file
82
tests/unit.sh
Executable file
@@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
echo "" > log.txt
|
||||||
|
for f in *.grnd; do
|
||||||
|
[ -e "$f" ] || continue # skip if no files match
|
||||||
|
# Files to skip over
|
||||||
|
if [[ "$f" == "lib.grnd" ]] ||
|
||||||
|
[[ "$f" == "string.grnd" ]] ||
|
||||||
|
[[ "$f" == "test.grnd" ]] ||
|
||||||
|
[[ "$f" == "to1000.grnd" ]] ||
|
||||||
|
[[ "$f" == "uhoh.grnd" ]] ||
|
||||||
|
[[ "$f" == "pause.grnd" ]];
|
||||||
|
then continue
|
||||||
|
fi
|
||||||
|
echo "Running $f"
|
||||||
|
ground "$f" > log.txt
|
||||||
|
|
||||||
|
FILE="log.txt"
|
||||||
|
FAILED="\033[31mFailed\n\033[0m"
|
||||||
|
if [[ "$f" == "closure.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "13 10\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "convs.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "32\n12\n3.140000\na\n97\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "drop.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "dingus\nGround runtime error:\n ErrorType: UnknownVariable\n ErrorInstruction: println \$x\n ErrorLine: 4\n"));
|
||||||
|
then printf "\033[31mFailed\n\033[0m"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "error.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "Ground runtime error:\n ErrorType: Hello\n ErrorContext: [1, 2, 3, Hi!]\n ErrorInstruction: error \"Hello\" [1, 2, 3, Hi!] 1\n ErrorLine: 2\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "fib.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "Fibonacci result: 7540113804746346429\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "function.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "dingle\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "list.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "[hello, there, general, kenobi]\nhello\nthere\ngeneral\nkenobi\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "recursivefib.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "Computing fib(20) recursively...\nResult: 6765\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "simple.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "dingus\ndinglefart\n5.840000\n464773025\n5164.120000\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "struct.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "53\n32\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
elif [[ "$f" == "use.grnd" ]]; then
|
||||||
|
if !(cmp -s "$FILE" <(printf "Hello\n"));
|
||||||
|
then printf $FAILED
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
printf "\033[31mCould not find test case\n\033[0m"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
rm log.txt
|
||||||
|
printf "\033[32mAll tests passed!\n\033[0m"
|
||||||
|
exit 0
|
||||||
2
tests/use.grnd
Normal file
2
tests/use.grnd
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
use "lib"
|
||||||
|
call !lib_PrintHello &tmp
|
||||||
Reference in New Issue
Block a user