created mirror

This commit is contained in:
whatever
2026-09-14 20:19:29 -04:00
commit 6764738d92
600 changed files with 87187 additions and 0 deletions

130
Java/clang/Data.java Normal file
View File

@@ -0,0 +1,130 @@
/*
Data.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag;
/** An immutable list of Clang data pieces.
* Each piece can be a cell, a string, an int, or a bool. Pieces are
* automatically converted if possible. That way, data can be stored
* virtually someplace, or in cells; from a list of cells, a Data object
* can be constructed which behaves just as a list of objects containing
* the data directly. E.g.: <code>
* Data d = new Data("hi", new Integer(17), new Boolean(false));
* </code> can be stored as <code>
* new Data(cell1, cell2, cell3);
* </code> if these cells contain the strings "hi", "17", and "false". Saving
* a Data object into a strip of cells is simple: just do something like this:
* <code>
* for(int i=0; i<data.len(); i++)
* cell[i].setText(data.s(i));
* </code>
* (s(i) is the method to get the ith element of a Data strip as a String.)
*/
public final class Data {
String rcsid = "$Id: Data.java,v 1.3 2001/01/07 18:21:42 bfallenstein Exp $";
private Object[] obs;
public Data() { obs = new Object[0]; }
public Data(Object o1) { obs = new Object[]{o1}; }
public Data(Object o1, Object o2) {
obs = new Object[]{o1,o2}; }
public Data(Object o1, Object o2, Object o3) {
obs = new Object[]{o1,o2,o3}; }
public Data(Object o1, Object o2, Object o3, Object o4) {
obs = new Object[]{o1,o2,o3,o4}; }
public Data(Object o1, Object o2, Object o3, Object o4, Object o5) {
obs = new Object[]{o1,o2,o3,o4,o5}; }
public Data(Object[] o) {
obs = new Object[o.length];
System.arraycopy(o, 0, obs, 0, o.length);
}
public Data(Data d, Object[] o) {
obs = new Object[d.obs.length + o.length];
System.arraycopy(d.obs, 0, obs, 0, d.obs.length);
System.arraycopy(o, 0, obs, d.obs.length, o.length);
}
public Data(Data d1, Data d2) {
obs = new Object[d1.obs.length + d2.obs.length];
System.arraycopy(d1.obs, 0, obs, 0, d1.obs.length);
System.arraycopy(d2.obs, 0, obs, d1.obs.length, d2.obs.length);
}
public int len() { return obs.length; }
public Object o(int at) { return obs[at]; }
public ZZCell c(int at) { return c(obs[at]); }
public String s(int at) { return s(obs[at]); }
public boolean b(int at) { return b(obs[at]); }
public int i(int at) { return i(obs[at]); }
public static Object o(Object o) { return o; }
public static ZZCell c(Object o) {
if(!(o instanceof ZZCell))
throw new ZZError("Wrong clang datum "+o+": not a cell");
return (ZZCell)o;
}
public static String s(Object o) {
if(o instanceof String)
return (String)o;
else if(o instanceof Integer)
return ""+((Integer)o).intValue();
else if(o instanceof ZZCell)
return ((ZZCell)o).getText();
else if(o instanceof Boolean) {
if(((Boolean)o).booleanValue())
return "true";
else
return "false";
} else
return o.toString(); // That's right, right?
// throw new ZZError("Wrong clang datum "+o+": not a str");
}
public static boolean b(Object o) {
if(o instanceof Boolean)
return ((Boolean)o).booleanValue();
else {
String str = s(o);
if(str.equals("true"))
return true;
else if(str.equals("false"))
return false;
else
throw new ZZError("Wrong clang datum " + o + ": " +
"not a boolean");
}
}
public static int i(Object o) {
if(o instanceof Integer)
return ((Integer)o).intValue();
else {
try {
return Integer.parseInt(s(o));
} catch(NumberFormatException e) {
throw new ZZError("Wrong clang datum " + o + ": " +
"not an integer");
}
}
}
}

186
Java/clang/ZZClang1.java Normal file
View File

@@ -0,0 +1,186 @@
/*
ZZClang1.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.clang;
import org.gzigzag.*;
import java.util.*;
/** A primitive test version of Clang.
* NOT YET FINISHED!
* <p>
* XXX - make use stack frames in the structure
*/
public class ZZClang1 implements ZZClangContext /* , ZZClangOp */ {
public static final String rcsid = "$Id: ZZClang1.java,v 1.5 2000/10/18 14:35:31 tjl Exp $";
public static final boolean dbg = true;
static final void p(String s) { if(dbg) System.out.println(s); }
Hashtable specials;
Hashtable symtab;
ZZCursor cursor;
ZZCell viewspex;
/** Execute a script in response to user action.
* This means that the language can have special key words to
* pass as parameters the key as a cell with a span etc.
*/
synchronized public void execUserAction(ZZCell code, ZZCell viewspex,
ZZCell clicked, String key) {
try {
specials = new Hashtable();
if(key!=null) specials.put("UserAction.keyString", key);
if(clicked!=null)
specials.put("UserAction.clickedCell", clicked);
execEnv(code, viewspex);
} finally {
specials = null;
}
}
/** Execute a script in a fresh environment.
*/
synchronized public void execEnv(ZZCell code, ZZCell viewspex2) {
symtab = new Hashtable();
viewspex = viewspex2;
ZZCursor realcursor = new ZZCursorReal(viewspex);
// Speed & space hack
cursor = new ZZCursorVirtual(realcursor.get());
exec(code);
realcursor.set(cursor.get());
}
/** Execute starting from a cell.
* Should not be used from outside except in special cases.
*/
synchronized public void exec(ZZCell code) {
p("CLANG exec "+code);
for(ZZCell current = code;
current != null;
current = current.s("d.2", 1)) {
// Find op.
ZZClangOp op = (ZZClangOp)funcs.get(current.getText());
p("OP: op");
if(op==null)
throw new ZZError("Unknown op '"+current.getText()+ "'");
op.exec(current, cursor, this);
}
}
/*
// XXX allow VAR for local variables!
public void exec(ZZCell c, ZZCell it, ZZCell[] params) {
// First, read the prototype and set the params
ZZCell[] p = c.readRank("d.1", 1, false);
if(p.length > params.length)
throw new ZZError("Insufficient params");
for(int i=0; i<p.length; i++) {
h.put(p[i].h("d.clone", -1), params[i]);
}
p("Call real exec");
exec(c.s("d.2", 1), ctxt, h);
}
*/
Hashtable coords = new Hashtable();
{
coords.put("X", new Integer(1));
coords.put("Y", new Integer(2));
coords.put("Z", new Integer(3));
coords.put("W", new Integer(4));
coords.put("HOME", new Integer(-1));
}
// Implementation of ZZClangContext interface.
public ZZCursor getCursor() { return cursor; }
public ZZCell getViewspex() { return viewspex; }
public Object getSpecial(Object param) { return specials.get(param); }
// XXX Use something like this to get other params as cells / cursors!
/*
if(par==null) {
String s = p[i].getText();
Integer ind = (Integer)coords.get(s);
if(ind!=null) {
if(ind.intValue()==-1)
par = p[i].getSpace().getHomeCell();
else {
par = ctxt.getDim(ind.intValue()).get();
}
}
}
*/
public ZZCell paramAsCell(ZZCell var) {
ZZCell par =
(ZZCell)symtab.get(
var.h("d.clone",-1));
if(par==null) return var;
ZZCell res = ZZCursorReal.get(par);
// If it has an expression on d.2, execute.
ZZCell oc = var.s("d.2", 1);
if(oc==null)
return res;
// save cursor
ZZCursor tmp = cursor;
try {
cursor = new ZZCursorVirtual(res);
exec(oc);
res = cursor.get();
} finally {
cursor = tmp;
}
return res;
}
public ZZCursor paramAsCursor(ZZCell var) {
ZZCell par =
(ZZCell)symtab.get(
var.h("d.clone",-1));
if(par==null) return null;
ZZCell oc = var.s("d.2", 1);
if(oc != null)
throw new ZZError("Can't have a cursor with an expression");
return new ZZCursorReal(par);
}
public void execParam(ZZCell param) {
exec(param);
}
Hashtable funcs = new Hashtable();
/** WILL BE DEPRECATED. */
public void addOp(ZZClangOp op) {
p("Addop "+op.name());
funcs.put(op.name(), op);
}
// public void
}

View File

@@ -0,0 +1,95 @@
/*
ZZClangContext.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.clang;
import org.gzigzag.*;
import java.util.*;
/** A context object for Clang.
* Mostly corresponds to a viewspecs: has a viewCell,
* a cursor and other stuff.
*
* <p>
* However, may also be cloned with a new cursor cell as a subcontext.
* The subcontext is then totally independent of the parent, but starts
* out with the same values if null.
* NOT YET FINISHED!
* <p>
* NOTE!!!!!!! getViewCell's viewcell may not be up to date with the ZZCursor
* operations, if code is compiled to pure java for speed. Sync op?
*/
public interface ZZClangContext {
String rcsid = "$Id: ZZClangContext.java,v 1.3 2000/09/19 10:31:58 ajk Exp $";
/** Obtain a reference to the main cursor of this context.
* Ideally,
* <pre>
* clangcontext.getCursor() == ZZCursorReal(clangcontext.getViewcell())
* </pre>
* but this may be optimized away, especially for subcontexts, since
* the cursor is used a *lot*.
*/
ZZCursor getCursor();
/** Obtain a reference to the main viewcell of this context.
*/
ZZCell getViewspex();
/** Get a ZZCell that gives the cell that the given parameter
* ultimately refers to. For example, the parameter could be
* a variable in which case this routine returns the cell that
* the variable is set to. Alternatively, the parameter could
* have an expression using cells in which case the result of
* the evaluated expression is given.
* For instance, if the given param is
* @param param The cell that represents the parameter in the clang script.
* @return The actual cell that the given parameter means in this
* context.
*/
ZZCell paramAsCell(ZZCell param);
/** Get a cursor that represents the variable in the given parameter.
* @param param As for paramAsCell above.
* @return A cursor representing the given variable, or null
* if the param is not a variable.
*/
ZZCursor paramAsCursor(ZZCell param);
/** Execute the given parameter cell.
* Used to implement routines such as if, case and loops.
* The code given is executed as if on the main level, so all changes
* to the cursor are reflected back after the code.
* @param param As for paramAsCell above.
*/
void execParam(ZZCell param);
/** Break encapsulation.
* Obtain a special context thingy given to the implementation.
* Nasty optimization hack.
* XXX This should later be done with transient cells.
*/
Object getSpecial(Object which);
// ZZClangContext getSubcontext(ZZCell startCursor);
}

