diff --git a/.gitignore b/.gitignore index 227ac5b..949cae3 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ htmldocs/ # JAPL src/japl +src/compiler tests/runtests # MacOS diff --git a/src/stdlib.nim b/src/stdlib.nim index 531c9a3..ff00ff6 100644 --- a/src/stdlib.nim +++ b/src/stdlib.nim @@ -31,8 +31,12 @@ proc natPrint(args: seq[ptr Obj]): tuple[ok: bool, result: ptr Obj] = ## is passed, they will be printed separated ## by a space var res = "" - for arg in args: - res = res & arg.stringify() & " " + for i in countup(0, args.high()): + let arg = args[i] + if i < args.high(): + res = res & arg.stringify() & " " + else: + res = res & arg.stringify() echo res return (ok: true, result: asNil()) diff --git a/tests/japl/arithmetic.jpl b/tests/japl/arithmetic.jpl new file mode 100644 index 0000000..7923450 --- /dev/null +++ b/tests/japl/arithmetic.jpl @@ -0,0 +1,19 @@ +//int arithmetic + +print(7+5); //output:12 +print(-8); //output:-8 +print(5-8); //output:-3 +print(1+1+1+1+1); //output:5 +print(1-1+1-1+1-1); //output:0 +print(2*3+2);//output:8 +print(2+3*2);//output:8 +print(3+2*7);//output:17 +print(2-9*5);//output:-43 +print(2*9-5);//output:13 +print(2**5);//output:32 +print(3**3);//output:27 +print(3**3*2);//output:54 +print(8+2**4);//output:24 +print(2+7*2+4);//output:20 +print(1-2**2*5);//output:-19 +print(7*-2**3+4*7);//output:-28 diff --git a/tests/runtests.nim b/tests/runtests.nim index 29601e8..815da55 100644 --- a/tests/runtests.nim +++ b/tests/runtests.nim @@ -18,6 +18,21 @@ proc compileExpectedOutput(path: string): string = if line =~ re"^.*//output:(.*)$": result &= matches[0] & "\n" +proc deepComp(left, right: string): tuple[same: bool, place: int] = + result.same = true + if left.high() != right.high(): + result.same = false + for i in countup(0, left.high()): + result.place = i + if i > right.high(): + # already false bc of the len check at the beginning + # already correct place bc it's updated every i + return + if left[i] != right[i]: + result.same = false + return + + var testsDir = "tests" / "japl" var japlExec = "src" / "japl" @@ -62,19 +77,26 @@ for file in walkDir(testsDir): break singularTest log &"Running test {file.path}" + if fileExists("testoutput.txt"): + removeFile("testoutput.txt") # in case this crashed discard execShellCmd(&"{japlExec} {file.path} >>testoutput.txt") - let expectedOutput = compileExpectedOutput(file.path) + let expectedOutput = compileExpectedOutput(file.path).replace(re"(\n*)$", "") let realOutputFile = open("testoutput.txt", fmRead) - let realOutput = realOutputFile.readAll() + let realOutput = realOutputFile.readAll().replace(re"([\n\r]*)$", "") realOutputFile.close() removeFile("testoutput.txt") - if expectedOutput == realOutput: + let comparison = deepComp(expectedOutput, realOutput) + if comparison.same: log &"Successful test {file.path}" else: detail &"Expected output:\n{expectedOutput}\n" detail &"Received output:\n{realOutput}\n" + detail &"Mismatch at pos {comparison.place}" + if comparison.place > expectedOutput.high() or + comparison.place > realOutput.high(): + detail &"Length mismatch" + else: + detail &"Expected is '{expectedOutput[comparison.place]}' while received '{realOutput[comparison.place]}'" log &"Test failed {file.path}, check 'testresults.txt' for details" - - - +testResultsFile.close()