forked from ground/ground
Compare commits
3 Commits
7443722dd5
...
a0a8cb69cc
| Author | SHA1 | Date | |
|---|---|---|---|
| a0a8cb69cc | |||
| 1acc20db82 | |||
| b6e2a594e9 |
@@ -15,8 +15,6 @@ int currentInstruction = 0;
|
||||
|
||||
bool isMainScopeGlobal = true;
|
||||
|
||||
char* getFileContents(const char* filename);
|
||||
|
||||
[[noreturn]] void customError(GroundArg type, GroundArg what, GroundInstruction* where, int whereLine, int exitCode) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
printGroundArg(&type);
|
||||
@@ -707,7 +705,7 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
|
||||
return gv;
|
||||
}
|
||||
if (ci != currentInstruction) {
|
||||
i = currentInstruction;
|
||||
i = ci;
|
||||
}
|
||||
currentInstruction++;
|
||||
}
|
||||
@@ -1898,35 +1896,38 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
snprintf(path, sizeof(path), "%s.grnd", libName);
|
||||
snprintf(path, sizeof(path), "./%s.grnd", libName);
|
||||
if (access(path, F_OK) == 0) found = 1;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
char errorMsg[1100];
|
||||
snprintf(errorMsg, sizeof(errorMsg), "Could not find library: %s", libName);
|
||||
snprintf(errorMsg, sizeof(errorMsg), "Could not find library: %s.grnd", libName);
|
||||
runtimeError(FIXME, errorMsg, in, currentInstruction);
|
||||
}
|
||||
|
||||
char* fileContents = getFileContents(path);
|
||||
|
||||
GroundProgram program = parseFile(fileContents);
|
||||
free(fileContents);
|
||||
|
||||
GroundScope newScope = {
|
||||
.variables = scope->variables,
|
||||
.labels = malloc(sizeof(GroundLabel*)),
|
||||
.isMainScope = false
|
||||
};
|
||||
|
||||
*newScope.labels = NULL;
|
||||
|
||||
int ci = currentInstruction;
|
||||
|
||||
interpretGroundProgram(&program, &newScope);
|
||||
|
||||
currentInstruction = ci;
|
||||
|
||||
FILE* f = fopen(path, "r");
|
||||
if (!f) {
|
||||
runtimeError(FIXME, "Failed to open file", in, currentInstruction);
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long fsize = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
char* content = malloc(fsize + 1);
|
||||
if (!content) {
|
||||
fclose(f);
|
||||
runtimeError(FIXME, "Failed to allocate memory for file content", in, currentInstruction);
|
||||
}
|
||||
fread(content, 1, fsize, f);
|
||||
fclose(f);
|
||||
content[fsize] = 0;
|
||||
|
||||
GroundProgram program = parseFile(content);
|
||||
free(content);
|
||||
|
||||
interpretGroundProgram(&program, scope);
|
||||
freeGroundProgram(&program);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,27 @@
|
||||
set &x 5
|
||||
|
||||
PAUSE
|
||||
|
||||
fun !dingle -function -int &a
|
||||
|
||||
PAUSE
|
||||
|
||||
fun !capture -int -int &b
|
||||
|
||||
PAUSE
|
||||
|
||||
add $a $b &tmp
|
||||
add $tmp $x &tmp
|
||||
return $tmp
|
||||
endfun
|
||||
|
||||
return $capture
|
||||
|
||||
endfun
|
||||
|
||||
set &x 10
|
||||
call !dingle 3 &result
|
||||
|
||||
PAUSE
|
||||
|
||||
call !result 5 &y
|
||||
println $y $x
|
||||
println $y
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
set &x "dingus"
|
||||
println $x
|
||||
PAUSE
|
||||
drop &x
|
||||
println $x
|
||||
PAUSE
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
fun !lib_PrintHello -int
|
||||
println "Hello"
|
||||
println "Hello, world!"
|
||||
return 0
|
||||
endfun
|
||||
endfun
|
||||
|
||||
println "Hello!"
|
||||
println "Hello!"
|
||||
@@ -1,6 +1,5 @@
|
||||
# A cool list
|
||||
setlist &favWords "hello" "there" "general"
|
||||
listappend &favWords "kenobi"
|
||||
setlist &favWords "hello" "there" "general" "kenobi"
|
||||
println $favWords
|
||||
|
||||
set &count 0
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
[hello, there, general, hello]
|
||||
hello
|
||||
there
|
||||
general
|
||||
,WV
|
||||
11
tests/pause.grnd
Normal file
11
tests/pause.grnd
Normal file
@@ -0,0 +1,11 @@
|
||||
fun !dingle -int
|
||||
|
||||
endfun
|
||||
|
||||
set &x 5
|
||||
set &y "dingle"
|
||||
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
|
||||
|
||||
# Main program
|
||||
println "Computing fib(20) recursively..."
|
||||
call !fib 20 $fib &answer
|
||||
println "Computing fib(30) recursively..."
|
||||
call !fib 30 $fib &answer
|
||||
println "Result:" $answer
|
||||
end
|
||||
|
||||
@@ -2,10 +2,10 @@ input &str
|
||||
getstrsize $str &size
|
||||
set &idx 0
|
||||
@loop
|
||||
getstrcharat $str $idx &char
|
||||
println $char
|
||||
add 1 $idx &idx
|
||||
equal $idx $size &cond
|
||||
if $cond %loopend
|
||||
jump %loop
|
||||
getstrcharat $str $idx &char
|
||||
println $char
|
||||
add 1 $idx &idx
|
||||
equal $idx $size &cond
|
||||
if $cond %loopend
|
||||
jump %loop
|
||||
@loopend
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
#!/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" ]];
|
||||
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
|
||||
printf "\033[32mAll tests passed!\n\033[0m"
|
||||
exit 0
|
||||
Reference in New Issue
Block a user