View File

@@ -0,0 +1,56 @@
/*
ZZClangContextReal.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.clang;
import org.gzigzag.*;
class EUNRNBRRV {
}
/** A real viewspecs -context object for Clang.
*/
/*
public class ZZClangContextReal implements ZZClangContext {
String rcsid = "$Id: ZZClangContextReal.java,v 1.5 2000/10/18 14:35:31 tjl Exp $";
ZZCell viewCell;
ZZClangContextReal(ZZCell vc) { viewCell = vc; }
public ZZCursor getCursor() { return new ZZCursorReal(viewCell); }
public String getDimString(int n) { return getDim(n).get().getText(); }
public ZZCursor getDim(int n) {
ZZCell d = viewCell;
for(int i=0; i<n && d!=null; d=d.s("d.dims", 1)) {}
if(d==null) return null;
return new ZZCursorReal(d);
}
public ZZCell getViewcell() { return viewCell; }
public ZZClangContext getSubcontext(ZZCell startCursor) {
return new ZZClangContextVirtual(this, startCursor);
}
}
*/

View File

@@ -0,0 +1,60 @@
/*
ZZClangContextVirtual.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.clang;
import org.gzigzag.*;
class EKMVLSVMEVSE {
}
/** A virtual viewspecs -context object for Clang. The main cursor
* is virtualized, the others are still global - XXX IS THIS RIGHT?
*/
/*
public class ZZClangContextVirtual implements ZZClangContext {
String rcsid = "$Id: ZZClangContextVirtual.java,v 1.4 2000/09/19 10:31:58 ajk Exp $";
ZZCursor cursor;
ZZClangContext parent;
ZZClangContextVirtual(ZZClangContext orig, ZZCell start) {
cursor = new ZZCursorVirtual(start);
parent = orig;
}
public ZZCursor getCursor() { return cursor; }
public String getDimString(int n) { return getDim(n).get().getText(); }
public ZZCursor getDim(int n) {
return parent.getDim(n);
}
public ZZCell getViewcell() { return null; }
public ZZClangContext getSubcontext(ZZCell startCursor) {
return new ZZClangContextVirtual(this, startCursor);
}
}
*/

59
Java/clang/ZZClangOp.java Normal file
View File

@@ -0,0 +1,59 @@
/*
ZZClangOp.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.clang;
import org.gzigzag.*;
import java.util.*;
/** A primitive operation for Clang.
* NOT YET FINISHED!
*/
public interface ZZClangOp {
String rcsid = "$Id: ZZClangOp.java,v 1.3 2000/09/19 10:31:58 ajk Exp $";
/** Execute the operation.
* The op is to start at cell c and go poswards on d.1 to find
* its parameters, with which it can call the ctxt object to find
* out the values.
* <p>
* The cursor crs may or may not be the actual cursor of the viewcell
* of the context: it may also be given negwards on d.1 from the object
* to allow ops to act on other cursors easily.
* @param c The cell that made this routine to be called.
* @param crs The main cursor
* @param ctxt The context of the operation.
* @return The it parameter for the next op.
*/
void exec(ZZCell c, ZZCursor crs, ZZClangContext ctxt);
/* Be mentally prepared for something like
*
* void compile(ZZClangCompiler clc, ZZCell c, ZZClangContext ctxt);
*
* to appear in the future.
*/
/** Return the name of this operation.
*/
String name();
}

144
Java/clang/ZZClangOps.pl Normal file
View File

@@ -0,0 +1,144 @@
#
# $Id: ZZClangOps.pl,v 1.5 2000/11/03 08:25:16 ajk Exp $
#
# Generate ZZClangOps.java for clang operations
$file = $ARGV[0];
$file = "ZZClangOps.java" if ($file eq "");
open F, ">$file" or die "Can't open file $file";
$rcsid = '$Id: ZZClangOps.pl,v 1.5 2000/11/03 08:25:16 ajk Exp $';
print F <<END;
/*
ZZClangOps.java
*
* GENERATED USING ZZClangObs.pl - DO NOT MODIFY THE JAVA CODE:
* YOUR CHANGES WILL BE OVERWRITTEN. MODIFY THE GENERATING
* PERL CODE IN ZZClangObs.pl instead.
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.clang;
import org.gzigzag.*;
import java.util.*;
/** Some primitive operations for Clang.
*/
public class ZZClangOps {
public static final String rcsid = "$rcsid";
public static final boolean dbg = false;
static final void p(String s) { if(dbg) System.out.println(s); }
/** Convert a cell to a dimension. */
static final String d(ZZCell c) { return c.getText(); }
END
$exec = " public void exec(ZZCell c, ZZCursor crs, ZZClangContext ctxt) ";
for(
#
# First, operators without side effects, operating just on the given
# cursor
#
[ Step,
2,
q{dim n},
q%
int n = Integer.parseInt($P(n).getText());
String dim = d($P(dim));
ZZCell it = $CURS.get();
it = it.s(dim, n);
$CURS.set(it);
%,
q{steps the cursor n steps (positive or negative) along a dim. },
],
[ Set,
2,
q{cell},
q%
$CURS.set($P(cell));
%,
q{set the cursor cursor to point to cell cell. },
],
# [ Deref,
# 0,
# q{},
# q%
# ZZCursor curs = ctxt.getCursor();
# curs.set(ZZDefaultSpace.getCursor(curs.get()));
# %,
# q{dereference a cursor. },
# ],
[ Connect,
3,
q{cell0 dim cell1},
q%
$P(cell0).connect(d($P(dim)), $P(cell1));
%,
q{connect two cells along dim. },
],
[ Connect2,
3,
q{dim cell1},
q%
$CURS.get().connect(d($P(dim)), $P(cell1));
%,
q{connect the cursor to cells along dim. },
],
) {
$lcname = lc $_->[0];
@pars = split ' ',$_->[2];
$npars = @pars;
my $ind = 1;
my %pars = map {($_ => $ind++)} @pars;
$_->[3] =~ s/\$P\((.*?)\)/(ctxt.paramAsCell(c.s("d.1", $pars{$1}, null)))/g;
$_->[3] =~ s/\$CURS/ crs /g;
$classes .= "
/** $_->[2] */
static class $_->[0] implements ZZClangOp {
public String name() { return \"$lcname\"; }
$exec
{
/* if(params.length != $npars)
throw new ZZError(\"Wrong number of params for $lcname, got \"+
params.length+\", was expecting $npars\");
*/
$_->[3]
}
}
";
push @arrays, " new $_->[0]() ";
}
$ja = join ",\n",@arrays;
print F <<END;
$classes
public static ZZClangOp[] getOps1() {
return new ZZClangOp[] {
$ja
};
}
}
END

99
Java/clang/flowing/B.java Normal file
View File

@@ -0,0 +1,99 @@
/*
B.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
import java.util.*;
/*
* The "B" (Binary Logic) primitive set for Flowing clang.
*/
public class B extends PrimitiveSet {
public static final String rcsid = "$Id: B.java,v 1.2 2000/10/13 13:52:24 bfallenstein Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
public Primitive get(String id) {
if(id.equals("AND") || id.equals("&"))
return new And();
if(id.equals("OR") || id.equals("|"))
return new Or();
if(id.equals("NOT") || id.equals("!"))
return new Not();
if(id.equals("XOR"))
return new Xor();
if(id.equals("NOTNULL"))
return new NotNull();
return null;
}
static public class And extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
boolean res = params.b(0);
for(int i=1; i<params.len(); i++) {
res = res && params.b(i);
}
return new Data(new Boolean(res));
}
}
static public class Or extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
boolean res = params.b(0);
for(int i=1; i<params.len(); i++) {
res = res || params.b(i);
}
return new Data(new Boolean(res));
}
}
static public class Not extends Primitive {
protected Data execute(Data params) {
count(params, 1);
boolean res = !params.b(0);
return new Data(new Boolean(res));
}
}
static public class Xor extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
boolean res = params.b(0);
for(int i=1; i<params.len(); i++) {
boolean b = params.b(i);
res = (!res && b) || (res && !b);
}
return new Data(new Boolean(res));
}
}
static public class NotNull extends Primitive {
protected Data execute(Data params) {
count(params, 1);
boolean res = params.o(0) != null;
return new Data(new Boolean(res));
}
}
}

115
Java/clang/flowing/C.java Normal file
View File

@@ -0,0 +1,115 @@
/*
C.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
import java.util.*;
/*
* The "C" (Number Crunching) primitive set for Flowing clang.
*/
public class C extends PrimitiveSet {
public static final String rcsid = "$Id: C.java,v 1.2 2000/10/13 13:52:24 bfallenstein Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
public Primitive get(String id) {
if(id.equals("+"))
return new Plus();
if(id.equals("-"))
return new Minus();
if(id.equals("*"))
return new Mul();
if(id.equals("/"))
return new Div();
if(id.equals("="))
return new Compare(0);
if(id.equals(">"))
return new Compare(-2);
if(id.equals("<"))
return new Compare(+2);
if(id.equals(">="))
return new Compare(-1);
if(id.equals("<="))
return new Compare(+1);
return null;
}
static public class Plus extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
int res = params.i(0);
for(int i=1; i<params.len(); i++) {
res += params.i(i);
}
return new Data(new Integer(res));
}
}
static public class Minus extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
int res = params.i(0);
for(int i=1; i<params.len(); i++) {
res -= params.i(i);
}
return new Data(new Integer(res));
}
}
static public class Mul extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
int res = params.i(0);
for(int i=1; i<params.len(); i++) {
res *= params.i(i);
}
return new Data(new Integer(res));
}
}
static public class Div extends Primitive {
protected Data execute(Data params) {
count(params, 2);
return new Data(new Integer(params.i(0) / params.i(1)));
}
}
static public class Compare extends Primitive {
int weight;
public Compare(int weight) { this.weight = weight; }
protected Data execute(Data params) {
count(params, 2);
int i0, i1;
if(weight > 0) { i0 = params.i(0); i1 = params.i(1); }
else { i0 = params.i(1); i1 = params.i(0); }
boolean res;
if(weight==0) res = i0 == i1;
else if(weight*weight == 4) res = i0 < i1;
else res = i0 <= i1;
return new Data(new Boolean(res));
}
}
}

