Skip to content

Commit

Permalink
REMOVE function counter, ADD comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjf1 committed Nov 24, 2020
1 parent 4ef33a8 commit 0e82b21
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
58 changes: 58 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {

static class Person {
public char gender;
public int age;
public float height;
public Boolean isGay;
public Person[] relatives;
}

static Scanner _reader = new Scanner(new InputStreamReader(System.in));
static Object[] _ret;

public static int read() {
return Integer.valueOf(_reader.nextLine());
}

public static void main(String args[]) {
main();
}

static Object[] genPerson(int ammount) {
Person[] p;
int i;
p = new Person[ammount];
i = 0;
int _cond_0 = ammount;
for(int _i_0 = 0; _i_0 < _cond_0; _i_0++) {
p[i] = new Person();
i = i + 1;
}
return new Object[]{p};
}

static Object[] foo() {
return new Object[]{1, 4.2f, 'a'};
}

static void main() {
Person[] a;
int x;
float y;
char z;
int k;
a = (Person[]) genPerson(10)[0];
a[0].age = 20;
System.out.print(a);
_ret = foo();
x = (int)_ret[0];
y = (float)_ret[1];
z = (char)_ret[2];
_ret = foo();
k = (int)_ret[0];
y = (float)_ret[1];
}
}
6 changes: 1 addition & 5 deletions src/main/java/generator/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class CodeGenerator extends LexLangBaseVisitor<Object> {
final FunctionManager functionManager;

private int itCounter = -1;
private int fCounter = -1;

public CodeGenerator(SemanticAnalyzer analyzer, STGroup templates) {
this.t = templates;
Expand Down Expand Up @@ -233,7 +232,6 @@ public ST visitAttrCmd(LexLangParser.AttrCmdContext ctx) {
public ST visitFuncCmd(LexLangParser.FuncCmdContext ctx) {
FunctionDeclaration func = getFunction(ctx.ID().getText(), ctx.exps());
ST template = t.getInstanceOf("funcCmd");
fCounter++;
if (ctx.lvalue().size() > 0) {
List<ST> attributions = new ArrayList<>();
List<Object> variables = visitList(ctx.lvalue());
Expand All @@ -242,7 +240,6 @@ public ST visitFuncCmd(LexLangParser.FuncCmdContext ctx) {
t.getInstanceOf("funcCmdAttr")
.add("lvalue", variables.get(i))
.add("type", visit(func.getReturnTypes().get(i)))
.add("counter", fCounter)
.add("index", i)

);
Expand All @@ -253,8 +250,7 @@ public ST visitFuncCmd(LexLangParser.FuncCmdContext ctx) {
template.add("exps", visitList(ctx.exps().exp()));

return template
.add("id", ctx.ID().getText())
.add("counter", fCounter);
.add("id", ctx.ID().getText());
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/generator/Template.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Maxwell Souza 201435009
Rodolpho Rossete 201435032
*/

package generator;

import org.stringtemplate.v4.ST;
Expand Down
18 changes: 11 additions & 7 deletions src/main/resources/java.stg
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Maxwell Souza 201435009
Rodolpho Rossete 201435032
*/

program(data, funcs) ::= <<
import java.io.InputStreamReader;
import java.util.Scanner;
Expand All @@ -6,6 +11,7 @@ public class Main {
<data; separator="\n\n">

static Scanner _reader = new Scanner(new InputStreamReader(System.in));
static Object[] _ret;

public static int read() {
return Integer.valueOf(_reader.nextLine());
Expand Down Expand Up @@ -60,7 +66,8 @@ if(<expr>) {
>>

iterate(expr, cmd, counter) ::= <<
for(int _i_<counter> = 0; _i_<counter> \< <expr>; _i_<counter>++) {
int _cond_<counter> = <expr>;
for(int _i_<counter> = 0; _i_<counter> \< _cond_<counter>; _i_<counter>++) {
<cmd>
}
>>
Expand All @@ -71,11 +78,11 @@ read(lvalue) ::= "<lvalue> = read();"
attr(lvalue, exp) ::= "<lvalue> = <exp>;"

funcCmd(id, exps, attr, counter) ::= <<
<if(attr)>Object[] _ret_<counter> = <endif><id>(<exps; separator=", ">);
<if(attr)>_ret = <endif><id>(<exps; separator=", ">);
<attr; separator="\n">
>>

funcCmdAttr(lvalue, type, index, counter) ::= "<lvalue> = (<type>)_ret_<counter>[<index>];"
funcCmdAttr(lvalue, type, index, counter) ::= "<lvalue> = (<type>)_ret[<index>];"

returnCmd(values) ::= <<return new Object[]{<values; separator=", ">};>>

Expand All @@ -100,9 +107,6 @@ charSexp(exp) ::= "'<exp>'"
floatSexp(exp) ::= "<exp>f"

closurePexp(exp) ::= "(<exp>)"
instancePexp(type, depth, exp) ::= "new <type><if(exp)>[<exp>]<endif><depth>"
instancePexp(type, depth, exp) ::= "new <type><if(exp)>[<exp>]<endif><depth>"

funcCallPexp(id, exps, type, exp) ::= <<(<type>) <id>(<exps; separator=", ">)[<exp>]>>


literal(val) ::= "<val>"

0 comments on commit 0e82b21

Please sign in to comment.