Skip to content

Commit

Permalink
FIX function return interpretation, clean code, ADD README instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjf1 committed Nov 18, 2020
1 parent bf14e4c commit f6fe5f7
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 28 deletions.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,85 @@ O programa recebem por parâmetro o caminho do arquivo de programa lang a ser ex
```bash
java -jar target/compile-V-0.0.1-jar-with-dependencies.jar caminho/do/programa.lang
```

## Exemplo de programa

```c++
-- Comentário em linha

{-

Comentário em bloco

-}

-- Declaração de estrutura
data Person {
gender :: Char;
age :: Int;
relatives :: Person[];
}

-- função principal
main(){

read times; -- Le inteiro do teclado
iterate(times) -- for primitivo
print hello()[0]; -- chamada de função

me = new Person; -- Instancia estrutura
me.age = 24; -- atribuição de propriedade
me.gender = 'm';
me.relatives = new Person[10];
me.relatives[0] = new Person;
me.relatives[0].age = 40;
me.relatives[0].gender = 'f';

print fibonacci(9)[0]; -- get return of a function
ln();

divide(10, 3)<result, resto>;
print result;
ln();
print resto;
ln();

}

fibonacci(n :: Int) : Int
{
if (n < 1)
return n;
if (n == 1)
return n;
return fibonacci(n-1)[0] + fibonacci(n-2)[0];
}

divide(a :: Int, b :: Int) : Int, Int {
return a / b, a % b;
}
-- sobrecarga
divide(a :: Float, b :: Float) : Float, Float {
return a / b, a % b;
}

ln(){ print '\n'; }

hello() : Char[] {
message = new Char[13]; -- Instancia array
message[0] = 'h';
message[1] = 'e';
message[2] = 'l';
message[3] = 'l';
message[4] = 'o';
message[5] = ' ';
message[6] = 'w';
message[7] = 'o';
message[8] = 'r';
message[9] = 'l';
message[10] = 'd';
message[11] = '!';
message[12] = '\n';
return message;
}
```
44 changes: 16 additions & 28 deletions src/main/java/lexlang/LangInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class LangInterpreter extends LexLangBaseVisitor<Value> {
FunctionManager functionManager;

Boolean returnCalled = false;
List<Value> returnValues = null;

public LangInterpreter(SemanticAnalyzer analyzer) {
this.functionManager = analyzer.getFuncManager();
Expand Down Expand Up @@ -156,56 +155,45 @@ public Value visitInstancePexp(LexLangParser.InstancePexpContext ctx) {
}
if (List.of("Int", "Char", "Bool", "Float").contains(type))
return new Value(null);
// if (!dataTypes.containsKey(type))
// throw new LangException("Data '" + type + "' not found");
return new Value(new Data(dataTypes.get(type)));
}

// functions
// @Override
// public Value visitFunc(LexLangParser.FuncContext ctx) {
// functionManager.addFunction(ctx);
// return Value.VOID;
// }

@Override
public Value visitFuncCmd(LexLangParser.FuncCmdContext ctx) {
String name = ctx.ID().getText();
Value ret = runFunction(name, ctx.exps());
for (int i = 0; i < ctx.lvalue().size(); i++) {
resolveVariable(ctx.lvalue(i), returnValues.get(i));
Value returnValue = runFunction(name, ctx.exps());
if(ctx.lvalue().size() > 0) {
ArrayList<Value> returnList = (ArrayList<Value>) returnValue.getPrimitive();
for (int i = 0; i < ctx.lvalue().size(); i++)
resolveVariable(ctx.lvalue(i), returnList.get(i));
}
returnValues = null;
return ret;
return returnValue;
}

@Override
public Value visitFuncCallPexp(LexLangParser.FuncCallPexpContext ctx) {
String name = ctx.ID().getText();
Value result = runFunction(name, ctx.exps());
Value returnValue = runFunction(name, ctx.exps());
ArrayList<Value> returnList = (ArrayList<Value>) returnValue.getPrimitive();
if (ctx.exp() != null) {
int i = visit(ctx.exp()).getInt();
// if (returnValues == null)
// throw new LangException("Function '" + name +
// "' doesn't returns arguments, tried to access argument [" + i + ']');
// if (i >= returnValues.size())
// throw new LangException("Function '" + name +
// "' only returns " + returnValues.size() +
// " arguments, tried to access argument [" + i + ']');
result = returnValues.get(i);
returnValue = returnList.get(i);
}
returnValues = null;
return result;
else returnValue = returnList.get(0);
return returnValue;
}

@Override
public Value visitReturnCmd(LexLangParser.ReturnCmdContext ctx) {
this.returnValues = new ArrayList<>();
ArrayList<Value> ret = new ArrayList<Value>();
for (LexLangParser.ExpContext expContext : ctx.exp()) {
this.returnValues.add(visit(expContext));
ret.add(visit(expContext));

}
this.returnCalled = true;
return this.returnValues.get(0);
return new Value(ret);
}

// variables
Expand Down Expand Up @@ -386,7 +374,7 @@ public Value visitReadCmd(LexLangParser.ReadCmdContext ctx) {
String response = reader.nextLine();
int val;
try {
val = Integer.parseInt(response);
val = Integer.valueOf(response);
} catch (Exception e) {
throw new LangException("Read error: 'Int' expected, received '" + response + "'", ctx);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/lexlang/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public ArrayValue getArray() {
return (ArrayValue) primitive;
}

public Object getPrimitive() {
return primitive;
}

@Override
public boolean equals(Object o) {
if (primitive == o) return true;
Expand Down

0 comments on commit f6fe5f7

Please sign in to comment.