View File

@@ -0,0 +1,260 @@
/*
Flowing.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.module;
import org.gzigzag.*;
import org.gzigzag.flowing.*;
import java.awt.*;
/** A module interface to Flowing Clang.
*/
public class Flowing {
public static final String rcsid = "$Id: Flowing.java,v 1.7.8.1 2001/08/16 13:40:44 bfallenstein Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
static final void createdebugger(ZZCell debug) {
debug.getSpace().getHomeCell().h("d.2", 1).insert("d.2", 1, debug);
ZZCell normal = debug.N("d.1", 1);
normal.setText("Debug mode");
ZZCell edit = normal.N("d.1", 1);
edit.setText("Text edit mode");
ZZCell dim0 = edit.N("d.1", 1);
dim0.setText("d..flowing-frame");
dim0.N("d.2", 1).setText("d.2");
dim0.N("d.2", 1).setText("d..flowing-data");
ZZCell dim1 = dim0.N("d.1", 1);
dim1.setText("d.1");
dim1.N("d.2", 1).setText("d.clone");
dim1.N("d.2", 1).setText("d.xeq");
// EDIT MODE
ZZCell c = edit.N("d.2", 1);
c.setText("Tab");
c.N("d.1", 1).setText("SWITCHOFFCURSMODE");
normal.insert("d.3", 1, c.s("d.1"));
c = c.N("d.2", 1);
c.setText("Esc");
c.N("d.1", 1).setText("SWITCHOFFCURSMODE");
normal.insert("d.3", 1, c.s("d.1"));
c = c.N("d.2", 1);
ZZDefaultSpace.findOnSystemlist(c.getSpace(), "Bindings", false)
.s("d.1", 2).s("d.2", 1).insert("d.3", -1, c);
// NORMAL MODE
c = normal.N("d.2", 1);
c.setText(" ");
c.N("d.1", 1).setText("Flowing.D-STEP");
c = c.N("d.2", 1);
c.setText("Enter");
c.N("d.1", 1).setText("Flowing.D-JUMP");
c = c.N("d.2", 1);
c.setText("Backspace");
c.N("d.1", 1).setText("UNDO");
c = c.N("d.2", 1);
c.setText("Alt-Backspace");
c.N("d.1", 1).setText("REDO");
c = c.N("d.2", 1);
c.setText("q");
c.N("d.1", 1).setText("QUIT");
c = c.N("d.2", 1);
c.setText("Tab");
c.N("d.1", 1).setText("SWITCHCURSMODE");
edit.insert("d.3", 1, c.s("d.1"));
c = c.N("d.2", 1);
c.setText("Left");
c.N("d.1", 1).setText("CRSR X-");
c = c.N("d.2", 1);
c.setText("Right");
c.N("d.1", 1).setText("CRSR X+");
c = c.N("d.2", 1);
c.setText("Up");
c.N("d.1", 1).setText("CRSR Y-");
c = c.N("d.2", 1);
c.setText("Down");
c.N("d.1", 1).setText("CRSR Y+");
c = c.N("d.2", 1);
c.setText("j");
c.N("d.1", 1).setText("CRSR X-");
c = c.N("d.2", 1);
c.setText("l");
c.N("d.1", 1).setText("CRSR X+");
c = c.N("d.2", 1);
c.setText("i");
c.N("d.1", 1).setText("CRSR Y-");
c = c.N("d.2", 1);
c.setText(",");
c.N("d.1", 1).setText("CRSR Y+");
c = c.N("d.2", 1);
c.setText("K");
c.N("d.1", 1).setText("CRSR Z-");
c = c.N("d.2", 1);
c.setText("k");
c.N("d.1", 1).setText("CRSR Z+");
c = c.N("d.2", 1);
c.setText("s");
c.N("d.1", 1).setText("CRSR CX-");
c = c.N("d.2", 1);
c.setText("f");
c.N("d.1", 1).setText("CRSR CX+");
c = c.N("d.2", 1);
c.setText("e");
c.N("d.1", 1).setText("CRSR CY-");
c = c.N("d.2", 1);
c.setText("c");
c.N("d.1", 1).setText("CRSR CY+");
c = c.N("d.2", 1);
c.setText("D");
c.N("d.1", 1).setText("CRSR CZ-");
c = c.N("d.2", 1);
c.setText("d");
c.N("d.1", 1).setText("CRSR CZ+");
}
static public ZZModule module = new ZZModule() {
public void action(String id,
ZZCell code,
ZZCell target,
ZZView view, ZZView cview, String key, Point pt, ZZScene xi) {
if(id.equals("BIND") || id.equals("BINDREAL") ||
id.equals("RUN") || id.equals("RUNREAL")) {
ZZCell prog;
if(id.indexOf("BIND") > -1) {
prog = code.s("d.2");
if(prog == null)
throw new ZZError("Flowing.BIND without program");
prog = prog.h("d.clone", -1, true);
if(prog == null)
throw new ZZError("Flowing.BIND prog must be cloned");
} else {
prog = target;
if(prog == null)
prog = ZZCursorReal.get(view.getViewcell());
}
boolean real = id.indexOf("REAL") > 0;
FlowingClang.run(prog, new Data(cview.getViewcell(),
view.getViewcell(),
key), real);
} else if(id.equals("DEBUG")) {
p("Create debugger:");
ZZCell prog =
ZZCursorReal.get(view.getViewcell()).getRootclone();
ZZCell debug = ZZDefaultSpace.findOnSystemlist(
view.getViewcell().getSpace(), "Flowing.Debugger", true);
if(debug.s("d.1") == null) createdebugger(debug);
ZZCell views = ZZDefaultSpace.findOnSystemlist(
view.getViewcell().getSpace(), "Views", false).s("d.1");
p("Start program.");
Data d = new Data(cview.getViewcell(),
view.getViewcell(), key);
ZZCell frame = FlowingClang.start(prog, d);
p("Create windows.");
ZZCell frameview = ZZDefaultSpace.newToplevelView(
prog.getSpace(), "Flowing stack frame",
20, 20, 400, 500,
views, debug.s("d.1"),
frame, null,
debug.s("d.1", 3).readRank("d.2", 1, true),
ZZUtil.perturb(Color.blue.brighter()));
ZZCursorReal.setColor(frameview, Color.blue.darker());
ZZCell progview = ZZDefaultSpace.newToplevelView(
prog.getSpace(), "Flowing program",
440, 20, 400, 500,
views, debug.s("d.1"),
ZZCursorReal.get(frame), null,
debug.s("d.1", 4).readRank("d.2", 1, true),
ZZUtil.perturb(Color.red.brighter()));
ZZCursorReal.setColor(progview, Color.red.darker());
p("Connecting windows.");
frameview.connect("d.ctrlview", 1, progview);
ZZCell framecur = progview.N("d.2", 1);
framecur.setText("Flowing.StackFrame");
ZZCursorReal.set(framecur, frame);
p("Update windows.");
ZZWindows.update();
} else if(id.indexOf("D-") > -1) {
ZZCell framecur = ZZDefaultSpace.findInheritableParam(
view.getViewcell(), "Flowing.StackFrame");
if(framecur == null)
throw new ZZError("Flowing Clang debugging command on a "
+ "non-debug view");
ZZCell frame = ZZCursorReal.get(framecur);
StackFrameReal sf = new StackFrameReal(frame), nsf;
if(id.equals("D-STEP"))
nsf = (StackFrameReal)FlowingClang.step(sf, true);
else if(id.equals("D-JUMP")) {
nsf = (StackFrameReal)FlowingClang.jump(sf);
} else
throw new ZZError("Unknown Flowing.XXX code");
if(nsf != null) {
if(nsf != sf) {
ZZCursorReal.set(framecur, nsf.main);
ZZCursorReal.set(cview.getViewcell(), nsf.main);
}
ZZCursorReal.set(view.getViewcell(), nsf.getPos());
} else {
view.getViewcell().h("d.1").delete();
cview.getViewcell().h("d.1").delete();
ZZWindows.update();
ZZCursorReal.delete(view.getViewcell());
ZZCursorReal.delete(cview.getViewcell());
}
} else
throw new ZZError("Unknown Flowing.XXX code");
}
};
}

View File

