Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable type conversions #63

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/compiler/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { methodInvocationTest } from "./tests/methodInvocation.test";
import { importTest } from "./tests/import.test";
import { arrayTest } from "./tests/array.test";
import { classTest } from "./tests/class.test";
import { assignmentExpressionTest } from './tests/assignmentExpression.test'

describe("compiler tests", () => {
printlnTest();
Expand All @@ -22,4 +23,5 @@ describe("compiler tests", () => {
importTest();
arrayTest();
classTest();
assignmentExpressionTest();
})
52 changes: 52 additions & 0 deletions src/compiler/__tests__/tests/arithmeticExpression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,58 @@ const testCases: testCase[] = [
expectedLines: ["-2147483648", "-32769", "-32768",
"-129", "-128", "-1", "0", "1", "127", "128", "32767", "32768", "2147483647"],
},
{
comment: "Mixed int and float addition (order swapped)",
program: `
public class Main {
public static void main(String[] args) {
int a = 5;
float b = 2.5f;
System.out.println(a + b);
}
}
`,
expectedLines: ["7.5"],
},
{
comment: "Mixed long and double multiplication",
program: `
public class Main {
public static void main(String[] args) {
double a = 3.5;
long b = 10L;
System.out.println(a * b);
}
}
`,
expectedLines: ["35.0"],
},
{
comment: "Mixed long and double multiplication (order swapped)",
program: `
public class Main {
public static void main(String[] args) {
long a = 10L;
double b = 3.5;
System.out.println(a * b);
}
}
`,
expectedLines: ["35.0"],
},
{
comment: "Mixed int and double division",
program: `
public class Main {
public static void main(String[] args) {
double a = 2.0;
int b = 5;
System.out.println(a / b);
}
}
`,
expectedLines: ["0.4"],
}
];

export const arithmeticExpressionTest = () => describe("arithmetic expression", () => {
Expand Down
124 changes: 124 additions & 0 deletions src/compiler/__tests__/tests/assignmentExpression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import {
runTest,
testCase,
} from "../__utils__/test-utils";

const testCases: testCase[] = [
{
comment: "int to double assignment",
program: `
public class Main {
public static void main(String[] args) {
int x = 5;
double y = x;
System.out.println(y);
}
}
`,
expectedLines: ["5.0"],
},
{
comment: "int to double conversion",
program: `
public class Main {
public static void main(String[] args) {
int x = 5;
double y;
y = x;
System.out.println(y);
}
}
`,
expectedLines: ["5.0"],
},
{
comment: "int to double conversion, array",
program: `
public class Main {
public static void main(String[] args) {
int x = 6;
double[] y = {1.0, 2.0, 3.0, 4.0, 5.0};
y[1] = x;
System.out.println(y[1]);
}
}
`,
expectedLines: ["6.0"],
},
{
comment: "int to long",
program: `
public class Main {
public static void main(String[] args) {
int a = 123;
long b = a;
System.out.println(b);
}
}
`,
expectedLines: ["123"],
},
{
comment: "int to float",
program: `
public class Main {
public static void main(String[] args) {
int a = 123;
float b = a;
System.out.println(b);
}
}
`,
expectedLines: ["123.0"],
},

// long -> other types
{
comment: "long to float",
program: `
public class Main {
public static void main(String[] args) {
long a = 9223372036854775807L;
float b = a;
System.out.println(b);
}
}
`,
expectedLines: ["9.223372E18"],
},
{
comment: "long to double",
program: `
public class Main {
public static void main(String[] args) {
long a = 9223372036854775807L;
double b = a;
System.out.println(b);
}
}
`,
expectedLines: ["9.223372036854776E18"],
},

// float -> other types
{
comment: "float to double",
program: `
public class Main {
public static void main(String[] args) {
float a = 3.0f;
double b = a;
System.out.println(b);
}
}
`,
expectedLines: ["3.0"],
},
];

export const assignmentExpressionTest = () => describe("assignment expression", () => {
for (let testCase of testCases) {
const { comment: comment, program: program, expectedLines: expectedLines } = testCase;
it(comment, () => runTest(program, expectedLines));
}
});
16 changes: 16 additions & 0 deletions src/compiler/__tests__/tests/println.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ const testCases: testCase[] = [
`,
expectedLines: ["true", "false"],
},
{
comment: "println with concatenated arguments",
program: `
public class Main {
public static void main(String[] args) {
System.out.println("Hello" + " " + "world!");
System.out.println("This is an int: " + 123);
System.out.println("This is a float: " + 4.5f);
System.out.println("This is a long: " + 10000000000L);
System.out.println("This is a double: " + 10.3);
}
}
`,
expectedLines: ["Hello world!", "This is an int: 123", "This is a float: 4.5",
"This is a long: 10000000000", "This is a double: 10.3"],
},
{
comment: "multiple println statements",
program: `
Expand Down
39 changes: 39 additions & 0 deletions src/compiler/__tests__/tests/unaryExpression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,45 @@ const testCases: testCase[] = [
}`,
expectedLines: ["10", "10", "-10", "-10", "-10", "-10", "10", "9", "-10"],
},
{
comment: "unary plus/minus for long",
program: `
public class Main {
public static void main(String[] args) {
long a = 9223372036854775807L;
System.out.println(+a);
System.out.println(-a);
}
}
`,
expectedLines: ["9223372036854775807", "-9223372036854775807"],
},
{
comment: "unary plus/minus for float",
program: `
public class Main {
public static void main(String[] args) {
float a = 4.5f;
System.out.println(+a);
System.out.println(-a);
}
}
`,
expectedLines: ["4.5", "-4.5"],
},
{
comment: "unary plus/minus for double",
program: `
public class Main {
public static void main(String[] args) {
double a = 10.75;
System.out.println(+a);
System.out.println(-a);
}
}
`,
expectedLines: ["10.75", "-10.75"],
},
{
comment: "bitwise complement",
program: `
Expand Down
Loading
Loading