Skip to content

Commit

Permalink
Neki pa že dela zdj pr pizdi
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanja-S committed Apr 24, 2023
1 parent 727e45f commit 701381d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
"request": "launch",
"mainClass": "Main",
"projectName": "PINSCompiler_aaffa8a2",
"args": "PINS --exec TYP --dump TYP test.pns"
"args": "PINS --exec FRM --dump FRM test.pns"
},
{
"type": "java",
"name": "App",
"request": "launch",
"mainClass": "App",
"projectName": "PINSCompiler_aaffa8a2",
"args": "test ./ TestsRunner/tests/5-typ.tst -v"
"args": "test ./ TestsRunner/tests/6-frm-1.tst -v"
},
{
"type": "java",
Expand Down
Binary file modified src/compiler/.DS_Store
Binary file not shown.
89 changes: 74 additions & 15 deletions src/compiler/frm/FrameEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static common.RequireNonNull.requireNonNull;

import compiler.common.Visitor;
import compiler.frm.Access.Local;
import compiler.parser.ast.def.*;
import compiler.parser.ast.def.FunDef.Parameter;
import compiler.parser.ast.expr.*;
Expand All @@ -17,6 +18,10 @@
import compiler.seman.common.NodeDescription;
import compiler.seman.type.type.Type;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class FrameEvaluator implements Visitor {
/**
* Opis definicij funkcij in njihovih klicnih zapisov.
Expand Down Expand Up @@ -44,9 +49,14 @@ public class FrameEvaluator implements Visitor {
private int staticLevel;

/*
* Števec za stack offset
* Lista stackOffseta trenutnega staticLevel-a oz. funkcijskega klica
*/
private List<StackOffsets> stackOffset;

/*
* Lista funkcijskih klicev
*/
private int stackOffset;
private List<Integer> functionCalls;

public FrameEvaluator(
NodeDescription<Frame> frames,
Expand All @@ -58,19 +68,27 @@ public FrameEvaluator(
this.accesses = accesses;
this.definitions = definitions;
this.types = types;
staticLevel = 1;
stackOffset = 0;

staticLevel = 0;
stackOffset = new ArrayList<StackOffsets>();

functionCalls = new ArrayList<Integer>();
}

@Override
public void visit(Call call) {
if (staticLevel == 1) {
if (staticLevel == 0) {
accesses.store(new Access.Global(types.valueFor(definitions.valueFor(call).get()).get().sizeInBytes(),
frames.valueFor(definitions.valueFor(call).get()).get().label), call);
} else {
accesses.store(new Access.Local(types.valueFor(definitions.valueFor(call).get()).get().sizeInBytes(),
stackOffset, staticLevel), call);
stackOffset.get(staticLevel - 1).getTopLocVarOffset(), staticLevel), call);
}

int argSize = call.arguments.stream()
.map(argument -> types.valueFor(argument).get().sizeInBytesAsParam())
.reduce(0, Integer::sum);
functionCalls.set(staticLevel - 1, Math.max(functionCalls.get(staticLevel - 1).intValue(), argSize));
}

@Override
Expand Down Expand Up @@ -144,21 +162,30 @@ public void visit(Defs defs) {

@Override
public void visit(FunDef funDef) {
stackOffset.add(new StackOffsets());
functionCalls.add(Integer.valueOf(0));
compiler.frm.Frame.Builder Builder = null;
if (staticLevel == 1) {
Builder = new Frame.Builder(Frame.Label.named(funDef.name), staticLevel);
if (staticLevel == 0) {
Builder = new Frame.Builder(Frame.Label.named(funDef.name), ++staticLevel);
} else {
Builder = new Frame.Builder(Frame.Label.nextAnonymous(), staticLevel);
Builder = new Frame.Builder(Frame.Label.nextAnonymous(), ++staticLevel);
}

for (Parameter param : funDef.parameters) {
param.accept(this);
Builder.addParameter(types.valueFor(param).get().sizeInBytesAsParam());
}
staticLevel++;
funDef.body.accept(this);

for (Integer Int : stackOffset.get(staticLevel - 1).locVarStackOffset) {
Builder.addLocalVariable(-Int.intValue());
}
Builder.addFunctionCall(functionCalls.get(staticLevel - 1).intValue());

staticLevel--;
frames.store(Builder.build(), funDef);
stackOffset.remove(staticLevel);
functionCalls.remove(staticLevel);
}

@Override
Expand All @@ -167,21 +194,28 @@ public void visit(TypeDef typeDef) {

@Override
public void visit(VarDef varDef) {
if (staticLevel == 1) {
if (staticLevel == 0) {
accesses.store(
new Access.Global(types.valueFor(varDef).get().sizeInBytes(), Frame.Label.named(varDef.name)),
varDef);
} else {
accesses.store(new Access.Local(types.valueFor(varDef).get().sizeInBytes(), stackOffset, staticLevel),
stackOffset.get(staticLevel - 1).addLocVarToStack(types.valueFor(varDef).get().sizeInBytes());
accesses.store(
new Access.Local(types.valueFor(varDef).get().sizeInBytes(),
stackOffset.get(staticLevel - 1).getTopLocVarOffset(),
staticLevel),
varDef);
stackOffset += types.valueFor(varDef).get().sizeInBytes();
}
}

@Override
public void visit(Parameter parameter) {
accesses.store(new Access.Parameter(types.valueFor(parameter).get().sizeInBytesAsParam(), stackOffset, staticLevel), parameter);
stackOffset += types.valueFor(parameter).get().sizeInBytesAsParam();
stackOffset.get(staticLevel - 1).addParamToStack(types.valueFor(parameter).get().sizeInBytesAsParam());
accesses.store(
new Access.Parameter(types.valueFor(parameter).get().sizeInBytesAsParam(),
stackOffset.get(staticLevel - 1).paramStackOffset,
staticLevel),
parameter);
}

@Override
Expand All @@ -195,4 +229,29 @@ public void visit(Atom atom) {
@Override
public void visit(TypeName name) {
}

/*
* Razred za hranjenje podatke o skladu vsake funkcije
*/
private class StackOffsets {
public int paramStackOffset;
public ArrayList<Integer> locVarStackOffset;

public StackOffsets() {
paramStackOffset = 0;
locVarStackOffset = new ArrayList<Integer>();
}

public void addParamToStack(int size) {
paramStackOffset += size;
}

public void addLocVarToStack(int size) {
locVarStackOffset.add(Integer.valueOf(-size));
}

public int getTopLocVarOffset() {
return locVarStackOffset.stream().reduce(0, Integer::sum);
}
}
}
48 changes: 23 additions & 25 deletions src/compiler/seman/type/TypeChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,23 @@ public class TypeChecker implements Visitor {
* Opis vozlišč, ki jim priredimo podatkovne tipe.
*/
private NodeDescription<Type> types;
private List<Def> visited;

public TypeChecker(NodeDescription<Def> definitions, NodeDescription<Type> types) {
requireNonNull(definitions, types);
this.definitions = definitions;
this.types = types;
this.visited = new ArrayList<>();
}

@Override
public void visit(Call call) {
types.store(types.valueFor(definitions.valueFor(call).get()).get(), call);
Optional<Type> funDefReturnType = types.valueFor(definitions.valueFor(call).get());
if (funDefReturnType.isEmpty()) {
((FunDef) definitions.valueFor(call).get()).accept(this);
funDefReturnType = types.valueFor(definitions.valueFor(call).get());
}
types.store(funDefReturnType.get().asFunction().get().returnType, call);
call.arguments.forEach(arg -> {
arg.accept(this);
});
Expand Down Expand Up @@ -218,21 +225,8 @@ public void visit(Where where) {
@Override
public void visit(Defs defs) {
defs.definitions.forEach(def -> {
if (def instanceof TypeDef typeDef) {
typeDef.accept(this);
}
});

defs.definitions.forEach(def -> {
if (def instanceof FunDef funDef) {
funDef.accept(this);
} else if (def instanceof TypeDef typeDef) {

} else if (def instanceof VarDef varDef) {
varDef.accept(this);
} else {
Report.error(def.position, "Non-valid definition!");
}
if (!visited.contains(def))
def.accept(this);
});
}

Expand All @@ -258,13 +252,14 @@ public void visit(FunDef funDef) {

@Override
public void visit(TypeDef typeDef) {
checkRecursion(typeDef);
typeDef.type.accept(this);
types.store(types.valueFor(typeDef.type).get(), typeDef);
definitions.store(typeDef, typeDef.type);
}

@Override
public void visit(VarDef varDef) {
checkRecursion(varDef);
varDef.type.accept(this);
types.store(types.valueFor(varDef.type).get(), varDef);
}
Expand Down Expand Up @@ -299,14 +294,17 @@ public void visit(Atom atom) {

@Override
public void visit(TypeName name) {
try {
Optional<Def> temp = definitions.valueFor(name);
if (temp.isEmpty())
throw new Exception("null");
Type neki = types.valueFor(temp.get()).get();
types.store(neki, name);
} catch (Exception e) {
Report.error(name.position, "The type is not defined!");
Def nameDef = definitions.valueFor(name).get();
if (types.valueFor(nameDef).isEmpty()) {
nameDef.accept(this);
}
types.store(types.valueFor(nameDef).get(), name);
}

private void checkRecursion(Def def) {
if (visited.contains(def)) {
Report.error(def.position, "Definition recursion detected!");
}
visited.add(def);
}
}
62 changes: 59 additions & 3 deletions test.pns
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
fun f(x:integer, y:integer): integer = g(x, y);
fun g(x:integer, y:integer): integer = f(x,y);
fun h(z: integer, y:string): logical = f(z,z)
fun main(args:arr[100]int):int = (
# najde najbolj pogost element v tabeli
# tabela ima elemente vrednosti od 1 do 10 vključno

# napolnimo tabelo pogostosti z ničlami
{for i = 0, 10, 1:
{pogostost[i] = 0}
},

# preštejemo koliko je katerih elementov
{for i = 0, 100, 1:
{pogostost[args[i-1]] = pogostost[args[i-1]] + 1}
},

# poiščemo največjo vrednost v tabeli pogostosti
{max = najvecji(pogostost)},

# poiščemo indeks največje vrednosti v tabeli pogostosti
{max_index = index(pogostost, max)},

# vrnemo največji element
max_index+1
) {where
var pogostost:arr[10]int;
var i:int;
var max:int;
var max_index:int;

# definirane funkcije
fun najvecji(array:arr[10]int):int = (
{max = 0},
{for i = 0, 10, 1:
{if array[i] > max then
{max = array[i]}
}
},
max
) {where
var i:int;
var max:int
};

fun index(array:arr[10]int, value:int):int = (
{returnValue = -1},
{for i = 0, 10, 1:
{if array[i] == value & returnValue == -1 then
{returnValue = i}
}
},
returnValue
) {where
var i:int;
var returnValue:int
}
};

typ int:integer;
typ str:string;
typ bool:logical

0 comments on commit 701381d

Please sign in to comment.