@@ -0,0 +1,219 @@
/*
FlowingClang.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written for Heraclitus Clang by Tuomas Lukka
* Adaption for Flowing Clang by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
import java.util.*;
/** A simple (imperative) clang passing values dataflow-like.
* Kind of a cross between a dataflow ("graphical") programming language and
* an assembler. On the one hand we have sequences of instructions, each with
* zero or more in- and output variables; variables are set and read;
* jmp-style branching.
* <p>
* However, variables can only be set in one place
* * XXX not any more -- explain XXX
* (they can be set more than
* once by executing the same instruction multiple times). All references to
* the variable are clones; the original (rootclone) is the place where the
* variable is set. This means when you view d.clone, you can see how the
* data "flows" through the program (hence the language's name).
* <p>
* Execution strictly proceeds on d.xeq. There are no blocks: when there's
* nothing more on d.xeq, the call returns. Until then, the return values
* (if any) have to be set. Primitives are called by putting their ID into
* a cell, putting the input values negwards and the variables to take the
* output poswards on d.1 from it. (XXX use a less string-based behavior?)
* Subroutines are called by cloning their maincell. Parameter passing and
* results work the same as with primitives.
* <p>
* Primitives and subroutines are passed cells, and return cells. If in
* the "input zone" of for a cell or subroutine (neg on d.1) there's a
* cell which isn't cloned (and thus cannot be read as a variable, because
* it cannot have a value assigned), it is taken as literal and passed to
* the subroutine. Cloned cells (variables) are dereferenced in the stack frame
* for the current routine, which means they have to be set. What's passed to
* the subroutine or primitive is the cell which was formerly assigned to the
* variable, not the variable cell itself.
* <p>
* To create a subroutine, create a non-clone cell with the routine's name.
* (Later, you clone this into other code.) Put its input variables
* negwards on d.1. Then put the subroutine on d.xeq. When you're done,
* clone the result variable(s) from the point where you set them in the
* routine to the right of the routine's maincell.
* <p>
* To branch, create a cell with a question mark ("?") in a strip, put a value
* (variable or literal, but the latter doesn't make too much sense) in its
* "input zone" (neg on d.1), which contains a boolean value ("true" or "false"
* -- everything else is an error), and put a cell in what would usually be
* its "output zone" (pos on d.1). From there, hang a new strip. If the
* condition is true, execution proceeds on the new strip; if the condition
* is false, execution proceeds on the old strip.
* <p>
* Blank cells in a strip are usually just ignored. However, a blank cell at
* the end of a strip works like a goto statement: if there's something
* poswards on d.1, the execution pointer jumps to the endcell on d.1
* and proceeds execution on d.xeq. This is used when, after branching
* because of some condition, you want to unify your strips (i.e., you
* make a blank cell on the end of each one, connect them on d.1, and
* proceed programming on the posmost cell on d.1).
* <p>
* Loops can be constructed by looping ZZ cells. You should take care to
* have a condition somewhere in the loop which can turn true. (XXX break
* infinite loops somehow!)
* <p>
* The language only uses the d.1, d.xeq, and d.clone dimensions. The
* combinations d.1/d.xeq, d.1/d.clone, d.1/d.xeq/d.clone, d.1/d.clone/d.xeq
* all make sense to view, especially in the row and column views.
*/
public class FlowingClang {
public static final String rcsid = "$Id: FlowingClang.java,v 1.7 2000/11/16 20:33:13 bfallenstein Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
public static final String dim = "d.xeq";
static Hashtable pss = new Hashtable();
static PrimitiveSet findPrimitiveSet(String set) {
Object o = pss.get(set);
if(o != null) return (PrimitiveSet)o;
try {
o = Class.forName("org.gzigzag.flowing."+set).newInstance();
} catch(Exception e) {
ZZLogger.exc(e);
return null;
}
if(!(o instanceof PrimitiveSet)) return null;
pss.put(set, o);
return (PrimitiveSet)o;
}
static Hashtable prims = new Hashtable();
static Primitive findPrimitive(String s) {
Object o = prims.get(s);
if(o != null) return (Primitive)o;
int ind = s.indexOf(".");
if(ind < 0) return null;
String set = s.substring(0, ind);
String id = s.substring(ind+1);
PrimitiveSet ps = findPrimitiveSet(set);
Primitive prim = ps.get(id);
if(prim != null)
prims.put(id, prim);
return prim;
}
public static Data run(ZZCell c, Data d) { return run(c, d, false); }
public static Data run(ZZCell c0, Data d, boolean real) {
p("Flowing Clang run: "+c0.getText());
ZZCell c = c0.getRootclone();
StackFrame frame;
if(real) frame = new StackFrameReal(c.getSpace());
else frame = new StackFrameVirtual();
frame.setPos(c);
frame.put(c, d, -1);
StackFrame f = frame;
try {
while(f != null) f = step(f, true);
} catch(Throwable t) {
ZZLogger.exc(t, "Exception in Flowing Clang run. ");
return null;
}
return frame.get(c, +1);
}
public static ZZCell start(ZZCell c0, Data d) {
StackFrameReal frame = new StackFrameReal(c0.getSpace());
frame.setPos(c0);
frame.put(c0, d, -1);
return frame.main;
}
public static StackFrame jump(StackFrame frame) {
StackFrame f = frame;
do {
f = step(f, true);
} while(frame.parentof(f));
return f;
}
public static StackFrame step(StackFrame frame, boolean throwerrs) {
ZZCell cur = frame.getPos().s(dim);
if(cur == null) { // RETURN
p("Flowing clang return step");
StackFrame ret = frame.ret();
if(ret == null) return null;
ZZCell rpos = ret.getPos();
ret.put(rpos, frame.get(rpos.getRootclone(), +1), +1);
frame.delete();
return ret;
}
p("Flowing Clang step: "+cur.getText()+" ("+cur.getID()+")");
ZZCell root = cur.h("d.clone", -1, true);
String s = cur.getText();
if(s.equals("") && root==null) { // GOTO
frame.setPos(cur.h("d.1", 1));
return frame;
}
Data d = frame.get(cur, -1); // IF
if(s.equals("?")) {
Primitive.count(d, 1);
if(d.b(0)) cur = cur.h("d.1", 1);
frame.setPos(cur);
return frame;
}
Primitive p = findPrimitive(s);
if(p != null) { // PRIMITIVE
try {
d = p.execute(d, cur.getSpace());
} catch(ZZError e) {
if(throwerrs) throw e;
ZZLogger.exc(e, "Exception in Flowing Clang step. ");
return frame;
}
frame.put(cur, d, +1);
frame.setPos(cur);
return frame;
} else if(root != null) { // FUNCTION CALL
frame.setPos(cur);
StackFrame called = frame.call();
called.put(root, d, -1);
called.setPos(root);
return called;
} else { // UNKNOWN
if(throwerrs)
throw new ZZError("Not a primitive at " + cur.getID() + ": " +
cur.getText());
ZZLogger.log("Flowing Clang step cannot be executed: Not a " +
"primitive at " + cur.getID() + ": " + cur.getText());
return frame;
}
}
}

View File

@@ -0,0 +1,64 @@
/*
Primitive.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
/** An Flowing Clang primitive.
*/
public abstract class Primitive {
String rcsid = "$Id: Primitive.java,v 1.3 2000/10/13 13:52:24 bfallenstein Exp $";
// In subclasses, override one of the following two:
protected Data execute(Data params) { return null; }
public Data execute(Data params, ZZSpace space) {
return execute(params);
}
static public void count(Data pars, int n) {
if(n != pars.len()) miscount();
}
static public void miscount() { throw new ZZError("Wrong parameter count"); }
/** Read a direction or step count from a parameter array.
* If the pos is out of the bounds of the array, +1 is assumed, so that
* the dir can optionally be left out.
* <p>
* (This, obviously, is just a convenience function.)
*/
static int readsteps(Data pars, int pos) {
if(pos < pars.len()) {
String stepstr = pars.s(pos);
if(stepstr.equals("+"))
return 1;
else if(stepstr.equals("-"))
return -1;
else
return pars.i(pos);
} else {
return 1;
}
}
}

View File

@@ -0,0 +1,34 @@
/*
PrimitiveSet.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka (for Heraclitus Clang)
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
/** A set of Flowing Clang primitives.
*/
public abstract class PrimitiveSet {
String rcsid = "$Id: PrimitiveSet.java,v 1.3 2000/10/13 13:52:24 bfallenstein Exp $";
public abstract Primitive get(String id);
}

View File

@@ -0,0 +1,117 @@
/*
StackFrame.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
/** A stack frame for Flowing Clang.
*/
public abstract class StackFrame {
String rcsid = "$Id: StackFrame.java,v 1.5 2000/11/16 20:33:13 bfallenstein Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
// METHODS TO BE IMPLEMENTED BY SUBCLASSES
/** Get the value associated with a cell. */
public abstract Object getSingle(ZZCell c);
/** Set the value associated with a cell.
* @param d The Data object in which the value to be set is found.
* @param at The value's position in the Data object.
*/
public abstract void setSingle(ZZCell c, Data d, int at);
/** Get position of instruction pointer. */
public abstract ZZCell getPos();
/** Set position of instruction pointer. */
public abstract void setPos(ZZCell c);
/** Create a lower-level stackframe. */
public abstract StackFrame call();
/** Return true if this stack frame is parent of given stack frame. */
public abstract boolean parentof(StackFrame sf);
/** Return to the next higher stackframe (disconnects this (lower) frame).
* Also sets instruction pointer to null, but keeps the values associated
* with cells, so that the returned data can be read. Call delete() when
* you're done with the values.
*/
public abstract StackFrame ret();
/** Delete the cells associated with the stackframe, if any.
* This renders the values of a structural stackframe inaccessible.
*/
public void delete() {}
// METHODS StackFrame PROVIDES
private static final ZZCell[] reverse(ZZCell[] arr) {
ZZCell[] res = new ZZCell[arr.length];
for(int i=0; i<arr.length; i++) res[arr.length-i-1] = arr[i];
return res;
}
public Data get(ZZCell c, int dir) {
p("Start getting for "+c.getText()+" ("+c.getID()+")");
ZZCell[] cells = c.readRank("d.1", dir, false);
if(dir<0) cells = reverse(cells);
Object[] result = new Object[cells.length];
p("Getting "+cells.length+" values");
for(int i=0; i<cells.length; i++) {
if(cells[i].s("d.clone", -1) != null) {
p("Getting: " + cells[i].getText());
result[i] = getSingle(cells[i].getRootclone());
if(result[i] == null)
throw new ZZError("Value for " + cells[i].getText() +
" not yet set");
} else {
p("Getting literal");
result[i] = cells[i];
}
p("Got "+result[i]);
}
p("Finish getting");
return new Data(result);
}
public void put(ZZCell c, Data params, int dir) {
p("Start putting for "+c.getText()+" ("+c.getID()+")");
Data pars = params != null ? params : new Data();
ZZCell[] cells = c.readRank("d.1", dir, false);
if(dir<0) cells = reverse(cells);
p("Putting "+cells.length+" values");
if(pars.len() < cells.length)
throw new ZZError("Pars: " + pars.len() +
" < cells: " + cells.length);
for(int i=0; i<cells.length; i++) {
p("Putting: "+pars.s(i)+" into: "+cells[i].getText());
setSingle(cells[i].getRootclone(), pars, i);
}
p("Finish putting");
}
}

View File

