Skip to content

Commit 0bdc968

Browse files
committed
week10 materjalid
1 parent 041b7e6 commit 0bdc968

7 files changed

Lines changed: 415 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ gen
1919
graphs
2020
# week9
2121
cmas
22+
# week10
23+
aktk-out

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ dependencies {
3434
implementation 'org.jeasy:easy-rules-core:4.1.0'
3535
implementation 'org.jeasy:easy-rules-mvel:4.1.0'
3636
implementation 'org.mvel:mvel2:2.5.2.Final'
37+
// week10
38+
implementation 'org.ow2.asm:asm:9.9.1'
3739

3840
// initial
3941
testImplementation 'junit:junit:4.13.2'

inputs/yks_pluss_yks.aktk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(1+1)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package week10;
2+
3+
import week7.AktkAst;
4+
import week7.ast.*;
5+
import week9.AktkBinding;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
12+
13+
public class AktkCompiler {
14+
15+
static void main(String[] args) throws IOException {
16+
// lihtsam viis "käsurea parameetrite andmiseks":
17+
//args = new String[] {"inputs/yks_pluss_yks.aktk"};
18+
19+
if (args.length != 1) {
20+
throw new IllegalArgumentException("Sellele programmile tuleb anda parameetriks kompileeritava AKTK faili nimi");
21+
}
22+
23+
Path sourceFile = Paths.get(args[0]);
24+
if (!Files.isRegularFile(sourceFile)) {
25+
throw new IllegalArgumentException("Ei leia faili nimega '" + sourceFile + "'");
26+
}
27+
28+
String className = sourceFile.getFileName().toString().replace(".aktk", "");
29+
Path classFile = sourceFile.toAbsolutePath().getParent().resolve(className + ".class");
30+
31+
createClassFile(sourceFile, className, classFile);
32+
}
33+
34+
private static void createClassFile(Path sourceFile, String className, Path classFile) throws IOException {
35+
// loen faili sisu muutujasse
36+
String source = Files.readString(sourceFile);
37+
38+
// parsin ja moodustan AST'i
39+
Statement ast = AktkAst.createAst(source);
40+
41+
// seon muutujad
42+
AktkBinding.bind(ast);
43+
44+
// kompileerin
45+
byte[] bytes = createClass(ast, className);
46+
Files.write(classFile, bytes);
47+
}
48+
49+
public static byte[] createClass(Statement statement, String className) {
50+
throw new UnsupportedOperationException();
51+
}
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package week10;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.UncheckedIOException;
7+
import java.nio.charset.StandardCharsets;
8+
9+
@SuppressWarnings("unused")
10+
public class AktkCompilerBuiltins {
11+
private static BufferedReader inputReader = null;
12+
13+
// sisend-väljund
14+
public static int print(int value) {
15+
System.out.println(value);
16+
return 0; // mugavuse jaoks tagastame alati int
17+
}
18+
19+
public static int readInt() {
20+
if (inputReader == null) {
21+
inputReader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
22+
}
23+
24+
try {
25+
return Integer.parseInt(inputReader.readLine());
26+
}
27+
catch (IOException e) {
28+
throw new UncheckedIOException(e);
29+
}
30+
}
31+
32+
// loogikatehted
33+
public static int not(int x) {
34+
return ~x;
35+
}
36+
37+
public static int and(int a, int b) {
38+
return a & b;
39+
}
40+
41+
public static int or(int a, int b) {
42+
return a | b;
43+
}
44+
45+
// täisarvude tehted
46+
/**
47+
* Astendamine
48+
*/
49+
public static int power(int x, int n) {
50+
if (n < 0) {
51+
throw new IllegalArgumentException("Ainult mittenegatiivne astendaja on lubatud");
52+
}
53+
54+
int result = 1;
55+
while (n > 0) {
56+
result *= x;
57+
n--;
58+
}
59+
return result;
60+
}
61+
62+
/**
63+
* Suurim ühistegur
64+
*/
65+
public static int gcd(int a, int b) {
66+
while (b > 0) {
67+
int c = a % b;
68+
a = b;
69+
b = c;
70+
}
71+
return a;
72+
}
73+
}

src/main/java/week10/AsmDemo.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package week10;
2+
3+
import org.objectweb.asm.ClassWriter;
4+
import org.objectweb.asm.MethodVisitor;
5+
6+
import java.io.FileOutputStream;
7+
8+
import static org.objectweb.asm.Opcodes.*;
9+
10+
public class AsmDemo {
11+
private static final String className = "Kala";
12+
13+
static void main() throws Exception {
14+
try (FileOutputStream out = new FileOutputStream(className + ".class")) {
15+
out.write(generateClassBytes());
16+
}
17+
}
18+
19+
private static byte[] generateClassBytes() {
20+
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
21+
22+
// Klassi attribuudid
23+
cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null);
24+
cw.visitSource(null, null);
25+
26+
// main meetod
27+
MethodVisitor mv = cw.visitMethod(
28+
ACC_PUBLIC + ACC_STATIC, // modifikaatorid
29+
"main", // meetodi nimi
30+
"([Ljava/lang/String;)V", // meetodi kirjeldaja
31+
null, // geneerikute info
32+
new String[] { "java/io/IOException" });
33+
mv.visitCode();
34+
35+
generateMethodBody(mv);
36+
37+
mv.visitMaxs(0, 0);
38+
mv.visitEnd();
39+
40+
41+
// klassi lõpetamine
42+
cw.visitEnd();
43+
44+
// klassi baidijada genereerimine
45+
return cw.toByteArray();
46+
47+
}
48+
49+
private static void generateMethodBody(MethodVisitor mv) {
50+
mv.visitFieldInsn(GETSTATIC,
51+
"java/lang/System",
52+
"out",
53+
"Ljava/io/PrintStream;");
54+
mv.visitLdcInsn("hello");
55+
mv.visitMethodInsn(INVOKEVIRTUAL,
56+
"java/io/PrintStream",
57+
"println",
58+
"(Ljava/lang/String;)V", false);
59+
mv.visitInsn(RETURN);
60+
}
61+
}

0 commit comments

Comments
 (0)