Classes 12

This commit is contained in:
nazrin 2025-06-02 22:22:04 +00:00
parent 52a7b73a9e
commit d8ac625429
18 changed files with 417 additions and 186 deletions

View file

@ -1,5 +1,6 @@
#!/bin/env rdmd
import std.stdio;
import std.process;
import std.concurrency;
import std.conv;
@ -7,18 +8,20 @@ import std.string, std.format;
import std.algorithm, std.range;
void main(){
auto scheduler = new ThreadScheduler();
scheduler.spawn((){ "./test/closure.lox".run.output.match("1\n2\n"); });
scheduler.spawn((){ "./test/scope.lox".run.output.match("global first first second first ".replace(' ', '\n').repeat(2).join("\n")); });
scheduler.spawn((){ "./test/fib_for.lox".run.output.match(fib(6765)); });
scheduler.spawn((){ "./test/fib_recursive.lox".run.output.match(fib(34)); });
scheduler.spawn((){ "./test/fib_closure.lox".run.output.match(fib(34)); });
"./test/ops.lox".match("1\n2\n3\n4\n5\n6\n7\ntrue\nfalse\ntrue\ntrue\nhello, world\n");
"./test/shortcircuit.lox".match("true\nAAAA!\nAAAA!\nAAAA?\n");
"./test/closure.lox".match("1\n2\n");
"./test/scope.lox".match("global first first second first ".replace(' ', '\n').repeat(2).join("\n"));
"./test/fib_for.lox".match(fib(6765));
"./test/fib_recursive.lox".match(fib(34));
"./test/fib_closure.lox".match(fib(34));
"./test/class.lox".match("The German chocolate cake is delicious!\n");
scheduler.spawn((){ "./test/err/already_defined.lox".shouldFail(RetVal.other, "Already a variable with this name"); });
scheduler.spawn((){ "./test/err/undefined_var.lox".shouldFail(RetVal.runtime, "Undefined variable"); });
scheduler.spawn((){ "./test/err/self_ref_vardecl.lox".shouldFail(RetVal.runtime, "Undefined variable"); });
scheduler.spawn((){ "./test/err/invalid_syntax.lox".shouldFail(RetVal.other); });
scheduler.spawn((){ "./test/err/global_scope_return.lox".shouldFail(RetVal.other, "Can't return from top-level code"); });
"./test/err/already_defined.lox".shouldFail(RetVal.other, "Already a variable with this name");
"./test/err/undefined_var.lox".shouldFail(RetVal.runtime, "Undefined variable");
"./test/err/self_ref_vardecl.lox".shouldFail(RetVal.runtime, "Undefined variable");
"./test/err/invalid_syntax.lox".shouldFail(RetVal.other);
"./test/err/global_scope_return.lox".shouldFail(RetVal.other, "Can't return from top-level code");
}
enum RetVal{
@ -37,8 +40,9 @@ string fib(uint n){
return r;
}
auto run(string file) => [ "./lox", file ].execute;
void match(string res, string correct){
assert(res == correct, "Match failed\n-- Got --\n%s\n-- Expected --\n%s".format(res, correct));
void match(string file, string correct){
auto res = file.run.output;
assert(res == correct, "Match %s failed\n-- Got --\n%s\n-- Expected --\n%s".format(file, res, correct));
}
void shouldFail(string file, int code = 1, string msg = null){
auto c = file.run;

21
test/class.lox Normal file
View file

@ -0,0 +1,21 @@
class Cake{
init(goodness){
this.adjective = this.rate(goodness);
}
rate(goodness){
if(goodness)
return "delicious";
else
return "horrendous";
}
taste(){
print "The " + this.flavor + " cake is " + this.adjective + "!";
}
}
var cake = Cake(true);
cake.flavor = "German chocolate";
var t = cake.taste;
t();

17
test/ops.lox Normal file
View file

@ -0,0 +1,17 @@
print 2 - 1;
print 1 + 1;
print 6 / 2;
print 2 * 2;
print 2 * 3 - 1;
print 1 + 2 + 3;
print 1 + 2 * 3;
print 2 <= 3 or false;
print 1 > 2;
print 1 == 1;
print 1 != 2;
print "hello, " + "world";

12
test/shortcircuit.lox Normal file
View file

@ -0,0 +1,12 @@
fun scream(extra){
extra = extra or "!";
print "AAAA" + extra;
}
print nil or true;
scream(false);
scream(nil);
scream("?");

View file

@ -1 +1,2 @@