@@ -0,0 +1,97 @@
/*
StackFrameReal.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
/** A structural implementation for Flowing Clang stack frames.
*/
public class StackFrameReal extends StackFrame {
String rcsid = "$Id: StackFrameReal.java,v 1.6 2000/11/16 20:33:13 bfallenstein Exp $";
public ZZCell main;
public StackFrameReal(ZZSpace sp) { main = sp.getHomeCell().N(); }
public StackFrameReal(ZZCell main) { this.main = main; }
public StackFrameReal(StackFrameReal caller) {
this(caller.main.N("d.2", 1));
}
public Object getSingle(ZZCell c) {
ZZCell where = main.intersect("d..flowing-frame", 1,
c, "d..flowing-data", 1);
if(where == null) return null;
ZZCell acc = ZZCursorReal.get(where);
if(acc != null) return acc;
else return where;
}
public void setSingle(ZZCell c, Data d, int at) {
ZZCell where = main.intersect("d..flowing-frame", 1,
c, "d..flowing-data", 1);
if(where == null) {
where = main.N("d..flowing-frame", 1);
c.insert("d..flowing-data", 1, where);
}
if(d.o(at) instanceof ZZCell)
ZZCursorReal.setcursor(where, d.c(at));
else {
ZZCursorReal.setcursor(where, null);
where.setText(d.s(at));
}
}
public ZZCell getPos() { return ZZCursorReal.get(main.h("d.2")); }
public void setPos(ZZCell c) { ZZCursorReal.setcursor(main, c); }
public StackFrame call() { return new StackFrameReal(this); }
public boolean parentof(StackFrame sf) {
if(!(sf instanceof StackFrameReal)) return false;
ZZCell find = ((StackFrameReal)sf).main;
for(ZZCell c = main; c != null && c != main; c = c.s("d.2")) {
if(c.equals(find)) return true;
}
return false;
}
public StackFrame ret() {
ZZCell caller = main.s("d.2", -1);
setPos(null);
if(caller != null) {
caller.disconnect("d.2", +1);
return new StackFrameReal(caller);
} else
return null;
}
public void delete() {
ZZCell pos = main;
while(pos != null) {
ZZCell nextrow = pos.s("d.2");
while(pos != null) {
ZZCell next = pos.s("d..flowing-frame");
pos.delete();
pos = next;
}
pos = nextrow;
}
}
public boolean equals(StackFrameReal f) { return main.equals(f.main); }
}

View File

@@ -0,0 +1,63 @@
/*
StackFrameVirtual.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
import java.util.*;
/** A virtual implementation for Flowing Clang stack frames.
*/
public class StackFrameVirtual extends StackFrame {
String rcsid = "$Id: StackFrameVirtual.java,v 1.5 2000/11/16 20:33:13 bfallenstein Exp $";
Hashtable values = new Hashtable();
static final Object NULL = new Object();
StackFrameVirtual caller = null, called = null;
ZZCell pos;
public StackFrameVirtual() {}
public StackFrameVirtual(StackFrameVirtual frame) { caller = frame; }
public Object getSingle(ZZCell c) {
Object res = values.get(c);
return res == NULL ? null : res;
}
public void setSingle(ZZCell c, Data d, int at) {
values.put(c, d.o(at) == null ? NULL : d.o(at));
}
public ZZCell getPos() { return called == null ? pos : called.getPos(); }
public void setPos(ZZCell c) { pos = c; }
public StackFrame call() { return called = new StackFrameVirtual(this); }
public boolean parentof(StackFrame sf) {
if(called == null) return false;
return (sf == called) || (called.parentof(sf));
}
public StackFrame ret() {
pos = null;
if(caller != null) caller.called = null;
return caller;
}
public boolean equals(StackFrameVirtual frame) { return this == frame; }
}

229
Java/clang/flowing/T.java Normal file
View File

@@ -0,0 +1,229 @@
/*
T.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
import java.util.*;
/*
* The "T" (Space Transformation) primitive set for Flowing clang.
*/
public class T extends PrimitiveSet {
public static final String rcsid = "$Id: T.java,v 1.4 2000/10/18 14:35:31 tjl Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
public Primitive get(String id) {
if(id.equals("GET"))
return new Get();
if(id.equals("SET"))
return new Set();
if(id.equals("STEP") || id.equals("s"))
return new Step();
if(id.equals("END") || id.equals("h"))
return new End();
if(id.equals("FIND"))
return new Find();
if(id.equals("ORIGINAL"))
return new Original();
if(id.equals("CROSS")) // == intersect
return new Cross();
if(id.equals("HOMECELL"))
return new HomeCell();
if(id.equals("CONNECT"))
return new Connect();
if(id.equals("INSERT"))
return new Insert();
if(id.equals("DISCONNECT"))
return new Disconnect();
if(id.equals("HOP"))
return new Hop();
if(id.equals("NEW") || id.equals("N"))
return new New();
if(id.equals("CLONE"))
return new Clone();
if(id.equals("DELETE"))
return new Delete();
if(id.equals("SAME"))
return new Same();
return null;
}
static public class Get extends Primitive {
protected Data execute(Data params) {
count(params, 1);
return new Data(ZZCursorReal.get(params.c(0)));
}
}
static public class Set extends Primitive {
protected Data execute(Data params) {
count(params, 2);
ZZCursorReal.set(params.c(0), params.c(1));
return null;
}
}
static public class Step extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2 || params.len() > 3) miscount();
int steps = readsteps(params, 2);
String dim = params.s(1);
return new Data(params.c(0).s(dim, steps));
}
}
static public class End extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2 || params.len() > 3) miscount();
String dim = params.s(1);
return new Data(params.c(0).h(dim, readsteps(params, 2)));
}
}
static public class Find extends Primitive {
protected Data execute(Data params) {
if(params.len() < 3 || params.len() > 4) miscount();
String dim = params.s(1), txt; int dir;
if(params.len() == 3) {
dir = 1;
txt = params.s(2);
} else {
dir = readsteps(params, 2);
txt = params.s(3);
}
ZZCell c = params.c(0).findText(dim, dir, txt);
return new Data(c);
}
}
static public class Original extends Primitive {
protected Data execute(Data params) {
count(params, 1);
return new Data(params.c(0).getRootclone());
}
}
static public class Cross extends Primitive { // i.e., intersect
protected Data execute(Data params) {
if(params.len() != 4 && params.len() != 6) miscount();
ZZCell c0, c1;
String dim0, dim1;
int dir0, dir1;
if(params.len() == 4) {
c0 = params.c(0); c1 = params.c(2);
dim0 = params.s(1); dim1 = params.s(3);
dir0 = 1; dir1 = 1;
} else {
c0 = params.c(0); c1 = params.c(3);
dim0 = params.s(1); dim1 = params.s(4);
dir0 = readsteps(params, 2); dir1 = readsteps(params, 5);
}
p("CROSS: "+c0.getText()+" "+dim0+" "+dir0+" "+
c1.getText()+" "+dim1+" "+dir1);
return new Data(c0.intersect(dim0, dir0, c1, dim1, dir1));
}
}
static public class HomeCell extends Primitive {
public Data execute(Data params, ZZSpace space) {
count(params, 0);
return new Data(space.getHomeCell());
}
}
static public class Connect extends Primitive {
protected Data execute(Data params) {
if(params.len() < 3 || params.len() > 4) miscount();
if(params.len() == 3)
params.c(0).connect(params.s(1), params.c(2));
else
params.c(0).connect(params.s(1), readsteps(params, 2),
params.c(3));
return null;
}
}
static public class Insert extends Primitive {
protected Data execute(Data params) {
if(params.len() < 3 || params.len() > 4) miscount();
if(params.len() == 3)
params.c(0).insert(params.s(1), 1, params.c(2));
else
params.c(0).insert(params.s(1), readsteps(params, 2),
params.c(3));
return null;
}
}
static public class Disconnect extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2 || params.len() > 3) miscount();
params.c(0).disconnect(params.s(1), readsteps(params, 2));
return null;
}
}
static public class Hop extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2 || params.len() > 3) miscount();
params.c(0).hop(params.s(1), readsteps(params, 2));
return null;
}
}
static public class New extends Primitive {
public Data execute(Data params, ZZSpace space) {
if(params.len() == 1 || params.len() > 3) miscount();
if(params.len() == 0)
return new Data(space.getHomeCell().N());
else {
String dim = params.s(1);
return new Data(params.c(0).N(dim,readsteps(params, 2)));
}
}
}
static public class Clone extends Primitive {
protected Data execute(Data params) {
count(params, 1);
return new Data(params.c(0).zzclone());
}
}
static public class Delete extends Primitive {
protected Data execute(Data params) {
count(params, 1);
params.c(0).delete();
return null;
}
}
static public class Same extends Primitive {
protected Data execute(Data params) {
count(params, 1);
return params;
}
}
}

138
Java/clang/flowing/X.java Normal file
View File

@@ -0,0 +1,138 @@
/*
X.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Benjamin Fallenstein
*/
package org.gzigzag.flowing;
import org.gzigzag.*;
import java.util.*;
/*
* The "X" (Cell Text) primitive set for Flowing clang.
*/
public class X extends PrimitiveSet {
public static final String rcsid = "$Id: X.java,v 1.4 2000/10/20 15:16:54 bfallenstein Exp $";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
public Primitive get(String id) {
if(id.equals("SET"))
return new Set();
if(id.equals("+"))
return new Concat();
if(id.equals("="))
return new Equals();
if(id.equals("LEN"))
return new Len();
if(id.equals("PART"))
return new Part();
if(id.equals("FIND"))
return new Find();
if(id.equals("PUICOPY"))
return new PUICopy();
if(id.equals("PUIPASTE"))
return new PUIPaste();
if(id.equals("LOG"))
return new Log();
return null;
}
static public class Set extends Primitive {
protected Data execute(Data params) {
count(params, 2);
params.c(0).setText(params.s(1));
return null;
}
}
static public class Concat extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2) miscount();
String s = "";
for(int i=0; i<params.len(); i++) {
s = s + params.s(i);
}
return new Data(s);
}
}
static public class Equals extends Primitive {
protected Data execute(Data params) {
count(params, 2);
boolean res = params.s(0).equals(params.s(1));
return new Data(new Boolean(res));
}
}
static public class Len extends Primitive {
protected Data execute(Data params) {
count(params, 1);
return new Data(new Integer(params.s(0).length()));
}
}
static public class Part extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2 || params.len() > 3) miscount();
String txt = params.s(0); int start = params.i(1);
if(params.len() == 2) {
txt = txt.substring(start);
} else {
txt = txt.substring(start, params.i(2));
}
return new Data(txt);
}
}
static public class Find extends Primitive {
protected Data execute(Data params) {
if(params.len() < 2 || params.len() > 3) miscount();
String txt = params.s(0), sub = params.s(1);
int start;
if(params.len() == 3) start = params.i(2);
else start = 0;
return new Data(new Integer(txt.indexOf(sub, start)));
}
}
static public class PUICopy extends Primitive {
protected Data execute(Data params) {
count(params, 1);
ZZUtil.puiCopy(params.s(0));
return null;
}
}
static public class PUIPaste extends Primitive {
public Data execute(Data params, ZZSpace space) {
count(params, 0);
return new Data(ZZUtil.puiGetClipText());
}
}
static public class Log extends Primitive {
protected Data execute(Data params) {
count(params, 1);
ZZLogger.log(params.s(0));
return null;
}
}
}

