Skip to content

Commit db2137f

Browse files
committed
week4 alusosa materjalid
1 parent 46c9425 commit db2137f

110 files changed

Lines changed: 3090 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package toylangs;
2+
3+
import java.io.IOException;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.lang.reflect.RecordComponent;
6+
import java.nio.file.Path;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public interface AbstractNode {
11+
12+
/**
13+
* Abimeetod klassile AbstractNodeVisualizer.
14+
* @return tipu klassinimi väiketähtedes ja ilma keelenimeta.
15+
*/
16+
default String getNodeLabel() {
17+
String className = this.getClass().getSimpleName();
18+
return className.replaceFirst("[A-Z][a-z]*", "").toLowerCase();
19+
}
20+
21+
/**
22+
* Abimeetod klassile AbstractNodeVisualizer.
23+
* @return järjend kõikidest välja kutsuva isendi alamtippudest
24+
*/
25+
default List<Object> getChildren() {
26+
Class<?> clazz = this.getClass();
27+
if (!clazz.isRecord())
28+
throw new IllegalArgumentException("getChildren() oskab käsitleda vaid kirjeid (Record).");
29+
30+
try {
31+
List<Object> children = new ArrayList<>();
32+
for (RecordComponent recordComponent : clazz.getRecordComponents()) {
33+
Object field = recordComponent.getAccessor().invoke(this);
34+
children.add(field);
35+
}
36+
return children;
37+
} catch (IllegalAccessException | InvocationTargetException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
42+
/**
43+
* @param path failitee, kuhu loodav pilt salvestada.
44+
*/
45+
default void renderPngFile(Path path) throws IOException {
46+
AbstractNodeVisualizer.renderPngFile(this, path);
47+
}
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package toylangs;
2+
3+
import guru.nidi.graphviz.attribute.Label;
4+
import guru.nidi.graphviz.engine.Format;
5+
import guru.nidi.graphviz.engine.Graphviz;
6+
import guru.nidi.graphviz.model.Graph;
7+
import guru.nidi.graphviz.model.Node;
8+
9+
import java.io.IOException;
10+
import java.nio.file.Path;
11+
import java.util.Arrays;
12+
import java.util.Collection;
13+
import java.util.Objects;
14+
import java.util.stream.Stream;
15+
16+
import static guru.nidi.graphviz.model.Factory.graph;
17+
import static guru.nidi.graphviz.model.Factory.node;
18+
19+
public class AbstractNodeVisualizer {
20+
21+
private int nextNodeId = 0;
22+
23+
private Node makeNode(String label) {
24+
return node(Integer.toString(nextNodeId++)).with(Label.of(label));
25+
}
26+
27+
private Node visit(Object object) {
28+
return switch (object) {
29+
case AbstractNode node -> visit(node);
30+
default -> makeNode(Objects.toString(object));
31+
};
32+
}
33+
34+
private Node visit(AbstractNode node) {
35+
return makeNode(node.getNodeLabel()).link(
36+
node.getChildren().stream()
37+
.flatMap(AbstractNodeVisualizer::flattenChild)
38+
.filter(Objects::nonNull)
39+
.map(this::visit)
40+
.toList()
41+
);
42+
}
43+
44+
private static Stream<?> flattenChild(Object child) {
45+
if (child instanceof Collection<?> collection)
46+
return collection.stream();
47+
else if (child != null && child.getClass().isArray())
48+
return Arrays.stream((Object[]) child);
49+
else
50+
return Stream.of(child);
51+
}
52+
53+
public static void renderPngFile(AbstractNode node, Path path) throws IOException {
54+
Node rootNode = new AbstractNodeVisualizer().visit(node);
55+
Graph graph = graph("tree").directed().with(rootNode).graphAttr().with("ordering", "out");
56+
//System.out.println(Graphviz.fromGraph(graph).render(Format.DOT));
57+
Graphviz.fromGraph(graph).scale(3).render(Format.PNG).toFile(path.toFile().getCanonicalFile());
58+
}
59+
}

src/main/java/toylangs/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Eksami näidiskeeled
2+
3+
## Jooksev harjutamine
4+
1. [Imp](imp/)
5+
2. [Bolog](bolog/)
6+
3. [Parm](parm/)
7+
4. [Hulk](hulk/)
8+
5. [Dialoog](dialoog/)
9+
6. [Pullet](pullet/)
10+
11+
## Iseseisev harjutamine
12+
1. [Sholog](sholog/)
13+
2. [Safdi](safdi/)
14+
3. [Modul](modul/)
15+
4. [Vhile](vhile/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package toylangs.bolog;
2+
3+
import toylangs.bolog.ast.BologNode;
4+
5+
import java.util.Set;
6+
7+
public class BologEvaluator {
8+
private final Set<String> trueVars;
9+
10+
public BologEvaluator(Set<String> trueVars) {
11+
this.trueVars = trueVars;
12+
}
13+
14+
public static boolean eval(BologNode node, Set<String> trueVars) {
15+
BologEvaluator bologEvaluator = new BologEvaluator(trueVars);
16+
return bologEvaluator.eval(node);
17+
}
18+
19+
private boolean eval(BologNode node) {
20+
throw new UnsupportedOperationException();
21+
}
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Beebiprolog ehk Bolog
2+
3+
[Keele kirjeldus](https://courses.cs.ut.ee/t/akt/Main/ToyLangsBolog).
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package toylangs.bolog.ast;
2+
3+
import java.util.List;
4+
5+
public record BologImp(BologNode conclusion, List<BologNode> assumptions) implements BologNode {
6+
7+
public BologImp(BologNode conclusion, BologNode... assumptions) {
8+
this(conclusion, List.of(assumptions));
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return "imp(" +
14+
"" + conclusion +
15+
", " + assumptions.toString().replaceAll("[\\[\\]{}]", "") +
16+
")";
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package toylangs.bolog.ast;
2+
3+
public record BologLit(boolean value) implements BologNode {
4+
@Override
5+
public String getNodeLabel() {
6+
return "tv";
7+
}
8+
9+
@Override
10+
public String toString() {
11+
return "tv(" +
12+
"" + value +
13+
")";
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package toylangs.bolog.ast;
2+
3+
public record BologNand(BologNode left, BologNode right) implements BologNode {
4+
@Override
5+
public String toString() {
6+
return "nand(" +
7+
"" + left +
8+
", " + right +
9+
")";
10+
}
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package toylangs.bolog.ast;
2+
3+
import toylangs.AbstractNode;
4+
5+
import java.util.List;
6+
7+
public sealed interface BologNode extends AbstractNode permits BologImp, BologLit, BologNand, BologVar {
8+
// Literaalid ja muutuja kasutus
9+
static BologLit tv(boolean value) { return new BologLit(value); }
10+
static BologVar var(String name) { return new BologVar(name); }
11+
12+
// Implikatsioon
13+
static BologImp imp(BologNode conclusion, List<BologNode> assumptions) {
14+
return new BologImp(conclusion, assumptions);
15+
}
16+
static BologImp imp(BologNode conclusion, BologNode... assumptions) {
17+
return new BologImp(conclusion, assumptions);
18+
}
19+
20+
// Nand
21+
static BologNand nand(BologNode left, BologNode right) {
22+
return new BologNand(left, right);
23+
}
24+
static BologNode not(BologNode expr) {
25+
return nand(tv(true), expr);
26+
}
27+
static BologNode and(BologNode left, BologNode right) {
28+
return not(nand(left, right));
29+
}
30+
static BologNode or(BologNode left, BologNode right) {
31+
return nand(not(left), not(right));
32+
}
33+
static BologNode xor(BologNode left, BologNode right) {
34+
return and(or(left,right), nand(left,right));
35+
}
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package toylangs.bolog.ast;
2+
3+
public record BologVar(String name) implements BologNode {
4+
@Override
5+
public String toString() {
6+
return "var(" +
7+
"\"" + name + "\"" +
8+
")";
9+
}
10+
}

0 commit comments

Comments
 (0)