diff --git a/README.md b/README.md index 766bdef..25b038d 100644 --- a/README.md +++ b/README.md @@ -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); + 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; +} +``` \ No newline at end of file diff --git a/src/main/java/lexlang/LangInterpreter.java b/src/main/java/lexlang/LangInterpreter.java index a43cb3f..d5aa529 100644 --- a/src/main/java/lexlang/LangInterpreter.java +++ b/src/main/java/lexlang/LangInterpreter.java @@ -25,7 +25,6 @@ public class LangInterpreter extends LexLangBaseVisitor { FunctionManager functionManager; Boolean returnCalled = false; - List returnValues = null; public LangInterpreter(SemanticAnalyzer analyzer) { this.functionManager = analyzer.getFuncManager(); @@ -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 returnList = (ArrayList) 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 returnList = (ArrayList) 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 ret = new ArrayList(); 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 @@ -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); } diff --git a/src/main/java/lexlang/Value.java b/src/main/java/lexlang/Value.java index d42f9e6..02aced9 100644 --- a/src/main/java/lexlang/Value.java +++ b/src/main/java/lexlang/Value.java @@ -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;