View File

@@ -0,0 +1,108 @@
/*
HeraclitusClang.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.heraclitus;
import org.gzigzag.*;
import java.util.*;
/** A simple execution-based clang.
* Based on a cascading interpreter model, like #! in unix.
* Executables are given as parameters the cell they were referred
* to in (for structural params) and another cell, the parameter.
*/
public class HeraclitusClang {
public static final String rcsid = "$Id$";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
static Hashtable pss = new Hashtable();
static PrimitiveSet findPrimitiveSet(String set) {
Object o = pss.get(set);
if(o != null) return (PrimitiveSet)o;
try {
o = Class.forName("org.gzigzag.heraclitus."+set).newInstance();
} catch(Exception e) {
ZZLogger.exc(e);
return null;
}
if(!(o instanceof PrimitiveSet)) return null;
pss.put(set, o);
return (PrimitiveSet)o;
}
static Hashtable prims = new Hashtable();
static Primitive findPrimitive(ZZCell c) {
String s = c.getText();
Object o = prims.get(s);
if(o != null) return (Primitive)o;
int ind = s.indexOf(".");
if(ind < 0) return null;
String set = s.substring(0, ind);
String id = s.substring(ind+1);
PrimitiveSet ps = findPrimitiveSet(set);
Primitive prim = ps.get(id);
if(prim != null)
prims.put(id, prim);
return prim;
}
/** Execute a heraclitus operation.
* @param op This cell gives the operation, either by
* being the clone of a Heraclitus primitive
* or by having its root clone have an "exe-type".
* @param param The parameter to be given for the
* execution.
*/
public static ZZCell execute(ZZCell op, ZZCell param) {
Primitive p = findPrimitive(op);
p("Heraclitus exec: "+p);
if(p == null) {
ZZCell n = op.h("d.clone", -1);
p = findPrimitive(n);
if(p == null) {
ZZCell nx = op.h("d.exe-type", -1, true);
if(nx == null) {
throw new ZZError("No executable primitive");
}
// The core: create the new langcache level
ZZCell curs1 = op.getHomeCell().N();
ZZCell curs2 = curs1.N("d.1", 1);
ZZCursorReal.set(curs1, op);
ZZCursorReal.set(curs2, param);
try {
p("Heraclitus recurse: "+nx);
return execute(nx, curs1);
} finally {
// XXX ZZCursorReal -delete as well?!?!
ZZCursorReal.delete(curs1);
ZZCursorReal.delete(curs2);
curs1.delete();
curs2.delete();
}
}
}
return p.execute(op, param);
}
}

View File

@@ -0,0 +1,79 @@
/*
HeraclitusCommand.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho
*/
package org.gzigzag;
import java.awt.*;
/** ZZCommand interface to Heraclitus Clang.
*/
class HeraclitusCommand extends ZZCommand implements ZOb {
public static boolean dbg = false;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
static final void pa(String s) { ZZLogger.log(s); }
STRUCTPARAMS {
/** Heraclitus operation cell. */
ZZCell op
= null;
}
INITIALIZE {
if(op == null)
throw new ZZError("Must specify Heraclitus Clang operation!");
}
public void exec(ZZCell param) {
throw new ZZError("exec not implemented yet");
}
public void execCallback(
ZZCell target,
ZZView view,
ZZView cview,
String key,
Point pt,
ZZScene xi
) {
ZZCell curspar = op.N();
ZZCell curs1 = op.N();
ZZCell curs2 = curs1.N("d.1", 1);
ZZCursorReal.set(curs1, cview.getViewcell());
ZZCursorReal.set(curs2, view.getViewcell());
ZZCursorReal.set(curspar, curs1);
try {
p("Enter heraclitus: ");
org.gzigzag.heraclitus.HeraclitusClang.execute(op, curspar);
p("Exit heraclitus");
} finally {
ZZCursorReal.delete(curs1);
ZZCursorReal.delete(curs2);
ZZCursorReal.delete(curspar);
curs1.delete();
curs2.delete();
curspar.delete();
}
}
}
// vim: set syntax=java :

View File

@@ -0,0 +1,85 @@
/*
PS1.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.heraclitus;
import org.gzigzag.*;
import java.util.*;
/**
* The first primitive set for Heraclitus clang.
*/
public class PS1 implements PrimitiveSet {
public static final String rcsid = "$Id$";
public static boolean dbg = true;
static final void p(String s) { if(dbg) ZZLogger.log(s); }
public Primitive get(String id) {
if(id.equals("Move1Test"))
return new Move1Test();
return null;
}
/** First test: move a HERA1 (see ZZPrimitiveActions) datacursor one
* step pos on d.1.
*/
static public class Move1Test implements Primitive {
public ZZCell execute(ZZCell primitive, ZZCell cursorparam) {
p("Move1test");
ZZCell c0 = ZZCursorReal.get(cursorparam);
ZZCell c = c0.s("d.1", 1);
ZZCell v = ZZCursorReal.get(c);
ZZCell t0 = ZZCursorReal.get(v);
ZZCell t = t0.s("d.1", 1);
p("M1T: "+cursorparam+" "+c0+" "+c+" "+t0+" "+t);
ZZCursorReal.set(v, t);
return cursorparam;
}
}
/** A text translator: translates fixed strings into other fixed
* strings.
*/
/** Dereference the cursorparameter: expects the cursorparameter
* to point to another cursor, and makes it point to the place
* the other cursor pointed to.
*/
static public class Deref1 implements Primitive {
public ZZCell execute(ZZCell primitive, ZZCell cursorparam) {
ZZCell c = ZZCursorReal.get(cursorparam);
if(c == null) return null;
ZZCursorReal.set(cursorparam, ZZCursorReal.get(c));
return cursorparam;
}
}
/** Execute a sequence of cells with the same cursorparam.
*/
static public class Seq1 implements Primitive {
public ZZCell execute(ZZCell primitive, ZZCell cursorparam) {
// ZZCell root = cursorParam.
return cursorparam;
}
}
}

View File

@@ -0,0 +1,41 @@
/*
Primitive.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.heraclitus;
import org.gzigzag.*;
/** An Heraclitus Clang primitive.
* A primitive, just like a subroutine, executes one cell with another
* as a parameter, and returns another cell.
*/
public interface Primitive {
String rcsid = "$Id$";
/** Execute this primitive.
* @param instance The cell instance (clone) that was originally
* requested to be executed.
* @param param The parameter given to the execution.
*/
ZZCell execute(ZZCell instance, ZZCell param);
}

View File

@@ -0,0 +1,34 @@
/*
PrimitiveSet.java
*
* Copyright (c) 2000, Ted Nelson and Tuomas Lukka
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Tuomas Lukka
*/
package org.gzigzag.heraclitus;
import org.gzigzag.*;
/** A set of Heraclitus Clang primitives.
*/
public interface PrimitiveSet {
String rcsid = "$Id$";
Primitive get(String id);
}

View File

@@ -0,0 +1,52 @@
/*
DimensionNames.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales;
import org.gzigzag.*;
/** Parametrized names of Thales Clang special dimensions. This class
allows easy renaming of these dimensions should one become
necessary. */
class DimensionNames {
// Note: the members should not be final! (Allows substituting
// this class alone to a precompiled GZigZag.)
// syntax
public static String sequence = "d.thales-sequence";
public static String literal = "d.thales-literal";
public static String argument = "d.thales-argument";
public static String procedure = "d.thales-procedure";
public static String primitives = "d.thales-internal-primitives";
public static String primbinding = "d.thales-internal-binding";
public static String syntaxform = "d.thales-internal-syntactic-form";
public static String syntaxformlist = "d.thales-internal-syntactic-form-list";
// runtime
public static String tasks = "d.thales-rt-active-tasks";
public static String stack = "d.thales-rt-stack";
public static String stackptr = "d.thales-rt-stackptr";
public static String activations = "d.thales-rt-denoter-activations";
public static String environment = "d.thales-rt-environment";
public static String applicable = "d.thales-rt-applicable";
}

View File

@@ -0,0 +1,109 @@
/*
Task.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.*;
import org.gzigzag.*;
/** A runtime environment. It contains runtime instances of cell
denoters. Remember to register/unregister references (that are in
the structure (not Java references)). */
class Environment implements Refcounted {
private ZZCell cell;
public Environment(ZZCell cell) {
this.cell = cell.h(DimensionNames.environment);
}
public Environment(ZZCell firstparam, Environment parent) {
cell = firstparam.N(DimensionNames.activations, 1);
ZZCell prev = cell;
for (ZZCell c = cell.s(DimensionNames.argument, 1);
c != null;
c = c.s(DimensionNames.argument, 1)) {
ZZCell nc = c.N(DimensionNames.activations, 1);
prev.connect(DimensionNames.environment, 1, nc);
prev = nc;
}
if (parent != null) ZZCursorReal.set(first, parent.getCell());
cell.setReferenceCount("0");
parent.registerReference();
}
public ZZCell getCell() { return cell; }
protected int getReferenceCount() {
String text = cell.getText();
return Integer.parseInt(text);
}
protected void setReferenceCount(int rc) {
cell.setText("" + rc);
}
public void registerReference() {
setReferenceCount(getReferenceCount() + 1);
}
public void unregisterReference() {
setReferenceCount(getReferenceCount() - 1);
if (getReferenceCount() < 1) delete();
}
public void delete() {
ZZCell parent = ZZCursorReal.get(cell);
ZZCell c = cell;
while (c != null) {
ZZCell tmp = c;
c = c.s(DimensionNames.environment, 1);
tmp.delete();
}
if (parent != null) parent.unregisterReference();
}
protected ZZCell getDenoterInstance(ZZCell denoter) {
ZZCell ecell = cell;
ZZCell rv = null;
while (rv == null) {
rv = denoter.intersect(DimensionNames.activations, 1,
ecell, DimensionNames.environment, 1);
if (rv == null) {
ecell = ZZCursorReal.get(ecell);
if (ecell == null) {
throw new ZZError("accessing inaccessible denoter");
}
}
}
return rv;
}
public ZZCell getDenoterValue(ZZCell denoter) {
ZZCell dc = getDenoterInstance(denoter);
return ZZCursorReal.get(dc);
}
public void setDenoterValue(ZZCell denoter, ZZCell value) {
ZZCell dc = getDenoterInstance(denoter);
ZZCursorReal.set(dc, value);
}
}

