Skip to content

Commit

Permalink
[main] Java.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Oct 31, 2023
1 parent f263ab2 commit 695cb02
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 52 deletions.
36 changes: 29 additions & 7 deletions GenJava.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,21 @@ public class GenJava : GenTyped
WriteChild(statement.Body);
}

internal override void VisitReturn!(FuReturn statement)
{
if (statement.Value != null && this.CurrentMethod.Id == FuId.Main) {
if (!statement.Value.IsLiteralZero()) {
EnsureChildBlock();
Write("System.exit(");
statement.Value.Accept(this, FuPriority.Argument);
WriteLine(");");
}
WriteLine("return;");
}
else
base.VisitReturn(statement);
}

protected override void WriteSwitchValue!(FuExpr expr)
{
if (IsUnsignedByteIndexing(expr)) {
Expand Down Expand Up @@ -1249,14 +1264,21 @@ public class GenJava : GenTyped
default:
assert false;
}
WriteTypeAndName(method);
if (method.Id == FuId.Main)
Write("void main");
else
WriteTypeAndName(method);
WriteChar('(');
FuVar param = method.Parameters.FirstParameter();
for (int i = 0; i < paramCount; i++) {
if (i > 0)
Write(", ");
WriteTypeAndName(param);
param = param.NextParameter();
if (method.Id == FuId.Main && paramCount == 0)
Write("String[] args");
else {
FuVar param = method.Parameters.FirstParameter();
for (int i = 0; i < paramCount; i++) {
if (i > 0)
Write(", ");
WriteTypeAndName(param);
param = param.NextParameter();
}
}
WriteChar(')');
if (method.Throws)
Expand Down
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ test/bin/%/cs.txt: test/bin/%/cs.dll test/cs.runtimeconfig.json
test/bin/%/d.txt: test/bin/%/d.exe
$(DO)./$< >$@ || grep '//FAIL:.*\<d\>' test/$*.fu

test/bin/%/java.txt: test/bin/%/Test.class test/bin/Runner.class
$(DO)java -cp "test/bin$(JAVACPSEP)$(<D)" Runner >$@ || grep '//FAIL:.*\<java\>' test/$*.fu
test/bin/%/java.txt: test/bin/%/Test.class
$(DO)java -cp $(<D) Runner >$@ || grep '//FAIL:.*\<java\>' test/$*.fu

test/bin/%/js.txt: test/bin/%/Test.js
$(DO)node $< >$@ || grep '//FAIL:.*\<js\>' test/$*.fu
Expand Down Expand Up @@ -206,7 +206,7 @@ test/bin/%/Test.cs: test/%.fu test/Runner.fu fut
test/bin/%/Test.d: test/%.fu test/Runner.fu fut
$(DO_FUT)

test/bin/%/Test.java: test/%.fu fut
test/bin/%/Test.java: test/%.fu test/Runner.fu fut
$(DO_FUT)

test/bin/%/Test.js: test/%.fu test/Runner.fu fut
Expand All @@ -224,14 +224,11 @@ test/bin/%/Test.swift: test/%.fu test/Runner.fu fut
test/bin/%/Test.cl: test/%.fu fut
$(DO_FUT)

test/bin/Resource/java.txt: test/bin/Resource/Test.class test/bin/Runner.class
$(DO)java -cp "test/bin$(JAVACPSEP)$(<D)$(JAVACPSEP)test" Runner >$@
test/bin/Resource/java.txt: test/bin/Resource/Test.class
$(DO)java -cp "$(<D)$(JAVACPSEP)test" Runner >$@

.PRECIOUS: test/bin/%/Test.c test/bin/%/Test.cpp test/bin/%/Test.cs test/bin/%/Test.d test/bin/%/Test.java test/bin/%/Test.js test/bin/%/Test.ts test/bin/%/Test.d.ts test/bin/%/Test.py test/bin/%/Test.swift test/bin/%/Test.cl

test/bin/Runner.class: test/Runner.java test/bin/Basic/Test.class
$(DO)javac -d $(@D) -cp test/bin/Basic $<

test/node_modules: test/package.json
cd $(<D) && npm i --no-package-lock

Expand Down
36 changes: 29 additions & 7 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17655,6 +17655,21 @@ void GenJava::visitLock(const FuLock * statement)
writeChild(statement->body.get());
}

void GenJava::visitReturn(const FuReturn * statement)
{
if (statement->value != nullptr && this->currentMethod->id == FuId::main) {
if (!statement->value->isLiteralZero()) {
ensureChildBlock();
write("System.exit(");
statement->value->accept(this, FuPriority::argument);
writeLine(");");
}
writeLine("return;");
}
else
GenBase::visitReturn(statement);
}

void GenJava::writeSwitchValue(const FuExpr * expr)
{
if (isUnsignedByteIndexing(expr)) {
Expand Down Expand Up @@ -17812,14 +17827,21 @@ void GenJava::writeSignature(const FuMethod * method, int paramCount)
default:
std::abort();
}
writeTypeAndName(method);
if (method->id == FuId::main)
write("void main");
else
writeTypeAndName(method);
writeChar('(');
const FuVar * param = method->parameters.firstParameter();
for (int i = 0; i < paramCount; i++) {
if (i > 0)
write(", ");
writeTypeAndName(param);
param = param->nextParameter();
if (method->id == FuId::main && paramCount == 0)
write("String[] args");
else {
const FuVar * param = method->parameters.firstParameter();
for (int i = 0; i < paramCount; i++) {
if (i > 0)
write(", ");
writeTypeAndName(param);
param = param->nextParameter();
}
}
writeChar(')');
if (method->throws)
Expand Down
36 changes: 29 additions & 7 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18290,6 +18290,21 @@ internal override void VisitLock(FuLock statement)
WriteChild(statement.Body);
}

internal override void VisitReturn(FuReturn statement)
{
if (statement.Value != null && this.CurrentMethod.Id == FuId.Main) {
if (!statement.Value.IsLiteralZero()) {
EnsureChildBlock();
Write("System.exit(");
statement.Value.Accept(this, FuPriority.Argument);
WriteLine(");");
}
WriteLine("return;");
}
else
base.VisitReturn(statement);
}

protected override void WriteSwitchValue(FuExpr expr)
{
if (IsUnsignedByteIndexing(expr)) {
Expand Down Expand Up @@ -18444,14 +18459,21 @@ void WriteSignature(FuMethod method, int paramCount)
default:
throw new NotImplementedException();
}
WriteTypeAndName(method);
if (method.Id == FuId.Main)
Write("void main");
else
WriteTypeAndName(method);
WriteChar('(');
FuVar param = method.Parameters.FirstParameter();
for (int i = 0; i < paramCount; i++) {
if (i > 0)
Write(", ");
WriteTypeAndName(param);
param = param.NextParameter();
if (method.Id == FuId.Main && paramCount == 0)
Write("String[] args");
else {
FuVar param = method.Parameters.FirstParameter();
for (int i = 0; i < paramCount; i++) {
if (i > 0)
Write(", ");
WriteTypeAndName(param);
param = param.NextParameter();
}
}
WriteChar(')');
if (method.Throws)
Expand Down
1 change: 1 addition & 0 deletions libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,7 @@ class GenJava : public GenTyped
void visitLambdaExpr(const FuLambdaExpr * expr) override;
void visitForeach(const FuForeach * statement) override;
void visitLock(const FuLock * statement) override;
void visitReturn(const FuReturn * statement) override;
void visitSwitch(const FuSwitch * statement) override;
void visitThrow(const FuThrow * statement) override;
void visitEnumValue(const FuConst * konst, const FuConst * previous) override;
Expand Down
36 changes: 29 additions & 7 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -18812,6 +18812,21 @@ export class GenJava extends GenTyped
this.writeChild(statement.body);
}

visitReturn(statement)
{
if (statement.value != null && this.currentMethod.id == FuId.MAIN) {
if (!statement.value.isLiteralZero()) {
this.ensureChildBlock();
this.write("System.exit(");
statement.value.accept(this, FuPriority.ARGUMENT);
this.writeLine(");");
}
this.writeLine("return;");
}
else
super.visitReturn(statement);
}

writeSwitchValue(expr)
{
if (GenJava.#isUnsignedByteIndexing(expr)) {
Expand Down Expand Up @@ -18971,14 +18986,21 @@ export class GenJava extends GenTyped
default:
throw new Error();
}
this.writeTypeAndName(method);
if (method.id == FuId.MAIN)
this.write("void main");
else
this.writeTypeAndName(method);
this.writeChar(40);
let param = method.parameters.firstParameter();
for (let i = 0; i < paramCount; i++) {
if (i > 0)
this.write(", ");
this.writeTypeAndName(param);
param = param.nextParameter();
if (method.id == FuId.MAIN && paramCount == 0)
this.write("String[] args");
else {
let param = method.parameters.firstParameter();
for (let i = 0; i < paramCount; i++) {
if (i > 0)
this.write(", ");
this.writeTypeAndName(param);
param = param.nextParameter();
}
}
this.writeChar(41);
if (method.throws)
Expand Down
4 changes: 4 additions & 0 deletions test/Runner.fu
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class Runner
native {
System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
}
#elif JAVA
native {
java.util.Locale.setDefault(java.util.Locale.ROOT);
}
#endif
if (Test.Run()) {
Console.WriteLine("PASSED");
Expand Down
16 changes: 0 additions & 16 deletions test/Runner.java

This file was deleted.

0 comments on commit 695cb02

Please sign in to comment.