Skip to content

Commit

Permalink
2018.11.30 (1.52j3; Macro Interpreter)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasband committed Dec 1, 2018
1 parent da30563 commit 6d30e8d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 19 deletions.
13 changes: 13 additions & 0 deletions ij/IJ.java
Expand Up @@ -70,6 +70,7 @@ public class IJ {
private static DecimalFormatSymbols dfs;
private static boolean trustManagerCreated;
private static String smoothMacro;
private static Interpreter macroInterpreter;

static {
osname = System.getProperty("os.name");
Expand Down Expand Up @@ -312,6 +313,13 @@ public static void run(String command, String options) {
//IJ.log("run2: "+command+" "+Thread.currentThread().hashCode());
}

/** Used by the macro interpretor to run commands. */
public static void runFromMacro(Interpreter interpreter, String command, String options) {
macroInterpreter = interpreter;
run(command, options);
macroInterpreter = null;
}

/** Converts commands that have been renamed so
macros using the old names continue to work. */
private static String convert(String command) {
Expand Down Expand Up @@ -645,6 +653,11 @@ public static void showMessage(String title, String msg) {
macro or JavaScript is running, it is aborted. Writes to the
Java console if the ImageJ window is not present.*/
public static void error(String msg) {
if ( macroInterpreter!=null) {
macroInterpreter.abort(msg);
macroInterpreter = null;
return;
}
error(null, msg);
if (Thread.currentThread().getName().endsWith("JavaScript"))
throw new RuntimeException(Macro.MACRO_CANCELED);
Expand Down
4 changes: 2 additions & 2 deletions ij/ImageJ.java
Expand Up @@ -78,8 +78,8 @@ public class ImageJ extends Frame implements ActionListener,
MouseListener, KeyListener, WindowListener, ItemListener, Runnable {

/** Plugins should call IJ.getVersion() or IJ.getFullVersion() to get the version string. */
public static final String VERSION = "1.52i";
public static final String BUILD = ""; //52
public static final String VERSION = "1.52j";
public static final String BUILD = "3";
public static Color backgroundColor = new Color(237,237,237);
/** SansSerif, 12-point, plain font. */
public static final Font SansSerif12 = new Font("SansSerif", Font.PLAIN, 12);
Expand Down
5 changes: 1 addition & 4 deletions ij/macro/Functions.java
Expand Up @@ -620,10 +620,7 @@ void doRun() {
arg2 = getString();
interp.getRightParen();
}
if (arg2!=null)
IJ.run(arg1, arg2);
else
IJ.run(arg1);
IJ.runFromMacro(this.interp, arg1, arg2);
resetImage();
IJ.setKeyUp(IJ.ALL_KEYS);
shiftKeyDown = altKeyDown = false;
Expand Down
44 changes: 41 additions & 3 deletions ij/macro/Interpreter.java
Expand Up @@ -72,6 +72,7 @@ public class Interpreter implements MacroConstants {
ResultsTable applyMacroTable;
int errorCount;
volatile boolean ignoreErrors;
String errorMessage;


/** Interprets the specified string. */
Expand All @@ -83,12 +84,24 @@ public void run(String macro) {
macro = macro + additionalFunctions;
}
IJ.resetEscape();
if (pgm!=null)
reuseSymbolTable();
Tokenizer tok = new Tokenizer();
Program pgm = tok.tokenize(macro);
if (pgm.hasVars && pgm.hasFunctions)
saveGlobals2(pgm);
run(pgm);
}

private void reuseSymbolTable() {
if (pgm==null || pgm.stLoc>799)
return;
Symbol[] table1 = pgm.getSymbolTable();
Symbol[] table2 = new Symbol[pgm.stLoc+1];
for (int i=0; i<=pgm.stLoc; i++)
table2[i] = table1[i];
Program.systemTable = table2;
}

/** Runs the specified macro, passing it a string
argument and returning a string value. */
Expand Down Expand Up @@ -116,6 +129,8 @@ public void run(Program pgm) {
if (func==null)
func = new Functions(this, pgm);
func.plot = null;
done = false;
errorMessage = null;
doStatements();
finishUp();
}
Expand Down Expand Up @@ -1214,6 +1229,7 @@ final void getComma() {
}

void error (String message) {
errorMessage = message;
if (ignoreErrors)
return;
errorCount++;
Expand Down Expand Up @@ -1796,9 +1812,13 @@ void dump() {
void dumpStack() {
IJ.log("");
IJ.log("Stack");
if (stack!=null)
for (int i=topOfStack; i>=0; i--)
IJ.log(i+" "+pgm.table[stack[i].symTabIndex].str+" "+stack[i]);
if (stack!=null) {
for (int i=topOfStack; i>=0; i--) {
Variable v = stack[i];
Symbol symbol = v!=null?pgm.table[v.symTabIndex]:null;
IJ.log(i+" "+(symbol!=null?symbol.str:"null")+" "+v);
}
}
}

void finishUp() {
Expand Down Expand Up @@ -1883,6 +1903,16 @@ public void abortMacro() {
}
}

public void abort(String message) {
errorMessage = message;
if (ignoreErrors) {
done = true;
finishUp();
} else {
error(message);
}
}

private synchronized void shutdown() {
ignoreErrors = true;
for (int i=0; i<10; i++)
Expand Down Expand Up @@ -2265,6 +2295,14 @@ public void setApplyMacroTable(ResultsTable rt) {
applyMacroTable = rt;
}

public void setIgnoreErrors(boolean ignoreErrors) {
this.ignoreErrors = ignoreErrors;
}

public String getErrorMessage() {
return errorMessage;
}

} // class Interpreter


Expand Down
16 changes: 7 additions & 9 deletions ij/macro/Program.java
Expand Up @@ -22,15 +22,13 @@ public class Program implements MacroConstants {
// run keyboard shortcut macros on event dispatch thread?
boolean queueCommands;
Hashtable extensionRegistry;



public Program() {
if (systemTable!=null) {
stLoc = systemTable.length - 1;
for (int i=0; i<=stLoc; i++)
table[i] = systemTable[i];
table[i] = systemTable[i];
} else {
//IJ.log("make table");
addKeywords();
addFunctions();
addNumericFunctions();
Expand Down Expand Up @@ -99,11 +97,9 @@ void addToken(int tok, int lineNumber) {//n__
int[] tmp = new int[maxProgramSize*2];
System.arraycopy(code, 0, tmp, 0, maxProgramSize);
code = tmp;

tmp = new int[maxProgramSize*2]; //n__
System.arraycopy(lineNumbers, 0, tmp, 0, maxProgramSize);
lineNumbers = tmp;

maxProgramSize *= 2;
}
code[pc] = tok;
Expand All @@ -112,7 +108,6 @@ void addToken(int tok, int lineNumber) {//n__

/** Looks up a word in the symbol table. Returns null if the word is not found. */
Symbol lookupWord(String str) {
//IJ.log("lookupWord: "+str);
Symbol symbol;
String symStr;
for (int i=0; i<=stLoc; i++) {
Expand All @@ -126,7 +121,6 @@ Symbol lookupWord(String str) {
}

void saveGlobals(Interpreter interp) {
//IJ.log("saveGlobals: "+interp.topOfStack);
if (interp.topOfStack==-1)
return;
int n = interp.topOfStack+1;
Expand Down Expand Up @@ -274,9 +268,13 @@ public boolean hasWord(String word) {
}
return false;
}

public int getSize() {
return pc;
}

public String toString() {
return "pgm[code="+(code!=null?""+code.length:"null") + " tab="+(table!=null?""+table.length:"null")+"]";
}

} // Program
1 change: 0 additions & 1 deletion ij/macro/Tokenizer.java
Expand Up @@ -17,7 +17,6 @@ public Program tokenize(String program) {
if (program.contains("/*") && program.contains("*/"))
program = addSpacesToEmptyLines(program);
st = new StreamTokenizer(new StringReader(program));
//st.eolIsSignificant(true);
st.ordinaryChar('-');
st.ordinaryChar('/');
st.ordinaryChar('.');
Expand Down
7 changes: 7 additions & 0 deletions release-notes.html
Expand Up @@ -5,6 +5,13 @@
</head>
<body>

<li> <u>1.52j 30 November 2018</u>
<ul>
<li> Thanks to Jerome Mutterer, added the setIgnoreErrors() and
getErrorMessage() methods to the Interpreter class
(<a href="http://wsr.imagej.net/plugins/Test_IgnoreErrors.java">example</a>).
</ul>

<li> <u>1.52i 26 November 2018</u>
<ul>
<li> Thanks to Ved Sharma and Michael Kaul, added the
Expand Down

0 comments on commit 6d30e8d

Please sign in to comment.