View File

@@ -0,0 +1,62 @@
/*
Evaluator.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.*;
import org.gzigzag.*;
public class Evaluator {
private Task task;
public Evaluator() { this(null); }
public Evaluator(Task task) { this.task = task; }
public Environment getCurrentEnvironment() {
return task.getCurrentEnvironment();
}
public void setCurrentEnvironment(Environment e) {
task.setCurrentEnvironment(e);
}
public void request(SyntaxForm sf) {
task.push(sf.getCell());
sf.registerReference();
}
public void finish_eval(SyntaxForm sf) {
task.remove(sf.getCell());
sf.unregisterReference();
}
private final void eval_iter() {
ZZCell c = task.peek();
SyntaxForm sf = SyntaxForm.rt_instantiate(c, this);
sf.eval_iteration();
}
public void eval_iteration() { eval_iter(); }
public void eval() { while (!task.empty()) eval_iter(); }
}

View File

@@ -0,0 +1,32 @@
/*
PrimitiveOperation.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales;
import org.gzigzag.*;
interface PrimitiveOperation {
int parameterCount();
ZZCell invoke(ZZCell[] arguments);
}

4
Java/clang/thales/README Normal file
View File

@@ -0,0 +1,4 @@
This directory contains the reference implementation of Thales Clang.
Talk to Antti-Juhani Kaijanaho <gaia@iki.fi> before making any
nontrivial changes.

View File

@@ -0,0 +1,28 @@
/*
Task.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.*;
interface Refcounted {
void registerReference();
void unregisterReference();
}

View File

@@ -0,0 +1,168 @@
/*
SyntaxForm.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales;
import org.gzigzag.*;
import java.lang.reflect.*;
/** Syntax form. Subclasses must have at least two constructors: one
taking a ZZCell as an argument (the cell must be used to deduce
existing runtime state) and another taking two ZZCells as
arguments (the first cell must be used for parsing the expression,
the other cell must be understood to mean the cell that should be
updated to point to the result of the expression). */
public abstract class SyntaxForm implements Refcounted {
static protected ZZCell newRT(ZZSpace space, Class cl) {
String name = cl.getName();
name = name.substring(name.lastIndexOf(".")+1);
ZZCell home = space.getHomecell();
ZZCell namec = home.findText(DimensionNames.syntaxformlist, 1, name);
if (namec == null) {
namec = home.N(DimensionNames.syntaxformlist, 1);
namec.setText(name);
}
return namec.zzclone();
}
static public SyntaxForm rt_instantiate(ZZCell c, Evaluator etor) {
ZZCell sfc = c.h("d.clone");
if (sfc == null) throw new ZZError("no d.clone headcell");
if (sfc.h(DimensionNames.syntaxformlist) != sfc.getHomecell()) {
throw new ZZError("not in syntax form list");
}
Object o;
try {
Class cl = Class.forName("org.gzigzag.clang.thales.syntaxform"
+ name);
Class cellcl = Class.forName("org.gzigzag.ZZCell");
Constructor ctor = cl.getConstructor(new Class[] { cellcl });
o = ctor.newInstance(new Object[] { c } );
} catch (ClassNotFoundException e) {
throw new ZZError("syntax form not found (" + e + ")");
} catch (InstantiationException e) {
throw new ZZError("problems instantiating syntax form: " + e);
} catch (IllegalAccessException e) {
throw new ZZError("" + e);
}
if (!(o instanceof SyntaxForm)) throw new InvalidException();
SyntaxForm rv = (SyntaxForm) o;
rv.etor = etor;
return rv;
}
static public SyntaxForm code_instantiate(ZZCell code, ZZCell rv,
Evaluator etor) {
ZZCell sfc = code.h(DimensionNames.syntaxform);
if (sfc == null) throw new ZZError("invalid syntax form");
Object o;
try {
Class cl = Class.forName("org.gzigzag.clang.thales.syntaxform"
+ sfc);
Class cellcl = Class.forName("org.gzigzag.ZZCell");
Constructor ctor = cl.getConstructor(new Class[] { cellcl, cellcl });
o = ctor.newInstance(new Object[] { c, rv } );
} catch (ClassNotFoundException e) {
throw new ZZError("syntax form not found (" + e + ")");
} catch (InstantiationException e) {
throw new ZZError("problems instantiating syntax form: " + e);
} catch (IllegalAccessException e) {
throw new ZZError("" + e);
}
if (!(o instanceof SyntaxForm)) throw new InvalidException();
SyntaxForm rv = (SyntaxForm)o;
rv.etor = etor;
return rv;
}
private ZZCell base;
private Evaluator etor = null;
/** Construct oneself using an existing syntax form runtime
cell. */
protected SyntaxForm(ZZCell c) {
base = c;
}
/** Construct oneself using a new syntax form runtime cell. */
protected SyntaxForm(ZZSpace s) {
this(SyntaxForm.newRT(s, this.getClass()));
}
protected SyntaxForm code_instantiate(ZZCell code, ZZCell rv) {
SyntaxForm.code_instantiate(code, rv, etor);
}
protected final ZZCell getBaseCell() { return base; }
protected final void requestEval(SyntaxForm sf) { etor.request(sf); }
protected final void finishEval() { etor.finishEval(this); }
protected final void setCurrentEnvironment(Environment e) {
etor.setCurrentEnvironment(e);
}
protected final Environment getCurrentEnvironment() {
return etor.getCurrentEnvironment();
}
public abstract void evalIteration();
/** Delete the runtime representation unconditionally. */
protected abstract void delete();
private int getReferenceCount() {
String text = cell.getText();
if (text.equals("")) {
setReferenceCount(0);
return getReferenceCount();
}
return Integer.parseInt(text);
}
private void setReferenceCount(int rc) {
cell.setText("" + rc);
}
public final void registerReference() {
setReferenceCount(getReferenceCount() + 1);
}
public final void unregisterReference() {
setReferenceCount(getReferenceCount() - 1);
if (getReferenceCount() < 1) {
delete();
cell.delete();
}
}
}

View File

@@ -0,0 +1,81 @@
/*
Task.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.*;
import org.gzigzag.*;
class Task {
private ZZCell cell;
public Environment getCurrentEnvironment() {
return new Environment(ZZCursorReal.get(cell));
}
public void setCurrentEnvironment(Environment e) {
ZZCursorReal.set(cell, e.getCell());
}
public void push(ZZCell c) {
ZZCell ct = peek();
if (ct != null) {
c.connect(DimensionNames.stack, 1, ct);
cell.disconnect(DimensionNames.stackptr, 1);
}
cell.connect(DimensionNames.stackptr, 1, c);
}
public void pop() {
ZZCell ct = peek();
if (ct == null) throw ZZError("empty Thales Clang runtime stack");
ZZCell nt = ct.s(DimensionNames.stack, 1);
cell.disconnect(DimensionNames.stackptr, 1);
if (nt != null) cell.connect(DimensionNames.stackptr, 1, nt);
nt.disconnect(DimensionNames.stack, -1);
}
public ZZCell peek() {
return cell.s(DimensionNames.stackptr, 1);
}
public void remove(ZZCell c) {
{
ZZCell nei = c.s(DimensionNames.stackptr, -1);
if (nei.equals(cell)) {
pop();
return;
}
}
for (ZZCell i = peek(); i != null && !i.equals(c);
i = i.s(DimensionNames.stack, 1)) {
// nothing
}
if (i == null) throw ZZError("cannot remove that which is not there");
c.deleteFromRank(DimensionNames.stack);
}
public boolean empty(ZZCell c) {
return peek() == null;
}
}

View File

@@ -0,0 +1,46 @@
/*
ThalesClang.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.module;
import org.gzigzag.*;
import java.awt.*;
import org.gzigzag.clang.thales.*;
public class ThalesClang {
static public ZZModule module = new ZZModule() {
public void action(String id,
ZZCell code,
ZZCell target,
ZZView view, ZZView cview,
String key, Point pt, ZZScene xi) {
ZZSpace space = code.getSpace();
if (id.equals("RELOAD")) {
Primitive.readPrimitivesFromStructure(code.getSpace());
} else if (id.equals("INITIALIZE")) {
Primitive.register(space, "If");
} else {
throw new ZZError("unknown ThalesClang module command");
}
}
};
}

View File

@@ -0,0 +1,45 @@
/*
ThalesProgram.zob
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.*;
import org.gzigzag.clang.thales.*;
class ThalesProgram extends ZZCommand implements ZOb {
STRUCTPARAM {
ZZCell body;
}
public void execCallback(
ZZCell target,
ZZView view,
ZZView cview,
String key,
Point pt,
ZZScene xi
) {
}
}

View File

@@ -0,0 +1,28 @@
/*
If.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.primitive;
import org.gzigzag.clang.thales.*;
public class If extends Primitive {
}

View File

@@ -0,0 +1,55 @@
/*
Primitive.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.syntaxform;
import org.gzigzag.*;
import org.gzigzag.clang.thales.*;
public class Abstraction extends SyntaxForm {
public static final String rcsid = "$Id: Abstraction.java,v 1.4 2000/10/18 14:35:31 tjl Exp $";
// Runtime representation basecell -> rvcc -> bodyc -> envc posward on d.1
private final class Rep {
public ZZCell rvcc;
public ZZCell rvc;
public ZZCell bodyc;
public ZZCell body;
public ZZCell envc; // pointer to inherited environment
public Environment env; // dereferenced envc
/** Create from existing structure. */
public Rep() {
rvcc = getBaseCell().s("d.1", 1);
rvc = ZZCursorReal.get(rvcc);
bodyc = rvcc.s("d.1", 1);
body = ZZCursorReal.get(bodyc);
envc = bodyc.s("d.1", 1);
env = new Environment(ZZCursorReal.get(envc));
}
public Rep(ZZCell body, Environment env, ZZCell rvc
}
}

