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

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));
}
}
}