Skip to content

#441: changed operation check in UserDefinedFunction to a simple cond… #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,12 @@ private boolean isTailRecursive() {

AST lastChild = ast.get().lastChild();

switch (lastChild.getOp()) {
case FUNCALL:
if (lastChild.getOp() == Operation.FUNCALL) {
if (getFuncName().isPresent()
&& getFuncName().get().equalsIgnoreCase(
lastChild.getToken())) {
isTailRecursive = true;
}
break;
default:
}

return isTailRecursive;
Expand Down
2 changes: 1 addition & 1 deletion src/org/aavso/tools/vstar/vela/VeLaInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ private void binaryOpError(Operation op, Type... types) {
/**
* Unify operand types by converting both operands to strings if only one is a
* string or both operands to double if only one is an integer. We change
* nothing if either type is composite or Boolean.
* nothing if either type is composite.
*
* @param a The first operand.
* @param b The second operand.
Expand Down
129 changes: 80 additions & 49 deletions src/org/aavso/tools/vstar/vela/VeLaScriptDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,93 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Optional;

import org.aavso.tools.vstar.ui.mediator.Mediator;
import org.aavso.tools.vstar.util.Pair;

/**
* This class allows a VeLa program to be run from the command-line.
* This class allows a VeLa program to be run from the command-line or for its
* LISP or DOT AST to be sent to standard output.
*/
public class VeLaScriptDriver {
public static void main(String[] args) {
if (args.length >= 1 && args.length <= 3) {
BufferedReader reader = null;
try {
// Process command-line arguments.
String velaSourceFile = null;
boolean verbose = false;
boolean restartOnError = false;
for (String arg : args) {
if (arg.startsWith("--")) {
verbose = "--verbose".equals(arg);
restartOnError = "--restart".equals(arg);
} else {
velaSourceFile = arg;
}
}
public static void main(String[] args) {
if (args.length >= 1 && args.length <= 5) {
BufferedReader reader = null;
try {
// Process command-line arguments.
String velaSourceFile = null;
boolean verbose = false;
boolean restartOnError = false;
boolean lispAST = false;
boolean dotAST = false;

if (Mediator.getUI() != null) {
Mediator.getUI().setScriptingStatus(true);
}
for (String arg : args) {
if (arg.startsWith("--")) {
if ("--verbose".equals(arg)) {
verbose = true;
}
if ("--restart".equals(arg)) {
restartOnError = true;
}
if ("--lisp".equals(arg)) {
lispAST = true;
}
if ("--dot".equals(arg)) {
dotAST = true;
}
} else {
velaSourceFile = arg;
}
}

// Run interpreter, optionally restarting it on error.
VeLaInterpreter vela = new VeLaInterpreter(verbose);
do {
try {
reader = new BufferedReader(new FileReader(
velaSourceFile));
StringBuffer buf = new StringBuffer();
String line = reader.readLine();
while (line != null) {
buf.append(line);
buf.append("\n");
line = reader.readLine();
}
vela.program(buf.toString());
} catch (Throwable t) {
System.out.println(t.getLocalizedMessage());
}
} while (restartOnError);
} finally {
try {
reader.close();
} catch (IOException e) {
}
if (Mediator.getUI() != null) {
Mediator.getUI().setScriptingStatus(true);
}

if (Mediator.getUI() != null) {
Mediator.getUI().setScriptingStatus(false);
}
}
}
}
// Run interpreter, optionally restarting it on error.
VeLaInterpreter vela = new VeLaInterpreter(verbose);
do {
try {
reader = new BufferedReader(new FileReader(velaSourceFile));
StringBuffer buf = new StringBuffer();
String line = reader.readLine();
while (line != null) {
buf.append(line);
buf.append("\n");
line = reader.readLine();
}

Pair<Optional<Operand>, AST> pair = vela.veLaToResultASTPair(buf.toString());

Optional<Operand> result = pair.first;
if (result.isPresent()) {
System.out.println(result.get());
}

if (lispAST) {
System.out.println(pair.second.toString());
}

if (dotAST) {
System.out.println(pair.second.toFullDOT());
}

} catch (Throwable t) {
System.err.println(t.getLocalizedMessage());
t.printStackTrace();
}
} while (restartOnError);
} finally {
try {
reader.close();
} catch (IOException e) {
}

if (Mediator.getUI() != null) {
Mediator.getUI().setScriptingStatus(false);
}
}
}
}
}
12 changes: 12 additions & 0 deletions test/org/aavso/tools/vstar/vela/VeLaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,18 @@ public void testBoundFun() {
assertEquals(720, result.get().intVal());
}

// see https://github.com/AAVSO/VStar/issues/441
public void testFunReturningLiteralExpr() {
String prog = "";
prog += "f(n:real) : real { 12.0 }";
prog += "f(4)";

Optional<Operand> result = vela.program(prog);

assertTrue(result.isPresent());
assertEquals(12.0, result.get().doubleVal());
}

public void testFunMap() {
String prog = "";
prog += "fact(n:integer) : integer {";
Expand Down
Loading