View File

@@ -0,0 +1,82 @@
/*
CellDenoter.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.syntaxform;
import org.gzigzag.*;
import org.gzigzag.clang.thales.*;
class CellDenoter extends SyntaxForm {
// Runtime representation: basecell -> rvcc -> instc on d.1 posward
private final class Rep {
public ZZCell rvcc; // pointer to return value pointer
public ZZCell rvc; // dereferenced rvcc
public ZZCell instc; // Pointer to runtime instance of the denoter
public ZZCell inst; // dereferenced instc
/** Create from existing structure. */
public Rep() {
rvcc = getBaseCell().s("d.1", 1);
rvc = ZZCursorReal.get(rvcc);
instc = rvcc.s("d.1", 1);
inst = ZZCursorReal.get(instc);
}
public Rep(ZZCell inst, ZZCell rvc) {
this.rvc = rvc;
this.inst = inst;
rvcc = getBaseCell().N("d.1", 1);
instc = rvcc.N("d.1", 1);
ZZCursorReal.set(rvcc, rvc);
ZZCursorReal.set(instc, inst);
Environment env = new Environment(inst);
env.registerReference();
}
public void delete() {
rvcc.delete();
instc.delete();
Environment env = new Environment(inst);
env.unregisterReference();
}
}
public CellDenoter(ZZCell c, Evaluator etor) { super(c, etor); }
public CellDenoter(ZZCell expr, ZZCell rvc, Evaluator etor) {
super(SyntaxForm.newRT(rvc.getSpace(), this.getClass()), etor);
Environment env = getCurrentEnvironment();
ZZCell instance = env.getDenoterInstance(expr);
Rep rep(instance, rvc);
}
public void evalIteration() {
Rep rep;
ZZCursorReal.set(rep.rvc, ZZCursorReal.get(rep.inst));
finishEval();
}
public void delete() {
Rep rep;
rep.delete();
}
}

View File

@@ -0,0 +1,75 @@
/*
CellLiteral.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.syntaxform;
import org.gzigzag.*;
import org.gzigzag.clang.thales.*;
class CellLiteral extends SyntaxForm {
// Runtime representation: basecell -> rvcc -> litc on d.1 posward
private final class Rep {
public ZZCell rvcc; // pointer to the return value pointer
public ZZCell rvc; // dereferenced rvcc
public ZZCell litc; // pointer to literal value
/** Create from existing structure. */
public Rep() {
rvcc = getBaseCell().s("d.1", 1);
rvc = ZZCursorReal.get(rvcc);
litc = rvcc.s("d.1", 1);
}
/** Create also a new structure. */
public Rep(ZZCell lit, ZZCell rvc) {
this.rvc = rvc;
rvcc = getBaseCell().N("d.1", 1);
ZZCursorReal.set(rvcc, rvc);
litc = rvcc.N("d.1", 1);
ZZCursorReal.set(litc, lit);
}
public void delete() {
rvcc.delete();
litc.delete();
}
}
public CellLiteral(ZZCell c, Evaluator etor) {
super(c, etor);
}
public CellLiteral(ZZCell expr, ZZCell rvc, Evaluator etor) {
super(SyntaxForm.newRT(rvc.getSpace(), this.getClass()), etor);
Rep rep(ZZCursorReal.get(expr), rvc);
}
public void evalIteration() {
Rep rep;
ZZCursorReal.set(rep.rvc, ZZCursorReal.get(rep.litc));
finishEval();
}
protected void delete() {
Rep rep;
rep.delete();
}
}

View File

@@ -0,0 +1,191 @@
/*
Primitive.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.syntaxform;
import org.gzigzag.*;
import org.gzigzag.clang.thales.*;
import java.util.*;
public class Primitive extends SyntaxForm {
public static final String rcsid = "$Id: Primitive.java,v 1.7 2000/10/18 14:35:31 tjl Exp $";
public static boolean dbg = false;
final static void p(String s) { if(dbg) System.out.println(s); }
final static void pa(String s) { System.out.println(s); }
// Runtime representation basecell -> rvcc -> primc posward on d.1
private final class Rep {
public ZZCell rvcc; // pointer to return value pointer
public ZZCell rvc; // dereferenced rvcc
public ZZCell primc; // pointer to the primitive cell
public ZZCell prim; // the primitive cell
/** Create from existing structure. */
public Rep() {
rvcc = getBaseCell().s("d.1", 1);
rvc = ZZCursorReal.get(rvcc);
primc = rvcc.s("d.1", 1);
prim = ZZCursorReal.get(rvc);
}
/** Create also a new structure. */
public Rep(ZZCell prim, ZZCell rvc) {
this.rvc = rvc;
rvcc = getBaseCell().N("d.1", 1);
ZZCursorReal.set(rvcc, rvc);
this.prim = prim;
primc = rvcc.N("d.1", 1);
ZZCursorReal.set(primc, prim);
}
public void delete() {
rvcc.delete();
primc.delete();
}
}
public Primitive(ZZCell c) { super(c); }
public Primitive(ZZCell expr, ZZCell rvc) {
super(rvc.getSpace());
Rep rep(expr.h("d.clone", -1), rvc);
}
public void evalIteration() {
Rep rep;
ZZCell rv = rep.rvc.N();
rv.connect(DimensionNames.applicable, 1, rep.prim);
ZZCursorReal.set(rep.rvc, rv);
finishEval();
}
public void delete() {
Rep rep;
rep.delete();
}
static private Hashtable prims = new Hashtable();
static private Hashtable optocell = new Hashtable();
static public PrimitiveOperation get(ZZCell c) {
ZZCell id = c.h("d.clone", -1);
synchronized (prims) {
return (PrimitiveOperation) prims.get(id);
}
}
static public class PrimitiveProblemException extends ZZError {
public PrimitiveProblemException(String s) {
super(s);
}
}
static public class DuplicateException extends PrimitiveProblemException {
public DuplicateException() {
this("duplicate Thales Clang primitive cell");
}
public DuplicateException(String s) {
super(s);
}
}
static public class InvalidException extends PrimitiveProblemException {
public InvalidException() {
this("invalid Thales Clang primitive cell");
}
public InvalidException(String s) {
super(s);
}
}
static private void readAPrimitive(ZZCell c) {
ZZCell op = c.s(DimensionNames.primbinding, 1);
if (op == null) throw new InvalidException();
Object o;
try {
Class cl = Class.forName("org.gzigzag.clang.thales.primitive." + op.getText());
o = cl.newInstance();
} catch (ClassNotFoundException e) {
throw new InvalidException("primitive not found (" + e + ")");
} catch (InstantiationException e) {
throw new InvalidException("problems instantiating primitive: " + e);
} catch (IllegalAccessException e) {
throw new InvalidException("" + e);
}
if (!(o instanceof PrimitiveOperation)) throw new InvalidException();
// XXX: is this necessary or desirable?
if (optocell.containsKey(o)) throw new DuplicateException();
synchronized (prims) {
prims.put(c, o);
optocell.put(o, c);
}
}
static public void readPrimitivesFromStructure(ZZSpace space) {
ZZCell home = space.getHomeCell();
synchronized (prims) {
for (ZZCell c = home.s(DimensionNames.primitives, 1);
c != null;
c = c.s(DimensionNames.primitives, 1)) {
try {
readAPrimitive(c);
} catch (PrimitiveProblemException e) {
pa("" + e);
}
}
}
}
static public void register(ZZSpace space, String mc) {
ZZCell home = space.getHomeCell();
synchronized (prims) {
ZZCell id = home.N();
ZZCell name = id.N(DimensionNames.primbinding, 1);
name.setText(mc);
try {
readAPrimitive(id);
} catch (ZZError e) {
id.delete();
name.delete();
throw e;
}
home.insert(DimensionNames.primitives, 1, id);
}
}
}

View File

@@ -0,0 +1,95 @@
/*
Sequence.java
*
* You may use and distribute under the terms of either the GNU Lesser
* General Public License, either version 2 of the license or,
* at your choice, any later version. Alternatively, you may use and
* distribute under the terms of the XPL.
*
* See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of
* the licenses.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README
* file for more details.
*
*/
/*
* Written by Antti-Juhani Kaijanaho.
*/
package org.gzigzag.clang.thales.syntaxform;
import org.gzigzag.*;
import org.gzigzag.clang.thales.*;
class Sequence extends SyntaxForm {
// Runtime representation: basecell -> rvcc -> tmprc -> csec on
// d.1 posward
private final class Rep {
public ZZCell rvcc; // pointer to return value pointer
public ZZCell rvc; // dereferenced rvcc
public ZZCell tmprc; // temporary holder of component expression results
public ZZCell tmpr; // dereferenced tmprc
public ZZCell csec; // pointer to the current subexpression in code
public ZZCell cse; // current subexpression in code
/** Create from existing structure. */
public Rep() {
rvcc = getBaseCell().s("d.1", 1);
rvc = ZZCursorReal.get(rvcc);
tmprc = rvcc.s("d.1", 1);
tmpr = ZZCursorReal.get(tmprc);
csec = tmprc.s("d.1", 1);
cse = ZZCursorReal.get(csec);
}
/** Create also a new structure. */
public Rep(ZZCell fse, ZZCell rvc) {
rvcc = getBaseCell().N("d.1", 1);
ZZCursorReal.set(rvcc, rvc);
this.rvc = rvc;
tmprc = rvcc.N("d.1", 1);
tmpr = null;
csec = tmprc.N("d.1", 1);
ZZCursorReal.set(csec, fse);
cse = fse;
}
public void delete() {
rvcc.delete();
tmprc.delete();
csec.delete();
}
public Sequence(ZZCell c) { super(c); }
public Sequence(ZZCell expr, ZZCell rvc) {
super(rvc.getSpace());
ZZCell fse = expr.s(DimensionNames.sequence, 1);
if (fse == null) {
throw new SyntaxError("sequence must have at least one element");
}
Rep rep(fse, rvc);
}
public void evalIteration() {
Rep rep;
if (rep.cse == null) {
ZZCursorReal.set(rvc, tmpr);
finishEval();
}
SyntaxForm sf = code_instantiate(rep.cse, tmprc);
requestEval(sf);
ZZCursorReal.set(rep.csec,
rep.cse.s(DimensionNames.sequence, 1));
}
}
}