Skip to content

Commit f6fe5f7

Browse files
committed
FIX function return interpretation, clean code, ADD README instructions
1 parent bf14e4c commit f6fe5f7

File tree

3 files changed

+102
-28
lines changed

3 files changed

+102
-28
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,85 @@ O programa recebem por parâmetro o caminho do arquivo de programa lang a ser ex
2323
```bash
2424
java -jar target/compile-V-0.0.1-jar-with-dependencies.jar caminho/do/programa.lang
2525
```
26+
27+
## Exemplo de programa
28+
29+
```c++
30+
-- Comentário em linha
31+
32+
{-
33+
34+
Comentário em bloco
35+
36+
-}
37+
38+
-- Declaração de estrutura
39+
data Person {
40+
gender :: Char;
41+
age :: Int;
42+
relatives :: Person[];
43+
}
44+
45+
-- função principal
46+
main(){
47+
48+
read times; -- Le inteiro do teclado
49+
iterate(times) -- for primitivo
50+
print hello()[0]; -- chamada de função
51+
52+
me = new Person; -- Instancia estrutura
53+
me.age = 24; -- atribuição de propriedade
54+
me.gender = 'm';
55+
me.relatives = new Person[10];
56+
me.relatives[0] = new Person;
57+
me.relatives[0].age = 40;
58+
me.relatives[0].gender = 'f';
59+
60+
print fibonacci(9)[0]; -- get return of a function
61+
ln();
62+
63+
divide(10, 3)<result, resto>;
64+
print result;
65+
ln();
66+
print resto;
67+
ln();
68+
69+
}
70+
71+
fibonacci(n :: Int) : Int
72+
{
73+
if (n < 1)
74+
return n;
75+
if (n == 1)
76+
return n;
77+
return fibonacci(n-1)[0] + fibonacci(n-2)[0];
78+
}
79+
80+
divide(a :: Int, b :: Int) : Int, Int {
81+
return a / b, a % b;
82+
}
83+
-- sobrecarga
84+
divide(a :: Float, b :: Float) : Float, Float {
85+
return a / b, a % b;
86+
}
87+
88+
ln(){ print '\n'; }
89+
90+
hello() : Char[] {
91+
message = new Char[13]; -- Instancia array
92+
message[0] = 'h';
93+
message[1] = 'e';
94+
message[2] = 'l';
95+
message[3] = 'l';
96+
message[4] = 'o';
97+
message[5] = ' ';
98+
message[6] = 'w';
99+
message[7] = 'o';
100+
message[8] = 'r';
101+
message[9] = 'l';
102+
message[10] = 'd';
103+
message[11] = '!';
104+
message[12] = '\n';
105+
return message;
106+
}
107+
```

src/main/java/lexlang/LangInterpreter.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class LangInterpreter extends LexLangBaseVisitor<Value> {
2525
FunctionManager functionManager;
2626

2727
Boolean returnCalled = false;
28-
List<Value> returnValues = null;
2928

3029
public LangInterpreter(SemanticAnalyzer analyzer) {
3130
this.functionManager = analyzer.getFuncManager();
@@ -156,56 +155,45 @@ public Value visitInstancePexp(LexLangParser.InstancePexpContext ctx) {
156155
}
157156
if (List.of("Int", "Char", "Bool", "Float").contains(type))
158157
return new Value(null);
159-
// if (!dataTypes.containsKey(type))
160-
// throw new LangException("Data '" + type + "' not found");
161158
return new Value(new Data(dataTypes.get(type)));
162159
}
163160

164161
// functions
165-
// @Override
166-
// public Value visitFunc(LexLangParser.FuncContext ctx) {
167-
// functionManager.addFunction(ctx);
168-
// return Value.VOID;
169-
// }
170162

171163
@Override
172164
public Value visitFuncCmd(LexLangParser.FuncCmdContext ctx) {
173165
String name = ctx.ID().getText();
174-
Value ret = runFunction(name, ctx.exps());
175-
for (int i = 0; i < ctx.lvalue().size(); i++) {
176-
resolveVariable(ctx.lvalue(i), returnValues.get(i));
166+
Value returnValue = runFunction(name, ctx.exps());
167+
if(ctx.lvalue().size() > 0) {
168+
ArrayList<Value> returnList = (ArrayList<Value>) returnValue.getPrimitive();
169+
for (int i = 0; i < ctx.lvalue().size(); i++)
170+
resolveVariable(ctx.lvalue(i), returnList.get(i));
177171
}
178-
returnValues = null;
179-
return ret;
172+
return returnValue;
180173
}
181174

182175
@Override
183176
public Value visitFuncCallPexp(LexLangParser.FuncCallPexpContext ctx) {
184177
String name = ctx.ID().getText();
185-
Value result = runFunction(name, ctx.exps());
178+
Value returnValue = runFunction(name, ctx.exps());
179+
ArrayList<Value> returnList = (ArrayList<Value>) returnValue.getPrimitive();
186180
if (ctx.exp() != null) {
187181
int i = visit(ctx.exp()).getInt();
188-
// if (returnValues == null)
189-
// throw new LangException("Function '" + name +
190-
// "' doesn't returns arguments, tried to access argument [" + i + ']');
191-
// if (i >= returnValues.size())
192-
// throw new LangException("Function '" + name +
193-
// "' only returns " + returnValues.size() +
194-
// " arguments, tried to access argument [" + i + ']');
195-
result = returnValues.get(i);
182+
returnValue = returnList.get(i);
196183
}
197-
returnValues = null;
198-
return result;
184+
else returnValue = returnList.get(0);
185+
return returnValue;
199186
}
200187

201188
@Override
202189
public Value visitReturnCmd(LexLangParser.ReturnCmdContext ctx) {
203-
this.returnValues = new ArrayList<>();
190+
ArrayList<Value> ret = new ArrayList<Value>();
204191
for (LexLangParser.ExpContext expContext : ctx.exp()) {
205-
this.returnValues.add(visit(expContext));
192+
ret.add(visit(expContext));
193+
206194
}
207195
this.returnCalled = true;
208-
return this.returnValues.get(0);
196+
return new Value(ret);
209197
}
210198

211199
// variables
@@ -386,7 +374,7 @@ public Value visitReadCmd(LexLangParser.ReadCmdContext ctx) {
386374
String response = reader.nextLine();
387375
int val;
388376
try {
389-
val = Integer.parseInt(response);
377+
val = Integer.valueOf(response);
390378
} catch (Exception e) {
391379
throw new LangException("Read error: 'Int' expected, received '" + response + "'", ctx);
392380
}

src/main/java/lexlang/Value.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public ArrayValue getArray() {
4747
return (ArrayValue) primitive;
4848
}
4949

50+
public Object getPrimitive() {
51+
return primitive;
52+
}
53+
5054
@Override
5155
public boolean equals(Object o) {
5256
if (primitive == o) return true;

0 commit comments

Comments
 (0)